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

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