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 448 - (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 448 structure Compile = CompileFn (structure MachDepVC = MachDepVC
32 :     fun compile_there _ = false)
33 : blume 360
34 : blume 403 structure BFC = BfcFn (structure MachDepVC = MachDepVC)
35 :    
36 : blume 327 (* instantiate Stabilize... *)
37 : blume 329 structure Stabilize =
38 : blume 432 StabilizeFn (fun destroy_state _ i = Compile.evict i
39 : blume 403 structure MachDepVC = MachDepVC
40 : blume 399 fun recomp gp g = let
41 : blume 403 val { store, get } = BFC.new ()
42 : blume 399 val { group, ... } =
43 : blume 403 Compile.newTraversal (fn _ => fn _ => (),
44 :     store, g)
45 : blume 399 in
46 : blume 403 case group gp of
47 :     NONE => NONE
48 :     | SOME _ => SOME get
49 :     end
50 :     val getII = Compile.getII)
51 : blume 398
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 433 val keep_going = #get StdConfig.keep_going ()
125 : blume 329
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 : blume 433 symval = SSV.symval,
152 : blume 349 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 400 val sbnode = Compile.newSbnodeTraversal ()
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 398 val core = valOf (sbnode ginfo_nocore core)
185 :     val corenv = CoerceEnv.es2bs (#statenv (#ii core) ())
186 :     val core_sym = #symenv (#ii 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 398 fun rt n = valOf (sbnode ginfo_justcore n)
197 : blume 327 val rts = rt rts
198 :     val pervasive = rt pervasive
199 :    
200 :     fun sn2pspec (name, n) = let
201 : blume 398 val { ii = { statenv, symenv, statpid, sympid }, ctxt } = rt n
202 : blume 327 val env =
203 : blume 398 E.mkenv { static = statenv (),
204 :     symbolic = symenv (),
205 :     dynamic = emptydyn }
206 :     val pidInfo =
207 :     { statpid = statpid, sympid = sympid, ctxt = ctxt }
208 : blume 327 in
209 :     { name = name, env = env, pidInfo = pidInfo }
210 :     end
211 :    
212 :     val pspecs = map sn2pspec primitives
213 :    
214 : blume 329 val _ = ovldR := savedOvld
215 :    
216 : blume 372 (* The following is a hack but must be done for both the symbolic
217 : blume 356 * and later the dynamic part of the core environment:
218 :     * we must include these parts in the pervasive env. *)
219 : blume 398 val perv_sym = E.layerSymbolic (#symenv (#ii pervasive) (),
220 :     core_sym)
221 : blume 356
222 : blume 349 val param =
223 :     mkParam { primconf = Primitive.configuration pspecs,
224 : blume 398 pervasive = E.mkenv { static =
225 :     #statenv (#ii pervasive) (),
226 : blume 356 symbolic = perv_sym,
227 : blume 327 dynamic = emptydyn },
228 :     pervcorepids =
229 :     PidSet.addList (PidSet.empty,
230 : blume 398 [#statpid (#ii pervasive),
231 :     #sympid (#ii pervasive),
232 :     #statpid (#ii 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 399 | SOME (g, gp) => let
240 : blume 403 fun store _ = ()
241 : blume 399 val { group = recomp, ... } =
242 : blume 403 Compile.newTraversal (fn _ => fn _ => (), store, g)
243 : blume 399 in
244 :     if isSome (recomp gp) then let
245 : blume 398 val rtspid = PS.toHex (#statpid (#ii rts))
246 : blume 349 fun writeList s = let
247 : blume 364 fun add ((p, flag), l) = let
248 :     val n = listName (p, true)
249 :     in
250 :     if flag then n :: l else l
251 :     end
252 :     fun transcribe (p, NONE) = listName (p, true)
253 :     | transcribe (p, SOME (off, desc)) =
254 :     concat [listName (p, false),
255 :     "@", Int.toString off, ":", desc]
256 :     val bootstrings =
257 :     foldr add (map transcribe (MkBootList.group g))
258 :     binpaths
259 :     fun show str =
260 :     (TextIO.output (s, str);
261 :     TextIO.output (s, "\n"))
262 : blume 349 in
263 : blume 364 app show bootstrings
264 : blume 349 end
265 :     in
266 : blume 362 if deliver then
267 :     (SafeIO.perform { openIt = fn () =>
268 : blume 354 AutoDir.openTextOut pidfile,
269 : blume 349 closeIt = TextIO.closeOut,
270 :     work = fn s =>
271 :     TextIO.output (s, rtspid ^ "\n"),
272 :     cleanup = fn () =>
273 : blume 354 OS.FileSys.remove pidfile
274 :     handle _ => () };
275 : blume 349 SafeIO.perform { openIt = fn () =>
276 : blume 354 AutoDir.openTextOut listfile,
277 : blume 349 closeIt = TextIO.closeOut,
278 :     work = writeList,
279 :     cleanup = fn () =>
280 : blume 354 OS.FileSys.remove listfile
281 :     handle _ => () };
282 : blume 368 copyTextFile (SrcPath.osstring initgspec, cmifile);
283 : blume 366 Say.say ["Runtime System PID is: ", rtspid, "\n"])
284 : blume 362 else ();
285 :     true
286 : blume 349 end
287 :     else false
288 : blume 399 end
289 : blume 398 end handle Option => (Compile.reset (); false)
290 : blume 330 (* to catch valOf failures in "rt" *)
291 : blume 327 in
292 : blume 329 case BuildInitDG.build ginfo_nocore initgspec of
293 : blume 327 SOME x => main_compile x
294 : blume 349 | NONE => false
295 : blume 327 end
296 : blume 362
297 :     fun reset () =
298 : blume 398 (Compile.reset ();
299 : blume 367 Parse.reset ())
300 : blume 377
301 :     val make' = compile false
302 :     fun make () = make' NONE
303 :     fun deliver' arg =
304 :     SafeIO.perform { openIt = fn () => (),
305 :     closeIt = reset,
306 :     work = fn () => compile true arg,
307 :     cleanup = fn () => () }
308 :     fun deliver () = deliver' NONE
309 : blume 434 val symval = SSV.symval
310 : blume 327 end

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