-
Notifications
You must be signed in to change notification settings - Fork 3
/
mips.cc
588 lines (520 loc) · 20 KB
/
mips.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/* File: mips.cc
* -------------
* Implementation of Mips class, which is responsible for TAC->MIPS
* translation, register allocation, etc.
*
* Julie Zelenski academic year 2001-02 for CS143
* Loosely based on some earlier work by Steve Freund
*
* A simple final code generator to translate Tac to MIPS.
* It uses simplistic algorithms, in particular, its register handling
* and spilling strategy is inefficient to the point of begin mocked
* by elementary school children.
*/
#include "mips.h"
#include <stdarg.h>
#include <string.h>
/* Method: GetRegister
* -------------------
* Given a location for a current var, a reason (ForRead or ForWrite)
* and up to two registers to avoid, this method will assign
* to a var to register trying these alternatives in order:
* 1) if that var is already in a register ("same" is determined
* by matching name and same scope), we use that one
* 2) find an empty register to use for the var
* 3) find an in-use register that is not dirty. We don't need
* to write value back to memory since it's clean, so we just
* replace with the new var
* 4) spill an in-use, dirty register, by writing its contents to
* memory and then replace with the new var
* For steps 3 & 4, we respect the registers given to avoid (ie the
* other operands in this operation). The register descriptors are
* updated to show the new state of the world. If for read, we
* load the current value from memory into the register. If for
* write, we mark the register as dirty (since it is getting a
* new value).
*/
Mips::Register Mips::GetRegister(Location *var, Reason reason,
Register avoid1, Register avoid2)
{
Register reg;
if (!FindRegisterWithContents(var, reg)) {
if (!FindRegisterWithContents(NULL, reg)) {
reg = SelectRegisterToSpill(avoid1, avoid2);
SpillRegister(reg);
}
regs[reg].var = var;
if (reason == ForRead) { // load current value
Assert(var->GetOffset() % 4 == 0); // all variables are 4 bytes
const char *offsetFromWhere = var->GetSegment() == fpRelative? regs[fp].name : regs[gp].name;
Emit("lw %s, %d(%s)\t# load %s from %s%+d into %s", regs[reg].name,
var->GetOffset(), offsetFromWhere, var->GetName(),
offsetFromWhere, var->GetOffset(), regs[reg].name);
regs[reg].isDirty = false;
}
}
if (reason == ForWrite)
regs[reg].isDirty = true;
return reg;
}
// Two covers for the above method to make it slightly more
// convenient to call it
Mips::Register Mips::GetRegister(Location *var, Register avoid1)
{
return GetRegister(var, ForRead, avoid1, zero);
}
Mips::Register Mips::GetRegisterForWrite(Location *var, Register avoid1,
Register avoid2)
{
return GetRegister(var, ForWrite, avoid1, avoid2);
}
// Helper to check if two variable locations are one and the same
// (same name, segment, and offset)
static bool LocationsAreSame(Location *var1, Location *var2)
{
return (var1 == var2 ||
(var1 && var2
&& !strcmp(var1->GetName(), var2->GetName())
&& var1->GetSegment() == var2->GetSegment()
&& var1->GetOffset() == var2->GetOffset()));
}
/* Method: FindRegisterWithContents
* --------------------------------
* Searches the descriptors for one with contents var. Assigns
* register by reference, and returns true/false on whether match found.
*/
bool Mips::FindRegisterWithContents(Location *var, Register& reg)
{
for (reg = zero; reg < NumRegs; reg = Register(reg+1))
if (regs[reg].isGeneralPurpose && LocationsAreSame(var, regs[reg].var))
return true;
return false;
}
/* Method: SelectRegisterToSpill
* -----------------------------
* Chooses an in-use register to replace with a new variable. We
* prefer to replace a non-dirty once since we would not have to
* write its contents back to memory, so the first loop searches
* for a clean one. If none found, we take a dirty one. In both
* loops we deliberately won't choose either of the registers we
* were asked to avoid. We track the last dirty register spilled
* and advance on each subsequent spill as a primitive means of
* trying to not throw out things we just loaded and thus are likely
* to need.
*/
Mips::Register Mips::SelectRegisterToSpill(Register avoid1, Register avoid2)
{
// first hunt for a non-dirty one, since no work to spill
for (Register i = zero; i < NumRegs; i = (Register)(i+1)) {
if (i != avoid1 && i != avoid2 && regs[i].isGeneralPurpose &&
!regs[i].isDirty)
return i;
}
do { // otherwise just pick the next usuable register
lastUsed = (Register)((lastUsed + 1) % NumRegs);
} while (lastUsed == avoid1 || lastUsed == avoid2 ||
!regs[lastUsed].isGeneralPurpose);
return lastUsed;
}
/* Method: SpillRegister
* ---------------------
* "Empties" register. If variable is currently slaved in this register
* and its contents are out of synch with memory (isDirty), we write back
* the current contents to memory. We then clear the descriptor so we
* realize the register is empty.
*/
void Mips::SpillRegister(Register reg)
{
Location *var = regs[reg].var;
if (var && regs[reg].isDirty) {
const char *offsetFromWhere = var->GetSegment() == fpRelative? regs[fp].name : regs[gp].name;
Assert(var->GetOffset() % 4 == 0); // all variables are 4 bytes in size
Emit("sw %s, %d(%s)\t# spill %s from %s to %s%+d", regs[reg].name,
var->GetOffset(), offsetFromWhere, var->GetName(), regs[reg].name,
offsetFromWhere,var->GetOffset());
}
regs[reg].var = NULL;
}
/* Method: SpillAllDirtyRegisters
* ------------------------------
* Used before flow of control change (branch, label, jump, etc.) to
* save contents of all dirty registers. This synchs the contents of
* the registers with the memory locations for the variables.
*/
void Mips::SpillAllDirtyRegisters()
{
Register i;
for (i = zero; i < NumRegs; i = Register(i+1))
if (regs[i].var && regs[i].isDirty) break;
if (i != NumRegs) // none are dirty, don't print message to avoid confusion
Emit("# (save modified registers before flow of control change)");
for (i = zero; i < NumRegs; i = Register(i+1))
SpillRegister(i);
}
/* Method: SpillForEndFunction
* ---------------------------
* Slight optimization on the above method used when spilling for
* end of function (return/fall off). In such a case, we do not
* need to save values for locals, temps, and parameters because the
* function is about to exit and those variables are going away
* immediately, so no need to bother with updating contents.
*/
void Mips::SpillForEndFunction()
{
for (Register i = zero; i < NumRegs; i = Register(i+1)) {
if (regs[i].isGeneralPurpose && regs[i].var) {
if (regs[i].var->GetSegment() == gpRelative)
SpillRegister(i);
else // all stack variables can just be tossed at end func
regs[i].var = NULL;
}
}
}
/* Method: Emit
* ------------
* General purpose helper used to emit assembly instructions in
* a reasonable tidy manner. Takes printf-style formatting strings
* and variable arguments.
*/
void Mips::Emit(const char *fmt, ...)
{
va_list args;
char buf[1024];
va_start(args, fmt);
vsprintf(buf, fmt, args);
va_end(args);
if (buf[strlen(buf) - 1] != ':') printf("\t"); // don't tab in labels
if (buf[0] != '#') printf(" "); // outdent comments a little
printf("%s", buf);
if (buf[strlen(buf)-1] != '\n') printf("\n"); // end with a newline
}
/* Method: EmitLoadConstant
* ------------------------
* Used to assign variable an integer constant value. Slaves dst into
* a register (using GetRegister above) and then emits an li (load
* immediate) instruction with the constant value.
*/
void Mips::EmitLoadConstant(Location *dst, int val)
{
Register reg = GetRegisterForWrite(dst);
Emit("li %s, %d\t\t# load constant value %d into %s", regs[reg].name,
val, val, regs[reg].name);
}
/* Method: EmitLoadStringConstant
* ------------------------------
* Used to assign a variable a pointer to string constant. Emits
* assembly directives to create a new null-terminated string in the
* data segment and assigns it a unique label. Slaves dst into a register
* and loads that label address into the register.
*/
void Mips::EmitLoadStringConstant(Location *dst, const char *str)
{
static int strNum = 1;
char label[16];
sprintf(label, "_string%d", strNum++);
Emit(".data\t\t\t# create string constant marked with label");
Emit("%s: .asciiz %s", label, str);
Emit(".text");
EmitLoadLabel(dst, label);
}
/* Method: EmitLoadLabel
* ---------------------
* Used to load a label (ie address in text/data segment) into a variable.
* Slaves dst into a register and emits an la (load address) instruction
*/
void Mips::EmitLoadLabel(Location *dst, const char *label)
{
Register reg = GetRegisterForWrite(dst);
Emit("la %s, %s\t# load label", regs[reg].name, label);
}
/* Method: EmitCopy
* ----------------
* Used to copy the value of one variable to another. Slaves both
* src and dst into registers and then emits a move instruction to
* copy the contents from src to dst.
*/
void Mips::EmitCopy(Location *dst, Location *src)
{
Register rSrc = GetRegister(src), rDst = GetRegisterForWrite(dst, rSrc);
Emit("move %s, %s\t\t# copy value", regs[rDst].name, regs[rSrc].name);
}
/* Method: EmitLoad
* ----------------
* Used to assign dst the contents of memory at the address in reference,
* potentially with some positive/negative offset (defaults to 0).
* Slaves both ref and dst to registers, then emits a lw instruction
* using constant-offset addressing mode y(rx) which accesses the address
* at an offset of y bytes from the address currently contained in rx.
*/
void Mips::EmitLoad(Location *dst, Location *reference, int offset)
{
Register rSrc = GetRegister(reference), rDst = GetRegisterForWrite(dst, rSrc);
Emit("lw %s, %d(%s) \t# load with offset", regs[rDst].name,
offset, regs[rSrc].name);
}
/* Method: EmitStore
* -----------------
* Used to write value to memory at the address in reference,
* potentially with some positive/negative offset (defaults to 0).
* Slaves both ref and dst to registers, then emits a sw instruction
* using constant-offset addressing mode y(rx) which writes to the address
* at an offset of y bytes from the address currently contained in rx.
*/
void Mips::EmitStore(Location *reference, Location *value, int offset)
{
Register rVal = GetRegister(value), rRef = GetRegister(reference, rVal);
Emit("sw %s, %d(%s) \t# store with offset",
regs[rVal].name, offset, regs[rRef].name);
}
/* Method: EmitBinaryOp
* --------------------
* Used to perform a binary operation on 2 operands and store result
* in dst. All binary forms for arithmetic, logical, relational, equality
* use this method. Slaves both operands and dst to registers, then
* emits the appropriate instruction by looking up the mips name
* for the particular op code.
*/
void Mips::EmitBinaryOp(BinaryOp::OpCode code, Location *dst,
Location *op1, Location *op2)
{
Register rLeft = GetRegister(op1), rRight = GetRegister(op2, rLeft);
Register rDst = GetRegisterForWrite(dst, rLeft, rRight);
Emit("%s %s, %s, %s\t", NameForTac(code), regs[rDst].name,
regs[rLeft].name, regs[rRight].name);
}
/* Method: EmitLabel
* -----------------
* Used to emit label marker. Before a label, we spill all registers since
* we can't be sure what the situation upon arriving at this label (ie
* starts new basic block), and rather than try to be clever, we just
* wipe the slate clean.
*/
void Mips::EmitLabel(const char *label)
{
SpillAllDirtyRegisters();
Emit("%s:", label);
}
/* Method: EmitGoto
* ----------------
* Used for an unconditional transfer to a named label. Before a goto,
* we spill all registers, since we don't know what the situation is
* we are heading to (ie this ends current basic block) and rather than
* try to be clever, we just wipe slate clean.
*/
void Mips::EmitGoto(const char *label)
{
SpillAllDirtyRegisters();
Emit("b %s\t\t# unconditional branch", label);
}
/* Method: EmitIfZ
* ---------------
* Used for a conditional branch based on value of test variable.
* We slave test var to register and use in the emitted test instruction,
* either beqz. See comments above on Goto for why we spill
* all registers here.
*/
void Mips::EmitIfZ(Location *test, const char *label)
{
Register testReg = GetRegister(test);
SpillAllDirtyRegisters();
Emit("beqz %s, %s\t# branch if %s is zero ", regs[testReg].name, label,
test->GetName());
}
/* Method: EmitParam
* -----------------
* Used to push a parameter on the stack in anticipation of upcoming
* function call. Decrements the stack pointer by 4. Slaves argument into
* register and then stores contents to location just made at end of
* stack.
*/
void Mips::EmitParam(Location *arg)
{
Emit("subu $sp, $sp, 4\t# decrement sp to make space for param");
Register reg = GetRegister(arg);
Emit("sw %s, 4($sp)\t# copy param value to stack", regs[reg].name);
}
/* Method: EmitCallInstr
* ---------------------
* Used to effect a function call. All necessary arguments should have
* already been pushed on the stack, this is the last step that
* transfers control from caller to callee. See comments on Goto method
* above for why we spill all registers before making the jump. We issue
* jal for a label, a jalr if address in register. Both will save the
* return address in $ra. If there is an expected result passed, we slave
* the var to a register and copy function return value from $v0 into that
* register.
*/
void Mips::EmitCallInstr(Location *result, const char *fn, bool isLabel)
{
SpillAllDirtyRegisters();
Emit("%s %-15s\t# jump to function", isLabel? "jal": "jalr", fn);
if (result != NULL) {
Register r1 = GetRegisterForWrite(result);
Emit("move %s, %s\t\t# copy function return value from $v0", regs[r1].name, regs[v0].name);
}
}
// Two covers for the above method for specific LCall/ACall variants
void Mips::EmitLCall(Location *dst, const char *label)
{
EmitCallInstr(dst, label, true);
}
void Mips::EmitACall(Location *dst, Location *fn)
{
EmitCallInstr(dst, regs[GetRegister(fn)].name, false);
}
/*
* We remove all parameters from the stack after a completed call
* by adjusting the stack pointer upwards.
*/
void Mips::EmitPopParams(int bytes)
{
if (bytes != 0)
Emit("add $sp, $sp, %d\t# pop params off stack", bytes);
}
/* Method: EmitReturn
* ------------------
* Used to emit code for returning from a function (either from an
* explicit return or falling off the end of the function body).
* If there is an expression to return, we slave that variable into
* a register and move its contents to $v0 (the standard register for
* function result). Before exiting, we spill dirty registers (to
* commit contents of slaved registers to memory, necessary for
* consistency, see comments at SpillForEndFunction above). We also
* do the last part of the callee's job in function call protocol,
* which is to remove our locals/temps from the stack, remove
* saved registers ($fp and $ra) and restore previous values of
* $fp and $ra so everything is returned to the state we entered.
* We then emit jr to jump to the saved $ra.
*/
void Mips::EmitReturn(Location *returnVal)
{
if (returnVal != NULL)
Emit("move $v0, %s\t\t# assign return value into $v0",
regs[GetRegister(returnVal)].name);
SpillForEndFunction();
Emit("move $sp, $fp\t\t# pop callee frame off stack");
Emit("lw $ra, -4($fp)\t# restore saved ra");
Emit("lw $fp, 0($fp)\t# restore saved fp");
Emit("jr $ra\t\t# return from function");
}
/* Method: EmitBeginFunction
* -------------------------
* Used to handle the callee's part of the function call protocol
* upon entering a new function. We decrement the $sp to make space
* and then save the current values of $fp and $ra (since we are
* going to change them), then set up the $fp and bump the $sp down
* to make space for all our locals/temps.
*/
void Mips::EmitBeginFunction(int stackFrameSize)
{
Assert(stackFrameSize >= 0);
Emit("subu $sp, $sp, 8\t# decrement sp to make space to save ra, fp");
Emit("sw $fp, 8($sp)\t# save fp");
Emit("sw $ra, 4($sp)\t# save ra");
Emit("addiu $fp, $sp, 8\t# set up new fp");
if (stackFrameSize != 0)
Emit("subu $sp, $sp, %d\t# decrement sp to make space for locals/temps",
stackFrameSize);
}
/* Method: EmitEndFunction
* -----------------------
* Used to end the body of a function. Does an implicit return in fall off
* case to clean up stack frame, return to caller etc. See comments on
* EmitReturn above.
*/
void Mips::EmitEndFunction()
{
Emit("# (below handles reaching end of fn body with no explicit return)");
EmitReturn(NULL);
}
/* Method: EmitVTable
* ------------------
* Used to layout a vtable. Uses assembly directives to set up new
* entry in data segment, emits label, and lays out the function
* labels one after another.
*/
void Mips::EmitVTable(const char *label, List<const char*> *methodLabels)
{
Emit(".data");
Emit(".align 2");
Emit("%s:\t\t# label for class %s vtable", label, label);
for (int i = 0; i < methodLabels->NumElements(); i++)
Emit(".word %s\n", methodLabels->Nth(i));
Emit(".text");
}
/* Method: EmitPreamble
* --------------------
* Used to emit the starting sequence needed for a program. Not much
* here, but need to indicate what follows is in text segment and
* needs to be aligned on word boundary. main is our only global symbol.
*/
void Mips::EmitPreamble()
{
Emit("# standard Decaf preamble ");
Emit(".text");
Emit(".align 2");
Emit(".globl main");
}
/* Method: NameForTac
* ------------------
* Returns the appropriate MIPS instruction (add, seq, etc.) for
* a given BinaryOp:OpCode (BinaryOp::Add, BinaryOp:Equals, etc.).
* Asserts if asked for name of an unset/out of bounds code.
*/
const char *Mips::NameForTac(BinaryOp::OpCode code)
{
Assert(code >=0 && code < BinaryOp::NumOps);
const char *name = mipsName[code];
Assert(name != NULL);
return name;
}
/* Constructor
* ----------
* Constructor sets up the mips names and register descriptors to
* the initial starting state.
*/
Mips::Mips() {
mipsName[BinaryOp::Add] = "add";
mipsName[BinaryOp::Sub] = "sub";
mipsName[BinaryOp::Mul] = "mul";
mipsName[BinaryOp::Div] = "div";
mipsName[BinaryOp::Mod] = "rem";
mipsName[BinaryOp::Eq] = "seq";
mipsName[BinaryOp::Less] = "slt";
mipsName[BinaryOp::And] = "and";
mipsName[BinaryOp::Or] = "or";
regs[zero] = (RegContents){false, NULL, "$zero", false};
regs[at] = (RegContents){false, NULL, "$at", false};
regs[v0] = (RegContents){false, NULL, "$v0", false};
regs[v1] = (RegContents){false, NULL, "$v1", false};
regs[a0] = (RegContents){false, NULL, "$a0", false};
regs[a1] = (RegContents){false, NULL, "$a1", false};
regs[a2] = (RegContents){false, NULL, "$a2", false};
regs[a3] = (RegContents){false, NULL, "$a3", false};
regs[k0] = (RegContents){false, NULL, "$k0", false};
regs[k1] = (RegContents){false, NULL, "$k1", false};
regs[gp] = (RegContents){false, NULL, "$gp", false};
regs[sp] = (RegContents){false, NULL, "$sp", false};
regs[fp] = (RegContents){false, NULL, "$fp", false};
regs[ra] = (RegContents){false, NULL, "$ra", false};
regs[t0] = (RegContents){false, NULL, "$t0", true};
regs[t1] = (RegContents){false, NULL, "$t1", true};
regs[t2] = (RegContents){false, NULL, "$t2", true};
regs[t3] = (RegContents){false, NULL, "$t3", true};
regs[t4] = (RegContents){false, NULL, "$t4", true};
regs[t5] = (RegContents){false, NULL, "$t5", true};
regs[t6] = (RegContents){false, NULL, "$t6", true};
regs[t7] = (RegContents){false, NULL, "$t7", true};
regs[t8] = (RegContents){false, NULL, "$t8", true};
regs[t9] = (RegContents){false, NULL, "$t9", true};
regs[s0] = (RegContents){false, NULL, "$s0", true};
regs[s1] = (RegContents){false, NULL, "$s1", true};
regs[s2] = (RegContents){false, NULL, "$s2", true};
regs[s3] = (RegContents){false, NULL, "$s3", true};
regs[s4] = (RegContents){false, NULL, "$s4", true};
regs[s5] = (RegContents){false, NULL, "$s5", true};
regs[s6] = (RegContents){false, NULL, "$s6", true};
regs[s7] = (RegContents){false, NULL, "$s7", true};
lastUsed = zero;
}
const char *Mips::mipsName[BinaryOp::NumOps];