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

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