SCM Repository
Annotation of /sml/trunk/src/cm/bootstrap/btcompile.sml
Parent Directory
|
Revision Log
Revision 377 - (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 | 357 | end = struct |
17 : | |||
18 : | blume | 327 | structure EM = GenericVC.ErrorMsg |
19 : | structure E = GenericVC.Environment | ||
20 : | structure SE = GenericVC.CMStaticEnv | ||
21 : | structure BE = GenericVC.BareEnvironment | ||
22 : | structure PS = GenericVC.PersStamps | ||
23 : | structure CoerceEnv = GenericVC.CoerceEnv | ||
24 : | blume | 336 | structure SSV = SpecificSymValFn (structure MachDepVC = MachDepVC |
25 : | val os = os) | ||
26 : | blume | 364 | structure P = OS.Path |
27 : | structure F = OS.FileSys | ||
28 : | blume | 327 | |
29 : | (* Since the bootstrap compiler never executes any of the code | ||
30 : | * it produces, we don't need any dynamic values. Therefore, | ||
31 : | * we create RecompPersstate (but not FullPersstate!) and | ||
32 : | * instantiate Recomp as well as RecompTraversal. | ||
33 : | * Since RecompPersstate is not part of any surrounding FullPersstate, | ||
34 : | * function "discard_value" simply does nothing. *) | ||
35 : | structure RecompPersstate = | ||
36 : | RecompPersstateFn (structure MachDepVC = MachDepVC | ||
37 : | val discard_code = true | ||
38 : | blume | 362 | fun stable_value_present i = false |
39 : | fun new_smlinfo i = ()) | ||
40 : | blume | 360 | |
41 : | blume | 327 | structure Recomp = RecompFn (structure PS = RecompPersstate) |
42 : | blume | 371 | structure RT = CompileGenericFn (structure CT = Recomp) |
43 : | blume | 327 | |
44 : | fun recomp gp g = isSome (RT.group gp g) | ||
45 : | |||
46 : | (* instantiate Stabilize... *) | ||
47 : | blume | 329 | structure Stabilize = |
48 : | blume | 372 | StabilizeFn (fun bn2statenv gp i = #1 (#stat (valOf (RT.bnode' gp i))) |
49 : | blume | 357 | fun warmup (i, p) = () |
50 : | blume | 363 | val recomp = recomp |
51 : | val transfer_state = RecompPersstate.transfer_state) | ||
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 | 362 | fun 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 | 329 | val keep_going = EnvConfig.getSet StdConfig.keep_going NONE |
125 : | |||
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 : | symenv = SSV.env, | ||
152 : | 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 : | fun main_compile arg = let | ||
175 : | blume | 335 | val { rts, core, pervasive, primitives, binpaths } = arg |
176 : | blume | 327 | |
177 : | blume | 329 | val ovldR = GenericVC.Control.overloadKW |
178 : | val savedOvld = !ovldR | ||
179 : | val _ = ovldR := true | ||
180 : | blume | 372 | val ts = RT.start () |
181 : | blume | 329 | |
182 : | blume | 327 | (* here we build a new gp -- the one that uses the freshly |
183 : | * brewed pervasive env, core env, and primitives *) | ||
184 : | blume | 372 | val core = valOf (RT.sbnode ts ginfo_nocore core) |
185 : | blume | 329 | val corenv = CoerceEnv.es2bs (#1 (#stat core)) |
186 : | blume | 356 | val core_sym = #1 (#sym core) |
187 : | blume | 329 | |
188 : | (* The following is a bit of a hack (but corenv is a hack anyway): | ||
189 : | * As soon as we have core available, we have to patch the | ||
190 : | * ginfo to include the correct corenv (because virtually | ||
191 : | * everybody else needs access to corenv). *) | ||
192 : | blume | 349 | val param_justcore = mkInitParam { corenv = corenv } |
193 : | blume | 329 | val ginfo_justcore = { param = param_justcore, groupreg = groupreg, |
194 : | errcons = errcons } | ||
195 : | |||
196 : | blume | 372 | fun rt n = valOf (RT.sbnode ts ginfo_justcore n) |
197 : | blume | 327 | val rts = rt rts |
198 : | val pervasive = rt pervasive | ||
199 : | |||
200 : | fun sn2pspec (name, n) = let | ||
201 : | blume | 369 | val { stat = (s, sp), sym = (sy, syp), ctxt, bfc } = rt n |
202 : | blume | 327 | val env = |
203 : | E.mkenv { static = s, symbolic = sy, dynamic = emptydyn } | ||
204 : | val pidInfo = { statpid = sp, sympid = syp, ctxt = ctxt } | ||
205 : | in | ||
206 : | { name = name, env = env, pidInfo = pidInfo } | ||
207 : | end | ||
208 : | |||
209 : | val pspecs = map sn2pspec primitives | ||
210 : | |||
211 : | blume | 329 | val _ = ovldR := savedOvld |
212 : | |||
213 : | blume | 372 | (* To be consistent, we would have to call RT.finish here. |
214 : | * However, this isn't really necessary because no dynamic | ||
215 : | * values exist and we drop "ts" at this point anyway. *) | ||
216 : | (* val _ = RT.finish ts *) | ||
217 : | |||
218 : | (* The following is a hack but must be done for both the symbolic | ||
219 : | blume | 356 | * and later the dynamic part of the core environment: |
220 : | * we must include these parts in the pervasive env. *) | ||
221 : | val perv_sym = E.layerSymbolic (#1 (#sym pervasive), core_sym) | ||
222 : | |||
223 : | blume | 349 | val param = |
224 : | mkParam { primconf = Primitive.configuration pspecs, | ||
225 : | blume | 327 | pervasive = E.mkenv { static = #1 (#stat pervasive), |
226 : | blume | 356 | symbolic = perv_sym, |
227 : | blume | 327 | dynamic = emptydyn }, |
228 : | pervcorepids = | ||
229 : | PidSet.addList (PidSet.empty, | ||
230 : | [#2 (#stat pervasive), | ||
231 : | #2 (#sym pervasive), | ||
232 : | blume | 364 | #2 (#stat core)]) } |
233 : | blume | 349 | { corenv = corenv } |
234 : | blume | 362 | val stab = |
235 : | if deliver then SOME true else NONE | ||
236 : | blume | 327 | in |
237 : | blume | 362 | case Parse.parse NONE param stab maingspec of |
238 : | blume | 349 | NONE => false |
239 : | blume | 329 | | SOME (g, gp) => |
240 : | blume | 349 | if recomp gp g then let |
241 : | val rtspid = PS.toHex (#2 (#stat rts)) | ||
242 : | fun writeList s = let | ||
243 : | blume | 364 | fun add ((p, flag), l) = let |
244 : | val n = listName (p, true) | ||
245 : | in | ||
246 : | if flag then n :: l else l | ||
247 : | end | ||
248 : | fun transcribe (p, NONE) = listName (p, true) | ||
249 : | | transcribe (p, SOME (off, desc)) = | ||
250 : | concat [listName (p, false), | ||
251 : | "@", Int.toString off, ":", desc] | ||
252 : | val bootstrings = | ||
253 : | foldr add (map transcribe (MkBootList.group g)) | ||
254 : | binpaths | ||
255 : | fun show str = | ||
256 : | (TextIO.output (s, str); | ||
257 : | TextIO.output (s, "\n")) | ||
258 : | blume | 349 | in |
259 : | blume | 364 | app show bootstrings |
260 : | blume | 349 | end |
261 : | in | ||
262 : | blume | 362 | if deliver then |
263 : | (SafeIO.perform { openIt = fn () => | ||
264 : | blume | 354 | AutoDir.openTextOut pidfile, |
265 : | blume | 349 | closeIt = TextIO.closeOut, |
266 : | work = fn s => | ||
267 : | TextIO.output (s, rtspid ^ "\n"), | ||
268 : | cleanup = fn () => | ||
269 : | blume | 354 | OS.FileSys.remove pidfile |
270 : | handle _ => () }; | ||
271 : | blume | 349 | SafeIO.perform { openIt = fn () => |
272 : | blume | 354 | AutoDir.openTextOut listfile, |
273 : | blume | 349 | closeIt = TextIO.closeOut, |
274 : | work = writeList, | ||
275 : | cleanup = fn () => | ||
276 : | blume | 354 | OS.FileSys.remove listfile |
277 : | handle _ => () }; | ||
278 : | blume | 368 | copyTextFile (SrcPath.osstring initgspec, cmifile); |
279 : | blume | 366 | Say.say ["Runtime System PID is: ", rtspid, "\n"]) |
280 : | blume | 362 | else (); |
281 : | true | ||
282 : | blume | 349 | end |
283 : | else false | ||
284 : | blume | 351 | end handle Option => (RT.reset (); false) |
285 : | blume | 330 | (* to catch valOf failures in "rt" *) |
286 : | blume | 327 | in |
287 : | blume | 329 | case BuildInitDG.build ginfo_nocore initgspec of |
288 : | blume | 327 | SOME x => main_compile x |
289 : | blume | 349 | | NONE => false |
290 : | blume | 327 | end |
291 : | blume | 362 | |
292 : | fun reset () = | ||
293 : | (RecompPersstate.reset (); | ||
294 : | blume | 372 | RT.reset (); |
295 : | blume | 367 | Recomp.reset (); |
296 : | Parse.reset ()) | ||
297 : | blume | 377 | |
298 : | val make' = compile false | ||
299 : | fun make () = make' NONE | ||
300 : | fun deliver' arg = | ||
301 : | SafeIO.perform { openIt = fn () => (), | ||
302 : | closeIt = reset, | ||
303 : | work = fn () => compile true arg, | ||
304 : | cleanup = fn () => () } | ||
305 : | fun deliver () = deliver' NONE | ||
306 : | blume | 327 | end |
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |