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

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