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/compile/compile.sml
ViewVC logotype

Annotation of /sml/trunk/src/cm/compile/compile.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 450 - (view) (download)

1 : blume 402 (*
2 :     * Compilation traversals.
3 :     *
4 :     * (C) 1999 Lucent Technologies, Bell Laboratories
5 :     *
6 :     * Author: Matthias Blume (blume@kurims.kyoto-u.ac.jp)
7 :     *)
8 : blume 398 local
9 :     structure GP = GeneralParams
10 :     structure DG = DependencyGraph
11 :     structure GG = GroupGraph
12 :     structure E = GenericVC.Environment
13 :     structure Pid = GenericVC.PersStamps
14 :     structure DE = GenericVC.DynamicEnv
15 :     structure PP = PrettyPrint
16 :     structure EM = GenericVC.ErrorMsg
17 :    
18 :     type pid = Pid.persstamp
19 :     type statenv = E.staticEnv
20 :     type symenv = E.symenv
21 :     type result = { stat: statenv, sym: symenv }
22 :     type ed = { ii: IInfo.info, ctxt: statenv }
23 :     in
24 :     signature COMPILE = sig
25 : blume 402
26 : blume 400 type bfc
27 :    
28 : blume 398 (* reset internal persistent state *)
29 :     val reset : unit -> unit
30 : blume 399
31 :     (* notify linkage module about recompilation *)
32 : blume 400 type notifier = GP.info -> SmlInfo.info -> unit
33 : blume 399
34 : blume 403 (* type of a function to store away the binfile contents *)
35 :     type bfcReceiver = SmlInfo.info * bfc -> unit
36 :    
37 : blume 398 val getII : SmlInfo.info -> IInfo.info
38 : blume 399
39 : blume 400 val evict : SmlInfo.info -> unit
40 : blume 402 val evictAll : unit -> unit
41 : blume 399
42 : blume 400 val newSbnodeTraversal : unit -> GP.info -> DG.sbnode -> ed option
43 :    
44 : blume 403 val newTraversal : notifier * bfcReceiver * GG.group ->
45 : blume 399 { group: GP.info -> result option,
46 :     exports: (GP.info -> result option) SymbolMap.map }
47 : blume 398 end
48 :    
49 : blume 448 functor CompileFn (structure MachDepVC : MACHDEP_VC
50 :     val compile_there : SrcPath.t -> bool) :>
51 : blume 400 COMPILE where type bfc = MachDepVC.Binfile.bfContent =
52 :     struct
53 : blume 398
54 : blume 400 type notifier = GP.info -> SmlInfo.info -> unit
55 : blume 399
56 : blume 398 structure BF = MachDepVC.Binfile
57 :    
58 :     type bfc = BF.bfContent
59 :    
60 : blume 403 type bfcReceiver = SmlInfo.info * bfc -> unit
61 :    
62 : blume 447 structure FilterMap = MapFn
63 : blume 398 (struct
64 :     type ord_key = pid * SymbolSet.set
65 :     fun compare ((u, f), (u', f')) =
66 :     case Pid.compare (u, u') of
67 :     EQUAL => SymbolSet.compare (f, f')
68 :     | unequal => unequal
69 :     end)
70 :    
71 : blume 403 type bfinfo =
72 :     { cmdata: PidSet.set,
73 :     statenv: unit -> statenv,
74 :     symenv: unit -> symenv,
75 :     statpid: pid,
76 :     sympid: pid }
77 :    
78 : blume 399 type env = { envs: unit -> result, pids: PidSet.set }
79 : blume 403 type envdelta = { ii: IInfo.info, ctxt: unit -> statenv }
80 : blume 398
81 : blume 403 type memo =
82 :     { ii: IInfo.info, ctxt: statenv, ts: TStamp.t, cmdata: PidSet.set }
83 : blume 398
84 :     (* persistent state! *)
85 :     val filtermap = ref (FilterMap.empty: pid FilterMap.map)
86 :    
87 :     (* more persistent state! *)
88 : blume 402 val globalstate = ref (SmlInfoMap.empty: memo SmlInfoMap.map)
89 : blume 398
90 :     fun reset () =
91 :     (filtermap := FilterMap.empty;
92 : blume 402 globalstate := SmlInfoMap.empty)
93 : blume 398
94 :     fun isValidMemo (memo: memo, provided, smlinfo) =
95 :     not (TStamp.needsUpdate { source = SmlInfo.lastseen smlinfo,
96 :     target = #ts memo })
97 : blume 403 andalso PidSet.equal (provided, #cmdata memo)
98 : blume 398
99 : blume 403 fun memo2ii (memo: memo) = #ii memo
100 : blume 398
101 : blume 403 fun memo2ed memo = { ii = memo2ii memo, ctxt = fn () => #ctxt memo }
102 : blume 398
103 : blume 403 fun bfc2memo (bfc, ctxt, ts) = let
104 :     val ii = { statenv = fn () => BF.senvOf bfc,
105 :     symenv = fn () => BF.symenvOf bfc,
106 :     statpid = BF.staticPidOf bfc,
107 :     sympid = BF.lambdaPidOf bfc }
108 :     val cmdata = PidSet.addList (PidSet.empty, BF.cmDataOf bfc)
109 :     in
110 :     { ii = ii, ctxt = ctxt, ts = ts, cmdata = cmdata }
111 :     end
112 :    
113 : blume 398 fun pidset (p1, p2) = PidSet.add (PidSet.singleton p1, p2)
114 :    
115 :     fun nofilter (ed: envdelta) = let
116 : blume 403 val { ii = { statenv, symenv, statpid, sympid }, ctxt } = ed
117 : blume 398 in
118 :     { envs = fn () => { stat = statenv (), sym = symenv () },
119 :     pids = pidset (statpid, sympid) }
120 :     end
121 :    
122 :     fun exportsNothingBut set se =
123 :     List.all (fn sy => SymbolSet.member (set, sy)) (E.catalogEnv se)
124 :    
125 : blume 403 fun filter ({ ii, ctxt }: envdelta, s) = let
126 : blume 398 val { statenv, symenv, statpid, sympid } = ii
127 :     val ste = statenv ()
128 :     in
129 :     if exportsNothingBut s ste then
130 :     { envs = fn () => { stat = ste, sym = symenv () },
131 :     pids = pidset (statpid, sympid) }
132 :     else let
133 :     val ste' = E.filterStaticEnv (ste, SymbolSet.listItems s)
134 :     val key = (statpid, s)
135 :     val statpid' =
136 :     case FilterMap.find (!filtermap, key) of
137 :     SOME statpid' => statpid'
138 :     | NONE => let
139 :     val statpid' =
140 :     GenericVC.MakePid.makePid (ctxt (), ste')
141 :     in
142 :     filtermap :=
143 :     FilterMap.insert (!filtermap, key, statpid');
144 :     statpid'
145 :     end
146 :     in
147 :     { envs = fn () => { stat = ste', sym = symenv () },
148 :     pids = pidset (statpid', sympid) }
149 :     end
150 :     end
151 :    
152 : blume 399 local
153 : blume 398 fun r2e { stat, sym } = E.mkenv { static = stat, symbolic = sym,
154 :     dynamic = DE.empty }
155 :     fun e2r e = { stat = E.staticPart e, sym = E.symbolicPart e }
156 :     in
157 : blume 399 (* This is a bit ugly because somehow we need to mix dummy
158 :     * dynamic envs into the equation just to be able to use
159 :     * concatEnv. But, alas', that's life... *)
160 :     fun rlayer (r, r') = e2r (E.concatEnv (r2e r, r2e r'))
161 :    
162 :     val emptyEnv =
163 :     { envs = fn () => e2r E.emptyEnv, pids = PidSet.empty }
164 : blume 398 end
165 :    
166 :     fun layer ({ envs = e, pids = p }, { envs = e', pids = p' }) =
167 :     { envs = fn () => rlayer (e (), e' ()),
168 :     pids = PidSet.union (p, p') }
169 :    
170 : blume 399 fun layerwork k w v0 l = let
171 :     fun lw v0 [] = v0
172 :     | lw NONE (h :: t) =
173 :     if k then (ignore (w h); lw NONE t)
174 :     else NONE
175 :     | lw (SOME v) (h :: t) = let
176 :     fun lay (NONE, v) = NONE
177 :     | lay (SOME v', v) = SOME (layer (v', v))
178 :     in
179 :     lw (lay (w h, v)) t
180 :     end
181 :     in
182 :     lw v0 l
183 :     end
184 :    
185 : blume 403 fun mkTraversal (notify, storeBFC) = let
186 : blume 402 val localstate = ref SmlInfoMap.empty
187 : blume 398
188 :     fun pervenv (gp: GP.info) = let
189 :     val e = #pervasive (#param gp)
190 :     val ste = E.staticPart e
191 :     val sye = E.symbolicPart e
192 :     in
193 :     { envs = fn () => { stat = ste, sym = sye },
194 :     pids = PidSet.empty }
195 :     end
196 :    
197 :     fun sbnode gp n =
198 :     case n of
199 :     DG.SB_BNODE (_, ii) =>
200 :     (* The beauty of this scheme is that we don't have
201 :     * to do anything at all for SB_BNODEs: Everything
202 :     * is prepared ready to be used when the library
203 :     * is unpickled.
204 :     *
205 :     * Making ctxt equal to ste is basically a hack
206 :     * because we want to avoid having to keep the
207 :     * real context around. As a result there is a
208 :     * slight loss of "smart recompilation":
209 :     * eliminating a definition is not the same as
210 :     * stripping it away using a filter. This is a
211 :     * minor issue anyway, and in the present case
212 :     * it only happens when a stable library is
213 :     * replaced by a different one. *)
214 : blume 403 SOME { ii = ii, ctxt = #statenv ii }
215 : blume 432 | DG.SB_SNODE n =>
216 :     (case snode gp n of
217 :     NONE => NONE
218 :     | SOME { ii, ... } =>
219 :     (* Now, unfortunately, because of the
220 :     * hack above (ctxt = ste) we must
221 :     * do the same thing here or we end up
222 :     * not being consistent. *)
223 :     SOME { ii = ii, ctxt = #statenv ii })
224 : blume 398
225 :     and fsbnode gp (f, n) =
226 :     case (sbnode gp n, f) of
227 :     (NONE, _) => NONE
228 :     | (SOME d, NONE) => SOME (nofilter d)
229 :     | (SOME d, SOME s) => SOME (filter (d, s))
230 :    
231 :     and snode gp (DG.SNODE n) = let
232 :     val { smlinfo = i, localimports = li, globalimports = gi } = n
233 :     val binname = SmlInfo.binname i
234 :    
235 : blume 448 fun compile_here (stat, sym, pids) = let
236 : blume 398 fun save bfc = let
237 :     fun writer s =
238 :     (BF.write { stream = s, content = bfc,
239 :     nopickle = false };
240 :     Say.vsay ["[wrote ", binname, "]\n"])
241 :     fun cleanup () =
242 :     OS.FileSys.remove binname handle _ => ()
243 :     in
244 : blume 400 notify gp i;
245 : blume 398 SafeIO.perform { openIt =
246 :     fn () => AutoDir.openBinOut binname,
247 :     closeIt = BinIO.closeOut,
248 :     work = writer,
249 :     cleanup = cleanup }
250 :     handle exn => let
251 :     fun ppb pps =
252 :     (PP.add_newline pps;
253 :     PP.add_string pps (General.exnMessage exn))
254 :     in
255 :     SmlInfo.error gp i EM.WARN
256 :     ("failed to write " ^ binname) ppb
257 :     end;
258 :     TStamp.setTime (binname, SmlInfo.lastseen i)
259 :     end (* save *)
260 :     in
261 :     case SmlInfo.parsetree gp i of
262 :     NONE => NONE
263 :     | SOME (ast, source) => let
264 :     val _ =
265 :     Say.vsay ["[compiling ", SmlInfo.descr i, "]\n"]
266 :     val corenv = #corenv (#param gp)
267 :     val cmData = PidSet.listItems pids
268 :     (* clear error flag (could still be set from
269 :     * earlier run) *)
270 :     val _ = #anyErrors source := false
271 :     val bfc = BF.create { runtimePid = NONE,
272 :     splitting = SmlInfo.split i,
273 :     cmData = cmData,
274 :     ast = ast,
275 :     source = source,
276 :     senv = stat,
277 :     symenv = sym,
278 :     corenv = corenv }
279 : blume 403 val memo = bfc2memo (bfc, stat, SmlInfo.lastseen i)
280 : blume 398 in
281 :     save bfc;
282 : blume 403 storeBFC (i, bfc);
283 : blume 402 SOME memo
284 : blume 398 end
285 : blume 448 end (* compile_here *)
286 : blume 398 fun notlocal () = let
287 : blume 402 (* Ok, it is not in the local state, so we first have
288 : blume 398 * to traverse all children before we can proceed... *)
289 :     val k = #keep_going (#param gp)
290 :     fun loc li_n = Option.map nofilter (snode gp li_n)
291 :     fun glob gi_n = fsbnode gp gi_n
292 : blume 448 val gi_cl =
293 :     map (fn gi_n => Concur.fork (fn () => glob gi_n)) gi
294 :     val li_cl =
295 :     map (fn li_n => Concur.fork (fn () => loc li_n)) li
296 : blume 398 val e =
297 : blume 448 layerwork k Concur.wait
298 :     (layerwork k Concur.wait
299 :     (SOME (pervenv gp)) gi_cl)
300 :     li_cl
301 : blume 398 in
302 :     case e of
303 :     NONE => NONE
304 :     | SOME { envs, pids } => let
305 :     (* We have successfully traversed all
306 :     * children. Now it is time to check the
307 :     * global map... *)
308 :     fun fromfile () = let
309 :     val { stat, sym } = envs ()
310 :     fun load () = let
311 :     val ts = TStamp.fmodTime binname
312 :     fun openIt () = BinIO.openIn binname
313 :     fun reader s =
314 :     (BF.read { stream = s,
315 :     name = binname,
316 :     senv = stat },
317 :     ts)
318 : blume 403
319 : blume 398 in
320 :     SOME (SafeIO.perform
321 :     { openIt = openIt,
322 :     closeIt = BinIO.closeIn,
323 :     work = reader,
324 :     cleanup = fn () => () })
325 :     handle _ => NONE
326 :     end (* load *)
327 : blume 448 fun tryload (what, otherwise) =
328 :     case load () of
329 :     NONE => otherwise ()
330 :     | SOME (bfc, ts) => let
331 :     val memo = bfc2memo (bfc, stat, ts)
332 :     in
333 :     if isValidMemo (memo, pids, i) then
334 :     (Say.vsay ["[", binname,
335 :     " ", what, "]\n"];
336 :     storeBFC (i, bfc);
337 :     SOME memo)
338 :     else otherwise ()
339 :     end
340 :     fun compile_again () =
341 :     compile_here (stat, sym, pids)
342 :     fun compile () = let
343 :     val sp = SmlInfo.sourcepath i
344 :     in
345 :     if compile_there sp then
346 : blume 450 tryload ("received", compile_again)
347 : blume 448 else compile_again ()
348 :     end
349 : blume 398 in
350 : blume 448 (* If anything goes wrong loading the first
351 :     * time, we go and compile. Compiling
352 :     * may mean compiling externally, and if so,
353 :     * we must load the result of that.
354 :     * If the second load also goes wrong, we
355 :     * compile locally to gather error messages
356 :     * and make everything look "normal". *)
357 :     tryload ("loaded", compile)
358 : blume 398 end (* fromfile *)
359 : blume 402 fun notglobal () =
360 :     case fromfile () of
361 :     NONE => NONE
362 :     | SOME memo =>
363 :     (globalstate :=
364 :     SmlInfoMap.insert (!globalstate, i,
365 :     memo);
366 :     SOME memo)
367 : blume 398 in
368 : blume 402 case SmlInfoMap.find (!globalstate, i) of
369 :     NONE => notglobal ()
370 : blume 398 | SOME memo =>
371 :     if isValidMemo (memo, pids, i) then
372 : blume 402 SOME memo
373 :     else notglobal ()
374 : blume 398 end
375 :     end (* notlocal *)
376 :     in
377 : blume 402 case SmlInfoMap.find (!localstate, i) of
378 : blume 448 SOME mopt_c => Option.map memo2ed (Concur.wait mopt_c)
379 : blume 398 | NONE => let
380 : blume 448 val mopt_c = Concur.fork
381 :     (fn () => notlocal () before
382 :     (* "Not local" means that we have not processed
383 :     * this file before. Therefore, we should now
384 :     * remove its parse tree... *)
385 :     SmlInfo.forgetParsetree i)
386 : blume 398 in
387 : blume 402 localstate :=
388 : blume 448 SmlInfoMap.insert (!localstate, i, mopt_c);
389 :     Option.map memo2ed (Concur.wait mopt_c)
390 : blume 398 end
391 :     end (* snode *)
392 :    
393 :     fun impexp gp (n, _) = fsbnode gp n
394 : blume 399 in
395 :     { sbnode = sbnode, impexp = impexp }
396 :     end
397 : blume 398
398 : blume 403 fun newTraversal (notify, storeBFC, GG.GROUP { exports, ... }) = let
399 :     val { impexp, ... } = mkTraversal (notify, storeBFC)
400 : blume 399 fun group gp = let
401 :     val k = #keep_going (#param gp)
402 :     fun loop ([], success) = success
403 :     | loop (h :: t, success) =
404 :     if isSome (impexp gp h) then loop (t, success)
405 :     else if k then loop (t, false) else false
406 : blume 448 val eo_cl =
407 :     map (fn x => Concur.fork (fn () => impexp gp x))
408 :     (SymbolMap.listItems exports)
409 :     val eo = layerwork k Concur.wait (SOME emptyEnv) eo_cl
410 : blume 399 in
411 :     case eo of
412 :     NONE => NONE
413 :     | SOME e => SOME (#envs e ())
414 :     end
415 :     fun mkExport ie gp =
416 :     case impexp gp ie of
417 :     NONE => NONE
418 :     | SOME e => SOME (#envs e ())
419 : blume 398 in
420 : blume 399 { group = group,
421 :     exports = SymbolMap.map mkExport exports }
422 : blume 398 end
423 :    
424 : blume 400 fun newSbnodeTraversal () = let
425 : blume 403 val { sbnode, ... } = mkTraversal (fn _ => fn _ => (),
426 :     fn _ => ())
427 :     fun envdelta2ed { ii, ctxt } = { ii = ii, ctxt = ctxt () }
428 : blume 398 in
429 : blume 399 fn gp => fn n => Option.map envdelta2ed (sbnode gp n)
430 : blume 398 end
431 :    
432 : blume 403 fun evict i =
433 :     (globalstate := #1 (SmlInfoMap.remove (!globalstate, i)))
434 :     handle LibBase.NotFound => ()
435 : blume 400
436 : blume 403 fun evictAll () = globalstate := SmlInfoMap.empty
437 : blume 402
438 : blume 403 fun getII i = memo2ii (valOf (SmlInfoMap.find (!globalstate, i)))
439 : blume 398 end
440 :     end

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