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 452 - (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 452
242 :     val touch = HostMachDepVC.Interact.useStream o TextIO.openString
243 : blume 448
244 :     fun waitForStart () = let
245 :     val line = TextIO.inputLine TextIO.stdIn
246 :     in
247 :     if line = "" then shutdown ()
248 :     else case String.tokens Char.isSpace line of
249 : blume 449 ["cm", d, f] => do_cm (d, f)
250 : blume 452 | ["cmb", archos, d, db] => do_cmb (archos, d, db)
251 : blume 448 | ["shutdown"] => shutdown ()
252 :     | _ => (say_error (); waitForStart ())
253 :     end handle _ => (say_error (); waitForStart ())
254 :    
255 : blume 452 and do_cmb (archos, d, db) = let
256 : blume 448 val _ = OS.FileSys.chDir d
257 :     val c = SrcPath.cwdContext ()
258 : blume 452 val slave = CMBSlave.slave { load = autoload, touch = touch }
259 : blume 449 in
260 : blume 452 case slave archos db of
261 : blume 449 NONE => (say_error (); waitForStart ())
262 : blume 452 | SOME (g, trav) => let
263 : blume 449 val _ = say_ok ()
264 :     val index = Reachable.snodeMap g
265 :     in
266 : blume 452 workLoop (index, trav, c)
267 : blume 449 end
268 :     end handle _ => (say_error (); waitForStart ())
269 :    
270 :     and do_cm (d, f) = let
271 :     val _ = OS.FileSys.chDir d
272 :     val c = SrcPath.cwdContext ()
273 : blume 448 val p = SrcPath.native { context = c, spec = f }
274 :     in
275 :     case Parse.parse NONE (param ()) NONE p of
276 :     NONE => (say_error (); waitForStart ())
277 : blume 449 | SOME (g, gp) => let
278 :     val _ = say_ok ()
279 :     val index = Reachable.snodeMap g
280 :     val trav = Compile.newSbnodeTraversal () gp
281 :     fun trav' sbn = isSome (trav sbn)
282 :     in
283 : blume 452 workLoop (index, trav', c)
284 : blume 449 end
285 : blume 448 end handle _ => (say_error (); waitForStart ())
286 :    
287 : blume 452 and workLoop (index, trav, c) = let
288 : blume 451 fun f2sn f =
289 :     SrcPathMap.find (index,
290 :     SrcPath.native { context = c,
291 :     spec = f })
292 : blume 448 fun loop () = let
293 :     val line = TextIO.inputLine TextIO.stdIn
294 :     in
295 :     if line = "" then shutdown ()
296 :     else case String.tokens Char.isSpace line of
297 : blume 452 ["compile", f] => let
298 : blume 448 val p = SrcPath.native { context = c, spec = f }
299 :     in
300 :     case SrcPathMap.find (index, p) of
301 :     NONE => (say_error (); loop ())
302 :     | SOME sn => let
303 : blume 451 val sbn = DG.SB_SNODE sn
304 : blume 448 in
305 : blume 449 if trav sbn then (say_ok (); loop ())
306 :     else (say_error (); loop ())
307 : blume 452 end handle _ => (say_error (); loop ())
308 : blume 448 end
309 : blume 449 | ["cm", d, f] => do_cm (d, f)
310 : blume 448 | ["finish"] => (say_ok (); waitForStart ())
311 :     | ["shutdown"] => shutdown ()
312 :     | _ => (say_error (); loop ())
313 :     end handle _ => (say_error (); loop ())
314 :     in
315 :     loop ()
316 :     end
317 :     in
318 :     say_ok (); (* announce readiness *)
319 : blume 452 waitForStart () handle _ => ();
320 :     OS.Process.exit OS.Process.failure
321 : blume 448 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