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 451 - (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 434 val symval : string -> { get: unit -> int option, set: int option -> unit }
17 : blume 357 end = struct
18 :    
19 : blume 327 structure EM = GenericVC.ErrorMsg
20 :     structure E = GenericVC.Environment
21 :     structure SE = GenericVC.CMStaticEnv
22 :     structure BE = GenericVC.BareEnvironment
23 :     structure PS = GenericVC.PersStamps
24 :     structure CoerceEnv = GenericVC.CoerceEnv
25 : blume 336 structure SSV = SpecificSymValFn (structure MachDepVC = MachDepVC
26 :     val os = os)
27 : blume 364 structure P = OS.Path
28 :     structure F = OS.FileSys
29 : blume 403 structure BF = MachDepVC.Binfile
30 : blume 327
31 : blume 448 structure Compile = CompileFn (structure MachDepVC = MachDepVC
32 : blume 449 val compile_there = Servers.compile)
33 : blume 360
34 : blume 403 structure BFC = BfcFn (structure MachDepVC = MachDepVC)
35 :    
36 : blume 327 (* instantiate Stabilize... *)
37 : blume 329 structure Stabilize =
38 : blume 451 StabilizeFn (fun destroy_state _ i =
39 :     (Compile.evict i;
40 :     Servers.evict i)
41 : blume 403 structure MachDepVC = MachDepVC
42 : blume 399 fun recomp gp g = let
43 : blume 403 val { store, get } = BFC.new ()
44 : blume 399 val { group, ... } =
45 : blume 403 Compile.newTraversal (fn _ => fn _ => (),
46 :     store, g)
47 : blume 399 in
48 : blume 450 case Servers.withServers (fn () => group gp) of
49 : blume 403 NONE => NONE
50 :     | SOME _ => SOME get
51 :     end
52 :     val getII = Compile.getII)
53 : blume 398
54 : blume 327 (* ... and Parse *)
55 : blume 362 structure Parse = ParseFn (structure Stabilize = Stabilize
56 : blume 372 fun pending () = SymbolMap.empty)
57 : blume 327
58 : blume 366 (* copying an input file to an output file safely... *)
59 : blume 368 fun copyFile (oi, ci, oo, co, inp, outp, eof) (inf, outf) = let
60 :     fun workIn is = let
61 :     fun workOut os = let
62 : blume 366 val N = 4096
63 :     fun loop () =
64 : blume 368 if eof is then () else (outp (os, inp (is, N)); loop ())
65 : blume 366 in
66 :     loop ()
67 :     end
68 :     in
69 : blume 368 SafeIO.perform { openIt = fn () => oo outf,
70 :     closeIt = co,
71 : blume 366 work = workOut,
72 :     cleanup = fn () =>
73 :     (F.remove outf handle _ => ()) }
74 :     end
75 : blume 364 in
76 : blume 368 SafeIO.perform { openIt = fn () => oi inf,
77 :     closeIt = ci,
78 : blume 366 work = workIn,
79 :     cleanup = fn () => () }
80 : blume 364 end
81 : blume 327
82 : blume 368 val copyTextFile =
83 :     copyFile (TextIO.openIn, TextIO.closeIn,
84 :     AutoDir.openTextOut, TextIO.closeOut,
85 :     TextIO.inputN, TextIO.output, TextIO.endOfStream)
86 :    
87 :     val copyBinFile =
88 :     copyFile (BinIO.openIn, BinIO.closeIn,
89 :     AutoDir.openBinOut, BinIO.closeOut,
90 :     BinIO.inputN, BinIO.output, BinIO.endOfStream)
91 :    
92 : blume 449 fun mk_compile deliver dbopt = let
93 : blume 358
94 : blume 360 val dirbase = getOpt (dbopt, BtNames.dirbaseDefault)
95 :     val pcmodespec = BtNames.pcmodespec
96 :     val initgspec = BtNames.initgspec
97 :     val maingspec = BtNames.maingspec
98 :    
99 : blume 357 val arch = MachDepVC.architecture
100 :     val osname = FilenamePolicy.kind2name os
101 :     val bindir = concat [dirbase, ".bin.", arch, "-", osname]
102 :     val bootdir = concat [dirbase, ".boot.", arch, "-", osname]
103 :    
104 : blume 364 fun listName (p, copy) =
105 :     case P.fromString p of
106 :     { vol = "", isAbs = false, arcs = arc0 :: arc1 :: arcn } => let
107 :     fun win32name () =
108 :     concat (arc1 ::
109 :     foldr (fn (a, r) => "\\" :: a :: r) [] arcn)
110 :     fun doCopy () = let
111 :     val bootpath =
112 :     P.toString { isAbs = false, vol = "",
113 :     arcs = bootdir :: arc1 :: arcn }
114 :     in
115 : blume 368 copyBinFile (p, bootpath)
116 : blume 364 end
117 :     in
118 :     if copy andalso arc0 = bindir then doCopy () else ();
119 :     case os of
120 :     SMLofNJ.SysInfo.WIN32 => win32name ()
121 :     | _ => P.toString { isAbs = false, vol = "",
122 :     arcs = arc1 :: arcn }
123 :     end
124 :     | _ => raise Fail "BootstrapCompile:listName: bad name"
125 :    
126 : blume 433 val keep_going = #get StdConfig.keep_going ()
127 : blume 329
128 : blume 354 val ctxt = SrcPath.cwdContext ()
129 : blume 329
130 : blume 364 val pidfile = P.joinDirFile { dir = bootdir, file = "RTPID" }
131 :     val listfile = P.joinDirFile { dir = bootdir, file = "BOOTLIST" }
132 : blume 329
133 : blume 361 val pcmode = PathConfig.new ()
134 :     val _ = PathConfig.processSpecFile (pcmode, pcmodespec)
135 : blume 329
136 : blume 354 fun stdpath s = SrcPath.standard pcmode { context = ctxt, spec = s }
137 : blume 352
138 :     val initgspec = stdpath initgspec
139 :     val maingspec = stdpath maingspec
140 :    
141 : blume 360 val cmifile = valOf (SrcPath.reAnchoredName (initgspec, bootdir))
142 :     handle Option => raise Fail "BootstrapCompile: cmifile"
143 :    
144 : blume 364 val fnpolicy =
145 : blume 357 FilenamePolicy.separate { bindir = bindir, bootdir = bootdir }
146 :     { arch = arch, os = os }
147 :    
148 : blume 364 fun mkParam { primconf, pervasive, pervcorepids }
149 : blume 357 { corenv } =
150 : blume 349 { primconf = primconf,
151 :     fnpolicy = fnpolicy,
152 :     pcmode = pcmode,
153 : blume 433 symval = SSV.symval,
154 : blume 349 keep_going = keep_going,
155 :     pervasive = pervasive,
156 :     corenv = corenv,
157 :     pervcorepids = pervcorepids }
158 :    
159 : blume 327 val emptydyn = E.dynamicPart E.emptyEnv
160 :    
161 :     (* first, build an initial GeneralParam.info, so we can
162 :     * deal with the pervasive env and friends... *)
163 :    
164 : blume 360 val primconf = Primitive.primEnvConf
165 : blume 349 val mkInitParam = mkParam { primconf = primconf,
166 :     pervasive = E.emptyEnv,
167 : blume 364 pervcorepids = PidSet.empty }
168 : blume 327
169 : blume 349 val param_nocore = mkInitParam { corenv = BE.staticPart BE.emptyEnv }
170 :    
171 : blume 327 val groupreg = GroupReg.new ()
172 :     val errcons = EM.defaultConsumer ()
173 : blume 329 val ginfo_nocore = { param = param_nocore, groupreg = groupreg,
174 :     errcons = errcons }
175 : blume 327
176 : blume 449 fun mk_main_compile arg = let
177 : blume 450
178 : blume 335 val { rts, core, pervasive, primitives, binpaths } = arg
179 : blume 327
180 : blume 329 val ovldR = GenericVC.Control.overloadKW
181 :     val savedOvld = !ovldR
182 :     val _ = ovldR := true
183 : blume 400 val sbnode = Compile.newSbnodeTraversal ()
184 : blume 329
185 : blume 327 (* here we build a new gp -- the one that uses the freshly
186 :     * brewed pervasive env, core env, and primitives *)
187 : blume 398 val core = valOf (sbnode ginfo_nocore core)
188 :     val corenv = CoerceEnv.es2bs (#statenv (#ii core) ())
189 :     val core_sym = #symenv (#ii core) ()
190 : blume 329
191 :     (* The following is a bit of a hack (but corenv is a hack anyway):
192 :     * As soon as we have core available, we have to patch the
193 :     * ginfo to include the correct corenv (because virtually
194 :     * everybody else needs access to corenv). *)
195 : blume 349 val param_justcore = mkInitParam { corenv = corenv }
196 : blume 329 val ginfo_justcore = { param = param_justcore, groupreg = groupreg,
197 :     errcons = errcons }
198 :    
199 : blume 398 fun rt n = valOf (sbnode ginfo_justcore n)
200 : blume 327 val rts = rt rts
201 :     val pervasive = rt pervasive
202 :    
203 :     fun sn2pspec (name, n) = let
204 : blume 398 val { ii = { statenv, symenv, statpid, sympid }, ctxt } = rt n
205 : blume 327 val env =
206 : blume 398 E.mkenv { static = statenv (),
207 :     symbolic = symenv (),
208 :     dynamic = emptydyn }
209 :     val pidInfo =
210 :     { statpid = statpid, sympid = sympid, ctxt = ctxt }
211 : blume 327 in
212 :     { name = name, env = env, pidInfo = pidInfo }
213 :     end
214 :    
215 :     val pspecs = map sn2pspec primitives
216 :    
217 : blume 329 val _ = ovldR := savedOvld
218 :    
219 : blume 372 (* The following is a hack but must be done for both the symbolic
220 : blume 356 * and later the dynamic part of the core environment:
221 :     * we must include these parts in the pervasive env. *)
222 : blume 398 val perv_sym = E.layerSymbolic (#symenv (#ii pervasive) (),
223 :     core_sym)
224 : blume 356
225 : blume 349 val param =
226 :     mkParam { primconf = Primitive.configuration pspecs,
227 : blume 398 pervasive = E.mkenv { static =
228 :     #statenv (#ii pervasive) (),
229 : blume 356 symbolic = perv_sym,
230 : blume 327 dynamic = emptydyn },
231 :     pervcorepids =
232 :     PidSet.addList (PidSet.empty,
233 : blume 398 [#statpid (#ii pervasive),
234 :     #sympid (#ii pervasive),
235 :     #statpid (#ii core)]) }
236 : blume 349 { corenv = corenv }
237 : blume 362 val stab =
238 :     if deliver then SOME true else NONE
239 : blume 327 in
240 : blume 451 Servers.cmb dirbase;
241 : blume 362 case Parse.parse NONE param stab maingspec of
242 : blume 449 NONE => NONE
243 : blume 399 | SOME (g, gp) => let
244 : blume 449 fun thunk () = let
245 :     fun store _ = ()
246 :     val { group = recomp, ... } =
247 :     Compile.newTraversal (fn _ => fn _ => (), store, g)
248 : blume 450 val res =
249 :     Servers.withServers (fn () => recomp gp)
250 : blume 449 in
251 : blume 450 if isSome res then let
252 : blume 449 val rtspid = PS.toHex (#statpid (#ii rts))
253 :     fun writeList s = let
254 :     fun add ((p, flag), l) = let
255 :     val n = listName (p, true)
256 :     in
257 :     if flag then n :: l else l
258 :     end
259 :     fun transcribe (p, NONE) = listName (p, true)
260 :     | transcribe (p, SOME (off, desc)) =
261 :     concat [listName (p, false),
262 :     "@", Int.toString off, ":", desc]
263 :     val bootstrings =
264 :     foldr add
265 :     (map transcribe (MkBootList.group g))
266 :     binpaths
267 :     fun show str =
268 :     (TextIO.output (s, str);
269 :     TextIO.output (s, "\n"))
270 : blume 364 in
271 : blume 449 app show bootstrings
272 : blume 364 end
273 : blume 349 in
274 : blume 449 if deliver then
275 :     (SafeIO.perform
276 :     { openIt = fn () =>
277 :     AutoDir.openTextOut pidfile,
278 :     closeIt = TextIO.closeOut,
279 :     work = fn s =>
280 :     TextIO.output (s, rtspid ^ "\n"),
281 :     cleanup = fn () =>
282 :     OS.FileSys.remove pidfile
283 :     handle _ => () };
284 :     SafeIO.perform
285 :     { openIt = fn () =>
286 :     AutoDir.openTextOut listfile,
287 :     closeIt = TextIO.closeOut,
288 :     work = writeList,
289 :     cleanup = fn () =>
290 :     OS.FileSys.remove listfile
291 :     handle _ => () };
292 :     copyTextFile (SrcPath.osstring initgspec,
293 :     cmifile);
294 :     Say.say ["Runtime System PID is: ",
295 :     rtspid, "\n"])
296 :     else ();
297 :     true
298 : blume 349 end
299 : blume 449 else false
300 : blume 349 end
301 : blume 449 in
302 : blume 450 SOME ((g, gp), thunk)
303 : blume 399 end
304 : blume 449 end handle Option => (Compile.reset (); NONE)
305 : blume 330 (* to catch valOf failures in "rt" *)
306 : blume 327 in
307 : blume 329 case BuildInitDG.build ginfo_nocore initgspec of
308 : blume 449 SOME x => mk_main_compile x
309 :     | NONE => NONE
310 : blume 327 end
311 : blume 362
312 : blume 449 fun compile deliver dbopt =
313 : blume 450 case mk_compile deliver dbopt of
314 :     NONE => false
315 : blume 451 | SOME (_, thunk) => thunk () before Servers.reset ()
316 : blume 449
317 :     local
318 :     fun slave dirbase =
319 :     case mk_compile false (SOME dirbase) of
320 :     NONE => NONE
321 : blume 450 | SOME ((g, gp), _) => let
322 : blume 449 val trav = Compile.newSbnodeTraversal () gp
323 :     fun trav' sbn = isSome (trav sbn)
324 :     in
325 : blume 451 SOME (g, trav', Compile.evict)
326 : blume 449 end
327 :     in
328 :     val _ = CMBSlaveHook.init slave
329 :     end
330 :    
331 : blume 362 fun reset () =
332 : blume 398 (Compile.reset ();
333 : blume 367 Parse.reset ())
334 : blume 377
335 :     val make' = compile false
336 :     fun make () = make' NONE
337 :     fun deliver' arg =
338 :     SafeIO.perform { openIt = fn () => (),
339 :     closeIt = reset,
340 :     work = fn () => compile true arg,
341 :     cleanup = fn () => () }
342 :     fun deliver () = deliver' NONE
343 : blume 434 val symval = SSV.symval
344 : blume 327 end

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