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

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