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 163 - (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 :     fun phase x = Stats.doPhase (Stats.makePhase x)
25 :    
26 : monnier 163 (* val lcontract = phase "Compiler 052 lcontract" LContract.lcontract *)
27 : monnier 122 val fcontract = phase "Compiler 052 fcontract" FContract.contract
28 : monnier 71 val specialize= phase "Compiler 053 specialize" Specialize.specialize
29 :     val wrapping = phase "Compiler 054 wrapping" Wrapping.wrapping
30 :     val reify = phase "Compiler 055 reify" Reify.reify
31 : monnier 122 val fixfix = phase "Compiler 056 fixfix" FixFix.fixfix
32 : monnier 16
33 : monnier 71 val convert = phase "Compiler 060 convert" Convert.convert
34 :     val cpstrans = phase "Compiler 065 cpstrans" CPStrans.cpstrans
35 : monnier 16 val cpsopt = phase "Compiler 070 cpsopt" CPSopt.reduce
36 : monnier 102 val litsplit = phase "Compiler 075 litsplit" Literals.litsplit
37 :     val lit2cps = phase "Compiler 076 lit2cps" Literals.lit2cps
38 :     val closure = phase "Compiler 080 closure" Closure.closeCPS
39 : monnier 16 val globalfix = phase "Compiler 090 globalfix" GlobalFix.globalfix
40 :     val spill = if MachSpec.spillAreaSz < 500 * MachSpec.valueSize
41 :     then phase "Compiler 100 spill" Spill.spill
42 :     else fn x => x
43 :     val limit = phase "Compiler 110 limit" Limit.nolimit
44 :     val codegen = phase "Compiler 120 cpsgen" Gen.codegen
45 :    
46 : monnier 102 val closureD = phase "Compiler 081 closureD" Closure.closeCPS
47 :     val globalfixD= phase "Compiler 091 globalfixD" GlobalFix.globalfix
48 :     val spillD = if MachSpec.spillAreaSz < 500 * MachSpec.valueSize
49 :     then phase "Compiler 101 spillD" Spill.spill
50 :     else fn x => x
51 :     val limitD = phase "Compiler 110 limitD" Limit.nolimit
52 :     val codegenD = phase "Compiler 121 cpsgenD" Gen.codegen
53 : monnier 16
54 : monnier 71 (** pretty printing for the FLINT and CPS code *)
55 :     val (prF, prC) =
56 :     let fun prGen (flag,printE) s e =
57 : monnier 102 if !flag then (say ("\n[After " ^ s ^ " ...]\n\n"); printE e;
58 :     say "\n"; e)
59 : monnier 71 else e
60 : monnier 162 in (prGen (CTRL.print, PPFlint.printProg),
61 : monnier 122 prGen (Control.CG.printit, PPCps.printcps0))
62 : monnier 71 end
63 : monnier 16
64 : monnier 71 (** writing out a term into a error output file *)
65 :     fun dumpTerm (printE, s, le) =
66 :     let val outS = TextIO.openAppend s;
67 :     val saveOut = !Control.Print.out
68 :     fun done () =
69 :     (TextIO.closeOut outS; Control.Print.out := saveOut)
70 :     in Control.Print.out := {say = fn s => TextIO.output(outS,s),
71 :     flush = fn () => TextIO.flushOut outS};
72 :     printE le handle x => (done () handle _ => (); raise x);
73 :     done ()
74 :     end (* function dumpTerm *)
75 :    
76 : monnier 16 (** compiling FLINT code into the binary machine code *)
77 :     fun flintcomp(flint, compInfo as {error, sourceName=src, ...}: CB.compInfo) =
78 :     let fun err severity s =
79 :     error (0,0) severity (concat["Real constant out of range: ",s,"\n"])
80 :    
81 :     fun check (checkE,printE,chkId) (enableChk,lvl,logId) e =
82 :     (if !enableChk andalso checkE (e,lvl) then
83 : monnier 71 (dumpTerm (printE, src ^ "." ^ chkId ^ logId, e);
84 :     bug (chkId ^ " typing errors " ^ logId))
85 : monnier 16 else ();
86 :     e)
87 : monnier 71 fun chkF (b, s) =
88 :     check (ChkFlint.checkTop, PPFlint.printFundec,
89 : monnier 162 "FLINT") (CTRL.check, b, s)
90 : monnier 16
91 : monnier 162 (* f:FLINT.prog flint codee
92 :     * r:boot whether it has gone through reify yet
93 :     * l:string last phase through which it went *)
94 :     fun runphase (p as "fcontract",(f,r,l)) = (fcontract f, r, p)
95 : monnier 163 (* | runphase (p as "lcontract",(f,r,l)) = (lcontract f, r, p) *)
96 : monnier 162 | runphase (p as "fixfix",(f,r,l)) = (fixfix f, r, p)
97 :     | runphase (p as "wrap",(f,false,l)) = (wrapping f, false, p)
98 :     | runphase (p as "specialize",(f,false,l)) = (specialize f, false, p)
99 :     | runphase (p as "reify",(f,false,l)) = (reify f, true, p)
100 : monnier 122
101 : monnier 162 (* pseudo FLINT phases *)
102 :     | runphase ("id",(f,r,l)) = (f,r,l)
103 :     | runphase (p as "print",(f,r,l)) =
104 : monnier 163 (say("\n[ After "^l^"... ]\n\n");
105 :     PPFlint.printFundec f; (f,r,l)
106 :     before say "\n")
107 : monnier 162 | runphase ("check",(f,r,l)) =
108 :     (check (ChkFlint.checkTop, PPFlint.printFundec, "FLINT")
109 :     (ref true, r, l) f; (f,r,l))
110 :     | runphase (p as ("reify"|"specialize"|"wrap"),(f,true,l)) =
111 :     (say("\n"^p^"cannot be used after reify!\n"); (f,true,l))
112 :     | runphase (p,(f,r,l)) =
113 :     (say("\n!! Unknown FLINT phase '"^p^"' !!\n"); (f,r,l))
114 : monnier 16
115 : monnier 162 fun print (f,r,l) = (prF l f; (f, r, l))
116 :     fun check (f,r,l) = (chkF (r, l) f; (f, r, l))
117 :    
118 : monnier 163 fun runphase' (arg as (_,{1=f,...})) = ((runphase arg)
119 :     handle x => (dumpTerm(PPFlint.printFundec,"FLINT.bug", f); raise x))
120 :    
121 : monnier 162 (* the "id" phases is just added to do the print/check at the entrance *)
122 : monnier 163 val (flint,r,_) = foldl (check o print o runphase')
123 :     (flint,false,"flintnm")
124 :     ("id" :: !CTRL.phases)
125 :     val flint = if r then flint else (say "\n!!Forgot reify!!\n"; reify flint)
126 : monnier 162
127 :     (* val _ = (chkF (false,"1") o prF "Translation/Normalization") flint *)
128 :     (* val flint = (chkF (false,"2") o prF "Fcontract" o fcontract) flint *)
129 :    
130 :     (* val flint = *)
131 :     (* if !Control.FLINT.specialize then *)
132 :     (* (chkF (false,"3") o prF "Specialization" o specialize) flint *)
133 :     (* else flint *)
134 :     (* val flint = (chkF (false,"2") o prF "Fcontract" o fcontract) flint *)
135 :    
136 : monnier 122 (* val flint = (chkF (false,"6") o prF "FixFix" o fixfix) flint *)
137 : monnier 162 (* val flint = (chkF (false,"2") o prF "Fcontract" o fcontract) flint *)
138 : monnier 122
139 : monnier 162 (* val flint = (chkF (false, "4") o prF "Wrapping" o wrapping) flint *)
140 :     (* val flint = (chkF (true, "5") o prF "Reify" o reify) flint *)
141 : monnier 102
142 : monnier 162 (* val flint = (chkF (true,"2") o prF "Fcontract" o fcontract) flint *)
143 : monnier 122
144 : monnier 102 val (nc0, ncn, dseg) =
145 :     let val function = convert flint
146 :     val _ = prC "convert" function
147 : monnier 71 val function = (prC "cpstrans" o cpstrans) function
148 : monnier 162 val function = cpsopt (function,NONE,false)
149 : monnier 71 val _ = prC "cpsopt" function
150 : monnier 45
151 : monnier 102 val (function, dlit) = litsplit function
152 :     val data = lit2cps dlit
153 :     val _ = prC "cpsopt-code" function
154 :     val _ = prC "cpsopt-data" data
155 :    
156 : monnier 71 fun gen fx =
157 :     let val fx = (prC "closure" o closure) fx
158 :     val carg = globalfix fx
159 :     val carg = spill carg
160 :     val (carg, limit) = limit carg
161 :     in codegen (carg, limit, err);
162 :     collect ()
163 :     end
164 : monnier 102
165 :     fun gdata dd =
166 :     let val x = Control.CG.printit
167 :     val y = !x
168 :     val _ = (x := false)
169 :     val fx = (prC "closure" o closureD) dd
170 :     val carg = globalfixD fx
171 :     val carg = spillD carg
172 :     val (carg, limit) = limitD carg
173 :     in codegenD (carg, limit, err);
174 :     (collect ()) before (x := y)
175 :     end
176 : monnier 71 in case CpsSplit.cpsSplit function
177 : monnier 102 of (fun0 :: funn) => (gen fun0, map gen funn, gdata data)
178 : monnier 71 | [] => bug "unexpected case on gen in flintcomp"
179 :     end
180 : monnier 102 in {c0=nc0, cn=ncn, data=dseg, name=ref (SOME src)}
181 : monnier 16 end (* function flintcomp *)
182 :    
183 : monnier 71 val flintcomp = phase "Compiler 050 flintcomp" flintcomp
184 : monnier 16
185 :     end (* local *)
186 :     end (* structure FLINTComp *)
187 : monnier 95
188 :     (*
189 : monnier 118 * $Log$
190 : monnier 95 *)

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