[presubmit] Enable readability/namespace linter checking.
[platform/upstream/v8.git] / src / ic / ic-inl.h
1 // Copyright 2012 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.
4
5 #ifndef V8_IC_INL_H_
6 #define V8_IC_INL_H_
7
8 #include "src/ic/ic.h"
9
10 #include "src/compiler.h"
11 #include "src/debug/debug.h"
12 #include "src/macro-assembler.h"
13 #include "src/prototype.h"
14
15 namespace v8 {
16 namespace internal {
17
18
19 Address IC::address() const {
20   // Get the address of the call.
21   return Assembler::target_address_from_return_address(pc());
22 }
23
24
25 Address IC::constant_pool() const {
26   if (FLAG_enable_embedded_constant_pool) {
27     return raw_constant_pool();
28   } else {
29     return NULL;
30   }
31 }
32
33
34 Address IC::raw_constant_pool() const {
35   if (FLAG_enable_embedded_constant_pool) {
36     return *constant_pool_address_;
37   } else {
38     return NULL;
39   }
40 }
41
42
43 Code* IC::GetTargetAtAddress(Address address, Address constant_pool) {
44   // Get the target address of the IC.
45   Address target = Assembler::target_address_at(address, constant_pool);
46   // Convert target address to the code object. Code::GetCodeFromTargetAddress
47   // is safe for use during GC where the map might be marked.
48   Code* result = Code::GetCodeFromTargetAddress(target);
49   DCHECK(result->is_inline_cache_stub());
50   return result;
51 }
52
53
54 void IC::SetTargetAtAddress(Address address, Code* target,
55                             Address constant_pool) {
56   if (AddressIsDeoptimizedCode(target->GetIsolate(), address)) return;
57
58   DCHECK(target->is_inline_cache_stub() || target->is_compare_ic_stub());
59
60   DCHECK(!target->is_inline_cache_stub() ||
61          (target->kind() != Code::LOAD_IC &&
62           target->kind() != Code::KEYED_LOAD_IC &&
63           target->kind() != Code::CALL_IC &&
64           (!FLAG_vector_stores || (target->kind() != Code::STORE_IC &&
65                                    target->kind() != Code::KEYED_STORE_IC))));
66
67   Heap* heap = target->GetHeap();
68   Code* old_target = GetTargetAtAddress(address, constant_pool);
69 #ifdef DEBUG
70   // STORE_IC and KEYED_STORE_IC use Code::extra_ic_state() to mark
71   // ICs as language mode. The language mode of the IC must be preserved.
72   if (old_target->kind() == Code::STORE_IC ||
73       old_target->kind() == Code::KEYED_STORE_IC) {
74     DCHECK(StoreICState::GetLanguageMode(old_target->extra_ic_state()) ==
75            StoreICState::GetLanguageMode(target->extra_ic_state()));
76   }
77 #endif
78   Assembler::set_target_address_at(address, constant_pool,
79                                    target->instruction_start());
80   if (heap->gc_state() == Heap::MARK_COMPACT) {
81     heap->mark_compact_collector()->RecordCodeTargetPatch(address, target);
82   } else {
83     heap->incremental_marking()->RecordCodeTargetPatch(address, target);
84   }
85   PostPatching(address, target, old_target);
86 }
87
88
89 void IC::set_target(Code* code) {
90   SetTargetAtAddress(address(), code, constant_pool());
91   target_set_ = true;
92 }
93
94
95 void LoadIC::set_target(Code* code) {
96   // The contextual mode must be preserved across IC patching.
97   DCHECK(LoadICState::GetTypeofMode(code->extra_ic_state()) ==
98          LoadICState::GetTypeofMode(target()->extra_ic_state()));
99   // Strongness must be preserved across IC patching.
100   DCHECK(LoadICState::GetLanguageMode(code->extra_ic_state()) ==
101          LoadICState::GetLanguageMode(target()->extra_ic_state()));
102
103   IC::set_target(code);
104 }
105
106
107 void StoreIC::set_target(Code* code) {
108   // Language mode must be preserved across IC patching.
109   DCHECK(StoreICState::GetLanguageMode(code->extra_ic_state()) ==
110          StoreICState::GetLanguageMode(target()->extra_ic_state()));
111   IC::set_target(code);
112 }
113
114
115 void KeyedStoreIC::set_target(Code* code) {
116   // Language mode must be preserved across IC patching.
117   DCHECK(StoreICState::GetLanguageMode(code->extra_ic_state()) ==
118          language_mode());
119   IC::set_target(code);
120 }
121
122
123 Code* IC::raw_target() const {
124   return GetTargetAtAddress(address(), constant_pool());
125 }
126
127 void IC::UpdateTarget() { target_ = handle(raw_target(), isolate_); }
128
129
130 JSFunction* IC::GetRootConstructor(Map* receiver_map, Context* native_context) {
131   DisallowHeapAllocation no_alloc;
132   if (receiver_map->IsPrimitiveMap()) {
133     int constructor_function_index =
134         receiver_map->GetConstructorFunctionIndex();
135     if (constructor_function_index != Map::kNoConstructorFunctionIndex) {
136       return JSFunction::cast(native_context->get(constructor_function_index));
137     }
138   }
139   return nullptr;
140 }
141
142
143 Handle<Map> IC::GetHandlerCacheHolder(Handle<Map> receiver_map,
144                                       bool receiver_is_holder, Isolate* isolate,
145                                       CacheHolderFlag* flag) {
146   if (receiver_is_holder) {
147     *flag = kCacheOnReceiver;
148     return receiver_map;
149   }
150   Context* native_context = *isolate->native_context();
151   JSFunction* builtin_ctor = GetRootConstructor(*receiver_map, native_context);
152   if (builtin_ctor != NULL) {
153     *flag = kCacheOnPrototypeReceiverIsPrimitive;
154     return handle(HeapObject::cast(builtin_ctor->instance_prototype())->map());
155   }
156   *flag = receiver_map->is_dictionary_map()
157               ? kCacheOnPrototypeReceiverIsDictionary
158               : kCacheOnPrototype;
159   // Callers must ensure that the prototype is non-null.
160   return handle(JSObject::cast(receiver_map->prototype())->map());
161 }
162
163
164 Handle<Map> IC::GetICCacheHolder(Handle<Map> map, Isolate* isolate,
165                                  CacheHolderFlag* flag) {
166   Context* native_context = *isolate->native_context();
167   JSFunction* builtin_ctor = GetRootConstructor(*map, native_context);
168   if (builtin_ctor != NULL) {
169     *flag = kCacheOnPrototype;
170     return handle(builtin_ctor->initial_map());
171   }
172   *flag = kCacheOnReceiver;
173   return map;
174 }
175
176
177 Code* IC::get_host() {
178   return isolate()
179       ->inner_pointer_to_code_cache()
180       ->GetCacheEntry(address())
181       ->code;
182 }
183
184
185 bool IC::AddressIsDeoptimizedCode() const {
186   return AddressIsDeoptimizedCode(isolate(), address());
187 }
188
189
190 bool IC::AddressIsDeoptimizedCode(Isolate* isolate, Address address) {
191   Code* host =
192       isolate->inner_pointer_to_code_cache()->GetCacheEntry(address)->code;
193   return (host->kind() == Code::OPTIMIZED_FUNCTION &&
194           host->marked_for_deoptimization());
195 }
196 }  // namespace internal
197 }  // namespace v8
198
199 #endif  // V8_IC_INL_H_