mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame] | 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_ALLOCATION_SITE_SCOPES_H_ |
| 29 | #define V8_ALLOCATION_SITE_SCOPES_H_ |
| 30 | |
| 31 | #include "ast.h" |
| 32 | #include "handles.h" |
| 33 | #include "objects.h" |
| 34 | #include "zone.h" |
| 35 | |
| 36 | namespace v8 { |
| 37 | namespace internal { |
| 38 | |
| 39 | |
| 40 | // AllocationSiteContext is the base class for walking and copying a nested |
| 41 | // boilerplate with AllocationSite and AllocationMemento support. |
| 42 | class AllocationSiteContext { |
| 43 | public: |
| 44 | AllocationSiteContext(Isolate* isolate, bool activated) { |
| 45 | isolate_ = isolate; |
| 46 | activated_ = activated; |
| 47 | }; |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame] | 48 | |
| 49 | Handle<AllocationSite> top() { return top_; } |
| 50 | Handle<AllocationSite> current() { return current_; } |
| 51 | |
| 52 | // If activated, then recursively create mementos |
| 53 | bool activated() const { return activated_; } |
| 54 | |
machenbach@chromium.org | 37be408 | 2013-11-26 13:50:38 +0000 | [diff] [blame^] | 55 | Isolate* isolate() { return isolate_; } |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame] | 56 | |
| 57 | protected: |
| 58 | void update_current_site(AllocationSite* site) { |
| 59 | *(current_.location()) = site; |
| 60 | } |
| 61 | |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame] | 62 | void InitializeTraversal(Handle<AllocationSite> site) { |
| 63 | top_ = site; |
| 64 | current_ = Handle<AllocationSite>(*top_, isolate()); |
| 65 | } |
| 66 | |
| 67 | private: |
| 68 | Isolate* isolate_; |
| 69 | Handle<AllocationSite> top_; |
| 70 | Handle<AllocationSite> current_; |
| 71 | bool activated_; |
| 72 | }; |
| 73 | |
| 74 | |
| 75 | // AllocationSiteCreationContext aids in the creation of AllocationSites to |
| 76 | // accompany object literals. |
| 77 | class AllocationSiteCreationContext : public AllocationSiteContext { |
| 78 | public: |
| 79 | explicit AllocationSiteCreationContext(Isolate* isolate) |
| 80 | : AllocationSiteContext(isolate, true) { } |
| 81 | |
machenbach@chromium.org | 37be408 | 2013-11-26 13:50:38 +0000 | [diff] [blame^] | 82 | Handle<AllocationSite> EnterNewScope(); |
| 83 | void ExitScope(Handle<AllocationSite> site, Handle<JSObject> object); |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | |
| 87 | // AllocationSiteUsageContext aids in the creation of AllocationMementos placed |
| 88 | // behind some/all components of a copied object literal. |
| 89 | class AllocationSiteUsageContext : public AllocationSiteContext { |
| 90 | public: |
| 91 | AllocationSiteUsageContext(Isolate* isolate, Handle<AllocationSite> site, |
| 92 | bool activated) |
| 93 | : AllocationSiteContext(isolate, activated), |
| 94 | top_site_(site) { } |
| 95 | |
machenbach@chromium.org | 37be408 | 2013-11-26 13:50:38 +0000 | [diff] [blame^] | 96 | inline Handle<AllocationSite> EnterNewScope() { |
| 97 | if (top().is_null()) { |
| 98 | InitializeTraversal(top_site_); |
| 99 | } else { |
| 100 | // Advance current site |
| 101 | Object* nested_site = current()->nested_site(); |
| 102 | // Something is wrong if we advance to the end of the list here. |
| 103 | ASSERT(nested_site->IsAllocationSite()); |
| 104 | update_current_site(AllocationSite::cast(nested_site)); |
| 105 | } |
| 106 | return Handle<AllocationSite>(*current(), isolate()); |
| 107 | } |
| 108 | |
| 109 | inline void ExitScope(Handle<AllocationSite> scope_site, |
| 110 | Handle<JSObject> object) { |
| 111 | // This assert ensures that we are pointing at the right sub-object in a |
| 112 | // recursive walk of a nested literal. |
| 113 | ASSERT(object.is_null() || *object == scope_site->transition_info()); |
| 114 | } |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame] | 115 | |
| 116 | private: |
| 117 | Handle<AllocationSite> top_site_; |
| 118 | }; |
| 119 | |
| 120 | |
| 121 | } } // namespace v8::internal |
| 122 | |
| 123 | #endif // V8_ALLOCATION_SITE_SCOPES_H_ |