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 461 - (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 : blume 460 type ed = IInfo.info
23 : blume 398 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 460 type envdelta = IInfo.info
80 : blume 398
81 : blume 460 type memo = { ii: IInfo.info, ts: TStamp.t, cmdata: PidSet.set }
82 : blume 398
83 :     (* persistent state! *)
84 :     val filtermap = ref (FilterMap.empty: pid FilterMap.map)
85 :    
86 :     (* more persistent state! *)
87 : blume 402 val globalstate = ref (SmlInfoMap.empty: memo SmlInfoMap.map)
88 : blume 398
89 :     fun reset () =
90 :     (filtermap := FilterMap.empty;
91 : blume 402 globalstate := SmlInfoMap.empty)
92 : blume 398
93 :     fun isValidMemo (memo: memo, provided, smlinfo) =
94 :     not (TStamp.needsUpdate { source = SmlInfo.lastseen smlinfo,
95 :     target = #ts memo })
96 : blume 403 andalso PidSet.equal (provided, #cmdata memo)
97 : blume 398
98 : blume 403 fun memo2ii (memo: memo) = #ii memo
99 : blume 398
100 : blume 460 fun memo2ed memo = memo2ii memo
101 : blume 398
102 : blume 460 fun bfc2memo (bfc, ts) = let
103 : blume 403 val ii = { statenv = fn () => BF.senvOf bfc,
104 :     symenv = fn () => BF.symenvOf bfc,
105 :     statpid = BF.staticPidOf bfc,
106 :     sympid = BF.lambdaPidOf bfc }
107 :     val cmdata = PidSet.addList (PidSet.empty, BF.cmDataOf bfc)
108 :     in
109 : blume 460 { ii = ii, ts = ts, cmdata = cmdata }
110 : blume 403 end
111 :    
112 : blume 398 fun pidset (p1, p2) = PidSet.add (PidSet.singleton p1, p2)
113 :    
114 :     fun nofilter (ed: envdelta) = let
115 : blume 460 val { statenv, symenv, statpid, sympid } = ed
116 : blume 398 in
117 : blume 461 { envs = fn () => { stat = #env (statenv ()), sym = symenv () },
118 : blume 398 pids = pidset (statpid, sympid) }
119 :     end
120 :    
121 :     fun exportsNothingBut set se =
122 :     List.all (fn sy => SymbolSet.member (set, sy)) (E.catalogEnv se)
123 :    
124 : blume 460 fun filter (ii, s) = let
125 : blume 398 val { statenv, symenv, statpid, sympid } = ii
126 : blume 461 val { env = ste, ctxt } = statenv ()
127 : blume 398 in
128 :     if exportsNothingBut s ste then
129 :     { envs = fn () => { stat = ste, sym = symenv () },
130 :     pids = pidset (statpid, sympid) }
131 :     else let
132 :     val ste' = E.filterStaticEnv (ste, SymbolSet.listItems s)
133 :     val key = (statpid, s)
134 :     val statpid' =
135 :     case FilterMap.find (!filtermap, key) of
136 :     SOME statpid' => statpid'
137 :     | NONE => let
138 : blume 460 (* We re-pickle the filtered ste relative to
139 :     * the original one. This should give a fairly
140 :     * minimal pickle. *)
141 : blume 461 val statpid' = GenericVC.Rehash.rehash
142 :     { context = ctxt,
143 :     env = GenericVC.CoerceEnv.es2bs ste',
144 :     orig_hash = statpid }
145 : blume 398 in
146 :     filtermap :=
147 :     FilterMap.insert (!filtermap, key, statpid');
148 :     statpid'
149 :     end
150 :     in
151 :     { envs = fn () => { stat = ste', sym = symenv () },
152 :     pids = pidset (statpid', sympid) }
153 :     end
154 :     end
155 :    
156 : blume 399 local
157 : blume 398 fun r2e { stat, sym } = E.mkenv { static = stat, symbolic = sym,
158 :     dynamic = DE.empty }
159 :     fun e2r e = { stat = E.staticPart e, sym = E.symbolicPart e }
160 :     in
161 : blume 399 (* This is a bit ugly because somehow we need to mix dummy
162 :     * dynamic envs into the equation just to be able to use
163 :     * concatEnv. But, alas', that's life... *)
164 :     fun rlayer (r, r') = e2r (E.concatEnv (r2e r, r2e r'))
165 :    
166 :     val emptyEnv =
167 :     { envs = fn () => e2r E.emptyEnv, pids = PidSet.empty }
168 : blume 398 end
169 :    
170 :     fun layer ({ envs = e, pids = p }, { envs = e', pids = p' }) =
171 :     { envs = fn () => rlayer (e (), e' ()),
172 :     pids = PidSet.union (p, p') }
173 :    
174 : blume 399 fun layerwork k w v0 l = let
175 :     fun lw v0 [] = v0
176 :     | lw NONE (h :: t) =
177 :     if k then (ignore (w h); lw NONE t)
178 :     else NONE
179 :     | lw (SOME v) (h :: t) = let
180 :     fun lay (NONE, v) = NONE
181 :     | lay (SOME v', v) = SOME (layer (v', v))
182 :     in
183 :     lw (lay (w h, v)) t
184 :     end
185 :     in
186 :     lw v0 l
187 :     end
188 :    
189 : blume 454 fun mkTraversal (notify, storeBFC, getUrgency) = let
190 : blume 402 val localstate = ref SmlInfoMap.empty
191 : blume 398
192 :     fun pervenv (gp: GP.info) = let
193 :     val e = #pervasive (#param gp)
194 :     val ste = E.staticPart e
195 :     val sye = E.symbolicPart e
196 :     in
197 :     { envs = fn () => { stat = ste, sym = sye },
198 :     pids = PidSet.empty }
199 :     end
200 :    
201 :     fun sbnode gp n =
202 :     case n of
203 :     DG.SB_BNODE (_, ii) =>
204 :     (* The beauty of this scheme is that we don't have
205 :     * to do anything at all for SB_BNODEs: Everything
206 :     * is prepared ready to be used when the library
207 : blume 460 * is unpickled. *)
208 :     SOME ii
209 : blume 432 | DG.SB_SNODE n =>
210 :     (case snode gp n of
211 :     NONE => NONE
212 : blume 460 | SOME ii => SOME ii)
213 : blume 398
214 :     and fsbnode gp (f, n) =
215 :     case (sbnode gp n, f) of
216 :     (NONE, _) => NONE
217 :     | (SOME d, NONE) => SOME (nofilter d)
218 :     | (SOME d, SOME s) => SOME (filter (d, s))
219 :    
220 :     and snode gp (DG.SNODE n) = let
221 :     val { smlinfo = i, localimports = li, globalimports = gi } = n
222 :     val binname = SmlInfo.binname i
223 :    
224 : blume 448 fun compile_here (stat, sym, pids) = let
225 : blume 398 fun save bfc = let
226 :     fun writer s =
227 :     (BF.write { stream = s, content = bfc,
228 :     nopickle = false };
229 :     Say.vsay ["[wrote ", binname, "]\n"])
230 : blume 459 fun cleanup _ =
231 : blume 398 OS.FileSys.remove binname handle _ => ()
232 :     in
233 : blume 400 notify gp i;
234 : blume 398 SafeIO.perform { openIt =
235 :     fn () => AutoDir.openBinOut binname,
236 :     closeIt = BinIO.closeOut,
237 :     work = writer,
238 :     cleanup = cleanup }
239 :     handle exn => let
240 :     fun ppb pps =
241 :     (PP.add_newline pps;
242 :     PP.add_string pps (General.exnMessage exn))
243 :     in
244 :     SmlInfo.error gp i EM.WARN
245 :     ("failed to write " ^ binname) ppb
246 :     end;
247 :     TStamp.setTime (binname, SmlInfo.lastseen i)
248 :     end (* save *)
249 :     in
250 :     case SmlInfo.parsetree gp i of
251 :     NONE => NONE
252 :     | SOME (ast, source) => let
253 :     val corenv = #corenv (#param gp)
254 :     val cmData = PidSet.listItems pids
255 :     (* clear error flag (could still be set from
256 :     * earlier run) *)
257 :     val _ = #anyErrors source := false
258 :     val bfc = BF.create { runtimePid = NONE,
259 :     splitting = SmlInfo.split i,
260 :     cmData = cmData,
261 :     ast = ast,
262 :     source = source,
263 :     senv = stat,
264 :     symenv = sym,
265 :     corenv = corenv }
266 : blume 460 val memo = bfc2memo (bfc, SmlInfo.lastseen i)
267 : blume 398 in
268 :     save bfc;
269 : blume 403 storeBFC (i, bfc);
270 : blume 402 SOME memo
271 : blume 460 end handle _ => NONE (* catch elaborator exn *)
272 : blume 448 end (* compile_here *)
273 : blume 398 fun notlocal () = let
274 : blume 454 val urgency = getUrgency i
275 : blume 402 (* Ok, it is not in the local state, so we first have
276 : blume 398 * to traverse all children before we can proceed... *)
277 :     val k = #keep_going (#param gp)
278 :     fun loc li_n = Option.map nofilter (snode gp li_n)
279 :     fun glob gi_n = fsbnode gp gi_n
280 : blume 448 val gi_cl =
281 :     map (fn gi_n => Concur.fork (fn () => glob gi_n)) gi
282 :     val li_cl =
283 :     map (fn li_n => Concur.fork (fn () => loc li_n)) li
284 : blume 398 val e =
285 : blume 454 layerwork k (Concur.wait' urgency)
286 :     (layerwork k (Concur.wait' urgency)
287 : blume 448 (SOME (pervenv gp)) gi_cl)
288 :     li_cl
289 : blume 398 in
290 :     case e of
291 :     NONE => NONE
292 :     | SOME { envs, pids } => let
293 :     (* We have successfully traversed all
294 :     * children. Now it is time to check the
295 :     * global map... *)
296 :     fun fromfile () = let
297 :     val { stat, sym } = envs ()
298 :     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 :     compile_here (stat, sym, pids))
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 402 case SmlInfoMap.find (!localstate, i) of
368 : blume 448 SOME mopt_c => Option.map memo2ed (Concur.wait mopt_c)
369 : blume 398 | NONE => let
370 : blume 448 val mopt_c = Concur.fork
371 :     (fn () => notlocal () before
372 :     (* "Not local" means that we have not processed
373 :     * this file before. Therefore, we should now
374 :     * remove its parse tree... *)
375 :     SmlInfo.forgetParsetree i)
376 : blume 398 in
377 : blume 402 localstate :=
378 : blume 448 SmlInfoMap.insert (!localstate, i, mopt_c);
379 :     Option.map memo2ed (Concur.wait mopt_c)
380 : blume 398 end
381 :     end (* snode *)
382 :    
383 :     fun impexp gp (n, _) = fsbnode gp n
384 : blume 399 in
385 :     { sbnode = sbnode, impexp = impexp }
386 :     end
387 : blume 398
388 : blume 454 fun newTraversal (notify, storeBFC, g) = let
389 :     val GG.GROUP { exports, ... } = g
390 :     val um = Indegree.indegrees g
391 :     fun getUrgency i = getOpt (SmlInfoMap.find (um, i), 0)
392 :     val { impexp, ... } = mkTraversal (notify, storeBFC, getUrgency)
393 : blume 399 fun group gp = let
394 :     val k = #keep_going (#param gp)
395 :     fun loop ([], success) = success
396 :     | loop (h :: t, success) =
397 :     if isSome (impexp gp h) then loop (t, success)
398 :     else if k then loop (t, false) else false
399 : blume 448 val eo_cl =
400 :     map (fn x => Concur.fork (fn () => impexp gp x))
401 :     (SymbolMap.listItems exports)
402 :     val eo = layerwork k Concur.wait (SOME emptyEnv) eo_cl
403 : blume 399 in
404 :     case eo of
405 : blume 461 NONE => (Servers.reset false; NONE)
406 : blume 399 | SOME e => SOME (#envs e ())
407 :     end
408 :     fun mkExport ie gp =
409 :     case impexp gp ie of
410 : blume 461 NONE => (Servers.reset false; NONE)
411 : blume 399 | SOME e => SOME (#envs e ())
412 : blume 398 in
413 : blume 399 { group = group,
414 :     exports = SymbolMap.map mkExport exports }
415 : blume 398 end
416 :    
417 : blume 400 fun newSbnodeTraversal () = let
418 : blume 403 val { sbnode, ... } = mkTraversal (fn _ => fn _ => (),
419 : blume 454 fn _ => (),
420 :     fn _ => 0)
421 : blume 461 fun sbn_trav gp g = let
422 :     val r = sbnode gp g
423 :     in
424 :     if isSome r then () else Servers.reset false;
425 :     r
426 :     end
427 : blume 398 in
428 : blume 461 sbn_trav
429 : blume 398 end
430 :    
431 : blume 403 fun evict i =
432 :     (globalstate := #1 (SmlInfoMap.remove (!globalstate, i)))
433 :     handle LibBase.NotFound => ()
434 : blume 400
435 : blume 403 fun evictAll () = globalstate := SmlInfoMap.empty
436 : blume 402
437 : blume 403 fun getII i = memo2ii (valOf (SmlInfoMap.find (!globalstate, i)))
438 : blume 398 end
439 :     end

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