Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Diff of /sml/trunk/src/compiler/FLINT/main/flintcomp.sml
ViewVC logotype

Diff of /sml/trunk/src/compiler/FLINT/main/flintcomp.sml

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 118, Fri Jun 5 21:38:17 1998 UTC revision 251, Mon Apr 19 02:55:26 1999 UTC
# Line 1  Line 1 
1  (* COPYRIGHT (c) 1998 YALE FLINT PROJECT *)  (* COPYRIGHT (c) 1998 YALE FLINT PROJECT *)
2  (* flintcomp.sml *)  (* flintcomp.sml *)
3    
4  functor FLINTComp (structure Gen: CPSGEN  functor FLINTComp (structure Gen: MACHINE_GEN
5                     val collect: unit -> Word8Vector.vector) : CODEGENERATOR =                     val collect: unit -> CodeObj.code_object) : CODEGENERATOR =
6  struct  struct
7    
8  local structure CB = CompBasic  local structure CB = CompBasic
9        structure CGC = Control.CG  (*        structure CGC = Control.CG *)
10        structure MachSpec = Gen.MachSpec        structure MachSpec = Gen.MachSpec
11        structure Convert = Convert(MachSpec)        structure Convert = Convert(MachSpec)
12        structure CPStrans = CPStrans(MachSpec)        structure CPStrans = CPStrans(MachSpec)
13        structure CPSopt = CPSopt(MachSpec)        structure CPSopt = CPSopt(MachSpec)
14        structure Closure = Closure(MachSpec)        structure Closure = Closure(MachSpec)
15        structure Spill = Spill(MachSpec)        structure Spill = SpillFn(MachSpec)
16        structure CpsSplit = CpsSplitFun (MachSpec)        structure CpsSplit = CpsSplitFun (MachSpec)
17          structure CTRL = FLINT_Control
18          structure PP = PPFlint
19          structure LT = LtyExtern
20          structure O  = Option
21          structure F  = FLINT
22  in  in
23    
24    structure Machine = Gen
25  val architecture = Gen.MachSpec.architecture  val architecture = Gen.MachSpec.architecture
26  fun bug s = ErrorMsg.impossible ("FLINTComp:" ^ s)  fun bug s = ErrorMsg.impossible ("FLINTComp:" ^ s)
27  val say = Control.Print.say  val say = Control_Print.say
28    
29    datatype flintkind = FK_WRAP | FK_REIFY | FK_DEBRUIJN | FK_NAMED | FK_CPS
30    
31  fun phase x = Stats.doPhase (Stats.makePhase x)  fun phase x = Stats.doPhase (Stats.makePhase x)
32    
33    val deb2names = phase "Compiler 056 deb2names" TvarCvt.debIndex2names
34    val names2deb = phase "Compiler 057 names2deb" TvarCvt.names2debIndex
35    
36  val lcontract = phase "Compiler 052 lcontract" LContract.lcontract  val lcontract = phase "Compiler 052 lcontract" LContract.lcontract
37    (*  val lcontract' = phase "Compiler 052 lcontract'" LContract.lcontract *)
38    val fcollect  = phase "Compiler 052a fcollect" Collect.collect
39    val fcontract = phase "Compiler 052b fcontract" FContract.contract
40    val fcontract = fcontract o fcollect
41    val loopify   = phase "Compiler 057 loopify" Loopify.loopify
42    val fixfix    = phase "Compiler 056 fixfix" FixFix.fixfix
43    
44    val split     = phase "Compiler 058 split" FSplit.split
45    
46    val typelift  = phase "Compiler 0535 typelift" Lift.typeLift
47    val wformed   = phase "Compiler 0536 wformed" Lift.wellFormed
48    
49  val specialize= phase "Compiler 053 specialize" Specialize.specialize  val specialize= phase "Compiler 053 specialize" Specialize.specialize
50  val wrapping  = phase "Compiler 054 wrapping" Wrapping.wrapping  val wrapping  = phase "Compiler 054 wrapping" Wrapping.wrapping
51  val reify     = phase "Compiler 055 reify" Reify.reify  val reify     = phase "Compiler 055 reify" Reify.reify
52  (* val lambdaopt = phase "Compiler 057 lambdaopt" LContract.lcontract *)  val recover   = phase "Compiler 05a recover" Recover.recover
53    
54  val convert   = phase "Compiler 060 convert" Convert.convert  val convert   = phase "Compiler 060 convert" Convert.convert
55  val cpstrans  = phase "Compiler 065 cpstrans" CPStrans.cpstrans  val cpstrans  = phase "Compiler 065 cpstrans" CPStrans.cpstrans
56  val cpsopt    = phase "Compiler 070 cpsopt" CPSopt.reduce  val cpsopt    = phase "Compiler 070 cpsopt" CPSopt.reduce
57  val litsplit  = phase "Compiler 075 litsplit" Literals.litsplit  val litsplit  = phase "Compiler 075 litsplit" Literals.litsplit
58  val lit2cps   = phase "Compiler 076 lit2cps" Literals.lit2cps  val litToBytes = phase "Compiler 076 litToBytes" Literals.litToBytes
59  val closure   = phase "Compiler 080 closure"  Closure.closeCPS  val closure   = phase "Compiler 080 closure"  Closure.closeCPS
60  val globalfix = phase "Compiler 090 globalfix" GlobalFix.globalfix  val globalfix = phase "Compiler 090 globalfix" GlobalFix.globalfix
61  val spill     = if MachSpec.spillAreaSz < 500 * MachSpec.valueSize  val spill     = phase "Compiler 100 spill" Spill.spill
                 then phase "Compiler 100 spill" Spill.spill  
                 else fn x => x  
62  val limit     = phase "Compiler 110 limit" Limit.nolimit  val limit     = phase "Compiler 110 limit" Limit.nolimit
63  val codegen   = phase "Compiler 120 cpsgen" Gen.codegen  val codegen   = phase "Compiler 120 cpsgen" Gen.codegen
64    
 val closureD  = phase "Compiler 081 closureD"  Closure.closeCPS  
 val globalfixD= phase "Compiler 091 globalfixD" GlobalFix.globalfix  
 val spillD    = if MachSpec.spillAreaSz < 500 * MachSpec.valueSize  
                 then phase "Compiler 101 spillD" Spill.spill  
                 else fn x => x  
 val limitD    = phase "Compiler 110 limitD" Limit.nolimit  
 val codegenD  = phase "Compiler 121 cpsgenD" Gen.codegen  
   
65  (** pretty printing for the FLINT and CPS code *)  (** pretty printing for the FLINT and CPS code *)
66  val (prF, prC) =  val (prF, prC) =
67    let fun prGen (flag,printE) s e =    let fun prGen (flag,printE) s e =
68          if !flag then (say ("\n[After " ^ s ^ " ...]\n\n"); printE e;          if !flag then (say ("\n[After " ^ s ^ " ...]\n\n"); printE e;
69                         say "\n"; e)                         say "\n"; e)
70          else e          else e
71     in (prGen (CGC.printFlint, PPFlint.printProg),     in (prGen (CTRL.print, PPFlint.printProg),
72         prGen (CGC.printit, PPCps.printcps0))         prGen (Control.CG.printit, PPCps.printcps0))
73    end    end
74    
75  (** writing out a term into a error output file *)  (** writing out a term into a error output file *)
# Line 71  Line 84 
84        done ()        done ()
85    end (* function dumpTerm *)    end (* function dumpTerm *)
86    
87    val fcs : (FLINT.prog -> FLINT.prog) list ref = ref []
88    
89  (** compiling FLINT code into the binary machine code *)  (** compiling FLINT code into the binary machine code *)
90  fun flintcomp(flint, compInfo as {error, sourceName=src, ...}: CB.compInfo) =  fun flintcomp(flint, compInfo as {error, sourceName=src, ...}: CB.compInfo) =
91    let fun err severity s =    let fun err severity s =
92          error (0,0) severity (concat["Real constant out of range: ",s,"\n"])          error (0,0) severity (concat["Real constant out of range: ",s,"\n"])
93    
94        fun check (checkE,printE,chkId) (enableChk,lvl,logId) e =        fun check (checkE,printE,chkId) (lvl,logId) e =
95          (if !enableChk andalso checkE (e,lvl) then            if checkE (e,lvl) then
96             (dumpTerm (printE, src ^ "." ^ chkId ^ logId, e);             (dumpTerm (printE, src ^ "." ^ chkId ^ logId, e);
97              bug (chkId ^ " typing errors " ^ logId))              bug (chkId ^ " typing errors " ^ logId))
98              else ()
99          fun wff (f, s) = if wformed f then ()
100                           else print ("\nAfter " ^ s ^ " CODE NOT WELL FORMED\n")
101    
102          (* f:prog         flint code
103           * fi:prog opt    inlinable approximation of f
104           * fk:flintkind   what kind of flint variant this is
105           * l:string       last phase through which it went *)
106          fun runphase (p,(f,fi,fk,l)) =
107              case (p,fk)
108               of (("fcontract" | "lcontract"), FK_DEBRUIJN) =>
109                  (say("\n!! "^p^" cannot be applied to the DeBruijn form !!\n");
110                   (f, fi, fk, l))
111    
112                | ("fcontract",_)           => (fcontract f,  fi, fk, p)
113                | ("lcontract",_)           => (lcontract f,  fi, fk, p)
114                | ("fixfix",   _)           => (fixfix f,     fi, fk, p)
115                | ("loopify",  _)           => (loopify f,    fi, fk, p)
116                | ("specialize",FK_NAMED)   => (specialize f, fi, fk, p)
117                | ("wrap",FK_NAMED)         => (wrapping f,   fi, FK_WRAP, p)
118                | ("reify",FK_WRAP)         => (reify f,      fi, FK_REIFY, p)
119                | ("deb2names",FK_DEBRUIJN) => (deb2names f,  fi, FK_NAMED, p)
120                | ("names2deb",FK_NAMED)    => (names2deb f,  fi, FK_DEBRUIJN, p)
121                | ("typelift", _)           =>
122                  let val f = typelift f
123                  in if !CTRL.check then wff(f, p) else (); (f, fi, fk, p) end
124                | ("split",    FK_NAMED)    =>
125                  let val (f,fi) = split f in (f, fi, fk, p) end
126    
127                (* pseudo FLINT phases *)
128                | ("pickle",   _)           =>
129                  (valOf(UnpickMod.unpickleFLINT(PickMod.pickleFLINT(SOME f))),
130                   UnpickMod.unpickleFLINT(PickMod.pickleFLINT fi),
131                   fk, p)
132                | ("collect",_) => (fcollect f, fi, fk, p)
133                | _ =>
134                  ((case (p,fk)
135                     of ("id",_) => ()
136                      | ("wellformed",_) => wff(f,l)
137                      | ("recover",_) =>
138                        let val {getLty,...} = recover(f, fk = FK_REIFY)
139                        in CTRL.recover := (say o LT.lt_print o getLty o F.VAR)
140                        end
141                      | ("print",_) =>
142                        (say("\n[After "^l^"...]\n\n"); PP.printFundec f; say "\n")
143                      | ("printsplit", _) =>
144                        (say "[ splitted ]\n\n"; O.map PP.printFundec fi; say "\n")
145                      | ("check",_) =>
146                        (check (ChkFlint.checkTop, PPFlint.printFundec, "FLINT")
147                               (fk = FK_REIFY, l) f)
148                      | _ =>
149                        say("\n!! Unknown or badly scheduled FLINT phase '"^p^"' !!\n"));
150                        (f, fi, fk, l))
151    
152          fun print (f,fi,fk,l) = (prF l f; (f, fi, fk, l))
153          fun check' (f,fi,fk,l) =
154              let fun c n reified f =
155                      check (ChkFlint.checkTop, PPFlint.printFundec, n)
156                            (reified, l) (names2deb f)
157              in if !CTRL.check then
158                  (c "FLINT" (fk = FK_REIFY) f; O.map (c "iFLINT" false) fi; ())
159           else ();           else ();
160           e)                   (f, fi, fk, l)
161        fun chkF (b, s) =            end
         check (ChkFlint.checkTop, PPFlint.printFundec,  
                "FLINT") (CGC.checkFlint, b, s)  
   
       val _ = (chkF (false,"1") o prF "Translation/Normalization") flint  
       val flint = (chkF (false,"2") o prF "Lcontract" o lcontract) flint  
       val flint =  
         if !CGC.specialize then  
            (chkF (false,"3") o prF "Specialization" o specialize) flint  
         else flint  
162    
163        val flint = (chkF (false, "4") o prF "Wrapping" o wrapping) flint        fun runphase' (arg as (p,{1=f,...})) =
164        val flint = (chkF (true, "5") o prF "Reify" o reify) flint            (if !CTRL.printPhases then say("Phase "^p^"...") else ();
165               ((check' o print o runphase) arg) before
166               (if !CTRL.printPhases then say("..."^p^" Done.\n") else ()))
167                  handle x => (say ("\nwhile in "^p^" phase\n");
168                               dumpTerm(PPFlint.printFundec,"FLINT.core", f);
169                               raise x)
170    
171          val (flint,fi,fk,_) = foldl runphase'
172                                      (flint, NONE, FK_DEBRUIJN, "flintnm")
173                                      ((* "id" :: *) "deb2names" :: !CTRL.phases)
174    
175          (* run any missing phases *)
176          val (flint,fk) =
177              if fk = FK_DEBRUIJN
178              then (say "\n!!Forgot deb2names!!\n"; (deb2names flint, FK_NAMED))
179              else (flint,fk)
180          val (flint,fk) =
181              if fk = FK_NAMED
182              then (say "\n!!Forgot wrap!!\n"; (wrapping flint, FK_WRAP))
183              else (flint,fk)
184          val (flint,fk) =
185              if fk = FK_WRAP
186              then (say "\n!!Forgot reify!!\n"; (reify flint, FK_REIFY))
187              else (flint,fk)
188    
189          (* finish up with CPS *)
190        val (nc0, ncn, dseg) =        val (nc0, ncn, dseg) =
191          let val function = convert flint          let val function = convert flint
192              val _ = prC "convert" function              val _ = prC "convert" function
193              val function = (prC "cpstrans" o cpstrans) function              val function = (prC "cpstrans" o cpstrans) function
194              val function =              val function = cpsopt (function,NONE,false)
               if !CGC.cpsopt then cpsopt (function,NONE,false)  
               else function  
195              val _ = prC "cpsopt" function              val _ = prC "cpsopt" function
196    
197              val (function, dlit) = litsplit function              val (function, dlit) = litsplit function
198              val data = lit2cps dlit              val data = litToBytes dlit
199              val _ = prC "cpsopt-code" function              val _ = prC "cpsopt-code" function
             val _ = prC "cpsopt-data" data  
200    
201    (** NOTE: we should be passing the source-code name (src) to the
202     ** code generator somehow (for the second argument to code object allocation).
203     **)
204              fun gen fx =              fun gen fx =
205                let val fx = (prC "closure" o closure) fx                let val fx = (prC "closure" o closure) fx
206                    val carg = globalfix fx                    val carg = globalfix fx
# Line 119  Line 210 
210                    collect ()                    collect ()
211                end                end
212    
             fun gdata dd =  
               let val x = Control.CG.printit  
                   val y = !x  
                   val _ = (x := false)  
                   val fx = (prC "closure" o closureD) dd  
                   val carg = globalfixD fx  
                   val carg = spillD carg  
                   val (carg, limit) = limitD carg  
                in codegenD (carg, limit, err);  
                   (collect ()) before (x := y)  
               end  
213           in case CpsSplit.cpsSplit function           in case CpsSplit.cpsSplit function
214               of (fun0 :: funn) => (gen fun0, map gen funn, gdata data)               of (fun0 :: funn) => (gen fun0, map gen funn, data)
215                | [] => bug "unexpected case on gen in flintcomp"                | [] => bug "unexpected case on gen in flintcomp"
216          end          end
217     in {c0=nc0, cn=ncn, data=dseg, name=ref (SOME src)}     in ({c0=nc0, cn=ncn, data=dseg}, fi)
218    end (* function flintcomp *)    end (* function flintcomp *)
219    
220  val flintcomp = phase "Compiler 050 flintcomp" flintcomp  val flintcomp = phase "Compiler 050 flintcomp" flintcomp
# Line 143  Line 223 
223  end (* structure FLINTComp *)  end (* structure FLINTComp *)
224    
225  (*  (*
226   * $Log$   * $Log: flintcomp.sml,v $
227     * Revision 1.8  1999/01/11 16:53:25  george
228     *   new array representation support
229     *
230   *)   *)

Legend:
Removed from v.118  
changed lines
  Added in v.251

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