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 537 - (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 : blume 537 structure BE = GenericVC.BareEnvironment
14 : blume 398 structure Pid = GenericVC.PersStamps
15 :     structure DE = GenericVC.DynamicEnv
16 :     structure PP = PrettyPrint
17 :     structure EM = GenericVC.ErrorMsg
18 :    
19 :     type pid = Pid.persstamp
20 :     type statenv = E.staticEnv
21 :     type symenv = E.symenv
22 :     type result = { stat: statenv, sym: symenv }
23 : blume 460 type ed = IInfo.info
24 : blume 398 in
25 :     signature COMPILE = sig
26 : blume 402
27 : blume 400 type bfc
28 :    
29 : blume 398 (* reset internal persistent state *)
30 :     val reset : unit -> unit
31 : blume 399
32 :     (* notify linkage module about recompilation *)
33 : blume 400 type notifier = GP.info -> SmlInfo.info -> unit
34 : blume 399
35 : blume 403 (* type of a function to store away the binfile contents *)
36 :     type bfcReceiver = SmlInfo.info * bfc -> unit
37 :    
38 : blume 398 val getII : SmlInfo.info -> IInfo.info
39 : blume 399
40 : blume 537 val evictStale : unit -> unit
41 : blume 402 val evictAll : unit -> unit
42 : blume 399
43 : blume 400 val newSbnodeTraversal : unit -> GP.info -> DG.sbnode -> ed option
44 :    
45 : blume 403 val newTraversal : notifier * bfcReceiver * GG.group ->
46 : blume 399 { group: GP.info -> result option,
47 :     exports: (GP.info -> result option) SymbolMap.map }
48 : blume 398 end
49 :    
50 : blume 448 functor CompileFn (structure MachDepVC : MACHDEP_VC
51 :     val compile_there : SrcPath.t -> bool) :>
52 : blume 400 COMPILE where type bfc = MachDepVC.Binfile.bfContent =
53 :     struct
54 : blume 398
55 : blume 400 type notifier = GP.info -> SmlInfo.info -> unit
56 : blume 399
57 : blume 398 structure BF = MachDepVC.Binfile
58 :    
59 :     type bfc = BF.bfContent
60 :    
61 : blume 403 type bfcReceiver = SmlInfo.info * bfc -> unit
62 :    
63 : blume 447 structure FilterMap = MapFn
64 : blume 398 (struct
65 :     type ord_key = pid * SymbolSet.set
66 :     fun compare ((u, f), (u', f')) =
67 :     case Pid.compare (u, u') of
68 :     EQUAL => SymbolSet.compare (f, f')
69 :     | unequal => unequal
70 :     end)
71 :    
72 : blume 403 type bfinfo =
73 :     { cmdata: PidSet.set,
74 :     statenv: unit -> statenv,
75 :     symenv: unit -> symenv,
76 :     statpid: pid,
77 :     sympid: pid }
78 :    
79 : blume 399 type env = { envs: unit -> result, pids: PidSet.set }
80 : blume 460 type envdelta = IInfo.info
81 : blume 398
82 : blume 460 type memo = { ii: IInfo.info, 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 460 fun memo2ed memo = memo2ii memo
102 : blume 398
103 : blume 460 fun bfc2memo (bfc, ts) = let
104 : blume 403 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 : blume 460 { ii = ii, ts = ts, cmdata = cmdata }
111 : blume 403 end
112 :    
113 : blume 398 fun pidset (p1, p2) = PidSet.add (PidSet.singleton p1, p2)
114 :    
115 :     fun nofilter (ed: envdelta) = let
116 : blume 460 val { statenv, symenv, statpid, sympid } = ed
117 : blume 398 in
118 : blume 461 { envs = fn () => { stat = #env (statenv ()), sym = symenv () },
119 : blume 398 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 460 fun filter (ii, s) = let
126 : blume 398 val { statenv, symenv, statpid, sympid } = ii
127 : blume 461 val { env = ste, ctxt } = statenv ()
128 : blume 398 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 : blume 460 (* We re-pickle the filtered ste relative to
140 :     * the original one. This should give a fairly
141 :     * minimal pickle. *)
142 : blume 461 val statpid' = GenericVC.Rehash.rehash
143 :     { context = ctxt,
144 :     env = GenericVC.CoerceEnv.es2bs ste',
145 :     orig_hash = statpid }
146 : blume 398 in
147 :     filtermap :=
148 :     FilterMap.insert (!filtermap, key, statpid');
149 :     statpid'
150 :     end
151 :     in
152 :     { envs = fn () => { stat = ste', sym = symenv () },
153 :     pids = pidset (statpid', sympid) }
154 :     end
155 :     end
156 :    
157 : blume 399 local
158 : blume 398 fun r2e { stat, sym } = E.mkenv { static = stat, symbolic = sym,
159 :     dynamic = DE.empty }
160 :     fun e2r e = { stat = E.staticPart e, sym = E.symbolicPart e }
161 :     in
162 : blume 399 (* This is a bit ugly because somehow we need to mix dummy
163 :     * dynamic envs into the equation just to be able to use
164 :     * concatEnv. But, alas', that's life... *)
165 :     fun rlayer (r, r') = e2r (E.concatEnv (r2e r, r2e r'))
166 :    
167 :     val emptyEnv =
168 :     { envs = fn () => e2r E.emptyEnv, pids = PidSet.empty }
169 : blume 398 end
170 :    
171 :     fun layer ({ envs = e, pids = p }, { envs = e', pids = p' }) =
172 :     { envs = fn () => rlayer (e (), e' ()),
173 :     pids = PidSet.union (p, p') }
174 :    
175 : blume 462 (* I would rather not use an exception here, but short of a better
176 :     * implementation of concurrency I see no choice.
177 :     * The problem is that at each node we sequentiallay wait for the
178 :     * children nodes. But the scheduler might (and probably will)
179 :     * let a child run that we are not currently waiting for, so an
180 :     * error there will not result in "wait" to immediately return
181 :     * as it should for clean error recovery.
182 :     * Using the exception avoids having to implement a
183 :     * "wait for any child -- whichever finishes first" kind of call. *)
184 :     exception Abort
185 : blume 399
186 : blume 462 fun layer'wait u (p, NONE) =
187 :     (ignore (Concur.waitU u p); NONE)
188 :     | layer'wait u (p, SOME e) =
189 :     (case Concur.waitU u p of
190 :     SOME e' => SOME (layer (e', e))
191 :     | NONE => NONE)
192 :    
193 : blume 454 fun mkTraversal (notify, storeBFC, getUrgency) = let
194 : blume 402 val localstate = ref SmlInfoMap.empty
195 : blume 398
196 : blume 537 fun sbnode gp (DG.SB_SNODE n) = snode gp n
197 :     (* The beauty of this scheme is that we don't have
198 :     * to do anything at all for SB_BNODEs: Everything
199 :     * is prepared ready to be used when the library
200 :     * is unpickled: *)
201 :     | sbnode gp (DG.SB_BNODE (_, ii)) = SOME ii
202 : blume 398
203 :     and fsbnode gp (f, n) =
204 :     case (sbnode gp n, f) of
205 :     (NONE, _) => NONE
206 :     | (SOME d, NONE) => SOME (nofilter d)
207 :     | (SOME d, SOME s) => SOME (filter (d, s))
208 :    
209 :     and snode gp (DG.SNODE n) = let
210 :     val { smlinfo = i, localimports = li, globalimports = gi } = n
211 :     val binname = SmlInfo.binname i
212 :    
213 : blume 462 fun fail () =
214 :     if #keep_going (#param gp) then NONE else raise Abort
215 :    
216 : blume 537 fun compile_here (stat, sym, pids, split) = let
217 : blume 398 fun save bfc = let
218 :     fun writer s =
219 :     (BF.write { stream = s, content = bfc,
220 :     nopickle = false };
221 :     Say.vsay ["[wrote ", binname, "]\n"])
222 : blume 459 fun cleanup _ =
223 : blume 398 OS.FileSys.remove binname handle _ => ()
224 :     in
225 : blume 400 notify gp i;
226 : blume 398 SafeIO.perform { openIt =
227 :     fn () => AutoDir.openBinOut binname,
228 :     closeIt = BinIO.closeOut,
229 :     work = writer,
230 :     cleanup = cleanup }
231 :     handle exn => let
232 :     fun ppb pps =
233 :     (PP.add_newline pps;
234 :     PP.add_string pps (General.exnMessage exn))
235 :     in
236 :     SmlInfo.error gp i EM.WARN
237 :     ("failed to write " ^ binname) ppb
238 :     end;
239 :     TStamp.setTime (binname, SmlInfo.lastseen i)
240 :     end (* save *)
241 :     in
242 :     case SmlInfo.parsetree gp i of
243 : blume 462 NONE => fail ()
244 : blume 398 | SOME (ast, source) => let
245 :     val corenv = #corenv (#param gp)
246 :     val cmData = PidSet.listItems pids
247 :     (* clear error flag (could still be set from
248 :     * earlier run) *)
249 :     val _ = #anyErrors source := false
250 : blume 537 val bfc = BF.create
251 :     { runtimePid = NONE,
252 :     splitting = split,
253 :     cmData = cmData,
254 :     ast = ast,
255 :     source = source,
256 :     senv = stat,
257 :     symenv = BE.layerSymbolic
258 :     (sym, BE.symbolicPart corenv),
259 :     corenv = BE.staticPart corenv }
260 : blume 460 val memo = bfc2memo (bfc, SmlInfo.lastseen i)
261 : blume 398 in
262 :     save bfc;
263 : blume 403 storeBFC (i, bfc);
264 : blume 402 SOME memo
265 : blume 462 end handle _ => fail () (* catch elaborator exn *)
266 : blume 448 end (* compile_here *)
267 : blume 398 fun notlocal () = let
268 : blume 454 val urgency = getUrgency i
269 : blume 402 (* Ok, it is not in the local state, so we first have
270 : blume 398 * to traverse all children before we can proceed... *)
271 :     fun loc li_n = Option.map nofilter (snode gp li_n)
272 :     fun glob gi_n = fsbnode gp gi_n
273 : blume 448 val gi_cl =
274 :     map (fn gi_n => Concur.fork (fn () => glob gi_n)) gi
275 :     val li_cl =
276 :     map (fn li_n => Concur.fork (fn () => loc li_n)) li
277 : blume 398 val e =
278 : blume 462 foldl (layer'wait urgency)
279 :     (foldl (layer'wait urgency)
280 : blume 537 (SOME emptyEnv)
281 : blume 462 gi_cl)
282 :     li_cl
283 : blume 398 in
284 :     case e of
285 :     NONE => NONE
286 :     | SOME { envs, pids } => let
287 :     (* We have successfully traversed all
288 :     * children. Now it is time to check the
289 :     * global map... *)
290 :     fun fromfile () = let
291 :     val { stat, sym } = envs ()
292 : blume 537 val { split, extra_compenv, ... } =
293 :     SmlInfo.attribs i
294 :     val stat =
295 :     case extra_compenv of
296 :     NONE => stat
297 :     | SOME s => E.layerStatic (stat, s)
298 : blume 398 fun load () = let
299 :     val ts = TStamp.fmodTime binname
300 :     fun openIt () = BinIO.openIn binname
301 :     fun reader s =
302 :     (BF.read { stream = s,
303 :     name = binname,
304 :     senv = stat },
305 :     ts)
306 : blume 403
307 : blume 398 in
308 :     SOME (SafeIO.perform
309 :     { openIt = openIt,
310 :     closeIt = BinIO.closeIn,
311 :     work = reader,
312 : blume 459 cleanup = fn _ => () })
313 : blume 398 handle _ => NONE
314 :     end (* load *)
315 : blume 448 fun tryload (what, otherwise) =
316 :     case load () of
317 :     NONE => otherwise ()
318 :     | SOME (bfc, ts) => let
319 : blume 460 val memo = bfc2memo (bfc, ts)
320 : blume 448 in
321 :     if isValidMemo (memo, pids, i) then
322 :     (Say.vsay ["[", binname,
323 :     " ", what, "]\n"];
324 :     storeBFC (i, bfc);
325 :     SOME memo)
326 :     else otherwise ()
327 :     end
328 :     fun compile_again () =
329 : blume 452 (Say.vsay ["[compiling ",
330 :     SmlInfo.descr i, "]\n"];
331 : blume 537 compile_here (stat, sym, pids, split))
332 : blume 448 fun compile () = let
333 :     val sp = SmlInfo.sourcepath i
334 :     in
335 :     if compile_there sp then
336 : blume 450 tryload ("received", compile_again)
337 : blume 448 else compile_again ()
338 :     end
339 : blume 398 in
340 : blume 448 (* If anything goes wrong loading the first
341 :     * time, we go and compile. Compiling
342 :     * may mean compiling externally, and if so,
343 :     * we must load the result of that.
344 :     * If the second load also goes wrong, we
345 :     * compile locally to gather error messages
346 :     * and make everything look "normal". *)
347 :     tryload ("loaded", compile)
348 : blume 398 end (* fromfile *)
349 : blume 402 fun notglobal () =
350 :     case fromfile () of
351 :     NONE => NONE
352 :     | SOME memo =>
353 :     (globalstate :=
354 :     SmlInfoMap.insert (!globalstate, i,
355 :     memo);
356 :     SOME memo)
357 : blume 398 in
358 : blume 402 case SmlInfoMap.find (!globalstate, i) of
359 :     NONE => notglobal ()
360 : blume 398 | SOME memo =>
361 :     if isValidMemo (memo, pids, i) then
362 : blume 402 SOME memo
363 :     else notglobal ()
364 : blume 398 end
365 :     end (* notlocal *)
366 :     in
367 : blume 462 (* Here we just wait (no "waitU") so we don't get
368 :     * priority over threads that may have to clean up after
369 :     * errors. *)
370 : blume 402 case SmlInfoMap.find (!localstate, i) of
371 : blume 448 SOME mopt_c => Option.map memo2ed (Concur.wait mopt_c)
372 : blume 398 | NONE => let
373 : blume 448 val mopt_c = Concur.fork
374 :     (fn () => notlocal () before
375 :     (* "Not local" means that we have not processed
376 :     * this file before. Therefore, we should now
377 :     * remove its parse tree... *)
378 :     SmlInfo.forgetParsetree i)
379 : blume 398 in
380 : blume 402 localstate :=
381 : blume 448 SmlInfoMap.insert (!localstate, i, mopt_c);
382 :     Option.map memo2ed (Concur.wait mopt_c)
383 : blume 398 end
384 :     end (* snode *)
385 :    
386 :     fun impexp gp (n, _) = fsbnode gp n
387 : blume 399 in
388 :     { sbnode = sbnode, impexp = impexp }
389 :     end
390 : blume 398
391 : blume 454 fun newTraversal (notify, storeBFC, g) = let
392 :     val GG.GROUP { exports, ... } = g
393 :     val um = Indegree.indegrees g
394 :     fun getUrgency i = getOpt (SmlInfoMap.find (um, i), 0)
395 :     val { impexp, ... } = mkTraversal (notify, storeBFC, getUrgency)
396 : blume 399 fun group gp = let
397 : blume 448 val eo_cl =
398 :     map (fn x => Concur.fork (fn () => impexp gp x))
399 :     (SymbolMap.listItems exports)
400 : blume 462 val eo = foldl (layer'wait 0) (SOME emptyEnv) eo_cl
401 : blume 399 in
402 :     case eo of
403 : blume 461 NONE => (Servers.reset false; NONE)
404 : blume 399 | SOME e => SOME (#envs e ())
405 : blume 462 end handle Abort => (Servers.reset false; NONE)
406 : blume 399 fun mkExport ie gp =
407 : blume 462 case impexp gp ie handle Abort => NONE of
408 : blume 461 NONE => (Servers.reset false; NONE)
409 : blume 399 | SOME e => SOME (#envs e ())
410 : blume 398 in
411 : blume 399 { group = group,
412 :     exports = SymbolMap.map mkExport exports }
413 : blume 398 end
414 :    
415 : blume 400 fun newSbnodeTraversal () = let
416 : blume 537 val { sbnode, ... } =
417 :     mkTraversal (fn _ => fn _ => (), fn _ => (), fn _ => 0)
418 : blume 461 fun sbn_trav gp g = let
419 : blume 462 val r = sbnode gp g handle Abort => NONE
420 : blume 461 in
421 :     if isSome r then () else Servers.reset false;
422 :     r
423 :     end
424 : blume 398 in
425 : blume 461 sbn_trav
426 : blume 398 end
427 :    
428 : blume 537 fun evictStale () =
429 :     globalstate :=
430 :     SmlInfoMap.filteri (SmlInfo.isKnown o #1) (!globalstate)
431 : blume 400
432 : blume 403 fun evictAll () = globalstate := SmlInfoMap.empty
433 : blume 402
434 : blume 403 fun getII i = memo2ii (valOf (SmlInfoMap.find (!globalstate, i)))
435 : blume 398 end
436 :     end

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