SCM Repository
Annotation of /sml/trunk/src/cm/bootstrap/btcompile.sml
Parent Directory
|
Revision Log
Revision 465 - (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 | 360 | val cmifile = valOf (SrcPath.reAnchoredName (initgspec, bootdir)) |
151 : | handle Option => raise Fail "BootstrapCompile: cmifile" | ||
152 : | |||
153 : | blume | 364 | val fnpolicy = |
154 : | blume | 357 | FilenamePolicy.separate { bindir = bindir, bootdir = bootdir } |
155 : | { arch = arch, os = os } | ||
156 : | |||
157 : | blume | 364 | fun mkParam { primconf, pervasive, pervcorepids } |
158 : | blume | 357 | { corenv } = |
159 : | blume | 349 | { primconf = primconf, |
160 : | fnpolicy = fnpolicy, | ||
161 : | pcmode = pcmode, | ||
162 : | blume | 433 | symval = SSV.symval, |
163 : | blume | 349 | keep_going = keep_going, |
164 : | pervasive = pervasive, | ||
165 : | corenv = corenv, | ||
166 : | pervcorepids = pervcorepids } | ||
167 : | |||
168 : | blume | 327 | val emptydyn = E.dynamicPart E.emptyEnv |
169 : | |||
170 : | (* first, build an initial GeneralParam.info, so we can | ||
171 : | * deal with the pervasive env and friends... *) | ||
172 : | |||
173 : | blume | 360 | val primconf = Primitive.primEnvConf |
174 : | blume | 349 | val mkInitParam = mkParam { primconf = primconf, |
175 : | pervasive = E.emptyEnv, | ||
176 : | blume | 364 | pervcorepids = PidSet.empty } |
177 : | blume | 327 | |
178 : | blume | 349 | val param_nocore = mkInitParam { corenv = BE.staticPart BE.emptyEnv } |
179 : | |||
180 : | blume | 327 | val groupreg = GroupReg.new () |
181 : | val errcons = EM.defaultConsumer () | ||
182 : | blume | 329 | val ginfo_nocore = { param = param_nocore, groupreg = groupreg, |
183 : | errcons = errcons } | ||
184 : | blume | 327 | |
185 : | blume | 449 | fun mk_main_compile arg = let |
186 : | blume | 450 | |
187 : | blume | 335 | val { rts, core, pervasive, primitives, binpaths } = arg |
188 : | blume | 327 | |
189 : | blume | 329 | val ovldR = GenericVC.Control.overloadKW |
190 : | val savedOvld = !ovldR | ||
191 : | val _ = ovldR := true | ||
192 : | blume | 400 | val sbnode = Compile.newSbnodeTraversal () |
193 : | blume | 329 | |
194 : | blume | 327 | (* here we build a new gp -- the one that uses the freshly |
195 : | * brewed pervasive env, core env, and primitives *) | ||
196 : | blume | 398 | val core = valOf (sbnode ginfo_nocore core) |
197 : | blume | 461 | val corenv = CoerceEnv.es2bs (#env (#statenv core ())) |
198 : | blume | 460 | val core_sym = #symenv core () |
199 : | blume | 329 | |
200 : | (* The following is a bit of a hack (but corenv is a hack anyway): | ||
201 : | * As soon as we have core available, we have to patch the | ||
202 : | * ginfo to include the correct corenv (because virtually | ||
203 : | * everybody else needs access to corenv). *) | ||
204 : | blume | 349 | val param_justcore = mkInitParam { corenv = corenv } |
205 : | blume | 329 | val ginfo_justcore = { param = param_justcore, groupreg = groupreg, |
206 : | errcons = errcons } | ||
207 : | |||
208 : | blume | 398 | fun rt n = valOf (sbnode ginfo_justcore n) |
209 : | blume | 327 | val rts = rt rts |
210 : | val pervasive = rt pervasive | ||
211 : | |||
212 : | fun sn2pspec (name, n) = let | ||
213 : | blume | 460 | val { statenv, symenv, statpid, sympid } = rt n |
214 : | blume | 461 | val { env = static, ctxt } = statenv () |
215 : | blume | 327 | val env = |
216 : | blume | 461 | E.mkenv { static = static, |
217 : | blume | 398 | symbolic = symenv (), |
218 : | dynamic = emptydyn } | ||
219 : | blume | 461 | val pidInfo = |
220 : | { statpid = statpid, sympid = sympid, ctxt = ctxt } | ||
221 : | blume | 327 | in |
222 : | { name = name, env = env, pidInfo = pidInfo } | ||
223 : | end | ||
224 : | |||
225 : | val pspecs = map sn2pspec primitives | ||
226 : | |||
227 : | blume | 329 | val _ = ovldR := savedOvld |
228 : | |||
229 : | blume | 372 | (* The following is a hack but must be done for both the symbolic |
230 : | blume | 356 | * and later the dynamic part of the core environment: |
231 : | * we must include these parts in the pervasive env. *) | ||
232 : | blume | 460 | val perv_sym = E.layerSymbolic (#symenv pervasive (), |
233 : | blume | 398 | core_sym) |
234 : | blume | 356 | |
235 : | blume | 349 | val param = |
236 : | mkParam { primconf = Primitive.configuration pspecs, | ||
237 : | blume | 461 | pervasive = |
238 : | E.mkenv { static = #env (#statenv pervasive ()), | ||
239 : | symbolic = perv_sym, | ||
240 : | dynamic = emptydyn }, | ||
241 : | blume | 327 | pervcorepids = |
242 : | PidSet.addList (PidSet.empty, | ||
243 : | blume | 460 | [#statpid pervasive, |
244 : | #sympid pervasive, | ||
245 : | #statpid core]) } | ||
246 : | blume | 349 | { corenv = corenv } |
247 : | blume | 362 | val stab = |
248 : | if deliver then SOME true else NONE | ||
249 : | blume | 327 | in |
250 : | blume | 456 | Servers.dirbase dirbase; |
251 : | blume | 362 | case Parse.parse NONE param stab maingspec of |
252 : | blume | 449 | NONE => NONE |
253 : | blume | 399 | | SOME (g, gp) => let |
254 : | blume | 449 | fun thunk () = let |
255 : | blume | 456 | val _ = init_servers g |
256 : | blume | 449 | fun store _ = () |
257 : | val { group = recomp, ... } = | ||
258 : | Compile.newTraversal (fn _ => fn _ => (), store, g) | ||
259 : | blume | 450 | val res = |
260 : | Servers.withServers (fn () => recomp gp) | ||
261 : | blume | 449 | in |
262 : | blume | 450 | if isSome res then let |
263 : | blume | 460 | val rtspid = PS.toHex (#statpid rts) |
264 : | blume | 449 | fun writeList s = let |
265 : | fun add ((p, flag), l) = let | ||
266 : | val n = listName (p, true) | ||
267 : | in | ||
268 : | if flag then n :: l else l | ||
269 : | end | ||
270 : | fun transcribe (p, NONE) = listName (p, true) | ||
271 : | | transcribe (p, SOME (off, desc)) = | ||
272 : | concat [listName (p, false), | ||
273 : | "@", Int.toString off, ":", desc] | ||
274 : | val bootstrings = | ||
275 : | foldr add | ||
276 : | (map transcribe (MkBootList.group g)) | ||
277 : | binpaths | ||
278 : | fun show str = | ||
279 : | (TextIO.output (s, str); | ||
280 : | TextIO.output (s, "\n")) | ||
281 : | blume | 364 | in |
282 : | blume | 449 | app show bootstrings |
283 : | blume | 364 | end |
284 : | blume | 349 | in |
285 : | blume | 449 | if deliver then |
286 : | (SafeIO.perform | ||
287 : | { openIt = fn () => | ||
288 : | AutoDir.openTextOut pidfile, | ||
289 : | closeIt = TextIO.closeOut, | ||
290 : | work = fn s => | ||
291 : | TextIO.output (s, rtspid ^ "\n"), | ||
292 : | blume | 459 | cleanup = fn _ => |
293 : | blume | 449 | OS.FileSys.remove pidfile |
294 : | handle _ => () }; | ||
295 : | SafeIO.perform | ||
296 : | { openIt = fn () => | ||
297 : | AutoDir.openTextOut listfile, | ||
298 : | closeIt = TextIO.closeOut, | ||
299 : | work = writeList, | ||
300 : | blume | 459 | cleanup = fn _ => |
301 : | blume | 449 | OS.FileSys.remove listfile |
302 : | handle _ => () }; | ||
303 : | copyTextFile (SrcPath.osstring initgspec, | ||
304 : | cmifile); | ||
305 : | Say.say ["Runtime System PID is: ", | ||
306 : | rtspid, "\n"]) | ||
307 : | else (); | ||
308 : | true | ||
309 : | blume | 349 | end |
310 : | blume | 449 | else false |
311 : | blume | 349 | end |
312 : | blume | 449 | in |
313 : | blume | 457 | SOME ((g, gp, pcmode), thunk) |
314 : | blume | 399 | end |
315 : | blume | 449 | end handle Option => (Compile.reset (); NONE) |
316 : | blume | 330 | (* to catch valOf failures in "rt" *) |
317 : | blume | 327 | in |
318 : | blume | 329 | case BuildInitDG.build ginfo_nocore initgspec of |
319 : | blume | 449 | SOME x => mk_main_compile x |
320 : | | NONE => NONE | ||
321 : | blume | 327 | end |
322 : | blume | 362 | |
323 : | blume | 449 | fun compile deliver dbopt = |
324 : | blume | 456 | case mk_compile deliver NONE dbopt of |
325 : | blume | 450 | NONE => false |
326 : | blume | 459 | | SOME (_, thunk) => thunk () |
327 : | blume | 449 | |
328 : | local | ||
329 : | blume | 456 | fun slave (dirbase, root) = |
330 : | case mk_compile false (SOME root) (SOME dirbase) of | ||
331 : | blume | 449 | NONE => NONE |
332 : | blume | 457 | | SOME ((g, gp, pcmode), _) => let |
333 : | blume | 449 | val trav = Compile.newSbnodeTraversal () gp |
334 : | fun trav' sbn = isSome (trav sbn) | ||
335 : | in | ||
336 : | blume | 457 | SOME (g, trav', pcmode) |
337 : | blume | 449 | end |
338 : | in | ||
339 : | blume | 452 | val _ = CMBSlaveHook.init archos slave |
340 : | blume | 449 | end |
341 : | |||
342 : | blume | 362 | fun reset () = |
343 : | blume | 398 | (Compile.reset (); |
344 : | blume | 367 | Parse.reset ()) |
345 : | blume | 377 | |
346 : | val make' = compile false | ||
347 : | fun make () = make' NONE | ||
348 : | fun deliver' arg = | ||
349 : | SafeIO.perform { openIt = fn () => (), | ||
350 : | closeIt = reset, | ||
351 : | work = fn () => compile true arg, | ||
352 : | blume | 459 | cleanup = fn _ => () } |
353 : | blume | 377 | fun deliver () = deliver' NONE |
354 : | blume | 434 | val symval = SSV.symval |
355 : | blume | 327 | end |
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |