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/main/flintcomp.sml
ViewVC logotype

Annotation of /sml/trunk/src/compiler/FLINT/main/flintcomp.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 251 - (view) (download)

1 : monnier 16 (* COPYRIGHT (c) 1998 YALE FLINT PROJECT *)
2 :     (* flintcomp.sml *)
3 :    
4 : monnier 251 functor FLINTComp (structure Gen: MACHINE_GEN
5 :     val collect: unit -> CodeObj.code_object) : CODEGENERATOR =
6 : monnier 16 struct
7 :    
8 :     local structure CB = CompBasic
9 : monnier 122 (* structure CGC = Control.CG *)
10 : monnier 16 structure MachSpec = Gen.MachSpec
11 :     structure Convert = Convert(MachSpec)
12 :     structure CPStrans = CPStrans(MachSpec)
13 :     structure CPSopt = CPSopt(MachSpec)
14 : monnier 102 structure Closure = Closure(MachSpec)
15 : monnier 251 structure Spill = SpillFn(MachSpec)
16 : monnier 16 structure CpsSplit = CpsSplitFun (MachSpec)
17 : monnier 220 structure CTRL = FLINT_Control
18 :     structure PP = PPFlint
19 :     structure LT = LtyExtern
20 :     structure O = Option
21 :     structure F = FLINT
22 : monnier 16 in
23 :    
24 : monnier 251 structure Machine = Gen
25 : monnier 16 val architecture = Gen.MachSpec.architecture
26 :     fun bug s = ErrorMsg.impossible ("FLINTComp:" ^ s)
27 : monnier 220 val say = Control_Print.say
28 : monnier 16
29 : monnier 197 datatype flintkind = FK_WRAP | FK_REIFY | FK_DEBRUIJN | FK_NAMED | FK_CPS
30 :    
31 : monnier 16 fun phase x = Stats.doPhase (Stats.makePhase x)
32 :    
33 : monnier 202 val deb2names = phase "Compiler 056 deb2names" TvarCvt.debIndex2names
34 :     val names2deb = phase "Compiler 057 names2deb" TvarCvt.names2debIndex
35 :    
36 :     val lcontract = phase "Compiler 052 lcontract" LContract.lcontract
37 : monnier 216 (* val lcontract' = phase "Compiler 052 lcontract'" LContract.lcontract *)
38 : monnier 164 val fcollect = phase "Compiler 052a fcollect" Collect.collect
39 :     val fcontract = phase "Compiler 052b fcontract" FContract.contract
40 : monnier 213 val fcontract = fcontract o fcollect
41 : monnier 191 val loopify = phase "Compiler 057 loopify" Loopify.loopify
42 : monnier 197 val fixfix = phase "Compiler 056 fixfix" FixFix.fixfix
43 : monnier 191
44 : monnier 216 val split = phase "Compiler 058 split" FSplit.split
45 :    
46 : monnier 197 val typelift = phase "Compiler 0535 typelift" Lift.typeLift
47 :     val wformed = phase "Compiler 0536 wformed" Lift.wellFormed
48 :    
49 : monnier 71 val specialize= phase "Compiler 053 specialize" Specialize.specialize
50 :     val wrapping = phase "Compiler 054 wrapping" Wrapping.wrapping
51 :     val reify = phase "Compiler 055 reify" Reify.reify
52 : monnier 220 val recover = phase "Compiler 05a recover" Recover.recover
53 : monnier 16
54 : monnier 71 val convert = phase "Compiler 060 convert" Convert.convert
55 :     val cpstrans = phase "Compiler 065 cpstrans" CPStrans.cpstrans
56 : monnier 16 val cpsopt = phase "Compiler 070 cpsopt" CPSopt.reduce
57 : monnier 102 val litsplit = phase "Compiler 075 litsplit" Literals.litsplit
58 : monnier 251 val litToBytes = phase "Compiler 076 litToBytes" Literals.litToBytes
59 : monnier 102 val closure = phase "Compiler 080 closure" Closure.closeCPS
60 : monnier 16 val globalfix = phase "Compiler 090 globalfix" GlobalFix.globalfix
61 : monnier 251 val spill = phase "Compiler 100 spill" Spill.spill
62 : monnier 16 val limit = phase "Compiler 110 limit" Limit.nolimit
63 :     val codegen = phase "Compiler 120 cpsgen" Gen.codegen
64 :    
65 : monnier 71 (** pretty printing for the FLINT and CPS code *)
66 :     val (prF, prC) =
67 :     let fun prGen (flag,printE) s e =
68 : monnier 102 if !flag then (say ("\n[After " ^ s ^ " ...]\n\n"); printE e;
69 :     say "\n"; e)
70 : monnier 71 else e
71 : monnier 162 in (prGen (CTRL.print, PPFlint.printProg),
72 : monnier 122 prGen (Control.CG.printit, PPCps.printcps0))
73 : monnier 71 end
74 : monnier 16
75 : monnier 71 (** writing out a term into a error output file *)
76 :     fun dumpTerm (printE, s, le) =
77 :     let val outS = TextIO.openAppend s;
78 :     val saveOut = !Control.Print.out
79 :     fun done () =
80 :     (TextIO.closeOut outS; Control.Print.out := saveOut)
81 :     in Control.Print.out := {say = fn s => TextIO.output(outS,s),
82 :     flush = fn () => TextIO.flushOut outS};
83 :     printE le handle x => (done () handle _ => (); raise x);
84 :     done ()
85 :     end (* function dumpTerm *)
86 :    
87 : monnier 189 val fcs : (FLINT.prog -> FLINT.prog) list ref = ref []
88 :    
89 : monnier 16 (** compiling FLINT code into the binary machine code *)
90 :     fun flintcomp(flint, compInfo as {error, sourceName=src, ...}: CB.compInfo) =
91 :     let fun err severity s =
92 :     error (0,0) severity (concat["Real constant out of range: ",s,"\n"])
93 :    
94 : monnier 202 fun check (checkE,printE,chkId) (lvl,logId) e =
95 :     if checkE (e,lvl) then
96 :     (dumpTerm (printE, src ^ "." ^ chkId ^ logId, e);
97 :     bug (chkId ^ " typing errors " ^ logId))
98 :     else ()
99 : monnier 197 fun wff (f, s) = if wformed f then ()
100 :     else print ("\nAfter " ^ s ^ " CODE NOT WELL FORMED\n")
101 :    
102 : monnier 220 (* f:prog flint code
103 :     * fi:prog opt inlinable approximation of f
104 : monnier 213 * fk:flintkind what kind of flint variant this is
105 : monnier 162 * l:string last phase through which it went *)
106 : monnier 220 fun runphase (p,(f,fi,fk,l)) =
107 : monnier 197 case (p,fk)
108 :     of (("fcontract" | "lcontract"), FK_DEBRUIJN) =>
109 :     (say("\n!! "^p^" cannot be applied to the DeBruijn form !!\n");
110 : monnier 220 (f, fi, fk, l))
111 : monnier 122
112 : monnier 220 | ("fcontract",_) => (fcontract f, fi, fk, p)
113 :     | ("lcontract",_) => (lcontract f, fi, fk, p)
114 :     | ("fixfix", _) => (fixfix f, fi, fk, p)
115 :     | ("loopify", _) => (loopify f, fi, fk, p)
116 :     | ("specialize",FK_NAMED) => (specialize f, fi, fk, p)
117 :     | ("wrap",FK_NAMED) => (wrapping f, fi, FK_WRAP, p)
118 :     | ("reify",FK_WRAP) => (reify f, fi, FK_REIFY, p)
119 :     | ("deb2names",FK_DEBRUIJN) => (deb2names f, fi, FK_NAMED, p)
120 :     | ("names2deb",FK_NAMED) => (names2deb f, fi, FK_DEBRUIJN, p)
121 : monnier 198 | ("typelift", _) =>
122 : monnier 220 let val f = typelift f
123 :     in if !CTRL.check then wff(f, p) else (); (f, fi, fk, p) end
124 :     | ("split", FK_NAMED) =>
125 :     let val (f,fi) = split f in (f, fi, fk, p) end
126 : monnier 16
127 : monnier 197 (* pseudo FLINT phases *)
128 : monnier 220 | ("pickle", _) =>
129 :     (valOf(UnpickMod.unpickleFLINT(PickMod.pickleFLINT(SOME f))),
130 :     UnpickMod.unpickleFLINT(PickMod.pickleFLINT fi),
131 :     fk, p)
132 :     | ("collect",_) => (fcollect f, fi, fk, p)
133 : monnier 197 | _ =>
134 : monnier 220 ((case (p,fk)
135 :     of ("id",_) => ()
136 :     | ("wellformed",_) => wff(f,l)
137 :     | ("recover",_) =>
138 :     let val {getLty,...} = recover(f, fk = FK_REIFY)
139 :     in CTRL.recover := (say o LT.lt_print o getLty o F.VAR)
140 :     end
141 :     | ("print",_) =>
142 :     (say("\n[After "^l^"...]\n\n"); PP.printFundec f; say "\n")
143 :     | ("printsplit", _) =>
144 :     (say "[ splitted ]\n\n"; O.map PP.printFundec fi; say "\n")
145 :     | ("check",_) =>
146 :     (check (ChkFlint.checkTop, PPFlint.printFundec, "FLINT")
147 :     (fk = FK_REIFY, l) f)
148 :     | _ =>
149 :     say("\n!! Unknown or badly scheduled FLINT phase '"^p^"' !!\n"));
150 :     (f, fi, fk, l))
151 : monnier 162
152 : monnier 220 fun print (f,fi,fk,l) = (prF l f; (f, fi, fk, l))
153 :     fun check' (f,fi,fk,l) =
154 :     let fun c n reified f =
155 :     check (ChkFlint.checkTop, PPFlint.printFundec, n)
156 :     (reified, l) (names2deb f)
157 :     in if !CTRL.check then
158 :     (c "FLINT" (fk = FK_REIFY) f; O.map (c "iFLINT" false) fi; ())
159 :     else ();
160 :     (f, fi, fk, l)
161 :     end
162 : monnier 197
163 : monnier 184 fun runphase' (arg as (p,{1=f,...})) =
164 : monnier 189 (if !CTRL.printPhases then say("Phase "^p^"...") else ();
165 : monnier 202 ((check' o print o runphase) arg) before
166 : monnier 189 (if !CTRL.printPhases then say("..."^p^" Done.\n") else ()))
167 :     handle x => (say ("\nwhile in "^p^" phase\n");
168 : monnier 185 dumpTerm(PPFlint.printFundec,"FLINT.core", f);
169 :     raise x)
170 : monnier 163
171 : monnier 220 val (flint,fi,fk,_) = foldl runphase'
172 :     (flint, NONE, FK_DEBRUIJN, "flintnm")
173 :     ((* "id" :: *) "deb2names" :: !CTRL.phases)
174 : monnier 162
175 : monnier 197 (* run any missing phases *)
176 :     val (flint,fk) =
177 : monnier 198 if fk = FK_DEBRUIJN
178 :     then (say "\n!!Forgot deb2names!!\n"; (deb2names flint, FK_NAMED))
179 : monnier 197 else (flint,fk)
180 :     val (flint,fk) =
181 : monnier 198 if fk = FK_NAMED
182 : monnier 197 then (say "\n!!Forgot wrap!!\n"; (wrapping flint, FK_WRAP))
183 :     else (flint,fk)
184 :     val (flint,fk) =
185 :     if fk = FK_WRAP
186 :     then (say "\n!!Forgot reify!!\n"; (reify flint, FK_REIFY))
187 :     else (flint,fk)
188 : monnier 162
189 : monnier 197 (* finish up with CPS *)
190 : monnier 102 val (nc0, ncn, dseg) =
191 :     let val function = convert flint
192 :     val _ = prC "convert" function
193 : monnier 71 val function = (prC "cpstrans" o cpstrans) function
194 : monnier 162 val function = cpsopt (function,NONE,false)
195 : monnier 71 val _ = prC "cpsopt" function
196 : monnier 45
197 : monnier 102 val (function, dlit) = litsplit function
198 : monnier 251 val data = litToBytes dlit
199 : monnier 102 val _ = prC "cpsopt-code" function
200 :    
201 : monnier 251 (** NOTE: we should be passing the source-code name (src) to the
202 :     ** code generator somehow (for the second argument to code object allocation).
203 :     **)
204 : monnier 71 fun gen fx =
205 :     let val fx = (prC "closure" o closure) fx
206 :     val carg = globalfix fx
207 :     val carg = spill carg
208 :     val (carg, limit) = limit carg
209 :     in codegen (carg, limit, err);
210 :     collect ()
211 :     end
212 : monnier 102
213 : monnier 71 in case CpsSplit.cpsSplit function
214 : monnier 251 of (fun0 :: funn) => (gen fun0, map gen funn, data)
215 : monnier 71 | [] => bug "unexpected case on gen in flintcomp"
216 :     end
217 : monnier 251 in ({c0=nc0, cn=ncn, data=dseg}, fi)
218 : monnier 16 end (* function flintcomp *)
219 :    
220 : monnier 71 val flintcomp = phase "Compiler 050 flintcomp" flintcomp
221 : monnier 16
222 :     end (* local *)
223 :     end (* structure FLINTComp *)
224 : monnier 95
225 :     (*
226 : monnier 251 * $Log: flintcomp.sml,v $
227 :     * Revision 1.8 1999/01/11 16:53:25 george
228 :     * new array representation support
229 :     *
230 : monnier 95 *)

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