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