Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/branches/primop-branch-2/src/compiler/FLINT/trans/translate.sml
ViewVC logotype

Annotation of /sml/branches/primop-branch-2/src/compiler/FLINT/trans/translate.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1682 - (view) (download)
Original Path: sml/trunk/src/compiler/FLINT/trans/translate.sml

1 : monnier 16 (* COPYRIGHT (c) 1996 Bell Laboratories *)
2 :     (* translate.sml *)
3 :    
4 :     signature TRANSLATE =
5 :     sig
6 :    
7 :     (* Invariant: transDec always applies to a top-level absyn declaration *)
8 : blume 1078 val transDec : { rootdec: Absyn.dec,
9 :     exportLvars: Access.lvar list,
10 :     env: StaticEnv.staticEnv,
11 :     cproto_conv: string,
12 :     compInfo: Absyn.dec CompInfo.compInfo }
13 : monnier 45 -> {flint: FLINT.prog,
14 : monnier 100 imports: (PersStamps.persstamp
15 : blume 879 * ImportTree.importTree) list}
16 : monnier 16
17 :     end (* signature TRANSLATE *)
18 :    
19 :     structure Translate : TRANSLATE =
20 :     struct
21 :    
22 :     local structure B = Bindings
23 :     structure BT = BasicTypes
24 :     structure DA = Access
25 :     structure DI = DebIndex
26 :     structure EM = ErrorMsg
27 :     structure II = InlInfo
28 :     structure LT = PLambdaType
29 :     structure M = Modules
30 :     structure MC = MatchComp
31 :     structure PO = PrimOp
32 :     structure PP = PrettyPrint
33 :     structure S = Symbol
34 : monnier 100 structure SP = SymPath
35 : monnier 16 structure LN = LiteralToNum
36 :     structure TT = TransTypes
37 :     structure TP = Types
38 :     structure TU = TypesUtil
39 :     structure V = VarCon
40 : mblume 1347 structure EU = ElabUtil
41 : monnier 16
42 : mblume 1347 structure IIMap = RedBlackMapFn (type ord_key = IntInf.int
43 :     val compare = IntInf.compare)
44 : monnier 16
45 :     open Absyn PLambda
46 :     in
47 :    
48 :     (****************************************************************************
49 :     * CONSTANTS AND UTILITY FUNCTIONS *
50 :     ****************************************************************************)
51 :    
52 :     val debugging = ref true
53 :     fun bug msg = EM.impossible("Translate: " ^ msg)
54 :     val say = Control.Print.say
55 :     val ppDepth = Control.Print.printDepth
56 :    
57 :     fun ppType ty =
58 :     ElabDebug.withInternals
59 :     (fn () => ElabDebug.debugPrint debugging
60 :     ("type: ",PPType.ppType StaticEnv.empty, ty))
61 :    
62 :     fun ident x = x
63 :     val unitLexp = RECORD []
64 :    
65 : monnier 100 fun getNameOp p = if SP.null p then NONE else SOME(SP.last p)
66 :    
67 : monnier 16 type pid = PersStamps.persstamp
68 :    
69 :     (** old-style fold for cases where it is partially applied *)
70 :     fun fold f l init = foldr f init l
71 :    
72 :     (** sorting the record fields for record types and record expressions *)
73 :     fun elemgtr ((LABEL{number=x,...},_),(LABEL{number=y,...},_)) = (x>y)
74 : monnier 422 fun sorted x = ListMergeSort.sorted elemgtr x
75 :     fun sortrec x = ListMergeSort.sort elemgtr x
76 : monnier 16
77 : monnier 100 (** check if an access is external *)
78 :     fun extern (DA.EXTERN _) = true
79 :     | extern (DA.PATH(a, _)) = extern a
80 :     | extern _ = false
81 :    
82 : monnier 45 (** an exception raised if coreEnv is not available *)
83 :     exception NoCore
84 :    
85 :     (****************************************************************************
86 :     * MAIN FUNCTION *
87 :     * *
88 : monnier 100 * val transDec : Absyn.dec * Access.lvar list *
89 :     * * StaticEnv.staticEnv * CompBasic.compInfo *
90 :     * -> {flint: FLINT.prog, *
91 :     * imports: (PersStamps.persstamp *
92 : blume 879 * * ImportTree.importTree) list} *
93 : monnier 45 ****************************************************************************)
94 :    
95 : blume 902 fun transDec
96 : blume 1078 { rootdec, exportLvars, env, cproto_conv,
97 :     compInfo as {errorMatch,error,...}: Absyn.dec CompInfo.compInfo } =
98 : monnier 45 let
99 :    
100 : blume 903 (* We take mkLvar from compInfo. This should answer Zhong's question... *)
101 :     (*
102 :     (*
103 :     * MAJOR CLEANUP REQUIRED ! The function mkv is currently directly taken
104 :     * from the LambdaVar module; I think it should be taken from the
105 :     * "compInfo". Similarly, should we replace all mkLvar in the backend
106 :     * with the mkv in "compInfo" ? (ZHONG)
107 :     *)
108 :     val mkv = LambdaVar.mkLvar
109 :     fun mkvN NONE = mkv()
110 :     | mkvN (SOME s) = LambdaVar.namedLvar s
111 :     *)
112 :    
113 :     val mkvN = #mkLvar compInfo
114 :     fun mkv () = mkvN NONE
115 :    
116 : monnier 45 (** generate the set of ML-to-FLINT type translation functions *)
117 : blume 902 val {tpsKnd, tpsTyc, toTyc, toLty, strLty, fctLty, markLBOUND} =
118 :     TT.genTT()
119 : monnier 45 fun toTcLt d = (toTyc d, toLty d)
120 :    
121 : monnier 16 (** translating the typ field in DATACON into lty; constant datacons
122 :     will take ltc_unit as the argument *)
123 :     fun toDconLty d ty =
124 :     (case ty
125 :     of TP.POLYty{sign, tyfun=TP.TYFUN{arity, body}} =>
126 : monnier 45 if BT.isArrowType body then toLty d ty
127 :     else toLty d (TP.POLYty{sign=sign,
128 : monnier 16 tyfun=TP.TYFUN{arity=arity,
129 :     body=BT.-->(BT.unitTy, body)}})
130 : monnier 45 | _ => if BT.isArrowType ty then toLty d ty
131 :     else toLty d (BT.-->(BT.unitTy, ty)))
132 : monnier 16
133 :     (** the special lookup functions for the Core environment *)
134 :     fun coreLookup(id, env) =
135 : blume 592 let val sp = SymPath.SPATH [CoreSym.coreSym, S.varSymbol id]
136 : monnier 16 val err = fn _ => fn _ => fn _ => raise NoCore
137 :     in Lookup.lookVal(env, sp, err)
138 :     end
139 :    
140 :     fun CON' ((_, DA.REF, lt), ts, e) = APP (PRIM (PO.MAKEREF, lt, ts), e)
141 :     | CON' ((_, DA.SUSP (SOME(DA.LVAR d, _)), lt), ts, e) =
142 : monnier 109 let val v = mkv ()
143 :     val fe = FN (v, LT.ltc_tuple [], e)
144 :     in APP(TAPP (VAR d, ts), fe)
145 :     end
146 : monnier 16 | CON' x = CON x
147 :    
148 :     (*
149 :     * The following code implements the exception tracking and
150 :     * errormsg reporting.
151 :     *)
152 :    
153 :     local val region = ref(0,0)
154 :     val markexn = PRIM(PO.MARKEXN,
155 : monnier 69 LT.ltc_parrow(LT.ltc_tuple [LT.ltc_exn, LT.ltc_string],
156 :     LT.ltc_exn), [])
157 : monnier 16 in
158 :    
159 :     fun withRegion loc f x =
160 :     let val r = !region
161 :     in (region := loc; f x before region:=r)
162 :     handle e => (region := r; raise e)
163 :     end
164 :    
165 :     fun mkRaise(x, lt) =
166 :     let val e = if !Control.trackExn
167 :     then APP(markexn, RECORD[x, STRING(errorMatch(!region))])
168 :     else x
169 :     in RAISE(e, lt)
170 :     end
171 :    
172 :     fun complain s = error (!region) s
173 :     fun repErr x = complain EM.COMPLAIN x EM.nullErrorBody
174 : monnier 504 fun repPolyEq () =
175 :     if !Control.polyEqWarn then complain EM.WARN "calling polyEqual" EM.nullErrorBody
176 :     else ()
177 : monnier 16
178 :     end (* markexn-local *)
179 :    
180 : monnier 100 (***************************************************************************
181 :     * SHARING AND LIFTING OF STRUCTURE IMPORTS AND ACCESSES *
182 :     ***************************************************************************)
183 :    
184 : monnier 16 exception HASHTABLE
185 :     type key = int
186 :    
187 : monnier 100 (** hashkey of accesspath + accesspath + resvar *)
188 :     type info = (key * int list * lvar)
189 : blume 733 val hashtable : info list IntHashTable.hash_table =
190 :     IntHashTable.mkTable(32,HASHTABLE)
191 : monnier 16 fun hashkey l = foldr (fn (x,y) => ((x * 10 + y) mod 1019)) 0 l
192 :    
193 : monnier 100 fun buildHdr v =
194 : blume 733 let val info = IntHashTable.lookup hashtable v
195 : monnier 100 fun h((_, l, w), hdr) =
196 : monnier 16 let val le = foldl (fn (k,e) => SELECT(k,e)) (VAR v) l
197 : monnier 100 in fn e => hdr(LET(w, le, e))
198 : monnier 16 end
199 :     in foldr h ident info
200 :     end handle _ => ident
201 :    
202 : monnier 100 fun bindvar (v, [], _) = v
203 :     | bindvar (v, l, nameOp) =
204 : blume 733 let val info = (IntHashTable.lookup hashtable v) handle _ => []
205 : monnier 100 val key = hashkey l
206 :     fun h [] =
207 :     let val u = mkvN nameOp
208 : blume 733 in IntHashTable.insert hashtable (v,(key,l,u)::info); u
209 : monnier 100 end
210 :     | h((k',l',w)::r) =
211 :     if (k' = key) then (if (l'=l) then w else h r) else h r
212 :     in h info
213 :     end
214 : monnier 16
215 : monnier 100 datatype pidInfo = ANON of (int * pidInfo) list
216 :     | NAMED of lvar * lty * (int * pidInfo) list
217 :    
218 :     fun mkPidInfo (t, l, nameOp) =
219 :     let val v = mkvN nameOp
220 :     fun h [] = NAMED(v, t, [])
221 :     | h (a::r) = ANON [(a, h r)]
222 :     in (h l, v)
223 : monnier 16 end
224 :    
225 : monnier 100 fun mergePidInfo (pi, t, l, nameOp) =
226 :     let fun h (z as NAMED(v,_,_), []) = (z, v)
227 :     | h (ANON xl, []) =
228 :     let val v = mkvN nameOp
229 :     in (NAMED(v, t, xl), v)
230 :     end
231 :     | h (z, a::r) =
232 :     let val (xl, mknode) =
233 :     case z of ANON c => (c, ANON)
234 :     | NAMED (v,tt,c) => (c, fn x => NAMED(v,tt,x))
235 : monnier 16
236 : monnier 100 fun dump ((np, v), z, y) =
237 :     let val nz = (a, np)::z
238 :     in (mknode((rev y) @ nz), v)
239 :     end
240 :    
241 :     fun look ([], y) = dump(mkPidInfo(t, r, nameOp), [], y)
242 :     | look (u as ((x as (i,pi))::z), y) =
243 :     if i < a then look(z, x::y)
244 :     else if i = a then dump(h(pi, r), z, y)
245 :     else dump(mkPidInfo(t, r, nameOp), u, y)
246 :    
247 :     in look(xl, [])
248 :     end
249 :     in h(pi, l)
250 :     end (* end of mergePidInfo *)
251 :    
252 : monnier 16 (** a map that stores information about external references *)
253 : mblume 1347 val persmap = ref (PersMap.empty : pidInfo PersMap.map)
254 : monnier 16
255 : monnier 100 fun mkPid (pid, t, l, nameOp) =
256 : mblume 1347 case PersMap.find (!persmap, pid)
257 : monnier 422 of NONE =>
258 :     let val (pinfo, var) = mkPidInfo (t, l, nameOp)
259 : mblume 1347 in persmap := PersMap.insert(!persmap, pid, pinfo);
260 : monnier 422 var
261 :     end
262 :     | SOME pinfo =>
263 :     let val (npinfo, var) = mergePidInfo (pinfo, t, l, nameOp)
264 :     fun rmv (key, map) =
265 : mblume 1347 let val (newMap, _) = PersMap.remove(map, key)
266 : monnier 422 in newMap
267 :     end handle e => map
268 : mblume 1347 in persmap := PersMap.insert(rmv(pid, !persmap), pid, npinfo);
269 : monnier 422 var
270 :     end
271 : monnier 16
272 : mblume 1347 val iimap = ref (IIMap.empty : lvar IIMap.map)
273 :    
274 :     fun getII n =
275 :     case IIMap.find (!iimap, n) of
276 :     SOME v => v
277 :     | NONE => let val v = mkv ()
278 :     in
279 :     iimap := IIMap.insert (!iimap, n, v);
280 :     v
281 :     end
282 :    
283 : monnier 16 (** converting an access w. type into a lambda expression *)
284 : monnier 100 fun mkAccT (p, t, nameOp) =
285 :     let fun h(DA.LVAR v, l) = bindvar(v, l, nameOp)
286 :     | h(DA.EXTERN pid, l) = mkPid(pid, t, l, nameOp)
287 : monnier 16 | h(DA.PATH(a,i), l) = h(a, i::l)
288 :     | h _ = bug "unexpected access in mkAccT"
289 :     in VAR (h(p, []))
290 :     end (* new def for mkAccT *)
291 :    
292 :     (** converting an access into a lambda expression *)
293 : monnier 100 fun mkAcc (p, nameOp) =
294 :     let fun h(DA.LVAR v, l) = bindvar(v, l, nameOp)
295 : monnier 16 | h(DA.PATH(a,i), l) = h(a, i::l)
296 :     | h _ = bug "unexpected access in mkAcc"
297 :     in VAR (h(p, []))
298 :     end (* new def for mkAcc *)
299 :    
300 :     (*
301 :     * These two functions are major gross hacks. The NoCore exceptions would
302 :     * be raised when compiling boot/dummy.sml, boot/assembly.sig, and
303 :     * boot/core.sml; the assumption is that the result of coreExn and coreAcc
304 :     * would never be used when compiling these three files. A good way to
305 :     * clean up this is to put all the core constructors and primitives into
306 :     * the primitive environment. (ZHONG)
307 :     *)
308 : blume 904 exception NoCore
309 :    
310 : monnier 16 fun coreExn id =
311 : blume 904 (case CoreAccess.getCon' (fn () => raise NoCore) (env, id) of
312 :     TP.DATACON { name, rep as DA.EXN _, typ, ... } =>
313 :     let val nt = toDconLty DI.top typ
314 :     val nrep = mkRep(rep, nt, name)
315 :     in CON'((name, nrep, nt), [], unitLexp)
316 :     end
317 :     | _ => bug "coreExn in translate")
318 :     handle NoCore => (say "WARNING: no Core access\n"; INT 0)
319 : monnier 16
320 :     and coreAcc id =
321 : blume 904 (case CoreAccess.getVar' (fn () => raise NoCore) (env, id) of
322 :     V.VALvar { access, typ, path, ... } =>
323 :     mkAccT(access, toLty DI.top (!typ), getNameOp path)
324 :     | _ => bug "coreAcc in translate")
325 :     handle NoCore => (say "WARNING: no Core access\n"; INT 0)
326 : monnier 16
327 :     (** expands the flex record pattern and convert the EXN access pat *)
328 :     (** internalize the conrep's access, always exceptions *)
329 : monnier 100 and mkRep (rep, lt, name) =
330 :     let fun g (DA.LVAR v, l, t) = bindvar(v, l, SOME name)
331 :     | g (DA.PATH(a, i), l, t) = g(a, i::l, t)
332 :     | g (DA.EXTERN p, l, t) = mkPid(p, t, l, SOME name)
333 : monnier 16 | g _ = bug "unexpected access in mkRep"
334 :    
335 :     in case rep
336 :     of (DA.EXN x) =>
337 :     let val (argt, _) = LT.ltd_parrow lt
338 :     in DA.EXN (DA.LVAR (g(x, [], LT.ltc_etag argt)))
339 :     end
340 :     | (DA.SUSP NONE) => (* a hack to support "delay-force" primitives *)
341 :     (case (coreAcc "delay", coreAcc "force")
342 :     of (VAR x, VAR y) => DA.SUSP(SOME (DA.LVAR x, DA.LVAR y))
343 :     | _ => bug "unexpected case on conrep SUSP 1")
344 :     | (DA.SUSP (SOME _)) => bug "unexpected case on conrep SUSP 2"
345 :     | _ => rep
346 :     end
347 :    
348 :     (** converting a value of access+info into the lambda expression *)
349 : monnier 100 fun mkAccInfo (acc, info, getLty, nameOp) =
350 :     if extern acc then mkAccT(acc, getLty(), nameOp) else mkAcc (acc, nameOp)
351 : monnier 16
352 :     fun fillPat(pat, d) =
353 :     let fun fill (CONSTRAINTpat (p,t)) = fill p
354 :     | fill (LAYEREDpat (p,q)) = LAYEREDpat(fill p, fill q)
355 :     | fill (RECORDpat {fields, flex=false, typ}) =
356 :     RECORDpat{fields = map (fn (lab, p) => (lab, fill p)) fields,
357 :     typ = typ, flex = false}
358 :     | fill (pat as RECORDpat {fields, flex=true, typ}) =
359 :     let exception DontBother
360 :     val fields' = map (fn (l,p) => (l, fill p)) fields
361 :    
362 :     fun find (t as TP.CONty(TP.RECORDtyc labels, _)) =
363 :     (typ := t; labels)
364 :     | find _ = (complain EM.COMPLAIN "unresolved flexible record"
365 :     (fn ppstrm =>
366 : macqueen 1344 (PP.newline ppstrm;
367 :     PP.string ppstrm "pattern: ";
368 : monnier 16 PPAbsyn.ppPat env ppstrm
369 :     (pat,!Control.Print.printDepth)));
370 :     raise DontBother)
371 :    
372 :     fun merge (a as ((id,p)::r), lab::s) =
373 :     if S.eq(id,lab) then (id,p) :: merge(r,s)
374 :     else (lab,WILDpat) :: merge(a,s)
375 :     | merge ([], lab::s) = (lab,WILDpat) :: merge([], s)
376 :     | merge ([], []) = []
377 :     | merge _ = bug "merge in translate"
378 :    
379 :     in RECORDpat{fields = merge(fields',
380 :     find(TU.headReduceType (!typ))),
381 :     flex = false, typ = typ}
382 :     handle DontBother => WILDpat
383 :     end
384 :     | fill (VECTORpat(pats,ty)) = VECTORpat(map fill pats, ty)
385 :     | fill (ORpat(p1, p2)) = ORpat(fill p1, fill p2)
386 : monnier 109 | fill (CONpat(TP.DATACON{name, const, typ, rep, sign, lazyp}, ts)) =
387 :     CONpat(TP.DATACON{name=name, const=const, typ=typ, lazyp=lazyp,
388 : monnier 100 sign=sign, rep=mkRep(rep, toDconLty d typ, name)}, ts)
389 : monnier 109 | fill (APPpat(TP.DATACON{name, const, typ, rep, sign, lazyp}, ts, pat)) =
390 :     APPpat(TP.DATACON{name=name, const=const, typ=typ, sign=sign, lazyp=lazyp,
391 : monnier 100 rep=mkRep(rep, toDconLty d typ, name)}, ts, fill pat)
392 : monnier 16 | fill xp = xp
393 :    
394 :     in fill pat
395 :     end (* function fillPat *)
396 :    
397 :     (** The runtime polymorphic equality and string equality dictionary. *)
398 :     val eqDict =
399 :     let val strEqRef : lexp option ref = ref NONE
400 :     val polyEqRef : lexp option ref = ref NONE
401 : mblume 1347 val intInfEqRef : lexp option ref = ref NONE
402 : monnier 16
403 :     fun getStrEq () =
404 :     (case (!strEqRef)
405 :     of SOME e => e
406 :     | NONE => (let val e = coreAcc "stringequal"
407 :     in strEqRef := (SOME e); e
408 :     end))
409 :    
410 : mblume 1347 fun getIntInfEq () = (* same as polyeq, but silent *)
411 :     case !intInfEqRef of
412 :     SOME e => e
413 :     | NONE => let val e =
414 :     TAPP (coreAcc "polyequal",
415 :     [toTyc DI.top BT.intinfTy])
416 :     in
417 :     intInfEqRef := SOME e; e
418 :     end
419 :    
420 : monnier 16 fun getPolyEq () =
421 : monnier 504 (repPolyEq();
422 :     case (!polyEqRef)
423 : monnier 16 of SOME e => e
424 :     | NONE => (let val e = coreAcc "polyequal"
425 :     in polyEqRef := (SOME e); e
426 :     end))
427 : mblume 1347 in {getStrEq=getStrEq, getIntInfEq=getIntInfEq, getPolyEq=getPolyEq}
428 : monnier 16 end
429 :    
430 : monnier 504 val eqGen = PEqual.equal (eqDict, env)
431 : monnier 16
432 :     (***************************************************************************
433 :     * *
434 :     * Translating the primops; this should be moved into a separate file *
435 :     * in the future. (ZHONG) *
436 :     * *
437 :     ***************************************************************************)
438 :    
439 :     val lt_tyc = LT.ltc_tyc
440 :     val lt_arw = LT.ltc_parrow
441 :     val lt_tup = LT.ltc_tuple
442 :     val lt_int = LT.ltc_int
443 :     val lt_int32 = LT.ltc_int32
444 :     val lt_bool = LT.ltc_bool
445 : mblume 1332 val lt_unit = LT.ltc_unit
446 : monnier 16
447 :     val lt_ipair = lt_tup [lt_int, lt_int]
448 :     val lt_icmp = lt_arw (lt_ipair, lt_bool)
449 :     val lt_ineg = lt_arw (lt_int, lt_int)
450 :     val lt_intop = lt_arw (lt_ipair, lt_int)
451 : mblume 1332 val lt_u_u = lt_arw (lt_unit, lt_unit)
452 : monnier 16
453 :     val boolsign = BT.boolsign
454 :     val (trueDcon', falseDcon') =
455 :     let val lt = LT.ltc_parrow(LT.ltc_unit, LT.ltc_bool)
456 :     fun h (TP.DATACON{name,rep,typ,...}) = (name, rep, lt)
457 :     in (h BT.trueDcon, h BT.falseDcon)
458 :     end
459 :    
460 :     val trueLexp = CON(trueDcon', [], unitLexp)
461 :     val falseLexp = CON(falseDcon', [], unitLexp)
462 :    
463 :     fun COND(a,b,c) =
464 :     SWITCH(a,boolsign, [(DATAcon(trueDcon', [], mkv()),b),
465 :     (DATAcon(falseDcon', [], mkv()),c)], NONE)
466 :    
467 :     fun composeNOT (eq, t) =
468 :     let val v = mkv()
469 :     val argt = lt_tup [t, t]
470 :     in FN(v, argt, COND(APP(eq, VAR v), falseLexp, trueLexp))
471 :     end
472 :    
473 :     fun cmpOp p = PRIM(p, lt_icmp, [])
474 :     fun inegOp p = PRIM(p, lt_ineg, [])
475 :    
476 :     val LESSU = PO.CMP{oper=PO.LTU, kind=PO.UINT 31}
477 :    
478 :     val lt_len = LT.ltc_poly([LT.tkc_mono], [lt_arw(LT.ltc_tv 0, lt_int)])
479 :     val lt_upd =
480 :     let val x = LT.ltc_ref (LT.ltc_tv 0)
481 :     in LT.ltc_poly([LT.tkc_mono],
482 :     [lt_arw(lt_tup [x, lt_int, LT.ltc_tv 0], LT.ltc_unit)])
483 :     end
484 :    
485 :     fun lenOp(tc) = PRIM(PO.LENGTH, lt_len, [tc])
486 :    
487 :     fun rshiftOp k = PO.ARITH{oper=PO.RSHIFT, overflow=false, kind=k}
488 :     fun rshiftlOp k = PO.ARITH{oper=PO.RSHIFTL, overflow=false, kind=k}
489 :     fun lshiftOp k = PO.ARITH{oper=PO.LSHIFT, overflow=false, kind=k}
490 :    
491 :     fun lword0 (PO.UINT 31) = WORD 0w0
492 :     | lword0 (PO.UINT 32) = WORD32 0w0
493 :     | lword0 _ = bug "unexpected case in lword0"
494 :    
495 :     fun baselt (PO.UINT 31) = lt_int
496 :     | baselt (PO.UINT 32) = lt_int32
497 :     | baselt _ = bug "unexpected case in baselt"
498 :    
499 :     fun shiftTy k =
500 :     let val elem = baselt k
501 :     val tupt = lt_tup [elem, lt_int]
502 :     in lt_arw(tupt, elem)
503 :     end
504 :    
505 :     fun inlineShift(shiftOp, kind, clear) =
506 :     let fun shiftLimit (PO.UINT lim) = WORD(Word.fromInt lim)
507 :     | shiftLimit _ = bug "unexpected case in shiftLimit"
508 :    
509 :     val p = mkv() val vp = VAR p
510 :     val w = mkv() val vw = VAR w
511 :     val cnt = mkv() val vcnt = VAR cnt
512 :    
513 :     val argt = lt_tup [baselt(kind), lt_int]
514 :     val cmpShiftAmt =
515 :     PRIM(PO.CMP{oper=PO.LEU, kind=PO.UINT 31}, lt_icmp, [])
516 :     in FN(p, argt,
517 :     LET(w, SELECT(0, vp),
518 :     LET(cnt, SELECT(1, vp),
519 :     COND(APP(cmpShiftAmt, RECORD [shiftLimit(kind), vcnt]),
520 :     clear vw,
521 :     APP(PRIM(shiftOp(kind), shiftTy(kind), []),
522 :     RECORD [vw, vcnt])))))
523 :     end
524 :    
525 : blume 1183 fun inlops nk = let
526 :     val (lt_arg, zero, overflow) =
527 :     case nk of
528 :     PO.INT 31 => (LT.ltc_int, INT 0, true)
529 :     | PO.UINT 31 => (LT.ltc_int, WORD 0w0, false)
530 :     | PO.INT 32 => (LT.ltc_int32, INT32 0, true)
531 :     | PO.UINT 32 => (LT.ltc_int32, WORD32 0w0, false)
532 :     | PO.FLOAT 64 => (LT.ltc_real, REAL "0.0", false)
533 :     | _ => bug "inlops: bad numkind"
534 :     val lt_argpair = lt_tup [lt_arg, lt_arg]
535 :     val lt_cmp = lt_arw (lt_argpair, lt_bool)
536 :     val lt_neg = lt_arw (lt_arg, lt_arg)
537 :     val less = PRIM (PO.CMP { oper = PO.<, kind = nk }, lt_cmp, [])
538 :     val greater = PRIM (PO.CMP { oper = PO.>, kind = nk }, lt_cmp, [])
539 :     val negate =
540 :     PRIM (PO.ARITH { oper = PO.~, overflow = overflow, kind = nk },
541 :     lt_neg, [])
542 :     in
543 :     { lt_arg = lt_arg, lt_argpair = lt_argpair, lt_cmp = lt_cmp,
544 :     less = less, greater = greater,
545 :     zero = zero, negate = negate }
546 :     end
547 : monnier 16
548 : blume 1183 fun inlminmax (nk, ismax) = let
549 :     val { lt_argpair, less, greater, lt_cmp, ... } = inlops nk
550 :     val x = mkv () and y = mkv () and z = mkv ()
551 :     val cmpop = if ismax then greater else less
552 :     val elsebranch =
553 :     case nk of
554 :     PO.FLOAT _ => let
555 :     (* testing for NaN *)
556 :     val fequal =
557 :     PRIM (PO.CMP { oper = PO.EQL, kind = nk }, lt_cmp, [])
558 :     in
559 : blume 1249 COND (APP (fequal, RECORD [VAR y, VAR y]), VAR y, VAR x)
560 : blume 1183 end
561 :     | _ => VAR y
562 :     in
563 :     FN (z, lt_argpair,
564 :     LET (x, SELECT (0, VAR z),
565 :     LET (y, SELECT (1, VAR z),
566 :     COND (APP (cmpop, RECORD [VAR x, VAR y]),
567 :     VAR x, elsebranch))))
568 :     end
569 :    
570 :     fun inlabs nk = let
571 :     val { lt_arg, greater, zero, negate, ... } = inlops nk
572 :     val x = mkv ()
573 :     in
574 :     FN (x, lt_arg,
575 :     COND (APP (greater, RECORD [VAR x, zero]),
576 :     VAR x, APP (negate, VAR x)))
577 :     end
578 :    
579 : mblume 1347 fun inl_infPrec (what, corename, p, lt, is_from_inf) = let
580 :     val (orig_arg_lt, res_lt) =
581 :     case LT.ltd_arrow lt of
582 :     (_, [a], [r]) => (a, r)
583 :     | _ => bug ("unexpected type of " ^ what)
584 :     val extra_arg_lt =
585 :     LT.ltc_parrow (if is_from_inf then (orig_arg_lt, LT.ltc_int32)
586 :     else (LT.ltc_int32, orig_arg_lt))
587 :     val new_arg_lt = LT.ltc_tuple [orig_arg_lt, extra_arg_lt]
588 :     val new_lt = LT.ltc_parrow (new_arg_lt, res_lt)
589 :     val x = mkv ()
590 :     in
591 :     FN (x, orig_arg_lt,
592 :     APP (PRIM (p, new_lt, []),
593 :     RECORD [VAR x, coreAcc corename]))
594 :     end
595 :    
596 : monnier 16 fun transPrim (prim, lt, ts) =
597 :     let fun g (PO.INLLSHIFT k) = inlineShift(lshiftOp, k, fn _ => lword0(k))
598 :     | g (PO.INLRSHIFTL k) = inlineShift(rshiftlOp, k, fn _ => lword0(k))
599 :     | g (PO.INLRSHIFT k) = (* preserve sign bit with arithmetic rshift *)
600 :     let fun clear w = APP(PRIM(rshiftOp k, shiftTy k, []),
601 :     RECORD [w, WORD 0w31])
602 :     in inlineShift(rshiftOp, k, clear)
603 :     end
604 :    
605 : blume 1183 | g (PO.INLMIN nk) = inlminmax (nk, false)
606 :     | g (PO.INLMAX nk) = inlminmax (nk, true)
607 :     | g (PO.INLABS nk) = inlabs nk
608 : monnier 16
609 :     | g (PO.INLNOT) =
610 :     let val x = mkv()
611 :     in FN(x, lt_bool, COND(VAR x, falseLexp, trueLexp))
612 :     end
613 :    
614 :     | g (PO.INLCOMPOSE) =
615 :     let val (t1, t2, t3) =
616 :     case ts of [a,b,c] => (lt_tyc a, lt_tyc b, lt_tyc c)
617 :     | _ => bug "unexpected type for INLCOMPOSE"
618 :    
619 :     val argt = lt_tup [lt_arw(t2, t3), lt_arw(t1, t2)]
620 :    
621 :     val x = mkv() and z = mkv()
622 :     val f = mkv() and g = mkv()
623 :     in FN(z, argt,
624 :     LET(f, SELECT(0,VAR z),
625 :     LET(g,SELECT(1,VAR z),
626 :     FN(x, t1, APP(VAR f,APP(VAR g,VAR x))))))
627 :     end
628 :     | g (PO.INLBEFORE) =
629 :     let val (t1, t2) =
630 :     case ts of [a,b] => (lt_tyc a, lt_tyc b)
631 :     | _ => bug "unexpected type for INLBEFORE"
632 :     val argt = lt_tup [t1, t2]
633 :     val x = mkv()
634 :     in FN(x, argt, SELECT(0,VAR x))
635 :     end
636 : blume 1183 | g (PO.INLIGNORE) =
637 :     let val argt =
638 :     case ts of [a] => lt_tyc a
639 :     | _ => bug "unexpected type for INLIGNORE"
640 :     in FN (mkv (), argt, unitLexp)
641 :     end
642 : monnier 16
643 : mblume 1347 | g (PO.INLIDENTITY) =
644 :     let val argt =
645 :     case ts of [a] => lt_tyc a
646 :     | _ => bug "unexpected type for INLIDENTITY"
647 :     val v = mkv ()
648 :     in
649 :     FN (v, argt, VAR v)
650 :     end
651 :    
652 : monnier 16 | g (PO.INLSUBSCRIPTV) =
653 :     let val (tc1, t1) = case ts of [z] => (z, lt_tyc z)
654 :     | _ => bug "unexpected ty for INLSUBV"
655 :    
656 :     val seqtc = LT.tcc_vector tc1
657 :     val argt = lt_tup [lt_tyc seqtc, lt_int]
658 :    
659 :     val oper = PRIM(PO.SUBSCRIPT, lt, ts)
660 :     val p = mkv() and a = mkv() and i = mkv()
661 :     val vp = VAR p and va = VAR a and vi = VAR i
662 :     in FN(p, argt,
663 :     LET(a, SELECT(0,vp),
664 :     LET(i, SELECT(1,vp),
665 :     COND(APP(cmpOp(LESSU),
666 :     RECORD[vi, APP(lenOp seqtc, va)]),
667 :     APP(oper, RECORD[va, vi]),
668 :     mkRaise(coreExn "Subscript", t1)))))
669 :     end
670 :    
671 :     | g (PO.INLSUBSCRIPT) =
672 :     let val (tc1, t1) = case ts of [z] => (z, lt_tyc z)
673 :     | _ => bug "unexpected ty for INLSUB"
674 :    
675 :     val seqtc = LT.tcc_array tc1
676 :     val argt = lt_tup [lt_tyc seqtc, lt_int]
677 :    
678 :     val oper = PRIM(PO.SUBSCRIPT, lt, ts)
679 :     val p = mkv() and a = mkv() and i = mkv()
680 :     val vp = VAR p and va = VAR a and vi = VAR i
681 :     in FN(p, argt,
682 :     LET(a, SELECT(0, vp),
683 :     LET(i, SELECT(1, vp),
684 :     COND(APP(cmpOp(LESSU),
685 :     RECORD[vi, APP(lenOp seqtc, va)]),
686 :     APP(oper, RECORD[va, vi]),
687 :     mkRaise(coreExn "Subscript", t1)))))
688 :     end
689 :    
690 :     | g (PO.NUMSUBSCRIPT{kind,checked=true,immutable}) =
691 :     let val (tc1, t1, t2) =
692 :     case ts of [a,b] => (a, lt_tyc a, lt_tyc b)
693 :     | _ => bug "unexpected type for NUMSUB"
694 :    
695 :     val argt = lt_tup [t1, lt_int]
696 :     val p = mkv() and a = mkv() and i = mkv()
697 :     val vp = VAR p and va = VAR a and vi = VAR i
698 :     val oper = PO.NUMSUBSCRIPT{kind=kind,checked=false,
699 :     immutable=immutable}
700 :     val oper' = PRIM(oper, lt, ts)
701 :     in FN(p, argt,
702 :     LET(a, SELECT(0, vp),
703 :     LET(i, SELECT(1, vp),
704 :     COND(APP(cmpOp(LESSU), RECORD[vi,
705 :     APP(lenOp tc1, va)]),
706 :     APP(oper', RECORD [va, vi]),
707 :     mkRaise(coreExn "Subscript", t2)))))
708 :     end
709 :    
710 :     | g (PO.INLUPDATE) =
711 :     let val (tc1, t1) = case ts of [z] => (z, lt_tyc z)
712 :     | _ => bug "unexpected ty for INLSUB"
713 :    
714 :     val seqtc = LT.tcc_array tc1
715 :     val argt = lt_tup [lt_tyc seqtc, lt_int, t1]
716 :    
717 :     val oper = PRIM(PO.UPDATE, lt, ts)
718 :     val x = mkv() and a = mkv() and i = mkv() and v = mkv()
719 :     val vx = VAR x and va = VAR a and vi = VAR i and vv = VAR v
720 :    
721 :     in FN(x, argt,
722 :     LET(a, SELECT(0, vx),
723 :     LET(i, SELECT(1, vx),
724 :     LET(v, SELECT(2, vx),
725 :     COND(APP(cmpOp(LESSU),
726 :     RECORD[vi,APP(lenOp seqtc, va)]),
727 :     APP(oper, RECORD[va,vi,vv]),
728 : monnier 45 mkRaise(coreExn "Subscript", LT.ltc_unit))))))
729 : monnier 16 end
730 :    
731 :     | g (PO.NUMUPDATE{kind,checked=true}) =
732 :     let val (tc1, t1, t2) =
733 :     case ts of [a,b] => (a, lt_tyc a, lt_tyc b)
734 :     | _ => bug "unexpected type for NUMUPDATE"
735 :    
736 :     val argt = lt_tup [t1, lt_int, t2]
737 :    
738 :     val p=mkv() and a=mkv() and i=mkv() and v=mkv()
739 :     val vp=VAR p and va=VAR a and vi=VAR i and vv=VAR v
740 :    
741 :     val oper = PO.NUMUPDATE{kind=kind,checked=false}
742 :     val oper' = PRIM(oper, lt, ts)
743 :     in FN(p, argt,
744 :     LET(a, SELECT(0, vp),
745 :     LET(i, SELECT(1, vp),
746 :     LET(v, SELECT(2, vp),
747 :     COND(APP(cmpOp(LESSU),
748 :     RECORD[vi,APP(lenOp tc1, va)]),
749 :     APP(oper', RECORD[va,vi,vv]),
750 : monnier 45 mkRaise(coreExn "Subscript", LT.ltc_unit))))))
751 : monnier 16 end
752 :    
753 : monnier 251 (**** ASSIGN(r, x) <> UPDATE(r, 0, x) under new array reps (JHR;1998-10-30)
754 : monnier 16 | g (PO.ASSIGN) =
755 :     let val (tc1, t1) = case ts of [z] => (z, lt_tyc z)
756 :     | _ => bug "unexpected ty for ASSIGN"
757 :    
758 :     val seqtc = LT.tcc_ref tc1
759 :     val argt = lt_tup [lt_tyc seqtc, t1]
760 :    
761 :     val oper = PRIM(PO.UPDATE, lt_upd, [tc1])
762 :    
763 :     val x = mkv()
764 :     val varX = VAR x
765 :    
766 :     in FN(x, argt,
767 :     APP(oper, RECORD[SELECT(0, varX), INT 0, SELECT(1, varX)]))
768 :     end
769 : monnier 251 ****)
770 : monnier 16
771 : mblume 1347 (* Precision-conversion operations involving IntInf.
772 :     * These need to be translated specially by providing
773 :     * a second argument -- the routine from _Core that
774 :     * does the actual conversion to or from IntInf. *)
775 :    
776 :     | g (p as PO.TEST_INF prec) =
777 :     inl_infPrec ("TEST_INF", "testInf", p, lt, true)
778 :     | g (p as PO.TRUNC_INF prec) =
779 :     inl_infPrec ("TRUNC_INF", "truncInf", p, lt, true)
780 :     | g (p as PO.EXTEND_INF prec) =
781 :     inl_infPrec ("EXTEND_INF", "finToInf", p, lt, false)
782 :     | g (p as PO.COPY_INF prec) =
783 :     inl_infPrec ("COPY", "finToInf", p, lt, false)
784 :    
785 :     (* default handling for all other primops *)
786 : monnier 16 | g p = PRIM(p, lt, ts)
787 :    
788 :     in g prim
789 :     end (* function transPrim *)
790 :    
791 : mblume 1347 fun genintinfswitch (sv, cases, default) = let
792 :     val v = mkv ()
793 :    
794 :     (* build a chain of equality tests for checking large pattern values *)
795 :     fun build [] = default
796 :     | build ((n, e) :: r) =
797 :     COND (APP (#getIntInfEq eqDict (), RECORD [VAR v, VAR (getII n)]),
798 :     e, build r)
799 :    
800 :     (* split pattern values into small values and large values;
801 :     * small values can be handled directly using SWITCH *)
802 :     fun split ([], s, l) = (rev s, rev l)
803 :     | split ((n, e) :: r, sm, lg) =
804 :     (case LN.lowVal n of
805 :     SOME l => split (r, (INTcon l, e) :: sm, lg)
806 :     | NONE => split (r, sm, (n, e) :: lg))
807 :    
808 :     fun gen () =
809 :     case split (cases, [], []) of
810 :     ([], largeints) => build largeints
811 :     | (smallints, largeints) => let
812 :     val iv = mkv ()
813 :     in
814 :     LET (iv, APP (coreAcc "infLowValue", VAR v),
815 :     SWITCH (VAR iv,
816 :     DA.CNIL, smallints, SOME (build largeints)))
817 :     end
818 :     in
819 :     LET (v, sv, gen ())
820 :     end
821 :    
822 :    
823 : monnier 16 (***************************************************************************
824 :     * *
825 :     * Translating various bindings into lambda expressions: *
826 :     * *
827 :     * val mkVar : V.var * DI.depth -> L.lexp *
828 :     * val mkVE : V.var * T.ty list -> L.lexp *
829 :     * val mkCE : T.datacon * T.ty list * L.lexp option * DI.depth -> L.lexp *
830 :     * val mkStr : M.Structure * DI.depth -> L.lexp *
831 :     * val mkFct : M.Functor * DI.depth -> L.lexp *
832 :     * val mkBnd : DI.depth -> B.binding -> L.lexp *
833 :     * *
834 :     ***************************************************************************)
835 : monnier 100 fun mkVar (v as V.VALvar{access, info, typ, path}, d) =
836 :     mkAccInfo(access, info, fn () => toLty d (!typ), getNameOp path)
837 : monnier 16 | mkVar _ = bug "unexpected vars in mkVar"
838 :    
839 : blume 902 fun mkVE (v, ts, d) = let
840 :     fun otherwise () =
841 :     case ts of
842 :     [] => mkVar (v, d)
843 :     | _ => TAPP(mkVar(v, d), map (toTyc d) ts)
844 :     in
845 :     case v of
846 : macqueen 1374 V.VALvar { info, ... } =>
847 : blume 902 II.match info
848 : macqueen 1374 { inl_prim = fn (p, typ) =>
849 : blume 902 (case (p, ts) of
850 :     (PO.POLYEQL, [t]) => eqGen(typ, t, toTcLt d)
851 :     | (PO.POLYNEQ, [t]) =>
852 :     composeNOT(eqGen(typ, t, toTcLt d), toLty d t)
853 :     | (PO.INLMKARRAY, [t]) =>
854 :     let val dict =
855 :     {default = coreAcc "mkNormArray",
856 :     table = [([LT.tcc_real], coreAcc "mkRealArray")]}
857 :     in GENOP (dict, p, toLty d typ, map (toTyc d) ts)
858 :     end
859 : blume 1178 | (PO.RAW_CCALL NONE, [a, b, c]) =>
860 :     let val i = SOME (CProto.decode cproto_conv
861 :     { fun_ty = a, encoding = b })
862 :     handle CProto.BadEncoding => NONE
863 : blume 902 in PRIM (PO.RAW_CCALL i, toLty d typ, map (toTyc d) ts)
864 :     end
865 :     | _ => transPrim(p, (toLty d typ), map (toTyc d) ts)),
866 :     inl_str = fn _ => otherwise (),
867 :     inl_no = fn () => otherwise () }
868 :     | _ => otherwise ()
869 :     end
870 : monnier 16
871 :     fun mkCE (TP.DATACON{const, rep, name, typ, ...}, ts, apOp, d) =
872 :     let val lt = toDconLty d typ
873 : monnier 100 val rep' = mkRep(rep, lt, name)
874 : monnier 16 val dc = (name, rep', lt)
875 : monnier 45 val ts' = map (toTyc d) ts
876 : monnier 16 in if const then CON'(dc, ts', unitLexp)
877 :     else (case apOp
878 :     of SOME le => CON'(dc, ts', le)
879 :     | NONE =>
880 :     let val (argT, _) = LT.ltd_parrow(LT.lt_pinst(lt, ts'))
881 :     val v = mkv()
882 :     in FN(v, argT, CON'(dc, ts', VAR v))
883 :     end)
884 :     end
885 :    
886 : blume 587 fun mkStr (s as M.STR { access, info, ... }, d) =
887 :     mkAccInfo(access, info, fn () => strLty(s, d, compInfo), NONE)
888 : monnier 16 | mkStr _ = bug "unexpected structures in mkStr"
889 :    
890 : blume 587 fun mkFct (f as M.FCT { access, info, ... }, d) =
891 :     mkAccInfo(access, info, fn () => fctLty(f, d, compInfo), NONE)
892 : monnier 16 | mkFct _ = bug "unexpected functors in mkFct"
893 :    
894 :     fun mkBnd d =
895 :     let fun g (B.VALbind v) = mkVar(v, d)
896 :     | g (B.STRbind s) = mkStr(s, d)
897 :     | g (B.FCTbind f) = mkFct(f, d)
898 : monnier 100 | g (B.CONbind (TP.DATACON{rep=(DA.EXN acc), name, typ, ...})) =
899 : blume 587 let val nt = toDconLty d typ
900 :     val (argt,_) = LT.ltd_parrow nt
901 :     in mkAccT (acc, LT.ltc_etag argt, SOME name)
902 :     end
903 : monnier 16 | g _ = bug "unexpected bindings in mkBnd"
904 :     in g
905 :     end
906 :    
907 :    
908 :     (***************************************************************************
909 :     * *
910 :     * Translating core absyn declarations into lambda expressions: *
911 :     * *
912 :     * val mkVBs : Absyn.vb list * depth -> Lambda.lexp -> Lambda.lexp *
913 :     * val mkRVBs : Absyn.rvb list * depth -> Lambda.lexp -> Lambda.lexp *
914 :     * val mkEBs : Absyn.eb list * depth -> Lambda.lexp -> Lambda.lexp *
915 :     * *
916 :     ***************************************************************************)
917 :     fun mkPE (exp, d, []) = mkExp(exp, d)
918 :     | mkPE (exp, d, boundtvs) =
919 :     let val savedtvs = map ! boundtvs
920 :    
921 :     fun g (i, []) = ()
922 : blume 902 | g (i, (tv as ref (TP.OPEN _))::rest) = let
923 :     val m = markLBOUND (d, i);
924 :     in
925 :     tv := TP.TV_MARK m;
926 :     g (i+1, rest)
927 :     end
928 :     | g (i, (tv as ref (TP.TV_MARK _))::res) =
929 :     bug ("unexpected tyvar TV_MARK in mkPE")
930 : monnier 16 | g _ = bug "unexpected tyvar INSTANTIATED in mkPE"
931 :    
932 : blume 902 val _ = g(0, boundtvs) (* assign the TV_MARK tyvars *)
933 : monnier 16 val exp' = mkExp(exp, DI.next d)
934 :    
935 :     fun h ([], []) = ()
936 :     | h (a::r, b::z) = (b := a; h(r, z))
937 :     | h _ = bug "unexpected cases in mkPE"
938 :    
939 :     val _ = h(savedtvs, boundtvs) (* recover *)
940 :     val len = length(boundtvs)
941 :    
942 :     in TFN(LT.tkc_arg(len), exp')
943 :     end
944 :    
945 :     and mkVBs (vbs, d) =
946 :     let fun eqTvs ([], []) = true
947 :     | eqTvs (a::r, (TP.VARty b)::s) = if (a=b) then eqTvs(r, s) else false
948 :     | eqTvs _ = false
949 :    
950 :     fun g (VB{pat=VARpat(V.VALvar{access=DA.LVAR v, ...}),
951 :     exp as VARexp (ref (w as (V.VALvar _)), instys),
952 :     boundtvs=tvs, ...}, b) =
953 :     if eqTvs(tvs, instys) then LET(v, mkVar(w, d), b)
954 :     else LET(v, mkPE(exp, d, tvs), b)
955 :    
956 :     | g (VB{pat=VARpat(V.VALvar{access=DA.LVAR v, ...}),
957 :     exp, boundtvs=tvs, ...}, b) = LET(v, mkPE(exp, d, tvs), b)
958 :    
959 :     | g (VB{pat=CONSTRAINTpat(VARpat(V.VALvar{access=DA.LVAR v, ...}),_),
960 :     exp, boundtvs=tvs, ...}, b) = LET(v, mkPE(exp, d, tvs), b)
961 :    
962 :     | g (VB{pat, exp, boundtvs=tvs, ...}, b) =
963 :     let val ee = mkPE(exp, d, tvs)
964 :     val rules = [(fillPat(pat, d), b), (WILDpat, unitLexp)]
965 :     val rootv = mkv()
966 :     fun finish x = LET(rootv, ee, x)
967 : mblume 1347 in MC.bindCompile(env, rules, finish, rootv, toTcLt d, complain,
968 :     genintinfswitch)
969 : monnier 16 end
970 :     in fold g vbs
971 :     end
972 :    
973 :     and mkRVBs (rvbs, d) =
974 :     let fun g (RVB{var=V.VALvar{access=DA.LVAR v, typ=ref ty, ...},
975 :     exp, boundtvs=tvs, ...}, (vlist, tlist, elist)) =
976 :     let val ee = mkExp(exp, d) (* was mkPE(exp, d, tvs) *)
977 :     (* we no longer track type bindings at RVB anymore ! *)
978 : monnier 45 val vt = toLty d ty
979 : monnier 16 in (v::vlist, vt::tlist, ee::elist)
980 :     end
981 :     | g _ = bug "unexpected valrec bindings in mkRVBs"
982 :    
983 :     val (vlist, tlist, elist) = foldr g ([], [], []) rvbs
984 :    
985 :     in fn b => FIX(vlist, tlist, elist, b)
986 :     end
987 :    
988 :     and mkEBs (ebs, d) =
989 :     let fun g (EBgen {exn=TP.DATACON{rep=DA.EXN(DA.LVAR v), typ, ...},
990 :     ident, ...}, b) =
991 :     let val nt = toDconLty d typ
992 :     val (argt, _) = LT.ltd_parrow nt
993 :     in LET(v, ETAG(mkExp(ident, d), argt), b)
994 :     end
995 : monnier 100 | g (EBdef {exn=TP.DATACON{rep=DA.EXN(DA.LVAR v), typ, name, ...},
996 : monnier 16 edef=TP.DATACON{rep=DA.EXN(acc), ...}}, b) =
997 :     let val nt = toDconLty d typ
998 :     val (argt, _) = LT.ltd_parrow nt
999 : monnier 100 in LET(v, mkAccT(acc, LT.ltc_etag argt, SOME name), b)
1000 : monnier 16 end
1001 :     | g _ = bug "unexpected exn bindings in mkEBs"
1002 :    
1003 :     in fold g ebs
1004 :     end
1005 :    
1006 :    
1007 :     (***************************************************************************
1008 :     * *
1009 :     * Translating module exprs and decls into lambda expressions: *
1010 :     * *
1011 :     * val mkStrexp : Absyn.strexp * depth -> Lambda.lexp *
1012 :     * val mkFctexp : Absyn.fctexp * depth -> Lambda.lexp *
1013 :     * val mkStrbs : Absyn.strb list * depth -> Lambda.lexp -> Lambda.lexp *
1014 :     * val mkFctbs : Absyn.fctb list * depth -> Lambda.lexp -> Lambda.lexp *
1015 :     * *
1016 :     ***************************************************************************)
1017 :     and mkStrexp (se, d) =
1018 :     let fun g (VARstr s) = mkStr(s, d)
1019 :     | g (STRstr bs) = SRECORD (map (mkBnd d) bs)
1020 :     | g (APPstr {oper, arg, argtycs}) =
1021 :     let val e1 = mkFct(oper, d)
1022 : monnier 45 val tycs = map (tpsTyc d) argtycs
1023 : monnier 16 val e2 = mkStr(arg, d)
1024 :     in APP(TAPP(e1, tycs), e2)
1025 :     end
1026 :     | g (LETstr (dec, b)) = mkDec (dec, d) (g b)
1027 :     | g (MARKstr (b, reg)) = withRegion reg g b
1028 :    
1029 :     in g se
1030 :     end
1031 :    
1032 :     and mkFctexp (fe, d) =
1033 :     let fun g (VARfct f) = mkFct(f, d)
1034 : blume 587 | g (FCTfct {param as M.STR { access, ... }, argtycs, def }) =
1035 :     (case access of
1036 :     DA.LVAR v =>
1037 :     let val knds = map tpsKnd argtycs
1038 :     val nd = DI.next d
1039 :     val body = mkStrexp (def, nd)
1040 :     val hdr = buildHdr v
1041 :     (* binding of all v's components *)
1042 :     in
1043 :     TFN(knds, FN(v, strLty(param, nd, compInfo), hdr body))
1044 :     end
1045 :     | _ => bug "mkFctexp: unexpected access")
1046 : monnier 16 | g (LETfct (dec, b)) = mkDec (dec, d) (g b)
1047 :     | g (MARKfct (b, reg)) = withRegion reg g b
1048 :     | g _ = bug "unexpected functor expressions in mkFctexp"
1049 :    
1050 :     in g fe
1051 :     end
1052 :    
1053 :     and mkStrbs (sbs, d) =
1054 : blume 587 let fun g (STRB{str=M.STR { access, ... }, def, ... }, b) =
1055 :     (case access of
1056 :     DA.LVAR v =>
1057 : monnier 16 let val hdr = buildHdr v
1058 : blume 587 (* binding of all v's components *)
1059 :     in
1060 :     LET(v, mkStrexp(def, d), hdr b)
1061 : monnier 16 end
1062 : blume 587 | _ => bug "mkStrbs: unexpected access")
1063 : monnier 16 | g _ = bug "unexpected structure bindings in mkStrbs"
1064 : blume 587 in fold g sbs
1065 : monnier 16 end
1066 :    
1067 :     and mkFctbs (fbs, d) =
1068 : blume 587 let fun g (FCTB{fct=M.FCT { access, ... }, def, ... }, b) =
1069 :     (case access of
1070 :     DA.LVAR v =>
1071 : monnier 16 let val hdr = buildHdr v
1072 : blume 587 in
1073 :     LET(v, mkFctexp(def, d), hdr b)
1074 : monnier 16 end
1075 : blume 587 | _ => bug "mkFctbs: unexpected access")
1076 : monnier 16 | g _ = bug "unexpected functor bindings in mkStrbs"
1077 : blume 587 in fold g fbs
1078 : monnier 16 end
1079 :    
1080 :    
1081 :     (***************************************************************************
1082 :     * Translating absyn decls and exprs into lambda expression: *
1083 :     * *
1084 :     * val mkExp : A.exp * DI.depth -> L.lexp *
1085 :     * val mkDec : A.dec * DI.depth -> L.lexp -> L.lexp *
1086 :     * *
1087 :     ***************************************************************************)
1088 :     and mkDec (dec, d) =
1089 :     let fun g (VALdec vbs) = mkVBs(vbs, d)
1090 :     | g (VALRECdec rvbs) = mkRVBs(rvbs, d)
1091 :     | g (ABSTYPEdec{body,...}) = g body
1092 :     | g (EXCEPTIONdec ebs) = mkEBs(ebs, d)
1093 :     | g (STRdec sbs) = mkStrbs(sbs, d)
1094 :     | g (ABSdec sbs) = mkStrbs(sbs, d)
1095 :     | g (FCTdec fbs) = mkFctbs(fbs, d)
1096 :     | g (LOCALdec(ld, vd)) = (g ld) o (g vd)
1097 :     | g (SEQdec ds) = foldr (op o) ident (map g ds)
1098 :     | g (MARKdec(x, reg)) =
1099 :     let val f = withRegion reg g x
1100 :     in fn y => withRegion reg f y
1101 :     end
1102 : monnier 100 | g (OPENdec xs) =
1103 :     let (* special hack to make the import tree simpler *)
1104 : blume 587 fun mkos (_, s as M.STR { access = acc, ... }) =
1105 :     if extern acc then
1106 : monnier 100 let val _ = mkAccT(acc, strLty(s, d, compInfo), NONE)
1107 : blume 587 in ()
1108 : monnier 100 end
1109 : blume 587 else ()
1110 : monnier 100 | mkos _ = ()
1111 :     in app mkos xs; ident
1112 :     end
1113 : monnier 16 | g _ = ident
1114 :     in g dec
1115 :     end
1116 :    
1117 :     and mkExp (exp, d) =
1118 : monnier 45 let val tTyc = toTyc d
1119 :     val tLty = toLty d
1120 : monnier 16
1121 :     fun mkRules xs = map (fn (RULE(p, e)) => (fillPat(p, d), g e)) xs
1122 :    
1123 :     and g (VARexp (ref v, ts)) = mkVE(v, ts, d)
1124 :    
1125 :     | g (CONexp (dc, ts)) = mkCE(dc, ts, NONE, d)
1126 :     | g (APPexp (CONexp(dc, ts), e2)) = mkCE(dc, ts, SOME(g e2), d)
1127 :    
1128 :     | g (INTexp (s, t)) =
1129 :     ((if TU.equalType (t, BT.intTy) then INT (LN.int s)
1130 :     else if TU.equalType (t, BT.int32Ty) then INT32 (LN.int32 s)
1131 : mblume 1347 else if TU.equalType (t, BT.intinfTy) then VAR (getII s)
1132 : mblume 1682 else if TU.equalType (t, BT.int64Ty) then
1133 :     let val (hi, lo) = LN.int64 s
1134 :     in RECORD [WORD32 hi, WORD32 lo]
1135 :     end
1136 : mblume 1347 else bug "translate INTexp")
1137 :     handle Overflow => (repErr "int constant too large"; INT 0))
1138 : monnier 16
1139 :     | g (WORDexp(s, t)) =
1140 :     ((if TU.equalType (t, BT.wordTy) then WORD (LN.word s)
1141 : mblume 1682 else if TU.equalType (t, BT.word8Ty) then WORD (LN.word8 s)
1142 :     else if TU.equalType (t, BT.word32Ty) then WORD32 (LN.word32 s)
1143 :     else if TU.equalType (t, BT.word64Ty) then
1144 :     let val (hi, lo) = LN.word64 s
1145 :     in RECORD [WORD32 hi, WORD32 lo]
1146 :     end
1147 :     else (ppType t; bug "translate WORDexp"))
1148 : monnier 16 handle Overflow => (repErr "word constant too large"; INT 0))
1149 :    
1150 :     | g (REALexp s) = REAL s
1151 :     | g (STRINGexp s) = STRING s
1152 :     | g (CHARexp s) = INT (Char.ord(String.sub(s, 0)))
1153 :     (** NOTE: the above won't work for cross compiling to
1154 :     multi-byte characters **)
1155 :    
1156 : monnier 45 | g (RECORDexp []) = unitLexp
1157 : monnier 16 | g (RECORDexp xs) =
1158 :     if sorted xs then RECORD (map (fn (_,e) => g e) xs)
1159 :     else let val vars = map (fn (l,e) => (l,(g e, mkv()))) xs
1160 :     fun bind ((_,(e,v)),x) = LET(v,e,x)
1161 :     val bexp = map (fn (_,(_,v)) => VAR v) (sortrec vars)
1162 :     in foldr bind (RECORD bexp) vars
1163 :     end
1164 :    
1165 :     | g (SELECTexp (LABEL{number=i,...}, e)) = SELECT(i, g e)
1166 :    
1167 :     | g (VECTORexp ([], ty)) =
1168 :     TAPP(coreAcc "vector0", [tTyc ty])
1169 :     | g (VECTORexp (xs, ty)) =
1170 :     let val tc = tTyc ty
1171 :     val vars = map (fn e => (g e, mkv())) xs
1172 :     fun bind ((e,v),x) = LET(v, e, x)
1173 :     val bexp = map (fn (_,v) => VAR v) vars
1174 :     in foldr bind (VECTOR (bexp, tc)) vars
1175 :     end
1176 :    
1177 :     | g (PACKexp(e, ty, tycs)) = g e
1178 :     (*
1179 :     let val (nty, ks, tps) = TU.reformat(ty, tycs, d)
1180 : monnier 45 val ts = map (tpsTyc d) tps
1181 : monnier 16 (** use of LtyEnv.tcAbs is a temporary hack (ZHONG) **)
1182 :     val nts = ListPair.map LtyEnv.tcAbs (ts, ks)
1183 :     val nd = DI.next d
1184 :     in case (ks, tps)
1185 :     of ([], []) => g e
1186 : monnier 45 | _ => PACK(LT.ltc_poly(ks, [toLty nd nty]),
1187 :     ts, nts , g e)
1188 : monnier 16 end
1189 :     *)
1190 :     | g (SEQexp [e]) = g e
1191 :     | g (SEQexp (e::r)) = LET(mkv(), g e, g (SEQexp r))
1192 :    
1193 :     | g (APPexp (e1, e2)) = APP(g e1, g e2)
1194 :     | g (MARKexp (e, reg)) = withRegion reg g e
1195 :     | g (CONSTRAINTexp (e,_)) = g e
1196 :    
1197 :     | g (RAISEexp (e, ty)) = mkRaise(g e, tLty ty)
1198 : mblume 1641 | g (HANDLEexp (e, (l, ty))) =
1199 : monnier 16 let val rootv = mkv()
1200 :     fun f x = FN(rootv, tLty ty, x)
1201 :     val l' = mkRules l
1202 : monnier 45 in HANDLE(g e, MC.handCompile(env, l', f,
1203 : mblume 1347 rootv, toTcLt d, complain,
1204 :     genintinfswitch))
1205 : monnier 16 end
1206 :    
1207 :     | g (FNexp (l, ty)) =
1208 :     let val rootv = mkv()
1209 :     fun f x = FN(rootv, tLty ty, x)
1210 : mblume 1347 in MC.matchCompile (env, mkRules l, f, rootv, toTcLt d,
1211 :     complain, genintinfswitch)
1212 : monnier 16 end
1213 :    
1214 :     | g (CASEexp (ee, l, isMatch)) =
1215 :     let val rootv = mkv()
1216 :     val ee' = g ee
1217 :     fun f x = LET(rootv, ee', x)
1218 :     val l' = mkRules l
1219 :     in if isMatch
1220 : mblume 1347 then MC.matchCompile (env, l', f, rootv, toTcLt d,
1221 :     complain, genintinfswitch)
1222 :     else MC.bindCompile (env, l', f, rootv, toTcLt d,
1223 :     complain, genintinfswitch)
1224 : monnier 16 end
1225 :    
1226 : mblume 1332 | g (IFexp { test, thenCase, elseCase }) =
1227 :     COND (g test, g thenCase, g elseCase)
1228 :    
1229 :     | g (ANDALSOexp (e1, e2)) =
1230 :     COND (g e1, g e2, falseLexp)
1231 :    
1232 :     | g (ORELSEexp (e1, e2)) =
1233 :     COND (g e1, trueLexp, g e2)
1234 :    
1235 :     | g (WHILEexp { test, expr }) =
1236 :     let val fv = mkv ()
1237 :     val body =
1238 :     FN (mkv (), lt_unit,
1239 :     COND (g test,
1240 :     LET (mkv (), g expr, APP (VAR fv, unitLexp)),
1241 :     unitLexp))
1242 :     in
1243 :     FIX ([fv], [lt_u_u], [body], APP (VAR fv, unitLexp))
1244 :     end
1245 :    
1246 : monnier 16 | g (LETexp (dc, e)) = mkDec (dc, d) (g e)
1247 :    
1248 :     | g e =
1249 :     EM.impossibleWithBody "untranslateable expression"
1250 : macqueen 1344 (fn ppstrm => (PP.string ppstrm " expression: ";
1251 : monnier 16 PPAbsyn.ppExp (env,NONE) ppstrm (e, !ppDepth)))
1252 :    
1253 :     in g exp
1254 :     end
1255 :    
1256 : mblume 1347 and transIntInf d s =
1257 :     (* This is a temporary solution. Since IntInf literals
1258 :     * are created using a core function call, there is
1259 :     * no indication within the program that we are really
1260 :     * dealing with a constant value that -- in principle --
1261 :     * could be subject to such things as constant folding. *)
1262 :     let val consexp = CONexp (BT.consDcon, [BT.wordTy])
1263 :     fun build [] = CONexp (BT.nilDcon, [BT.wordTy])
1264 :     | build (d :: ds) = let
1265 :     val i = Word.toIntX d
1266 :     in
1267 :     APPexp (consexp,
1268 :     EU.TUPLEexp [WORDexp (IntInf.fromInt i, BT.wordTy),
1269 :     build ds])
1270 :     end
1271 :     fun small w =
1272 :     APP (coreAcc (if LN.isNegative s then "makeSmallNegInf"
1273 :     else "makeSmallPosInf"),
1274 :     mkExp (WORDexp (IntInf.fromInt (Word.toIntX w), BT.wordTy),
1275 :     d))
1276 :     in
1277 :     case LN.repDigits s of
1278 :     [] => small 0w0
1279 :     | [w] => small w
1280 :     | ws => APP (coreAcc (if LN.isNegative s then "makeNegInf"
1281 :     else "makePosInf"),
1282 :     mkExp (build ws, d))
1283 :     end
1284 : monnier 16
1285 : mblume 1347 (* Wrap bindings for IntInf.int literals around body. *)
1286 :     fun wrapII body = let
1287 :     fun one (n, v, b) = LET (v, transIntInf DI.top n, b)
1288 :     in
1289 :     IIMap.foldli one body (!iimap)
1290 :     end
1291 :    
1292 : monnier 100 (* wrapPidInfo: lexp * (pid * pidInfo) list -> lexp * importTree *)
1293 :     fun wrapPidInfo (body, pidinfos) =
1294 :     let val imports =
1295 :     let fun p2itree (ANON xl) =
1296 : blume 879 ImportTree.ITNODE (map (fn (i,z) => (i, p2itree z)) xl)
1297 :     | p2itree (NAMED _) = ImportTree.ITNODE []
1298 : monnier 100 in map (fn (p, pi) => (p, p2itree pi)) pidinfos
1299 :     end
1300 :     (*
1301 :     val _ = let val _ = say "\n ****************** \n"
1302 :     val _ = say "\n the current import tree is :\n"
1303 : blume 879 fun tree (ImportTree.ITNODE []) = ["\n"]
1304 :     | tree (ImportTree.ITNODE xl) =
1305 : monnier 100 foldr (fn ((i, x), z) =>
1306 :     let val ts = tree x
1307 :     val u = (Int.toString i) ^ " "
1308 :     in (map (fn y => (u ^ y)) ts) @ z
1309 :     end) [] xl
1310 :     fun pp (p, n) =
1311 :     (say ("Pid " ^ (PersStamps.toHex p) ^ "\n");
1312 :     app say (tree n))
1313 :     in app pp imports; say "\n ****************** \n"
1314 :     end
1315 :     *)
1316 :     val plexp =
1317 :     let fun get ((_, ANON xl), z) = foldl get z xl
1318 :     | get ((_, u as NAMED (_,t,_)), (n,cs,ts)) =
1319 :     (n+1, (n,u)::cs, t::ts)
1320 : monnier 16
1321 : monnier 100 (* get the fringe information *)
1322 :     val getp = fn ((_, pi), z) => get((0, pi), z)
1323 :     val (finfos, lts) =
1324 :     let val (_, fx, lx) = foldl getp (0,[],[]) pidinfos
1325 :     in (rev fx, rev lx)
1326 :     end
1327 : monnier 16
1328 : monnier 100 (* do the selection of all import variables *)
1329 :     fun mksel (u, xl, be) =
1330 :     let fun g ((i, pi), be) =
1331 :     let val (v, xs) = case pi of ANON z => (mkv(), z)
1332 :     | NAMED(v,_,z) => (v, z)
1333 :     in LET(v, SELECT(i, u), mksel(VAR v, xs, be))
1334 :     end
1335 :     in foldr g be xl
1336 :     end
1337 :     val impvar = mkv()
1338 :     val implty = LT.ltc_str lts
1339 :     val nbody = mksel (VAR impvar, finfos, body)
1340 :     in FN(impvar, implty, nbody)
1341 :     end
1342 :     in (plexp, imports)
1343 :     end (* function wrapPidInfo *)
1344 : monnier 16
1345 : monnier 100 (** the list of things being exported from the current compilation unit *)
1346 : monnier 16 val exportLexp = SRECORD (map VAR exportLvars)
1347 :    
1348 : monnier 100 (** translating the ML absyn into the PLambda expression *)
1349 :     val body = mkDec (rootdec, DI.top) exportLexp
1350 :    
1351 : mblume 1347 (** add bindings for intinf constants *)
1352 :     val body = wrapII body
1353 :    
1354 : monnier 100 (** wrapping up the body with the imported variables *)
1355 : mblume 1347 val (plexp, imports) = wrapPidInfo (body, PersMap.listItemsi (!persmap))
1356 : monnier 100
1357 :     fun prGen (flag,printE) s e =
1358 :     if !flag then (say ("\n\n[After " ^ s ^ " ...]\n\n"); printE e) else ()
1359 : monnier 122 val _ = prGen(Control.FLINT.print, PPLexp.printLexp) "Translate" plexp
1360 : monnier 100
1361 :     (** normalizing the plambda expression into FLINT *)
1362 :     val flint = FlintNM.norm plexp
1363 :    
1364 :     in {flint = flint, imports = imports}
1365 : monnier 16 end (* function transDec *)
1366 :    
1367 :     end (* top-level local *)
1368 :     end (* structure Translate *)

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0