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/main/autoload.sml
ViewVC logotype

Annotation of /sml/trunk/src/cm/main/autoload.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 399 - (view) (download)

1 : blume 355 (*
2 :     * The CM autoloading mechanism.
3 :     *
4 :     * (C) 1999 Lucent Technologies, Bell Laboratories
5 :     *
6 :     * Author: Matthias Blume (blume@kurims.kyoto-u.ac.jp)
7 :     *)
8 : blume 362 local
9 : blume 399 structure GP = GeneralParams
10 : blume 362 structure DG = DependencyGraph
11 :     structure BE = GenericVC.BareEnvironment
12 :     structure ER = GenericVC.EnvRef
13 :     structure GG = GroupGraph
14 : blume 372 structure E = GenericVC.Environment
15 : blume 399 structure EM = GenericVC.ErrorMsg
16 : blume 362 in
17 : blume 355 signature AUTOLOAD = sig
18 :    
19 : blume 362 val register : ER.envref * GG.group -> unit
20 : blume 355
21 : blume 399 val mkManager : (unit -> GP.info) -> GenericVC.Ast.dec * ER.envref -> unit
22 : blume 361
23 : blume 362 val getPending : unit -> DG.impexp SymbolMap.map
24 :    
25 : blume 361 val reset : unit -> unit
26 : blume 355 end
27 :    
28 : blume 399 functor AutoLoadFn (structure C : COMPILE
29 :     structure L : LINK) :> AUTOLOAD = struct
30 : blume 355
31 : blume 364 structure SE = GenericVC.StaticEnv
32 :    
33 : blume 399 type traversal = GP.info -> E.environment option
34 : blume 355 (* We let the topLevel env *logically* sit atop the pending
35 :     * autoload bindings. This way we do not have to intercept every
36 :     * change to the topLevel env. However, it means that any addition
37 :     * to "pending" must be subtracted from the topLevel env. *)
38 : blume 399 val pending = ref (SymbolMap.empty: (DG.impexp * traversal) SymbolMap.map)
39 : blume 355
40 : blume 361 fun reset () = pending := SymbolMap.empty
41 :    
42 : blume 399 fun register (ter: ER.envref, g as GG.GROUP { exports, ... }) = let
43 : blume 355 val te = #get ter ()
44 :     (* toplevel bindings (symbol set) ... *)
45 :     val tss = foldl SymbolSet.add' SymbolSet.empty
46 :     (BE.catalogEnv (BE.staticPart te))
47 :     (* "new" bindings (symbol set) ... *)
48 :     val nss = SymbolMap.foldli (fn (i, _, s) => SymbolSet.add (s, i))
49 :     SymbolSet.empty exports
50 :     (* to-be-retained bindings ... *)
51 :     val rss = SymbolSet.difference (tss, nss)
52 :     (* getting rid of unneeded bindings... *)
53 :     val te' = BE.filterEnv (te, SymbolSet.listItems rss)
54 : blume 372 (* make traversal states *)
55 : blume 399 val { exports = cTrav, ... } = C.newTraversal (L.evict, g)
56 :     val { exports = lTrav, ... } = L.newTraversal g
57 :     fun combine (ss, d) gp =
58 :     case ss gp of
59 :     SOME { stat, sym } =>
60 :     (case d gp of
61 :     SOME dyn => SOME (E.mkenv { static = stat,
62 :     symbolic = sym,
63 :     dynamic = dyn })
64 :     | NONE => NONE)
65 :     | NONE => NONE
66 :     fun mkNode (sy, ie) =
67 :     (ie, combine (valOf (SymbolMap.find (cTrav, sy)),
68 :     valOf (SymbolMap.find (lTrav, sy))))
69 :     val newNodes = SymbolMap.mapi mkNode exports
70 : blume 355 in
71 :     #set ter te';
72 : blume 399 pending := SymbolMap.unionWith #1 (newNodes, !pending)
73 : blume 355 end
74 :    
75 : blume 372 fun mkManager get_ginfo (ast, ter: ER.envref) = let
76 :    
77 :     val gp = get_ginfo ()
78 :    
79 : blume 399 fun loadit m = let
80 :     fun one ((_, tr), NONE) = NONE
81 :     | one ((_, tr), SOME e) =
82 :     (case tr gp of
83 :     NONE => NONE
84 :     | SOME e' => let
85 :     val be = GenericVC.CoerceEnv.e2b e'
86 :     in
87 :     SOME (BE.concatEnv (be, e))
88 :     end)
89 :     in
90 :     (* make sure that there are no stale value around... *)
91 :     L.cleanup ();
92 :     SymbolMap.foldl one (SOME BE.emptyEnv) m
93 :     end
94 : blume 372
95 : blume 355 val { skeleton, ... } =
96 :     SkelCvt.convert { tree = ast, err = fn _ => fn _ => fn _ => () }
97 :     val te = #get ter ()
98 : blume 364 val ste = BE.staticPart te
99 :    
100 :     (* First, we get rid of anything in "pending" that has
101 :     * meanwhile been added to the toplevel. *)
102 :     fun notTopDefined (sy, _) =
103 :     (SE.look (ste, sy); false) handle SE.Unbound => true
104 :     val pend = SymbolMap.filteri notTopDefined (!pending)
105 :     val _ = pending := pend
106 :     val (dae, _) = Statenv2DAEnv.cvt ste
107 : blume 355 val load = ref SymbolMap.empty
108 : blume 399 fun lookpend sy = let
109 :     fun otherwise _ = EM.impossible "Autoload:lookpend"
110 :     in
111 : blume 355 case SymbolMap.find (pend, sy) of
112 : blume 399 SOME (x as ((_, e), _)) =>
113 : blume 372 (load := SymbolMap.insert (!load, sy, x);
114 : blume 399 BuildDepend.look otherwise e sy)
115 : blume 355 | NONE => DAEnv.EMPTY
116 : blume 399 end
117 : blume 355 val lookimport = BuildDepend.look lookpend dae
118 :     val _ = BuildDepend.processOneSkeleton lookimport skeleton
119 : blume 364
120 :     (* Here are the nodes that actually have been picked because
121 :     * something demanded an exported symbol: *)
122 :     val loadmap0 = !load
123 :    
124 :     (* However, we want to avoid hanging on to stuff unnecessarily, so
125 :     * we now look for symbols that become available "for free" because
126 :     * their corresponding node has been picked. So we first build
127 :     * three sets: sml- and stable-infos of picked nodes as well
128 :     * as the set of PNODEs: *)
129 : blume 399 fun add ((((_, DG.SB_SNODE (DG.SNODE { smlinfo, ... })), _), _),
130 : blume 364 (ss, bs, ps)) =
131 :     (SmlInfoSet.add (ss, smlinfo), bs, ps)
132 : blume 399 | add ((((_, DG.SB_BNODE (DG.BNODE { bininfo, ... }, _)), _), _),
133 : blume 364 (ss, bs, ps)) =
134 :     (ss, StableSet.add (bs, bininfo), ps)
135 : blume 399 | add ((((_, DG.SB_BNODE (DG.PNODE p, _)), _), _), (ss, bs, ps)) =
136 : blume 364 (ss, bs, StringSet.add (ps, Primitive.toString p))
137 :    
138 :     val (smlinfos, stableinfos, prims) =
139 :     SymbolMap.foldl add
140 :     (SmlInfoSet.empty, StableSet.empty, StringSet.empty)
141 :     loadmap0
142 :    
143 :     (* now we can easily find out whether a node has been picked... *)
144 : blume 399 fun isPicked (((_, DG.SB_SNODE (DG.SNODE n)), _), _) =
145 : blume 372 SmlInfoSet.member (smlinfos, #smlinfo n)
146 : blume 399 | isPicked (((_, DG.SB_BNODE (DG.BNODE n, _)), _), _) =
147 : blume 372 StableSet.member (stableinfos, #bininfo n)
148 : blume 399 | isPicked (((_, DG.SB_BNODE (DG.PNODE p, _)), _), _) =
149 : blume 364 StringSet.member (prims, Primitive.toString p)
150 :    
151 :     val loadmap = SymbolMap.filter isPicked pend
152 :     val noloadmap = SymbolMap.filter (not o isPicked) pend
153 : blume 355 in
154 : blume 366 if SymbolMap.isEmpty loadmap then ()
155 :     else
156 : blume 375 (Say.say ["[autoloading]\n"];
157 : blume 366 SrcPath.revalidateCwd ();
158 :     (* We temporarily turn verbosity off, so we need to wrap this
159 :     * with a SafeIO.perform... *)
160 :     SafeIO.perform
161 :     { openIt = fn () =>
162 :     EnvConfig.getSet StdConfig.verbose (SOME false),
163 :     closeIt = ignore o (EnvConfig.getSet StdConfig.verbose) o SOME,
164 :     cleanup = fn () => (),
165 :     work = fn _ =>
166 :     (case loadit loadmap of
167 :     SOME e =>
168 :     (#set ter (BE.concatEnv (e, te));
169 :     pending := noloadmap;
170 : blume 372 Say.say ["[autoloading done]\n"])
171 : blume 377 | NONE => raise Fail "unable to load module(s)") }
172 :     handle Fail msg =>
173 :     Say.say ["[autoloading failed: ", msg, "]\n"])
174 : blume 355 end
175 : blume 362
176 : blume 372 fun getPending () = SymbolMap.map #1 (!pending)
177 : blume 355 end
178 : blume 362 end

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