Debian Codegraph @

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

raw
1 (* mdl-gen-cells.sml
2 *
3 * COPYRIGHT (c) 2016 The Fellowship of SML/NJ (http://www.smlnj.org)
4 * All rights reserved.
5 *
6 * Generate the <arch>Cells structure.
7 * This structure contains various information about the register
8 * properties of the architecture.
9 *)
10
11 functor MDLGenCells(Comp : MDL_COMPILE) : MDL_GEN_MODULE =
12 struct
13
14 structure Ast = Comp.Ast
15 structure Comp = Comp
16 structure R = Comp.Rewriter
17
18 open Ast Comp.Util
19
20 val NO = R.noRewrite
21
22 val SZty = IDty(IDENT([],"CellsBasis.sz"))
23 val REGISTER_IDty = IDty(IDENT([],"CellsBasis.register_id"))
24 val showFunTy = FUNty(REGISTER_IDty, STRINGty)
25 val showWithSizeFunTy = FUNty(TUPLEty[REGISTER_IDty,SZty], STRINGty)
26
27 fun gen md =
28 let (* name of the structure *)
29 val strName = Comp.strname md "Cells"
30 val sigName = Comp.signame md "CELLS"
31
32 (* all cell kinds *)
33 val cellKinds = Comp.cells md
34
35 (* Process *)
36 fun process([], r) = r
37 | process(CELLdecl{id, from, to, count, ...}::ds, r) =
38 let val count = case count of NONE => 0 | SOME c => c
39 in from := r;
40 to := r + count - 1;
41 process(ds, r+count)
42 end
43
44 val firstPseudo = process(cellKinds, 0)
45
46
47 (* all cell kind names *)
48 val cellKindNames = map (fn CELLdecl{id, ...} => id) cellKinds
49
50 val allCellKindNames = cellKindNames
51
52 (* cellkinds that has to be put into the cellset *)
53 (* val cellSets = Comp.cellSets md
54 val cellSets' = Comp.cellSetsAliases md
55
56 val cellSetNames = map (fn CELLdecl{id, ...} => id) cellSets
57 *)
58
59 val clientDefinedCellKinds =
60 List.filter (fn CELLdecl{id, ...} =>
61 not(MLRiscDefs.isPredefinedCellKind id)) cellKinds
62
63 (* locations *)
64 val locations = Comp.locations md
65
66
67 (* Functions showXXX *)
68 val showFunSig =
69 VALSIGdecl(map (fn k => "show"^k) cellKindNames,showFunTy)
70 val showWithSizeFunSig =
71 VALSIGdecl(map (fn k => "show"^k^"WithSize")
72 cellKindNames,showWithSizeFunTy)
73 val showWithSizeFuns =
74 let fun shift(from, to) e =
75 if !from = 0 then e
76 else LET([VAL("r",
77 IFexp(APP("<=",TUPLEexp[ID "r",INTexp(!to)]),
78 APP("-",TUPLEexp[ID "r",INTexp(!from)]),
79 ID "r"))],e)
80 in FUNdecl(
81 map (fn CELLdecl{id, from, to, print, ...} =>
82 FUNbind("show"^id^"WithSize",
83 [CLAUSE([TUPLEpat[IDpat "r",IDpat "ty"]],
84 NONE,
85 (APPexp(print, TUPLEexp[ID "r",ID "ty"])))]))
86 cellKinds)
87 end
88
89 val showFuns =
90 SEQdecl(map (fn CELLdecl{id, from, to, print, bits, ...} =>
91 FUN("show"^id,IDpat "r",
92 APP("show"^id^"WithSize",TUPLEexp[ID "r",INTexp bits])))
93 cellKinds)
94
95 (* Functions addXXX *)
96 val addFunTy = FUNty(TUPLEty[REGISTERty,CELLSETty],CELLSETty)
97 val addFunSig = VALSIGdecl(map (fn s => "add"^s)
98 cellKindNames, addFunTy)
99 val addFun = VALdecl
100 (map (fn k => VALbind(IDpat("add"^k),
101 IDexp(IDENT(["CellSet"],"add"))))
102 cellKindNames)
103
104 (* Client defined cellkinds *)
105 val clientDefinedCellKindsSig =
106 VALSIGdecl(map (fn CELLdecl{id, ...} => id) clientDefinedCellKinds,
107 IDty(IDENT([],"CellsBasis.cellkind")))
108 fun createCellKind(CELLdecl{id, nickname, ...}) =
109 VALbind(IDpat id,
110 APPexp(IDexp(IDENT(["CellsBasis"],"newCellKind")),
111 RECORDexp[("name",STRINGexp id),
112 ("nickname",STRINGexp nickname)]))
113
114 val clientDefinedCellKindsDecl =
115 VALdecl(map createCellKind clientDefinedCellKinds)
116
117 val None = ID "NONE"
118 val newCounter = APP("ref",INTexp 0)
119
120
121 val nonAliasedCellKinds =
122 List.filter(fn CELLdecl{alias=NONE, ...} => true
123 | CELLdecl _ => false) cellKinds
124
125 fun kindName k =
126 if MLRiscDefs.isPredefinedCellKind k
127 then IDexp(IDENT(["CellsBasis"],k))
128 else ID k
129
130
131 (* Generate descriptor for a cellkind *)
132 fun mkDesc(CELLdecl{from, to, id, nickname, defaults, ...}) =
133 let val zeroReg =
134 List.foldr(fn ((r,LITexp(INTlit 0)),_) => APP("SOME",INTexp r)
135 | (_,d) => d) None defaults
136 val defaultValues =
137 LISTexp(map (fn (r,v) => TUPLEexp[INTexp(r + !from),v])
138 defaults, NONE)
139
140 val count = Int.max(!to - !from + 1,0)
141 val physicalRegs = APP("ref", ID("CellsBasis.array0"))
142 val exp =
143 APP("CellsBasis.DESC",
144 RECORDexp[("low", INTexp(!from)),
145 ("high", INTexp(!to)),
146 ("kind", kindName id),
147 ("defaultValues", defaultValues),
148 ("zeroReg", zeroReg),
149 ("toString", ID("show"^id)),
150 ("toStringWithSize", ID("show"^id^"WithSize")),
151 ("counter", newCounter),
152 ("dedicated", newCounter),
153 ("physicalRegs", physicalRegs)
154 ]
155 )
156 in VALbind(IDpat("desc_"^id), exp)
157 end
158
159 fun mkKindDesc(CELLdecl{alias=NONE, id, ...}) =
160 TUPLEexp[kindName id, ID("desc_"^id)]
161 | mkKindDesc(CELLdecl{alias=SOME x, id, ...}) =
162 TUPLEexp[kindName id, ID("desc_"^x)]
163
164 (* size of general-purpose registers in bytes *)
165 val cellSize = let
166 val CELLdecl{bits=widthOfGP, ...} = Comp.lookupCellKind md "GP"
167 in
168 widthOfGP div 8
169 end
170
171 (* create CellsBasis *)
172 val applyCellsCommon =
173 STRUCTUREdecl("MyCells",[],NONE,
174 APPsexp(IDsexp(IDENT([],"Cells")),
175 DECLsexp
176 [$["exception Cells = "^strName,
177 "val firstPseudo = 256"
178 ],
179 VALdecl(map mkDesc nonAliasedCellKinds),
180 VAL("cellKindDescs",LISTexp(map mkKindDesc cellKinds,NONE)),
181 VAL("cellSize", LITexp(INTlit cellSize))
182 ]))
183
184 (* User defined locations *)
185 (*
186 val locationsSig =
187 map (fn LOCbind(id,NONE,_) => VALSIGdecl([id],REGISTERty)
188 | LOCbind(id,SOME _,_) =>
189 VALSIGdecl([id],FUNty(INTty,REGISTERty)))
190 locations
191 *)
192
193 (* stackptrR, asmTmpR, and fasmTmpR are in the common CELLS
194 * interface, so we do not include them in the architecture interface
195 * as well -- or we would have a duplicate specification error.
196 *)
197 val locationsSig = let
198 fun locs(LOCbind("stackptrR",_,_)::rest) = locs rest
199 | locs(LOCbind("asmTmpR",_,_)::rest) = locs rest
200 | locs(LOCbind("fasmTmp",_,_)::rest) = locs rest
201 | locs(LOCbind(id,NONE,_)::rest) = VALSIGdecl([id],REGISTERty)::locs rest
202 | locs(LOCbind(id,SOME _,_)::rest) =
203 VALSIGdecl([id],FUNty(INTty,REGISTERty))::locs rest
204 | locs [] = []
205 in
206 locs locations
207 end
208
209 val locationsFun0 =
210 VALdecl(map (fn CELLdecl{id, ...} =>
211 VALbind(IDpat("Reg"^id),APP("Reg",ID id)))
212 cellKinds)
213
214 val locationsFun =
215 let fun mkLoc e =
216 let fun exp _ (LOCexp(id,e,_)) =
217 let val CELLdecl{id, ...} = Comp.lookupCellKind md id
218 in APP("Reg"^id,e)
219 end
220 | exp _ e = e
221 in #exp(R.rewrite{exp=exp,sexp=NO,decl=NO,ty=NO,pat=NO}) e
222 end
223 in
224 map (fn LOCbind(id,NONE,e) => VAL(id,mkLoc e)
225 | LOCbind(id,SOME p,e) =>
226 VAL(id,LAMBDAexp[CLAUSE([p],NONE,mkLoc e)]))
227 locations
228 end
229
230 fun set k = ID("set"^k)
231
232 (* body of signature *)
233 val sigBody =
234 [$["include CELLS"],
235 clientDefinedCellKindsSig,
236 showFunSig,
237 showWithSizeFunSig,
238 SEQdecl locationsSig,
239 addFunSig
240 ]
241
242 (* body of structure *)
243 val strBody =
244 [$["exception "^strName,
245 "fun error msg = MLRiscErrorMsg.error(\""^strName^"\",msg)",
246 "open CellsBasis"
247 ],
248 showWithSizeFuns,
249 showFuns,
250 clientDefinedCellKindsDecl,
251 applyCellsCommon,
252 $["open MyCells"],
253 addFun,
254 locationsFun0,
255 SEQdecl locationsFun,
256 Comp.declOf md "Cells"
257 ]
258
259 in
260 Comp.codegen md "instructions/Cells"
261 [Comp.mkSig md "CELLS" sigBody,
262 Comp.mkStr md "Cells" sigName strBody]
263
264 end
265
266 end