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/bootstrap/btcompile.sml
ViewVC logotype

Annotation of /sml/trunk/src/cm/bootstrap/btcompile.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 677 - (view) (download)

1 : blume 327 (*
2 :     * The bootstrap compiler.
3 :     * (Formerly known as "batch" compiler.)
4 :     *
5 :     * (C) 1999 Lucent Technologies, Bell Laboratories
6 :     *
7 :     * Author: Matthias Blume (blume@kurims.kyoto-u.ac.jp)
8 :     *)
9 : blume 569 local
10 :     structure EM = GenericVC.ErrorMsg
11 :     structure E = GenericVC.Environment
12 : blume 587 structure SE = GenericVC.StaticEnv
13 : blume 569 structure PS = GenericVC.PersStamps
14 :     structure GG = GroupGraph
15 :     structure DG = DependencyGraph
16 :     in
17 : blume 578 functor BootstrapCompileFn
18 :     (structure MachDepVC : MACHDEP_VC
19 : blume 677 val useStream : TextIO.instream -> unit
20 : blume 578 val os : SMLofNJ.SysInfo.os_kind
21 : blume 666 val load_plugin : SrcPath.dir -> string -> bool) :> sig
22 : blume 364 val make' : string option -> bool
23 :     val make : unit -> bool
24 : blume 362 val reset : unit -> unit
25 : blume 434 val symval : string -> { get: unit -> int option, set: int option -> unit }
26 : blume 357 end = struct
27 : blume 336 structure SSV = SpecificSymValFn (structure MachDepVC = MachDepVC
28 :     val os = os)
29 : blume 364 structure P = OS.Path
30 :     structure F = OS.FileSys
31 : blume 403 structure BF = MachDepVC.Binfile
32 : blume 327
33 : blume 452 val arch = MachDepVC.architecture
34 :     val osname = FilenamePolicy.kind2name os
35 : blume 642
36 : blume 452 val archos = concat [arch, "-", osname]
37 :    
38 : blume 569 fun init_servers (GG.GROUP { grouppath, ... }) =
39 : blume 464 Servers.cmb { archos = archos,
40 : blume 666 root = SrcPath.encode grouppath }
41 : blume 587 | init_servers GG.ERRORGROUP = ()
42 : blume 456
43 : blume 588 structure StabModmap = StabModmapFn ()
44 :    
45 : blume 448 structure Compile = CompileFn (structure MachDepVC = MachDepVC
46 : blume 588 structure StabModmap = StabModmap
47 : blume 677 val useStream = useStream
48 : blume 464 val compile_there =
49 : blume 666 Servers.compile o SrcPath.encode)
50 : blume 360
51 : blume 403 structure BFC = BfcFn (structure MachDepVC = MachDepVC)
52 :    
53 : blume 327 (* instantiate Stabilize... *)
54 : blume 329 structure Stabilize =
55 : blume 537 StabilizeFn (structure MachDepVC = MachDepVC
56 : blume 588 structure StabModmap = StabModmap
57 : blume 399 fun recomp gp g = let
58 : blume 403 val { store, get } = BFC.new ()
59 : blume 456 val _ = init_servers g
60 : blume 399 val { group, ... } =
61 : blume 403 Compile.newTraversal (fn _ => fn _ => (),
62 :     store, g)
63 : blume 399 in
64 : blume 450 case Servers.withServers (fn () => group gp) of
65 : blume 403 NONE => NONE
66 :     | SOME _ => SOME get
67 :     end
68 :     val getII = Compile.getII)
69 : blume 398
70 : blume 569 structure VerifyStable = VerStabFn (structure Stabilize = Stabilize)
71 :    
72 : blume 327 (* ... and Parse *)
73 : blume 362 structure Parse = ParseFn (structure Stabilize = Stabilize
74 : blume 588 structure StabModmap = StabModmap
75 : blume 537 val evictStale = Compile.evictStale
76 : blume 372 fun pending () = SymbolMap.empty)
77 : blume 327
78 : blume 537 fun mkBootList g = let
79 :     fun listName p =
80 :     case P.fromString p of
81 :     { vol = "", isAbs = false, arcs = _ :: arc1 :: arcn } => let
82 :     fun win32name () =
83 :     concat (arc1 ::
84 :     foldr (fn (a, r) => "\\" :: a :: r) [] arcn)
85 :     in
86 :     case os of
87 :     SMLofNJ.SysInfo.WIN32 => win32name ()
88 :     | _ => P.toString { isAbs = false, vol = "",
89 :     arcs = arc1 :: arcn }
90 :     end
91 :     | _ => raise Fail ("BootstrapCompile:listName: bad name: " ^ p)
92 : blume 364 in
93 : blume 537 MkBootList.group listName g
94 : blume 364 end
95 : blume 327
96 : blume 588 local
97 :     fun internal_reset () =
98 :     (Compile.reset ();
99 :     Parse.reset ();
100 :     StabModmap.reset ())
101 :     in
102 :     fun reset () =
103 :     (Say.vsay ["[CMB reset]\n"];
104 :     internal_reset ())
105 :     val checkDirbase = let
106 :     val prev = ref NONE
107 :     fun ck db =
108 :     (case !prev of
109 :     NONE => prev := SOME db
110 :     | SOME db' =>
111 :     if db = db' then ()
112 :     else (Say.vsay ["[new dirbase is `", db,
113 :     "'; CMB reset]\n"];
114 :     internal_reset ();
115 :     prev := SOME db))
116 :     in
117 :     ck
118 :     end
119 :     end
120 :    
121 : blume 569 fun mk_compile { deliver, root, dirbase = dbopt, paranoid } = let
122 : blume 358
123 : blume 360 val dirbase = getOpt (dbopt, BtNames.dirbaseDefault)
124 : blume 588 val _ = checkDirbase dirbase
125 : blume 666 val penvspec = BtNames.penvspec
126 : blume 360 val initgspec = BtNames.initgspec
127 :     val maingspec = BtNames.maingspec
128 :    
129 : blume 537 val bindir = concat [dirbase, BtNames.bin_infix, archos]
130 :     val bootdir = concat [dirbase, BtNames.boot_infix, archos]
131 : blume 357
132 : blume 433 val keep_going = #get StdConfig.keep_going ()
133 : blume 329
134 : blume 666 val ctxt = SrcPath.cwd ()
135 : blume 329
136 : blume 537 val listfile = P.joinDirFile { dir = bootdir, file = BtNames.bootlist }
137 :     val pidmapfile = P.joinDirFile { dir = bootdir, file = BtNames.pidmap }
138 : blume 329
139 : blume 666 val penv = SrcPath.newEnv ()
140 :     val _ = SrcPath.processSpecFile (penv, penvspec)
141 : blume 676 val _ = SrcPath.sync ()
142 : blume 329
143 : blume 666 fun stdpath s =
144 :     SrcPath.file (SrcPath.standard
145 :     { err = fn s => raise Fail s, env = penv }
146 :     { context = ctxt, spec = s })
147 : blume 352
148 :     val initgspec = stdpath initgspec
149 : blume 456 val maingspec =
150 :     case root of
151 :     NONE => stdpath maingspec
152 : blume 666 | SOME r => SrcPath.decode penv r
153 : blume 352
154 : blume 364 val fnpolicy =
155 : blume 357 FilenamePolicy.separate { bindir = bindir, bootdir = bootdir }
156 :     { arch = arch, os = os }
157 :    
158 : blume 592 val param =
159 : blume 537 { fnpolicy = fnpolicy,
160 : blume 666 penv = penv,
161 : blume 433 symval = SSV.symval,
162 : blume 592 keep_going = keep_going }
163 : blume 349
164 : blume 327 val emptydyn = E.dynamicPart E.emptyEnv
165 :    
166 :     (* first, build an initial GeneralParam.info, so we can
167 :     * deal with the pervasive env and friends... *)
168 :    
169 :     val groupreg = GroupReg.new ()
170 :     val errcons = EM.defaultConsumer ()
171 : blume 592 val ginfo = { param = param, groupreg = groupreg, errcons = errcons }
172 : blume 327
173 : blume 449 fun mk_main_compile arg = let
174 : blume 450
175 : blume 592 val { pervasive = perv_n, others, src } = arg
176 : blume 327
177 : blume 569 fun recompInitGroup () = let
178 :     val ovldR = GenericVC.Control.overloadKW
179 :     val savedOvld = !ovldR
180 :     val _ = ovldR := true
181 :     val sbnode = Compile.newSbnodeTraversal ()
182 : blume 329
183 : blume 569 val perv_fsbnode = (NONE, perv_n)
184 : blume 327
185 : blume 569 fun rt n = valOf (sbnode ginfo n)
186 :     val pervasive = rt perv_n
187 : blume 537
188 : blume 569 fun rt2ie (n, ii: IInfo.info) = let
189 : blume 587 val s = #statenv ii ()
190 :     val (dae, mkDomain) = Statenv2DAEnv.cvt s
191 : blume 652 val domain = mkDomain ()
192 : blume 569 in
193 : blume 652 { ie = (fn () => (NONE, n), dae, domain), domain = domain }
194 : blume 569 end
195 : blume 537
196 : blume 569 fun add_exports (n, exports) = let
197 : blume 652 val { ie, domain } = rt2ie (n, rt n)
198 : blume 569 fun ins_ie (sy, m) = SymbolMap.insert (m, sy, ie)
199 :     in
200 : blume 652 SymbolSet.foldl ins_ie exports domain
201 : blume 569 end
202 :    
203 :     val special_exports = let
204 :     fun mkie (n, rtn) = #ie (rt2ie (n, rtn))
205 :     in
206 : blume 592 SymbolMap.insert (SymbolMap.empty,
207 :     PervAccess.pervStrSym,
208 :     mkie (perv_n, pervasive))
209 : blume 569 end
210 : blume 537 in
211 : blume 592 GG.GROUP { exports = foldl add_exports special_exports others,
212 : blume 632 kind = GG.LIB {
213 :     kind = GG.DEVELOPED { wrapped = StringSet.empty,
214 : blume 592 subgroups = [] },
215 : blume 632 version = NONE },
216 : blume 592 required = StringSet.singleton "primitive",
217 :     grouppath = initgspec,
218 : blume 642 (* hack: sources never used for this group *)
219 :     sources = SrcPathMap.empty,
220 : blume 592 sublibs = [] }
221 : blume 569 before (ovldR := savedOvld)
222 : blume 537 end
223 : blume 327
224 : blume 569 (* just go and load the stable init group or signal failure *)
225 :     fun loadInitGroup () = let
226 :     val lsarg =
227 :     { getGroup = fn _ => raise Fail "CMB: initial getGroup",
228 :     anyerrors = ref false }
229 : blume 537 in
230 : blume 666 case Stabilize.loadStable lsarg (ginfo, initgspec, NONE, []) of
231 : blume 569 NONE => NONE
232 : blume 592 | SOME (g as GG.GROUP { exports, ... }) => SOME g
233 : blume 587 | SOME GG.ERRORGROUP => NONE
234 : blume 537 end
235 : blume 569
236 :     (* Don't try to load the stable init group. Instead, recompile
237 :     * directly. *)
238 :     fun dontLoadInitGroup () = let
239 : blume 592 val g0 = recompInitGroup ()
240 : blume 569 val stabarg = { group = g0, anyerrors = ref false }
241 :     in
242 :     if deliver then
243 : blume 592 case Stabilize.stabilize ginfo stabarg of
244 :     SOME g => g
245 : blume 569 | NONE => raise Fail "CMB: cannot stabilize init group"
246 : blume 592 else g0
247 : blume 569 end
248 : blume 327
249 : blume 569 (* Try loading the init group from the stable file if possible;
250 :     * recompile if loading fails *)
251 :     fun tryLoadInitGroup () =
252 :     case loadInitGroup () of
253 :     SOME g => g
254 :     | NONE => dontLoadInitGroup ()
255 :    
256 :     (* Ok, now, based on "paranoid" and stable verification,
257 :     * call the appropriate function(s) to get the init group. *)
258 : blume 592 val init_group =
259 : blume 569 if paranoid then let
260 : blume 592 val export_nodes = perv_n :: others
261 : blume 569 val ver_arg = (initgspec, export_nodes, [],
262 : blume 632 SrcPathSet.empty, NONE)
263 : blume 569 val em = StableMap.empty
264 : blume 537 in
265 : blume 592 if VerifyStable.verify' ginfo em ver_arg then
266 : blume 569 tryLoadInitGroup ()
267 :     else dontLoadInitGroup ()
268 : blume 537 end
269 : blume 569 else tryLoadInitGroup ()
270 : blume 356
271 : blume 569
272 :     val stab = if deliver then SOME true else NONE
273 :    
274 : blume 537 val gr = GroupReg.new ()
275 :     val _ = GroupReg.register gr (initgspec, src)
276 :    
277 :     val parse_arg =
278 :     { load_plugin = load_plugin,
279 :     gr = gr,
280 :     param = param,
281 :     stabflag = stab,
282 :     group = maingspec,
283 :     init_group = init_group,
284 : blume 569 paranoid = paranoid }
285 : blume 327 in
286 : blume 456 Servers.dirbase dirbase;
287 : blume 632 Servers.cmb_new { archos = archos };
288 : blume 537 case Parse.parse parse_arg of
289 : blume 449 NONE => NONE
290 : blume 399 | SOME (g, gp) => let
291 : blume 449 fun thunk () = let
292 : blume 456 val _ = init_servers g
293 : blume 449 fun store _ = ()
294 :     val { group = recomp, ... } =
295 :     Compile.newTraversal (fn _ => fn _ => (), store, g)
296 : blume 450 val res =
297 :     Servers.withServers (fn () => recomp gp)
298 : blume 449 in
299 : blume 450 if isSome res then let
300 : blume 537 val { l = bootitems, ss } = mkBootList g
301 :     val stablelibs = Reachable.stableLibsOf g
302 :     fun inSet bi = StableSet.member (ss, bi)
303 :     val frontiers =
304 :     SrcPathMap.map (Reachable.frontier inSet)
305 :     stablelibs
306 :     fun writeBootList s = let
307 :     fun wr str = TextIO.output (s, str ^ "\n")
308 : blume 569 val numitems = length bootitems
309 :     fun biggerlen (s, n) = Int.max (size s, n)
310 :     val maxlen = foldl biggerlen 0 bootitems
311 : blume 364 in
312 : blume 569 wr (concat ["%", Int.toString numitems,
313 :     " ", Int.toString maxlen]);
314 : blume 537 app wr bootitems
315 : blume 364 end
316 : blume 537 fun writePid s i = let
317 :     val sn = BinInfo.stablename i
318 :     val os = BinInfo.offset i
319 :     val descr = BinInfo.describe i
320 :     val bfc = BFC.getStable
321 :     { stable = sn, offset = os, descr = descr }
322 :     in
323 :     case BF.exportPidOf bfc of
324 :     NONE => ()
325 :     | SOME pid =>
326 :     (TextIO.output (s, " ");
327 :     TextIO.output (s, PS.toHex pid))
328 :     end
329 :     fun writePidLine s (p, set) =
330 :     if StableSet.isEmpty set then ()
331 : blume 666 else (TextIO.output (s, SrcPath.encode p);
332 : blume 537 StableSet.app (writePid s) set;
333 :     TextIO.output (s, "\n"))
334 :     fun writePidMap s =
335 :     SrcPathMap.appi (writePidLine s) frontiers
336 : blume 349 in
337 : blume 449 if deliver then
338 :     (SafeIO.perform
339 :     { openIt = fn () =>
340 : blume 537 AutoDir.openTextOut listfile,
341 : blume 449 closeIt = TextIO.closeOut,
342 : blume 537 work = writeBootList,
343 : blume 459 cleanup = fn _ =>
344 : blume 537 OS.FileSys.remove listfile
345 : blume 449 handle _ => () };
346 :     SafeIO.perform
347 :     { openIt = fn () =>
348 : blume 537 AutoDir.openTextOut pidmapfile,
349 : blume 449 closeIt = TextIO.closeOut,
350 : blume 537 work = writePidMap,
351 : blume 459 cleanup = fn _ =>
352 : blume 537 OS.FileSys.remove pidmapfile
353 : blume 449 handle _ => () };
354 : blume 537 Say.say
355 :     ["New boot directory has been built.\n"])
356 : blume 449 else ();
357 :     true
358 : blume 349 end
359 : blume 449 else false
360 : blume 349 end
361 : blume 449 in
362 : blume 666 SOME ((g, gp, penv), thunk)
363 : blume 399 end
364 : blume 449 end handle Option => (Compile.reset (); NONE)
365 : blume 330 (* to catch valOf failures in "rt" *)
366 : blume 327 in
367 : blume 592 case BuildInitDG.build ginfo initgspec of
368 : blume 449 SOME x => mk_main_compile x
369 :     | NONE => NONE
370 : blume 327 end
371 : blume 362
372 : blume 569 fun compile dbopt =
373 : blume 632 (StabModmap.reset ();
374 :     case mk_compile { deliver = true, root = NONE,
375 :     dirbase = dbopt, paranoid = true } of
376 :     NONE => false
377 :     | SOME (_, thunk) => thunk ())
378 : blume 449
379 :     local
380 : blume 632 fun slave NONE = (StabModmap.reset (); NONE)
381 :     | slave (SOME (dirbase, root)) =
382 : blume 569 case mk_compile { deliver = false, root = SOME root,
383 :     dirbase = SOME dirbase, paranoid = false } of
384 : blume 449 NONE => NONE
385 : blume 666 | SOME ((g, gp, penv), _) => let
386 : blume 449 val trav = Compile.newSbnodeTraversal () gp
387 :     fun trav' sbn = isSome (trav sbn)
388 :     in
389 : blume 666 SOME (g, trav', penv)
390 : blume 449 end
391 :     in
392 : blume 452 val _ = CMBSlaveHook.init archos slave
393 : blume 449 end
394 :    
395 : blume 569 val make' = compile
396 : blume 377 fun make () = make' NONE
397 : blume 434 val symval = SSV.symval
398 : blume 327 end
399 : blume 569 end (* local *)

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