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/main/cm-boot.sml
ViewVC logotype

Annotation of /sml/trunk/src/cm/main/cm-boot.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 453 - (view) (download)

1 : blume 375 (*
2 :     * This is the module that actually puts together the contents of the
3 :     * structure CM that people find at the top-level. The "real" structure
4 :     * CM is defined in CmHook, but it needs to be initialized at bootstrap
5 :     * time -- and _that_ is what's done here.
6 :     *
7 :     * Copyright (c) 1999 by Lucent Bell Laboratories
8 :     *
9 :     * author: Matthias Blume (blume@cs.princeton.edu)
10 :     *)
11 :     functor LinkCM (structure HostMachDepVC : MACHDEP_VC) = struct
12 :    
13 :     datatype envrequest = AUTOLOAD | BARE
14 :    
15 :     local
16 :     structure YaccTool = YaccTool
17 :     structure LexTool = LexTool
18 :     structure BurgTool = BurgTool
19 :    
20 :     structure E = GenericVC.Environment
21 :     structure SE = GenericVC.StaticEnv
22 :     structure ER = GenericVC.EnvRef
23 :     structure BE = GenericVC.BareEnvironment
24 :     structure CMSE = GenericVC.CMStaticEnv
25 :     structure S = GenericVC.Symbol
26 :     structure CoerceEnv = GenericVC.CoerceEnv
27 :     structure EM = GenericVC.ErrorMsg
28 :     structure BF = HostMachDepVC.Binfile
29 : blume 380 structure P = OS.Path
30 :     structure F = OS.FileSys
31 : blume 451 structure DG = DependencyGraph
32 : blume 375
33 :     val os = SMLofNJ.SysInfo.getOSKind ()
34 :    
35 :     structure SSV =
36 :     SpecificSymValFn (structure MachDepVC = HostMachDepVC
37 :     val os = os)
38 :    
39 :     val emptydyn = E.dynamicPart E.emptyEnv
40 :     val system_values = ref emptydyn
41 :    
42 : blume 400 structure Compile =
43 : blume 448 CompileFn (structure MachDepVC = HostMachDepVC
44 :     val compile_there = Servers.compile)
45 : blume 400
46 : blume 399 structure Link =
47 :     LinkFn (structure MachDepVC = HostMachDepVC
48 :     val system_values = system_values)
49 : blume 375
50 : blume 403 structure BFC =
51 :     BfcFn (structure MachDepVC = HostMachDepVC)
52 :    
53 : blume 375 structure AutoLoad = AutoLoadFn
54 : blume 399 (structure C = Compile
55 : blume 403 structure L = Link
56 :     structure BFC = BFC)
57 : blume 375
58 : blume 399 fun recomp_runner gp g = let
59 : blume 403 fun store _ = ()
60 :     val { group, ... } = Compile.newTraversal (Link.evict, store, g)
61 : blume 399 in
62 : blume 450 isSome (Servers.withServers (fn () => group gp))
63 :     before Link.cleanup gp
64 : blume 399 end
65 : blume 375
66 :     (* This function combines the actions of "recompile" and "exec".
67 :     * When successful, it combines the results (thus forming a full
68 :     * environment) and adds it to the toplevel environment. *)
69 : blume 399 fun make_runner gp g = let
70 : blume 403 val { store, get } = BFC.new ()
71 :     val { group = c_group, ... } =
72 :     Compile.newTraversal (Link.evict, store, g)
73 :     val { group = l_group, ... } = Link.newTraversal (g, get)
74 : blume 399 val GroupGraph.GROUP { required = rq, ... } = g
75 :     in
76 : blume 450 case Servers.withServers (fn () => c_group gp) of
77 : blume 375 NONE => false
78 :     | SOME { stat, sym} =>
79 : blume 399 (* Before executing the code, we announce the priviliges
80 :     * that are being invoked. (For the time being, we assume
81 :     * that everybody has every conceivable privilege, but at
82 :     * the very least we announce which ones are being made
83 :     * use of.) *)
84 : blume 400 (Link.cleanup gp;
85 : blume 399 if StringSet.isEmpty rq then ()
86 :     else Say.say ("$Execute: required privileges are:\n" ::
87 :     map (fn s => (" " ^ s ^ "\n")) (StringSet.listItems rq));
88 :     case l_group gp of
89 : blume 375 NONE => false
90 :     | SOME dyn => let
91 :     val delta = E.mkenv { static = stat, symbolic = sym,
92 :     dynamic = dyn }
93 :     val base = #get ER.topLevel ()
94 :     val new = BE.concatEnv (CoerceEnv.e2b delta, base)
95 :     in
96 :     #set ER.topLevel new;
97 :     Say.vsay ["[New bindings added.]\n"];
98 :     true
99 :     end)
100 : blume 399 end
101 : blume 375
102 :     val al_greg = GroupReg.new ()
103 :    
104 :     (* Instantiate the stabilization mechanism. *)
105 :     structure Stabilize =
106 : blume 403 StabilizeFn (structure MachDepVC = HostMachDepVC
107 :     fun recomp gp g = let
108 : blume 451 val GroupGraph.GROUP { grouppath, ... } = g
109 : blume 403 val { store, get } = BFC.new ()
110 :     val { group, ... } =
111 :     Compile.newTraversal (Link.evict, store, g)
112 :     in
113 : blume 450 case Servers.withServers (fn () => group gp) of
114 : blume 403 NONE => NONE
115 :     | SOME _ => SOME get
116 :     end
117 :     fun destroy_state gp i =
118 : blume 451 (Compile.evict i;
119 :     Link.evict gp i)
120 : blume 403 val getII = Compile.getII)
121 : blume 375
122 :     (* Access to the stabilization mechanism is integrated into the
123 :     * parser. I'm not sure if this is the cleanest way, but it works
124 :     * well enough. *)
125 :     structure Parse = ParseFn (structure Stabilize = Stabilize
126 :     val pending = AutoLoad.getPending)
127 :    
128 :     local
129 :     type kernelValues =
130 :     { primconf : Primitive.configuration,
131 :     pervasive : E.environment,
132 :     corenv : BE.staticEnv,
133 :     pervcorepids : PidSet.set }
134 :    
135 :     val fnpolicy = FilenamePolicy.colocate
136 :     { os = os, arch = HostMachDepVC.architecture }
137 :    
138 :     val pcmode = PathConfig.new ()
139 :    
140 :     val theValues = ref (NONE: kernelValues option)
141 :    
142 :     in
143 : blume 377 fun setAnchor (a, s) =
144 :     (PathConfig.set (pcmode, a, s); SrcPath.sync ())
145 :     (* cancelling anchors cannot affect the order of existing paths
146 :     * (it may invalidate some paths; but all other ones stay as
147 :     * they are) *)
148 : blume 375 fun cancelAnchor a = PathConfig.cancel (pcmode, a)
149 : blume 377 (* same goes for reset because it just cancels all anchors... *)
150 : blume 375 fun resetPathConfig () = PathConfig.reset pcmode
151 :    
152 :     fun showPending () = let
153 :     fun one (s, _) = let
154 :     val nss = Symbol.nameSpaceToString (Symbol.nameSpace s)
155 :     val n = Symbol.name s
156 :     in
157 :     Say.say [" ", nss, " ", n, "\n"]
158 :     end
159 :     in
160 :     SymbolMap.appi one (AutoLoad.getPending ())
161 :     end
162 :    
163 :     fun initPaths () = let
164 : blume 433 val lpcth = #get StdConfig.local_pathconfig ()
165 : blume 375 val p = case lpcth () of
166 :     NONE => []
167 :     | SOME f => [f]
168 : blume 433 val p = #get StdConfig.pathcfgspec () :: p
169 : blume 375 fun processOne f = PathConfig.processSpecFile (pcmode, f)
170 :     handle _ => ()
171 :     in
172 :     app processOne p
173 :     end
174 :    
175 :     fun param () = let
176 :     val v = valOf (!theValues)
177 :     handle Option =>
178 :     raise Fail "CMBoot: theParam not initialized"
179 :     in
180 :     { primconf = #primconf v,
181 :     fnpolicy = fnpolicy,
182 :     pcmode = pcmode,
183 : blume 433 symval = SSV.symval,
184 :     keep_going = #get StdConfig.keep_going (),
185 : blume 375 pervasive = #pervasive v,
186 :     corenv = #corenv v,
187 :     pervcorepids = #pervcorepids v }
188 :     end
189 :    
190 :     fun autoload s = let
191 :     val c = SrcPath.cwdContext ()
192 :     val p = SrcPath.standard pcmode { context = c, spec = s }
193 :     in
194 :     case Parse.parse (SOME al_greg) (param ()) NONE p of
195 :     NONE => false
196 :     | SOME (g, _) =>
197 :     (AutoLoad.register (GenericVC.EnvRef.topLevel, g);
198 :     true)
199 :     end
200 :    
201 :     fun al_ginfo () = { param = param (),
202 :     groupreg = al_greg,
203 :     errcons = EM.defaultConsumer () }
204 :    
205 :     val al_manager = AutoLoad.mkManager al_ginfo
206 :    
207 :     fun al_manager' (ast, _, ter) = al_manager (ast, ter)
208 :    
209 :     fun run sflag f s = let
210 :     val c = SrcPath.cwdContext ()
211 :     val p = SrcPath.standard pcmode { context = c, spec = s }
212 :     in
213 : blume 451 Servers.cm p;
214 : blume 375 case Parse.parse NONE (param ()) sflag p of
215 :     NONE => false
216 : blume 449 | SOME (g, gp) =>
217 : blume 452 SafeIO.perform { openIt = fn () => (),
218 :     closeIt = Servers.reset,
219 :     work = fn () => f gp g,
220 :     cleanup = fn () => () }
221 : blume 375 end
222 :    
223 : blume 449 val listLibs = Parse.listLibs
224 :     fun dismissLib l = let
225 :     val c = SrcPath.cwdContext ()
226 :     val p = SrcPath.standard pcmode { context = c, spec = l }
227 :     in
228 :     Parse.dismissLib p
229 :     end
230 :    
231 :     fun stabilize_runner gp g = true
232 :    
233 :     fun stabilize recursively = run (SOME recursively) stabilize_runner
234 :     val recomp = run NONE recomp_runner
235 :     val make = run NONE make_runner
236 :    
237 : blume 448 fun slave () = let
238 :     fun shutdown () = OS.Process.exit OS.Process.success
239 :     fun say_ok () = Say.say ["SLAVE: ok\n"]
240 :     fun say_error () = Say.say ["SLAVE: error\n"]
241 : blume 453 fun say_pong () = Say.say ["SLAVE: pong\n"]
242 : blume 452
243 :     val touch = HostMachDepVC.Interact.useStream o TextIO.openString
244 : blume 448
245 :     fun waitForStart () = let
246 :     val line = TextIO.inputLine TextIO.stdIn
247 :     in
248 :     if line = "" then shutdown ()
249 :     else case String.tokens Char.isSpace line of
250 : blume 449 ["cm", d, f] => do_cm (d, f)
251 : blume 452 | ["cmb", archos, d, db] => do_cmb (archos, d, db)
252 : blume 453 | ["ping"] => (say_pong (); waitForStart ())
253 :     | ["finish"] => (say_ok (); waitForStart ())
254 : blume 448 | ["shutdown"] => shutdown ()
255 :     | _ => (say_error (); waitForStart ())
256 :     end handle _ => (say_error (); waitForStart ())
257 :    
258 : blume 452 and do_cmb (archos, d, db) = let
259 : blume 448 val _ = OS.FileSys.chDir d
260 :     val c = SrcPath.cwdContext ()
261 : blume 452 val slave = CMBSlave.slave { load = autoload, touch = touch }
262 : blume 449 in
263 : blume 452 case slave archos db of
264 : blume 449 NONE => (say_error (); waitForStart ())
265 : blume 452 | SOME (g, trav) => let
266 : blume 449 val _ = say_ok ()
267 :     val index = Reachable.snodeMap g
268 :     in
269 : blume 452 workLoop (index, trav, c)
270 : blume 449 end
271 :     end handle _ => (say_error (); waitForStart ())
272 :    
273 :     and do_cm (d, f) = let
274 :     val _ = OS.FileSys.chDir d
275 :     val c = SrcPath.cwdContext ()
276 : blume 448 val p = SrcPath.native { context = c, spec = f }
277 :     in
278 :     case Parse.parse NONE (param ()) NONE p of
279 :     NONE => (say_error (); waitForStart ())
280 : blume 449 | SOME (g, gp) => let
281 :     val _ = say_ok ()
282 :     val index = Reachable.snodeMap g
283 :     val trav = Compile.newSbnodeTraversal () gp
284 :     fun trav' sbn = isSome (trav sbn)
285 :     in
286 : blume 452 workLoop (index, trav', c)
287 : blume 449 end
288 : blume 448 end handle _ => (say_error (); waitForStart ())
289 :    
290 : blume 452 and workLoop (index, trav, c) = let
291 : blume 451 fun f2sn f =
292 :     SrcPathMap.find (index,
293 :     SrcPath.native { context = c,
294 :     spec = f })
295 : blume 448 fun loop () = let
296 :     val line = TextIO.inputLine TextIO.stdIn
297 :     in
298 :     if line = "" then shutdown ()
299 :     else case String.tokens Char.isSpace line of
300 : blume 452 ["compile", f] => let
301 : blume 448 val p = SrcPath.native { context = c, spec = f }
302 :     in
303 :     case SrcPathMap.find (index, p) of
304 :     NONE => (say_error (); loop ())
305 :     | SOME sn => let
306 : blume 451 val sbn = DG.SB_SNODE sn
307 : blume 448 in
308 : blume 449 if trav sbn then (say_ok (); loop ())
309 :     else (say_error (); loop ())
310 : blume 452 end handle _ => (say_error (); loop ())
311 : blume 448 end
312 : blume 449 | ["cm", d, f] => do_cm (d, f)
313 : blume 453 | ["cmb", archos, d, db] => do_cmb (archos, d, db)
314 : blume 448 | ["finish"] => (say_ok (); waitForStart ())
315 : blume 453 | ["ping"] => (say_pong (); loop ())
316 : blume 448 | ["shutdown"] => shutdown ()
317 :     | _ => (say_error (); loop ())
318 :     end handle _ => (say_error (); loop ())
319 :     in
320 :     loop ()
321 :     end
322 :     in
323 : blume 453 ignore (Signals.setHandler (Signals.sigINT, Signals.IGNORE));
324 : blume 448 say_ok (); (* announce readiness *)
325 : blume 452 waitForStart () handle _ => ();
326 :     OS.Process.exit OS.Process.failure
327 : blume 448 end
328 :    
329 : blume 375 fun reset () =
330 : blume 399 (Compile.reset ();
331 :     Link.reset ();
332 : blume 375 AutoLoad.reset ();
333 :     Parse.reset ();
334 :     SmlInfo.forgetAllBut SrcPathSet.empty)
335 :    
336 :     fun initTheValues (bootdir, er) = let
337 :     val _ = let
338 :     fun listDir ds = let
339 :     fun loop l =
340 : blume 380 case F.readDir ds of
341 : blume 375 "" => l
342 :     | x => loop (x :: l)
343 :     in
344 :     loop []
345 :     end
346 :     val fileList = SafeIO.perform
347 : blume 380 { openIt = fn () => F.openDir bootdir,
348 :     closeIt = F.closeDir,
349 : blume 375 work = listDir,
350 :     cleanup = fn () => () }
351 : blume 380 fun isDir x = F.isDir x handle _ => false
352 : blume 375 fun subDir x = let
353 : blume 380 val d = P.concat (bootdir, x)
354 : blume 375 in
355 :     if isDir d then SOME (x, d) else NONE
356 :     end
357 :     val pairList = List.mapPartial subDir fileList
358 :     in
359 :     app (fn (x, d) => PathConfig.set (pcmode, x, d)) pairList
360 :     end
361 :     val initgspec =
362 :     SrcPath.standard pcmode { context = SrcPath.cwdContext (),
363 :     spec = BtNames.initgspec }
364 :     val ginfo = { param = { primconf = Primitive.primEnvConf,
365 :     fnpolicy = fnpolicy,
366 :     pcmode = pcmode,
367 : blume 433 symval = SSV.symval,
368 : blume 375 keep_going = false,
369 :     pervasive = E.emptyEnv,
370 :     corenv = BE.staticPart BE.emptyEnv,
371 :     pervcorepids = PidSet.empty },
372 :     groupreg = GroupReg.new (),
373 :     errcons = EM.defaultConsumer () }
374 :     in
375 :     case BuildInitDG.build ginfo initgspec of
376 :     NONE => raise Fail "CMBoot: BuiltInitDG.build"
377 :     | SOME { rts, core, pervasive, primitives, ... } => let
378 :     (* It is absolutely crucial that we don't finish the
379 :     * recomp traversal until we are done with all
380 :     * nodes of the InitDG. This is because we have
381 :     * been cheating, and if we ever have to try and
382 :     * fetch assembly.sig or core.sml in a separate
383 :     * traversal, it will fail. *)
384 : blume 400 val sbnode = Compile.newSbnodeTraversal ()
385 : blume 375 fun get n = let
386 : blume 399 val { ii, ctxt } = valOf (sbnode ginfo n)
387 :     val { statpid, statenv, symenv, sympid } = ii
388 :     (* We have not implemented the "sbnode" part
389 :     * in the Link module.
390 : blume 375 * But at boot time any relevant value should be
391 : blume 399 * available as a sysval, so there is no problem.
392 :     *
393 :     * WARNING! HACK!
394 :     * We are cheating somewhat by taking advantage
395 :     * of the fact that the staticPid is always
396 :     * the same as the exportPid if the latter exists.
397 :     *)
398 :     val d = case Link.sysval (SOME statpid) of
399 :     SOME d => d
400 :     | NONE => emptydyn
401 :     val env = E.mkenv { static = statenv (),
402 :     symbolic = symenv (),
403 : blume 375 dynamic = d }
404 : blume 399 val pidInfo = { statpid = statpid,
405 :     sympid = sympid,
406 : blume 375 ctxt = ctxt }
407 :     in
408 :     (env, pidInfo)
409 :     end
410 :     fun getPspec (name, n) = let
411 :     val (env, pidInfo) = get n
412 :     in
413 :     { name = name, env = env, pidInfo = pidInfo }
414 :     end
415 :    
416 :     val (core, corePidInfo) = get core
417 :     val corenv = CoerceEnv.es2bs (E.staticPart core)
418 :     val (rts, _) = get rts
419 :     val (pervasive0, pervPidInfo) = get pervasive
420 :     val pspecs = map getPspec primitives
421 :     val core_symdyn =
422 :     E.mkenv { static = E.staticPart E.emptyEnv,
423 :     dynamic = E.dynamicPart core,
424 :     symbolic = E.symbolicPart core }
425 :     val pervasive = E.layerEnv (pervasive0, core_symdyn)
426 :     val pervcorepids =
427 :     PidSet.addList (PidSet.empty,
428 :     [#statpid corePidInfo,
429 :     #statpid pervPidInfo,
430 :     #sympid pervPidInfo])
431 :     in
432 : blume 399 Compile.reset ();
433 :     Link.reset ();
434 : blume 375 #set ER.core corenv;
435 :     #set ER.pervasive pervasive;
436 :     #set ER.topLevel BE.emptyEnv;
437 :     theValues :=
438 :     SOME { primconf = Primitive.configuration pspecs,
439 :     pervasive = pervasive,
440 :     corenv = corenv,
441 :     pervcorepids = pervcorepids };
442 :     case er of
443 :     BARE =>
444 :     (make "basis.cm";
445 :     make "host-compiler.cm";
446 :     system_values := emptydyn)
447 :     | AUTOLOAD =>
448 :     (HostMachDepVC.Interact.installCompManager
449 :     (SOME al_manager');
450 :     autoload "basis.cm";
451 :     autoload "host-cm.cm";
452 :     CmHook.init
453 :     { stabilize = stabilize,
454 :     recomp = recomp,
455 :     make = make,
456 :     autoload = autoload,
457 :     reset = reset,
458 : blume 433 verbose = StdConfig.verbose,
459 :     debug = StdConfig.debug,
460 :     keep_going = StdConfig.keep_going,
461 :     warn_obsolete = StdConfig.warn_obsolete,
462 :     parse_caching = StdConfig.parse_caching,
463 : blume 375 setAnchor = setAnchor,
464 :     cancelAnchor = cancelAnchor,
465 :     resetPathConfig = resetPathConfig,
466 :     synchronize = SrcPath.sync,
467 : blume 404 showPending = showPending,
468 :     listLibs = listLibs,
469 : blume 433 dismissLib = dismissLib,
470 : blume 448 symval = SSV.symval,
471 : blume 449 server_start = Servers.start,
472 :     server_stop = Servers.stop,
473 :     server_kill = Servers.kill })
474 : blume 375
475 :     end
476 :     end
477 :     end
478 :     in
479 :     fun init (bootdir, de, er) =
480 :     (system_values := de;
481 :     initTheValues (bootdir, er);
482 :     Cleanup.install initPaths)
483 : blume 448
484 :     fun procCmdLine () = let
485 :     fun p (f, "sml") = HostMachDepVC.Interact.useFile f
486 :     | p (f, "sig") = HostMachDepVC.Interact.useFile f
487 :     | p (f, "cm") = ignore (make f)
488 :     | p (f, e) =
489 :     (print (concat ["!* unable to process `", f,
490 :     "' (unknown extension `", e, "')\n"]))
491 :     fun c f = (f, String.map Char.toLower
492 :     (getOpt (OS.Path.ext f, "<none>")))
493 :     in
494 :     case SMLofNJ.getArgs () of
495 : blume 451 ["@CMslave"] => (#set StdConfig.verbose false; slave ())
496 : blume 448 | l => app (p o c) l
497 :     end
498 : blume 375 end
499 :     end

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