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 459 - (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 452 functor BootstrapCompileFn (structure MachDepVC : MACHDEP_VC
10 :     val os : SMLofNJ.SysInfo.os_kind) :> sig
11 : blume 364 val make' : string option -> bool
12 :     val make : unit -> bool
13 : blume 362 val deliver' : string option -> bool
14 :     val deliver : unit -> bool
15 :     val reset : unit -> unit
16 : blume 434 val symval : string -> { get: unit -> int option, set: int option -> unit }
17 : blume 357 end = struct
18 :    
19 : blume 327 structure EM = GenericVC.ErrorMsg
20 :     structure E = GenericVC.Environment
21 :     structure SE = GenericVC.CMStaticEnv
22 :     structure BE = GenericVC.BareEnvironment
23 :     structure PS = GenericVC.PersStamps
24 :     structure CoerceEnv = GenericVC.CoerceEnv
25 : blume 336 structure SSV = SpecificSymValFn (structure MachDepVC = MachDepVC
26 :     val os = os)
27 : blume 364 structure P = OS.Path
28 :     structure F = OS.FileSys
29 : blume 403 structure BF = MachDepVC.Binfile
30 : blume 327
31 : blume 452 val arch = MachDepVC.architecture
32 :     val osname = FilenamePolicy.kind2name os
33 :     val archos = concat [arch, "-", osname]
34 :    
35 : blume 456 fun init_servers (GroupGraph.GROUP { grouppath, ... }) =
36 :     Servers.cmb { archos = archos, root = grouppath }
37 :    
38 : blume 448 structure Compile = CompileFn (structure MachDepVC = MachDepVC
39 : blume 449 val compile_there = Servers.compile)
40 : blume 360
41 : blume 403 structure BFC = BfcFn (structure MachDepVC = MachDepVC)
42 :    
43 : blume 327 (* instantiate Stabilize... *)
44 : blume 329 structure Stabilize =
45 : blume 452 StabilizeFn (fun destroy_state _ i = Compile.evict i
46 : blume 403 structure MachDepVC = MachDepVC
47 : blume 399 fun recomp gp g = let
48 : blume 403 val { store, get } = BFC.new ()
49 : blume 456 val _ = init_servers g
50 : blume 399 val { group, ... } =
51 : blume 403 Compile.newTraversal (fn _ => fn _ => (),
52 :     store, g)
53 : blume 399 in
54 : blume 450 case Servers.withServers (fn () => group gp) of
55 : blume 403 NONE => NONE
56 :     | SOME _ => SOME get
57 :     end
58 :     val getII = Compile.getII)
59 : blume 398
60 : blume 327 (* ... and Parse *)
61 : blume 362 structure Parse = ParseFn (structure Stabilize = Stabilize
62 : blume 372 fun pending () = SymbolMap.empty)
63 : blume 327
64 : blume 366 (* copying an input file to an output file safely... *)
65 : blume 368 fun copyFile (oi, ci, oo, co, inp, outp, eof) (inf, outf) = let
66 :     fun workIn is = let
67 :     fun workOut os = let
68 : blume 366 val N = 4096
69 :     fun loop () =
70 : blume 368 if eof is then () else (outp (os, inp (is, N)); loop ())
71 : blume 366 in
72 :     loop ()
73 :     end
74 :     in
75 : blume 368 SafeIO.perform { openIt = fn () => oo outf,
76 :     closeIt = co,
77 : blume 366 work = workOut,
78 : blume 459 cleanup = fn _ =>
79 : blume 366 (F.remove outf handle _ => ()) }
80 :     end
81 : blume 364 in
82 : blume 368 SafeIO.perform { openIt = fn () => oi inf,
83 :     closeIt = ci,
84 : blume 366 work = workIn,
85 : blume 459 cleanup = fn _ => () }
86 : blume 364 end
87 : blume 327
88 : blume 368 val copyTextFile =
89 :     copyFile (TextIO.openIn, TextIO.closeIn,
90 :     AutoDir.openTextOut, TextIO.closeOut,
91 :     TextIO.inputN, TextIO.output, TextIO.endOfStream)
92 :    
93 :     val copyBinFile =
94 :     copyFile (BinIO.openIn, BinIO.closeIn,
95 :     AutoDir.openBinOut, BinIO.closeOut,
96 :     BinIO.inputN, BinIO.output, BinIO.endOfStream)
97 :    
98 : blume 456 fun mk_compile deliver root dbopt = let
99 : blume 358
100 : blume 360 val dirbase = getOpt (dbopt, BtNames.dirbaseDefault)
101 :     val pcmodespec = BtNames.pcmodespec
102 :     val initgspec = BtNames.initgspec
103 :     val maingspec = BtNames.maingspec
104 :    
105 : blume 452 val bindir = concat [dirbase, ".bin.", archos]
106 :     val bootdir = concat [dirbase, ".boot.", archos]
107 : blume 357
108 : blume 364 fun listName (p, copy) =
109 :     case P.fromString p of
110 :     { vol = "", isAbs = false, arcs = arc0 :: arc1 :: arcn } => let
111 :     fun win32name () =
112 :     concat (arc1 ::
113 :     foldr (fn (a, r) => "\\" :: a :: r) [] arcn)
114 :     fun doCopy () = let
115 :     val bootpath =
116 :     P.toString { isAbs = false, vol = "",
117 :     arcs = bootdir :: arc1 :: arcn }
118 :     in
119 : blume 368 copyBinFile (p, bootpath)
120 : blume 364 end
121 :     in
122 :     if copy andalso arc0 = bindir then doCopy () else ();
123 :     case os of
124 :     SMLofNJ.SysInfo.WIN32 => win32name ()
125 :     | _ => P.toString { isAbs = false, vol = "",
126 :     arcs = arc1 :: arcn }
127 :     end
128 :     | _ => raise Fail "BootstrapCompile:listName: bad name"
129 :    
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 364 val pidfile = P.joinDirFile { dir = bootdir, file = "RTPID" }
135 :     val listfile = P.joinDirFile { dir = bootdir, file = "BOOTLIST" }
136 : blume 329
137 : blume 361 val pcmode = PathConfig.new ()
138 :     val _ = PathConfig.processSpecFile (pcmode, pcmodespec)
139 : blume 329
140 : blume 354 fun stdpath s = SrcPath.standard pcmode { context = ctxt, spec = s }
141 : blume 352
142 :     val initgspec = stdpath initgspec
143 : blume 456 val maingspec =
144 :     case root of
145 :     NONE => stdpath maingspec
146 : blume 457 | SOME r => SrcPath.fromDescr pcmode r
147 : blume 352
148 : blume 360 val cmifile = valOf (SrcPath.reAnchoredName (initgspec, bootdir))
149 :     handle Option => raise Fail "BootstrapCompile: cmifile"
150 :    
151 : blume 364 val fnpolicy =
152 : blume 357 FilenamePolicy.separate { bindir = bindir, bootdir = bootdir }
153 :     { arch = arch, os = os }
154 :    
155 : blume 364 fun mkParam { primconf, pervasive, pervcorepids }
156 : blume 357 { corenv } =
157 : blume 349 { primconf = primconf,
158 :     fnpolicy = fnpolicy,
159 :     pcmode = pcmode,
160 : blume 433 symval = SSV.symval,
161 : blume 349 keep_going = keep_going,
162 :     pervasive = pervasive,
163 :     corenv = corenv,
164 :     pervcorepids = pervcorepids }
165 :    
166 : blume 327 val emptydyn = E.dynamicPart E.emptyEnv
167 :    
168 :     (* first, build an initial GeneralParam.info, so we can
169 :     * deal with the pervasive env and friends... *)
170 :    
171 : blume 360 val primconf = Primitive.primEnvConf
172 : blume 349 val mkInitParam = mkParam { primconf = primconf,
173 :     pervasive = E.emptyEnv,
174 : blume 364 pervcorepids = PidSet.empty }
175 : blume 327
176 : blume 349 val param_nocore = mkInitParam { corenv = BE.staticPart BE.emptyEnv }
177 :    
178 : blume 327 val groupreg = GroupReg.new ()
179 :     val errcons = EM.defaultConsumer ()
180 : blume 329 val ginfo_nocore = { param = param_nocore, groupreg = groupreg,
181 :     errcons = errcons }
182 : blume 327
183 : blume 449 fun mk_main_compile arg = let
184 : blume 450
185 : blume 335 val { rts, core, pervasive, primitives, binpaths } = arg
186 : blume 327
187 : blume 329 val ovldR = GenericVC.Control.overloadKW
188 :     val savedOvld = !ovldR
189 :     val _ = ovldR := true
190 : blume 400 val sbnode = Compile.newSbnodeTraversal ()
191 : blume 329
192 : blume 327 (* here we build a new gp -- the one that uses the freshly
193 :     * brewed pervasive env, core env, and primitives *)
194 : blume 398 val core = valOf (sbnode ginfo_nocore core)
195 :     val corenv = CoerceEnv.es2bs (#statenv (#ii core) ())
196 :     val core_sym = #symenv (#ii core) ()
197 : blume 329
198 :     (* The following is a bit of a hack (but corenv is a hack anyway):
199 :     * As soon as we have core available, we have to patch the
200 :     * ginfo to include the correct corenv (because virtually
201 :     * everybody else needs access to corenv). *)
202 : blume 349 val param_justcore = mkInitParam { corenv = corenv }
203 : blume 329 val ginfo_justcore = { param = param_justcore, groupreg = groupreg,
204 :     errcons = errcons }
205 :    
206 : blume 398 fun rt n = valOf (sbnode ginfo_justcore n)
207 : blume 327 val rts = rt rts
208 :     val pervasive = rt pervasive
209 :    
210 :     fun sn2pspec (name, n) = let
211 : blume 398 val { ii = { statenv, symenv, statpid, sympid }, ctxt } = rt n
212 : blume 327 val env =
213 : blume 398 E.mkenv { static = statenv (),
214 :     symbolic = symenv (),
215 :     dynamic = emptydyn }
216 :     val pidInfo =
217 :     { statpid = statpid, sympid = sympid, ctxt = ctxt }
218 : blume 327 in
219 :     { name = name, env = env, pidInfo = pidInfo }
220 :     end
221 :    
222 :     val pspecs = map sn2pspec primitives
223 :    
224 : blume 329 val _ = ovldR := savedOvld
225 :    
226 : blume 372 (* The following is a hack but must be done for both the symbolic
227 : blume 356 * and later the dynamic part of the core environment:
228 :     * we must include these parts in the pervasive env. *)
229 : blume 398 val perv_sym = E.layerSymbolic (#symenv (#ii pervasive) (),
230 :     core_sym)
231 : blume 356
232 : blume 349 val param =
233 :     mkParam { primconf = Primitive.configuration pspecs,
234 : blume 398 pervasive = E.mkenv { static =
235 :     #statenv (#ii pervasive) (),
236 : blume 356 symbolic = perv_sym,
237 : blume 327 dynamic = emptydyn },
238 :     pervcorepids =
239 :     PidSet.addList (PidSet.empty,
240 : blume 398 [#statpid (#ii pervasive),
241 :     #sympid (#ii pervasive),
242 :     #statpid (#ii core)]) }
243 : blume 349 { corenv = corenv }
244 : blume 362 val stab =
245 :     if deliver then SOME true else NONE
246 : blume 327 in
247 : blume 456 Servers.dirbase dirbase;
248 : blume 362 case Parse.parse NONE param stab maingspec of
249 : blume 449 NONE => NONE
250 : blume 399 | SOME (g, gp) => let
251 : blume 449 fun thunk () = let
252 : blume 456 val _ = init_servers g
253 : blume 449 fun store _ = ()
254 :     val { group = recomp, ... } =
255 :     Compile.newTraversal (fn _ => fn _ => (), store, g)
256 : blume 450 val res =
257 :     Servers.withServers (fn () => recomp gp)
258 : blume 449 in
259 : blume 450 if isSome res then let
260 : blume 449 val rtspid = PS.toHex (#statpid (#ii rts))
261 :     fun writeList s = let
262 :     fun add ((p, flag), l) = let
263 :     val n = listName (p, true)
264 :     in
265 :     if flag then n :: l else l
266 :     end
267 :     fun transcribe (p, NONE) = listName (p, true)
268 :     | transcribe (p, SOME (off, desc)) =
269 :     concat [listName (p, false),
270 :     "@", Int.toString off, ":", desc]
271 :     val bootstrings =
272 :     foldr add
273 :     (map transcribe (MkBootList.group g))
274 :     binpaths
275 :     fun show str =
276 :     (TextIO.output (s, str);
277 :     TextIO.output (s, "\n"))
278 : blume 364 in
279 : blume 449 app show bootstrings
280 : blume 364 end
281 : blume 349 in
282 : blume 449 if deliver then
283 :     (SafeIO.perform
284 :     { openIt = fn () =>
285 :     AutoDir.openTextOut pidfile,
286 :     closeIt = TextIO.closeOut,
287 :     work = fn s =>
288 :     TextIO.output (s, rtspid ^ "\n"),
289 : blume 459 cleanup = fn _ =>
290 : blume 449 OS.FileSys.remove pidfile
291 :     handle _ => () };
292 :     SafeIO.perform
293 :     { openIt = fn () =>
294 :     AutoDir.openTextOut listfile,
295 :     closeIt = TextIO.closeOut,
296 :     work = writeList,
297 : blume 459 cleanup = fn _ =>
298 : blume 449 OS.FileSys.remove listfile
299 :     handle _ => () };
300 :     copyTextFile (SrcPath.osstring initgspec,
301 :     cmifile);
302 :     Say.say ["Runtime System PID is: ",
303 :     rtspid, "\n"])
304 :     else ();
305 :     true
306 : blume 349 end
307 : blume 449 else false
308 : blume 349 end
309 : blume 449 in
310 : blume 457 SOME ((g, gp, pcmode), thunk)
311 : blume 399 end
312 : blume 449 end handle Option => (Compile.reset (); NONE)
313 : blume 330 (* to catch valOf failures in "rt" *)
314 : blume 327 in
315 : blume 329 case BuildInitDG.build ginfo_nocore initgspec of
316 : blume 449 SOME x => mk_main_compile x
317 :     | NONE => NONE
318 : blume 327 end
319 : blume 362
320 : blume 449 fun compile deliver dbopt =
321 : blume 456 case mk_compile deliver NONE dbopt of
322 : blume 450 NONE => false
323 : blume 459 | SOME (_, thunk) => thunk ()
324 : blume 449
325 :     local
326 : blume 456 fun slave (dirbase, root) =
327 :     case mk_compile false (SOME root) (SOME dirbase) of
328 : blume 449 NONE => NONE
329 : blume 457 | SOME ((g, gp, pcmode), _) => let
330 : blume 449 val trav = Compile.newSbnodeTraversal () gp
331 :     fun trav' sbn = isSome (trav sbn)
332 :     in
333 : blume 457 SOME (g, trav', pcmode)
334 : blume 449 end
335 :     in
336 : blume 452 val _ = CMBSlaveHook.init archos slave
337 : blume 449 end
338 :    
339 : blume 362 fun reset () =
340 : blume 398 (Compile.reset ();
341 : blume 367 Parse.reset ())
342 : blume 377
343 :     val make' = compile false
344 :     fun make () = make' NONE
345 :     fun deliver' arg =
346 :     SafeIO.perform { openIt = fn () => (),
347 :     closeIt = reset,
348 :     work = fn () => compile true arg,
349 : blume 459 cleanup = fn _ => () }
350 : blume 377 fun deliver () = deliver' NONE
351 : blume 434 val symval = SSV.symval
352 : blume 327 end

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