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 1128 - (view) (download)

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

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