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 432 - (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 :     type ed = { ii: IInfo.info, ctxt: statenv }
23 :     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 400 functor CompileFn (structure MachDepVC : MACHDEP_VC) :>
50 :     COMPILE where type bfc = MachDepVC.Binfile.bfContent =
51 :     struct
52 : blume 398
53 : blume 400 type notifier = GP.info -> SmlInfo.info -> unit
54 : blume 399
55 : blume 398 structure BF = MachDepVC.Binfile
56 :    
57 :     type bfc = BF.bfContent
58 :    
59 : blume 403 type bfcReceiver = SmlInfo.info * bfc -> unit
60 :    
61 : blume 398 structure FilterMap = BinaryMapFn
62 :     (struct
63 :     type ord_key = pid * SymbolSet.set
64 :     fun compare ((u, f), (u', f')) =
65 :     case Pid.compare (u, u') of
66 :     EQUAL => SymbolSet.compare (f, f')
67 :     | unequal => unequal
68 :     end)
69 :    
70 : blume 403 type bfinfo =
71 :     { cmdata: PidSet.set,
72 :     statenv: unit -> statenv,
73 :     symenv: unit -> symenv,
74 :     statpid: pid,
75 :     sympid: pid }
76 :    
77 : blume 399 type env = { envs: unit -> result, pids: PidSet.set }
78 : blume 403 type envdelta = { ii: IInfo.info, ctxt: unit -> statenv }
79 : blume 398
80 : blume 403 type memo =
81 :     { ii: IInfo.info, ctxt: statenv, 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 403 fun memo2ed memo = { ii = memo2ii memo, ctxt = fn () => #ctxt memo }
101 : blume 398
102 : blume 403 fun bfc2memo (bfc, ctxt, ts) = let
103 :     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 :     { ii = ii, ctxt = ctxt, ts = ts, cmdata = cmdata }
110 :     end
111 :    
112 : blume 398 fun pidset (p1, p2) = PidSet.add (PidSet.singleton p1, p2)
113 :    
114 :     fun nofilter (ed: envdelta) = let
115 : blume 403 val { ii = { statenv, symenv, statpid, sympid }, ctxt } = 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 403 fun filter ({ ii, ctxt }: envdelta, 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 :     val statpid' =
139 :     GenericVC.MakePid.makePid (ctxt (), ste')
140 :     in
141 :     filtermap :=
142 :     FilterMap.insert (!filtermap, key, statpid');
143 :     statpid'
144 :     end
145 :     in
146 :     { envs = fn () => { stat = ste', sym = symenv () },
147 :     pids = pidset (statpid', sympid) }
148 :     end
149 :     end
150 :    
151 : blume 399 local
152 : blume 398 fun r2e { stat, sym } = E.mkenv { static = stat, symbolic = sym,
153 :     dynamic = DE.empty }
154 :     fun e2r e = { stat = E.staticPart e, sym = E.symbolicPart e }
155 :     in
156 : blume 399 (* This is a bit ugly because somehow we need to mix dummy
157 :     * dynamic envs into the equation just to be able to use
158 :     * concatEnv. But, alas', that's life... *)
159 :     fun rlayer (r, r') = e2r (E.concatEnv (r2e r, r2e r'))
160 :    
161 :     val emptyEnv =
162 :     { envs = fn () => e2r E.emptyEnv, pids = PidSet.empty }
163 : blume 398 end
164 :    
165 :     fun layer ({ envs = e, pids = p }, { envs = e', pids = p' }) =
166 :     { envs = fn () => rlayer (e (), e' ()),
167 :     pids = PidSet.union (p, p') }
168 :    
169 : blume 399 fun layerwork k w v0 l = let
170 :     fun lw v0 [] = v0
171 :     | lw NONE (h :: t) =
172 :     if k then (ignore (w h); lw NONE t)
173 :     else NONE
174 :     | lw (SOME v) (h :: t) = let
175 :     fun lay (NONE, v) = NONE
176 :     | lay (SOME v', v) = SOME (layer (v', v))
177 :     in
178 :     lw (lay (w h, v)) t
179 :     end
180 :     in
181 :     lw v0 l
182 :     end
183 :    
184 : blume 403 fun mkTraversal (notify, storeBFC) = let
185 : blume 402 val localstate = ref SmlInfoMap.empty
186 : blume 398
187 :     fun pervenv (gp: GP.info) = let
188 :     val e = #pervasive (#param gp)
189 :     val ste = E.staticPart e
190 :     val sye = E.symbolicPart e
191 :     in
192 :     { envs = fn () => { stat = ste, sym = sye },
193 :     pids = PidSet.empty }
194 :     end
195 :    
196 :     fun sbnode gp n =
197 :     case n of
198 :     DG.SB_BNODE (_, ii) =>
199 :     (* The beauty of this scheme is that we don't have
200 :     * to do anything at all for SB_BNODEs: Everything
201 :     * is prepared ready to be used when the library
202 :     * is unpickled.
203 :     *
204 :     * Making ctxt equal to ste is basically a hack
205 :     * because we want to avoid having to keep the
206 :     * real context around. As a result there is a
207 :     * slight loss of "smart recompilation":
208 :     * eliminating a definition is not the same as
209 :     * stripping it away using a filter. This is a
210 :     * minor issue anyway, and in the present case
211 :     * it only happens when a stable library is
212 :     * replaced by a different one. *)
213 : blume 403 SOME { ii = ii, ctxt = #statenv ii }
214 : blume 432 | DG.SB_SNODE n =>
215 :     (case snode gp n of
216 :     NONE => NONE
217 :     | SOME { ii, ... } =>
218 :     (* Now, unfortunately, because of the
219 :     * hack above (ctxt = ste) we must
220 :     * do the same thing here or we end up
221 :     * not being consistent. *)
222 :     SOME { ii = ii, ctxt = #statenv ii })
223 : blume 398
224 :     and fsbnode gp (f, n) =
225 :     case (sbnode gp n, f) of
226 :     (NONE, _) => NONE
227 :     | (SOME d, NONE) => SOME (nofilter d)
228 :     | (SOME d, SOME s) => SOME (filter (d, s))
229 :    
230 :     and snode gp (DG.SNODE n) = let
231 :     val { smlinfo = i, localimports = li, globalimports = gi } = n
232 :     val binname = SmlInfo.binname i
233 :    
234 :     fun compile (stat, sym, pids) = let
235 :     fun save bfc = let
236 :     fun writer s =
237 :     (BF.write { stream = s, content = bfc,
238 :     nopickle = false };
239 :     Say.vsay ["[wrote ", binname, "]\n"])
240 :     fun cleanup () =
241 :     OS.FileSys.remove binname handle _ => ()
242 :     in
243 : blume 400 notify gp i;
244 : blume 398 SafeIO.perform { openIt =
245 :     fn () => AutoDir.openBinOut binname,
246 :     closeIt = BinIO.closeOut,
247 :     work = writer,
248 :     cleanup = cleanup }
249 :     handle exn => let
250 :     fun ppb pps =
251 :     (PP.add_newline pps;
252 :     PP.add_string pps (General.exnMessage exn))
253 :     in
254 :     SmlInfo.error gp i EM.WARN
255 :     ("failed to write " ^ binname) ppb
256 :     end;
257 :     TStamp.setTime (binname, SmlInfo.lastseen i)
258 :     end (* save *)
259 :     in
260 :     case SmlInfo.parsetree gp i of
261 :     NONE => NONE
262 :     | SOME (ast, source) => let
263 :     val _ =
264 :     Say.vsay ["[compiling ", SmlInfo.descr i, "]\n"]
265 :     val corenv = #corenv (#param gp)
266 :     val cmData = PidSet.listItems pids
267 :     (* clear error flag (could still be set from
268 :     * earlier run) *)
269 :     val _ = #anyErrors source := false
270 :     val bfc = BF.create { runtimePid = NONE,
271 :     splitting = SmlInfo.split i,
272 :     cmData = cmData,
273 :     ast = ast,
274 :     source = source,
275 :     senv = stat,
276 :     symenv = sym,
277 :     corenv = corenv }
278 : blume 403 val memo = bfc2memo (bfc, stat, SmlInfo.lastseen i)
279 : blume 398 in
280 :     save bfc;
281 : blume 403 storeBFC (i, bfc);
282 : blume 402 SOME memo
283 : blume 398 end
284 :     end (* compile *)
285 :     fun notlocal () = let
286 : blume 402 (* Ok, it is not in the local state, so we first have
287 : blume 398 * to traverse all children before we can proceed... *)
288 :     val k = #keep_going (#param gp)
289 :     fun loc li_n = Option.map nofilter (snode gp li_n)
290 :     fun glob gi_n = fsbnode gp gi_n
291 :     val e =
292 :     layerwork k loc
293 :     (layerwork k glob (SOME (pervenv gp)) gi)
294 :     li
295 :     in
296 :     case e of
297 :     NONE => NONE
298 :     | SOME { envs, pids } => let
299 :     (* We have successfully traversed all
300 :     * children. Now it is time to check the
301 :     * global map... *)
302 :     fun fromfile () = let
303 :     val { stat, sym } = envs ()
304 :     fun load () = let
305 :     val ts = TStamp.fmodTime binname
306 :     fun openIt () = BinIO.openIn binname
307 :     fun reader s =
308 :     (BF.read { stream = s,
309 :     name = binname,
310 :     senv = stat },
311 :     ts)
312 : blume 403
313 : blume 398 in
314 :     SOME (SafeIO.perform
315 :     { openIt = openIt,
316 :     closeIt = BinIO.closeIn,
317 :     work = reader,
318 :     cleanup = fn () => () })
319 :     handle _ => NONE
320 :     end (* load *)
321 :     in
322 :     case load () of
323 :     NONE => compile (stat, sym, pids)
324 :     | SOME (bfc, ts) => let
325 : blume 403 val memo = bfc2memo (bfc, stat, ts)
326 : blume 398 in
327 :     if isValidMemo (memo, pids, i) then
328 : blume 403 (Say.vsay ["[", binname,
329 :     " loaded]\n"];
330 :     storeBFC (i, bfc);
331 :     SOME memo)
332 : blume 398 else compile (stat, sym, pids)
333 :     end
334 :     end (* fromfile *)
335 : blume 402 fun notglobal () =
336 :     case fromfile () of
337 :     NONE => NONE
338 :     | SOME memo =>
339 :     (globalstate :=
340 :     SmlInfoMap.insert (!globalstate, i,
341 :     memo);
342 :     SOME memo)
343 : blume 398 in
344 : blume 402 case SmlInfoMap.find (!globalstate, i) of
345 :     NONE => notglobal ()
346 : blume 398 | SOME memo =>
347 :     if isValidMemo (memo, pids, i) then
348 : blume 402 SOME memo
349 :     else notglobal ()
350 : blume 398 end
351 :     end (* notlocal *)
352 :     in
353 : blume 402 case SmlInfoMap.find (!localstate, i) of
354 :     SOME mopt => Option.map memo2ed mopt
355 : blume 398 | NONE => let
356 : blume 402 val mopt = notlocal ()
357 : blume 398 in
358 : blume 403 (* "Not local" means that we have not processed
359 :     * this file before. Therefore, we should now
360 :     * remove its parse tree... *)
361 :     SmlInfo.forgetParsetree i;
362 : blume 402 localstate :=
363 :     SmlInfoMap.insert (!localstate, i, mopt);
364 :     Option.map memo2ed mopt
365 : blume 398 end
366 :     end (* snode *)
367 :    
368 :     fun impexp gp (n, _) = fsbnode gp n
369 : blume 399 in
370 :     { sbnode = sbnode, impexp = impexp }
371 :     end
372 : blume 398
373 : blume 403 fun newTraversal (notify, storeBFC, GG.GROUP { exports, ... }) = let
374 :     val { impexp, ... } = mkTraversal (notify, storeBFC)
375 : blume 399 fun group gp = let
376 :     val k = #keep_going (#param gp)
377 :     fun loop ([], success) = success
378 :     | loop (h :: t, success) =
379 :     if isSome (impexp gp h) then loop (t, success)
380 :     else if k then loop (t, false) else false
381 :     val eo =
382 :     layerwork k (impexp gp) (SOME emptyEnv)
383 :     (SymbolMap.listItems exports)
384 :     in
385 :     case eo of
386 :     NONE => NONE
387 :     | SOME e => SOME (#envs e ())
388 :     end
389 :     fun mkExport ie gp =
390 :     case impexp gp ie of
391 :     NONE => NONE
392 :     | SOME e => SOME (#envs e ())
393 : blume 398 in
394 : blume 399 { group = group,
395 :     exports = SymbolMap.map mkExport exports }
396 : blume 398 end
397 :    
398 : blume 400 fun newSbnodeTraversal () = let
399 : blume 403 val { sbnode, ... } = mkTraversal (fn _ => fn _ => (),
400 :     fn _ => ())
401 :     fun envdelta2ed { ii, ctxt } = { ii = ii, ctxt = ctxt () }
402 : blume 398 in
403 : blume 399 fn gp => fn n => Option.map envdelta2ed (sbnode gp n)
404 : blume 398 end
405 :    
406 : blume 403 fun evict i =
407 :     (globalstate := #1 (SmlInfoMap.remove (!globalstate, i)))
408 :     handle LibBase.NotFound => ()
409 : blume 400
410 : blume 403 fun evictAll () = globalstate := SmlInfoMap.empty
411 : blume 402
412 : blume 403 fun getII i = memo2ii (valOf (SmlInfoMap.find (!globalstate, i)))
413 : blume 398 end
414 :     end

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