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 213 - (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 202 val deb2names = phase "Compiler 056 deb2names" TvarCvt.debIndex2names
29 :     val names2deb = phase "Compiler 057 names2deb" TvarCvt.names2debIndex
30 :    
31 :     val lcontract = phase "Compiler 052 lcontract" LContract.lcontract
32 :     val lcontract' = phase "Compiler 052 lcontract'" LContract.lcontract
33 : monnier 164 val fcollect = phase "Compiler 052a fcollect" Collect.collect
34 :     val fcontract = phase "Compiler 052b fcontract" FContract.contract
35 : monnier 213 val fcontract = fcontract o fcollect
36 : monnier 191 val loopify = phase "Compiler 057 loopify" Loopify.loopify
37 : monnier 197 val fixfix = phase "Compiler 056 fixfix" FixFix.fixfix
38 : monnier 191
39 : monnier 197 val typelift = phase "Compiler 0535 typelift" Lift.typeLift
40 :     val wformed = phase "Compiler 0536 wformed" Lift.wellFormed
41 :    
42 : monnier 71 val specialize= phase "Compiler 053 specialize" Specialize.specialize
43 :     val wrapping = phase "Compiler 054 wrapping" Wrapping.wrapping
44 :     val reify = phase "Compiler 055 reify" Reify.reify
45 : monnier 16
46 : monnier 71 val convert = phase "Compiler 060 convert" Convert.convert
47 :     val cpstrans = phase "Compiler 065 cpstrans" CPStrans.cpstrans
48 : monnier 16 val cpsopt = phase "Compiler 070 cpsopt" CPSopt.reduce
49 : monnier 102 val litsplit = phase "Compiler 075 litsplit" Literals.litsplit
50 :     val lit2cps = phase "Compiler 076 lit2cps" Literals.lit2cps
51 :     val closure = phase "Compiler 080 closure" Closure.closeCPS
52 : monnier 16 val globalfix = phase "Compiler 090 globalfix" GlobalFix.globalfix
53 :     val spill = if MachSpec.spillAreaSz < 500 * MachSpec.valueSize
54 :     then phase "Compiler 100 spill" Spill.spill
55 :     else fn x => x
56 :     val limit = phase "Compiler 110 limit" Limit.nolimit
57 :     val codegen = phase "Compiler 120 cpsgen" Gen.codegen
58 :    
59 : monnier 102 val closureD = phase "Compiler 081 closureD" Closure.closeCPS
60 :     val globalfixD= phase "Compiler 091 globalfixD" GlobalFix.globalfix
61 :     val spillD = if MachSpec.spillAreaSz < 500 * MachSpec.valueSize
62 :     then phase "Compiler 101 spillD" Spill.spill
63 :     else fn x => x
64 :     val limitD = phase "Compiler 110 limitD" Limit.nolimit
65 :     val codegenD = phase "Compiler 121 cpsgenD" Gen.codegen
66 : monnier 16
67 : monnier 71 (** pretty printing for the FLINT and CPS code *)
68 :     val (prF, prC) =
69 :     let fun prGen (flag,printE) s e =
70 : monnier 102 if !flag then (say ("\n[After " ^ s ^ " ...]\n\n"); printE e;
71 :     say "\n"; e)
72 : monnier 71 else e
73 : monnier 162 in (prGen (CTRL.print, PPFlint.printProg),
74 : monnier 122 prGen (Control.CG.printit, PPCps.printcps0))
75 : monnier 71 end
76 : monnier 16
77 : monnier 71 (** writing out a term into a error output file *)
78 :     fun dumpTerm (printE, s, le) =
79 :     let val outS = TextIO.openAppend s;
80 :     val saveOut = !Control.Print.out
81 :     fun done () =
82 :     (TextIO.closeOut outS; Control.Print.out := saveOut)
83 :     in Control.Print.out := {say = fn s => TextIO.output(outS,s),
84 :     flush = fn () => TextIO.flushOut outS};
85 :     printE le handle x => (done () handle _ => (); raise x);
86 :     done ()
87 :     end (* function dumpTerm *)
88 :    
89 : monnier 189 val fcs : (FLINT.prog -> FLINT.prog) list ref = ref []
90 :    
91 : monnier 16 (** compiling FLINT code into the binary machine code *)
92 :     fun flintcomp(flint, compInfo as {error, sourceName=src, ...}: CB.compInfo) =
93 :     let fun err severity s =
94 :     error (0,0) severity (concat["Real constant out of range: ",s,"\n"])
95 :    
96 : monnier 202 fun check (checkE,printE,chkId) (lvl,logId) e =
97 :     if checkE (e,lvl) then
98 :     (dumpTerm (printE, src ^ "." ^ chkId ^ logId, e);
99 :     bug (chkId ^ " typing errors " ^ logId))
100 :     else ()
101 : monnier 197 fun wff (f, s) = if wformed f then ()
102 :     else print ("\nAfter " ^ s ^ " CODE NOT WELL FORMED\n")
103 :    
104 : monnier 213 (* f:FLINT.prog flint code
105 :     * fk:flintkind what kind of flint variant this is
106 : monnier 162 * l:string last phase through which it went *)
107 : monnier 197 fun runphase (p,(f,fk,l)) =
108 :     case (p,fk)
109 :     of (("fcontract" | "lcontract"), FK_DEBRUIJN) =>
110 :     (say("\n!! "^p^" cannot be applied to the DeBruijn form !!\n");
111 :     (f, fk, l))
112 : monnier 122
113 : monnier 197 | ("fcontract",_) => (fcontract f, fk, p)
114 :     | ("lcontract",_) => (lcontract f, fk, p)
115 :     | ("fixfix", _) => (fixfix f, fk, p)
116 :     | ("loopify", _) => (loopify f, fk, p)
117 :     | ("specialize",FK_NAMED) => (specialize f, fk, p)
118 :     | ("wrap",FK_NAMED) => (wrapping f, FK_WRAP, p)
119 :     | ("reify",FK_WRAP) => (reify f, FK_REIFY, p)
120 :     | ("deb2names",FK_DEBRUIJN) => (deb2names f, FK_NAMED, p)
121 :     | ("names2deb",FK_NAMED) => (names2deb f, FK_DEBRUIJN, p)
122 : monnier 198 | ("typelift", _) =>
123 :     let val f' = typelift f
124 :     in if !CTRL.check then wff(f', p) else (); (f', fk, p) end
125 : monnier 16
126 : monnier 197 (* pseudo FLINT phases *)
127 :     | ("id",_) => (f,fk,l)
128 :     | ("collect",_) => (fcollect f, fk, p)
129 :     | ("print",_) =>
130 :     (say("\n\n[ After "^l^"... ]\n\n");
131 :     PPFlint.printFundec f;
132 :     (f, fk, l) before say "\n")
133 : monnier 198 | ("wellformed",_) => (wff(f,l); (f,fk,p))
134 : monnier 197 | ("check",_) =>
135 :     (check (ChkFlint.checkTop, PPFlint.printFundec, "FLINT")
136 : monnier 202 (fk = FK_REIFY, l) f; (f,fk,l))
137 : monnier 197 | _ =>
138 :     (say("\n!! Unknown or badly scheduled FLINT phase '"^p^"' !!\n");
139 :     (f,fk,l))
140 : monnier 162
141 : monnier 197 fun print (f,fk,l) = (prF l f; (f, fk, l))
142 : monnier 202 fun check' (f,fk,l) =
143 :     (if !CTRL.check then
144 :     check (ChkFlint.checkTop, PPFlint.printFundec, "FLINT")
145 :     (fk = FK_REIFY, l)
146 :     (if fk = FK_DEBRUIJN then f else names2deb f)
147 :     else ();
148 : monnier 197 (f, fk, l))
149 :    
150 : monnier 184 fun runphase' (arg as (p,{1=f,...})) =
151 : monnier 189 (if !CTRL.printPhases then say("Phase "^p^"...") else ();
152 : monnier 202 ((check' o print o runphase) arg) before
153 : monnier 189 (if !CTRL.printPhases then say("..."^p^" Done.\n") else ()))
154 :     handle x => (say ("\nwhile in "^p^" phase\n");
155 : monnier 185 dumpTerm(PPFlint.printFundec,"FLINT.core", f);
156 :     raise x)
157 : monnier 163
158 : monnier 197 val (flint,fk,_) = foldl runphase'
159 : monnier 202 (flint, FK_DEBRUIJN, "flintnm")
160 :     ((* "id" :: *) "deb2names" :: !CTRL.phases)
161 : monnier 162
162 : monnier 197 (* run any missing phases *)
163 :     val (flint,fk) =
164 : monnier 198 if fk = FK_DEBRUIJN
165 :     then (say "\n!!Forgot deb2names!!\n"; (deb2names flint, FK_NAMED))
166 : monnier 197 else (flint,fk)
167 :     val (flint,fk) =
168 : monnier 198 if fk = FK_NAMED
169 : monnier 197 then (say "\n!!Forgot wrap!!\n"; (wrapping flint, FK_WRAP))
170 :     else (flint,fk)
171 :     val (flint,fk) =
172 :     if fk = FK_WRAP
173 :     then (say "\n!!Forgot reify!!\n"; (reify flint, FK_REIFY))
174 :     else (flint,fk)
175 : monnier 162
176 : monnier 197 (* finish up with CPS *)
177 : monnier 102 val (nc0, ncn, dseg) =
178 :     let val function = convert flint
179 :     val _ = prC "convert" function
180 : monnier 71 val function = (prC "cpstrans" o cpstrans) function
181 : monnier 162 val function = cpsopt (function,NONE,false)
182 : monnier 71 val _ = prC "cpsopt" function
183 : monnier 45
184 : monnier 102 val (function, dlit) = litsplit function
185 :     val data = lit2cps dlit
186 :     val _ = prC "cpsopt-code" function
187 :     val _ = prC "cpsopt-data" data
188 :    
189 : monnier 71 fun gen fx =
190 :     let val fx = (prC "closure" o closure) fx
191 :     val carg = globalfix fx
192 :     val carg = spill carg
193 :     val (carg, limit) = limit carg
194 :     in codegen (carg, limit, err);
195 :     collect ()
196 :     end
197 : monnier 102
198 :     fun gdata dd =
199 :     let val x = Control.CG.printit
200 :     val y = !x
201 :     val _ = (x := false)
202 :     val fx = (prC "closure" o closureD) dd
203 :     val carg = globalfixD fx
204 :     val carg = spillD carg
205 :     val (carg, limit) = limitD carg
206 :     in codegenD (carg, limit, err);
207 :     (collect ()) before (x := y)
208 :     end
209 : monnier 71 in case CpsSplit.cpsSplit function
210 : monnier 102 of (fun0 :: funn) => (gen fun0, map gen funn, gdata data)
211 : monnier 71 | [] => bug "unexpected case on gen in flintcomp"
212 :     end
213 : monnier 102 in {c0=nc0, cn=ncn, data=dseg, name=ref (SOME src)}
214 : monnier 16 end (* function flintcomp *)
215 :    
216 : monnier 71 val flintcomp = phase "Compiler 050 flintcomp" flintcomp
217 : monnier 16
218 :     end (* local *)
219 :     end (* structure FLINTComp *)
220 : monnier 95
221 :     (*
222 : monnier 118 * $Log$
223 : monnier 95 *)

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