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/debug/debug.h"
11 #include "src/macro-assembler.h"
12 #include "src/prototype.h"
13 
14 namespace v8 {
15 namespace internal {
16 
17 
address()18 Address IC::address() const {
19   // Get the address of the call.
20   return Assembler::target_address_from_return_address(pc());
21 }
22 
23 
constant_pool()24 Address IC::constant_pool() const {
25   if (FLAG_enable_embedded_constant_pool) {
26     return raw_constant_pool();
27   } else {
28     return NULL;
29   }
30 }
31 
32 
raw_constant_pool()33 Address IC::raw_constant_pool() const {
34   if (FLAG_enable_embedded_constant_pool) {
35     return *constant_pool_address_;
36   } else {
37     return NULL;
38   }
39 }
40 
41 
GetTargetAtAddress(Address address,Address constant_pool)42 Code* IC::GetTargetAtAddress(Address address, Address constant_pool) {
43   // Get the target address of the IC.
44   Address target = Assembler::target_address_at(address, constant_pool);
45   // Convert target address to the code object. Code::GetCodeFromTargetAddress
46   // is safe for use during GC where the map might be marked.
47   Code* result = Code::GetCodeFromTargetAddress(target);
48   DCHECK(result->is_inline_cache_stub());
49   return result;
50 }
51 
52 
SetTargetAtAddress(Address address,Code * target,Address constant_pool)53 void IC::SetTargetAtAddress(Address address, Code* target,
54                             Address constant_pool) {
55   if (AddressIsDeoptimizedCode(target->GetIsolate(), address)) return;
56 
57   DCHECK(target->is_inline_cache_stub() || target->is_compare_ic_stub());
58 
59   DCHECK(!target->is_inline_cache_stub() ||
60          (target->kind() != Code::LOAD_IC &&
61           target->kind() != Code::KEYED_LOAD_IC &&
62           target->kind() != Code::CALL_IC && target->kind() != Code::STORE_IC &&
63           target->kind() != Code::KEYED_STORE_IC));
64 
65   Heap* heap = target->GetHeap();
66   Code* old_target = GetTargetAtAddress(address, constant_pool);
67 #ifdef DEBUG
68   // STORE_IC and KEYED_STORE_IC use Code::extra_ic_state() to mark
69   // ICs as language mode. The language mode of the IC must be preserved.
70   if (old_target->kind() == Code::STORE_IC ||
71       old_target->kind() == Code::KEYED_STORE_IC) {
72     DCHECK(StoreICState::GetLanguageMode(old_target->extra_ic_state()) ==
73            StoreICState::GetLanguageMode(target->extra_ic_state()));
74   }
75 #endif
76   Assembler::set_target_address_at(heap->isolate(), address, constant_pool,
77                                    target->instruction_start());
78   if (heap->gc_state() == Heap::MARK_COMPACT) {
79     heap->mark_compact_collector()->RecordCodeTargetPatch(address, target);
80   } else {
81     heap->incremental_marking()->RecordCodeTargetPatch(address, target);
82   }
83   PostPatching(address, target, old_target);
84 }
85 
86 
set_target(Code * code)87 void IC::set_target(Code* code) {
88   SetTargetAtAddress(address(), code, constant_pool());
89 }
90 
target()91 Code* IC::target() const {
92   return GetTargetAtAddress(address(), constant_pool());
93 }
94 
IsHandler(Object * object)95 bool IC::IsHandler(Object* object) {
96   return (object->IsSmi() && (object != nullptr)) || object->IsTuple3() ||
97          object->IsFixedArray() ||
98          (object->IsCode() && Code::cast(object)->is_handler());
99 }
100 
GetHandlerCacheHolder(Handle<Map> receiver_map,bool receiver_is_holder,Isolate * isolate,CacheHolderFlag * flag)101 Handle<Map> IC::GetHandlerCacheHolder(Handle<Map> receiver_map,
102                                       bool receiver_is_holder, Isolate* isolate,
103                                       CacheHolderFlag* flag) {
104   if (receiver_is_holder) {
105     *flag = kCacheOnReceiver;
106     return receiver_map;
107   }
108   Handle<JSFunction> builtin_ctor;
109   if (Map::GetConstructorFunction(receiver_map, isolate->native_context())
110           .ToHandle(&builtin_ctor)) {
111     *flag = kCacheOnPrototypeReceiverIsPrimitive;
112     return handle(HeapObject::cast(builtin_ctor->instance_prototype())->map());
113   }
114   *flag = receiver_map->is_dictionary_map()
115               ? kCacheOnPrototypeReceiverIsDictionary
116               : kCacheOnPrototype;
117   // Callers must ensure that the prototype is non-null.
118   return handle(JSObject::cast(receiver_map->prototype())->map());
119 }
120 
121 
GetICCacheHolder(Handle<Map> map,Isolate * isolate,CacheHolderFlag * flag)122 Handle<Map> IC::GetICCacheHolder(Handle<Map> map, Isolate* isolate,
123                                  CacheHolderFlag* flag) {
124   Handle<JSFunction> builtin_ctor;
125   if (Map::GetConstructorFunction(map, isolate->native_context())
126           .ToHandle(&builtin_ctor)) {
127     *flag = kCacheOnPrototype;
128     return handle(builtin_ctor->initial_map());
129   }
130   *flag = kCacheOnReceiver;
131   return map;
132 }
133 
134 
get_host()135 Code* IC::get_host() {
136   return isolate()
137       ->inner_pointer_to_code_cache()
138       ->GetCacheEntry(address())
139       ->code;
140 }
141 
142 
AddressIsDeoptimizedCode()143 bool IC::AddressIsDeoptimizedCode() const {
144   return AddressIsDeoptimizedCode(isolate(), address());
145 }
146 
147 
AddressIsDeoptimizedCode(Isolate * isolate,Address address)148 bool IC::AddressIsDeoptimizedCode(Isolate* isolate, Address address) {
149   Code* host =
150       isolate->inner_pointer_to_code_cache()->GetCacheEntry(address)->code;
151   return (host->kind() == Code::OPTIMIZED_FUNCTION &&
152           host->marked_for_deoptimization());
153 }
154 }  // namespace internal
155 }  // namespace v8
156 
157 #endif  // V8_IC_INL_H_
158