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.
10 #include "src/compiler.h"
11 #include "src/debug.h"
12 #include "src/macro-assembler.h"
13 #include "src/prototype.h"
19 Address IC::address() const {
20 // Get the address of the call.
21 Address result = Assembler::target_address_from_return_address(pc());
23 Debug* debug = isolate()->debug();
24 // First check if any break points are active if not just return the address
26 if (!debug->has_break_points()) return result;
28 // At least one break point is active perform additional test to ensure that
29 // break point locations are updated correctly.
30 if (debug->IsDebugBreak(Assembler::target_address_at(result,
31 raw_constant_pool()))) {
32 // If the call site is a call to debug break then return the address in
33 // the original code instead of the address in the running code. This will
34 // cause the original code to be updated and keeps the breakpoint active in
36 Code* code = GetCode();
37 Code* original_code = GetOriginalCode();
39 original_code->instruction_start() - code->instruction_start();
40 // Return the address in the original code. This is the place where
41 // the call which has been overwritten by the DebugBreakXXX resides
42 // and the place where the inline cache system should look.
43 return result + delta;
45 // No break point here just return the address of the call.
51 ConstantPoolArray* IC::constant_pool() const {
52 if (!FLAG_enable_ool_constant_pool) {
55 Handle<ConstantPoolArray> result = raw_constant_pool_;
56 Debug* debug = isolate()->debug();
57 // First check if any break points are active if not just return the
58 // original constant pool.
59 if (!debug->has_break_points()) return *result;
61 // At least one break point is active perform additional test to ensure that
62 // break point locations are updated correctly.
63 Address target = Assembler::target_address_from_return_address(pc());
64 if (debug->IsDebugBreak(
65 Assembler::target_address_at(target, raw_constant_pool()))) {
66 // If the call site is a call to debug break then we want to return the
67 // constant pool for the original code instead of the breakpointed code.
68 return GetOriginalCode()->constant_pool();
75 ConstantPoolArray* IC::raw_constant_pool() const {
76 if (FLAG_enable_ool_constant_pool) {
77 return *raw_constant_pool_;
84 Code* IC::GetTargetAtAddress(Address address,
85 ConstantPoolArray* constant_pool) {
86 // Get the target address of the IC.
87 Address target = Assembler::target_address_at(address, constant_pool);
88 // Convert target address to the code object. Code::GetCodeFromTargetAddress
89 // is safe for use during GC where the map might be marked.
90 Code* result = Code::GetCodeFromTargetAddress(target);
91 DCHECK(result->is_inline_cache_stub());
96 void IC::SetTargetAtAddress(Address address,
98 ConstantPoolArray* constant_pool) {
99 DCHECK(target->is_inline_cache_stub() || target->is_compare_ic_stub());
100 Heap* heap = target->GetHeap();
101 Code* old_target = GetTargetAtAddress(address, constant_pool);
103 // STORE_IC and KEYED_STORE_IC use Code::extra_ic_state() to mark
104 // ICs as strict mode. The strict-ness of the IC must be preserved.
105 if (old_target->kind() == Code::STORE_IC ||
106 old_target->kind() == Code::KEYED_STORE_IC) {
107 DCHECK(StoreIC::GetStrictMode(old_target->extra_ic_state()) ==
108 StoreIC::GetStrictMode(target->extra_ic_state()));
111 Assembler::set_target_address_at(
112 address, constant_pool, target->instruction_start());
113 if (heap->gc_state() == Heap::MARK_COMPACT) {
114 heap->mark_compact_collector()->RecordCodeTargetPatch(address, target);
116 heap->incremental_marking()->RecordCodeTargetPatch(address, target);
118 PostPatching(address, target, old_target);
122 template <class TypeClass>
123 JSFunction* IC::GetRootConstructor(TypeClass* type, Context* native_context) {
124 if (type->Is(TypeClass::Boolean())) {
125 return native_context->boolean_function();
126 } else if (type->Is(TypeClass::Number())) {
127 return native_context->number_function();
128 } else if (type->Is(TypeClass::String())) {
129 return native_context->string_function();
130 } else if (type->Is(TypeClass::Symbol())) {
131 return native_context->symbol_function();
138 Handle<Map> IC::GetHandlerCacheHolder(HeapType* type, bool receiver_is_holder,
139 Isolate* isolate, CacheHolderFlag* flag) {
140 Handle<Map> receiver_map = TypeToMap(type, isolate);
141 if (receiver_is_holder) {
142 *flag = kCacheOnReceiver;
145 Context* native_context = *isolate->native_context();
146 JSFunction* builtin_ctor = GetRootConstructor(type, native_context);
147 if (builtin_ctor != NULL) {
148 *flag = kCacheOnPrototypeReceiverIsPrimitive;
149 return handle(HeapObject::cast(builtin_ctor->instance_prototype())->map());
151 *flag = receiver_map->is_dictionary_map()
152 ? kCacheOnPrototypeReceiverIsDictionary
154 // Callers must ensure that the prototype is non-null.
155 return handle(JSObject::cast(receiver_map->prototype())->map());
159 Handle<Map> IC::GetICCacheHolder(HeapType* type, Isolate* isolate,
160 CacheHolderFlag* flag) {
161 Context* native_context = *isolate->native_context();
162 JSFunction* builtin_ctor = GetRootConstructor(type, native_context);
163 if (builtin_ctor != NULL) {
164 *flag = kCacheOnPrototype;
165 return handle(builtin_ctor->initial_map());
167 *flag = kCacheOnReceiver;
168 return TypeToMap(type, isolate);
172 IC::State CallIC::FeedbackToState(Handle<FixedArray> vector,
173 Handle<Smi> slot) const {
174 IC::State state = UNINITIALIZED;
175 Object* feedback = vector->get(slot->value());
177 if (feedback == *TypeFeedbackInfo::MegamorphicSentinel(isolate())) {
179 } else if (feedback->IsAllocationSite() || feedback->IsJSFunction()) {
182 CHECK(feedback == *TypeFeedbackInfo::UninitializedSentinel(isolate()));
187 } } // namespace v8::internal
189 #endif // V8_IC_INL_H_