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

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