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

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