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 197 - (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 :     | ("typelift",FK_DEBRUIJN) => (typelift f, fk, p)
145 :     | ("wrap",FK_NAMED) => (wrapping f, FK_WRAP, p)
146 :     | ("reify",FK_WRAP) => (reify f, FK_REIFY, p)
147 :     | ("deb2names",FK_DEBRUIJN) => (deb2names f, FK_NAMED, p)
148 :     | ("names2deb",FK_NAMED) => (names2deb f, FK_DEBRUIJN, p)
149 : monnier 16
150 : monnier 197 (* pseudo FLINT phases *)
151 :     | ("id",_) => (f,fk,l)
152 :     | ("collect",_) => (fcollect f, fk, p)
153 :     | ("print",_) =>
154 :     (say("\n\n[ After "^l^"... ]\n\n");
155 :     PPFlint.printFundec f;
156 :     (f, fk, l) before say "\n")
157 :     | ("wellformed",FK_DEBRUIJN) => (wff(f,l); (f,fk,p))
158 :     | ("check",_) =>
159 :     (check (ChkFlint.checkTop, PPFlint.printFundec, "FLINT")
160 :     (ref true, fk = FK_REIFY, l) f; (f,fk,l))
161 :     | _ =>
162 :     (say("\n!! Unknown or badly scheduled FLINT phase '"^p^"' !!\n");
163 :     (f,fk,l))
164 : monnier 162
165 : monnier 197 fun print (f,fk,l) = (prF l f; (f, fk, l))
166 :     fun check (f,fk,l) =
167 :     ((* if fk <> FK_NAMED *) chkF (fk = FK_REIFY, l) (names2deb f) (* else f *);
168 :     (f, fk, l))
169 :    
170 : monnier 184 fun runphase' (arg as (p,{1=f,...})) =
171 : monnier 189 (if !CTRL.printPhases then say("Phase "^p^"...") else ();
172 :     ((check o print o runphase) arg) before
173 :     (if !CTRL.printPhases then say("..."^p^" Done.\n") else ()))
174 :     handle x => (say ("\nwhile in "^p^" phase\n");
175 : monnier 185 dumpTerm(PPFlint.printFundec,"FLINT.core", f);
176 :     raise x)
177 : monnier 163
178 : monnier 191 (* the "id" phase is just added to do the print/check at the entrance *)
179 : monnier 197 val (flint,fk,_) = foldl runphase'
180 :     (flint, FK_DEBRUIJN, "flintnm")
181 :     ((* "id" :: *) !CTRL.phases)
182 : monnier 162
183 : monnier 197 (* run any missing phases *)
184 :     val (flint,fk) =
185 :     if fk = FK_NAMED
186 :     then (say "\n!!Forgot deb2names!!\n"; (deb2names flint, FK_DEBRUIJN))
187 :     else (flint,fk)
188 :     val (flint,fk) =
189 :     if fk = FK_DEBRUIJN
190 :     then (say "\n!!Forgot wrap!!\n"; (wrapping flint, FK_WRAP))
191 :     else (flint,fk)
192 :     val (flint,fk) =
193 :     if fk = FK_WRAP
194 :     then (say "\n!!Forgot reify!!\n"; (reify flint, FK_REIFY))
195 :     else (flint,fk)
196 : monnier 162
197 : monnier 197 (* finish up with CPS *)
198 : monnier 102 val (nc0, ncn, dseg) =
199 :     let val function = convert flint
200 :     val _ = prC "convert" function
201 : monnier 71 val function = (prC "cpstrans" o cpstrans) function
202 : monnier 162 val function = cpsopt (function,NONE,false)
203 : monnier 71 val _ = prC "cpsopt" function
204 : monnier 45
205 : monnier 102 val (function, dlit) = litsplit function
206 :     val data = lit2cps dlit
207 :     val _ = prC "cpsopt-code" function
208 :     val _ = prC "cpsopt-data" data
209 :    
210 : monnier 71 fun gen fx =
211 :     let val fx = (prC "closure" o closure) fx
212 :     val carg = globalfix fx
213 :     val carg = spill carg
214 :     val (carg, limit) = limit carg
215 :     in codegen (carg, limit, err);
216 :     collect ()
217 :     end
218 : monnier 102
219 :     fun gdata dd =
220 :     let val x = Control.CG.printit
221 :     val y = !x
222 :     val _ = (x := false)
223 :     val fx = (prC "closure" o closureD) dd
224 :     val carg = globalfixD fx
225 :     val carg = spillD carg
226 :     val (carg, limit) = limitD carg
227 :     in codegenD (carg, limit, err);
228 :     (collect ()) before (x := y)
229 :     end
230 : monnier 71 in case CpsSplit.cpsSplit function
231 : monnier 102 of (fun0 :: funn) => (gen fun0, map gen funn, gdata data)
232 : monnier 71 | [] => bug "unexpected case on gen in flintcomp"
233 :     end
234 : monnier 102 in {c0=nc0, cn=ncn, data=dseg, name=ref (SOME src)}
235 : monnier 16 end (* function flintcomp *)
236 :    
237 : monnier 71 val flintcomp = phase "Compiler 050 flintcomp" flintcomp
238 : monnier 16
239 :     end (* local *)
240 :     end (* structure FLINTComp *)
241 : monnier 95
242 :     (*
243 : monnier 118 * $Log$
244 : monnier 95 *)

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