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

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