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