Debian Codegraph @

lib/mlrisc-lib/MLRISC/Tools/MDL/mdl-typing.sml

raw
1 (*
2 * Type checking for RTL.
3 * We also perform arity raising to convert the program into explicit type
4 * passing style at the same time.
5 *
6 * Note: there are quite a lot of bugs in the algorithm.
7 * I don't have time to fix them.
8 *
9 * Allen Leung ([email protected])
10 *)
11 functor MDLTyping
12 (structure Env : MDL_ENV
13 structure TypeUtil : MDL_TYPE_UTILS
14 structure AstUtil : MDL_AST_UTIL
15 structure AstPP : MDL_AST_PRETTY_PRINTER
16 structure Comp : MDL_COMPILE
17 sharing Env.Ast = TypeUtil.Ast = AstUtil.Ast = AstPP.Ast
18 sharing Comp.Env = Env
19 ) : MDL_TYPING =
20 struct
21 structure Env = Env
22 structure Ast = Env.Ast
23 structure TypeUtil = TypeUtil
24 structure Comp = Comp
25 structure R = Comp.Rewriter
26
27 open Comp.Error Ast AstUtil
28
29 infix ++
30
31 val op ++ = Env.++
32
33 fun e2s e = PP.text(AstPP.exp e)
34 fun p2s e = PP.text(AstPP.pat e)
35 fun d2s e = PP.text(AstPP.decl e)
36 fun id2s e = PP.text(AstPP.ident e)
37 fun t2s e = PP.text(AstPP.ty e)
38
39 fun unifyExp(e,t1,t2) = TypeUtil.unify(fn _ => " in "^e2s e,t1,t2)
40 fun unifyPat(p,t1,t2) = TypeUtil.unify(fn _ => " in "^p2s p,t1,t2)
41 fun undefinedCons(pat,id) =
42 error("undefined constructor "^id2s id^" in "^p2s pat)
43
44 fun lookupCons E id = Env.lookupVal E id
45
46 fun isPolymorphic t =
47 let val poly = ref false
48 fun ty ==> (t as VARty(_,_,_,ref NONE)) = (poly := true; t)
49 | ty ==> (t as POLYty _) = (poly := true; t)
50 | ty ==> (t as TYVARty _) = (poly := true; t)
51 | ty ==> t = t
52 in #ty (R.rewrite{ty=ty,pat=R.noRewrite,decl=R.noRewrite,
53 sexp=R.noRewrite,exp=R.noRewrite}) t;
54 !poly
55 end
56
57 fun openStrs E ids =
58 let val Es = map (Env.lookupStr E) ids
59 in foldr op++ Env.empty Es end
60
61 fun boundVar E (VARtv _) = TypeUtil.newVar 0
62 | boundVar E (INTtv _) = TypeUtil.newIVar 0
63
64 fun polyTy name = let val tv = TypeUtil.newIVar 0
65 in POLYty([tv],APPty(IDENT([],name),[tv])) end
66 fun mkTy name = IDty(IDENT([],name))
67 val bitsTy = polyTy "bits"
68 val regionTy = mkTy "region"
69 val effectTy = mkTy "effect"
70 val boolTy = mkTy "bool"
71 val stringTy = mkTy "string"
72 val intTy = mkTy "int"
73 val wordTy = mkTy "word"
74 fun listTy(ps,t) =
75 APPty(IDENT([],"list"),[INTVARty(length ps),TypeUtil.deref t])
76
77 fun appTy name x = APPty(IDENT([],name), [x])
78 fun intappTy name n = appTy name (INTVARty n)
79
80 (*
81 * Perform typechecking
82 *)
83 fun typeCheck md d =
84 let val _ = TypeUtil.init()
85 val bitsOf = intappTy "bits"
86 val cellOf = intappTy "bits"
87
88 val CELLdecl{bits=widthOfGP, ...} = Comp.lookupCellKind md "GP"
89
90 fun map2 f [] = ([], [])
91 | map2 f (x::xs) = let val (a,b) = f x
92 val (c,d) = map2 f xs
93 in (a::c,b::d) end
94
95 fun memOf E (exp, e, "cellset", region) =
96 (case region of
97 SOME _ => error("illegal region in "^e2s exp)
98 | NONE => ();
99 (bitsOf widthOfGP, cellOf widthOfGP, e)
100 )
101 | memOf E (exp, e, m, region) =
102 let val CELLdecl{bits=n, count, id, aggregable, ...} =
103 Comp.lookupCellKind md m
104 fun log2 1 = 0
105 | log2 n = log2(n div 2) + 1
106 val argTy = case count of
107 SOME m => bitsOf(log2 m)
108 | NONE =>
109 (case id of
110 "MEM" => bitsOf widthOfGP
111 | "CTRL" => bitsOf widthOfGP
112 | _ =>
113 (error("$"^m^"["^e2s e^"] is illegal"); Env.var E)
114 )
115 val _ =
116 if id = "MEM" then
117 (case region of
118 SOME r => let val (_,t') = W E (ID r)
119 in unifyExp(exp,t',regionTy) end
120 | NONE => warning("missing region in "^e2s exp)
121 ) else
122 (case region of
123 SOME _ => error("illegal region in "^e2s exp)
124 | _ => ()
125 )
126 val retTy =
127 if aggregable then appTy "bits" (TypeUtil.newIVar 0)
128 else cellOf n
129 in (argTy, retTy, e)
130 end
131
132 and W E (IDexp id) = Env.lookupVal E id
133 | W E (e as TUPLEexp []) = (e, effectTy)
134 | W E (TUPLEexp [e]) = W E e
135 | W E (TUPLEexp es) =
136 let val (es, ts) = Ws E es
137 in (TUPLEexp es, TUPLEty ts) end
138 | W E (RECORDexp les) =
139 let val (les, lts) = LWs E les
140 in (RECORDexp les, RECORDty lts) end
141 | W E (e as LITexp(INTlit _)) = (e, intTy)
142 | W E (e as LITexp(WORD32lit _)) = (e, wordTy)
143 | W E (e as LITexp(WORDlit _)) = (e, wordTy)
144 | W E (e as LITexp(BOOLlit _)) = (e, boolTy)
145 | W E (e as LITexp(STRINGlit _)) = (e, stringTy)
146 | W E (exp as TYPEDexp(e,t)) =
147 let val (e, t1) = W E e
148 val t2 = T E t
149 in unifyExp(exp,t1,t2); (e, t2) end
150 | W E (exp as LISTexp(es,NONE)) =
151 let val (es, ts) = Ws E es
152 val t = Env.var E
153 in foldr (fn (a,b) => (unifyExp(exp,a,b); a)) t ts;
154 (LISTexp(es,NONE), listTy(es,t))
155 end
156 | W E (exp as BITSLICEexp(e,l)) =
157 let val (e,t) = W E e
158 val n = foldr (fn ((a,b),l) => b-a+1+l) 0 l
159 val (e,t') = Env.inst E (BITSLICEexp(e,l), bitsTy)
160 in unifyExp(exp,t,t');
161 (e, bitsOf n)
162 end
163 | W E (exp as LOCexp(id,e,region)) =
164 let val (e, t) = W E e
165 val (argTy, retTy, e) = memOf E (exp, e, id, region)
166 in unifyExp(exp,t,argTy);
167 (LOCexp(id,e,region), retTy)
168 end
169 | W E (exp as APPexp(f,x)) =
170 let val (f, t1) = W E f
171 val (x, t2) = W E x
172 val t = Env.var E
173 in unifyExp(exp,t1,FUNty(t2,t));
174 (APPexp(f, x), t)
175 end
176 | W E (exp as IFexp(a,b,c)) =
177 let val (a, t1) = W E a
178 val (b, t2) = W E b
179 val (c, t3) = W E c
180 in unifyExp(a,t1,boolTy); unifyExp(exp,t2,t3);
181 (IFexp(a,b,c), t2)
182 end
183 | W E (exp as CASEexp(e,cs)) =
184 let val (e, t1) = W E e
185 val (cs, t2) = CSs E cs
186 val t3 = Env.var E
187 in unifyExp(exp,t2,FUNty(t1,t3));
188 (CASEexp(e, cs), t3)
189 end
190 | W E (LAMBDAexp cs) =
191 let val (cs, t) = CSs E cs
192 in (LAMBDAexp cs, t) end
193 | W E (e as SEQexp []) = (e, effectTy)
194 | W E (SEQexp [e]) = W E e
195 | W E (SEQexp(e::es)) =
196 let val (e, _) = W E e
197 val (es, t) = Wseq E es
198 in (SEQexp(e::es), t) end
199 | W E (LETexp(ds,es)) =
200 let val (ds, E') = Ds E ds
201 val (es, t) = Wseq (E ++ E') es
202 in (LETexp(ds, es), t) end
203 | W E (MARKexp(l,e)) = (setLoc l; W E e)
204 | W E exp = fail("W "^e2s exp)
205
206 and Ws E es = map2 (W E) es
207
208 and Wseq E [] =
209 let val (e, t) = W E (SEQexp [])
210 in ([e], t) end
211 | Wseq E [e] =
212 let val (e, t) = W E e
213 in ([e], t) end
214 | Wseq E (e::es) =
215 let val (e, _) = W E e
216 val (es, t) = Wseq E es
217 in (e::es, t) end
218
219 and LW E (l,e) =
220 let val (e, t) = W E e
221 in ((l,e), (l,t)) end
222
223 and LWs E les = map2 (LW E) les
224
225 and CSs E [] = ([], Env.var E)
226 | CSs E (all as c::cs) =
227 let val (c, t) = CS E c
228 val (cs, t') = CSs E cs
229 in unifyExp(LAMBDAexp all,t,t');
230 (c::cs, t)
231 end
232
233 and CS E (CLAUSE(ps, g, e)) =
234 let val (ts, Es) = map2 (P E) ps
235 val E' = foldr op++ Env.empty Es
236 val (e, t2) = W (E ++ E') e
237 fun f [] = t2
238 | f (t::ts) = FUNty(t,f ts)
239 val g =
240 case g of
241 NONE => NONE
242 | SOME ge =>
243 let val (ge', tg) = W E ge
244 in unifyExp(ge, tg, BOOLty);
245 SOME ge
246 end
247 in (CLAUSE(ps, g, e), f ts) end
248
249 and P E (IDpat id) = Pvar E id
250 | P E (ASpat(id,p)) =
251 let val (t1,E') = P E p
252 val E'' = Env.VALbind(id,ID id,t1)
253 in (t1, E'' ++ E') end
254 | P E (TUPLEpat [p]) = P E p
255 | P E (TUPLEpat ps) =
256 let val (ts,E') = Ps E ps in (TUPLEty ts,E') end
257 | P E (pat as ORpat ps) =
258 let val (ts,E') = Ps E ps
259 val t = Env.var E
260 in foldr (fn (t1,t2) => (unifyPat(pat,t1,t2); t1)) t ts;
261 (t,E')
262 end
263 | P E (RECORDpat(lps,false)) =
264 let val (lts,E') = LPs E lps in (RECORDty lts,E') end
265 | P E WILDpat = (Env.var E, Env.empty)
266 | P E (LITpat(INTlit _)) = (INTty, Env.empty)
267 | P E (LITpat(BOOLlit _)) = (BOOLty, Env.empty)
268 | P E (LITpat(WORDlit _)) = (WORD32ty, Env.empty)
269 | P E (LITpat(STRINGlit _)) = (STRINGty, Env.empty)
270 | P E (pat as CONSpat(id,NONE)) =
271 (let val (_,t1) = lookupCons E id
272 in (t1, Env.empty)
273 end handle _ =>
274 case id of
275 IDENT([],id) => Pvar E id
276 | _ => (undefinedCons(pat,id); (Env.var E, Env.empty))
277 )
278 | P E (pat as CONSpat(id,SOME p)) =
279 (let val (_,t1) = lookupCons E id
280 val (t2,E') = P E p
281 val t3 = Env.var E
282 in unifyPat(pat,t1,FUNty(t2,t3)); (t3,E')
283 end handle _ =>
284 case id of
285 IDENT([],id) => Pvar E id
286 | _ => (undefinedCons(pat,id); (Env.var E, Env.empty))
287 )
288 | P E (pat as LISTpat(ps,NONE)) =
289 let val (ts,E') = Ps E ps
290 val t = Env.var E
291 in foldr (fn (a,b) => (unifyPat(pat,a,b); a)) t ts;
292 (listTy(ps,t), E')
293 end
294 | P E p = (error("pattern "^p2s p^
295 " not allowed in semantics description");
296 (Env.var E, Env.empty)
297 )
298 and Ps E ps =
299 let val xs = map (P E) ps
300 val ts = map #1 xs
301 val Es = map #2 xs
302 in (ts, foldr op++ Env.empty Es) end
303
304 and LPs E lps =
305 let val xs = map (LP E) lps
306 val lts = map #1 xs
307 val Es = map #2 xs
308 in (lts, foldr op++ Env.empty Es) end
309
310 and LP E (l,p) = let val (t,E) = P E p in ((l,t), E) end
311
312 and Pvar E id = let val t = Env.var E
313 in (t, Env.VALbind(id,ID id,t)) end
314
315 and D E (DATATYPEdecl(dbs,tbs)) =
316 let val (dbs, tbs, E) = DTs E (dbs,tbs)
317 in (DATATYPEdecl(dbs, tbs), E) end
318 | D E (FUNdecl fbs) =
319 let val (fbs, E) = FDs E fbs
320 in (FUNdecl fbs, E) end
321 | D E (RTLdecl(pat,e,loc)) =
322 let val (VALbind(pat,e), E) = VD E (VALbind(pat,e))
323 in (RTLdecl(pat,e,loc), E) end
324 | D E (RTLSIGdecl(ids,ty)) =
325 let val E = VS E (ids,ty)
326 in (RTLSIGdecl(ids, ty), E) end
327 | D E (VALdecl vbs) =
328 let val (vbs, E) = VDs E vbs
329 in (VALdecl vbs, E) end
330 | D E (TYPESIGdecl(id,tvs)) =
331 let val E = TS E (id,tvs)
332 in (TYPESIGdecl(id,tvs), E) end
333 | D E (VALSIGdecl(ids,ty)) =
334 let val E = VS E (ids,ty)
335 in (VALSIGdecl(ids,ty), E) end
336 | D E (LOCALdecl(d1,d2)) =
337 let val (d1, E1) = Ds E d1
338 val (d2, E2) = Ds (E ++ E1) d2
339 in (LOCALdecl(d1, d2), E2) end
340 | D E (SEQdecl ds) =
341 let val (ds, E) = Ds E ds
342 in (SEQdecl ds, E) end
343 | D E (d as OPENdecl ids) =
344 let val E = openStrs E ids
345 in (d, E) end
346 | D E (STRUCTUREdecl(id,args,s,sexp)) =
347 let val (sexp, E') = SE E sexp
348 in (STRUCTUREdecl(id,args,s,sexp), Env.STRbind(id,args,E')) end
349 | D E (d as STRUCTURESIGdecl _) = (d, Env.empty)
350 | D E (d as INFIXdecl _) = (d, Env.empty)
351 | D E (d as INFIXRdecl _) = (d, Env.empty)
352 | D E (d as NONFIXdecl _) = (d, Env.empty)
353 | D E (MARKdecl(l,d)) =
354 let val (d, E) = withLoc l (D E) d
355 in (MARKdecl(l,d), E) end
356 | D E (d as $[]) = (d, Env.empty)
357 | D _ d = (error("illegal declaration: "^d2s d); (d, Env.empty))
358
359 and Ds E [] = ([], Env.empty)
360 | Ds E (d::ds) =
361 let val (d, E1) = D E d
362 val (ds, E2) = Ds (E ++ E1) ds
363 in (d::ds, E1 ++ E2) end
364
365 and TS E (id,[]) = Env.TYPEbind(id,IDty(IDENT([],id)))
366 | TS E (id,tvs) =
367 let val vs = map (boundVar E) tvs
368 val t = LAMBDAty(vs,APPty(IDENT([],id),vs))
369 in Env.TYPEbind(id,t) end
370
371 and VS E (ids,ty) =
372 let val t = T E ty
373 in foldr (fn (id,E) => Env.VALbind(id,ID id,t) ++ E) Env.empty ids
374 end
375
376 and FDs E [] = ([], Env.empty)
377 | FDs E (fb::fbs) =
378 let val (fb, E') = FD E fb
379 val (fbs, E'') = FDs (E ++ E') fbs
380 in (fb::fbs, E' ++ E'') end
381
382 and FD E (FUNbind(f,cs)) =
383 let val (cs, t) = CSs E cs
384 val (LAMBDAexp cs, t) = Env.gen E (LAMBDAexp cs, t)
385 in (FUNbind(f,cs), Env.VALbind(f,ID f,t)) end
386
387 and VDs E [] = ([], Env.empty)
388 | VDs E (vb::vbs) =
389 let val (vb, E') = VD E vb
390 val (vbs, E'') = VDs (E ++ E') vbs
391 in (vb::vbs, E' ++ E'') end
392
393 and VD E (vb as VALbind(p,e)) =
394 let val (t,E') = P E p
395 val (e, t') = W (E ++ E') e
396 in TypeUtil.unify(fn _ => PP.text(AstPP.valbind vb),t,t');
397 (VALbind(p,e), E')
398 end
399
400 and DTs E (dbs,tbs) =
401 let val (dbs, E1) = DBs E dbs
402 val (tbs, E2) = TDs E tbs
403 in (dbs, tbs, E1 ++ E2) end
404
405 and DBs E [] = ([], Env.empty)
406 | DBs E (db::dbs) =
407 let val (db, E) = DB E db
408 val (dbs, E') = DBs E dbs
409 in (db::dbs, E++E') end
410
411 and DB E (db as DATATYPEbind{id,tyvars,cbs,...}) = (db, Env.empty)
412
413 and TDs E [] = ([], Env.empty)
414 | TDs E (tb::tbs) =
415 let val (tb,E) = TD E tb
416 val (tbs,E') = TDs E tbs
417 in (tb::tbs, E++E') end
418
419 and TD E (TYPEbind(id,tvs,t)) =
420 let val tve = ref(map (fn tv => (tv,Env.var E)) tvs)
421 val t' = Env.lambda E (T' E tve t)
422 in (TYPEbind(id,tvs,t), Env.TYPEbind(id,t')) end
423
424 and T E t =
425 let val tvs = ref []
426 val t = T' E tvs t
427 val (_, t) = Env.gen E (INTexp 0, t)
428 in t
429 end
430
431 and T' E tvs (IDty id) = Env.lookupTy E id
432 | T' E tvs (ty as APPty(f,tys)) =
433 let val t = Env.lookupTy E f
434 val ts = map (T' E tvs) tys
435 in TypeUtil.apply(" in type "^t2s ty, t, ts) end
436 | T' E tvs (FUNty(x,y)) = FUNty(T' E tvs x,T' E tvs y)
437 | T' E tvs (TUPLEty ts) = TUPLEty(map (T' E tvs) ts)
438 | T' E tvs (RECORDty lts) = RECORDty(map (LT' E tvs) lts)
439 | T' E tvs (t as INTVARty _) = t
440 | T' E tvs (ty as TYVARty tv) =
441 let fun scan [] = let val v = boundVar E tv
442 in tvs := (tv,v) :: !tvs; v end
443 | scan((k,v)::tvs) = if k = tv then v else scan tvs
444 in scan(!tvs) end
445 | T' E tvs t = (error("unknown type "^t2s t); t)
446
447 and LT' E tvs (l,t) = (l,T' E tvs t)
448
449 and SE E (IDsexp id) = (IDsexp id, Env.lookupStr E id)
450 | SE E (DECLsexp ds) =
451 let val (ds, E) = Ds E ds
452 in (DECLsexp ds, E) end
453
454 val E = Env.empty
455 val (d, E) = D E d
456 in (d, E)
457 end
458
459 end