-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.sml
46 lines (40 loc) · 1.52 KB
/
main.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
structure Main =
struct
structure Codegen = MIPSGen
structure Frame = MIPSFrame
fun saytemp allocation t =
case Temp.Table.find(allocation, t)
of SOME r => r
| NONE => Temp.makestring t
fun emitFrag out (Frame.PROC{body,frame}) = let
val stms = Canon.linearize body
val stms = Canon.traceSchedule(Canon.basicBlocks stms)
val instrs = List.concat(map (Codegen.codegen frame) stms)
val instrs = Frame.procEntryExit2(frame, instrs)
val (instrs, allocation) = RegAlloc.alloc(instrs, frame)
val {prolog, body, epilog} = Frame.procEntryExit3(frame, instrs)
val format = Assem.format (saytemp allocation)
val _ = TextIO.output(out, "\n")
in TextIO.output(out, prolog);
app (fn i => TextIO.output(out, format i)) body;
TextIO.output(out, epilog)
end
| emitFrag out (Frame.STRING(lab, s)) =
TextIO.output(out, Frame.string(lab, s))
fun compile filename = let
val absyn = Parse.parse filename
val frags = Semant.transProg absyn
val (procs, strs) =
List.partition (fn Frame.PROC _ => true | _ => false) frags
in
if !ErrorMsg.anyErrors then () else let
val out = TextIO.stdOut
in
TextIO.output(out, ".data\n");
app (emitFrag out) procs;
TextIO.output(out, "\n.text\n");
app (emitFrag out) strs
end
end
fun main (cmd, args) = (app compile args; 0)
end