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

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