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

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