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

1 : monnier 16 (* COPYRIGHT (c) 1998 YALE FLINT PROJECT *)
2 :     (* flintcomp.sml *)
3 :    
4 :     functor FLINTComp (structure Gen: CPSGEN
5 :     val collect: unit -> Word8Vector.vector) : CODEGENERATOR =
6 :     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 16 structure Spill = Spill(MachSpec)
16 :     structure CpsSplit = CpsSplitFun (MachSpec)
17 : monnier 162 structure CTRL = Control.FLINT
18 : monnier 16 in
19 :    
20 :     val architecture = Gen.MachSpec.architecture
21 :     fun bug s = ErrorMsg.impossible ("FLINTComp:" ^ s)
22 :     val say = Control.Print.say
23 :    
24 : monnier 197 datatype flintkind = FK_WRAP | FK_REIFY | FK_DEBRUIJN | FK_NAMED | FK_CPS
25 :    
26 : monnier 16 fun phase x = Stats.doPhase (Stats.makePhase x)
27 :    
28 : monnier 184 val lcontract = phase "Compiler 052 lcontract" LContract.lcontract
29 : monnier 164 val fcollect = phase "Compiler 052a fcollect" Collect.collect
30 :     val fcontract = phase "Compiler 052b fcontract" FContract.contract
31 : monnier 191 val fcontract = fn f => (fcontract(fcollect f, Stats.newCounter[]))
32 :     val loopify = phase "Compiler 057 loopify" Loopify.loopify
33 : monnier 197 val fixfix = phase "Compiler 056 fixfix" FixFix.fixfix
34 : monnier 191
35 : monnier 197 val typelift = phase "Compiler 0535 typelift" Lift.typeLift
36 :     val wformed = phase "Compiler 0536 wformed" Lift.wellFormed
37 :    
38 : monnier 71 val specialize= phase "Compiler 053 specialize" Specialize.specialize
39 :     val wrapping = phase "Compiler 054 wrapping" Wrapping.wrapping
40 :     val reify = phase "Compiler 055 reify" Reify.reify
41 : monnier 16
42 : monnier 197 val deb2names = phase "Compiler 056 deb2names" TvarCvt.debIndex2names
43 :     val names2deb = phase "Compiler 057 names2deb" TvarCvt.names2debIndex
44 :    
45 : monnier 71 val convert = phase "Compiler 060 convert" Convert.convert
46 :     val cpstrans = phase "Compiler 065 cpstrans" CPStrans.cpstrans
47 : monnier 16 val cpsopt = phase "Compiler 070 cpsopt" CPSopt.reduce
48 : monnier 102 val litsplit = phase "Compiler 075 litsplit" Literals.litsplit
49 :     val lit2cps = phase "Compiler 076 lit2cps" Literals.lit2cps
50 :     val closure = phase "Compiler 080 closure" Closure.closeCPS
51 : monnier 16 val globalfix = phase "Compiler 090 globalfix" GlobalFix.globalfix
52 :     val spill = if MachSpec.spillAreaSz < 500 * MachSpec.valueSize
53 :     then phase "Compiler 100 spill" Spill.spill
54 :     else fn x => x
55 :     val limit = phase "Compiler 110 limit" Limit.nolimit
56 :     val codegen = phase "Compiler 120 cpsgen" Gen.codegen
57 :    
58 : monnier 102 val closureD = phase "Compiler 081 closureD" Closure.closeCPS
59 :     val globalfixD= phase "Compiler 091 globalfixD" GlobalFix.globalfix
60 :     val spillD = if MachSpec.spillAreaSz < 500 * MachSpec.valueSize
61 :     then phase "Compiler 101 spillD" Spill.spill
62 :     else fn x => x
63 :     val limitD = phase "Compiler 110 limitD" Limit.nolimit
64 :     val codegenD = phase "Compiler 121 cpsgenD" 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 :     fun flintcomp(flint, compInfo as {error, sourceName=src, ...}: CB.compInfo) =
92 :     let fun err severity s =
93 :     error (0,0) severity (concat["Real constant out of range: ",s,"\n"])
94 :    
95 :     fun check (checkE,printE,chkId) (enableChk,lvl,logId) e =
96 :     (if !enableChk andalso checkE (e,lvl) then
97 : monnier 71 (dumpTerm (printE, src ^ "." ^ chkId ^ logId, e);
98 :     bug (chkId ^ " typing errors " ^ logId))
99 : monnier 16 else ();
100 :     e)
101 : monnier 71 fun chkF (b, s) =
102 :     check (ChkFlint.checkTop, PPFlint.printFundec,
103 : monnier 162 "FLINT") (CTRL.check, b, s)
104 : monnier 16
105 : monnier 197 fun wff (f, s) = if wformed f then ()
106 :     else print ("\nAfter " ^ s ^ " CODE NOT WELL FORMED\n")
107 :    
108 :     (* val fcing = ref (!fcs)
109 :     fun fcontract f =
110 : monnier 189 case !fcing
111 :     of fcontract::fcs => (fcing := fcs; fcontract f)
112 :     | [] => let val fcc = Stats.newCounter[]
113 :     val fcname = "FContract-"^(Int.toString(length(!fcs)))
114 :     val coname = "FCollect-"^(Int.toString(length(!fcs)))
115 :     val lcname = "LContract-"^(Int.toString(length(!fcs)))
116 :     val fcstat = Stats.newStat(fcname, [fcc])
117 :     val fcphase = phase ("Compiler 052b "^fcname)
118 :     FContract.contract
119 :     val cophase = phase ("Compiler 052a "^coname)
120 :     Collect.collect
121 :     val lcphase = phase ("Compiler 052 "^lcname)
122 :     LContract.lcontract
123 :     fun g c = (lcphase c; fcphase(cophase c,fcc))
124 :     in
125 :     Stats.registerStat fcstat;
126 :     fcs := (!fcs) @ [g];
127 :     g f
128 :     end *)
129 :    
130 : monnier 162 (* f:FLINT.prog flint codee
131 :     * r:boot whether it has gone through reify yet
132 :     * l:string last phase through which it went *)
133 : monnier 197 fun runphase (p,(f,fk,l)) =
134 :     case (p,fk)
135 :     of (("fcontract" | "lcontract"), FK_DEBRUIJN) =>
136 :     (say("\n!! "^p^" cannot be applied to the DeBruijn form !!\n");
137 :     (f, fk, l))
138 : monnier 122
139 : monnier 197 | ("fcontract",_) => (fcontract f, fk, p)
140 :     | ("lcontract",_) => (lcontract f, fk, p)
141 :     | ("fixfix", _) => (fixfix f, fk, p)
142 :     | ("loopify", _) => (loopify f, fk, p)
143 :     | ("specialize",FK_NAMED) => (specialize f, fk, p)
144 :     | ("wrap",FK_NAMED) => (wrapping f, FK_WRAP, p)
145 :     | ("reify",FK_WRAP) => (reify f, FK_REIFY, p)
146 :     | ("deb2names",FK_DEBRUIJN) => (deb2names f, FK_NAMED, p)
147 :     | ("names2deb",FK_NAMED) => (names2deb f, FK_DEBRUIJN, p)
148 : monnier 198 | ("typelift", _) =>
149 :     let val f' = typelift f
150 :     in if !CTRL.check then wff(f', p) else (); (f', fk, p) end
151 : monnier 16
152 : monnier 197 (* pseudo FLINT phases *)
153 :     | ("id",_) => (f,fk,l)
154 :     | ("collect",_) => (fcollect f, fk, p)
155 :     | ("print",_) =>
156 :     (say("\n\n[ After "^l^"... ]\n\n");
157 :     PPFlint.printFundec f;
158 :     (f, fk, l) before say "\n")
159 : monnier 198 | ("wellformed",_) => (wff(f,l); (f,fk,p))
160 : monnier 197 | ("check",_) =>
161 :     (check (ChkFlint.checkTop, PPFlint.printFundec, "FLINT")
162 :     (ref true, fk = FK_REIFY, l) f; (f,fk,l))
163 :     | _ =>
164 :     (say("\n!! Unknown or badly scheduled FLINT phase '"^p^"' !!\n");
165 :     (f,fk,l))
166 : monnier 162
167 : monnier 197 fun print (f,fk,l) = (prF l f; (f, fk, l))
168 :     fun check (f,fk,l) =
169 :     ((* if fk <> FK_NAMED *) chkF (fk = FK_REIFY, l) (names2deb f) (* else f *);
170 :     (f, fk, l))
171 :    
172 : monnier 184 fun runphase' (arg as (p,{1=f,...})) =
173 : monnier 189 (if !CTRL.printPhases then say("Phase "^p^"...") else ();
174 :     ((check o print o runphase) arg) before
175 :     (if !CTRL.printPhases then say("..."^p^" Done.\n") else ()))
176 :     handle x => (say ("\nwhile in "^p^" phase\n");
177 : monnier 185 dumpTerm(PPFlint.printFundec,"FLINT.core", f);
178 :     raise x)
179 : monnier 163
180 : monnier 191 (* the "id" phase is just added to do the print/check at the entrance *)
181 : monnier 197 val (flint,fk,_) = foldl runphase'
182 : monnier 198 (deb2names flint, FK_NAMED, "flintnm")
183 : monnier 197 ((* "id" :: *) !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 :     val data = lit2cps dlit
209 :     val _ = prC "cpsopt-code" function
210 :     val _ = prC "cpsopt-data" data
211 :    
212 : monnier 71 fun gen fx =
213 :     let val fx = (prC "closure" o closure) fx
214 :     val carg = globalfix fx
215 :     val carg = spill carg
216 :     val (carg, limit) = limit carg
217 :     in codegen (carg, limit, err);
218 :     collect ()
219 :     end
220 : monnier 102
221 :     fun gdata dd =
222 :     let val x = Control.CG.printit
223 :     val y = !x
224 :     val _ = (x := false)
225 :     val fx = (prC "closure" o closureD) dd
226 :     val carg = globalfixD fx
227 :     val carg = spillD carg
228 :     val (carg, limit) = limitD carg
229 :     in codegenD (carg, limit, err);
230 :     (collect ()) before (x := y)
231 :     end
232 : monnier 71 in case CpsSplit.cpsSplit function
233 : monnier 102 of (fun0 :: funn) => (gen fun0, map gen funn, gdata data)
234 : monnier 71 | [] => bug "unexpected case on gen in flintcomp"
235 :     end
236 : monnier 102 in {c0=nc0, cn=ncn, data=dseg, name=ref (SOME src)}
237 : monnier 16 end (* function flintcomp *)
238 :    
239 : monnier 71 val flintcomp = phase "Compiler 050 flintcomp" flintcomp
240 : monnier 16
241 :     end (* local *)
242 :     end (* structure FLINTComp *)
243 : monnier 95
244 :     (*
245 : monnier 118 * $Log$
246 : monnier 95 *)

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