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

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