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 879 - (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 : blume 879 structure EM = ErrorMsg
11 :     structure E = Environment
12 :     structure SE = StaticEnv
13 :     structure PS = PersStamps
14 : blume 569 structure GG = GroupGraph
15 :     structure DG = DependencyGraph
16 :     in
17 : blume 578 functor BootstrapCompileFn
18 : blume 879 (structure Backend : BACKEND
19 : blume 677 val useStream : TextIO.instream -> unit
20 : blume 578 val os : SMLofNJ.SysInfo.os_kind
21 : blume 734 val load_plugin : SrcPath.dir -> string -> bool) =
22 :     struct
23 : blume 879 structure SSV = SpecificSymValFn (val arch = Backend.architecture
24 : blume 336 val os = os)
25 : blume 364 structure P = OS.Path
26 :     structure F = OS.FileSys
27 : blume 879 structure BF = Binfile
28 : blume 327
29 : blume 879 val arch = Backend.architecture
30 : blume 452 val osname = FilenamePolicy.kind2name os
31 : blume 642
32 : blume 452 val archos = concat [arch, "-", osname]
33 :    
34 : blume 588 structure StabModmap = StabModmapFn ()
35 :    
36 : blume 879 structure Compile = CompileFn (structure Backend = Backend
37 : blume 588 structure StabModmap = StabModmap
38 : blume 677 val useStream = useStream
39 : blume 464 val compile_there =
40 : blume 666 Servers.compile o SrcPath.encode)
41 : blume 360
42 : blume 879 structure BFC = BfcFn (val arch = Backend.architecture)
43 : blume 403
44 : blume 327 (* instantiate Stabilize... *)
45 : blume 329 structure Stabilize =
46 : blume 879 StabilizeFn (val arch = Backend.architecture
47 : blume 588 structure StabModmap = StabModmap
48 : blume 399 fun recomp gp g = let
49 : blume 403 val { store, get } = BFC.new ()
50 : blume 801 fun dummy _ _ = ()
51 : blume 399 val { group, ... } =
52 : blume 801 Compile.newTraversal (dummy, store, g)
53 : blume 399 in
54 : blume 801 case group gp of
55 : blume 403 NONE => NONE
56 :     | SOME _ => SOME get
57 :     end
58 :     val getII = Compile.getII)
59 : blume 398
60 : blume 569 structure VerifyStable = VerStabFn (structure Stabilize = Stabilize)
61 :    
62 : blume 327 (* ... and Parse *)
63 : blume 362 structure Parse = ParseFn (structure Stabilize = Stabilize
64 : blume 588 structure StabModmap = StabModmap
65 : blume 537 val evictStale = Compile.evictStale
66 : blume 372 fun pending () = SymbolMap.empty)
67 : blume 327
68 : blume 537 fun mkBootList g = let
69 :     fun listName p =
70 :     case P.fromString p of
71 :     { vol = "", isAbs = false, arcs = _ :: arc1 :: arcn } => let
72 :     fun win32name () =
73 :     concat (arc1 ::
74 :     foldr (fn (a, r) => "\\" :: a :: r) [] arcn)
75 :     in
76 :     case os of
77 :     SMLofNJ.SysInfo.WIN32 => win32name ()
78 :     | _ => P.toString { isAbs = false, vol = "",
79 :     arcs = arc1 :: arcn }
80 :     end
81 :     | _ => raise Fail ("BootstrapCompile:listName: bad name: " ^ p)
82 : blume 364 in
83 : blume 537 MkBootList.group listName g
84 : blume 364 end
85 : blume 327
86 : blume 805 fun internal_reset () =
87 :     (Compile.reset ();
88 :     Parse.reset ();
89 :     StabModmap.reset ())
90 :    
91 :     fun reset () =
92 :     (Say.vsay ["[CMB reset]\n"];
93 :     Servers.withServers (fn () => Servers.cmb_reset { archos = archos });
94 :     internal_reset ())
95 :    
96 :     val checkDirbase = let
97 :     val prev = ref NONE
98 :     fun ck db =
99 :     (case !prev of
100 :     NONE => prev := SOME db
101 :     | SOME db' =>
102 :     if db = db' then ()
103 :     else (Say.vsay ["[new dirbase is `", db,
104 :     "'; CMB reset]\n"];
105 :     internal_reset ();
106 :     prev := SOME db))
107 : blume 588 in
108 : blume 805 ck
109 : blume 588 end
110 :    
111 : blume 801 fun mk_compile { master, root, dirbase = dbopt } = let
112 : blume 358
113 : blume 360 val dirbase = getOpt (dbopt, BtNames.dirbaseDefault)
114 : blume 588 val _ = checkDirbase dirbase
115 : blume 666 val penvspec = BtNames.penvspec
116 : blume 360 val initgspec = BtNames.initgspec
117 :     val maingspec = BtNames.maingspec
118 :    
119 : blume 537 val bindir = concat [dirbase, BtNames.bin_infix, archos]
120 :     val bootdir = concat [dirbase, BtNames.boot_infix, archos]
121 : blume 357
122 : blume 433 val keep_going = #get StdConfig.keep_going ()
123 : blume 329
124 : blume 666 val ctxt = SrcPath.cwd ()
125 : blume 329
126 : blume 537 val listfile = P.joinDirFile { dir = bootdir, file = BtNames.bootlist }
127 :     val pidmapfile = P.joinDirFile { dir = bootdir, file = BtNames.pidmap }
128 : blume 329
129 : blume 666 val penv = SrcPath.newEnv ()
130 : blume 735 val _ = SafeIO.perform { openIt = fn () => TextIO.openIn penvspec,
131 :     closeIt = TextIO.closeIn,
132 :     work = SrcPath.processSpecFile
133 :     { env = penv, specfile = penvspec,
134 :     say = Say.say },
135 :     cleanup = fn _ => () }
136 : blume 676 val _ = SrcPath.sync ()
137 : blume 329
138 : blume 666 fun stdpath s =
139 :     SrcPath.file (SrcPath.standard
140 :     { err = fn s => raise Fail s, env = penv }
141 :     { 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 666 | SOME r => SrcPath.decode penv 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 592 val param =
154 : blume 537 { fnpolicy = fnpolicy,
155 : blume 666 penv = penv,
156 : blume 433 symval = SSV.symval,
157 : blume 873 archos = archos,
158 : blume 592 keep_going = keep_going }
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 :     val groupreg = GroupReg.new ()
166 :     val errcons = EM.defaultConsumer ()
167 : blume 692 val ginfo = { param = param, groupreg = groupreg,
168 :     errcons = errcons,
169 :     youngest = ref TStamp.ancient }
170 : blume 327
171 : blume 449 fun mk_main_compile arg = let
172 : blume 450
173 : blume 592 val { pervasive = perv_n, others, src } = arg
174 : blume 327
175 : blume 569 fun recompInitGroup () = let
176 : blume 879 val ovldR = Control.overloadKW
177 : blume 569 val savedOvld = !ovldR
178 :     val _ = ovldR := true
179 :     val sbnode = Compile.newSbnodeTraversal ()
180 : blume 329
181 : blume 569 val perv_fsbnode = (NONE, perv_n)
182 : blume 327
183 : blume 771 fun rt n = valOf (sbnode n ginfo)
184 : blume 569 val pervasive = rt perv_n
185 : blume 537
186 : blume 569 fun rt2ie (n, ii: IInfo.info) = let
187 : blume 587 val s = #statenv ii ()
188 :     val (dae, mkDomain) = Statenv2DAEnv.cvt s
189 : blume 652 val domain = mkDomain ()
190 : blume 569 in
191 : blume 652 { ie = (fn () => (NONE, n), dae, domain), domain = domain }
192 : blume 569 end
193 : blume 537
194 : blume 569 fun add_exports (n, exports) = let
195 : blume 652 val { ie, domain } = rt2ie (n, rt n)
196 : blume 569 fun ins_ie (sy, m) = SymbolMap.insert (m, sy, ie)
197 :     in
198 : blume 652 SymbolSet.foldl ins_ie exports domain
199 : blume 569 end
200 :    
201 :     val special_exports = let
202 :     fun mkie (n, rtn) = #ie (rt2ie (n, rtn))
203 :     in
204 : blume 592 SymbolMap.insert (SymbolMap.empty,
205 :     PervAccess.pervStrSym,
206 :     mkie (perv_n, pervasive))
207 : blume 569 end
208 : blume 537 in
209 : blume 592 GG.GROUP { exports = foldl add_exports special_exports others,
210 : blume 632 kind = GG.LIB {
211 :     kind = GG.DEVELOPED { wrapped = StringSet.empty,
212 : blume 592 subgroups = [] },
213 : blume 632 version = NONE },
214 : blume 592 required = StringSet.singleton "primitive",
215 :     grouppath = initgspec,
216 : blume 642 (* hack: sources never used for this group *)
217 :     sources = SrcPathMap.empty,
218 : blume 592 sublibs = [] }
219 : blume 569 before (ovldR := savedOvld)
220 : blume 537 end
221 : blume 327
222 : blume 569 (* just go and load the stable init group or signal failure *)
223 :     fun loadInitGroup () = let
224 :     val lsarg =
225 :     { getGroup = fn _ => raise Fail "CMB: initial getGroup",
226 :     anyerrors = ref false }
227 : blume 537 in
228 : blume 666 case Stabilize.loadStable lsarg (ginfo, initgspec, NONE, []) of
229 : blume 569 NONE => NONE
230 : blume 592 | SOME (g as GG.GROUP { exports, ... }) => SOME g
231 : blume 587 | SOME GG.ERRORGROUP => NONE
232 : blume 537 end
233 : blume 569
234 :     (* Don't try to load the stable init group. Instead, recompile
235 :     * directly. *)
236 :     fun dontLoadInitGroup () = let
237 : blume 771 (* Function recompileInitGroup will not use servers (hence no
238 :     * call to Servers.withServers), but since compile traversals
239 :     * invoke the scheduler anyway, we must still clear pending
240 :     * tasks when we hit an error or an interrupt. *)
241 :     val g0 = SafeIO.perform { openIt = fn () => (),
242 :     closeIt = fn () => (),
243 :     work = recompInitGroup,
244 :     cleanup = Servers.reset }
245 : blume 759 val stabarg = { group = g0, anyerrors = ref false,
246 :     rebindings = [] }
247 : blume 569 in
248 : blume 801 if master then
249 : blume 592 case Stabilize.stabilize ginfo stabarg of
250 : blume 805 SOME g => (Parse.reset (); g)
251 : blume 569 | NONE => raise Fail "CMB: cannot stabilize init group"
252 : blume 592 else g0
253 : blume 569 end
254 : blume 327
255 : blume 569 (* Try loading the init group from the stable file if possible;
256 :     * recompile if loading fails *)
257 :     fun tryLoadInitGroup () =
258 :     case loadInitGroup () of
259 :     SOME g => g
260 :     | NONE => dontLoadInitGroup ()
261 :    
262 :     (* Ok, now, based on "paranoid" and stable verification,
263 :     * call the appropriate function(s) to get the init group. *)
264 : blume 592 val init_group =
265 : blume 801 if master then let
266 : blume 592 val export_nodes = perv_n :: others
267 : blume 569 val ver_arg = (initgspec, export_nodes, [],
268 : blume 632 SrcPathSet.empty, NONE)
269 : blume 569 val em = StableMap.empty
270 : blume 537 in
271 : blume 592 if VerifyStable.verify' ginfo em ver_arg then
272 : blume 569 tryLoadInitGroup ()
273 :     else dontLoadInitGroup ()
274 : blume 537 end
275 : blume 801 else valOf (loadInitGroup ()) (* failure caught at the end *)
276 : blume 356
277 : blume 537 val gr = GroupReg.new ()
278 :     val _ = GroupReg.register gr (initgspec, src)
279 :    
280 : blume 801 fun parse_arg (s, p) =
281 : blume 537 { load_plugin = load_plugin,
282 :     gr = gr,
283 :     param = param,
284 : blume 801 stabflag = s,
285 : blume 537 group = maingspec,
286 :     init_group = init_group,
287 : blume 801 paranoid = p }
288 :    
289 :     val lonely_master = master andalso Servers.noServers ()
290 :    
291 : blume 805 val initial_parse_result =
292 :     if master then
293 :     if lonely_master then
294 : blume 854 (* no slaves available; do everything alone
295 :     * (Still wrap "withServers" around it to make sure
296 :     * our queues get cleaned when an interrupt or error
297 :     * occurs.) *)
298 :     Servers.withServers
299 :     (fn () => Parse.parse (parse_arg (SOME true, true)))
300 : blume 805 else
301 :     (* slaves available; we want master
302 :     * and slave initialization to overlap, so
303 :     * we do the master's parsing in its own
304 :     * thread *)
305 :     let fun worker () = let
306 :     val c =
307 :     Concur.fork
308 :     (fn () => Parse.parse
309 :     (parse_arg (NONE, true)))
310 :     in
311 :     Servers.cmb
312 :     { dirbase = dirbase,
313 :     archos = archos,
314 :     root = SrcPath.encode maingspec };
315 :     Concur.wait c
316 :     end
317 :     in
318 :     Servers.withServers worker
319 :     end
320 :     else
321 :     (* slave case *)
322 :     Parse.parse (parse_arg (NONE, false))
323 : blume 327 in
324 : blume 805 case initial_parse_result of
325 : blume 449 NONE => NONE
326 : blume 399 | SOME (g, gp) => let
327 : blume 801 fun finish (g, gp) = let
328 :     val { l = bootitems, ss } = mkBootList g
329 :     val stablelibs = Reachable.stableLibsOf g
330 :     fun inSet bi = StableSet.member (ss, bi)
331 :     val frontiers =
332 :     SrcPathMap.map (Reachable.frontier inSet)
333 :     stablelibs
334 :     fun writeBootList s = let
335 :     fun wr str = TextIO.output (s, str ^ "\n")
336 :     val numitems = length bootitems
337 :     fun biggerlen (s, n) = Int.max (size s, n)
338 :     val maxlen = foldl biggerlen 0 bootitems
339 : blume 349 in
340 : blume 801 wr (concat ["%", Int.toString numitems,
341 :     " ", Int.toString maxlen]);
342 :     app wr bootitems
343 : blume 349 end
344 : blume 801 fun writePid s i = let
345 :     val sn = BinInfo.stablename i
346 :     val os = BinInfo.offset i
347 :     val descr = BinInfo.describe i
348 :     val bfc = BFC.getStable { stable = sn, offset = os,
349 :     descr = descr }
350 :     in
351 :     case BF.exportPidOf bfc of
352 :     NONE => ()
353 :     | SOME pid =>
354 :     app (fn str => TextIO.output (s, str))
355 :     [" ", Int.toString os, ":", PS.toHex pid]
356 :     end
357 :     fun writePidLine s (p, set) =
358 :     if StableSet.isEmpty set then ()
359 :     else (TextIO.output (s, SrcPath.encode p);
360 :     StableSet.app (writePid s) set;
361 :     TextIO.output (s, "\n"))
362 :     fun writePidMap s =
363 :     SrcPathMap.appi (writePidLine s) frontiers
364 :     in
365 :     SafeIO.perform
366 :     { openIt = fn () => AutoDir.openTextOut listfile,
367 :     closeIt = TextIO.closeOut,
368 :     work = writeBootList,
369 :     cleanup = fn _ => (OS.FileSys.remove listfile
370 :     handle _ => ()) };
371 :     SafeIO.perform
372 :     { openIt = fn () => AutoDir.openTextOut pidmapfile,
373 :     closeIt = TextIO.closeOut,
374 :     work = writePidMap,
375 :     cleanup = fn _ => (OS.FileSys.remove pidmapfile
376 :     handle _ => ()) };
377 :     Say.say ["New boot directory has been built.\n"];
378 :     true
379 :     end
380 :    
381 :     (* the following thunk represents phase 2 (stabilization)
382 :     * of the master's execution path; it is never
383 :     * executed in slave mode *)
384 :     fun stabilize () =
385 :     (* now we re-parse everything with stabilization
386 :     * turnedon (and servers turned off *)
387 :     case Parse.parse (parse_arg (SOME true, false)) of
388 :     NONE => false
389 :     | SOME (g, gp) => finish (g, gp)
390 :    
391 :     (* Don't do another traversal if this is a lonely master *)
392 :     fun just_stabilize () = finish (g, gp)
393 :    
394 :     (* the following thunk is executed in "master" mode only;
395 :     * slaves just throw it away *)
396 :     fun compile_and_stabilize () = let
397 :    
398 :     (* make compilation traversal and execute it *)
399 :     val { allgroups, ... } =
400 :     Compile.newTraversal (fn _ => fn _ => (),
401 :     fn _ => (),
402 :     g)
403 :     in
404 :     if Servers.withServers (fn () => allgroups gp) then
405 :     (Compile.reset ();
406 :     stabilize ())
407 : blume 449 else false
408 : blume 349 end
409 : blume 449 in
410 : blume 801 SOME ((g, gp, penv),
411 :     if lonely_master then just_stabilize
412 :     else compile_and_stabilize)
413 : blume 399 end
414 : blume 449 end handle Option => (Compile.reset (); NONE)
415 : blume 801 (* to catch valOf failures in "rt" or slave's failure
416 :     * to load init group *)
417 : blume 327 in
418 : blume 592 case BuildInitDG.build ginfo initgspec of
419 : blume 449 SOME x => mk_main_compile x
420 :     | NONE => NONE
421 : blume 327 end
422 : blume 362
423 : blume 569 fun compile dbopt =
424 : blume 632 (StabModmap.reset ();
425 : blume 801 case mk_compile { master = true, root = NONE, dirbase = dbopt } of
426 : blume 632 NONE => false
427 : blume 805 | SOME (_, thunk) => thunk ())
428 : blume 449
429 :     local
430 : blume 805 fun slave NONE = (internal_reset (); NONE)
431 : blume 632 | slave (SOME (dirbase, root)) =
432 : blume 805 (StabModmap.reset ();
433 :     case mk_compile { master = false, root = SOME root,
434 :     dirbase = SOME dirbase } of
435 :     NONE => NONE
436 :     | SOME ((g, gp, penv), _) => let
437 :     val trav = Compile.newSbnodeTraversal ()
438 :     fun trav' sbn = isSome (trav sbn gp)
439 :     in
440 :     SOME (g, trav', penv)
441 :     end)
442 : blume 449 in
443 : blume 452 val _ = CMBSlaveHook.init archos slave
444 : blume 449 end
445 :    
446 : blume 569 val make' = compile
447 : blume 377 fun make () = make' NONE
448 : blume 434 val symval = SSV.symval
449 : blume 327 end
450 : blume 569 end (* local *)

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