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 1967 - (view) (download)

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 : macqueen 1967 (** converting a value of access+prim into the lambda expression
349 :     ** [KM???} But it is ignoring the prim argument!!!
350 :     **)
351 :     fun mkAccInfo (acc, prim, getLty, nameOp) =
352 : monnier 100 if extern acc then mkAccT(acc, getLty(), nameOp) else mkAcc (acc, nameOp)
353 : monnier 16
354 :     fun fillPat(pat, d) =
355 :     let fun fill (CONSTRAINTpat (p,t)) = fill p
356 :     | fill (LAYEREDpat (p,q)) = LAYEREDpat(fill p, fill q)
357 :     | fill (RECORDpat {fields, flex=false, typ}) =
358 :     RECORDpat{fields = map (fn (lab, p) => (lab, fill p)) fields,
359 :     typ = typ, flex = false}
360 :     | fill (pat as RECORDpat {fields, flex=true, typ}) =
361 :     let exception DontBother
362 :     val fields' = map (fn (l,p) => (l, fill p)) fields
363 :    
364 :     fun find (t as TP.CONty(TP.RECORDtyc labels, _)) =
365 :     (typ := t; labels)
366 :     | find _ = (complain EM.COMPLAIN "unresolved flexible record"
367 :     (fn ppstrm =>
368 : macqueen 1344 (PP.newline ppstrm;
369 :     PP.string ppstrm "pattern: ";
370 : monnier 16 PPAbsyn.ppPat env ppstrm
371 :     (pat,!Control.Print.printDepth)));
372 :     raise DontBother)
373 :    
374 :     fun merge (a as ((id,p)::r), lab::s) =
375 :     if S.eq(id,lab) then (id,p) :: merge(r,s)
376 :     else (lab,WILDpat) :: merge(a,s)
377 :     | merge ([], lab::s) = (lab,WILDpat) :: merge([], s)
378 :     | merge ([], []) = []
379 :     | merge _ = bug "merge in translate"
380 :    
381 :     in RECORDpat{fields = merge(fields',
382 :     find(TU.headReduceType (!typ))),
383 :     flex = false, typ = typ}
384 :     handle DontBother => WILDpat
385 :     end
386 :     | fill (VECTORpat(pats,ty)) = VECTORpat(map fill pats, ty)
387 :     | fill (ORpat(p1, p2)) = ORpat(fill p1, fill p2)
388 : monnier 109 | fill (CONpat(TP.DATACON{name, const, typ, rep, sign, lazyp}, ts)) =
389 :     CONpat(TP.DATACON{name=name, const=const, typ=typ, lazyp=lazyp,
390 : monnier 100 sign=sign, rep=mkRep(rep, toDconLty d typ, name)}, ts)
391 : monnier 109 | fill (APPpat(TP.DATACON{name, const, typ, rep, sign, lazyp}, ts, pat)) =
392 :     APPpat(TP.DATACON{name=name, const=const, typ=typ, sign=sign, lazyp=lazyp,
393 : monnier 100 rep=mkRep(rep, toDconLty d typ, name)}, ts, fill pat)
394 : monnier 16 | fill xp = xp
395 :    
396 :     in fill pat
397 :     end (* function fillPat *)
398 :    
399 :     (** The runtime polymorphic equality and string equality dictionary. *)
400 :     val eqDict =
401 :     let val strEqRef : lexp option ref = ref NONE
402 :     val polyEqRef : lexp option ref = ref NONE
403 : mblume 1347 val intInfEqRef : lexp option ref = ref NONE
404 : monnier 16
405 :     fun getStrEq () =
406 :     (case (!strEqRef)
407 :     of SOME e => e
408 :     | NONE => (let val e = coreAcc "stringequal"
409 :     in strEqRef := (SOME e); e
410 :     end))
411 :    
412 : mblume 1347 fun getIntInfEq () = (* same as polyeq, but silent *)
413 :     case !intInfEqRef of
414 :     SOME e => e
415 :     | NONE => let val e =
416 :     TAPP (coreAcc "polyequal",
417 :     [toTyc DI.top BT.intinfTy])
418 :     in
419 :     intInfEqRef := SOME e; e
420 :     end
421 :    
422 : monnier 16 fun getPolyEq () =
423 : monnier 504 (repPolyEq();
424 :     case (!polyEqRef)
425 : monnier 16 of SOME e => e
426 :     | NONE => (let val e = coreAcc "polyequal"
427 :     in polyEqRef := (SOME e); e
428 :     end))
429 : mblume 1347 in {getStrEq=getStrEq, getIntInfEq=getIntInfEq, getPolyEq=getPolyEq}
430 : monnier 16 end
431 :    
432 : monnier 504 val eqGen = PEqual.equal (eqDict, env)
433 : monnier 16
434 :     (***************************************************************************
435 :     * *
436 :     * Translating the primops; this should be moved into a separate file *
437 :     * in the future. (ZHONG) *
438 :     * *
439 :     ***************************************************************************)
440 :    
441 :     val lt_tyc = LT.ltc_tyc
442 :     val lt_arw = LT.ltc_parrow
443 :     val lt_tup = LT.ltc_tuple
444 :     val lt_int = LT.ltc_int
445 :     val lt_int32 = LT.ltc_int32
446 :     val lt_bool = LT.ltc_bool
447 : mblume 1332 val lt_unit = LT.ltc_unit
448 : monnier 16
449 :     val lt_ipair = lt_tup [lt_int, lt_int]
450 : mblume 1683 val lt_i32pair = lt_tup [lt_int32, lt_int32]
451 : monnier 16 val lt_icmp = lt_arw (lt_ipair, lt_bool)
452 :     val lt_ineg = lt_arw (lt_int, lt_int)
453 :     val lt_intop = lt_arw (lt_ipair, lt_int)
454 : mblume 1332 val lt_u_u = lt_arw (lt_unit, lt_unit)
455 : monnier 16
456 :     val boolsign = BT.boolsign
457 :     val (trueDcon', falseDcon') =
458 :     let val lt = LT.ltc_parrow(LT.ltc_unit, LT.ltc_bool)
459 :     fun h (TP.DATACON{name,rep,typ,...}) = (name, rep, lt)
460 :     in (h BT.trueDcon, h BT.falseDcon)
461 :     end
462 :    
463 :     val trueLexp = CON(trueDcon', [], unitLexp)
464 :     val falseLexp = CON(falseDcon', [], unitLexp)
465 :    
466 :     fun COND(a,b,c) =
467 :     SWITCH(a,boolsign, [(DATAcon(trueDcon', [], mkv()),b),
468 :     (DATAcon(falseDcon', [], mkv()),c)], NONE)
469 :    
470 :     fun composeNOT (eq, t) =
471 :     let val v = mkv()
472 :     val argt = lt_tup [t, t]
473 :     in FN(v, argt, COND(APP(eq, VAR v), falseLexp, trueLexp))
474 :     end
475 :    
476 :     fun cmpOp p = PRIM(p, lt_icmp, [])
477 :     fun inegOp p = PRIM(p, lt_ineg, [])
478 :    
479 :     val LESSU = PO.CMP{oper=PO.LTU, kind=PO.UINT 31}
480 :    
481 :     val lt_len = LT.ltc_poly([LT.tkc_mono], [lt_arw(LT.ltc_tv 0, lt_int)])
482 :     val lt_upd =
483 :     let val x = LT.ltc_ref (LT.ltc_tv 0)
484 :     in LT.ltc_poly([LT.tkc_mono],
485 :     [lt_arw(lt_tup [x, lt_int, LT.ltc_tv 0], LT.ltc_unit)])
486 :     end
487 :    
488 :     fun lenOp(tc) = PRIM(PO.LENGTH, lt_len, [tc])
489 :    
490 :     fun rshiftOp k = PO.ARITH{oper=PO.RSHIFT, overflow=false, kind=k}
491 :     fun rshiftlOp k = PO.ARITH{oper=PO.RSHIFTL, overflow=false, kind=k}
492 :     fun lshiftOp k = PO.ARITH{oper=PO.LSHIFT, overflow=false, kind=k}
493 :    
494 :     fun lword0 (PO.UINT 31) = WORD 0w0
495 :     | lword0 (PO.UINT 32) = WORD32 0w0
496 :     | lword0 _ = bug "unexpected case in lword0"
497 :    
498 :     fun baselt (PO.UINT 31) = lt_int
499 :     | baselt (PO.UINT 32) = lt_int32
500 :     | baselt _ = bug "unexpected case in baselt"
501 :    
502 :     fun shiftTy k =
503 :     let val elem = baselt k
504 :     val tupt = lt_tup [elem, lt_int]
505 :     in lt_arw(tupt, elem)
506 :     end
507 :    
508 :     fun inlineShift(shiftOp, kind, clear) =
509 : mblume 1683 let fun shiftLimit (PO.UINT lim | PO.INT lim) = WORD(Word.fromInt lim)
510 : monnier 16 | shiftLimit _ = bug "unexpected case in shiftLimit"
511 :    
512 :     val p = mkv() val vp = VAR p
513 :     val w = mkv() val vw = VAR w
514 :     val cnt = mkv() val vcnt = VAR cnt
515 :    
516 :     val argt = lt_tup [baselt(kind), lt_int]
517 :     val cmpShiftAmt =
518 :     PRIM(PO.CMP{oper=PO.LEU, kind=PO.UINT 31}, lt_icmp, [])
519 :     in FN(p, argt,
520 :     LET(w, SELECT(0, vp),
521 :     LET(cnt, SELECT(1, vp),
522 :     COND(APP(cmpShiftAmt, RECORD [shiftLimit(kind), vcnt]),
523 :     clear vw,
524 :     APP(PRIM(shiftOp(kind), shiftTy(kind), []),
525 :     RECORD [vw, vcnt])))))
526 :     end
527 :    
528 : blume 1183 fun inlops nk = let
529 :     val (lt_arg, zero, overflow) =
530 :     case nk of
531 :     PO.INT 31 => (LT.ltc_int, INT 0, true)
532 :     | PO.UINT 31 => (LT.ltc_int, WORD 0w0, false)
533 :     | PO.INT 32 => (LT.ltc_int32, INT32 0, true)
534 :     | PO.UINT 32 => (LT.ltc_int32, WORD32 0w0, false)
535 :     | PO.FLOAT 64 => (LT.ltc_real, REAL "0.0", false)
536 :     | _ => bug "inlops: bad numkind"
537 :     val lt_argpair = lt_tup [lt_arg, lt_arg]
538 :     val lt_cmp = lt_arw (lt_argpair, lt_bool)
539 :     val lt_neg = lt_arw (lt_arg, lt_arg)
540 :     val less = PRIM (PO.CMP { oper = PO.<, kind = nk }, lt_cmp, [])
541 :     val greater = PRIM (PO.CMP { oper = PO.>, kind = nk }, lt_cmp, [])
542 :     val negate =
543 :     PRIM (PO.ARITH { oper = PO.~, overflow = overflow, kind = nk },
544 :     lt_neg, [])
545 :     in
546 :     { lt_arg = lt_arg, lt_argpair = lt_argpair, lt_cmp = lt_cmp,
547 :     less = less, greater = greater,
548 :     zero = zero, negate = negate }
549 :     end
550 : monnier 16
551 : blume 1183 fun inlminmax (nk, ismax) = let
552 :     val { lt_argpair, less, greater, lt_cmp, ... } = inlops nk
553 :     val x = mkv () and y = mkv () and z = mkv ()
554 :     val cmpop = if ismax then greater else less
555 :     val elsebranch =
556 :     case nk of
557 :     PO.FLOAT _ => let
558 :     (* testing for NaN *)
559 :     val fequal =
560 :     PRIM (PO.CMP { oper = PO.EQL, kind = nk }, lt_cmp, [])
561 :     in
562 : blume 1249 COND (APP (fequal, RECORD [VAR y, VAR y]), VAR y, VAR x)
563 : blume 1183 end
564 :     | _ => VAR y
565 :     in
566 :     FN (z, lt_argpair,
567 :     LET (x, SELECT (0, VAR z),
568 :     LET (y, SELECT (1, VAR z),
569 :     COND (APP (cmpop, RECORD [VAR x, VAR y]),
570 :     VAR x, elsebranch))))
571 :     end
572 :    
573 :     fun inlabs nk = let
574 :     val { lt_arg, greater, zero, negate, ... } = inlops nk
575 :     val x = mkv ()
576 :     in
577 :     FN (x, lt_arg,
578 :     COND (APP (greater, RECORD [VAR x, zero]),
579 :     VAR x, APP (negate, VAR x)))
580 :     end
581 :    
582 : mblume 1347 fun inl_infPrec (what, corename, p, lt, is_from_inf) = let
583 :     val (orig_arg_lt, res_lt) =
584 :     case LT.ltd_arrow lt of
585 :     (_, [a], [r]) => (a, r)
586 :     | _ => bug ("unexpected type of " ^ what)
587 :     val extra_arg_lt =
588 :     LT.ltc_parrow (if is_from_inf then (orig_arg_lt, LT.ltc_int32)
589 :     else (LT.ltc_int32, orig_arg_lt))
590 :     val new_arg_lt = LT.ltc_tuple [orig_arg_lt, extra_arg_lt]
591 :     val new_lt = LT.ltc_parrow (new_arg_lt, res_lt)
592 :     val x = mkv ()
593 :     in
594 :     FN (x, orig_arg_lt,
595 :     APP (PRIM (p, new_lt, []),
596 :     RECORD [VAR x, coreAcc corename]))
597 :     end
598 :    
599 : monnier 16 fun transPrim (prim, lt, ts) =
600 :     let fun g (PO.INLLSHIFT k) = inlineShift(lshiftOp, k, fn _ => lword0(k))
601 :     | g (PO.INLRSHIFTL k) = inlineShift(rshiftlOp, k, fn _ => lword0(k))
602 :     | g (PO.INLRSHIFT k) = (* preserve sign bit with arithmetic rshift *)
603 :     let fun clear w = APP(PRIM(rshiftOp k, shiftTy k, []),
604 :     RECORD [w, WORD 0w31])
605 :     in inlineShift(rshiftOp, k, clear)
606 :     end
607 :    
608 : blume 1183 | g (PO.INLMIN nk) = inlminmax (nk, false)
609 :     | g (PO.INLMAX nk) = inlminmax (nk, true)
610 :     | g (PO.INLABS nk) = inlabs nk
611 : monnier 16
612 :     | g (PO.INLNOT) =
613 :     let val x = mkv()
614 :     in FN(x, lt_bool, COND(VAR x, falseLexp, trueLexp))
615 :     end
616 :    
617 :     | g (PO.INLCOMPOSE) =
618 :     let val (t1, t2, t3) =
619 :     case ts of [a,b,c] => (lt_tyc a, lt_tyc b, lt_tyc c)
620 :     | _ => bug "unexpected type for INLCOMPOSE"
621 :    
622 :     val argt = lt_tup [lt_arw(t2, t3), lt_arw(t1, t2)]
623 :    
624 :     val x = mkv() and z = mkv()
625 :     val f = mkv() and g = mkv()
626 :     in FN(z, argt,
627 :     LET(f, SELECT(0,VAR z),
628 :     LET(g,SELECT(1,VAR z),
629 :     FN(x, t1, APP(VAR f,APP(VAR g,VAR x))))))
630 :     end
631 :     | g (PO.INLBEFORE) =
632 :     let val (t1, t2) =
633 :     case ts of [a,b] => (lt_tyc a, lt_tyc b)
634 :     | _ => bug "unexpected type for INLBEFORE"
635 :     val argt = lt_tup [t1, t2]
636 :     val x = mkv()
637 :     in FN(x, argt, SELECT(0,VAR x))
638 :     end
639 : blume 1183 | g (PO.INLIGNORE) =
640 :     let val argt =
641 :     case ts of [a] => lt_tyc a
642 :     | _ => bug "unexpected type for INLIGNORE"
643 :     in FN (mkv (), argt, unitLexp)
644 :     end
645 : monnier 16
646 : mblume 1347 | g (PO.INLIDENTITY) =
647 :     let val argt =
648 :     case ts of [a] => lt_tyc a
649 :     | _ => bug "unexpected type for INLIDENTITY"
650 :     val v = mkv ()
651 :     in
652 :     FN (v, argt, VAR v)
653 :     end
654 :    
655 : mblume 1683 | g (PO.CVT64) = let val v = mkv () in FN (v, lt_i32pair, VAR v) end
656 :    
657 : monnier 16 | g (PO.INLSUBSCRIPTV) =
658 :     let val (tc1, t1) = case ts of [z] => (z, lt_tyc z)
659 :     | _ => bug "unexpected ty for INLSUBV"
660 :    
661 :     val seqtc = LT.tcc_vector tc1
662 :     val argt = lt_tup [lt_tyc seqtc, lt_int]
663 :    
664 :     val oper = PRIM(PO.SUBSCRIPT, lt, ts)
665 :     val p = mkv() and a = mkv() and i = mkv()
666 :     val vp = VAR p and va = VAR a and vi = VAR i
667 :     in FN(p, argt,
668 :     LET(a, SELECT(0,vp),
669 :     LET(i, SELECT(1,vp),
670 :     COND(APP(cmpOp(LESSU),
671 :     RECORD[vi, APP(lenOp seqtc, va)]),
672 :     APP(oper, RECORD[va, vi]),
673 :     mkRaise(coreExn "Subscript", t1)))))
674 :     end
675 :    
676 :     | g (PO.INLSUBSCRIPT) =
677 :     let val (tc1, t1) = case ts of [z] => (z, lt_tyc z)
678 :     | _ => bug "unexpected ty for INLSUB"
679 :    
680 :     val seqtc = LT.tcc_array tc1
681 :     val argt = lt_tup [lt_tyc seqtc, lt_int]
682 :    
683 :     val oper = PRIM(PO.SUBSCRIPT, lt, ts)
684 :     val p = mkv() and a = mkv() and i = mkv()
685 :     val vp = VAR p and va = VAR a and vi = VAR i
686 :     in FN(p, argt,
687 :     LET(a, SELECT(0, vp),
688 :     LET(i, SELECT(1, vp),
689 :     COND(APP(cmpOp(LESSU),
690 :     RECORD[vi, APP(lenOp seqtc, va)]),
691 :     APP(oper, RECORD[va, vi]),
692 :     mkRaise(coreExn "Subscript", t1)))))
693 :     end
694 :    
695 :     | g (PO.NUMSUBSCRIPT{kind,checked=true,immutable}) =
696 :     let val (tc1, t1, t2) =
697 :     case ts of [a,b] => (a, lt_tyc a, lt_tyc b)
698 :     | _ => bug "unexpected type for NUMSUB"
699 :    
700 :     val argt = lt_tup [t1, lt_int]
701 :     val p = mkv() and a = mkv() and i = mkv()
702 :     val vp = VAR p and va = VAR a and vi = VAR i
703 :     val oper = PO.NUMSUBSCRIPT{kind=kind,checked=false,
704 :     immutable=immutable}
705 :     val oper' = PRIM(oper, lt, ts)
706 :     in FN(p, argt,
707 :     LET(a, SELECT(0, vp),
708 :     LET(i, SELECT(1, vp),
709 :     COND(APP(cmpOp(LESSU), RECORD[vi,
710 :     APP(lenOp tc1, va)]),
711 :     APP(oper', RECORD [va, vi]),
712 :     mkRaise(coreExn "Subscript", t2)))))
713 :     end
714 :    
715 :     | g (PO.INLUPDATE) =
716 :     let val (tc1, t1) = case ts of [z] => (z, lt_tyc z)
717 :     | _ => bug "unexpected ty for INLSUB"
718 :    
719 :     val seqtc = LT.tcc_array tc1
720 :     val argt = lt_tup [lt_tyc seqtc, lt_int, t1]
721 :    
722 :     val oper = PRIM(PO.UPDATE, lt, ts)
723 :     val x = mkv() and a = mkv() and i = mkv() and v = mkv()
724 :     val vx = VAR x and va = VAR a and vi = VAR i and vv = VAR v
725 :    
726 :     in FN(x, argt,
727 :     LET(a, SELECT(0, vx),
728 :     LET(i, SELECT(1, vx),
729 :     LET(v, SELECT(2, vx),
730 :     COND(APP(cmpOp(LESSU),
731 :     RECORD[vi,APP(lenOp seqtc, va)]),
732 :     APP(oper, RECORD[va,vi,vv]),
733 : monnier 45 mkRaise(coreExn "Subscript", LT.ltc_unit))))))
734 : monnier 16 end
735 :    
736 :     | g (PO.NUMUPDATE{kind,checked=true}) =
737 :     let val (tc1, t1, t2) =
738 :     case ts of [a,b] => (a, lt_tyc a, lt_tyc b)
739 :     | _ => bug "unexpected type for NUMUPDATE"
740 :    
741 :     val argt = lt_tup [t1, lt_int, t2]
742 :    
743 :     val p=mkv() and a=mkv() and i=mkv() and v=mkv()
744 :     val vp=VAR p and va=VAR a and vi=VAR i and vv=VAR v
745 :    
746 :     val oper = PO.NUMUPDATE{kind=kind,checked=false}
747 :     val oper' = PRIM(oper, lt, ts)
748 :     in FN(p, argt,
749 :     LET(a, SELECT(0, vp),
750 :     LET(i, SELECT(1, vp),
751 :     LET(v, SELECT(2, vp),
752 :     COND(APP(cmpOp(LESSU),
753 :     RECORD[vi,APP(lenOp tc1, va)]),
754 :     APP(oper', RECORD[va,vi,vv]),
755 : monnier 45 mkRaise(coreExn "Subscript", LT.ltc_unit))))))
756 : monnier 16 end
757 :    
758 : monnier 251 (**** ASSIGN(r, x) <> UPDATE(r, 0, x) under new array reps (JHR;1998-10-30)
759 : monnier 16 | g (PO.ASSIGN) =
760 :     let val (tc1, t1) = case ts of [z] => (z, lt_tyc z)
761 :     | _ => bug "unexpected ty for ASSIGN"
762 :    
763 :     val seqtc = LT.tcc_ref tc1
764 :     val argt = lt_tup [lt_tyc seqtc, t1]
765 :    
766 :     val oper = PRIM(PO.UPDATE, lt_upd, [tc1])
767 :    
768 :     val x = mkv()
769 :     val varX = VAR x
770 :    
771 :     in FN(x, argt,
772 :     APP(oper, RECORD[SELECT(0, varX), INT 0, SELECT(1, varX)]))
773 :     end
774 : monnier 251 ****)
775 : monnier 16
776 : mblume 1347 (* Precision-conversion operations involving IntInf.
777 :     * These need to be translated specially by providing
778 :     * a second argument -- the routine from _Core that
779 :     * does the actual conversion to or from IntInf. *)
780 :    
781 :     | g (p as PO.TEST_INF prec) =
782 :     inl_infPrec ("TEST_INF", "testInf", p, lt, true)
783 :     | g (p as PO.TRUNC_INF prec) =
784 :     inl_infPrec ("TRUNC_INF", "truncInf", p, lt, true)
785 :     | g (p as PO.EXTEND_INF prec) =
786 :     inl_infPrec ("EXTEND_INF", "finToInf", p, lt, false)
787 :     | g (p as PO.COPY_INF prec) =
788 :     inl_infPrec ("COPY", "finToInf", p, lt, false)
789 :    
790 :     (* default handling for all other primops *)
791 : monnier 16 | g p = PRIM(p, lt, ts)
792 :    
793 :     in g prim
794 :     end (* function transPrim *)
795 :    
796 : mblume 1347 fun genintinfswitch (sv, cases, default) = let
797 :     val v = mkv ()
798 :    
799 :     (* build a chain of equality tests for checking large pattern values *)
800 :     fun build [] = default
801 :     | build ((n, e) :: r) =
802 :     COND (APP (#getIntInfEq eqDict (), RECORD [VAR v, VAR (getII n)]),
803 :     e, build r)
804 :    
805 :     (* split pattern values into small values and large values;
806 :     * small values can be handled directly using SWITCH *)
807 :     fun split ([], s, l) = (rev s, rev l)
808 :     | split ((n, e) :: r, sm, lg) =
809 :     (case LN.lowVal n of
810 :     SOME l => split (r, (INTcon l, e) :: sm, lg)
811 :     | NONE => split (r, sm, (n, e) :: lg))
812 :    
813 :     fun gen () =
814 :     case split (cases, [], []) of
815 :     ([], largeints) => build largeints
816 :     | (smallints, largeints) => let
817 :     val iv = mkv ()
818 :     in
819 :     LET (iv, APP (coreAcc "infLowValue", VAR v),
820 :     SWITCH (VAR iv,
821 :     DA.CNIL, smallints, SOME (build largeints)))
822 :     end
823 :     in
824 :     LET (v, sv, gen ())
825 :     end
826 :    
827 :    
828 : monnier 16 (***************************************************************************
829 :     * *
830 :     * Translating various bindings into lambda expressions: *
831 :     * *
832 :     * val mkVar : V.var * DI.depth -> L.lexp *
833 :     * val mkVE : V.var * T.ty list -> L.lexp *
834 :     * val mkCE : T.datacon * T.ty list * L.lexp option * DI.depth -> L.lexp *
835 :     * val mkStr : M.Structure * DI.depth -> L.lexp *
836 :     * val mkFct : M.Functor * DI.depth -> L.lexp *
837 :     * val mkBnd : DI.depth -> B.binding -> L.lexp *
838 :     * *
839 :     ***************************************************************************)
840 : macqueen 1967 (* [KM???] mkVar is calling mkAccInfo, which just drops the prim!!! *)
841 : macqueen 1961 fun mkVar (v as V.VALvar{access, prim, typ, path}, d) =
842 :     mkAccInfo(access, prim, fn () => toLty d (!typ), getNameOp path)
843 : monnier 16 | mkVar _ = bug "unexpected vars in mkVar"
844 :    
845 : blume 902 fun mkVE (v, ts, d) = let
846 :     fun otherwise () =
847 :     case ts of
848 :     [] => mkVar (v, d)
849 :     | _ => TAPP(mkVar(v, d), map (toTyc d) ts)
850 :     in
851 :     case v of
852 : macqueen 1961 V.VALvar { prim, ... } =>
853 :     case prim
854 :     of PrimOpId.Prim p =>
855 :     let val ts = (* compute intrinsic instantiation params *) []
856 :     in (case (p, ts)
857 :     of (PO.POLYEQL, [t]) => eqGen(typ, t, toTcLt d)
858 : blume 902 | (PO.POLYNEQ, [t]) =>
859 :     composeNOT(eqGen(typ, t, toTcLt d), toLty d t)
860 :     | (PO.INLMKARRAY, [t]) =>
861 :     let val dict =
862 :     {default = coreAcc "mkNormArray",
863 :     table = [([LT.tcc_real], coreAcc "mkRealArray")]}
864 :     in GENOP (dict, p, toLty d typ, map (toTyc d) ts)
865 :     end
866 : blume 1178 | (PO.RAW_CCALL NONE, [a, b, c]) =>
867 :     let val i = SOME (CProto.decode cproto_conv
868 :     { fun_ty = a, encoding = b })
869 :     handle CProto.BadEncoding => NONE
870 : blume 902 in PRIM (PO.RAW_CCALL i, toLty d typ, map (toTyc d) ts)
871 :     end
872 : macqueen 1961 | _ => transPrim(p, (toLty d typ), map (toTyc d) ts))
873 :     end
874 :     | PrimOpId.NonPrim => otherwise ()
875 : blume 902 | _ => otherwise ()
876 :     end
877 : monnier 16
878 :     fun mkCE (TP.DATACON{const, rep, name, typ, ...}, ts, apOp, d) =
879 :     let val lt = toDconLty d typ
880 : monnier 100 val rep' = mkRep(rep, lt, name)
881 : monnier 16 val dc = (name, rep', lt)
882 : macqueen 1967 val ts' = map (toTyc d o T.VARty) ts
883 : monnier 16 in if const then CON'(dc, ts', unitLexp)
884 :     else (case apOp
885 :     of SOME le => CON'(dc, ts', le)
886 :     | NONE =>
887 :     let val (argT, _) = LT.ltd_parrow(LT.lt_pinst(lt, ts'))
888 :     val v = mkv()
889 :     in FN(v, argT, CON'(dc, ts', VAR v))
890 :     end)
891 :     end
892 :    
893 : blume 587 fun mkStr (s as M.STR { access, info, ... }, d) =
894 :     mkAccInfo(access, info, fn () => strLty(s, d, compInfo), NONE)
895 : monnier 16 | mkStr _ = bug "unexpected structures in mkStr"
896 :    
897 : blume 587 fun mkFct (f as M.FCT { access, info, ... }, d) =
898 :     mkAccInfo(access, info, fn () => fctLty(f, d, compInfo), NONE)
899 : monnier 16 | mkFct _ = bug "unexpected functors in mkFct"
900 :    
901 :     fun mkBnd d =
902 :     let fun g (B.VALbind v) = mkVar(v, d)
903 :     | g (B.STRbind s) = mkStr(s, d)
904 :     | g (B.FCTbind f) = mkFct(f, d)
905 : monnier 100 | g (B.CONbind (TP.DATACON{rep=(DA.EXN acc), name, typ, ...})) =
906 : blume 587 let val nt = toDconLty d typ
907 :     val (argt,_) = LT.ltd_parrow nt
908 :     in mkAccT (acc, LT.ltc_etag argt, SOME name)
909 :     end
910 : monnier 16 | g _ = bug "unexpected bindings in mkBnd"
911 :     in g
912 :     end
913 :    
914 :    
915 :     (***************************************************************************
916 :     * *
917 :     * Translating core absyn declarations into lambda expressions: *
918 :     * *
919 :     * val mkVBs : Absyn.vb list * depth -> Lambda.lexp -> Lambda.lexp *
920 :     * val mkRVBs : Absyn.rvb list * depth -> Lambda.lexp -> Lambda.lexp *
921 :     * val mkEBs : Absyn.eb list * depth -> Lambda.lexp -> Lambda.lexp *
922 :     * *
923 :     ***************************************************************************)
924 :     fun mkPE (exp, d, []) = mkExp(exp, d)
925 :     | mkPE (exp, d, boundtvs) =
926 :     let val savedtvs = map ! boundtvs
927 :    
928 :     fun g (i, []) = ()
929 : blume 902 | g (i, (tv as ref (TP.OPEN _))::rest) = let
930 :     val m = markLBOUND (d, i);
931 :     in
932 :     tv := TP.TV_MARK m;
933 :     g (i+1, rest)
934 :     end
935 :     | g (i, (tv as ref (TP.TV_MARK _))::res) =
936 :     bug ("unexpected tyvar TV_MARK in mkPE")
937 : monnier 16 | g _ = bug "unexpected tyvar INSTANTIATED in mkPE"
938 :    
939 : blume 902 val _ = g(0, boundtvs) (* assign the TV_MARK tyvars *)
940 : monnier 16 val exp' = mkExp(exp, DI.next d)
941 :    
942 : macqueen 1967 (* restore tyvar states to that before translate *)
943 :     fun restore ([], []) = ()
944 :     | restore (a::r, b::z) = (b := a; restore(r, z))
945 :     | restore _ = bug "unexpected cases in mkPE"
946 : monnier 16
947 : macqueen 1948 (* [dbm, 6/22/06] Why do we need to restore the original
948 : macqueen 1967 contents of the uninstantiated meta type variables?
949 :     Only seems to be necessary if a given tyvar gets generalized
950 :     in two different valbinds *)
951 : macqueen 1948
952 : macqueen 1967 val _ = restore(savedtvs, boundtvs)
953 : monnier 16 val len = length(boundtvs)
954 :    
955 :     in TFN(LT.tkc_arg(len), exp')
956 :     end
957 :    
958 :     and mkVBs (vbs, d) =
959 : macqueen 1967 let fun g (VB{pat=VARpat(V.VALvar{access=DA.LVAR v, ...}),
960 :     exp as VARexp (ref (w as (V.VALvar{typ,prim,...})), instvs),
961 :     boundtvs=btvs, ...}, b) =
962 :     (* [dbm: 7/10/06] Originally, the mkVar and mkPE translations
963 :     * were chosen based on whether btvs and instvs were the same
964 :     * list of tyvars, which would be the case for all non-primop
965 :     * variables, but also in the primop case whenever the rhs
966 :     * variable environment type (!typ) was the same (equalTypeP)
967 :     * to the intrinsic type of the primop (e.g. when they are
968 :     * both monotypes). So in most cases, the mkVar translation
969 :     * will be used, and this drops the primop information!!!
970 :     * This seems definitely wrong. *)
971 :     (case prim
972 :     of PrimOpId.Prim name =>
973 :     let val (primop,primopty) = PrimOpMap name
974 :     in if TU.equalTypeP(!typ,primopty)
975 :     then LET(v, mkVar(w, d), b)
976 :     else LET(v, mkPE(exp, d, btvs), b)
977 :     end
978 :     | _ => LET(v, mkVar(w, d), b)
979 :     (* when generalized variables = instantiation params *)
980 : monnier 16
981 :     | g (VB{pat=VARpat(V.VALvar{access=DA.LVAR v, ...}),
982 :     exp, boundtvs=tvs, ...}, b) = LET(v, mkPE(exp, d, tvs), b)
983 :    
984 :     | g (VB{pat=CONSTRAINTpat(VARpat(V.VALvar{access=DA.LVAR v, ...}),_),
985 :     exp, boundtvs=tvs, ...}, b) = LET(v, mkPE(exp, d, tvs), b)
986 :    
987 :     | g (VB{pat, exp, boundtvs=tvs, ...}, b) =
988 :     let val ee = mkPE(exp, d, tvs)
989 :     val rules = [(fillPat(pat, d), b), (WILDpat, unitLexp)]
990 :     val rootv = mkv()
991 :     fun finish x = LET(rootv, ee, x)
992 : mblume 1347 in MC.bindCompile(env, rules, finish, rootv, toTcLt d, complain,
993 :     genintinfswitch)
994 : monnier 16 end
995 :     in fold g vbs
996 :     end
997 :    
998 :     and mkRVBs (rvbs, d) =
999 :     let fun g (RVB{var=V.VALvar{access=DA.LVAR v, typ=ref ty, ...},
1000 :     exp, boundtvs=tvs, ...}, (vlist, tlist, elist)) =
1001 :     let val ee = mkExp(exp, d) (* was mkPE(exp, d, tvs) *)
1002 : macqueen 1967 (* [ZHONG?]we no longer track type bindings at RVB anymore ! *)
1003 : monnier 45 val vt = toLty d ty
1004 : monnier 16 in (v::vlist, vt::tlist, ee::elist)
1005 :     end
1006 :     | g _ = bug "unexpected valrec bindings in mkRVBs"
1007 :    
1008 :     val (vlist, tlist, elist) = foldr g ([], [], []) rvbs
1009 :    
1010 :     in fn b => FIX(vlist, tlist, elist, b)
1011 :     end
1012 :    
1013 :     and mkEBs (ebs, d) =
1014 :     let fun g (EBgen {exn=TP.DATACON{rep=DA.EXN(DA.LVAR v), typ, ...},
1015 :     ident, ...}, b) =
1016 :     let val nt = toDconLty d typ
1017 :     val (argt, _) = LT.ltd_parrow nt
1018 :     in LET(v, ETAG(mkExp(ident, d), argt), b)
1019 :     end
1020 : monnier 100 | g (EBdef {exn=TP.DATACON{rep=DA.EXN(DA.LVAR v), typ, name, ...},
1021 : monnier 16 edef=TP.DATACON{rep=DA.EXN(acc), ...}}, b) =
1022 :     let val nt = toDconLty d typ
1023 :     val (argt, _) = LT.ltd_parrow nt
1024 : monnier 100 in LET(v, mkAccT(acc, LT.ltc_etag argt, SOME name), b)
1025 : monnier 16 end
1026 :     | g _ = bug "unexpected exn bindings in mkEBs"
1027 :    
1028 :     in fold g ebs
1029 :     end
1030 :    
1031 :    
1032 :     (***************************************************************************
1033 :     * *
1034 :     * Translating module exprs and decls into lambda expressions: *
1035 :     * *
1036 :     * val mkStrexp : Absyn.strexp * depth -> Lambda.lexp *
1037 :     * val mkFctexp : Absyn.fctexp * depth -> Lambda.lexp *
1038 :     * val mkStrbs : Absyn.strb list * depth -> Lambda.lexp -> Lambda.lexp *
1039 :     * val mkFctbs : Absyn.fctb list * depth -> Lambda.lexp -> Lambda.lexp *
1040 :     * *
1041 :     ***************************************************************************)
1042 :     and mkStrexp (se, d) =
1043 :     let fun g (VARstr s) = mkStr(s, d)
1044 :     | g (STRstr bs) = SRECORD (map (mkBnd d) bs)
1045 :     | g (APPstr {oper, arg, argtycs}) =
1046 :     let val e1 = mkFct(oper, d)
1047 : monnier 45 val tycs = map (tpsTyc d) argtycs
1048 : monnier 16 val e2 = mkStr(arg, d)
1049 :     in APP(TAPP(e1, tycs), e2)
1050 :     end
1051 :     | g (LETstr (dec, b)) = mkDec (dec, d) (g b)
1052 :     | g (MARKstr (b, reg)) = withRegion reg g b
1053 :    
1054 :     in g se
1055 :     end
1056 :    
1057 :     and mkFctexp (fe, d) =
1058 :     let fun g (VARfct f) = mkFct(f, d)
1059 : blume 587 | g (FCTfct {param as M.STR { access, ... }, argtycs, def }) =
1060 :     (case access of
1061 :     DA.LVAR v =>
1062 :     let val knds = map tpsKnd argtycs
1063 :     val nd = DI.next d
1064 :     val body = mkStrexp (def, nd)
1065 :     val hdr = buildHdr v
1066 :     (* binding of all v's components *)
1067 :     in
1068 :     TFN(knds, FN(v, strLty(param, nd, compInfo), hdr body))
1069 :     end
1070 :     | _ => bug "mkFctexp: unexpected access")
1071 : monnier 16 | g (LETfct (dec, b)) = mkDec (dec, d) (g b)
1072 :     | g (MARKfct (b, reg)) = withRegion reg g b
1073 :     | g _ = bug "unexpected functor expressions in mkFctexp"
1074 :    
1075 :     in g fe
1076 :     end
1077 :    
1078 :     and mkStrbs (sbs, d) =
1079 : blume 587 let fun g (STRB{str=M.STR { access, ... }, def, ... }, b) =
1080 :     (case access of
1081 :     DA.LVAR v =>
1082 : monnier 16 let val hdr = buildHdr v
1083 : blume 587 (* binding of all v's components *)
1084 :     in
1085 :     LET(v, mkStrexp(def, d), hdr b)
1086 : monnier 16 end
1087 : blume 587 | _ => bug "mkStrbs: unexpected access")
1088 : monnier 16 | g _ = bug "unexpected structure bindings in mkStrbs"
1089 : blume 587 in fold g sbs
1090 : monnier 16 end
1091 :    
1092 :     and mkFctbs (fbs, d) =
1093 : blume 587 let fun g (FCTB{fct=M.FCT { access, ... }, def, ... }, b) =
1094 :     (case access of
1095 :     DA.LVAR v =>
1096 : monnier 16 let val hdr = buildHdr v
1097 : blume 587 in
1098 :     LET(v, mkFctexp(def, d), hdr b)
1099 : monnier 16 end
1100 : blume 587 | _ => bug "mkFctbs: unexpected access")
1101 : monnier 16 | g _ = bug "unexpected functor bindings in mkStrbs"
1102 : blume 587 in fold g fbs
1103 : monnier 16 end
1104 :    
1105 :    
1106 :     (***************************************************************************
1107 :     * Translating absyn decls and exprs into lambda expression: *
1108 :     * *
1109 :     * val mkExp : A.exp * DI.depth -> L.lexp *
1110 :     * val mkDec : A.dec * DI.depth -> L.lexp -> L.lexp *
1111 :     * *
1112 :     ***************************************************************************)
1113 :     and mkDec (dec, d) =
1114 :     let fun g (VALdec vbs) = mkVBs(vbs, d)
1115 :     | g (VALRECdec rvbs) = mkRVBs(rvbs, d)
1116 :     | g (ABSTYPEdec{body,...}) = g body
1117 :     | g (EXCEPTIONdec ebs) = mkEBs(ebs, d)
1118 :     | g (STRdec sbs) = mkStrbs(sbs, d)
1119 :     | g (ABSdec sbs) = mkStrbs(sbs, d)
1120 :     | g (FCTdec fbs) = mkFctbs(fbs, d)
1121 :     | g (LOCALdec(ld, vd)) = (g ld) o (g vd)
1122 :     | g (SEQdec ds) = foldr (op o) ident (map g ds)
1123 :     | g (MARKdec(x, reg)) =
1124 :     let val f = withRegion reg g x
1125 :     in fn y => withRegion reg f y
1126 :     end
1127 : monnier 100 | g (OPENdec xs) =
1128 :     let (* special hack to make the import tree simpler *)
1129 : blume 587 fun mkos (_, s as M.STR { access = acc, ... }) =
1130 :     if extern acc then
1131 : monnier 100 let val _ = mkAccT(acc, strLty(s, d, compInfo), NONE)
1132 : blume 587 in ()
1133 : monnier 100 end
1134 : blume 587 else ()
1135 : monnier 100 | mkos _ = ()
1136 :     in app mkos xs; ident
1137 :     end
1138 : monnier 16 | g _ = ident
1139 :     in g dec
1140 :     end
1141 :    
1142 :     and mkExp (exp, d) =
1143 : monnier 45 let val tTyc = toTyc d
1144 :     val tLty = toLty d
1145 : monnier 16
1146 :     fun mkRules xs = map (fn (RULE(p, e)) => (fillPat(p, d), g e)) xs
1147 :    
1148 : macqueen 1967 and g (VARexp (ref v, ts)) =
1149 :     mkVE(v, map T.VARty ts, d)
1150 : monnier 16
1151 :     | g (CONexp (dc, ts)) = mkCE(dc, ts, NONE, d)
1152 :     | g (APPexp (CONexp(dc, ts), e2)) = mkCE(dc, ts, SOME(g e2), d)
1153 :    
1154 :     | g (INTexp (s, t)) =
1155 :     ((if TU.equalType (t, BT.intTy) then INT (LN.int s)
1156 :     else if TU.equalType (t, BT.int32Ty) then INT32 (LN.int32 s)
1157 : mblume 1347 else if TU.equalType (t, BT.intinfTy) then VAR (getII s)
1158 : mblume 1682 else if TU.equalType (t, BT.int64Ty) then
1159 :     let val (hi, lo) = LN.int64 s
1160 :     in RECORD [WORD32 hi, WORD32 lo]
1161 :     end
1162 : mblume 1347 else bug "translate INTexp")
1163 :     handle Overflow => (repErr "int constant too large"; INT 0))
1164 : monnier 16
1165 :     | g (WORDexp(s, t)) =
1166 :     ((if TU.equalType (t, BT.wordTy) then WORD (LN.word s)
1167 : mblume 1682 else if TU.equalType (t, BT.word8Ty) then WORD (LN.word8 s)
1168 :     else if TU.equalType (t, BT.word32Ty) then WORD32 (LN.word32 s)
1169 :     else if TU.equalType (t, BT.word64Ty) then
1170 :     let val (hi, lo) = LN.word64 s
1171 :     in RECORD [WORD32 hi, WORD32 lo]
1172 :     end
1173 :     else (ppType t; bug "translate WORDexp"))
1174 : monnier 16 handle Overflow => (repErr "word constant too large"; INT 0))
1175 :    
1176 :     | g (REALexp s) = REAL s
1177 :     | g (STRINGexp s) = STRING s
1178 :     | g (CHARexp s) = INT (Char.ord(String.sub(s, 0)))
1179 :     (** NOTE: the above won't work for cross compiling to
1180 :     multi-byte characters **)
1181 :    
1182 : monnier 45 | g (RECORDexp []) = unitLexp
1183 : monnier 16 | g (RECORDexp xs) =
1184 :     if sorted xs then RECORD (map (fn (_,e) => g e) xs)
1185 :     else let val vars = map (fn (l,e) => (l,(g e, mkv()))) xs
1186 :     fun bind ((_,(e,v)),x) = LET(v,e,x)
1187 :     val bexp = map (fn (_,(_,v)) => VAR v) (sortrec vars)
1188 :     in foldr bind (RECORD bexp) vars
1189 :     end
1190 :    
1191 :     | g (SELECTexp (LABEL{number=i,...}, e)) = SELECT(i, g e)
1192 :    
1193 :     | g (VECTORexp ([], ty)) =
1194 :     TAPP(coreAcc "vector0", [tTyc ty])
1195 :     | g (VECTORexp (xs, ty)) =
1196 :     let val tc = tTyc ty
1197 :     val vars = map (fn e => (g e, mkv())) xs
1198 :     fun bind ((e,v),x) = LET(v, e, x)
1199 :     val bexp = map (fn (_,v) => VAR v) vars
1200 :     in foldr bind (VECTOR (bexp, tc)) vars
1201 :     end
1202 :    
1203 :     | g (PACKexp(e, ty, tycs)) = g e
1204 : macqueen 1967 (* [dbm, 7/10/06]: Does PACKexp do anything now? What was it doing before
1205 :     * this was commented out? This appears to be the only place reformat was called
1206 :     * Is it also the only place the FLINT PACK constructor is used? [KM???] *)
1207 :     (* (by who, when why?)
1208 : monnier 16 let val (nty, ks, tps) = TU.reformat(ty, tycs, d)
1209 : monnier 45 val ts = map (tpsTyc d) tps
1210 : monnier 16 (** use of LtyEnv.tcAbs is a temporary hack (ZHONG) **)
1211 :     val nts = ListPair.map LtyEnv.tcAbs (ts, ks)
1212 :     val nd = DI.next d
1213 :     in case (ks, tps)
1214 :     of ([], []) => g e
1215 : monnier 45 | _ => PACK(LT.ltc_poly(ks, [toLty nd nty]),
1216 :     ts, nts , g e)
1217 : monnier 16 end
1218 :     *)
1219 :     | g (SEQexp [e]) = g e
1220 :     | g (SEQexp (e::r)) = LET(mkv(), g e, g (SEQexp r))
1221 :    
1222 :     | g (APPexp (e1, e2)) = APP(g e1, g e2)
1223 :     | g (MARKexp (e, reg)) = withRegion reg g e
1224 :     | g (CONSTRAINTexp (e,_)) = g e
1225 :    
1226 :     | g (RAISEexp (e, ty)) = mkRaise(g e, tLty ty)
1227 : mblume 1641 | g (HANDLEexp (e, (l, ty))) =
1228 : monnier 16 let val rootv = mkv()
1229 :     fun f x = FN(rootv, tLty ty, x)
1230 :     val l' = mkRules l
1231 : monnier 45 in HANDLE(g e, MC.handCompile(env, l', f,
1232 : mblume 1347 rootv, toTcLt d, complain,
1233 :     genintinfswitch))
1234 : monnier 16 end
1235 :    
1236 :     | g (FNexp (l, ty)) =
1237 :     let val rootv = mkv()
1238 :     fun f x = FN(rootv, tLty ty, x)
1239 : mblume 1347 in MC.matchCompile (env, mkRules l, f, rootv, toTcLt d,
1240 :     complain, genintinfswitch)
1241 : monnier 16 end
1242 :    
1243 :     | g (CASEexp (ee, l, isMatch)) =
1244 :     let val rootv = mkv()
1245 :     val ee' = g ee
1246 :     fun f x = LET(rootv, ee', x)
1247 :     val l' = mkRules l
1248 :     in if isMatch
1249 : mblume 1347 then MC.matchCompile (env, l', f, rootv, toTcLt d,
1250 :     complain, genintinfswitch)
1251 :     else MC.bindCompile (env, l', f, rootv, toTcLt d,
1252 :     complain, genintinfswitch)
1253 : monnier 16 end
1254 :    
1255 : mblume 1332 | g (IFexp { test, thenCase, elseCase }) =
1256 :     COND (g test, g thenCase, g elseCase)
1257 :    
1258 :     | g (ANDALSOexp (e1, e2)) =
1259 :     COND (g e1, g e2, falseLexp)
1260 :    
1261 :     | g (ORELSEexp (e1, e2)) =
1262 :     COND (g e1, trueLexp, g e2)
1263 :    
1264 :     | g (WHILEexp { test, expr }) =
1265 :     let val fv = mkv ()
1266 :     val body =
1267 :     FN (mkv (), lt_unit,
1268 :     COND (g test,
1269 :     LET (mkv (), g expr, APP (VAR fv, unitLexp)),
1270 :     unitLexp))
1271 :     in
1272 :     FIX ([fv], [lt_u_u], [body], APP (VAR fv, unitLexp))
1273 :     end
1274 :    
1275 : monnier 16 | g (LETexp (dc, e)) = mkDec (dc, d) (g e)
1276 :    
1277 :     | g e =
1278 :     EM.impossibleWithBody "untranslateable expression"
1279 : macqueen 1344 (fn ppstrm => (PP.string ppstrm " expression: ";
1280 : monnier 16 PPAbsyn.ppExp (env,NONE) ppstrm (e, !ppDepth)))
1281 :    
1282 :     in g exp
1283 :     end
1284 :    
1285 : mblume 1347 and transIntInf d s =
1286 :     (* This is a temporary solution. Since IntInf literals
1287 :     * are created using a core function call, there is
1288 :     * no indication within the program that we are really
1289 :     * dealing with a constant value that -- in principle --
1290 :     * could be subject to such things as constant folding. *)
1291 :     let val consexp = CONexp (BT.consDcon, [BT.wordTy])
1292 :     fun build [] = CONexp (BT.nilDcon, [BT.wordTy])
1293 :     | build (d :: ds) = let
1294 :     val i = Word.toIntX d
1295 :     in
1296 :     APPexp (consexp,
1297 :     EU.TUPLEexp [WORDexp (IntInf.fromInt i, BT.wordTy),
1298 :     build ds])
1299 :     end
1300 :     fun small w =
1301 :     APP (coreAcc (if LN.isNegative s then "makeSmallNegInf"
1302 :     else "makeSmallPosInf"),
1303 :     mkExp (WORDexp (IntInf.fromInt (Word.toIntX w), BT.wordTy),
1304 :     d))
1305 :     in
1306 :     case LN.repDigits s of
1307 :     [] => small 0w0
1308 :     | [w] => small w
1309 :     | ws => APP (coreAcc (if LN.isNegative s then "makeNegInf"
1310 :     else "makePosInf"),
1311 :     mkExp (build ws, d))
1312 :     end
1313 : monnier 16
1314 : mblume 1347 (* Wrap bindings for IntInf.int literals around body. *)
1315 :     fun wrapII body = let
1316 :     fun one (n, v, b) = LET (v, transIntInf DI.top n, b)
1317 :     in
1318 :     IIMap.foldli one body (!iimap)
1319 :     end
1320 :    
1321 : monnier 100 (* wrapPidInfo: lexp * (pid * pidInfo) list -> lexp * importTree *)
1322 :     fun wrapPidInfo (body, pidinfos) =
1323 :     let val imports =
1324 :     let fun p2itree (ANON xl) =
1325 : blume 879 ImportTree.ITNODE (map (fn (i,z) => (i, p2itree z)) xl)
1326 :     | p2itree (NAMED _) = ImportTree.ITNODE []
1327 : monnier 100 in map (fn (p, pi) => (p, p2itree pi)) pidinfos
1328 :     end
1329 :     (*
1330 :     val _ = let val _ = say "\n ****************** \n"
1331 :     val _ = say "\n the current import tree is :\n"
1332 : blume 879 fun tree (ImportTree.ITNODE []) = ["\n"]
1333 :     | tree (ImportTree.ITNODE xl) =
1334 : monnier 100 foldr (fn ((i, x), z) =>
1335 :     let val ts = tree x
1336 :     val u = (Int.toString i) ^ " "
1337 :     in (map (fn y => (u ^ y)) ts) @ z
1338 :     end) [] xl
1339 :     fun pp (p, n) =
1340 :     (say ("Pid " ^ (PersStamps.toHex p) ^ "\n");
1341 :     app say (tree n))
1342 :     in app pp imports; say "\n ****************** \n"
1343 :     end
1344 :     *)
1345 :     val plexp =
1346 :     let fun get ((_, ANON xl), z) = foldl get z xl
1347 :     | get ((_, u as NAMED (_,t,_)), (n,cs,ts)) =
1348 :     (n+1, (n,u)::cs, t::ts)
1349 : monnier 16
1350 : monnier 100 (* get the fringe information *)
1351 :     val getp = fn ((_, pi), z) => get((0, pi), z)
1352 :     val (finfos, lts) =
1353 :     let val (_, fx, lx) = foldl getp (0,[],[]) pidinfos
1354 :     in (rev fx, rev lx)
1355 :     end
1356 : monnier 16
1357 : monnier 100 (* do the selection of all import variables *)
1358 :     fun mksel (u, xl, be) =
1359 :     let fun g ((i, pi), be) =
1360 :     let val (v, xs) = case pi of ANON z => (mkv(), z)
1361 :     | NAMED(v,_,z) => (v, z)
1362 :     in LET(v, SELECT(i, u), mksel(VAR v, xs, be))
1363 :     end
1364 :     in foldr g be xl
1365 :     end
1366 :     val impvar = mkv()
1367 :     val implty = LT.ltc_str lts
1368 :     val nbody = mksel (VAR impvar, finfos, body)
1369 :     in FN(impvar, implty, nbody)
1370 :     end
1371 :     in (plexp, imports)
1372 :     end (* function wrapPidInfo *)
1373 : monnier 16
1374 : monnier 100 (** the list of things being exported from the current compilation unit *)
1375 : monnier 16 val exportLexp = SRECORD (map VAR exportLvars)
1376 :    
1377 : monnier 100 (** translating the ML absyn into the PLambda expression *)
1378 :     val body = mkDec (rootdec, DI.top) exportLexp
1379 :    
1380 : mblume 1347 (** add bindings for intinf constants *)
1381 :     val body = wrapII body
1382 :    
1383 : monnier 100 (** wrapping up the body with the imported variables *)
1384 : mblume 1347 val (plexp, imports) = wrapPidInfo (body, PersMap.listItemsi (!persmap))
1385 : monnier 100
1386 :     fun prGen (flag,printE) s e =
1387 :     if !flag then (say ("\n\n[After " ^ s ^ " ...]\n\n"); printE e) else ()
1388 : monnier 122 val _ = prGen(Control.FLINT.print, PPLexp.printLexp) "Translate" plexp
1389 : monnier 100
1390 :     (** normalizing the plambda expression into FLINT *)
1391 :     val flint = FlintNM.norm plexp
1392 :    
1393 :     in {flint = flint, imports = imports}
1394 : monnier 16 end (* function transDec *)
1395 :    
1396 :     end (* top-level local *)
1397 :     end (* structure Translate *)

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