-
Notifications
You must be signed in to change notification settings - Fork 1
/
compile.sml
278 lines (225 loc) · 7.42 KB
/
compile.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
(* compile.sml
*
* This module contains the driver code for all passes
* after parsing of the MLPolyR compiler.
*
* Copyright (c) 2005 by Matthias Blume ([email protected])
*)
structure Compile : sig
val compile :
{ pclust: bool, pbbt: bool, pigraph: bool, no_ra: bool, pdefs: bool } ->
Ast.program * Source.inputSource * TextIO.outstream -> bool
end = struct
fun compile { pclust, pbbt, pigraph, no_ra, pdefs }
(ast, source, asm_s) = let
val _ = LVar.reset ()
val _ = Label.reset ()
val _ = TVar.reset ()
val limit_ptr_lab = Label.external "mlpr_limit_ptr"
val main_stack_ptr_lab = Label.external "mlpr_main_stack_ptr"
val frame_info_table_lab = Label.external "mlpr_frame_info_table"
fun emit s = TextIO.output (asm_s, s)
fun label l =
let val n = Label.name l
in if Char.contains n #"'" then emit (concat ["\"", n, "\":\n"])
else emit (n ^ ":\n")
end
fun wordalign () = emit "\t.align 2\n"
fun stubsection () =
emit ".section __TEXT,__symbol_stub1,symbol_stubs,\
\pure_instructions,16\n"
fun textsection () =
emit ".section __TEXT,__text,regular,pure_instructions\n"
fun datasection () = emit ".data\n";
fun gvarheader lab =
(emit (concat ["\n\n\t.globl ", Label.name lab, "\n"]);
datasection();
wordalign();
label lab)
val absyn = Elaborate.elaborate (source, BaseEnv.elabBase, pdefs) ast
val { lambda, strings } =
Translate.translate (absyn, BaseEnv.transBase)
val anf = LambdaToANF.convert lambda
(*
val _ = PrintANF.function print anf
*)
val anf = ANFOpt.optimize anf
val anf = ANFOpt.optimize anf
val anf = Flatten.transform anf
val anf = ANFOpt.optimize anf
val anf = ANFOpt.optimize anf
(*
val _ = PrintANF.function print anf
*)
val anf = Uncurry.transform anf
(*
val _ = PrintANF.function print anf
*)
val anf = ANFOpt.optimize anf
val anf = ANFOpt.optimize anf
val anf = ANFOpt.optimize anf
(*
val _ = PrintANF.function print anf
*)
val anf = Flatten.transform anf
val anf = ANFOpt.optimize anf
val anf = ANFOpt.optimize anf
(*
val _ = PrintANF.function print anf
*)
val closed = Closure.convert anf
val fc as { entrylabel, clusters } = FunctionClusters.clusterify closed
val _ = if pclust then
PrintClusters.print print fc
else ()
val clusters = map ValueNumbering.cluster_cse clusters
val bbt_clusters = map Treeify.treeify clusters
val _ = if pbbt then
app (PrintBBTree.prcluster print) bbt_clusters
else ()
val traces = map TraceSchedule.schedule bbt_clusters
val compilation = PPCCodeGen.new ()
fun dotrace (et as (_, (l, _), _)) =
let val frame = Frame.new l
val il = PPCCodeGen.codegen (frame, compilation, et)
val (il_a, allocation) =
RegAlloc.alloc { showigraph = pigraph }
(il, frame, Frame.registers)
val (il', allocation') =
if no_ra then (il, Frame.precoloring)
else (il_a, allocation)
val { save, restore, size, sz_regsave, sz_upper } =
Frame.regSaveRestoreInfo (frame, allocation)
val i2sl = Asm.format (Frame.showTemp allocation',
save, restore)
fun withnl s = s ^ "\n"
fun emiti i = app (emit o withnl) (i2sl i)
val (l_start, l_end) = (Label.new NONE, Label.new NONE)
in textsection ();
wordalign ();
label l_start;
emit (concat [Frame.frameSzName frame, "=",
Int.toString size, "\n"]);
if Frame.hasSpills frame then
emit (concat [Frame.frameSpillName frame, "=",
Int.toString (size - sz_regsave), "\n"])
else ();
app emiti il';
label l_end;
(* return table entry for GC *)
(l_start, l_end, sz_upper)
end
fun dotable entries =
let fun emit1 (l1, l2, size) =
(emit (concat ["\t.long ", Label.name l1, "\n"]);
emit (concat ["\t.long ", Label.name l2, "\n"]);
emit (concat ["\t.long ", Int.toString size, "\n"]))
in gvarheader frame_info_table_lab;
app emit1 entries;
emit "\t.long 0\n"
end
fun dostring (s, l) =
(datasection ();
emit ".cstring\n";
wordalign ();
label l;
emit (concat ["\t.asciz \"", String.toCString s, "\"\n"]))
fun dostubs () =
let val { funstubs, datastubs, needgc } =
PPCCodeGen.getstubs compilation
in stubsection ();
app emit funstubs;
datasection ();
app emit datastubs;
if needgc then
(textsection ();
(* emit gc stub label *)
label Frame.gcstublab;
(* Get return address into r0 *)
emit("\tmflr r0\n");
(* Save the return address *)
emit("\tstw r0,8(r1)\n");
(* Allocate the stack frame *)
emit("\tstwu r1,-128(r1)\n");
(* Save all the callee saved registers *)
emit("\tstmw r15,60(r1)\n");
(* Move amount, alloc_ptr, limit_ptr, and
stack_ptr into argument registers.
Note: r3 already contains amount.
Note: while in ML, allocptr and limitptr
point to the last word written and
the last word available, resp.
*)
emit("\taddi r4,r13,4\n");
emit("\taddi r5,r14,4\n");
emit("\tmr r6,r1\n");
(* Invoke the GC function *)
emit ("\tbl L" ^ Label.escname Frame.gclab ^ "$stub\n");
(* Store the returned values (alloc_ptr and limit_ptr)
into r13 and r14. Restore the invariants with
respect to alloc_ptr and limit_ptr while in ML
while in ML (see Note above).
*)
emit("\taddi r13,r3,-4\n");
emit(concat["\tlis r14,ha16(", Label.name limit_ptr_lab,
")\n"]);
emit(concat["\tlwz r14,lo16(", Label.name limit_ptr_lab,
") (r14)\n"]);
emit(concat["\taddi r14,r14,-4\n"]);
(* Restore all callee saved registers. *)
emit("\tlmw r15,60(r1)\n");
(* Return to ml code *)
emit("\taddi r1,r1,128\n");
emit("\tlwz r0,8(r1)\n");
emit("\tmtlr r0\n");
emit("\tblr\n"))
else ()
end
val eplab = Label.external "mlpr_entrypoint"
fun load_zero (from, to) =
if from <= to then
(emit (concat ["\tli r", Int.toString from, ",0\n"]);
load_zero (from+1, to))
else ()
fun dosetup () =
(textsection ();
wordalign ();
label eplab;
(* Get return address into r0 *)
emit("\tmflr r0\n");
(* Save the return address *)
emit("\tstw r0,8(r1)\n");
(* Allocate the stack frame *)
emit("\tstwu r1,-128(r1)\n");
(* Save all the callee saved registers *)
emit("\tstmw r15,60(r1)\n");
load_zero(15,30);
emit "\taddi r13,r5,-4\n";
emit "\taddi r14,r6,-4\n";
emit (concat ["\tlis r5,ha16(", Label.name main_stack_ptr_lab,
")\n"]);
emit (concat ["\tstw r1,lo16(", Label.name main_stack_ptr_lab,
")(r5)\n"]);
emit (concat ["\tbl ", Label.name entrylabel, "\n"]);
(* Restore all callee saved registers. *)
emit("\tlmw r15,60(r1)\n");
(* Return to ml code *)
emit("\taddi r1,r1,128\n");
emit("\tlwz r0,8(r1)\n");
emit("\tmtlr r0\n");
emit("\tblr\n"))
in if ErrorMsg.anyErrors (ErrorMsg.errors source) then false
else let val _ = emit (concat ["\t.globl ", Label.name eplab, "\n"])
val _ = dosetup ()
val pctable = map dotrace traces
in app dostring strings;
dostubs ();
gvarheader main_stack_ptr_lab;
emit "\t.long 0\n";
gvarheader limit_ptr_lab;
emit "\t.long 0\n";
dotable pctable;
true
end
end
end