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/cm/bootstrap/btcompile.sml
ViewVC logotype

Annotation of /sml/trunk/src/cm/bootstrap/btcompile.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 371 - (view) (download)

1 : blume 327 (*
2 :     * The bootstrap compiler.
3 :     * (Formerly known as "batch" compiler.)
4 :     *
5 :     * (C) 1999 Lucent Technologies, Bell Laboratories
6 :     *
7 :     * Author: Matthias Blume (blume@kurims.kyoto-u.ac.jp)
8 :     *)
9 : blume 329 functor BootstrapCompileFn (structure MachDepVC: MACHDEP_VC
10 : blume 357 val os: SMLofNJ.SysInfo.os_kind) :> sig
11 : blume 364 val make' : string option -> bool
12 :     val make : unit -> bool
13 : blume 362 val deliver' : string option -> bool
14 :     val deliver : unit -> bool
15 :     val reset : unit -> unit
16 : blume 357 end = struct
17 :    
18 : blume 327 structure EM = GenericVC.ErrorMsg
19 :     structure E = GenericVC.Environment
20 :     structure SE = GenericVC.CMStaticEnv
21 :     structure BE = GenericVC.BareEnvironment
22 :     structure PS = GenericVC.PersStamps
23 :     structure CoerceEnv = GenericVC.CoerceEnv
24 : blume 336 structure SSV = SpecificSymValFn (structure MachDepVC = MachDepVC
25 :     val os = os)
26 : blume 364 structure P = OS.Path
27 :     structure F = OS.FileSys
28 : blume 327
29 :     (* Since the bootstrap compiler never executes any of the code
30 :     * it produces, we don't need any dynamic values. Therefore,
31 :     * we create RecompPersstate (but not FullPersstate!) and
32 :     * instantiate Recomp as well as RecompTraversal.
33 :     * Since RecompPersstate is not part of any surrounding FullPersstate,
34 :     * function "discard_value" simply does nothing. *)
35 :     structure RecompPersstate =
36 :     RecompPersstateFn (structure MachDepVC = MachDepVC
37 :     val discard_code = true
38 : blume 362 fun stable_value_present i = false
39 :     fun new_smlinfo i = ())
40 : blume 360
41 : blume 327 structure Recomp = RecompFn (structure PS = RecompPersstate)
42 : blume 371 structure RT = CompileGenericFn (structure CT = Recomp)
43 : blume 327
44 :     fun recomp gp g = isSome (RT.group gp g)
45 :    
46 :     (* instantiate Stabilize... *)
47 : blume 329 structure Stabilize =
48 :     StabilizeFn (fun bn2statenv gp i = #1 (#stat (valOf (RT.bnode gp i)))
49 : blume 357 fun warmup (i, p) = ()
50 : blume 363 val recomp = recomp
51 :     val transfer_state = RecompPersstate.transfer_state)
52 : blume 327 (* ... and Parse *)
53 : blume 362 structure Parse = ParseFn (structure Stabilize = Stabilize
54 :     val pending = AutoLoad.getPending)
55 : blume 327
56 : blume 366 (* copying an input file to an output file safely... *)
57 : blume 368 fun copyFile (oi, ci, oo, co, inp, outp, eof) (inf, outf) = let
58 :     fun workIn is = let
59 :     fun workOut os = let
60 : blume 366 val N = 4096
61 :     fun loop () =
62 : blume 368 if eof is then () else (outp (os, inp (is, N)); loop ())
63 : blume 366 in
64 :     loop ()
65 :     end
66 :     in
67 : blume 368 SafeIO.perform { openIt = fn () => oo outf,
68 :     closeIt = co,
69 : blume 366 work = workOut,
70 :     cleanup = fn () =>
71 :     (F.remove outf handle _ => ()) }
72 :     end
73 : blume 364 in
74 : blume 368 SafeIO.perform { openIt = fn () => oi inf,
75 :     closeIt = ci,
76 : blume 366 work = workIn,
77 :     cleanup = fn () => () }
78 : blume 364 end
79 : blume 327
80 : blume 368 val copyTextFile =
81 :     copyFile (TextIO.openIn, TextIO.closeIn,
82 :     AutoDir.openTextOut, TextIO.closeOut,
83 :     TextIO.inputN, TextIO.output, TextIO.endOfStream)
84 :    
85 :     val copyBinFile =
86 :     copyFile (BinIO.openIn, BinIO.closeIn,
87 :     AutoDir.openBinOut, BinIO.closeOut,
88 :     BinIO.inputN, BinIO.output, BinIO.endOfStream)
89 :    
90 : blume 362 fun compile deliver dbopt = let
91 : blume 358
92 : blume 360 val dirbase = getOpt (dbopt, BtNames.dirbaseDefault)
93 :     val pcmodespec = BtNames.pcmodespec
94 :     val initgspec = BtNames.initgspec
95 :     val maingspec = BtNames.maingspec
96 :    
97 : blume 357 val arch = MachDepVC.architecture
98 :     val osname = FilenamePolicy.kind2name os
99 :     val bindir = concat [dirbase, ".bin.", arch, "-", osname]
100 :     val bootdir = concat [dirbase, ".boot.", arch, "-", osname]
101 :    
102 : blume 364 fun listName (p, copy) =
103 :     case P.fromString p of
104 :     { vol = "", isAbs = false, arcs = arc0 :: arc1 :: arcn } => let
105 :     fun win32name () =
106 :     concat (arc1 ::
107 :     foldr (fn (a, r) => "\\" :: a :: r) [] arcn)
108 :     fun doCopy () = let
109 :     val bootpath =
110 :     P.toString { isAbs = false, vol = "",
111 :     arcs = bootdir :: arc1 :: arcn }
112 :     in
113 : blume 368 copyBinFile (p, bootpath)
114 : blume 364 end
115 :     in
116 :     if copy andalso arc0 = bindir then doCopy () else ();
117 :     case os of
118 :     SMLofNJ.SysInfo.WIN32 => win32name ()
119 :     | _ => P.toString { isAbs = false, vol = "",
120 :     arcs = arc1 :: arcn }
121 :     end
122 :     | _ => raise Fail "BootstrapCompile:listName: bad name"
123 :    
124 : blume 329 val keep_going = EnvConfig.getSet StdConfig.keep_going NONE
125 :    
126 : blume 354 val ctxt = SrcPath.cwdContext ()
127 : blume 329
128 : blume 364 val pidfile = P.joinDirFile { dir = bootdir, file = "RTPID" }
129 :     val listfile = P.joinDirFile { dir = bootdir, file = "BOOTLIST" }
130 : blume 329
131 : blume 361 val pcmode = PathConfig.new ()
132 :     val _ = PathConfig.processSpecFile (pcmode, pcmodespec)
133 : blume 329
134 : blume 354 fun stdpath s = SrcPath.standard pcmode { context = ctxt, spec = s }
135 : blume 352
136 :     val initgspec = stdpath initgspec
137 :     val maingspec = stdpath maingspec
138 :    
139 : blume 360 val cmifile = valOf (SrcPath.reAnchoredName (initgspec, bootdir))
140 :     handle Option => raise Fail "BootstrapCompile: cmifile"
141 :    
142 : blume 364 val fnpolicy =
143 : blume 357 FilenamePolicy.separate { bindir = bindir, bootdir = bootdir }
144 :     { arch = arch, os = os }
145 :    
146 : blume 364 fun mkParam { primconf, pervasive, pervcorepids }
147 : blume 357 { corenv } =
148 : blume 349 { primconf = primconf,
149 :     fnpolicy = fnpolicy,
150 :     pcmode = pcmode,
151 :     symenv = SSV.env,
152 :     keep_going = keep_going,
153 :     pervasive = pervasive,
154 :     corenv = corenv,
155 :     pervcorepids = pervcorepids }
156 :    
157 : blume 327 val emptydyn = E.dynamicPart E.emptyEnv
158 :    
159 :     (* first, build an initial GeneralParam.info, so we can
160 :     * deal with the pervasive env and friends... *)
161 :    
162 : blume 360 val primconf = Primitive.primEnvConf
163 : blume 349 val mkInitParam = mkParam { primconf = primconf,
164 :     pervasive = E.emptyEnv,
165 : blume 364 pervcorepids = PidSet.empty }
166 : blume 327
167 : blume 349 val param_nocore = mkInitParam { corenv = BE.staticPart BE.emptyEnv }
168 :    
169 : blume 327 val groupreg = GroupReg.new ()
170 :     val errcons = EM.defaultConsumer ()
171 : blume 329 val ginfo_nocore = { param = param_nocore, groupreg = groupreg,
172 :     errcons = errcons }
173 : blume 327
174 :     fun main_compile arg = let
175 : blume 335 val { rts, core, pervasive, primitives, binpaths } = arg
176 : blume 327
177 : blume 329 val ovldR = GenericVC.Control.overloadKW
178 :     val savedOvld = !ovldR
179 :     val _ = ovldR := true
180 :    
181 : blume 327 (* here we build a new gp -- the one that uses the freshly
182 :     * brewed pervasive env, core env, and primitives *)
183 : blume 358 val core = valOf (RT.sbnode ginfo_nocore core)
184 : blume 329 val corenv = CoerceEnv.es2bs (#1 (#stat core))
185 : blume 356 val core_sym = #1 (#sym core)
186 : blume 329
187 :     (* The following is a bit of a hack (but corenv is a hack anyway):
188 :     * As soon as we have core available, we have to patch the
189 :     * ginfo to include the correct corenv (because virtually
190 :     * everybody else needs access to corenv). *)
191 : blume 349 val param_justcore = mkInitParam { corenv = corenv }
192 : blume 329 val ginfo_justcore = { param = param_justcore, groupreg = groupreg,
193 :     errcons = errcons }
194 :    
195 : blume 358 fun rt n = valOf (RT.sbnode ginfo_justcore n)
196 : blume 327 val rts = rt rts
197 :     val pervasive = rt pervasive
198 :    
199 :     fun sn2pspec (name, n) = let
200 : blume 369 val { stat = (s, sp), sym = (sy, syp), ctxt, bfc } = rt n
201 : blume 327 val env =
202 :     E.mkenv { static = s, symbolic = sy, dynamic = emptydyn }
203 :     val pidInfo = { statpid = sp, sympid = syp, ctxt = ctxt }
204 :     in
205 :     { name = name, env = env, pidInfo = pidInfo }
206 :     end
207 :    
208 :     val pspecs = map sn2pspec primitives
209 :    
210 : blume 329 val _ = ovldR := savedOvld
211 :    
212 : blume 356 (* This is a hack but must be done for both the symbolic
213 :     * and later the dynamic part of the core environment:
214 :     * we must include these parts in the pervasive env. *)
215 :     val perv_sym = E.layerSymbolic (#1 (#sym pervasive), core_sym)
216 :    
217 : blume 349 val param =
218 :     mkParam { primconf = Primitive.configuration pspecs,
219 : blume 327 pervasive = E.mkenv { static = #1 (#stat pervasive),
220 : blume 356 symbolic = perv_sym,
221 : blume 327 dynamic = emptydyn },
222 :     pervcorepids =
223 :     PidSet.addList (PidSet.empty,
224 :     [#2 (#stat pervasive),
225 :     #2 (#sym pervasive),
226 : blume 364 #2 (#stat core)]) }
227 : blume 349 { corenv = corenv }
228 : blume 362 val stab =
229 :     if deliver then SOME true else NONE
230 : blume 327 in
231 : blume 362 case Parse.parse NONE param stab maingspec of
232 : blume 349 NONE => false
233 : blume 329 | SOME (g, gp) =>
234 : blume 349 if recomp gp g then let
235 :     val rtspid = PS.toHex (#2 (#stat rts))
236 :     fun writeList s = let
237 : blume 364 fun add ((p, flag), l) = let
238 :     val n = listName (p, true)
239 :     in
240 :     if flag then n :: l else l
241 :     end
242 :     fun transcribe (p, NONE) = listName (p, true)
243 :     | transcribe (p, SOME (off, desc)) =
244 :     concat [listName (p, false),
245 :     "@", Int.toString off, ":", desc]
246 :     val bootstrings =
247 :     foldr add (map transcribe (MkBootList.group g))
248 :     binpaths
249 :     fun show str =
250 :     (TextIO.output (s, str);
251 :     TextIO.output (s, "\n"))
252 : blume 349 in
253 : blume 364 app show bootstrings
254 : blume 349 end
255 :     in
256 : blume 362 if deliver then
257 :     (SafeIO.perform { openIt = fn () =>
258 : blume 354 AutoDir.openTextOut pidfile,
259 : blume 349 closeIt = TextIO.closeOut,
260 :     work = fn s =>
261 :     TextIO.output (s, rtspid ^ "\n"),
262 :     cleanup = fn () =>
263 : blume 354 OS.FileSys.remove pidfile
264 :     handle _ => () };
265 : blume 349 SafeIO.perform { openIt = fn () =>
266 : blume 354 AutoDir.openTextOut listfile,
267 : blume 349 closeIt = TextIO.closeOut,
268 :     work = writeList,
269 :     cleanup = fn () =>
270 : blume 354 OS.FileSys.remove listfile
271 :     handle _ => () };
272 : blume 368 copyTextFile (SrcPath.osstring initgspec, cmifile);
273 : blume 366 Say.say ["Runtime System PID is: ", rtspid, "\n"])
274 : blume 362 else ();
275 :     true
276 : blume 349 end
277 :     else false
278 : blume 351 end handle Option => (RT.reset (); false)
279 : blume 330 (* to catch valOf failures in "rt" *)
280 : blume 327 in
281 : blume 329 case BuildInitDG.build ginfo_nocore initgspec of
282 : blume 327 SOME x => main_compile x
283 : blume 349 | NONE => false
284 : blume 327 end
285 : blume 362
286 : blume 364 val make' = compile false
287 :     fun make () = make' NONE
288 : blume 362 val deliver' = compile true
289 :     fun deliver () = deliver' NONE
290 :     fun reset () =
291 :     (RecompPersstate.reset ();
292 :     RT.resetAll ();
293 : blume 367 Recomp.reset ();
294 :     Parse.reset ())
295 : blume 327 end

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