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

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