Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | |
| 2 // Redistribution and use in source and binary forms, with or without | |
| 3 // modification, are permitted provided that the following conditions are | |
| 4 // met: | |
| 5 // | |
| 6 // * Redistributions of source code must retain the above copyright | |
| 7 // notice, this list of conditions and the following disclaimer. | |
| 8 // * Redistributions in binary form must reproduce the above | |
| 9 // copyright notice, this list of conditions and the following | |
| 10 // disclaimer in the documentation and/or other materials provided | |
| 11 // with the distribution. | |
| 12 // * Neither the name of Google Inc. nor the names of its | |
| 13 // contributors may be used to endorse or promote products derived | |
| 14 // from this software without specific prior written permission. | |
| 15 // | |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 27 | |
| 28 #ifndef V8_EFFECTS_H_ | |
| 29 #define V8_EFFECTS_H_ | |
| 30 | |
| 31 #include "v8.h" | |
| 32 | |
| 33 #include "types.h" | |
| 34 | |
| 35 namespace v8 { | |
| 36 namespace internal { | |
| 37 | |
| 38 | |
| 39 // A simple struct to represent (write) effects. A write is represented as a | |
| 40 // modification of type bounds (e.g. of a variable). | |
| 41 // | |
| 42 // An effect can either be definite, if the write is known to have taken place, | |
| 43 // or 'possible', if it was optional. The difference is relevant when composing | |
| 44 // effects. | |
| 45 // | |
| 46 // There are two ways to compose effects: sequentially (they happen one after | |
| 47 // the other) or alternatively (either one or the other happens). A definite | |
| 48 // effect cancels out any previous effect upon sequencing. A possible effect | |
| 49 // merges into a previous effect, i.e., type bounds are merged. Alternative | |
| 50 // composition always merges bounds. It yields a possible effect if at least | |
| 51 // one was only possible. | |
| 52 struct Effect { | |
| 53 enum Modality { POSSIBLE, DEFINITE }; | |
| 54 | |
| 55 Modality modality; | |
| 56 Bounds bounds; | |
| 57 | |
| 58 Effect() {} | |
| 59 Effect(Bounds b, Modality m = DEFINITE) : modality(m), bounds(b) {} | |
| 60 | |
| 61 // The unknown effect. | |
| 62 static Effect Unknown(Isolate* isolate) { | |
| 63 return Effect(Bounds::Unbounded(isolate), POSSIBLE); | |
| 64 } | |
| 65 | |
| 66 static Effect Forget(Isolate* isolate) { | |
| 67 return Effect(Bounds::Unbounded(isolate), DEFINITE); | |
| 68 } | |
| 69 | |
| 70 // Sequential composition, as in 'e1; e2'. | |
| 71 static Effect Seq(Effect e1, Effect e2, Isolate* isolate) { | |
| 72 if (e2.modality == DEFINITE) return e2; | |
| 73 return Effect(Bounds::Either(e1.bounds, e2.bounds, isolate), e1.modality); | |
| 74 } | |
| 75 | |
| 76 // Alternative composition, as in 'cond ? e1 : e2'. | |
| 77 static Effect Alt(Effect e1, Effect e2, Isolate* isolate) { | |
| 78 return Effect( | |
| 79 Bounds::Either(e1.bounds, e2.bounds, isolate), | |
| 80 e1.modality == POSSIBLE ? POSSIBLE : e2.modality); | |
| 81 } | |
| 82 }; | |
| 83 | |
| 84 | |
| 85 // Classes encapsulating sets of effects on variables. | |
| 86 // | |
| 87 // Effects maps variables to effects and supports sequential and alternative | |
| 88 // composition. | |
| 89 // | |
| 90 // NestedEffects is an incremental representation that supports persistence | |
| 91 // through functional extension. It represents the map as an adjoin of a list | |
| 92 // of maps, whose tail can be shared. | |
| 93 // | |
| 94 // Both classes provide similar interfaces, implemented in parts through the | |
| 95 // EffectsMixin below (using sandwich style, to work around the style guide's | |
| 96 // MI restriction). | |
| 97 // | |
| 98 // We also (ab)use Effects/NestedEffects as a representation for abstract | |
| 99 // store typings. In that case, only definite effects are of interest. | |
| 100 | |
| 101 template<class Var, class Base, class Effects> | |
| 102 class EffectsMixin: public Base { | |
| 103 public: | |
| 104 explicit EffectsMixin(Zone* zone) : Base(zone) {} | |
| 105 | |
| 106 Effect Lookup(Var var) { | |
| 107 Locator locator; | |
| 108 return Find(var, &locator) | |
| 109 ? locator.value() : Effect::Unknown(Base::isolate()); | |
| 110 } | |
| 111 | |
| 112 Bounds LookupBounds(Var var) { | |
| 113 Effect effect = Lookup(var); | |
| 114 return effect.modality == Effect::DEFINITE | |
| 115 ? effect.bounds : Bounds::Unbounded(Base::isolate()); | |
| 116 } | |
| 117 | |
| 118 // Sequential composition. | |
| 119 void Seq(Var var, Effect effect) { | |
| 120 Locator locator; | |
| 121 if (!Insert(var, &locator)) | |
| 122 effect = Effect::Seq(locator.value(), effect, Base::isolate()); | |
|
Jakob Kummerow
2013/07/19 12:09:03
nit: {} please
rossberg
2013/07/19 12:54:41
Done.
| |
| 123 locator.set_value(effect); | |
| 124 } | |
| 125 | |
| 126 void Seq(Effects that) { | |
| 127 SeqMerger<EffectsMixin> merge = { *this }; | |
| 128 that.ForEach(&merge); | |
| 129 } | |
| 130 | |
| 131 // Alternative composition. | |
| 132 void Alt(Var var, Effect effect) { | |
| 133 Locator locator; | |
| 134 if (!Insert(var, &locator)) | |
| 135 effect = Effect::Alt(locator.value(), effect, Base::isolate()); | |
|
Jakob Kummerow
2013/07/19 12:09:03
nit: {} please
rossberg
2013/07/19 12:54:41
Done.
| |
| 136 locator.set_value(effect); | |
| 137 } | |
| 138 | |
| 139 void Alt(Effects that) { | |
| 140 AltWeakener<EffectsMixin> weaken = { *this, that }; | |
| 141 this->ForEach(&weaken); | |
| 142 AltMerger<EffectsMixin> merge = { *this }; | |
| 143 that.ForEach(&merge); | |
| 144 } | |
| 145 | |
| 146 // Invalidation. | |
| 147 void Forget() { | |
| 148 Overrider override = { | |
| 149 Effect::Forget(Base::isolate()), Effects(Base::zone()) }; | |
| 150 this->ForEach(&override); | |
| 151 Seq(override.effects); | |
| 152 } | |
| 153 | |
| 154 protected: | |
| 155 typedef typename Base::Locator Locator; | |
| 156 | |
| 157 template<class Self> | |
| 158 struct SeqMerger { | |
| 159 void Call(Var var, Effect effect) { self.Seq(var, effect); } | |
| 160 Self self; | |
| 161 }; | |
| 162 | |
| 163 template<class Self> | |
| 164 struct AltMerger { | |
| 165 void Call(Var var, Effect effect) { self.Alt(var, effect); } | |
| 166 Self self; | |
| 167 }; | |
| 168 | |
| 169 template<class Self> | |
| 170 struct AltWeakener { | |
| 171 void Call(Var var, Effect effect) { | |
| 172 if (effect.modality == Effect::DEFINITE && !other.Contains(var)) { | |
| 173 effect.modality = Effect::POSSIBLE; | |
| 174 Locator locator; | |
| 175 self.Insert(var, &locator); | |
| 176 locator.set_value(effect); | |
| 177 } | |
| 178 } | |
| 179 Self self; | |
| 180 Effects other; | |
| 181 }; | |
| 182 | |
| 183 struct Overrider { | |
| 184 void Call(Var var, Effect effect) { effects.Seq(var, new_effect); } | |
| 185 Effect new_effect; | |
| 186 Effects effects; | |
| 187 }; | |
| 188 }; | |
| 189 | |
| 190 | |
| 191 template<class Var, Var kNoVar> class Effects; | |
| 192 template<class Var, Var kNoVar> class NestedEffectsBase; | |
| 193 | |
| 194 template<class Var, Var kNoVar> | |
| 195 class EffectsBase { | |
| 196 public: | |
| 197 explicit EffectsBase(Zone* zone) : map_(new(zone) Map(zone)) {} | |
| 198 | |
| 199 bool IsEmpty() { return map_->is_empty(); } | |
| 200 | |
| 201 protected: | |
| 202 friend class NestedEffectsBase<Var, kNoVar>; | |
| 203 friend class | |
| 204 EffectsMixin<Var, NestedEffectsBase<Var, kNoVar>, Effects<Var, kNoVar> >; | |
| 205 | |
| 206 Zone* zone() { return map_->allocator().zone(); } | |
| 207 Isolate* isolate() { return zone()->isolate(); } | |
| 208 | |
| 209 struct SplayTreeConfig { | |
| 210 typedef Var Key; | |
| 211 typedef Effect Value; | |
| 212 static const Var kNoKey = kNoVar; | |
| 213 static Effect NoValue() { return Effect(); } | |
| 214 static int Compare(int x, int y) { return y - x; } | |
| 215 }; | |
| 216 typedef ZoneSplayTree<SplayTreeConfig> Map; | |
|
Jakob Kummerow
2013/07/19 12:09:03
We already have a v8::internal::Map. Please use an
rossberg
2013/07/19 12:54:41
Done.
| |
| 217 typedef typename Map::Locator Locator; | |
| 218 | |
| 219 bool Contains(Var var) { | |
| 220 ASSERT(var != kNoVar); | |
| 221 return map_->Contains(var); | |
| 222 } | |
| 223 bool Find(Var var, Locator* locator) { | |
| 224 ASSERT(var != kNoVar); | |
| 225 return map_->Find(var, locator); | |
| 226 } | |
| 227 bool Insert(Var var, Locator* locator) { | |
| 228 ASSERT(var != kNoVar); | |
| 229 return map_->Insert(var, locator); | |
| 230 } | |
| 231 | |
| 232 template<class Callback> | |
| 233 void ForEach(Callback* callback) { | |
| 234 return map_->ForEach(callback); | |
| 235 } | |
| 236 | |
| 237 private: | |
| 238 Map* map_; | |
| 239 }; | |
| 240 | |
| 241 template<class Var, Var kNoVar> | |
| 242 const Var EffectsBase<Var, kNoVar>::SplayTreeConfig::kNoKey; | |
| 243 | |
| 244 template<class Var, Var kNoVar> | |
| 245 class Effects: public | |
| 246 EffectsMixin<Var, EffectsBase<Var, kNoVar>, Effects<Var, kNoVar> > { | |
| 247 public: | |
| 248 explicit Effects(Zone* zone) : | |
|
Jakob Kummerow
2013/07/19 12:09:03
nit: ':' goes on the next line. Line break suggest
rossberg
2013/07/19 12:54:41
Done (though with fewer breaks)
| |
| 249 EffectsMixin<Var, EffectsBase<Var, kNoVar>, Effects<Var, kNoVar> >(zone) | |
| 250 {} | |
| 251 }; | |
| 252 | |
| 253 | |
| 254 template<class Var, Var kNoVar> | |
| 255 class NestedEffectsBase { | |
| 256 public: | |
| 257 explicit NestedEffectsBase(Zone* zone) : node_(new(zone) Node(zone)) {} | |
| 258 | |
| 259 template<class Callback> | |
| 260 void ForEach(Callback* callback) { | |
| 261 if (node_->previous) NestedEffectsBase(node_->previous).ForEach(callback); | |
| 262 node_->effects.ForEach(callback); | |
| 263 } | |
| 264 | |
| 265 Effects<Var, kNoVar> Top() { return node_->effects; } | |
| 266 | |
| 267 bool IsEmpty() { | |
| 268 for (Node* node = node_; node != NULL; node = node->previous) { | |
| 269 if (!node->effects.IsEmpty()) return false; | |
| 270 } | |
| 271 return true; | |
| 272 } | |
| 273 | |
| 274 protected: | |
| 275 typedef typename EffectsBase<Var, kNoVar>::Locator Locator; | |
| 276 | |
| 277 Zone* zone() { return node_->zone; } | |
| 278 Isolate* isolate() { return zone()->isolate(); } | |
| 279 | |
| 280 void push() { node_ = new(node_->zone) Node(node_->zone, node_); } | |
| 281 void pop() { node_ = node_->previous; } | |
| 282 bool is_empty() { return node_ == NULL; } | |
| 283 | |
| 284 bool Contains(Var var) { | |
| 285 ASSERT(var != kNoVar); | |
| 286 for (Node* node = node_; node != NULL; node = node->previous) { | |
| 287 if (node->effects.Contains(var)) return true; | |
| 288 } | |
| 289 return false; | |
| 290 } | |
| 291 | |
| 292 bool Find(Var var, Locator* locator) { | |
| 293 ASSERT(var != kNoVar); | |
| 294 for (Node* node = node_; node != NULL; node = node->previous) { | |
| 295 if (node->effects.Find(var, locator)) return true; | |
| 296 } | |
| 297 return false; | |
| 298 } | |
| 299 | |
| 300 bool Insert(Var var, Locator* locator); | |
| 301 | |
| 302 private: | |
| 303 struct Node: ZoneObject { | |
| 304 Zone* zone; | |
| 305 Effects<Var, kNoVar> effects; | |
| 306 Node* previous; | |
| 307 explicit Node(Zone* zone, Node* previous = NULL) | |
| 308 : zone(zone), effects(zone), previous(previous) {} | |
| 309 }; | |
| 310 | |
| 311 explicit NestedEffectsBase(Node* node) : node_(node) {} | |
| 312 | |
| 313 Node* node_; | |
| 314 }; | |
| 315 | |
| 316 | |
| 317 template<class Var, Var kNoVar> | |
| 318 bool NestedEffectsBase<Var, kNoVar>::Insert(Var var, Locator* locator) { | |
| 319 ASSERT(var != kNoVar); | |
| 320 if (!node_->effects.Insert(var, locator)) return false; | |
| 321 Locator shadowed; | |
| 322 for (Node* node = node_->previous; node != NULL; node = node->previous) { | |
| 323 if (node->effects.Find(var, &shadowed)) { | |
| 324 // Initialize with shadowed entry. | |
| 325 locator->set_value(shadowed.value()); | |
| 326 return false; | |
| 327 } | |
| 328 } | |
| 329 return true; | |
| 330 } | |
| 331 | |
| 332 | |
| 333 template<class Var, Var kNoVar> | |
| 334 class NestedEffects: public | |
| 335 EffectsMixin<Var, NestedEffectsBase<Var, kNoVar>, Effects<Var, kNoVar> > { | |
| 336 public: | |
| 337 explicit NestedEffects(Zone* zone) : | |
| 338 EffectsMixin<Var, NestedEffectsBase<Var, kNoVar>, Effects<Var, kNoVar> >( | |
| 339 zone) {} | |
| 340 | |
| 341 // Create an extension of the current effect set. The current set should not | |
| 342 // be modified while the extension is in use. | |
| 343 NestedEffects Push() { | |
| 344 NestedEffects result = *this; | |
| 345 result.push(); | |
| 346 return result; | |
| 347 } | |
| 348 | |
| 349 NestedEffects Pop() { | |
| 350 NestedEffects result = *this; | |
| 351 result.pop(); | |
| 352 ASSERT(!this->is_empty()); | |
| 353 return result; | |
| 354 } | |
| 355 }; | |
| 356 | |
| 357 } } // namespace v8::internal | |
| 358 | |
| 359 #endif // V8_EFFECTS_H_ | |
| OLD | NEW |