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/trunk/src/compiler/FLINT/trans/translate.sml
ViewVC logotype

Annotation of /sml/trunk/src/compiler/FLINT/trans/translate.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1249 - (view) (download)

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

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