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 460 - (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 :     { envs = fn () => { stat = statenv (), sym = symenv () },
118 :     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 :     val ste = statenv ()
127 :     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 398 val statpid' =
142 : blume 460 GenericVC.MakePid.makePid (ste, ste')
143 : blume 398 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 :     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 399 fun layerwork k w v0 l = let
173 :     fun lw v0 [] = v0
174 :     | lw NONE (h :: t) =
175 :     if k then (ignore (w h); lw NONE t)
176 :     else NONE
177 :     | lw (SOME v) (h :: t) = let
178 :     fun lay (NONE, v) = NONE
179 :     | lay (SOME v', v) = SOME (layer (v', v))
180 :     in
181 :     lw (lay (w h, v)) t
182 :     end
183 :     in
184 :     lw v0 l
185 :     end
186 :    
187 : blume 454 fun mkTraversal (notify, storeBFC, getUrgency) = let
188 : blume 402 val localstate = ref SmlInfoMap.empty
189 : blume 398
190 :     fun pervenv (gp: GP.info) = let
191 :     val e = #pervasive (#param gp)
192 :     val ste = E.staticPart e
193 :     val sye = E.symbolicPart e
194 :     in
195 :     { envs = fn () => { stat = ste, sym = sye },
196 :     pids = PidSet.empty }
197 :     end
198 :    
199 :     fun sbnode gp n =
200 :     case n of
201 :     DG.SB_BNODE (_, ii) =>
202 :     (* The beauty of this scheme is that we don't have
203 :     * to do anything at all for SB_BNODEs: Everything
204 :     * is prepared ready to be used when the library
205 : blume 460 * is unpickled. *)
206 :     SOME ii
207 : blume 432 | DG.SB_SNODE n =>
208 :     (case snode gp n of
209 :     NONE => NONE
210 : blume 460 | SOME ii => SOME ii)
211 : blume 398
212 :     and fsbnode gp (f, n) =
213 :     case (sbnode gp n, f) of
214 :     (NONE, _) => NONE
215 :     | (SOME d, NONE) => SOME (nofilter d)
216 :     | (SOME d, SOME s) => SOME (filter (d, s))
217 :    
218 :     and snode gp (DG.SNODE n) = let
219 :     val { smlinfo = i, localimports = li, globalimports = gi } = n
220 :     val binname = SmlInfo.binname i
221 :    
222 : blume 448 fun compile_here (stat, sym, pids) = let
223 : blume 398 fun save bfc = let
224 :     fun writer s =
225 :     (BF.write { stream = s, content = bfc,
226 :     nopickle = false };
227 :     Say.vsay ["[wrote ", binname, "]\n"])
228 : blume 459 fun cleanup _ =
229 : blume 398 OS.FileSys.remove binname handle _ => ()
230 :     in
231 : blume 400 notify gp i;
232 : blume 398 SafeIO.perform { openIt =
233 :     fn () => AutoDir.openBinOut binname,
234 :     closeIt = BinIO.closeOut,
235 :     work = writer,
236 :     cleanup = cleanup }
237 :     handle exn => let
238 :     fun ppb pps =
239 :     (PP.add_newline pps;
240 :     PP.add_string pps (General.exnMessage exn))
241 :     in
242 :     SmlInfo.error gp i EM.WARN
243 :     ("failed to write " ^ binname) ppb
244 :     end;
245 :     TStamp.setTime (binname, SmlInfo.lastseen i)
246 :     end (* save *)
247 :     in
248 :     case SmlInfo.parsetree gp i of
249 :     NONE => NONE
250 :     | SOME (ast, source) => let
251 :     val corenv = #corenv (#param gp)
252 :     val cmData = PidSet.listItems pids
253 :     (* clear error flag (could still be set from
254 :     * earlier run) *)
255 :     val _ = #anyErrors source := false
256 :     val bfc = BF.create { runtimePid = NONE,
257 :     splitting = SmlInfo.split i,
258 :     cmData = cmData,
259 :     ast = ast,
260 :     source = source,
261 :     senv = stat,
262 :     symenv = sym,
263 :     corenv = corenv }
264 : blume 460 val memo = bfc2memo (bfc, SmlInfo.lastseen i)
265 : blume 398 in
266 :     save bfc;
267 : blume 403 storeBFC (i, bfc);
268 : blume 402 SOME memo
269 : blume 460 end handle _ => NONE (* catch elaborator exn *)
270 : blume 448 end (* compile_here *)
271 : blume 398 fun notlocal () = let
272 : blume 454 val urgency = getUrgency i
273 : blume 402 (* Ok, it is not in the local state, so we first have
274 : blume 398 * to traverse all children before we can proceed... *)
275 :     val k = #keep_going (#param gp)
276 :     fun loc li_n = Option.map nofilter (snode gp li_n)
277 :     fun glob gi_n = fsbnode gp gi_n
278 : blume 448 val gi_cl =
279 :     map (fn gi_n => Concur.fork (fn () => glob gi_n)) gi
280 :     val li_cl =
281 :     map (fn li_n => Concur.fork (fn () => loc li_n)) li
282 : blume 398 val e =
283 : blume 454 layerwork k (Concur.wait' urgency)
284 :     (layerwork k (Concur.wait' urgency)
285 : blume 448 (SOME (pervenv gp)) gi_cl)
286 :     li_cl
287 : blume 398 in
288 :     case e of
289 :     NONE => NONE
290 :     | SOME { envs, pids } => let
291 :     (* We have successfully traversed all
292 :     * children. Now it is time to check the
293 :     * global map... *)
294 :     fun fromfile () = let
295 :     val { stat, sym } = envs ()
296 :     fun load () = let
297 :     val ts = TStamp.fmodTime binname
298 :     fun openIt () = BinIO.openIn binname
299 :     fun reader s =
300 :     (BF.read { stream = s,
301 :     name = binname,
302 :     senv = stat },
303 :     ts)
304 : blume 403
305 : blume 398 in
306 :     SOME (SafeIO.perform
307 :     { openIt = openIt,
308 :     closeIt = BinIO.closeIn,
309 :     work = reader,
310 : blume 459 cleanup = fn _ => () })
311 : blume 398 handle _ => NONE
312 :     end (* load *)
313 : blume 448 fun tryload (what, otherwise) =
314 :     case load () of
315 :     NONE => otherwise ()
316 :     | SOME (bfc, ts) => let
317 : blume 460 val memo = bfc2memo (bfc, ts)
318 : blume 448 in
319 :     if isValidMemo (memo, pids, i) then
320 :     (Say.vsay ["[", binname,
321 :     " ", what, "]\n"];
322 :     storeBFC (i, bfc);
323 :     SOME memo)
324 :     else otherwise ()
325 :     end
326 :     fun compile_again () =
327 : blume 452 (Say.vsay ["[compiling ",
328 :     SmlInfo.descr i, "]\n"];
329 :     compile_here (stat, sym, pids))
330 : blume 448 fun compile () = let
331 :     val sp = SmlInfo.sourcepath i
332 :     in
333 :     if compile_there sp then
334 : blume 450 tryload ("received", compile_again)
335 : blume 448 else compile_again ()
336 :     end
337 : blume 398 in
338 : blume 448 (* If anything goes wrong loading the first
339 :     * time, we go and compile. Compiling
340 :     * may mean compiling externally, and if so,
341 :     * we must load the result of that.
342 :     * If the second load also goes wrong, we
343 :     * compile locally to gather error messages
344 :     * and make everything look "normal". *)
345 :     tryload ("loaded", compile)
346 : blume 398 end (* fromfile *)
347 : blume 402 fun notglobal () =
348 :     case fromfile () of
349 :     NONE => NONE
350 :     | SOME memo =>
351 :     (globalstate :=
352 :     SmlInfoMap.insert (!globalstate, i,
353 :     memo);
354 :     SOME memo)
355 : blume 398 in
356 : blume 402 case SmlInfoMap.find (!globalstate, i) of
357 :     NONE => notglobal ()
358 : blume 398 | SOME memo =>
359 :     if isValidMemo (memo, pids, i) then
360 : blume 402 SOME memo
361 :     else notglobal ()
362 : blume 398 end
363 :     end (* notlocal *)
364 :     in
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 454 fun newTraversal (notify, storeBFC, g) = let
387 :     val GG.GROUP { exports, ... } = g
388 :     val um = Indegree.indegrees g
389 :     fun getUrgency i = getOpt (SmlInfoMap.find (um, i), 0)
390 :     val { impexp, ... } = mkTraversal (notify, storeBFC, getUrgency)
391 : blume 399 fun group gp = let
392 :     val k = #keep_going (#param gp)
393 :     fun loop ([], success) = success
394 :     | loop (h :: t, success) =
395 :     if isSome (impexp gp h) then loop (t, success)
396 :     else if k then loop (t, false) else false
397 : blume 448 val eo_cl =
398 :     map (fn x => Concur.fork (fn () => impexp gp x))
399 :     (SymbolMap.listItems exports)
400 :     val eo = layerwork k Concur.wait (SOME emptyEnv) eo_cl
401 : blume 399 in
402 :     case eo of
403 :     NONE => NONE
404 :     | SOME e => SOME (#envs e ())
405 :     end
406 :     fun mkExport ie gp =
407 :     case impexp gp ie of
408 :     NONE => NONE
409 :     | 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 403 val { sbnode, ... } = mkTraversal (fn _ => fn _ => (),
417 : blume 454 fn _ => (),
418 :     fn _ => 0)
419 : blume 398 in
420 : blume 460 sbnode
421 : blume 398 end
422 :    
423 : blume 403 fun evict i =
424 :     (globalstate := #1 (SmlInfoMap.remove (!globalstate, i)))
425 :     handle LibBase.NotFound => ()
426 : blume 400
427 : blume 403 fun evictAll () = globalstate := SmlInfoMap.empty
428 : blume 402
429 : blume 403 fun getII i = memo2ii (valOf (SmlInfoMap.find (!globalstate, i)))
430 : blume 398 end
431 :     end

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