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

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