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

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