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 460 - (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 : blume 460 val corenv = CoerceEnv.es2bs (#statenv core ())
196 :     val core_sym = #symenv 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 460 val { statenv, symenv, statpid, sympid } = rt n
212 : blume 327 val env =
213 : blume 398 E.mkenv { static = statenv (),
214 :     symbolic = symenv (),
215 :     dynamic = emptydyn }
216 : blume 460 val pidInfo = { statpid = statpid, sympid = sympid }
217 : blume 327 in
218 :     { name = name, env = env, pidInfo = pidInfo }
219 :     end
220 :    
221 :     val pspecs = map sn2pspec primitives
222 :    
223 : blume 329 val _ = ovldR := savedOvld
224 :    
225 : blume 372 (* The following is a hack but must be done for both the symbolic
226 : blume 356 * and later the dynamic part of the core environment:
227 :     * we must include these parts in the pervasive env. *)
228 : blume 460 val perv_sym = E.layerSymbolic (#symenv pervasive (),
229 : blume 398 core_sym)
230 : blume 356
231 : blume 349 val param =
232 :     mkParam { primconf = Primitive.configuration pspecs,
233 : blume 460 pervasive = E.mkenv { static = #statenv pervasive (),
234 : blume 356 symbolic = perv_sym,
235 : blume 327 dynamic = emptydyn },
236 :     pervcorepids =
237 :     PidSet.addList (PidSet.empty,
238 : blume 460 [#statpid pervasive,
239 :     #sympid pervasive,
240 :     #statpid core]) }
241 : blume 349 { corenv = corenv }
242 : blume 362 val stab =
243 :     if deliver then SOME true else NONE
244 : blume 327 in
245 : blume 456 Servers.dirbase dirbase;
246 : blume 362 case Parse.parse NONE param stab maingspec of
247 : blume 449 NONE => NONE
248 : blume 399 | SOME (g, gp) => let
249 : blume 449 fun thunk () = let
250 : blume 456 val _ = init_servers g
251 : blume 449 fun store _ = ()
252 :     val { group = recomp, ... } =
253 :     Compile.newTraversal (fn _ => fn _ => (), store, g)
254 : blume 450 val res =
255 :     Servers.withServers (fn () => recomp gp)
256 : blume 449 in
257 : blume 450 if isSome res then let
258 : blume 460 val rtspid = PS.toHex (#statpid rts)
259 : blume 449 fun writeList s = let
260 :     fun add ((p, flag), l) = let
261 :     val n = listName (p, true)
262 :     in
263 :     if flag then n :: l else l
264 :     end
265 :     fun transcribe (p, NONE) = listName (p, true)
266 :     | transcribe (p, SOME (off, desc)) =
267 :     concat [listName (p, false),
268 :     "@", Int.toString off, ":", desc]
269 :     val bootstrings =
270 :     foldr add
271 :     (map transcribe (MkBootList.group g))
272 :     binpaths
273 :     fun show str =
274 :     (TextIO.output (s, str);
275 :     TextIO.output (s, "\n"))
276 : blume 364 in
277 : blume 449 app show bootstrings
278 : blume 364 end
279 : blume 349 in
280 : blume 449 if deliver then
281 :     (SafeIO.perform
282 :     { openIt = fn () =>
283 :     AutoDir.openTextOut pidfile,
284 :     closeIt = TextIO.closeOut,
285 :     work = fn s =>
286 :     TextIO.output (s, rtspid ^ "\n"),
287 : blume 459 cleanup = fn _ =>
288 : blume 449 OS.FileSys.remove pidfile
289 :     handle _ => () };
290 :     SafeIO.perform
291 :     { openIt = fn () =>
292 :     AutoDir.openTextOut listfile,
293 :     closeIt = TextIO.closeOut,
294 :     work = writeList,
295 : blume 459 cleanup = fn _ =>
296 : blume 449 OS.FileSys.remove listfile
297 :     handle _ => () };
298 :     copyTextFile (SrcPath.osstring initgspec,
299 :     cmifile);
300 :     Say.say ["Runtime System PID is: ",
301 :     rtspid, "\n"])
302 :     else ();
303 :     true
304 : blume 349 end
305 : blume 449 else false
306 : blume 349 end
307 : blume 449 in
308 : blume 457 SOME ((g, gp, pcmode), thunk)
309 : blume 399 end
310 : blume 449 end handle Option => (Compile.reset (); NONE)
311 : blume 330 (* to catch valOf failures in "rt" *)
312 : blume 327 in
313 : blume 329 case BuildInitDG.build ginfo_nocore initgspec of
314 : blume 449 SOME x => mk_main_compile x
315 :     | NONE => NONE
316 : blume 327 end
317 : blume 362
318 : blume 449 fun compile deliver dbopt =
319 : blume 456 case mk_compile deliver NONE dbopt of
320 : blume 450 NONE => false
321 : blume 459 | SOME (_, thunk) => thunk ()
322 : blume 449
323 :     local
324 : blume 456 fun slave (dirbase, root) =
325 :     case mk_compile false (SOME root) (SOME dirbase) of
326 : blume 449 NONE => NONE
327 : blume 457 | SOME ((g, gp, pcmode), _) => let
328 : blume 449 val trav = Compile.newSbnodeTraversal () gp
329 :     fun trav' sbn = isSome (trav sbn)
330 :     in
331 : blume 457 SOME (g, trav', pcmode)
332 : blume 449 end
333 :     in
334 : blume 452 val _ = CMBSlaveHook.init archos slave
335 : blume 449 end
336 :    
337 : blume 362 fun reset () =
338 : blume 398 (Compile.reset ();
339 : blume 367 Parse.reset ())
340 : blume 377
341 :     val make' = compile false
342 :     fun make () = make' NONE
343 :     fun deliver' arg =
344 :     SafeIO.perform { openIt = fn () => (),
345 :     closeIt = reset,
346 :     work = fn () => compile true arg,
347 : blume 459 cleanup = fn _ => () }
348 : blume 377 fun deliver () = deliver' NONE
349 : blume 434 val symval = SSV.symval
350 : blume 327 end

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