-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtigercodegen.sml
568 lines (501 loc) · 15.5 KB
/
tigercodegen.sml
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
structure tigercodegen :> tigercodegen =
struct
(* NOTE: x86_64 instruction selection; AT&T syntax (GAS syntax)
The main advantage of using this syntax is its compatibility with the GCC inline assembly syntax
Remember: In the Tiger language, an int is a 64 bits long using sign convention
The fundamental data types are:
- byte = 8 bits
- word = 2 bytes (16 bits)
- doubleword = 4 bytes (32 bits)
- quadword = 8 bytes (64 bits)
- double quadword (octword) = 16 bytes (128 bits)
- double octword = 32 bytes (256 bits)
*)
open tigerassem
open tigertree
(* codegen : tigerframe.frame -> tigertree.stm -> tigerassem.instr list *)
(* Assuming that 'stm' parameter is canonized *)
fun codegen frame (stm : tigertree.stm) : instr list =
let
val ilist = ref ([] : instr list) (* list of assembly-lang instructions *)
fun emit instr = (ilist := instr::(!ilist)) (* Accumulates a list of instructions to
be returned later *)
fun generateTmp gen =
let
val t = tigertemp.newtemp()
in
(gen t; t)
end
(* munchStm : tigertree.stm -> unit *)
(* When MEM is used as the left child of a MOVE, it means "store",
but anywhere else it means "fetch" *)
(* - Move to memory location cases, like:
MOVE(MEM(e1), e2): Evaluate e1, yielding address a. Then evaluate
e2, and store the result into wordSize bytes of memory starting at
a *)
fun munchStm(MOVE(MEM(BINOP(PLUS, CONST i, e)), NAME l)) =
emit(OPER{
assem="movq $"^l^", "^utils.intToString i^"(`s0)\n",
src=[munchExp e],
dst=[],
jump=NONE
})
| munchStm(MOVE(MEM(BINOP(PLUS, CONST i, e)), CONST j)) =
emit(OPER{
assem="movq $"^utils.intToString j^", "^utils.intToString i^"(`s0)\n",
src=[munchExp e],
dst=[],
jump=NONE
})
| munchStm(MOVE(MEM(BINOP(PLUS, CONST i, e)), e')) =
emit(OPER{
assem="movq `s0, "^utils.intToString i^"(`s1)\n",
src=[munchExp e',
munchExp e],
dst=[],
jump=NONE
})
| munchStm(MOVE(MEM(BINOP(PLUS, e, CONST i)), e')) =
munchStm(MOVE(MEM(BINOP(PLUS, CONST i, e)), e'))
| munchStm(MOVE(MEM e, CONST i)) =
emit(OPER{
assem="movq $"^utils.intToString i^", (`s0)\n",
src=[munchExp e],
dst=[],
jump=NONE
})
| munchStm(MOVE(MEM e1, e2)) =
emit(OPER{
assem="movq `s0, (`s1)\n",
src=[munchExp e2,
munchExp e1],
dst=[],
jump=NONE
})
(* Move to temporary cases, like:
MOVE(TEMP t, e): Evaluate e and move it to temporary t *)
| munchStm(MOVE(TEMP t, MEM(BINOP(PLUS, CONST i, e)))) =
emit(OPER{
assem="movq "^utils.intToString i^"(`s0), `d0\n",
src=[munchExp e],
dst=[t],
jump=NONE
})
| munchStm(MOVE(TEMP t, MEM(BINOP(PLUS, e, CONST i)))) =
emit(OPER{
assem="movq "^utils.intToString i^"(`s0), `d0\n",
src=[munchExp e],
dst=[t],
jump=NONE
})
| munchStm(MOVE(TEMP t, MEM e)) =
emit(OPER{
assem="movq (`s0), `d0\n",
src=[munchExp e],
dst=[t],
jump=NONE
})
| munchStm(MOVE(TEMP t, CONST i)) =
emit(OPER{
assem="movq $"^utils.intToString i^", `d0\n",
src=[],
dst=[t],
jump=NONE
})
| munchStm(MOVE(TEMP t, NAME l)) =
emit(OPER{
assem="movq $"^l^", `d0\n",
src=[],
dst=[t],
jump=NONE
})
| munchStm(MOVE(TEMP t, CALL(NAME f, args))) =
(munchStm(EXP(CALL(NAME f, args))); (* Execute call *)
emit(tigerassem.MOVE{
assem="movq `s0, `d0\n",
src=tigerframe.rv,
dst=t
}))
| munchStm(MOVE(TEMP t, e)) =
emit(tigerassem.MOVE{
assem="movq `s0, `d0\n",
src=munchExp e,
dst=t
})
| munchStm(MOVE _) = raise Fail "Error - munchStm(): MOVE inválido a un no temporario/memoria"
| munchStm(LABEL lab) =
emit(tigerassem.LABEL{
assem=lab^":\n",
lab=lab
})
(* This case should not happen because of canonization... *)
| munchStm(SEQ(s1, s2)) =
(munchStm s1; munchStm s2)
| munchStm(JUMP(NAME n, labs)) =
emit(OPER{
assem="jmp "^n^"\n",
src=[],
dst=[],
jump=SOME labs
})
| munchStm(JUMP _) = raise Fail "Error - munchStm(): JUMP sin label"
| munchStm(CJUMP(relop, e1, e2, l1, l2)) =
let
val relToInstr = case relop of
EQ => "je"
| NE => "jne"
| LT => "jl"
| GT => "jg"
| LE => "jle"
| GE => "jge"
| _ => raise Fail "Error - munchStm(): operación de comparación no soportada"
val _ = emit(OPER{
assem="cmpq `s1, `s0\n",
src=[munchExp e1, munchExp e2],
dst=[],
jump=NONE
})
in
emit(OPER{
assem=relToInstr^" "^l1^"\n",
src=[],
dst=[],
jump=SOME [l1, l2]
})
end
(*
To invoke a function, we must first compute the arguments and place the in the
desired registers (or memory stack). Then, we must push the two caller-saved
registers on the stack, to save their values. We then issue the CALL instr,
which pushes the current instr pointer on to the stack then jumps to the code
location of the function. Upon return of the function, we pop the two caller-
saved registers off of the stack, and look for the return value of the
function in the %rax register
*)
| munchStm(EXP(CALL(NAME f, args))) =
let
val diff = List.length args - List.length (tigerframe.argregs)
in
emit(OPER{
assem="movq $0, `d0 # SOME EXTERNAL CALLS' PARAMETERS ARE NOT FIXED\n",
src=[],
dst=[tigerframe.rv],
jump=NONE
});
emit(OPER{
assem="call "^f^" # FROM munchStm(); diff = "^Int.toString diff^"\n",
src=(munchArgs args) @ [tigerframe.rv],
dst=tigerframe.calldefs,
jump=NONE
});
if diff > 0
then
emit(OPER{
assem="addq $"^utils.intToString(diff*(tigerframe.wSz))^", %rsp # diff = "^Int.toString diff^"\n",
src=[],
dst=[],
jump=NONE
})
else
()
end
| munchStm(EXP(CALL _)) = raise Fail "Error - munchStm(): CALL sin label"
| munchStm(EXP e) = (munchExp e; ()) (* Evaluate e and discard the result *)
and saveCallerSaves() =
let
fun emitcdefs s =
emit(OPER{
assem="pushq `s0 # SAVE CALLER-SAVE REGISTER\n",
src=[s],
dst=[],
jump=NONE
})
in
List.map emitcdefs (tigerframe.callersaves)
end
and restoreCallerSaves() =
let
fun emitcdefs s =
emit(OPER{
assem="popq `d0 # RESTORE CALLER-SAVE REGISTER\n",
src=[],
dst=[s],
jump=NONE
})
in
List.app emitcdefs (List.rev (tigerframe.callersaves))
end
(* munchArgs() generates code to move all the arguments to their correct positions,
in outgoing parameter registers and/or in memory. Returns a list of all the temporaries
that are to be passed to the machine's CALL instruction; they should be listed as
"sources" of the instruction, so that liveness analysis can see that their values need
to be kept up to the point of call. *)
and munchArgs args =
let
fun argsToStack [] = [] (* Push parameters in stack *)
| argsToStack (a::ass) =
let
val _ = case a of
CONST i => emit(OPER{
assem="pushq $"^utils.intToString i^" #\tFROM tigercodegen.munchArgs()\n",
src=[],
dst=[],
jump=NONE
})
| NAME l => emit(OPER{
assem="pushq $"^l^" #\tFROM tigercodegen.munchArgs()\n",
src=[],
dst=[],
jump=NONE
})
| TEMP t => emit(OPER{
assem="pushq `s0 #\tFROM tigercodegen.munchArgs()\n",
src=[t],
dst=[],
jump=NONE
})
| MEM(TEMP t) => emit(OPER{
assem="pushq (`s0) #\tFROM tigercodegen.munchArgs()\n",
src=[t],
dst=[],
jump=NONE
})
| MEM(BINOP(PLUS, e, CONST i)) => emit(OPER{
assem="pushq "^utils.intToString i^"(`s0) #\tFROM tigercodegen.munchArgs()\n",
src=[munchExp e],
dst=[],
jump=NONE
})
| MEM(e) => emit(OPER{
assem="pushq (`s0) #\tFROM tigercodegen.munchArgs()\n",
src=[munchExp e],
dst=[],
jump=NONE
})
| _ => emit(OPER{
assem="pushq `s0 #\tFROM tigercodegen.munchArgs()\n",
src=[munchExp a],
dst=[],
jump=NONE
})
in
argsToStack ass
end
fun argsToReg [] _ = []
| argsToReg ass [] = argsToStack (rev ass) (* Runned out of machine's temporaries argument registers to move function's arguments, so push them in stack applying C calling convention *)
| argsToReg (a::ass) (r::rss) =
let
val _ = munchStm(MOVE(TEMP r, a)) (* Move argument to temporary register *)
in
r::(argsToReg ass rss) (* Save list of registers *)
end
in
argsToReg args tigerframe.argregs (* Attempt to move function's arguments to machine's temporaries argument registers available if possible *)
end
(* Generates aassembly code to evaluate the input tree expression, then
returns register with final result in it *)
(* munchExp : tigertree.stm -> tigertemp.temp *)
and munchExp(MEM(e)) =
generateTmp(fn r =>
munchStm(MOVE(TEMP r, MEM e)))
| munchExp(CONST i) =
generateTmp(fn r =>
munchStm(MOVE(TEMP r, CONST i)))
| munchExp(TEMP t) = t
| munchExp(NAME n) =
generateTmp(fn r =>
munchStm(MOVE(TEMP r, NAME n)))
| munchExp(CALL _) = raise Fail "Error - munchExp(): CALL no debería aparecer después de canon"
| munchExp(ESEQ _) = raise Fail "Error - munchExp(): ESEQ no debería aparecer después de canon"
(* NOTE: Some x86_64 have arithmetic instructions with two operands, where one
of the operands is both a source and a destination *)
(* PLUS op cases *)
| munchExp(BINOP(PLUS, CONST i, e)) =
generateTmp(fn r =>
(munchStm(MOVE(TEMP r, e));
emit(OPER{
assem="addq $"^utils.intToString i^", `d0\n",
src=[r],
dst=[r],
jump=NONE
})))
(*
| munchExp(BINOP(PLUS, NAME l, e)) =
generateTmp(fn r =>
(munchStm(MOVE(TEMP r, e));
emit(OPER{
assem="addq $"^l^", `d0\n",
src=[r],
dst=[r],
jump=NONE
})))
*)
| munchExp(BINOP(PLUS, TEMP t, e)) =
generateTmp(fn r =>
(munchStm(MOVE(TEMP r, e));
emit(OPER{
assem="addq `s1, `d0\n",
src=[r, t],
dst=[r],
jump=NONE
})))
| munchExp(BINOP(PLUS, MEM(TEMP t), e)) =
generateTmp(fn r =>
(munchStm(MOVE(TEMP r, e));
emit(OPER{
assem="addq (`s1), `d0\n",
src=[r, t],
dst=[r],
jump=NONE
})))
| munchExp(BINOP(PLUS, MEM(BINOP(PLUS, CONST i, TEMP t)), e)) =
generateTmp(fn r =>
(munchStm(MOVE(TEMP r, e));
emit(OPER{
assem="addq "^utils.intToString i^"(`s1), `d0\n",
src=[r, t],
dst=[r],
jump=NONE
})))
| munchExp(BINOP(PLUS, MEM(BINOP(PLUS, TEMP t, CONST i)), e)) =
generateTmp(fn r =>
(munchStm(MOVE(TEMP r, e));
emit(OPER{
assem="addq "^utils.intToString i^"(`s1), `d0\n",
src=[r, t],
dst=[r],
jump=NONE
})))
| munchExp(BINOP(PLUS, MEM e1, e2)) =
generateTmp(fn r =>
(munchStm(MOVE(TEMP r, e2));
emit(OPER{
assem="addq (`s1), `d0\n",
src=[r, munchExp e1],
dst=[r],
jump=NONE
})))
| munchExp(BINOP(PLUS, e1, e2)) =
generateTmp(fn r =>
(munchStm(MOVE(TEMP r, e2));
emit(OPER{
assem="addq `s1, `d0\n",
src=[r, munchExp e1],
dst=[r],
jump=NONE
})))
(* MINUS op cases.
Remember: we have to meet the needs of BINOP(MINUS, e1, e2) = e1 - e2 *)
| munchExp(BINOP(MINUS, e, CONST i)) =
generateTmp(fn r =>
(munchStm(MOVE(TEMP r, e));
emit(OPER{
assem="subq $"^utils.intToString i^", `d0\n",
src=[r],
dst=[r],
jump=NONE
})))
(*
| munchExp(BINOP(MINUS, e, NAME l)) =
generateTmp(fn r =>
(munchStm(MOVE(TEMP r, e));
emit(OPER{
assem="subq $"^l^", `d0\n",
src=[r],
dst=[r],
jump=NONE
})))
*)
| munchExp(BINOP(MINUS, e, TEMP t)) =
generateTmp(fn r =>
(munchStm(MOVE(TEMP r, e));
emit(OPER{
assem="subq `s1, `d0\n",
src=[r, t],
dst=[r],
jump=NONE
})))
| munchExp(BINOP(MINUS, e, MEM(TEMP t))) =
generateTmp(fn r =>
(munchStm(MOVE(TEMP r, e));
emit(OPER{
assem="subq (`s1), `d0\n",
src=[r, t],
dst=[r],
jump=NONE
})))
| munchExp(BINOP(MINUS, e, MEM(BINOP(PLUS, CONST i, TEMP t)))) =
generateTmp(fn r =>
(munchStm(MOVE(TEMP r, e));
emit(OPER{
assem="subq "^utils.intToString i^"(`s1), `d0\n",
src=[r, t],
dst=[r],
jump=NONE
})))
| munchExp(BINOP(MINUS, e, MEM(BINOP(PLUS, TEMP t, CONST i)))) =
generateTmp(fn r =>
(munchStm(MOVE(TEMP r, e));
emit(OPER{
assem="subq "^utils.intToString i^"(`s1), `d0\n",
src=[r, t],
dst=[r],
jump=NONE
})))
| munchExp(BINOP(MINUS, e1, MEM e2)) =
generateTmp(fn r =>
(munchStm(MOVE(TEMP r, e1));
emit(OPER{
assem="subq (`s1), `d0\n",
src=[r, munchExp e2],
dst=[r],
jump=NONE
})))
| munchExp(BINOP(MINUS, e1, e2)) =
generateTmp(fn r =>
(munchStm(MOVE(TEMP r, e1));
emit(OPER{
assem="subq `s1, `d0\n",
src=[r, munchExp e2],
dst=[r],
jump=NONE
})))
(* DIV op cases *)
| munchExp(BINOP(DIV, _, CONST 0)) = raise Fail "Error - munchExp(): división por cero!" (* hacen falta casos así? creo que no *)
| munchExp(BINOP(DIV, e1, e2)) =
generateTmp(fn r =>
(munchStm(MOVE(TEMP tigerframe.rax, e1));
emit(OPER{
assem="cqto\n", (* cqto: Sign-extends the contents of RAX to RDX:RAX (now, 128 bits) -
broadcasts the sign bit of RAX into every bit of RDX *)
src=[],
dst=[tigerframe.rdx],
jump=NONE
});
emit(OPER{
assem="idivq `s0\n", (* idivq: Signed divide RDX:RAX by tmpe2. Quotient stored in RAX. Remainder stored in RDX *)
src=[munchExp e2,
tigerframe.rax],
dst=[tigerframe.rax,
tigerframe.rdx],
jump=NONE
});
munchStm(MOVE(TEMP r, TEMP tigerframe.rax)))) (* Move result of division to result temp *)
(* MUL op cases *)
| munchExp(BINOP(MUL, e1, e2)) =
generateTmp(fn r =>
(munchStm(MOVE(TEMP tigerframe.rax, e1));
emit(OPER{
assem="imulq `s0\n",
src=[munchExp e2,
tigerframe.rax],
dst=[tigerframe.rax,
tigerframe.rdx],
jump=NONE
});
munchStm(MOVE(TEMP r, TEMP tigerframe.rax))))
| munchExp(BINOP _) = raise Fail "Error - munchExp(): operación binaria no soportada"
in
(munchStm stm; rev (!ilist))
end
end