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

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