]>
Commit | Line | Data |
---|---|---|
5c79bb68 JW |
1 | signature OPTIMIZER = |
2 | sig | |
3 | datatype optfunc = | |
4 | IRPROG of (Tree.program -> Tree.program) | | |
5 | IRFUNC of (Tree.func -> Tree.func) | | |
6 | IRSTM of (Tree.stm -> Tree.stm list) | | |
7 | IREXP of (Tree.exp -> Tree.exp) | | |
f716a180 JW |
8 | PRELIVENESS of (Blarg.insn list -> Blarg.insn list) | |
9 | FINAL of (Blarg.insn list -> Blarg.insn list) | |
5c79bb68 JW |
10 | |
11 | type optimization = { | |
12 | shortname : string, | |
13 | description : string, | |
14 | func : optfunc | |
15 | } | |
16 | ||
17 | val optimize_ir : optimization list -> Tree.program -> Tree.program | |
f716a180 JW |
18 | val optimize_preliveness : optimization list -> Blarg.insn list -> Blarg.insn list |
19 | val optimize_final : optimization list -> Blarg.insn list -> Blarg.insn list | |
5c79bb68 JW |
20 | end |
21 | ||
22 | structure Optimizer :> OPTIMIZER = | |
23 | struct | |
24 | structure T = Tree | |
25 | ||
26 | datatype optfunc = | |
27 | IRPROG of (Tree.program -> Tree.program) | | |
28 | IRFUNC of (Tree.func -> Tree.func) | | |
29 | IRSTM of (Tree.stm -> Tree.stm list) | | |
30 | IREXP of (Tree.exp -> Tree.exp) | | |
f716a180 JW |
31 | PRELIVENESS of (Blarg.insn list -> Blarg.insn list) | |
32 | FINAL of (Blarg.insn list -> Blarg.insn list) | |
5c79bb68 JW |
33 | |
34 | type optimization = { | |
35 | shortname : string, | |
36 | description : string, | |
37 | func : optfunc | |
38 | } | |
39 | ||
40 | fun foldfunc f (T.FUNCTION (id, stml)) = T.FUNCTION (id, List.concat (List.map f stml)) | |
41 | fun expfunc f (T.MOVE (e1, e2)) = [T.MOVE (f e1, f e2)] | |
42 | | expfunc f (T.RETURN (e, s)) = [T.RETURN (f e, s)] | |
43 | | expfunc f (T.EFFECT e) = [T.EFFECT (f e)] | |
44 | | expfunc f (a as T.LABEL _) = [a] | |
45 | | expfunc f (T.JUMPIFN (e, l)) = [T.JUMPIFN (f e, l)] | |
46 | | expfunc f (a as T.JUMP _) = [a] | |
47 | ||
48 | fun optimize_ir ol prog = | |
49 | foldr ( | |
50 | fn (IRPROG f, prog) => f prog | |
51 | | (IRFUNC f, prog) => List.map f prog | |
52 | | (IRSTM f, prog) => List.map (foldfunc (f)) prog | |
53 | | (IREXP f, prog) => List.map (foldfunc (expfunc f)) prog | |
54 | | (_, prog) => prog) | |
55 | prog | |
56 | (map (fn (x : optimization) => #func x) ol) | |
57 | ||
58 | fun optimize_preliveness ol assem = | |
59 | foldr ( | |
60 | fn (PRELIVENESS f, assem) => f assem | |
61 | | (_, assem) => assem) | |
62 | assem | |
63 | (map (fn (x : optimization) => #func x) ol) | |
64 | ||
65 | fun optimize_final ol assem = | |
66 | foldr ( | |
67 | fn (FINAL f, assem) => f assem | |
68 | | (_, assem) => assem) | |
69 | assem | |
70 | (map (fn (x : optimization) => #func x) ol) | |
71 | end | |
72 | ||
73 | signature OPTIMIZATION = | |
74 | sig | |
75 | val optimizer : Optimizer.optimization | |
76 | end |