1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
14 // A simple struct to represent (write) effects. A write is represented as a
15 // modification of type bounds (e.g. of a variable).
17 // An effect can either be definite, if the write is known to have taken place,
18 // or 'possible', if it was optional. The difference is relevant when composing
21 // There are two ways to compose effects: sequentially (they happen one after
22 // the other) or alternatively (either one or the other happens). A definite
23 // effect cancels out any previous effect upon sequencing. A possible effect
24 // merges into a previous effect, i.e., type bounds are merged. Alternative
25 // composition always merges bounds. It yields a possible effect if at least
26 // one was only possible.
28 enum Modality { POSSIBLE, DEFINITE };
33 Effect() : modality(DEFINITE) {}
34 explicit Effect(Bounds b, Modality m = DEFINITE) : modality(m), bounds(b) {}
36 // The unknown effect.
37 static Effect Unknown(Zone* zone) {
38 return Effect(Bounds::Unbounded(), POSSIBLE);
41 static Effect Forget(Zone* zone) {
42 return Effect(Bounds::Unbounded(), DEFINITE);
45 // Sequential composition, as in 'e1; e2'.
46 static Effect Seq(Effect e1, Effect e2, Zone* zone) {
47 if (e2.modality == DEFINITE) return e2;
48 return Effect(Bounds::Either(e1.bounds, e2.bounds, zone), e1.modality);
51 // Alternative composition, as in 'cond ? e1 : e2'.
52 static Effect Alt(Effect e1, Effect e2, Zone* zone) {
54 Bounds::Either(e1.bounds, e2.bounds, zone),
55 e1.modality == POSSIBLE ? POSSIBLE : e2.modality);
60 // Classes encapsulating sets of effects on variables.
62 // Effects maps variables to effects and supports sequential and alternative
65 // NestedEffects is an incremental representation that supports persistence
66 // through functional extension. It represents the map as an adjoin of a list
67 // of maps, whose tail can be shared.
69 // Both classes provide similar interfaces, implemented in parts through the
70 // EffectsMixin below (using sandwich style, to work around the style guide's
73 // We also (ab)use Effects/NestedEffects as a representation for abstract
74 // store typings. In that case, only definite effects are of interest.
76 template<class Var, class Base, class Effects>
77 class EffectsMixin: public Base {
79 explicit EffectsMixin(Zone* zone) : Base(zone) {}
81 Effect Lookup(Var var) {
83 return this->Find(var, &locator)
84 ? locator.value() : Effect::Unknown(Base::zone());
87 Bounds LookupBounds(Var var) {
88 Effect effect = Lookup(var);
89 return effect.modality == Effect::DEFINITE
90 ? effect.bounds : Bounds::Unbounded();
93 // Sequential composition.
94 void Seq(Var var, Effect effect) {
96 if (!this->Insert(var, &locator)) {
97 effect = Effect::Seq(locator.value(), effect, Base::zone());
99 locator.set_value(effect);
102 void Seq(Effects that) {
103 SeqMerger<EffectsMixin> merge = { *this };
104 that.ForEach(&merge);
107 // Alternative composition.
108 void Alt(Var var, Effect effect) {
110 if (!this->Insert(var, &locator)) {
111 effect = Effect::Alt(locator.value(), effect, Base::zone());
113 locator.set_value(effect);
116 void Alt(Effects that) {
117 AltWeakener<EffectsMixin> weaken = { *this, that };
118 this->ForEach(&weaken);
119 AltMerger<EffectsMixin> merge = { *this };
120 that.ForEach(&merge);
125 Overrider override = {
126 Effect::Forget(Base::zone()), Effects(Base::zone()) };
127 this->ForEach(&override);
128 Seq(override.effects);
132 typedef typename Base::Locator Locator;
136 void Call(Var var, Effect effect) { self.Seq(var, effect); }
142 void Call(Var var, Effect effect) { self.Alt(var, effect); }
148 void Call(Var var, Effect effect) {
149 if (effect.modality == Effect::DEFINITE && !other.Contains(var)) {
150 effect.modality = Effect::POSSIBLE;
152 self.Insert(var, &locator);
153 locator.set_value(effect);
161 void Call(Var var, Effect effect) { effects.Seq(var, new_effect); }
168 template<class Var, Var kNoVar> class Effects;
169 template<class Var, Var kNoVar> class NestedEffectsBase;
171 template<class Var, Var kNoVar>
174 explicit EffectsBase(Zone* zone) : map_(new(zone) Mapping(zone)) {}
176 bool IsEmpty() { return map_->is_empty(); }
179 friend class NestedEffectsBase<Var, kNoVar>;
181 EffectsMixin<Var, NestedEffectsBase<Var, kNoVar>, Effects<Var, kNoVar> >;
183 Zone* zone() { return map_->allocator().zone(); }
185 struct SplayTreeConfig {
187 typedef Effect Value;
188 static const Var kNoKey = kNoVar;
189 static Effect NoValue() { return Effect(); }
190 static int Compare(int x, int y) { return y - x; }
192 typedef ZoneSplayTree<SplayTreeConfig> Mapping;
193 typedef typename Mapping::Locator Locator;
195 bool Contains(Var var) {
196 DCHECK(var != kNoVar);
197 return map_->Contains(var);
199 bool Find(Var var, Locator* locator) {
200 DCHECK(var != kNoVar);
201 return map_->Find(var, locator);
203 bool Insert(Var var, Locator* locator) {
204 DCHECK(var != kNoVar);
205 return map_->Insert(var, locator);
208 template<class Callback>
209 void ForEach(Callback* callback) {
210 return map_->ForEach(callback);
217 template<class Var, Var kNoVar>
218 const Var EffectsBase<Var, kNoVar>::SplayTreeConfig::kNoKey;
220 template<class Var, Var kNoVar>
221 class Effects: public
222 EffectsMixin<Var, EffectsBase<Var, kNoVar>, Effects<Var, kNoVar> > {
224 explicit Effects(Zone* zone)
225 : EffectsMixin<Var, EffectsBase<Var, kNoVar>, Effects<Var, kNoVar> >(zone)
230 template<class Var, Var kNoVar>
231 class NestedEffectsBase {
233 explicit NestedEffectsBase(Zone* zone) : node_(new(zone) Node(zone)) {}
235 template<class Callback>
236 void ForEach(Callback* callback) {
237 if (node_->previous) NestedEffectsBase(node_->previous).ForEach(callback);
238 node_->effects.ForEach(callback);
241 Effects<Var, kNoVar> Top() { return node_->effects; }
244 for (Node* node = node_; node != NULL; node = node->previous) {
245 if (!node->effects.IsEmpty()) return false;
251 typedef typename EffectsBase<Var, kNoVar>::Locator Locator;
253 Zone* zone() { return node_->zone; }
255 void push() { node_ = new(node_->zone) Node(node_->zone, node_); }
256 void pop() { node_ = node_->previous; }
257 bool is_empty() { return node_ == NULL; }
259 bool Contains(Var var) {
260 DCHECK(var != kNoVar);
261 for (Node* node = node_; node != NULL; node = node->previous) {
262 if (node->effects.Contains(var)) return true;
267 bool Find(Var var, Locator* locator) {
268 DCHECK(var != kNoVar);
269 for (Node* node = node_; node != NULL; node = node->previous) {
270 if (node->effects.Find(var, locator)) return true;
275 bool Insert(Var var, Locator* locator);
278 struct Node: ZoneObject {
280 Effects<Var, kNoVar> effects;
282 explicit Node(Zone* zone, Node* previous = NULL)
283 : zone(zone), effects(zone), previous(previous) {}
286 explicit NestedEffectsBase(Node* node) : node_(node) {}
292 template<class Var, Var kNoVar>
293 bool NestedEffectsBase<Var, kNoVar>::Insert(Var var, Locator* locator) {
294 DCHECK(var != kNoVar);
295 if (!node_->effects.Insert(var, locator)) return false;
297 for (Node* node = node_->previous; node != NULL; node = node->previous) {
298 if (node->effects.Find(var, &shadowed)) {
299 // Initialize with shadowed entry.
300 locator->set_value(shadowed.value());
308 template<class Var, Var kNoVar>
309 class NestedEffects: public
310 EffectsMixin<Var, NestedEffectsBase<Var, kNoVar>, Effects<Var, kNoVar> > {
312 explicit NestedEffects(Zone* zone) :
313 EffectsMixin<Var, NestedEffectsBase<Var, kNoVar>, Effects<Var, kNoVar> >(
316 // Create an extension of the current effect set. The current set should not
317 // be modified while the extension is in use.
318 NestedEffects Push() {
319 NestedEffects result = *this;
324 NestedEffects Pop() {
325 NestedEffects result = *this;
327 DCHECK(!this->is_empty());
332 } // namespace internal
335 #endif // V8_EFFECTS_H_