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 398 - (view) (download)

1 : blume 398 local
2 :     structure GP = GeneralParams
3 :     structure DG = DependencyGraph
4 :     structure GG = GroupGraph
5 :     structure E = GenericVC.Environment
6 :     structure Pid = GenericVC.PersStamps
7 :     structure DE = GenericVC.DynamicEnv
8 :     structure PP = PrettyPrint
9 :     structure EM = GenericVC.ErrorMsg
10 :    
11 :     type pid = Pid.persstamp
12 :     type statenv = E.staticEnv
13 :     type symenv = E.symenv
14 :     type result = { stat: statenv, sym: symenv }
15 :     type ed = { ii: IInfo.info, ctxt: statenv }
16 :     type env = { envs: unit -> result, pids: PidSet.set }
17 :     in
18 :     signature COMPILE = sig
19 :     (* reset internal persistent state *)
20 :     val reset : unit -> unit
21 :     val sizeBFC : SmlInfo.info -> int
22 :     val writeBFC : BinIO.outstream -> SmlInfo.info -> unit
23 :     val getII : SmlInfo.info -> IInfo.info
24 :     val newTraversal : unit ->
25 :     { sbnode: GP.info -> DG.sbnode -> ed option,
26 :     impexp: GP.info -> DG.impexp -> env option }
27 :     val recomp: GP.info -> GG.group -> bool
28 :     end
29 :    
30 :     functor CompileFn (structure MachDepVC : MACHDEP_VC) :> COMPILE = struct
31 :    
32 :     structure BF = MachDepVC.Binfile
33 :    
34 :     type bfc = BF.bfContent
35 :    
36 :     structure FilterMap = BinaryMapFn
37 :     (struct
38 :     type ord_key = pid * SymbolSet.set
39 :     fun compare ((u, f), (u', f')) =
40 :     case Pid.compare (u, u') of
41 :     EQUAL => SymbolSet.compare (f, f')
42 :     | unequal => unequal
43 :     end)
44 :    
45 :     type envdelta =
46 :     { ii: IInfo.info, ctxt: unit -> statenv, bfc: bfc option }
47 :    
48 :     type memo = { bfc: bfc, ctxt: statenv, ts: TStamp.t }
49 :    
50 :     (* persistent state! *)
51 :     val filtermap = ref (FilterMap.empty: pid FilterMap.map)
52 :    
53 :     (* more persistent state! *)
54 :     val globalmap = ref (SmlInfoMap.empty: memo SmlInfoMap.map)
55 :    
56 :     fun reset () =
57 :     (filtermap := FilterMap.empty;
58 :     globalmap := SmlInfoMap.empty)
59 :    
60 :     fun isValidMemo (memo: memo, provided, smlinfo) =
61 :     not (TStamp.needsUpdate { source = SmlInfo.lastseen smlinfo,
62 :     target = #ts memo })
63 :     andalso let
64 :     val demanded =
65 :     PidSet.addList (PidSet.empty, BF.cmDataOf (#bfc memo))
66 :     in
67 :     PidSet.equal (provided, demanded)
68 :     end
69 :    
70 :     fun memo2ii (memo: memo) =
71 :     { statenv = fn () => BF.senvOf (#bfc memo),
72 :     symenv = fn () => BF.symenvOf (#bfc memo),
73 :     statpid = BF.staticPidOf (#bfc memo),
74 :     sympid = BF.lambdaPidOf (#bfc memo) }
75 :    
76 :     fun memo2ed memo =
77 :     { ii = memo2ii memo,
78 :     ctxt = fn () => #ctxt memo,
79 :     bfc = SOME (#bfc memo) }
80 :    
81 :     fun pidset (p1, p2) = PidSet.add (PidSet.singleton p1, p2)
82 :    
83 :     fun nofilter (ed: envdelta) = let
84 :     val { ii = { statenv, symenv, statpid, sympid }, ctxt, bfc } = ed
85 :     in
86 :     { envs = fn () => { stat = statenv (), sym = symenv () },
87 :     pids = pidset (statpid, sympid) }
88 :     end
89 :    
90 :     fun exportsNothingBut set se =
91 :     List.all (fn sy => SymbolSet.member (set, sy)) (E.catalogEnv se)
92 :    
93 :     fun filter ({ ii, ctxt, bfc }: envdelta, s) = let
94 :     val { statenv, symenv, statpid, sympid } = ii
95 :     val ste = statenv ()
96 :     in
97 :     if exportsNothingBut s ste then
98 :     { envs = fn () => { stat = ste, sym = symenv () },
99 :     pids = pidset (statpid, sympid) }
100 :     else let
101 :     val ste' = E.filterStaticEnv (ste, SymbolSet.listItems s)
102 :     val key = (statpid, s)
103 :     val statpid' =
104 :     case FilterMap.find (!filtermap, key) of
105 :     SOME statpid' => statpid'
106 :     | NONE => let
107 :     val statpid' =
108 :     GenericVC.MakePid.makePid (ctxt (), ste')
109 :     in
110 :     filtermap :=
111 :     FilterMap.insert (!filtermap, key, statpid');
112 :     statpid'
113 :     end
114 :     in
115 :     { envs = fn () => { stat = ste', sym = symenv () },
116 :     pids = pidset (statpid', sympid) }
117 :     end
118 :     end
119 :    
120 :     (* This is a bit ugly because somehow we need to mix dummy
121 :     * dynamic envs into the equation just to be able to use
122 :     * concatEnv. But, alas', that's life... *)
123 :     fun rlayer (r, r') = let
124 :     fun r2e { stat, sym } = E.mkenv { static = stat, symbolic = sym,
125 :     dynamic = DE.empty }
126 :     fun e2r e = { stat = E.staticPart e, sym = E.symbolicPart e }
127 :     in
128 :     e2r (E.concatEnv (r2e r, r2e r'))
129 :     end
130 :    
131 :     fun layer ({ envs = e, pids = p }, { envs = e', pids = p' }) =
132 :     { envs = fn () => rlayer (e (), e' ()),
133 :     pids = PidSet.union (p, p') }
134 :    
135 :     fun newTraversal () = let
136 :     val localmap = ref SmlInfoMap.empty
137 :    
138 :     fun pervenv (gp: GP.info) = let
139 :     val e = #pervasive (#param gp)
140 :     val ste = E.staticPart e
141 :     val sye = E.symbolicPart e
142 :     in
143 :     { envs = fn () => { stat = ste, sym = sye },
144 :     pids = PidSet.empty }
145 :     end
146 :    
147 :     fun layerwork k w v0 l = let
148 :     fun lw v0 [] = v0
149 :     | lw NONE (h :: t) =
150 :     if k then (ignore (w h); lw NONE t)
151 :     else NONE
152 :     | lw (SOME v) (h :: t) = let
153 :     fun lay (NONE, v) = NONE
154 :     | lay (SOME v', v) = SOME (layer (v', v))
155 :     in
156 :     lw (lay (w h, v)) t
157 :     end
158 :     in
159 :     lw v0 l
160 :     end
161 :    
162 :     fun sbnode gp n =
163 :     case n of
164 :     DG.SB_BNODE (_, ii) =>
165 :     (* The beauty of this scheme is that we don't have
166 :     * to do anything at all for SB_BNODEs: Everything
167 :     * is prepared ready to be used when the library
168 :     * is unpickled.
169 :     *
170 :     * Making ctxt equal to ste is basically a hack
171 :     * because we want to avoid having to keep the
172 :     * real context around. As a result there is a
173 :     * slight loss of "smart recompilation":
174 :     * eliminating a definition is not the same as
175 :     * stripping it away using a filter. This is a
176 :     * minor issue anyway, and in the present case
177 :     * it only happens when a stable library is
178 :     * replaced by a different one. *)
179 :     SOME { ii = ii, ctxt = #statenv ii, bfc = NONE }
180 :     | DG.SB_SNODE n => snode gp n
181 :    
182 :     and fsbnode gp (f, n) =
183 :     case (sbnode gp n, f) of
184 :     (NONE, _) => NONE
185 :     | (SOME d, NONE) => SOME (nofilter d)
186 :     | (SOME d, SOME s) => SOME (filter (d, s))
187 :    
188 :     and snode gp (DG.SNODE n) = let
189 :     val { smlinfo = i, localimports = li, globalimports = gi } = n
190 :     val binname = SmlInfo.binname i
191 :    
192 :     fun compile (stat, sym, pids) = let
193 :     fun save bfc = let
194 :     fun writer s =
195 :     (BF.write { stream = s, content = bfc,
196 :     nopickle = false };
197 :     Say.vsay ["[wrote ", binname, "]\n"])
198 :     fun cleanup () =
199 :     OS.FileSys.remove binname handle _ => ()
200 :     in
201 :     SafeIO.perform { openIt =
202 :     fn () => AutoDir.openBinOut binname,
203 :     closeIt = BinIO.closeOut,
204 :     work = writer,
205 :     cleanup = cleanup }
206 :     handle exn => let
207 :     fun ppb pps =
208 :     (PP.add_newline pps;
209 :     PP.add_string pps (General.exnMessage exn))
210 :     in
211 :     SmlInfo.error gp i EM.WARN
212 :     ("failed to write " ^ binname) ppb
213 :     end;
214 :     TStamp.setTime (binname, SmlInfo.lastseen i)
215 :     end (* save *)
216 :     in
217 :     case SmlInfo.parsetree gp i of
218 :     NONE => NONE
219 :     | SOME (ast, source) => let
220 :     val _ =
221 :     Say.vsay ["[compiling ", SmlInfo.descr i, "]\n"]
222 :     val corenv = #corenv (#param gp)
223 :     val cmData = PidSet.listItems pids
224 :     (* clear error flag (could still be set from
225 :     * earlier run) *)
226 :     val _ = #anyErrors source := false
227 :     val bfc = BF.create { runtimePid = NONE,
228 :     splitting = SmlInfo.split i,
229 :     cmData = cmData,
230 :     ast = ast,
231 :     source = source,
232 :     senv = stat,
233 :     symenv = sym,
234 :     corenv = corenv }
235 :     val memo = { bfc = bfc, ctxt = stat,
236 :     ts = SmlInfo.lastseen i}
237 :     in
238 :     SmlInfo.forgetParsetree i;
239 :     save bfc;
240 :     globalmap :=
241 :     SmlInfoMap.insert (!globalmap, i, memo);
242 :     SOME (memo2ed memo)
243 :     end
244 :     end (* compile *)
245 :     fun notlocal () = let
246 :     (* Ok, it is not in the local map, so we first have
247 :     * to traverse all children before we can proceed... *)
248 :     val k = #keep_going (#param gp)
249 :     fun loc li_n = Option.map nofilter (snode gp li_n)
250 :     fun glob gi_n = fsbnode gp gi_n
251 :     val e =
252 :     layerwork k loc
253 :     (layerwork k glob (SOME (pervenv gp)) gi)
254 :     li
255 :     in
256 :     case e of
257 :     NONE => NONE
258 :     | SOME { envs, pids } => let
259 :     (* We have successfully traversed all
260 :     * children. Now it is time to check the
261 :     * global map... *)
262 :     fun fromfile () = let
263 :     val { stat, sym } = envs ()
264 :     fun load () = let
265 :     val ts = TStamp.fmodTime binname
266 :     fun openIt () = BinIO.openIn binname
267 :     fun reader s =
268 :     (BF.read { stream = s,
269 :     name = binname,
270 :     senv = stat },
271 :     ts)
272 :     in
273 :     SOME (SafeIO.perform
274 :     { openIt = openIt,
275 :     closeIt = BinIO.closeIn,
276 :     work = reader,
277 :     cleanup = fn () => () })
278 :     handle _ => NONE
279 :     end (* load *)
280 :     in
281 :     case load () of
282 :     NONE => compile (stat, sym, pids)
283 :     | SOME (bfc, ts) => let
284 :     val memo = { bfc = bfc,
285 :     ctxt = stat,
286 :     ts = ts }
287 :     in
288 :     if isValidMemo (memo, pids, i) then
289 :     (globalmap :=
290 :     SmlInfoMap.insert
291 :     (!globalmap, i, memo);
292 :     SOME (memo2ed memo))
293 :     else compile (stat, sym, pids)
294 :     end
295 :     end (* fromfile *)
296 :     in
297 :     case SmlInfoMap.find (!globalmap, i) of
298 :     NONE => fromfile ()
299 :     | SOME memo =>
300 :     if isValidMemo (memo, pids, i) then
301 :     SOME (memo2ed memo)
302 :     else fromfile ()
303 :     end
304 :     end (* notlocal *)
305 :     in
306 :     case SmlInfoMap.find (!localmap, i) of
307 :     SOME edopt => edopt
308 :     | NONE => let
309 :     val edopt = notlocal ()
310 :     in
311 :     localmap := SmlInfoMap.insert (!localmap, i, edopt);
312 :     edopt
313 :     end
314 :     end (* snode *)
315 :    
316 :     fun impexp gp (n, _) = fsbnode gp n
317 :    
318 :     fun envdelta2ed { ii, bfc, ctxt } = { ii = ii, ctxt = ctxt () }
319 :     in
320 :     { sbnode = fn gp => fn n => Option.map envdelta2ed (sbnode gp n),
321 :     impexp = impexp }
322 :     end
323 :    
324 :     fun recomp gp (GG.GROUP { exports, ... }) = let
325 :     val { impexp, ... } = newTraversal ()
326 :     val k = #keep_going (#param gp)
327 :     fun loop ([], success) = success
328 :     | loop (h :: t, success) =
329 :     if isSome (impexp gp h) then loop (t, success)
330 :     else if k then loop (t, false) else false
331 :     in
332 :     loop (SymbolMap.listItems exports, true)
333 :     end
334 :    
335 :     local
336 :     fun get i = valOf (SmlInfoMap.find (!globalmap, i))
337 :     in
338 :     fun sizeBFC i = BF.size { content = #bfc (get i), nopickle = true }
339 :     fun writeBFC s i = BF.write { content = #bfc (get i),
340 :     stream = s, nopickle = true }
341 :     fun getII i = memo2ii (get i)
342 :     end
343 :     end
344 :     end

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