blob: a195b27d85a0b180eaa7f71eaf3e8694331320b3 [file] [log] [blame]
mstarzinger@chromium.orgb4968be2013-10-16 09:00:56 +00001// 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
36namespace v8 {
37namespace internal {
38
39
40// AllocationSiteContext is the base class for walking and copying a nested
41// boilerplate with AllocationSite and AllocationMemento support.
42class AllocationSiteContext {
43 public:
machenbach@chromium.orgc86e8c22013-11-27 15:11:04 +000044 explicit AllocationSiteContext(Isolate* isolate) {
mstarzinger@chromium.orgb4968be2013-10-16 09:00:56 +000045 isolate_ = isolate;
mstarzinger@chromium.orgb4968be2013-10-16 09:00:56 +000046 };
mstarzinger@chromium.orgb4968be2013-10-16 09:00:56 +000047
48 Handle<AllocationSite> top() { return top_; }
49 Handle<AllocationSite> current() { return current_; }
50
machenbach@chromium.orgc86e8c22013-11-27 15:11:04 +000051 bool ShouldCreateMemento(Handle<JSObject> object) { return false; }
mstarzinger@chromium.orgb4968be2013-10-16 09:00:56 +000052
machenbach@chromium.org37be4082013-11-26 13:50:38 +000053 Isolate* isolate() { return isolate_; }
mstarzinger@chromium.orgb4968be2013-10-16 09:00:56 +000054
55 protected:
56 void update_current_site(AllocationSite* site) {
57 *(current_.location()) = site;
58 }
59
mstarzinger@chromium.orgb4968be2013-10-16 09:00:56 +000060 void InitializeTraversal(Handle<AllocationSite> site) {
61 top_ = site;
62 current_ = Handle<AllocationSite>(*top_, isolate());
63 }
64
65 private:
66 Isolate* isolate_;
67 Handle<AllocationSite> top_;
68 Handle<AllocationSite> current_;
mstarzinger@chromium.orgb4968be2013-10-16 09:00:56 +000069};
70
71
72// AllocationSiteCreationContext aids in the creation of AllocationSites to
73// accompany object literals.
74class AllocationSiteCreationContext : public AllocationSiteContext {
75 public:
76 explicit AllocationSiteCreationContext(Isolate* isolate)
machenbach@chromium.orgc86e8c22013-11-27 15:11:04 +000077 : AllocationSiteContext(isolate) { }
mstarzinger@chromium.orgb4968be2013-10-16 09:00:56 +000078
machenbach@chromium.org37be4082013-11-26 13:50:38 +000079 Handle<AllocationSite> EnterNewScope();
80 void ExitScope(Handle<AllocationSite> site, Handle<JSObject> object);
mstarzinger@chromium.orgb4968be2013-10-16 09:00:56 +000081};
82
83
84// AllocationSiteUsageContext aids in the creation of AllocationMementos placed
85// behind some/all components of a copied object literal.
86class AllocationSiteUsageContext : public AllocationSiteContext {
87 public:
88 AllocationSiteUsageContext(Isolate* isolate, Handle<AllocationSite> site,
89 bool activated)
machenbach@chromium.orgc86e8c22013-11-27 15:11:04 +000090 : AllocationSiteContext(isolate),
91 top_site_(site),
92 activated_(activated) { }
mstarzinger@chromium.orgb4968be2013-10-16 09:00:56 +000093
machenbach@chromium.org37be4082013-11-26 13:50:38 +000094 inline Handle<AllocationSite> EnterNewScope() {
95 if (top().is_null()) {
96 InitializeTraversal(top_site_);
97 } else {
98 // Advance current site
99 Object* nested_site = current()->nested_site();
100 // Something is wrong if we advance to the end of the list here.
101 ASSERT(nested_site->IsAllocationSite());
102 update_current_site(AllocationSite::cast(nested_site));
103 }
104 return Handle<AllocationSite>(*current(), isolate());
105 }
106
107 inline void ExitScope(Handle<AllocationSite> scope_site,
108 Handle<JSObject> object) {
109 // This assert ensures that we are pointing at the right sub-object in a
110 // recursive walk of a nested literal.
111 ASSERT(object.is_null() || *object == scope_site->transition_info());
112 }
mstarzinger@chromium.orgb4968be2013-10-16 09:00:56 +0000113
machenbach@chromium.orgc86e8c22013-11-27 15:11:04 +0000114 bool ShouldCreateMemento(Handle<JSObject> object);
115
mstarzinger@chromium.orgb4968be2013-10-16 09:00:56 +0000116 private:
117 Handle<AllocationSite> top_site_;
machenbach@chromium.orgc86e8c22013-11-27 15:11:04 +0000118 bool activated_;
mstarzinger@chromium.orgb4968be2013-10-16 09:00:56 +0000119};
120
121
122} } // namespace v8::internal
123
124#endif // V8_ALLOCATION_SITE_SCOPES_H_