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

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