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

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