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 569 - (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 :     kind = GroupGraph.LIB (StringSet.empty, []),
195 :     required = StringSet.singleton "primitive",
196 :     grouppath = initgspec,
197 :     sublibs = [] },
198 :     corenv)
199 :     before (ovldR := savedOvld)
200 : blume 537 end
201 : blume 327
202 : blume 569 (* just go and load the stable init group or signal failure *)
203 :     fun loadInitGroup () = let
204 :     val coresym = PervCoreAccess.coreStrSym
205 :     val lsarg =
206 :     { getGroup = fn _ => raise Fail "CMB: initial getGroup",
207 :     anyerrors = ref false }
208 : blume 537 in
209 : blume 569 case Stabilize.loadStable ginfo_nocore lsarg initgspec of
210 :     NONE => NONE
211 :     | SOME (g as GG.GROUP { exports, ... }) =>
212 :     (case SymbolMap.find (exports, coresym) of
213 :     SOME ((_, DG.SB_BNODE (_, ii)), _) => let
214 :     val stat = #env (#statenv ii ())
215 :     val sym = #symenv ii ()
216 :     val corenv =
217 :     BE.mkenv { static = CoerceEnv.es2bs stat,
218 :     symbolic = sym,
219 :     dynamic = emptydyn }
220 :     in
221 :     SOME (g, corenv)
222 :     end
223 :     | _ => NONE)
224 : blume 537 end
225 : blume 569
226 :     (* Don't try to load the stable init group. Instead, recompile
227 :     * directly. *)
228 :     fun dontLoadInitGroup () = let
229 :     val (g0, corenv) = recompInitGroup ()
230 :     val stabarg = { group = g0, anyerrors = ref false }
231 :     in
232 :     if deliver then
233 :     case Stabilize.stabilize ginfo_nocore stabarg of
234 :     SOME g => (g, corenv)
235 :     | NONE => raise Fail "CMB: cannot stabilize init group"
236 :     else (g0, corenv)
237 :     end
238 : blume 327
239 : blume 569 (* Try loading the init group from the stable file if possible;
240 :     * recompile if loading fails *)
241 :     fun tryLoadInitGroup () =
242 :     case loadInitGroup () of
243 :     SOME g => g
244 :     | NONE => dontLoadInitGroup ()
245 :    
246 :     (* Ok, now, based on "paranoid" and stable verification,
247 :     * call the appropriate function(s) to get the init group. *)
248 :     val (init_group, corenv) =
249 :     if paranoid then let
250 :     val export_nodes = core_n :: perv_n :: others
251 :     val ver_arg = (initgspec, export_nodes, [],
252 :     SrcPathSet.empty)
253 :     val em = StableMap.empty
254 : blume 537 in
255 : blume 569 if VerifyStable.verify' ginfo_nocore em ver_arg then
256 :     tryLoadInitGroup ()
257 :     else dontLoadInitGroup ()
258 : blume 537 end
259 : blume 569 else tryLoadInitGroup ()
260 : blume 356
261 : blume 569 (* now we finally build the real param and ginfo that we can
262 :     * use throughout the rest... *)
263 :     val param = mkParam corenv
264 :     val ginfo =
265 :     { param = param, errcons = errcons, groupreg = groupreg }
266 :    
267 :     val stab = if deliver then SOME true else NONE
268 :    
269 : blume 537 val gr = GroupReg.new ()
270 :     val _ = GroupReg.register gr (initgspec, src)
271 :    
272 :     val parse_arg =
273 :     { load_plugin = load_plugin,
274 :     gr = gr,
275 :     param = param,
276 :     stabflag = stab,
277 :     group = maingspec,
278 :     init_group = init_group,
279 : blume 569 paranoid = paranoid }
280 : blume 327 in
281 : blume 456 Servers.dirbase dirbase;
282 : blume 537 case Parse.parse parse_arg of
283 : blume 449 NONE => NONE
284 : blume 399 | SOME (g, gp) => let
285 : blume 449 fun thunk () = let
286 : blume 456 val _ = init_servers g
287 : blume 449 fun store _ = ()
288 :     val { group = recomp, ... } =
289 :     Compile.newTraversal (fn _ => fn _ => (), store, g)
290 : blume 450 val res =
291 :     Servers.withServers (fn () => recomp gp)
292 : blume 449 in
293 : blume 450 if isSome res then let
294 : blume 537 val { l = bootitems, ss } = mkBootList g
295 :     val stablelibs = Reachable.stableLibsOf g
296 :     fun inSet bi = StableSet.member (ss, bi)
297 :     val frontiers =
298 :     SrcPathMap.map (Reachable.frontier inSet)
299 :     stablelibs
300 :     fun writeBootList s = let
301 :     fun wr str = TextIO.output (s, str ^ "\n")
302 : blume 569 val numitems = length bootitems
303 :     fun biggerlen (s, n) = Int.max (size s, n)
304 :     val maxlen = foldl biggerlen 0 bootitems
305 : blume 364 in
306 : blume 569 wr (concat ["%", Int.toString numitems,
307 :     " ", Int.toString maxlen]);
308 : blume 537 app wr bootitems
309 : blume 364 end
310 : blume 537 fun writePid s i = let
311 :     val sn = BinInfo.stablename i
312 :     val os = BinInfo.offset i
313 :     val descr = BinInfo.describe i
314 :     val bfc = BFC.getStable
315 :     { stable = sn, offset = os, descr = descr }
316 :     in
317 :     case BF.exportPidOf bfc of
318 :     NONE => ()
319 :     | SOME pid =>
320 :     (TextIO.output (s, " ");
321 :     TextIO.output (s, PS.toHex pid))
322 :     end
323 :     fun writePidLine s (p, set) =
324 :     if StableSet.isEmpty set then ()
325 :     else (TextIO.output (s, SrcPath.descr p);
326 :     StableSet.app (writePid s) set;
327 :     TextIO.output (s, "\n"))
328 :     fun writePidMap s =
329 :     SrcPathMap.appi (writePidLine s) frontiers
330 : blume 349 in
331 : blume 449 if deliver then
332 :     (SafeIO.perform
333 :     { openIt = fn () =>
334 : blume 537 AutoDir.openTextOut listfile,
335 : blume 449 closeIt = TextIO.closeOut,
336 : blume 537 work = writeBootList,
337 : blume 459 cleanup = fn _ =>
338 : blume 537 OS.FileSys.remove listfile
339 : blume 449 handle _ => () };
340 :     SafeIO.perform
341 :     { openIt = fn () =>
342 : blume 537 AutoDir.openTextOut pidmapfile,
343 : blume 449 closeIt = TextIO.closeOut,
344 : blume 537 work = writePidMap,
345 : blume 459 cleanup = fn _ =>
346 : blume 537 OS.FileSys.remove pidmapfile
347 : blume 449 handle _ => () };
348 : blume 537 Say.say
349 :     ["New boot directory has been built.\n"])
350 : blume 449 else ();
351 :     true
352 : blume 349 end
353 : blume 449 else false
354 : blume 349 end
355 : blume 449 in
356 : blume 457 SOME ((g, gp, pcmode), thunk)
357 : blume 399 end
358 : blume 449 end handle Option => (Compile.reset (); NONE)
359 : blume 330 (* to catch valOf failures in "rt" *)
360 : blume 327 in
361 : blume 329 case BuildInitDG.build ginfo_nocore initgspec of
362 : blume 449 SOME x => mk_main_compile x
363 :     | NONE => NONE
364 : blume 327 end
365 : blume 362
366 : blume 569 fun compile dbopt =
367 :     case mk_compile { deliver = true, root = NONE,
368 :     dirbase = dbopt, paranoid = true } of
369 : blume 450 NONE => false
370 : blume 459 | SOME (_, thunk) => thunk ()
371 : blume 449
372 :     local
373 : blume 456 fun slave (dirbase, root) =
374 : blume 569 case mk_compile { deliver = false, root = SOME root,
375 :     dirbase = SOME dirbase, paranoid = false } of
376 : blume 449 NONE => NONE
377 : blume 457 | SOME ((g, gp, pcmode), _) => let
378 : blume 449 val trav = Compile.newSbnodeTraversal () gp
379 :     fun trav' sbn = isSome (trav sbn)
380 :     in
381 : blume 457 SOME (g, trav', pcmode)
382 : blume 449 end
383 :     in
384 : blume 452 val _ = CMBSlaveHook.init archos slave
385 : blume 449 end
386 :    
387 : blume 362 fun reset () =
388 : blume 398 (Compile.reset ();
389 : blume 367 Parse.reset ())
390 : blume 377
391 : blume 569 val make' = compile
392 : blume 377 fun make () = make' NONE
393 : blume 434 val symval = SSV.symval
394 : blume 327 end
395 : blume 569 end (* local *)

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