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 1982 - (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 1980 let val inst = (#1 (TU.instantiatePoly intrinsicType))
887 :     in PPType.ppType env ppstrm inst
888 :     end));
889 :     bug "primop intrinsic type doesn't match occurrence type")
890 :     val _ = print "mkVE: after matchInstTypes\n"
891 : macqueen 1970 in case (primop, intrinsicParams)
892 :     of (PO.POLYEQL, [t]) => eqGen(intrinsicType, t, toTcLt d)
893 :     | (PO.POLYNEQ, [t]) =>
894 :     composeNOT(eqGen(intrinsicType, t, toTcLt d), toLty d t)
895 :     | (PO.INLMKARRAY, [t]) =>
896 :     let val dict =
897 :     {default = coreAcc "mkNormArray",
898 :     table = [([LT.tcc_real], coreAcc "mkRealArray")]}
899 :     in GENOP (dict, primop, toLty d intrinsicType,
900 :     map (toTyc d) intrinsicParams)
901 :     end
902 :     | (PO.RAW_CCALL NONE, [a, b, c]) =>
903 :     let val i = SOME (CProto.decode cproto_conv
904 :     { fun_ty = a, encoding = b })
905 :     handle CProto.BadEncoding => NONE
906 :     in PRIM (PO.RAW_CCALL i, toLty d intrinsicType,
907 :     map (toTyc d) intrinsicParams)
908 :     end
909 :     | _ => transPrim(primop, (toLty d intrinsicType),
910 :     map (toTyc d) intrinsicParams)
911 :     end
912 : georgekuan 1971 | mkVE (v as V.VALvar{typ, prim = PrimOpId.NonPrim, ... }, ts, d) =
913 : macqueen 1970 (* non primop variable *)
914 :     (case ts
915 :     of [] => mkVar (v, d)
916 :     | _ => TAPP(mkVar(v, d), map (toTyc d) ts))
917 :     (* dbm: when does this second case occur? *)
918 :     | mkVE _ = bug "non VALvar passed to mkVE"
919 : monnier 16
920 : macqueen 1970
921 : monnier 16 fun mkCE (TP.DATACON{const, rep, name, typ, ...}, ts, apOp, d) =
922 :     let val lt = toDconLty d typ
923 : monnier 100 val rep' = mkRep(rep, lt, name)
924 : monnier 16 val dc = (name, rep', lt)
925 : macqueen 1970 val ts' = map (toTyc d o TP.VARty) ts
926 : monnier 16 in if const then CON'(dc, ts', unitLexp)
927 :     else (case apOp
928 :     of SOME le => CON'(dc, ts', le)
929 :     | NONE =>
930 :     let val (argT, _) = LT.ltd_parrow(LT.lt_pinst(lt, ts'))
931 :     val v = mkv()
932 :     in FN(v, argT, CON'(dc, ts', VAR v))
933 :     end)
934 :     end
935 :    
936 : georgekuan 1971 fun mkStr (s as M.STR { access, prim, ... }, d) =
937 :     mkAccInfo(access, prim, fn () => strLty(s, d, compInfo), NONE)
938 : monnier 16 | mkStr _ = bug "unexpected structures in mkStr"
939 :    
940 : georgekuan 1971 fun mkFct (f as M.FCT { access, prim, ... }, d) =
941 :     mkAccInfo(access, prim, fn () => fctLty(f, d, compInfo), NONE)
942 : monnier 16 | mkFct _ = bug "unexpected functors in mkFct"
943 :    
944 :     fun mkBnd d =
945 :     let fun g (B.VALbind v) = mkVar(v, d)
946 :     | g (B.STRbind s) = mkStr(s, d)
947 :     | g (B.FCTbind f) = mkFct(f, d)
948 : monnier 100 | g (B.CONbind (TP.DATACON{rep=(DA.EXN acc), name, typ, ...})) =
949 : blume 587 let val nt = toDconLty d typ
950 :     val (argt,_) = LT.ltd_parrow nt
951 :     in mkAccT (acc, LT.ltc_etag argt, SOME name)
952 :     end
953 : monnier 16 | g _ = bug "unexpected bindings in mkBnd"
954 :     in g
955 :     end
956 :    
957 :    
958 :     (***************************************************************************
959 :     * *
960 :     * Translating core absyn declarations into lambda expressions: *
961 :     * *
962 : macqueen 1970 * val mkVBs : Absyn.vb list * depth -> PLambda.lexp -> PLambda.lexp *
963 :     * val mkRVBs : Absyn.rvb list * depth -> PLambda.lexp -> PLambda.lexp *
964 :     * val mkEBs : Absyn.eb list * depth -> PLambda.lexp -> PLambda.lexp *
965 : monnier 16 * *
966 :     ***************************************************************************)
967 : macqueen 1970
968 :     (* mkPE : Absyn.exp * depth * Types.tyvar list -> PLambda.lexp
969 :     * translate an expression with potential type parameters *)
970 : monnier 16 fun mkPE (exp, d, []) = mkExp(exp, d)
971 :     | mkPE (exp, d, boundtvs) =
972 :     let val savedtvs = map ! boundtvs
973 : macqueen 1970 (* save original contents of boundtvs for later restoration
974 :     * by the restore function below *)
975 : monnier 16
976 : macqueen 1970 fun setbtvs (i, []) = ()
977 :     | setbtvs (i, (tv as ref (TP.OPEN _))::rest) =
978 :     let val m = markLBOUND (d, i)
979 :     in tv := TP.TV_MARK m;
980 :     setbtvs (i+1, rest)
981 :     end
982 :     | setbtvs (i, (tv as ref (TP.TV_MARK _))::res) =
983 :     bug ("unexpected tyvar TV_MARK in mkPE")
984 :     | setbtvs _ = bug "unexpected tyvar INSTANTIATED in mkPE"
985 : monnier 16
986 : macqueen 1970 val _ = setbtvs(0, boundtvs)
987 :     (* assign TV_MARKs to the boundtvs to mark them as type
988 :     * parameter variables during translation of exp *)
989 :    
990 : monnier 16 val exp' = mkExp(exp, DI.next d)
991 : macqueen 1970 (* increase the depth to indicate that the expression is
992 :     * going to be wrapped by a type abstraction (TFN) *)
993 : monnier 16
994 : macqueen 1970 (* restore tyvar states to that before the translation *)
995 : macqueen 1967 fun restore ([], []) = ()
996 :     | restore (a::r, b::z) = (b := a; restore(r, z))
997 :     | restore _ = bug "unexpected cases in mkPE"
998 : monnier 16
999 : macqueen 1948 (* [dbm, 6/22/06] Why do we need to restore the original
1000 : macqueen 1967 contents of the uninstantiated meta type variables?
1001 :     Only seems to be necessary if a given tyvar gets generalized
1002 : macqueen 1970 in two different valbinds. We assume that this does not
1003 :     happen (Single Generalization Conjecture) *)
1004 : macqueen 1948
1005 : macqueen 1967 val _ = restore(savedtvs, boundtvs)
1006 : monnier 16 val len = length(boundtvs)
1007 :    
1008 :     in TFN(LT.tkc_arg(len), exp')
1009 :     end
1010 :    
1011 :     and mkVBs (vbs, d) =
1012 : macqueen 1970 let fun mkVB (VB{pat=VARpat(V.VALvar{access=DA.LVAR v, ...}),
1013 :     exp as VARexp (ref (w as (V.VALvar{typ,prim,...})), instvs),
1014 :     boundtvs=btvs, ...}, b: lexp) =
1015 : macqueen 1967 (* [dbm: 7/10/06] Originally, the mkVar and mkPE translations
1016 :     * were chosen based on whether btvs and instvs were the same
1017 :     * list of tyvars, which would be the case for all non-primop
1018 :     * variables, but also in the primop case whenever the rhs
1019 :     * variable environment type (!typ) was the same (equalTypeP)
1020 :     * to the intrinsic type of the primop (e.g. when they are
1021 :     * both monotypes). So in most cases, the mkVar translation
1022 :     * will be used, and this drops the primop information!!!
1023 :     * This seems definitely wrong. *)
1024 :     (case prim
1025 :     of PrimOpId.Prim name =>
1026 : macqueen 1976 (case PrimOpTypeMap.primopTypeMap name
1027 :     of SOME(primopty) =>
1028 : macqueen 1970 if TU.equalTypeP(!typ,primopty)
1029 :     then LET(v, mkVar(w, d), b)
1030 :     else LET(v, mkPE(exp, d, btvs), b)
1031 :     | NONE => bug "mkVBs: unknown primop name")
1032 : georgekuan 1968 | _ => LET(v, mkVar(w, d), b))
1033 : macqueen 1967 (* when generalized variables = instantiation params *)
1034 : monnier 16
1035 : macqueen 1970 | mkVB (VB{pat=VARpat(V.VALvar{access=DA.LVAR v, ...}),
1036 :     exp, boundtvs=btvs, ...}, b) =
1037 :     LET(v, mkPE(exp, d, btvs), b)
1038 : monnier 16
1039 : macqueen 1970 | mkVB (VB{pat=CONSTRAINTpat(VARpat(V.VALvar{access=DA.LVAR v, ...}),_),
1040 :     exp, boundtvs=tvs, ...}, b) =
1041 :     LET(v, mkPE(exp, d, tvs), b)
1042 : monnier 16
1043 : macqueen 1970 | mkVB (VB{pat, exp, boundtvs=tvs, ...}, b) =
1044 :     let val ee = mkPE(exp, d, tvs)
1045 :     val rules = [(fillPat(pat, d), b), (WILDpat, unitLexp)]
1046 :     val rootv = mkv()
1047 :     fun finish x = LET(rootv, ee, x)
1048 :     in MC.bindCompile(env, rules, finish, rootv, toTcLt d, complain,
1049 :     genintinfswitch)
1050 :     end
1051 : monnier 16
1052 : macqueen 1970 in fold mkVB vbs
1053 :     end (* mkVBs *)
1054 :    
1055 : monnier 16 and mkRVBs (rvbs, d) =
1056 : macqueen 1970 let fun mkRVB (RVB{var=V.VALvar{access=DA.LVAR v, typ=ref ty, ...},
1057 :     exp, boundtvs=btvs, ...}, (vlist, tlist, elist)) =
1058 :     let val ee = mkExp(exp, d) (* was mkPE(exp, d, btvs) *)
1059 :     (* [ZHONG?] we no longer track type bindings at RVB anymore ! *)
1060 :     val vt = toLty d ty
1061 :     in (v::vlist, vt::tlist, ee::elist)
1062 :     end
1063 :     | mkRVB _ = bug "unexpected valrec bindings in mkRVBs"
1064 : monnier 16
1065 : macqueen 1970 val (vlist, tlist, elist) = foldr mkRVB ([], [], []) rvbs
1066 : monnier 16
1067 :     in fn b => FIX(vlist, tlist, elist, b)
1068 :     end
1069 :    
1070 :     and mkEBs (ebs, d) =
1071 :     let fun g (EBgen {exn=TP.DATACON{rep=DA.EXN(DA.LVAR v), typ, ...},
1072 :     ident, ...}, b) =
1073 :     let val nt = toDconLty d typ
1074 :     val (argt, _) = LT.ltd_parrow nt
1075 :     in LET(v, ETAG(mkExp(ident, d), argt), b)
1076 :     end
1077 : monnier 100 | g (EBdef {exn=TP.DATACON{rep=DA.EXN(DA.LVAR v), typ, name, ...},
1078 : monnier 16 edef=TP.DATACON{rep=DA.EXN(acc), ...}}, b) =
1079 :     let val nt = toDconLty d typ
1080 :     val (argt, _) = LT.ltd_parrow nt
1081 : monnier 100 in LET(v, mkAccT(acc, LT.ltc_etag argt, SOME name), b)
1082 : monnier 16 end
1083 :     | g _ = bug "unexpected exn bindings in mkEBs"
1084 :    
1085 :     in fold g ebs
1086 :     end
1087 :    
1088 :    
1089 :     (***************************************************************************
1090 :     * *
1091 :     * Translating module exprs and decls into lambda expressions: *
1092 :     * *
1093 : macqueen 1970 * val mkStrexp : Absyn.strexp * depth -> PLambda.lexp *
1094 :     * val mkFctexp : Absyn.fctexp * depth -> PLambda.lexp *
1095 :     * val mkStrbs : Absyn.strb list * depth -> PLambda.lexp -> PLambda.lexp *
1096 :     * val mkFctbs : Absyn.fctb list * depth -> PLambda.lexp -> PLambda.lexp *
1097 : monnier 16 * *
1098 :     ***************************************************************************)
1099 :     and mkStrexp (se, d) =
1100 :     let fun g (VARstr s) = mkStr(s, d)
1101 :     | g (STRstr bs) = SRECORD (map (mkBnd d) bs)
1102 :     | g (APPstr {oper, arg, argtycs}) =
1103 :     let val e1 = mkFct(oper, d)
1104 : monnier 45 val tycs = map (tpsTyc d) argtycs
1105 : monnier 16 val e2 = mkStr(arg, d)
1106 :     in APP(TAPP(e1, tycs), e2)
1107 :     end
1108 :     | g (LETstr (dec, b)) = mkDec (dec, d) (g b)
1109 :     | g (MARKstr (b, reg)) = withRegion reg g b
1110 :    
1111 :     in g se
1112 :     end
1113 :    
1114 :     and mkFctexp (fe, d) =
1115 :     let fun g (VARfct f) = mkFct(f, d)
1116 : blume 587 | g (FCTfct {param as M.STR { access, ... }, argtycs, def }) =
1117 :     (case access of
1118 :     DA.LVAR v =>
1119 :     let val knds = map tpsKnd argtycs
1120 :     val nd = DI.next d
1121 :     val body = mkStrexp (def, nd)
1122 :     val hdr = buildHdr v
1123 :     (* binding of all v's components *)
1124 :     in
1125 :     TFN(knds, FN(v, strLty(param, nd, compInfo), hdr body))
1126 :     end
1127 :     | _ => bug "mkFctexp: unexpected access")
1128 : monnier 16 | g (LETfct (dec, b)) = mkDec (dec, d) (g b)
1129 :     | g (MARKfct (b, reg)) = withRegion reg g b
1130 :     | g _ = bug "unexpected functor expressions in mkFctexp"
1131 :    
1132 :     in g fe
1133 :     end
1134 :    
1135 :     and mkStrbs (sbs, d) =
1136 : blume 587 let fun g (STRB{str=M.STR { access, ... }, def, ... }, b) =
1137 :     (case access of
1138 :     DA.LVAR v =>
1139 : monnier 16 let val hdr = buildHdr v
1140 : blume 587 (* binding of all v's components *)
1141 :     in
1142 :     LET(v, mkStrexp(def, d), hdr b)
1143 : monnier 16 end
1144 : blume 587 | _ => bug "mkStrbs: unexpected access")
1145 : monnier 16 | g _ = bug "unexpected structure bindings in mkStrbs"
1146 : blume 587 in fold g sbs
1147 : monnier 16 end
1148 :    
1149 :     and mkFctbs (fbs, d) =
1150 : blume 587 let fun g (FCTB{fct=M.FCT { access, ... }, def, ... }, b) =
1151 :     (case access of
1152 :     DA.LVAR v =>
1153 : monnier 16 let val hdr = buildHdr v
1154 : blume 587 in
1155 :     LET(v, mkFctexp(def, d), hdr b)
1156 : monnier 16 end
1157 : blume 587 | _ => bug "mkFctbs: unexpected access")
1158 : monnier 16 | g _ = bug "unexpected functor bindings in mkStrbs"
1159 : blume 587 in fold g fbs
1160 : monnier 16 end
1161 :    
1162 :    
1163 :     (***************************************************************************
1164 :     * Translating absyn decls and exprs into lambda expression: *
1165 :     * *
1166 : macqueen 1970 * val mkExp : A.exp * DI.depth -> PLambda.lexp *
1167 :     * val mkDec : A.dec * DI.depth -> PLambda.lexp -> PLambda.lexp *
1168 : monnier 16 * *
1169 :     ***************************************************************************)
1170 :     and mkDec (dec, d) =
1171 : georgekuan 1980 let fun g (VALdec vbs) = (print "VALdec"; mkVBs(vbs, d))
1172 :     | g (VALRECdec rvbs) = (print "VALRECdec"; mkRVBs(rvbs, d))
1173 : monnier 16 | g (ABSTYPEdec{body,...}) = g body
1174 : georgekuan 1980 | g (EXCEPTIONdec ebs) = (print "EXCEPTIONdec"; mkEBs(ebs, d))
1175 :     | g (STRdec sbs) = (print "STRdec"; mkStrbs(sbs, d))
1176 :     | g (ABSdec sbs) = (print "ABSdec"; mkStrbs(sbs, d))
1177 :     | g (FCTdec fbs) = (print "FCTdec"; mkFctbs(fbs, d))
1178 : monnier 16 | g (LOCALdec(ld, vd)) = (g ld) o (g vd)
1179 :     | g (SEQdec ds) = foldr (op o) ident (map g ds)
1180 :     | g (MARKdec(x, reg)) =
1181 :     let val f = withRegion reg g x
1182 :     in fn y => withRegion reg f y
1183 :     end
1184 : monnier 100 | g (OPENdec xs) =
1185 :     let (* special hack to make the import tree simpler *)
1186 : blume 587 fun mkos (_, s as M.STR { access = acc, ... }) =
1187 :     if extern acc then
1188 : monnier 100 let val _ = mkAccT(acc, strLty(s, d, compInfo), NONE)
1189 : blume 587 in ()
1190 : monnier 100 end
1191 : blume 587 else ()
1192 : monnier 100 | mkos _ = ()
1193 :     in app mkos xs; ident
1194 :     end
1195 : monnier 16 | g _ = ident
1196 :     in g dec
1197 :     end
1198 :    
1199 :     and mkExp (exp, d) =
1200 : monnier 45 let val tTyc = toTyc d
1201 :     val tLty = toLty d
1202 : monnier 16
1203 :     fun mkRules xs = map (fn (RULE(p, e)) => (fillPat(p, d), g e)) xs
1204 :    
1205 : macqueen 1967 and g (VARexp (ref v, ts)) =
1206 : georgekuan 1980 (print "mkExp VARexp\n"; mkVE(v, map TP.VARty ts, d))
1207 : monnier 16
1208 : georgekuan 1974 | g (CONexp (dc, ts)) = (let val _ = print "mkExp CONexp: "
1209 :     val c = mkCE(dc, ts, NONE, d)
1210 :     val _ = PPLexp.printLexp c
1211 :     in c end)
1212 :     | g (APPexp (CONexp(dc, ts), e2)) = (let val _ = print "mkExp APPexp: "
1213 :     val c = mkCE(dc, ts, SOME(g e2), d)
1214 :     val _ = PPLexp.printLexp c
1215 :     in c end)
1216 : monnier 16 | g (INTexp (s, t)) =
1217 : georgekuan 1980 (print "mkExp INTexp\n";
1218 : monnier 16 ((if TU.equalType (t, BT.intTy) then INT (LN.int s)
1219 :     else if TU.equalType (t, BT.int32Ty) then INT32 (LN.int32 s)
1220 : mblume 1347 else if TU.equalType (t, BT.intinfTy) then VAR (getII s)
1221 : mblume 1682 else if TU.equalType (t, BT.int64Ty) then
1222 :     let val (hi, lo) = LN.int64 s
1223 :     in RECORD [WORD32 hi, WORD32 lo]
1224 :     end
1225 : mblume 1347 else bug "translate INTexp")
1226 : georgekuan 1980 handle Overflow => (repErr "int constant too large"; INT 0)))
1227 : monnier 16
1228 :     | g (WORDexp(s, t)) =
1229 : georgekuan 1980 (print "WORDexp\n";
1230 : monnier 16 ((if TU.equalType (t, BT.wordTy) then WORD (LN.word s)
1231 : mblume 1682 else if TU.equalType (t, BT.word8Ty) then WORD (LN.word8 s)
1232 :     else if TU.equalType (t, BT.word32Ty) then WORD32 (LN.word32 s)
1233 :     else if TU.equalType (t, BT.word64Ty) then
1234 :     let val (hi, lo) = LN.word64 s
1235 :     in RECORD [WORD32 hi, WORD32 lo]
1236 :     end
1237 :     else (ppType t; bug "translate WORDexp"))
1238 : georgekuan 1980 handle Overflow => (repErr "word constant too large"; INT 0)))
1239 : monnier 16
1240 :     | g (REALexp s) = REAL s
1241 :     | g (STRINGexp s) = STRING s
1242 :     | g (CHARexp s) = INT (Char.ord(String.sub(s, 0)))
1243 :     (** NOTE: the above won't work for cross compiling to
1244 :     multi-byte characters **)
1245 :    
1246 : monnier 45 | g (RECORDexp []) = unitLexp
1247 : monnier 16 | g (RECORDexp xs) =
1248 :     if sorted xs then RECORD (map (fn (_,e) => g e) xs)
1249 :     else let val vars = map (fn (l,e) => (l,(g e, mkv()))) xs
1250 :     fun bind ((_,(e,v)),x) = LET(v,e,x)
1251 :     val bexp = map (fn (_,(_,v)) => VAR v) (sortrec vars)
1252 :     in foldr bind (RECORD bexp) vars
1253 :     end
1254 :    
1255 :     | g (SELECTexp (LABEL{number=i,...}, e)) = SELECT(i, g e)
1256 :    
1257 :     | g (VECTORexp ([], ty)) =
1258 :     TAPP(coreAcc "vector0", [tTyc ty])
1259 :     | g (VECTORexp (xs, ty)) =
1260 :     let val tc = tTyc ty
1261 :     val vars = map (fn e => (g e, mkv())) xs
1262 :     fun bind ((e,v),x) = LET(v, e, x)
1263 :     val bexp = map (fn (_,v) => VAR v) vars
1264 :     in foldr bind (VECTOR (bexp, tc)) vars
1265 :     end
1266 :    
1267 :     | g (PACKexp(e, ty, tycs)) = g e
1268 : macqueen 1967 (* [dbm, 7/10/06]: Does PACKexp do anything now? What was it doing before
1269 :     * this was commented out? This appears to be the only place reformat was called
1270 :     * Is it also the only place the FLINT PACK constructor is used? [KM???] *)
1271 : macqueen 1970 (* (commented out by whom, when why?)
1272 : monnier 16 let val (nty, ks, tps) = TU.reformat(ty, tycs, d)
1273 : monnier 45 val ts = map (tpsTyc d) tps
1274 : monnier 16 (** use of LtyEnv.tcAbs is a temporary hack (ZHONG) **)
1275 :     val nts = ListPair.map LtyEnv.tcAbs (ts, ks)
1276 :     val nd = DI.next d
1277 :     in case (ks, tps)
1278 :     of ([], []) => g e
1279 : monnier 45 | _ => PACK(LT.ltc_poly(ks, [toLty nd nty]),
1280 :     ts, nts , g e)
1281 : monnier 16 end
1282 :     *)
1283 :     | g (SEQexp [e]) = g e
1284 :     | g (SEQexp (e::r)) = LET(mkv(), g e, g (SEQexp r))
1285 :    
1286 :     | g (APPexp (e1, e2)) = APP(g e1, g e2)
1287 :     | g (MARKexp (e, reg)) = withRegion reg g e
1288 :     | g (CONSTRAINTexp (e,_)) = g e
1289 :    
1290 :     | g (RAISEexp (e, ty)) = mkRaise(g e, tLty ty)
1291 : mblume 1641 | g (HANDLEexp (e, (l, ty))) =
1292 : monnier 16 let val rootv = mkv()
1293 :     fun f x = FN(rootv, tLty ty, x)
1294 :     val l' = mkRules l
1295 : monnier 45 in HANDLE(g e, MC.handCompile(env, l', f,
1296 : mblume 1347 rootv, toTcLt d, complain,
1297 :     genintinfswitch))
1298 : monnier 16 end
1299 :    
1300 :     | g (FNexp (l, ty)) =
1301 :     let val rootv = mkv()
1302 :     fun f x = FN(rootv, tLty ty, x)
1303 : mblume 1347 in MC.matchCompile (env, mkRules l, f, rootv, toTcLt d,
1304 :     complain, genintinfswitch)
1305 : monnier 16 end
1306 :    
1307 :     | g (CASEexp (ee, l, isMatch)) =
1308 :     let val rootv = mkv()
1309 :     val ee' = g ee
1310 :     fun f x = LET(rootv, ee', x)
1311 :     val l' = mkRules l
1312 :     in if isMatch
1313 : mblume 1347 then MC.matchCompile (env, l', f, rootv, toTcLt d,
1314 :     complain, genintinfswitch)
1315 :     else MC.bindCompile (env, l', f, rootv, toTcLt d,
1316 :     complain, genintinfswitch)
1317 : monnier 16 end
1318 :    
1319 : mblume 1332 | g (IFexp { test, thenCase, elseCase }) =
1320 :     COND (g test, g thenCase, g elseCase)
1321 :    
1322 :     | g (ANDALSOexp (e1, e2)) =
1323 :     COND (g e1, g e2, falseLexp)
1324 :    
1325 :     | g (ORELSEexp (e1, e2)) =
1326 :     COND (g e1, trueLexp, g e2)
1327 :    
1328 :     | g (WHILEexp { test, expr }) =
1329 :     let val fv = mkv ()
1330 :     val body =
1331 :     FN (mkv (), lt_unit,
1332 :     COND (g test,
1333 :     LET (mkv (), g expr, APP (VAR fv, unitLexp)),
1334 :     unitLexp))
1335 :     in
1336 :     FIX ([fv], [lt_u_u], [body], APP (VAR fv, unitLexp))
1337 :     end
1338 :    
1339 : monnier 16 | g (LETexp (dc, e)) = mkDec (dc, d) (g e)
1340 :    
1341 :     | g e =
1342 :     EM.impossibleWithBody "untranslateable expression"
1343 : macqueen 1344 (fn ppstrm => (PP.string ppstrm " expression: ";
1344 : monnier 16 PPAbsyn.ppExp (env,NONE) ppstrm (e, !ppDepth)))
1345 :    
1346 :     in g exp
1347 :     end
1348 :    
1349 : mblume 1347 and transIntInf d s =
1350 :     (* This is a temporary solution. Since IntInf literals
1351 :     * are created using a core function call, there is
1352 :     * no indication within the program that we are really
1353 :     * dealing with a constant value that -- in principle --
1354 :     * could be subject to such things as constant folding. *)
1355 : georgekuan 1971 let val consexp = CONexp (BT.consDcon, [ref (TP.INSTANTIATED BT.wordTy)])
1356 :     fun build [] = CONexp (BT.nilDcon, [ref (TP.INSTANTIATED BT.wordTy)])
1357 : mblume 1347 | build (d :: ds) = let
1358 :     val i = Word.toIntX d
1359 :     in
1360 :     APPexp (consexp,
1361 :     EU.TUPLEexp [WORDexp (IntInf.fromInt i, BT.wordTy),
1362 :     build ds])
1363 :     end
1364 :     fun small w =
1365 :     APP (coreAcc (if LN.isNegative s then "makeSmallNegInf"
1366 :     else "makeSmallPosInf"),
1367 :     mkExp (WORDexp (IntInf.fromInt (Word.toIntX w), BT.wordTy),
1368 :     d))
1369 :     in
1370 :     case LN.repDigits s of
1371 :     [] => small 0w0
1372 :     | [w] => small w
1373 :     | ws => APP (coreAcc (if LN.isNegative s then "makeNegInf"
1374 :     else "makePosInf"),
1375 :     mkExp (build ws, d))
1376 :     end
1377 : monnier 16
1378 : mblume 1347 (* Wrap bindings for IntInf.int literals around body. *)
1379 :     fun wrapII body = let
1380 :     fun one (n, v, b) = LET (v, transIntInf DI.top n, b)
1381 :     in
1382 :     IIMap.foldli one body (!iimap)
1383 :     end
1384 :    
1385 : monnier 100 (* wrapPidInfo: lexp * (pid * pidInfo) list -> lexp * importTree *)
1386 :     fun wrapPidInfo (body, pidinfos) =
1387 :     let val imports =
1388 :     let fun p2itree (ANON xl) =
1389 : blume 879 ImportTree.ITNODE (map (fn (i,z) => (i, p2itree z)) xl)
1390 :     | p2itree (NAMED _) = ImportTree.ITNODE []
1391 : monnier 100 in map (fn (p, pi) => (p, p2itree pi)) pidinfos
1392 :     end
1393 :     (*
1394 :     val _ = let val _ = say "\n ****************** \n"
1395 :     val _ = say "\n the current import tree is :\n"
1396 : blume 879 fun tree (ImportTree.ITNODE []) = ["\n"]
1397 :     | tree (ImportTree.ITNODE xl) =
1398 : monnier 100 foldr (fn ((i, x), z) =>
1399 :     let val ts = tree x
1400 :     val u = (Int.toString i) ^ " "
1401 :     in (map (fn y => (u ^ y)) ts) @ z
1402 :     end) [] xl
1403 :     fun pp (p, n) =
1404 :     (say ("Pid " ^ (PersStamps.toHex p) ^ "\n");
1405 :     app say (tree n))
1406 :     in app pp imports; say "\n ****************** \n"
1407 :     end
1408 :     *)
1409 :     val plexp =
1410 :     let fun get ((_, ANON xl), z) = foldl get z xl
1411 :     | get ((_, u as NAMED (_,t,_)), (n,cs,ts)) =
1412 :     (n+1, (n,u)::cs, t::ts)
1413 : monnier 16
1414 : monnier 100 (* get the fringe information *)
1415 :     val getp = fn ((_, pi), z) => get((0, pi), z)
1416 :     val (finfos, lts) =
1417 :     let val (_, fx, lx) = foldl getp (0,[],[]) pidinfos
1418 :     in (rev fx, rev lx)
1419 :     end
1420 : monnier 16
1421 : monnier 100 (* do the selection of all import variables *)
1422 :     fun mksel (u, xl, be) =
1423 :     let fun g ((i, pi), be) =
1424 :     let val (v, xs) = case pi of ANON z => (mkv(), z)
1425 :     | NAMED(v,_,z) => (v, z)
1426 :     in LET(v, SELECT(i, u), mksel(VAR v, xs, be))
1427 :     end
1428 :     in foldr g be xl
1429 :     end
1430 :     val impvar = mkv()
1431 :     val implty = LT.ltc_str lts
1432 :     val nbody = mksel (VAR impvar, finfos, body)
1433 :     in FN(impvar, implty, nbody)
1434 :     end
1435 :     in (plexp, imports)
1436 :     end (* function wrapPidInfo *)
1437 : monnier 16
1438 : monnier 100 (** the list of things being exported from the current compilation unit *)
1439 : monnier 16 val exportLexp = SRECORD (map VAR exportLvars)
1440 :    
1441 : monnier 100 (** translating the ML absyn into the PLambda expression *)
1442 :     val body = mkDec (rootdec, DI.top) exportLexp
1443 :    
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