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 1137 - (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 SE = StaticEnv
12 :     structure PS = PersStamps
13 : blume 569 structure GG = GroupGraph
14 :     structure DG = DependencyGraph
15 :     in
16 : blume 578 functor BootstrapCompileFn
17 : blume 879 (structure Backend : BACKEND
18 : blume 677 val useStream : TextIO.instream -> unit
19 : blume 578 val os : SMLofNJ.SysInfo.os_kind
20 : blume 734 val load_plugin : SrcPath.dir -> string -> bool) =
21 :     struct
22 : blume 879 structure SSV = SpecificSymValFn (val arch = Backend.architecture
23 : blume 336 val os = os)
24 : blume 364 structure P = OS.Path
25 :     structure F = OS.FileSys
26 : blume 879 structure BF = Binfile
27 : blume 327
28 : blume 879 val arch = Backend.architecture
29 : blume 452 val osname = FilenamePolicy.kind2name os
30 : blume 642
31 : blume 452 val archos = concat [arch, "-", osname]
32 :    
33 : blume 588 structure StabModmap = StabModmapFn ()
34 :    
35 : blume 879 structure Compile = CompileFn (structure Backend = Backend
36 : blume 588 structure StabModmap = StabModmap
37 : blume 677 val useStream = useStream
38 : blume 464 val compile_there =
39 : blume 666 Servers.compile o SrcPath.encode)
40 : blume 360
41 : blume 879 structure BFC = BfcFn (val arch = Backend.architecture)
42 : blume 403
43 : blume 327 (* instantiate Stabilize... *)
44 : blume 329 structure Stabilize =
45 : blume 879 StabilizeFn (val arch = Backend.architecture
46 : blume 588 structure StabModmap = StabModmap
47 : blume 399 fun recomp gp g = let
48 : blume 403 val { store, get } = BFC.new ()
49 : blume 801 fun dummy _ _ = ()
50 : blume 399 val { group, ... } =
51 : blume 801 Compile.newTraversal (dummy, store, g)
52 : blume 399 in
53 : blume 801 case group gp of
54 : blume 403 NONE => NONE
55 :     | SOME _ => SOME get
56 :     end
57 :     val getII = Compile.getII)
58 : blume 398
59 : blume 569 structure VerifyStable = VerStabFn (structure Stabilize = Stabilize)
60 :    
61 : blume 327 (* ... and Parse *)
62 : blume 362 structure Parse = ParseFn (structure Stabilize = Stabilize
63 : blume 588 structure StabModmap = StabModmap
64 : blume 537 val evictStale = Compile.evictStale
65 : blume 372 fun pending () = SymbolMap.empty)
66 : blume 327
67 : blume 537 fun mkBootList g = let
68 :     fun listName p =
69 :     case P.fromString p of
70 :     { vol = "", isAbs = false, arcs = _ :: arc1 :: arcn } => let
71 :     fun win32name () =
72 :     concat (arc1 ::
73 :     foldr (fn (a, r) => "\\" :: a :: r) [] arcn)
74 :     in
75 :     case os of
76 :     SMLofNJ.SysInfo.WIN32 => win32name ()
77 :     | _ => P.toString { isAbs = false, vol = "",
78 :     arcs = arc1 :: arcn }
79 :     end
80 :     | _ => raise Fail ("BootstrapCompile:listName: bad name: " ^ p)
81 : blume 364 in
82 : blume 537 MkBootList.group listName g
83 : blume 364 end
84 : blume 327
85 : blume 805 fun internal_reset () =
86 :     (Compile.reset ();
87 :     Parse.reset ();
88 :     StabModmap.reset ())
89 :    
90 :     fun reset () =
91 :     (Say.vsay ["[CMB reset]\n"];
92 :     Servers.withServers (fn () => Servers.cmb_reset { archos = archos });
93 :     internal_reset ())
94 :    
95 :     val checkDirbase = let
96 :     val prev = ref NONE
97 :     fun ck db =
98 :     (case !prev of
99 :     NONE => prev := SOME db
100 :     | SOME db' =>
101 :     if db = db' then ()
102 :     else (Say.vsay ["[new dirbase is `", db,
103 :     "'; CMB reset]\n"];
104 :     internal_reset ();
105 :     prev := SOME db))
106 : blume 588 in
107 : blume 805 ck
108 : blume 588 end
109 :    
110 : blume 801 fun mk_compile { master, root, dirbase = dbopt } = let
111 : blume 358
112 : blume 360 val dirbase = getOpt (dbopt, BtNames.dirbaseDefault)
113 : blume 588 val _ = checkDirbase dirbase
114 : blume 666 val penvspec = BtNames.penvspec
115 : blume 360 val initgspec = BtNames.initgspec
116 :     val maingspec = BtNames.maingspec
117 :    
118 : blume 537 val bindir = concat [dirbase, BtNames.bin_infix, archos]
119 :     val bootdir = concat [dirbase, BtNames.boot_infix, archos]
120 : blume 357
121 : blume 433 val keep_going = #get StdConfig.keep_going ()
122 : blume 329
123 : blume 666 val ctxt = SrcPath.cwd ()
124 : blume 329
125 : blume 537 val listfile = P.joinDirFile { dir = bootdir, file = BtNames.bootlist }
126 :     val pidmapfile = P.joinDirFile { dir = bootdir, file = BtNames.pidmap }
127 : blume 329
128 : blume 666 val penv = SrcPath.newEnv ()
129 : blume 735 val _ = SafeIO.perform { openIt = fn () => TextIO.openIn penvspec,
130 :     closeIt = TextIO.closeIn,
131 :     work = SrcPath.processSpecFile
132 :     { env = penv, specfile = penvspec,
133 :     say = Say.say },
134 :     cleanup = fn _ => () }
135 : blume 676 val _ = SrcPath.sync ()
136 : blume 329
137 : blume 666 fun stdpath s =
138 :     SrcPath.file (SrcPath.standard
139 :     { err = fn s => raise Fail s, env = penv }
140 :     { context = ctxt, spec = s })
141 : blume 352
142 :     val initgspec = stdpath initgspec
143 : blume 456 val maingspec =
144 :     case root of
145 :     NONE => stdpath maingspec
146 : blume 666 | SOME r => SrcPath.decode penv r
147 : blume 352
148 : blume 364 val fnpolicy =
149 : blume 357 FilenamePolicy.separate { bindir = bindir, bootdir = bootdir }
150 :     { arch = arch, os = os }
151 :    
152 : blume 1058 fun param slave_mode =
153 : blume 537 { fnpolicy = fnpolicy,
154 : blume 666 penv = penv,
155 : blume 433 symval = SSV.symval,
156 : blume 873 archos = archos,
157 : blume 1058 keep_going = keep_going,
158 :     slave_mode = slave_mode }
159 : blume 349
160 : blume 905 val emptydyn = DynamicEnv.empty
161 : blume 327
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 1058 val ginfo = { param = param false, groupreg = groupreg,
168 : blume 692 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 1137 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 1058 fun parse_arg0 slave_mode (s, p) =
281 : blume 537 { load_plugin = load_plugin,
282 :     gr = gr,
283 : blume 1058 param = param slave_mode,
284 : blume 801 stabflag = s,
285 : blume 537 group = maingspec,
286 :     init_group = init_group,
287 : blume 801 paranoid = p }
288 :    
289 : blume 1058 val parse_arg = parse_arg0 false
290 :     val slave_parse_arg = parse_arg0 true
291 :    
292 : blume 801 val lonely_master = master andalso Servers.noServers ()
293 :    
294 : blume 805 val initial_parse_result =
295 :     if master then
296 :     if lonely_master then
297 : blume 854 (* no slaves available; do everything alone
298 :     * (Still wrap "withServers" around it to make sure
299 :     * our queues get cleaned when an interrupt or error
300 :     * occurs.) *)
301 :     Servers.withServers
302 : blume 1058 (fn () => Parse.parse(parse_arg (SOME true, true)))
303 : blume 805 else
304 :     (* slaves available; we want master
305 :     * and slave initialization to overlap, so
306 :     * we do the master's parsing in its own
307 :     * thread *)
308 :     let fun worker () = let
309 :     val c =
310 :     Concur.fork
311 :     (fn () => Parse.parse
312 :     (parse_arg (NONE, true)))
313 :     in
314 :     Servers.cmb
315 :     { dirbase = dirbase,
316 :     archos = archos,
317 :     root = SrcPath.encode maingspec };
318 :     Concur.wait c
319 :     end
320 :     in
321 :     Servers.withServers worker
322 :     end
323 :     else
324 :     (* slave case *)
325 : blume 1058 Parse.parse (slave_parse_arg (NONE, false))
326 : blume 327 in
327 : blume 805 case initial_parse_result of
328 : blume 449 NONE => NONE
329 : blume 399 | SOME (g, gp) => let
330 : blume 801 fun finish (g, gp) = let
331 :     val { l = bootitems, ss } = mkBootList g
332 :     val stablelibs = Reachable.stableLibsOf g
333 :     fun inSet bi = StableSet.member (ss, bi)
334 :     val frontiers =
335 :     SrcPathMap.map (Reachable.frontier inSet)
336 :     stablelibs
337 :     fun writeBootList s = let
338 :     fun wr str = TextIO.output (s, str ^ "\n")
339 :     val numitems = length bootitems
340 :     fun biggerlen (s, n) = Int.max (size s, n)
341 :     val maxlen = foldl biggerlen 0 bootitems
342 : blume 349 in
343 : blume 801 wr (concat ["%", Int.toString numitems,
344 :     " ", Int.toString maxlen]);
345 :     app wr bootitems
346 : blume 349 end
347 : blume 801 fun writePid s i = let
348 :     val sn = BinInfo.stablename i
349 :     val os = BinInfo.offset i
350 :     val descr = BinInfo.describe i
351 :     val bfc = BFC.getStable { stable = sn, offset = os,
352 :     descr = descr }
353 :     in
354 :     case BF.exportPidOf bfc of
355 :     NONE => ()
356 :     | SOME pid =>
357 :     app (fn str => TextIO.output (s, str))
358 :     [" ", Int.toString os, ":", PS.toHex pid]
359 :     end
360 :     fun writePidLine s (p, set) =
361 :     if StableSet.isEmpty set then ()
362 :     else (TextIO.output (s, SrcPath.encode p);
363 :     StableSet.app (writePid s) set;
364 :     TextIO.output (s, "\n"))
365 :     fun writePidMap s =
366 :     SrcPathMap.appi (writePidLine s) frontiers
367 :     in
368 :     SafeIO.perform
369 :     { openIt = fn () => AutoDir.openTextOut listfile,
370 :     closeIt = TextIO.closeOut,
371 :     work = writeBootList,
372 :     cleanup = fn _ => (OS.FileSys.remove listfile
373 :     handle _ => ()) };
374 :     SafeIO.perform
375 :     { openIt = fn () => AutoDir.openTextOut pidmapfile,
376 :     closeIt = TextIO.closeOut,
377 :     work = writePidMap,
378 :     cleanup = fn _ => (OS.FileSys.remove pidmapfile
379 :     handle _ => ()) };
380 :     Say.say ["New boot directory has been built.\n"];
381 :     true
382 :     end
383 :    
384 :     (* the following thunk represents phase 2 (stabilization)
385 :     * of the master's execution path; it is never
386 :     * executed in slave mode *)
387 :     fun stabilize () =
388 :     (* now we re-parse everything with stabilization
389 : blume 1058 * turned on (and servers turned off) *)
390 : blume 801 case Parse.parse (parse_arg (SOME true, false)) of
391 :     NONE => false
392 :     | SOME (g, gp) => finish (g, gp)
393 :    
394 :     (* Don't do another traversal if this is a lonely master *)
395 :     fun just_stabilize () = finish (g, gp)
396 :    
397 :     (* the following thunk is executed in "master" mode only;
398 :     * slaves just throw it away *)
399 :     fun compile_and_stabilize () = let
400 :    
401 :     (* make compilation traversal and execute it *)
402 :     val { allgroups, ... } =
403 :     Compile.newTraversal (fn _ => fn _ => (),
404 :     fn _ => (),
405 :     g)
406 :     in
407 :     if Servers.withServers (fn () => allgroups gp) then
408 :     (Compile.reset ();
409 :     stabilize ())
410 : blume 449 else false
411 : blume 349 end
412 : blume 449 in
413 : blume 801 SOME ((g, gp, penv),
414 :     if lonely_master then just_stabilize
415 :     else compile_and_stabilize)
416 : blume 399 end
417 : blume 449 end handle Option => (Compile.reset (); NONE)
418 : blume 801 (* to catch valOf failures in "rt" or slave's failure
419 :     * to load init group *)
420 : blume 327 in
421 : blume 592 case BuildInitDG.build ginfo initgspec of
422 : blume 449 SOME x => mk_main_compile x
423 :     | NONE => NONE
424 : blume 327 end
425 : blume 362
426 : blume 569 fun compile dbopt =
427 : blume 632 (StabModmap.reset ();
428 : blume 801 case mk_compile { master = true, root = NONE, dirbase = dbopt } of
429 : blume 632 NONE => false
430 : blume 805 | SOME (_, thunk) => thunk ())
431 : blume 449
432 :     local
433 : blume 805 fun slave NONE = (internal_reset (); NONE)
434 : blume 632 | slave (SOME (dirbase, root)) =
435 : blume 805 (StabModmap.reset ();
436 :     case mk_compile { master = false, root = SOME root,
437 :     dirbase = SOME dirbase } of
438 :     NONE => NONE
439 :     | SOME ((g, gp, penv), _) => let
440 :     val trav = Compile.newSbnodeTraversal ()
441 :     fun trav' sbn = isSome (trav sbn gp)
442 :     in
443 :     SOME (g, trav', penv)
444 :     end)
445 : blume 449 in
446 : blume 452 val _ = CMBSlaveHook.init archos slave
447 : blume 449 end
448 :    
449 : blume 569 val make' = compile
450 : blume 377 fun make () = make' NONE
451 : blume 434 val symval = SSV.symval
452 : blume 327 end
453 : blume 569 end (* local *)

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