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

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