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