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

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