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 588 - (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 569 local
10 :     structure EM = GenericVC.ErrorMsg
11 :     structure E = GenericVC.Environment
12 : blume 587 structure SE = GenericVC.StaticEnv
13 : blume 569 structure PS = GenericVC.PersStamps
14 :     structure GG = GroupGraph
15 :     structure DG = DependencyGraph
16 :     in
17 : blume 578 functor BootstrapCompileFn
18 :     (structure MachDepVC : MACHDEP_VC
19 :     val os : SMLofNJ.SysInfo.os_kind
20 :     val load_plugin : SrcPath.context -> string -> bool) :> sig
21 : blume 364 val make' : string option -> bool
22 :     val make : unit -> bool
23 : blume 362 val reset : unit -> unit
24 : blume 434 val symval : string -> { get: unit -> int option, set: int option -> unit }
25 : blume 357 end = struct
26 : blume 336 structure SSV = SpecificSymValFn (structure MachDepVC = MachDepVC
27 :     val os = os)
28 : blume 364 structure P = OS.Path
29 :     structure F = OS.FileSys
30 : blume 403 structure BF = MachDepVC.Binfile
31 : blume 327
32 : blume 452 val arch = MachDepVC.architecture
33 :     val osname = FilenamePolicy.kind2name os
34 :     val archos = concat [arch, "-", osname]
35 :    
36 : blume 569 fun init_servers (GG.GROUP { grouppath, ... }) =
37 : blume 464 Servers.cmb { archos = archos,
38 :     root = SrcPath.descr grouppath }
39 : blume 587 | init_servers GG.ERRORGROUP = ()
40 : blume 456
41 : blume 588 structure StabModmap = StabModmapFn ()
42 :    
43 : blume 448 structure Compile = CompileFn (structure MachDepVC = MachDepVC
44 : blume 588 structure StabModmap = StabModmap
45 : blume 464 val compile_there =
46 :     Servers.compile o SrcPath.descr)
47 : blume 360
48 : blume 403 structure BFC = BfcFn (structure MachDepVC = MachDepVC)
49 :    
50 : blume 327 (* instantiate Stabilize... *)
51 : blume 329 structure Stabilize =
52 : blume 537 StabilizeFn (structure MachDepVC = MachDepVC
53 : blume 588 structure StabModmap = StabModmap
54 : blume 399 fun recomp gp g = let
55 : blume 403 val { store, get } = BFC.new ()
56 : blume 456 val _ = init_servers g
57 : blume 399 val { group, ... } =
58 : blume 403 Compile.newTraversal (fn _ => fn _ => (),
59 :     store, g)
60 : blume 399 in
61 : blume 450 case Servers.withServers (fn () => group gp) of
62 : blume 403 NONE => NONE
63 :     | SOME _ => SOME get
64 :     end
65 :     val getII = Compile.getII)
66 : blume 398
67 : blume 569 structure VerifyStable = VerStabFn (structure Stabilize = Stabilize)
68 :    
69 : blume 327 (* ... and Parse *)
70 : blume 362 structure Parse = ParseFn (structure Stabilize = Stabilize
71 : blume 588 structure StabModmap = StabModmap
72 : blume 537 val evictStale = Compile.evictStale
73 : blume 372 fun pending () = SymbolMap.empty)
74 : blume 327
75 : blume 537 fun mkBootList g = let
76 :     fun listName p =
77 :     case P.fromString p of
78 :     { vol = "", isAbs = false, arcs = _ :: arc1 :: arcn } => let
79 :     fun win32name () =
80 :     concat (arc1 ::
81 :     foldr (fn (a, r) => "\\" :: a :: r) [] arcn)
82 :     in
83 :     case os of
84 :     SMLofNJ.SysInfo.WIN32 => win32name ()
85 :     | _ => P.toString { isAbs = false, vol = "",
86 :     arcs = arc1 :: arcn }
87 :     end
88 :     | _ => raise Fail ("BootstrapCompile:listName: bad name: " ^ p)
89 : blume 364 in
90 : blume 537 MkBootList.group listName g
91 : blume 364 end
92 : blume 327
93 : blume 588 local
94 :     fun internal_reset () =
95 :     (Compile.reset ();
96 :     Parse.reset ();
97 :     StabModmap.reset ())
98 :     in
99 :     fun reset () =
100 :     (Say.vsay ["[CMB reset]\n"];
101 :     internal_reset ())
102 :     val checkDirbase = let
103 :     val prev = ref NONE
104 :     fun ck db =
105 :     (case !prev of
106 :     NONE => prev := SOME db
107 :     | SOME db' =>
108 :     if db = db' then ()
109 :     else (Say.vsay ["[new dirbase is `", db,
110 :     "'; CMB reset]\n"];
111 :     internal_reset ();
112 :     prev := SOME db))
113 :     in
114 :     ck
115 :     end
116 :     end
117 :    
118 : blume 569 fun mk_compile { deliver, root, dirbase = dbopt, paranoid } = let
119 : blume 358
120 : blume 588 val _ = StabModmap.reset ()
121 :    
122 : blume 360 val dirbase = getOpt (dbopt, BtNames.dirbaseDefault)
123 : blume 588 val _ = checkDirbase dirbase
124 : blume 360 val pcmodespec = BtNames.pcmodespec
125 :     val initgspec = BtNames.initgspec
126 :     val maingspec = BtNames.maingspec
127 :    
128 : blume 537 val bindir = concat [dirbase, BtNames.bin_infix, archos]
129 :     val bootdir = concat [dirbase, BtNames.boot_infix, archos]
130 : blume 357
131 : blume 433 val keep_going = #get StdConfig.keep_going ()
132 : blume 329
133 : blume 354 val ctxt = SrcPath.cwdContext ()
134 : blume 329
135 : blume 537 val listfile = P.joinDirFile { dir = bootdir, file = BtNames.bootlist }
136 :     val pidmapfile = P.joinDirFile { dir = bootdir, file = BtNames.pidmap }
137 : blume 329
138 : blume 361 val pcmode = PathConfig.new ()
139 :     val _ = PathConfig.processSpecFile (pcmode, pcmodespec)
140 : blume 329
141 : blume 354 fun stdpath s = SrcPath.standard pcmode { context = ctxt, spec = s }
142 : blume 352
143 :     val initgspec = stdpath initgspec
144 : blume 456 val maingspec =
145 :     case root of
146 :     NONE => stdpath maingspec
147 : blume 457 | SOME r => SrcPath.fromDescr pcmode r
148 : blume 352
149 : blume 364 val fnpolicy =
150 : blume 357 FilenamePolicy.separate { bindir = bindir, bootdir = bootdir }
151 :     { arch = arch, os = os }
152 :    
153 : blume 537 fun mkParam corenv =
154 :     { fnpolicy = fnpolicy,
155 : blume 349 pcmode = pcmode,
156 : blume 433 symval = SSV.symval,
157 : blume 349 keep_going = keep_going,
158 : blume 537 corenv = corenv }
159 : blume 349
160 : blume 327 val emptydyn = E.dynamicPart E.emptyEnv
161 :    
162 :     (* first, build an initial GeneralParam.info, so we can
163 :     * deal with the pervasive env and friends... *)
164 :    
165 : blume 587 val param_nocore = mkParam E.emptyEnv
166 : blume 327
167 :     val groupreg = GroupReg.new ()
168 :     val errcons = EM.defaultConsumer ()
169 : blume 329 val ginfo_nocore = { param = param_nocore, groupreg = groupreg,
170 :     errcons = errcons }
171 : blume 327
172 : blume 449 fun mk_main_compile arg = let
173 : blume 450
174 : blume 537 val { core = core_n, pervasive = perv_n, others, src } = arg
175 : blume 327
176 : blume 569 fun recompInitGroup () = let
177 :     val ovldR = GenericVC.Control.overloadKW
178 :     val savedOvld = !ovldR
179 :     val _ = ovldR := true
180 :     val sbnode = Compile.newSbnodeTraversal ()
181 : blume 329
182 : blume 569 (* here we build a new gp -- the one that uses the freshly
183 :     * brewed pervasive env, core env, and primitives *)
184 :     val core = valOf (sbnode ginfo_nocore core_n)
185 :     val corenv =
186 : blume 587 E.mkenv { static = #statenv core (),
187 :     symbolic = #symenv core (),
188 :     dynamic = emptydyn }
189 : blume 329
190 : blume 569 (* The following is a bit of a hack (but corenv is a hack
191 :     * anyway): As soon as we have core available, we have to
192 :     * patch the ginfo to include the correct corenv (because
193 :     * virtually everybody else needs access to corenv). *)
194 :     val param = mkParam corenv
195 :     val ginfo =
196 :     { param = param, groupreg = groupreg, errcons = errcons }
197 : blume 329
198 : blume 569 val perv_fsbnode = (NONE, perv_n)
199 : blume 327
200 : blume 569 fun rt n = valOf (sbnode ginfo n)
201 :     val pervasive = rt perv_n
202 : blume 537
203 : blume 569 fun rt2ie (n, ii: IInfo.info) = let
204 : blume 587 val s = #statenv ii ()
205 :     val (dae, mkDomain) = Statenv2DAEnv.cvt s
206 : blume 569 in
207 : blume 587 (* Link path info = NONE, will be reset at import
208 :     * time (in members.sml). *)
209 : blume 569 { ie = ((NONE, n), dae), mkDomain = mkDomain }
210 :     end
211 : blume 537
212 : blume 569 fun add_exports (n, exports) = let
213 :     val { ie, mkDomain } = rt2ie (n, rt n)
214 :     fun ins_ie (sy, m) = SymbolMap.insert (m, sy, ie)
215 :     in
216 :     SymbolSet.foldl ins_ie exports (mkDomain ())
217 :     end
218 :    
219 :     val special_exports = let
220 :     fun mkie (n, rtn) = #ie (rt2ie (n, rtn))
221 :     in
222 :     foldl SymbolMap.insert' SymbolMap.empty
223 :     [(PervCoreAccess.pervStrSym, mkie (perv_n, pervasive)),
224 :     (PervCoreAccess.coreStrSym, mkie (core_n, core))]
225 :     end
226 : blume 537 in
227 : blume 569 (GG.GROUP { exports = foldl add_exports special_exports others,
228 : blume 573 kind = GroupGraph.LIB { wrapped = StringSet.empty,
229 :     subgroups = [] },
230 : blume 569 required = StringSet.singleton "primitive",
231 :     grouppath = initgspec,
232 :     sublibs = [] },
233 :     corenv)
234 :     before (ovldR := savedOvld)
235 : blume 537 end
236 : blume 327
237 : blume 569 (* just go and load the stable init group or signal failure *)
238 :     fun loadInitGroup () = let
239 :     val coresym = PervCoreAccess.coreStrSym
240 :     val lsarg =
241 :     { getGroup = fn _ => raise Fail "CMB: initial getGroup",
242 :     anyerrors = ref false }
243 : blume 537 in
244 : blume 569 case Stabilize.loadStable ginfo_nocore lsarg initgspec of
245 :     NONE => NONE
246 :     | SOME (g as GG.GROUP { exports, ... }) =>
247 :     (case SymbolMap.find (exports, coresym) of
248 :     SOME ((_, DG.SB_BNODE (_, ii)), _) => let
249 : blume 587 val stat = #statenv ii ()
250 : blume 569 val sym = #symenv ii ()
251 :     val corenv =
252 : blume 587 E.mkenv { static = stat,
253 :     symbolic = sym,
254 :     dynamic = emptydyn }
255 : blume 569 in
256 :     SOME (g, corenv)
257 :     end
258 :     | _ => NONE)
259 : blume 587 | SOME GG.ERRORGROUP => NONE
260 : blume 537 end
261 : blume 569
262 :     (* Don't try to load the stable init group. Instead, recompile
263 :     * directly. *)
264 :     fun dontLoadInitGroup () = let
265 :     val (g0, corenv) = recompInitGroup ()
266 :     val stabarg = { group = g0, anyerrors = ref false }
267 :     in
268 :     if deliver then
269 :     case Stabilize.stabilize ginfo_nocore stabarg of
270 :     SOME g => (g, corenv)
271 :     | NONE => raise Fail "CMB: cannot stabilize init group"
272 :     else (g0, corenv)
273 :     end
274 : blume 327
275 : blume 569 (* Try loading the init group from the stable file if possible;
276 :     * recompile if loading fails *)
277 :     fun tryLoadInitGroup () =
278 :     case loadInitGroup () of
279 :     SOME g => g
280 :     | NONE => dontLoadInitGroup ()
281 :    
282 :     (* Ok, now, based on "paranoid" and stable verification,
283 :     * call the appropriate function(s) to get the init group. *)
284 :     val (init_group, corenv) =
285 :     if paranoid then let
286 :     val export_nodes = core_n :: perv_n :: others
287 :     val ver_arg = (initgspec, export_nodes, [],
288 :     SrcPathSet.empty)
289 :     val em = StableMap.empty
290 : blume 537 in
291 : blume 569 if VerifyStable.verify' ginfo_nocore em ver_arg then
292 :     tryLoadInitGroup ()
293 :     else dontLoadInitGroup ()
294 : blume 537 end
295 : blume 569 else tryLoadInitGroup ()
296 : blume 356
297 : blume 569 (* now we finally build the real param and ginfo that we can
298 :     * use throughout the rest... *)
299 :     val param = mkParam corenv
300 :     val ginfo =
301 :     { param = param, errcons = errcons, groupreg = groupreg }
302 :    
303 :     val stab = if deliver then SOME true else NONE
304 :    
305 : blume 537 val gr = GroupReg.new ()
306 :     val _ = GroupReg.register gr (initgspec, src)
307 :    
308 :     val parse_arg =
309 :     { load_plugin = load_plugin,
310 :     gr = gr,
311 :     param = param,
312 :     stabflag = stab,
313 :     group = maingspec,
314 :     init_group = init_group,
315 : blume 569 paranoid = paranoid }
316 : blume 327 in
317 : blume 456 Servers.dirbase dirbase;
318 : blume 537 case Parse.parse parse_arg of
319 : blume 449 NONE => NONE
320 : blume 399 | SOME (g, gp) => let
321 : blume 449 fun thunk () = let
322 : blume 456 val _ = init_servers g
323 : blume 449 fun store _ = ()
324 :     val { group = recomp, ... } =
325 :     Compile.newTraversal (fn _ => fn _ => (), store, g)
326 : blume 450 val res =
327 :     Servers.withServers (fn () => recomp gp)
328 : blume 449 in
329 : blume 450 if isSome res then let
330 : blume 537 val { l = bootitems, ss } = mkBootList g
331 :     val stablelibs = Reachable.stableLibsOf g
332 :     fun inSet bi = StableSet.member (ss, bi)
333 :     val frontiers =
334 :     SrcPathMap.map (Reachable.frontier inSet)
335 :     stablelibs
336 :     fun writeBootList s = let
337 :     fun wr str = TextIO.output (s, str ^ "\n")
338 : blume 569 val numitems = length bootitems
339 :     fun biggerlen (s, n) = Int.max (size s, n)
340 :     val maxlen = foldl biggerlen 0 bootitems
341 : blume 364 in
342 : blume 569 wr (concat ["%", Int.toString numitems,
343 :     " ", Int.toString maxlen]);
344 : blume 537 app wr bootitems
345 : blume 364 end
346 : blume 537 fun writePid s i = let
347 :     val sn = BinInfo.stablename i
348 :     val os = BinInfo.offset i
349 :     val descr = BinInfo.describe i
350 :     val bfc = BFC.getStable
351 :     { stable = sn, offset = os, descr = descr }
352 :     in
353 :     case BF.exportPidOf bfc of
354 :     NONE => ()
355 :     | SOME pid =>
356 :     (TextIO.output (s, " ");
357 :     TextIO.output (s, PS.toHex pid))
358 :     end
359 :     fun writePidLine s (p, set) =
360 :     if StableSet.isEmpty set then ()
361 :     else (TextIO.output (s, SrcPath.descr p);
362 :     StableSet.app (writePid s) set;
363 :     TextIO.output (s, "\n"))
364 :     fun writePidMap s =
365 :     SrcPathMap.appi (writePidLine s) frontiers
366 : blume 349 in
367 : blume 449 if deliver then
368 :     (SafeIO.perform
369 :     { openIt = fn () =>
370 : blume 537 AutoDir.openTextOut listfile,
371 : blume 449 closeIt = TextIO.closeOut,
372 : blume 537 work = writeBootList,
373 : blume 459 cleanup = fn _ =>
374 : blume 537 OS.FileSys.remove listfile
375 : blume 449 handle _ => () };
376 :     SafeIO.perform
377 :     { openIt = fn () =>
378 : blume 537 AutoDir.openTextOut pidmapfile,
379 : blume 449 closeIt = TextIO.closeOut,
380 : blume 537 work = writePidMap,
381 : blume 459 cleanup = fn _ =>
382 : blume 537 OS.FileSys.remove pidmapfile
383 : blume 449 handle _ => () };
384 : blume 537 Say.say
385 :     ["New boot directory has been built.\n"])
386 : blume 449 else ();
387 :     true
388 : blume 349 end
389 : blume 449 else false
390 : blume 349 end
391 : blume 449 in
392 : blume 457 SOME ((g, gp, pcmode), thunk)
393 : blume 399 end
394 : blume 449 end handle Option => (Compile.reset (); NONE)
395 : blume 330 (* to catch valOf failures in "rt" *)
396 : blume 327 in
397 : blume 329 case BuildInitDG.build ginfo_nocore initgspec of
398 : blume 449 SOME x => mk_main_compile x
399 :     | NONE => NONE
400 : blume 327 end
401 : blume 362
402 : blume 569 fun compile dbopt =
403 :     case mk_compile { deliver = true, root = NONE,
404 :     dirbase = dbopt, paranoid = true } of
405 : blume 450 NONE => false
406 : blume 459 | SOME (_, thunk) => thunk ()
407 : blume 449
408 :     local
409 : blume 456 fun slave (dirbase, root) =
410 : blume 569 case mk_compile { deliver = false, root = SOME root,
411 :     dirbase = SOME dirbase, paranoid = false } of
412 : blume 449 NONE => NONE
413 : blume 457 | SOME ((g, gp, pcmode), _) => let
414 : blume 449 val trav = Compile.newSbnodeTraversal () gp
415 :     fun trav' sbn = isSome (trav sbn)
416 :     in
417 : blume 457 SOME (g, trav', pcmode)
418 : blume 449 end
419 :     in
420 : blume 452 val _ = CMBSlaveHook.init archos slave
421 : blume 449 end
422 :    
423 : blume 569 val make' = compile
424 : blume 377 fun make () = make' NONE
425 : blume 434 val symval = SSV.symval
426 : blume 327 end
427 : blume 569 end (* local *)

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