Debian Codegraph @

lib/mlrisc-lib/MLRISC/Tools/MDL/mdl-gen-rewrite.sml

raw
1 (*
2 * Generate the <arch>Rewrite functor.
3 * which performs register renaming.
4 *)
5
6 functor MDLGenRewrite(RTLComp : MDL_RTL_COMP) : MDL_GEN_MODULE2 =
7 struct
8
9 structure RTLComp = RTLComp
10 structure Comp = RTLComp.Comp
11 structure Ast = Comp.Ast
12 structure Env = Comp.Env
13 structure Trans = Comp.Trans
14 structure RTL = RTLComp.RTL
15 structure T = RTL.T
16 structure C = CellsBasis
17 structure MLRisc = RTLComp.MLRiscTypes
18
19 open Ast Comp.Util Comp.Error
20
21 (* Change these definitions if the cell type has changed *)
22 fun howToRename cellkind =
23 $["fun rename r = if regmap r = rs then rt else r",
24 "fun renamecellset cellset =",
25 " CellsBasis.CellSet.map C."^C.cellkindToString cellkind^" rename cellset"
26 ]
27
28 (* Main function *)
29 fun gen compiled_rtls =
30 let val md = RTLComp.md compiled_rtls
31
32 (* name of the structure/signature *)
33 val strName = Comp.strname md "Rewrite"
34 val sigName = "REWRITE_INSTRUCTIONS"
35
36 (* The instructions *)
37 val instructions = Comp.instructions md
38
39 (* The Instruction environment *)
40 val env = Env.lookupStr (Comp.env md) (IDENT([],"Instruction"))
41
42 (* Arguments to the instruction functor *)
43 val args =
44 ["Instr : "^Comp.signame md "INSTR"
45 ]
46
47 datatype defUse = DEF | USE
48
49 (*
50 * Make a rewrite function of type:
51 * regmap * instruction * fromReg * toReg -> instruction
52 *
53 *)
54 fun mkFun(funName, rwOpnd, cellKind, defUse) =
55 let fun mkRewriteBody{instr, rtl, const} =
56 let fun apply(f,x) = SOME(APP(f,ID x))
57 fun rewrite(x,ty,T.$(_,c,_)) =
58 if c = cellKind then apply("rename",x) else NONE
59 | rewrite(x,ty,T.ARG(_,ref(rep as T.REP k),_)) =
60 if MLRisc.isConst rep then NONE
61 else apply("rename"^k,x)
62 | rewrite(x,ty,_) = fail("bad argument "^x)
63 fun nonRtlArg _ = NONE
64 fun rtlArg(name, ty, exp, RTL.IN _) =
65 if defUse = USE then rewrite(name,ty,exp) else NONE
66 | rtlArg(name, ty, exp, RTL.OUT _) =
67 if defUse = DEF then rewrite(name,ty,exp) else NONE
68 | rtlArg(name, ty, exp, RTL.IO _) =
69 rewrite(name,ty,exp)
70 val exp = RTLComp.mapInstr{instr=instr,
71 rtl=rtl,
72 nonRtlArg=nonRtlArg,
73 rtlArg=rtlArg}
74 in {exp=exp, casePats=[]}
75 end
76 val decls =
77 [$["fun rewriteoperand opnd = "^rwOpnd^"(regmap,rs,rt,opnd)"
78 ],
79 howToRename cellKind,
80 RTLComp.simpleErrorHandler funName
81 ]
82 in RTLComp.mkQuery compiled_rtls
83 {name = funName,
84 namedArguments= false,
85 args = [["regmap","instr","rs","rt"]],
86 decls = decls,
87 caseArgs = [],
88 body = mkRewriteBody
89 }
90 end
91
92 (* The functor *)
93 val strBody =
94 [$ ["structure I = Instr",
95 "structure C = I.C",
96 ""
97 ],
98 Comp.errorHandler md "Rewrite",
99 Comp.declOf md "Rewrite",
100 mkFun("rewriteDef","rewriteOperandDef", C.GP, DEF),
101 mkFun("rewriteUse","rewriteOperandUse", C.GP, USE),
102 mkFun("frewriteDef","frewriteOperandDef", C.FP, DEF),
103 mkFun("frewriteUse","frewriteOperandUse", C.FP, USE)
104 ]
105
106 val _ = Comp.require md "Rewrite"
107 {values=["rewriteOperandDef",
108 "rewriteOperandUse",
109 "frewriteOperandDef",
110 "frewriteOperandUse"],
111 types=[]
112 }
113
114 in Comp.codegen md "ra/Rewrite2"
115 [Comp.mkFct md "Rewrite2" args sigName strBody
116 ]
117 end
118 end