Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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 | #include "v8.h" |
| 29 | |
| 30 | #include "accessors.h" |
| 31 | #include "api.h" |
| 32 | #include "arguments.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 33 | #include "codegen.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 34 | #include "execution.h" |
| 35 | #include "ic-inl.h" |
| 36 | #include "runtime.h" |
| 37 | #include "stub-cache.h" |
| 38 | |
| 39 | namespace v8 { |
| 40 | namespace internal { |
| 41 | |
| 42 | #ifdef DEBUG |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 43 | char IC::TransitionMarkFromState(IC::State state) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 44 | switch (state) { |
| 45 | case UNINITIALIZED: return '0'; |
| 46 | case PREMONOMORPHIC: return 'P'; |
| 47 | case MONOMORPHIC: return '1'; |
| 48 | case MONOMORPHIC_PROTOTYPE_FAILURE: return '^'; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 49 | case MEGAMORPHIC: return IsGeneric() ? 'G' : 'N'; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 50 | |
| 51 | // We never see the debugger states here, because the state is |
| 52 | // computed from the original code - not the patched code. Let |
| 53 | // these cases fall through to the unreachable code below. |
| 54 | case DEBUG_BREAK: break; |
| 55 | case DEBUG_PREPARE_STEP_IN: break; |
| 56 | } |
| 57 | UNREACHABLE(); |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | void IC::TraceIC(const char* type, |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 62 | Handle<Object> name, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 63 | State old_state, |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 64 | Code* new_target) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 65 | if (FLAG_trace_ic) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 66 | State new_state = StateFrom(new_target, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 67 | HEAP->undefined_value(), |
| 68 | HEAP->undefined_value()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 69 | PrintF("[%s in ", type); |
| 70 | StackFrameIterator it; |
| 71 | while (it.frame()->fp() != this->fp()) it.Advance(); |
| 72 | StackFrame* raw_frame = it.frame(); |
| 73 | if (raw_frame->is_internal()) { |
| 74 | Isolate* isolate = new_target->GetIsolate(); |
| 75 | Code* apply_builtin = isolate->builtins()->builtin( |
| 76 | Builtins::kFunctionApply); |
| 77 | if (raw_frame->unchecked_code() == apply_builtin) { |
| 78 | PrintF("apply from "); |
| 79 | it.Advance(); |
| 80 | raw_frame = it.frame(); |
| 81 | } |
| 82 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 83 | JavaScriptFrame::PrintTop(stdout, false, true); |
| 84 | bool new_can_grow = |
| 85 | Code::GetKeyedAccessGrowMode(new_target->extra_ic_state()) == |
| 86 | ALLOW_JSARRAY_GROWTH; |
| 87 | PrintF(" (%c->%c%s)", |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 88 | TransitionMarkFromState(old_state), |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 89 | TransitionMarkFromState(new_state), |
| 90 | new_can_grow ? ".GROW" : ""); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 91 | name->Print(); |
| 92 | PrintF("]\n"); |
| 93 | } |
| 94 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 95 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 96 | #define TRACE_GENERIC_IC(type, reason) \ |
| 97 | do { \ |
| 98 | if (FLAG_trace_ic) { \ |
| 99 | PrintF("[%s patching generic stub in ", type); \ |
| 100 | JavaScriptFrame::PrintTop(stdout, false, true); \ |
| 101 | PrintF(" (%s)]\n", reason); \ |
| 102 | } \ |
| 103 | } while (false) |
| 104 | |
| 105 | #else |
| 106 | #define TRACE_GENERIC_IC(type, reason) |
| 107 | #endif // DEBUG |
| 108 | |
| 109 | #define TRACE_IC(type, name, old_state, new_target) \ |
| 110 | ASSERT((TraceIC(type, name, old_state, new_target), true)) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 111 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 112 | IC::IC(FrameDepth depth, Isolate* isolate) : isolate_(isolate) { |
| 113 | ASSERT(isolate == Isolate::Current()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 114 | // To improve the performance of the (much used) IC code, we unfold |
| 115 | // a few levels of the stack frame iteration code. This yields a |
| 116 | // ~35% speedup when running DeltaBlue with the '--nouse-ic' flag. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 117 | const Address entry = |
| 118 | Isolate::c_entry_fp(isolate->thread_local_top()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 119 | Address* pc_address = |
| 120 | reinterpret_cast<Address*>(entry + ExitFrameConstants::kCallerPCOffset); |
| 121 | Address fp = Memory::Address_at(entry + ExitFrameConstants::kCallerFPOffset); |
| 122 | // If there's another JavaScript frame on the stack, we need to look |
| 123 | // one frame further down the stack to find the frame pointer and |
| 124 | // the return address stack slot. |
| 125 | if (depth == EXTRA_CALL_FRAME) { |
| 126 | const int kCallerPCOffset = StandardFrameConstants::kCallerPCOffset; |
| 127 | pc_address = reinterpret_cast<Address*>(fp + kCallerPCOffset); |
| 128 | fp = Memory::Address_at(fp + StandardFrameConstants::kCallerFPOffset); |
| 129 | } |
| 130 | #ifdef DEBUG |
| 131 | StackFrameIterator it; |
| 132 | for (int i = 0; i < depth + 1; i++) it.Advance(); |
| 133 | StackFrame* frame = it.frame(); |
| 134 | ASSERT(fp == frame->fp() && pc_address == frame->pc_address()); |
| 135 | #endif |
| 136 | fp_ = fp; |
| 137 | pc_address_ = pc_address; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | #ifdef ENABLE_DEBUGGER_SUPPORT |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 142 | Address IC::OriginalCodeAddress() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 143 | HandleScope scope; |
| 144 | // Compute the JavaScript frame for the frame pointer of this IC |
| 145 | // structure. We need this to be able to find the function |
| 146 | // corresponding to the frame. |
| 147 | StackFrameIterator it; |
| 148 | while (it.frame()->fp() != this->fp()) it.Advance(); |
| 149 | JavaScriptFrame* frame = JavaScriptFrame::cast(it.frame()); |
| 150 | // Find the function on the stack and both the active code for the |
| 151 | // function and the original code. |
| 152 | JSFunction* function = JSFunction::cast(frame->function()); |
| 153 | Handle<SharedFunctionInfo> shared(function->shared()); |
| 154 | Code* code = shared->code(); |
| 155 | ASSERT(Debug::HasDebugInfo(shared)); |
| 156 | Code* original_code = Debug::GetDebugInfo(shared)->original_code(); |
| 157 | ASSERT(original_code->IsCode()); |
| 158 | // Get the address of the call site in the active code. This is the |
| 159 | // place where the call to DebugBreakXXX is and where the IC |
| 160 | // normally would be. |
| 161 | Address addr = pc() - Assembler::kCallTargetAddressOffset; |
| 162 | // Return the address in the original code. This is the place where |
| 163 | // the call which has been overwritten by the DebugBreakXXX resides |
| 164 | // and the place where the inline cache system should look. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 165 | intptr_t delta = |
| 166 | original_code->instruction_start() - code->instruction_start(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 167 | return addr + delta; |
| 168 | } |
| 169 | #endif |
| 170 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 171 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 172 | static bool HasNormalObjectsInPrototypeChain(Isolate* isolate, |
| 173 | LookupResult* lookup, |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 174 | Object* receiver) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 175 | Object* end = lookup->IsProperty() |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 176 | ? lookup->holder() : Object::cast(isolate->heap()->null_value()); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 177 | for (Object* current = receiver; |
| 178 | current != end; |
| 179 | current = current->GetPrototype()) { |
| 180 | if (current->IsJSObject() && |
| 181 | !JSObject::cast(current)->HasFastProperties() && |
| 182 | !current->IsJSGlobalProxy() && |
| 183 | !current->IsJSGlobalObject()) { |
| 184 | return true; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 192 | static bool TryRemoveInvalidPrototypeDependentStub(Code* target, |
| 193 | Object* receiver, |
| 194 | Object* name) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 195 | InlineCacheHolderFlag cache_holder = |
| 196 | Code::ExtractCacheHolderFromFlags(target->flags()); |
| 197 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 198 | if (cache_holder == OWN_MAP && !receiver->IsJSObject()) { |
| 199 | // The stub was generated for JSObject but called for non-JSObject. |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 200 | // IC::GetCodeCacheHolder is not applicable. |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 201 | return false; |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 202 | } else if (cache_holder == PROTOTYPE_MAP && |
| 203 | receiver->GetPrototype()->IsNull()) { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 204 | // IC::GetCodeCacheHolder is not applicable. |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 205 | return false; |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 206 | } |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 207 | Map* map = IC::GetCodeCacheHolder(receiver, cache_holder)->map(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 208 | |
| 209 | // Decide whether the inline cache failed because of changes to the |
| 210 | // receiver itself or changes to one of its prototypes. |
| 211 | // |
| 212 | // If there are changes to the receiver itself, the map of the |
| 213 | // receiver will have changed and the current target will not be in |
| 214 | // the receiver map's code cache. Therefore, if the current target |
| 215 | // is in the receiver map's code cache, the inline cache failed due |
| 216 | // to prototype check failure. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 217 | int index = map->IndexInCodeCache(name, target); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 218 | if (index >= 0) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 219 | map->RemoveFromCodeCache(String::cast(name), target, index); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 220 | return true; |
| 221 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 222 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 223 | return false; |
| 224 | } |
| 225 | |
| 226 | |
| 227 | IC::State IC::StateFrom(Code* target, Object* receiver, Object* name) { |
| 228 | IC::State state = target->ic_state(); |
| 229 | |
| 230 | if (state != MONOMORPHIC || !name->IsString()) return state; |
| 231 | if (receiver->IsUndefined() || receiver->IsNull()) return state; |
| 232 | |
| 233 | // For keyed load/store/call, the most likely cause of cache failure is |
| 234 | // that the key has changed. We do not distinguish between |
| 235 | // prototype and non-prototype failures for keyed access. |
| 236 | Code::Kind kind = target->kind(); |
| 237 | if (kind == Code::KEYED_LOAD_IC || |
| 238 | kind == Code::KEYED_STORE_IC || |
| 239 | kind == Code::KEYED_CALL_IC) { |
| 240 | return MONOMORPHIC; |
| 241 | } |
| 242 | |
| 243 | // Remove the target from the code cache if it became invalid |
| 244 | // because of changes in the prototype chain to avoid hitting it |
| 245 | // again. |
| 246 | // Call stubs handle this later to allow extra IC state |
| 247 | // transitions. |
| 248 | if (kind != Code::CALL_IC && |
| 249 | TryRemoveInvalidPrototypeDependentStub(target, receiver, name)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 250 | return MONOMORPHIC_PROTOTYPE_FAILURE; |
| 251 | } |
| 252 | |
| 253 | // The builtins object is special. It only changes when JavaScript |
| 254 | // builtins are loaded lazily. It is important to keep inline |
| 255 | // caches for the builtins object monomorphic. Therefore, if we get |
| 256 | // an inline cache miss for the builtins object after lazily loading |
| 257 | // JavaScript builtins, we return uninitialized as the state to |
| 258 | // force the inline cache back to monomorphic state. |
| 259 | if (receiver->IsJSBuiltinsObject()) { |
| 260 | return UNINITIALIZED; |
| 261 | } |
| 262 | |
| 263 | return MONOMORPHIC; |
| 264 | } |
| 265 | |
| 266 | |
| 267 | RelocInfo::Mode IC::ComputeMode() { |
| 268 | Address addr = address(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 269 | Code* code = Code::cast(isolate()->heap()->FindCodeObject(addr)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 270 | for (RelocIterator it(code, RelocInfo::kCodeTargetMask); |
| 271 | !it.done(); it.next()) { |
| 272 | RelocInfo* info = it.rinfo(); |
| 273 | if (info->pc() == addr) return info->rmode(); |
| 274 | } |
| 275 | UNREACHABLE(); |
| 276 | return RelocInfo::NONE; |
| 277 | } |
| 278 | |
| 279 | |
| 280 | Failure* IC::TypeError(const char* type, |
| 281 | Handle<Object> object, |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 282 | Handle<Object> key) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 283 | HandleScope scope(isolate()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 284 | Handle<Object> args[2] = { key, object }; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 285 | Handle<Object> error = isolate()->factory()->NewTypeError( |
| 286 | type, HandleVector(args, 2)); |
| 287 | return isolate()->Throw(*error); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | |
| 291 | Failure* IC::ReferenceError(const char* type, Handle<String> name) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 292 | HandleScope scope(isolate()); |
| 293 | Handle<Object> error = isolate()->factory()->NewReferenceError( |
| 294 | type, HandleVector(&name, 1)); |
| 295 | return isolate()->Throw(*error); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 299 | static int ComputeTypeInfoCountDelta(IC::State old_state, IC::State new_state) { |
| 300 | bool was_uninitialized = |
| 301 | old_state == UNINITIALIZED || old_state == PREMONOMORPHIC; |
| 302 | bool is_uninitialized = |
| 303 | new_state == UNINITIALIZED || new_state == PREMONOMORPHIC; |
| 304 | return (was_uninitialized && !is_uninitialized) ? 1 : |
| 305 | (!was_uninitialized && is_uninitialized) ? -1 : 0; |
| 306 | } |
| 307 | |
| 308 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 309 | void IC::PostPatching(Address address, Code* target, Code* old_target) { |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 310 | if (FLAG_type_info_threshold == 0 && !FLAG_watch_ic_patching) { |
| 311 | return; |
| 312 | } |
| 313 | Code* host = target->GetHeap()->isolate()-> |
| 314 | inner_pointer_to_code_cache()->GetCacheEntry(address)->code; |
| 315 | if (host->kind() != Code::FUNCTION) return; |
| 316 | |
| 317 | if (FLAG_type_info_threshold > 0 && |
| 318 | old_target->is_inline_cache_stub() && |
| 319 | target->is_inline_cache_stub()) { |
| 320 | int delta = ComputeTypeInfoCountDelta(old_target->ic_state(), |
| 321 | target->ic_state()); |
| 322 | // Not all Code objects have TypeFeedbackInfo. |
| 323 | if (delta != 0 && host->type_feedback_info()->IsTypeFeedbackInfo()) { |
| 324 | TypeFeedbackInfo* info = |
| 325 | TypeFeedbackInfo::cast(host->type_feedback_info()); |
| 326 | info->set_ic_with_type_info_count( |
| 327 | info->ic_with_type_info_count() + delta); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | if (FLAG_watch_ic_patching) { |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 331 | host->set_profiler_ticks(0); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 332 | Isolate::Current()->runtime_profiler()->NotifyICChanged(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 333 | } |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 334 | // TODO(2029): When an optimized function is patched, it would |
| 335 | // be nice to propagate the corresponding type information to its |
| 336 | // unoptimized version for the benefit of later inlining. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 340 | void IC::Clear(Address address) { |
| 341 | Code* target = GetTargetAtAddress(address); |
| 342 | |
| 343 | // Don't clear debug break inline cache as it will remove the break point. |
| 344 | if (target->ic_state() == DEBUG_BREAK) return; |
| 345 | |
| 346 | switch (target->kind()) { |
| 347 | case Code::LOAD_IC: return LoadIC::Clear(address, target); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 348 | case Code::KEYED_LOAD_IC: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 349 | return KeyedLoadIC::Clear(address, target); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 350 | case Code::STORE_IC: return StoreIC::Clear(address, target); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 351 | case Code::KEYED_STORE_IC: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 352 | return KeyedStoreIC::Clear(address, target); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 353 | case Code::CALL_IC: return CallIC::Clear(address, target); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 354 | case Code::KEYED_CALL_IC: return KeyedCallIC::Clear(address, target); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 355 | case Code::UNARY_OP_IC: |
| 356 | case Code::BINARY_OP_IC: |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 357 | case Code::COMPARE_IC: |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 358 | case Code::TO_BOOLEAN_IC: |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 359 | // Clearing these is tricky and does not |
| 360 | // make any performance difference. |
| 361 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 362 | default: UNREACHABLE(); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 367 | void CallICBase::Clear(Address address, Code* target) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 368 | bool contextual = CallICBase::Contextual::decode(target->extra_ic_state()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 369 | State state = target->ic_state(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 370 | if (state == UNINITIALIZED) return; |
| 371 | Code* code = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 372 | Isolate::Current()->stub_cache()->FindCallInitialize( |
| 373 | target->arguments_count(), |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 374 | contextual ? RelocInfo::CODE_TARGET_CONTEXT : RelocInfo::CODE_TARGET, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 375 | target->kind()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 376 | SetTargetAtAddress(address, code); |
| 377 | } |
| 378 | |
| 379 | |
| 380 | void KeyedLoadIC::Clear(Address address, Code* target) { |
| 381 | if (target->ic_state() == UNINITIALIZED) return; |
| 382 | // Make sure to also clear the map used in inline fast cases. If we |
| 383 | // do not clear these maps, cached code can keep objects alive |
| 384 | // through the embedded maps. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 385 | SetTargetAtAddress(address, initialize_stub()); |
| 386 | } |
| 387 | |
| 388 | |
| 389 | void LoadIC::Clear(Address address, Code* target) { |
| 390 | if (target->ic_state() == UNINITIALIZED) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 391 | SetTargetAtAddress(address, initialize_stub()); |
| 392 | } |
| 393 | |
| 394 | |
| 395 | void StoreIC::Clear(Address address, Code* target) { |
| 396 | if (target->ic_state() == UNINITIALIZED) return; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 397 | SetTargetAtAddress(address, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 398 | (Code::GetStrictMode(target->extra_ic_state()) == kStrictMode) |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 399 | ? initialize_stub_strict() |
| 400 | : initialize_stub()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | |
| 404 | void KeyedStoreIC::Clear(Address address, Code* target) { |
| 405 | if (target->ic_state() == UNINITIALIZED) return; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 406 | SetTargetAtAddress(address, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 407 | (Code::GetStrictMode(target->extra_ic_state()) == kStrictMode) |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 408 | ? initialize_stub_strict() |
| 409 | : initialize_stub()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | |
| 413 | static bool HasInterceptorGetter(JSObject* object) { |
| 414 | return !object->GetNamedInterceptor()->getter()->IsUndefined(); |
| 415 | } |
| 416 | |
| 417 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 418 | static void LookupForRead(Handle<Object> object, |
| 419 | Handle<String> name, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 420 | LookupResult* lookup) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 421 | // Skip all the objects with named interceptors, but |
| 422 | // without actual getter. |
| 423 | while (true) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 424 | object->Lookup(*name, lookup); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 425 | // Besides normal conditions (property not found or it's not |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 426 | // an interceptor), bail out if lookup is not cacheable: we won't |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 427 | // be able to IC it anyway and regular lookup should work fine. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 428 | if (!lookup->IsFound() |
| 429 | || (lookup->type() != INTERCEPTOR) |
| 430 | || !lookup->IsCacheable()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 431 | return; |
| 432 | } |
| 433 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 434 | Handle<JSObject> holder(lookup->holder()); |
| 435 | if (HasInterceptorGetter(*holder)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 436 | return; |
| 437 | } |
| 438 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 439 | holder->LocalLookupRealNamedProperty(*name, lookup); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 440 | if (lookup->IsProperty()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 441 | ASSERT(lookup->type() != INTERCEPTOR); |
| 442 | return; |
| 443 | } |
| 444 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 445 | Handle<Object> proto(holder->GetPrototype()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 446 | if (proto->IsNull()) { |
| 447 | lookup->NotFound(); |
| 448 | return; |
| 449 | } |
| 450 | |
| 451 | object = proto; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 456 | Handle<Object> CallICBase::TryCallAsFunction(Handle<Object> object) { |
| 457 | Handle<Object> delegate = Execution::GetFunctionDelegate(object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 458 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 459 | if (delegate->IsJSFunction() && !object->IsJSFunctionProxy()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 460 | // Patch the receiver and use the delegate as the function to |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 461 | // invoke. This is used for invoking objects as if they were functions. |
| 462 | const int argc = target()->arguments_count(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 463 | StackFrameLocator locator; |
| 464 | JavaScriptFrame* frame = locator.FindJavaScriptFrame(0); |
| 465 | int index = frame->ComputeExpressionsCount() - (argc + 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 466 | frame->SetExpression(index, *object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 469 | return delegate; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 472 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 473 | void CallICBase::ReceiverToObjectIfRequired(Handle<Object> callee, |
| 474 | Handle<Object> object) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 475 | while (callee->IsJSFunctionProxy()) { |
| 476 | callee = Handle<Object>(JSFunctionProxy::cast(*callee)->call_trap()); |
| 477 | } |
| 478 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 479 | if (callee->IsJSFunction()) { |
| 480 | Handle<JSFunction> function = Handle<JSFunction>::cast(callee); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 481 | if (!function->shared()->is_classic_mode() || function->IsBuiltin()) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 482 | // Do not wrap receiver for strict mode functions or for builtins. |
| 483 | return; |
| 484 | } |
| 485 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 486 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 487 | // And only wrap string, number or boolean. |
| 488 | if (object->IsString() || object->IsNumber() || object->IsBoolean()) { |
| 489 | // Change the receiver to the result of calling ToObject on it. |
| 490 | const int argc = this->target()->arguments_count(); |
| 491 | StackFrameLocator locator; |
| 492 | JavaScriptFrame* frame = locator.FindJavaScriptFrame(0); |
| 493 | int index = frame->ComputeExpressionsCount() - (argc + 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 494 | frame->SetExpression(index, *isolate()->factory()->ToObject(object)); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 495 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 498 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 499 | MaybeObject* CallICBase::LoadFunction(State state, |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 500 | Code::ExtraICState extra_ic_state, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 501 | Handle<Object> object, |
| 502 | Handle<String> name) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 503 | // If the object is undefined or null it's illegal to try to get any |
| 504 | // of its properties; throw a TypeError in that case. |
| 505 | if (object->IsUndefined() || object->IsNull()) { |
| 506 | return TypeError("non_object_property_call", object, name); |
| 507 | } |
| 508 | |
| 509 | // Check if the name is trivially convertible to an index and get |
| 510 | // the element if so. |
| 511 | uint32_t index; |
| 512 | if (name->AsArrayIndex(&index)) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 513 | Handle<Object> result = Object::GetElement(object, index); |
| 514 | RETURN_IF_EMPTY_HANDLE(isolate(), result); |
| 515 | if (result->IsJSFunction()) return *result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 516 | |
| 517 | // Try to find a suitable function delegate for the object at hand. |
| 518 | result = TryCallAsFunction(result); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 519 | if (result->IsJSFunction()) return *result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 520 | |
| 521 | // Otherwise, it will fail in the lookup step. |
| 522 | } |
| 523 | |
| 524 | // Lookup the property in the object. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 525 | LookupResult lookup(isolate()); |
| 526 | LookupForRead(object, name, &lookup); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 527 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 528 | if (!lookup.IsProperty()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 529 | // If the object does not have the requested property, check which |
| 530 | // exception we need to throw. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 531 | return IsContextual(object) |
| 532 | ? ReferenceError("not_defined", name) |
| 533 | : TypeError("undefined_method", object, name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | // Lookup is valid: Update inline cache and stub cache. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 537 | if (FLAG_use_ic) { |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 538 | UpdateCaches(&lookup, state, extra_ic_state, object, name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | // Get the property. |
| 542 | PropertyAttributes attr; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 543 | Handle<Object> result = |
| 544 | Object::GetProperty(object, object, &lookup, name, &attr); |
| 545 | RETURN_IF_EMPTY_HANDLE(isolate(), result); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 546 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 547 | if (lookup.type() == INTERCEPTOR && attr == ABSENT) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 548 | // If the object does not have the requested property, check which |
| 549 | // exception we need to throw. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 550 | return IsContextual(object) |
| 551 | ? ReferenceError("not_defined", name) |
| 552 | : TypeError("undefined_method", object, name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 555 | ASSERT(!result->IsTheHole()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 556 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 557 | // Make receiver an object if the callee requires it. Strict mode or builtin |
| 558 | // functions do not wrap the receiver, non-strict functions and objects |
| 559 | // called as functions do. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 560 | ReceiverToObjectIfRequired(result, object); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 561 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 562 | if (result->IsJSFunction()) { |
| 563 | Handle<JSFunction> function = Handle<JSFunction>::cast(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 564 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 565 | // Handle stepping into a function if step into is active. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 566 | Debug* debug = isolate()->debug(); |
| 567 | if (debug->StepInActive()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 568 | // Protect the result in a handle as the debugger can allocate and might |
| 569 | // cause GC. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 570 | debug->HandleStepIn(function, object, fp(), false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 571 | } |
| 572 | #endif |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 573 | return *function; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | // Try to find a suitable function delegate for the object at hand. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 577 | result = TryCallAsFunction(result); |
| 578 | if (result->IsJSFunction()) return *result; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 579 | |
| 580 | return TypeError("property_not_function", object, name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 584 | bool CallICBase::TryUpdateExtraICState(LookupResult* lookup, |
| 585 | Handle<Object> object, |
| 586 | Code::ExtraICState* extra_ic_state) { |
| 587 | ASSERT(kind_ == Code::CALL_IC); |
| 588 | if (lookup->type() != CONSTANT_FUNCTION) return false; |
| 589 | JSFunction* function = lookup->GetConstantFunction(); |
| 590 | if (!function->shared()->HasBuiltinFunctionId()) return false; |
| 591 | |
| 592 | // Fetch the arguments passed to the called function. |
| 593 | const int argc = target()->arguments_count(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 594 | Address entry = isolate()->c_entry_fp(isolate()->thread_local_top()); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 595 | Address fp = Memory::Address_at(entry + ExitFrameConstants::kCallerFPOffset); |
| 596 | Arguments args(argc + 1, |
| 597 | &Memory::Object_at(fp + |
| 598 | StandardFrameConstants::kCallerSPOffset + |
| 599 | argc * kPointerSize)); |
| 600 | switch (function->shared()->builtin_function_id()) { |
| 601 | case kStringCharCodeAt: |
| 602 | case kStringCharAt: |
| 603 | if (object->IsString()) { |
| 604 | String* string = String::cast(*object); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 605 | // Check there's the right string value or wrapper in the receiver slot. |
| 606 | ASSERT(string == args[0] || string == JSValue::cast(args[0])->value()); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 607 | // If we're in the default (fastest) state and the index is |
| 608 | // out of bounds, update the state to record this fact. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 609 | if (StringStubState::decode(*extra_ic_state) == DEFAULT_STRING_STUB && |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 610 | argc >= 1 && args[1]->IsNumber()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 611 | double index = DoubleToInteger(args.number_at(1)); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 612 | if (index < 0 || index >= string->length()) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 613 | *extra_ic_state = |
| 614 | StringStubState::update(*extra_ic_state, |
| 615 | STRING_INDEX_OUT_OF_BOUNDS); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 616 | return true; |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | break; |
| 621 | default: |
| 622 | return false; |
| 623 | } |
| 624 | return false; |
| 625 | } |
| 626 | |
| 627 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 628 | Handle<Code> CallICBase::ComputeMonomorphicStub(LookupResult* lookup, |
| 629 | State state, |
| 630 | Code::ExtraICState extra_state, |
| 631 | Handle<Object> object, |
| 632 | Handle<String> name) { |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 633 | int argc = target()->arguments_count(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 634 | Handle<JSObject> holder(lookup->holder()); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 635 | switch (lookup->type()) { |
| 636 | case FIELD: { |
| 637 | int index = lookup->GetFieldIndex(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 638 | return isolate()->stub_cache()->ComputeCallField( |
| 639 | argc, kind_, extra_state, name, object, holder, index); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 640 | } |
| 641 | case CONSTANT_FUNCTION: { |
| 642 | // Get the constant function and compute the code stub for this |
| 643 | // call; used for rewriting to monomorphic state and making sure |
| 644 | // that the code stub is in the stub cache. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 645 | Handle<JSFunction> function(lookup->GetConstantFunction()); |
| 646 | return isolate()->stub_cache()->ComputeCallConstant( |
| 647 | argc, kind_, extra_state, name, object, holder, function); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 648 | } |
| 649 | case NORMAL: { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 650 | // If we return a null handle, the IC will not be patched. |
| 651 | if (!object->IsJSObject()) return Handle<Code>::null(); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 652 | Handle<JSObject> receiver = Handle<JSObject>::cast(object); |
| 653 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 654 | if (holder->IsGlobalObject()) { |
| 655 | Handle<GlobalObject> global = Handle<GlobalObject>::cast(holder); |
| 656 | Handle<JSGlobalPropertyCell> cell(global->GetPropertyCell(lookup)); |
| 657 | if (!cell->value()->IsJSFunction()) return Handle<Code>::null(); |
| 658 | Handle<JSFunction> function(JSFunction::cast(cell->value())); |
| 659 | return isolate()->stub_cache()->ComputeCallGlobal( |
| 660 | argc, kind_, extra_state, name, receiver, global, cell, function); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 661 | } else { |
| 662 | // There is only one shared stub for calling normalized |
| 663 | // properties. It does not traverse the prototype chain, so the |
| 664 | // property must be found in the receiver for the stub to be |
| 665 | // applicable. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 666 | if (!holder.is_identical_to(receiver)) return Handle<Code>::null(); |
| 667 | return isolate()->stub_cache()->ComputeCallNormal( |
| 668 | argc, kind_, extra_state); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 669 | } |
| 670 | break; |
| 671 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 672 | case INTERCEPTOR: |
| 673 | ASSERT(HasInterceptorGetter(*holder)); |
| 674 | return isolate()->stub_cache()->ComputeCallInterceptor( |
| 675 | argc, kind_, extra_state, name, object, holder); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 676 | default: |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 677 | return Handle<Code>::null(); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 678 | } |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 682 | void CallICBase::UpdateCaches(LookupResult* lookup, |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 683 | State state, |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 684 | Code::ExtraICState extra_ic_state, |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 685 | Handle<Object> object, |
| 686 | Handle<String> name) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 687 | // Bail out if we didn't find a result. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 688 | if (!lookup->IsProperty() || !lookup->IsCacheable()) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 689 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 690 | if (lookup->holder() != *object && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 691 | HasNormalObjectsInPrototypeChain( |
| 692 | isolate(), lookup, object->GetPrototype())) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 693 | // Suppress optimization for prototype chains with slow properties objects |
| 694 | // in the middle. |
| 695 | return; |
| 696 | } |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 697 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 698 | // Compute the number of arguments. |
| 699 | int argc = target()->arguments_count(); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 700 | bool had_proto_failure = false; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 701 | Handle<Code> code; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 702 | if (state == UNINITIALIZED) { |
| 703 | // This is the first time we execute this inline cache. |
| 704 | // Set the target to the pre monomorphic stub to delay |
| 705 | // setting the monomorphic state. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 706 | code = isolate()->stub_cache()->ComputeCallPreMonomorphic( |
| 707 | argc, kind_, extra_ic_state); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 708 | } else if (state == MONOMORPHIC) { |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 709 | if (kind_ == Code::CALL_IC && |
| 710 | TryUpdateExtraICState(lookup, object, &extra_ic_state)) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 711 | code = ComputeMonomorphicStub(lookup, state, extra_ic_state, |
| 712 | object, name); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 713 | } else if (kind_ == Code::CALL_IC && |
| 714 | TryRemoveInvalidPrototypeDependentStub(target(), |
| 715 | *object, |
| 716 | *name)) { |
| 717 | had_proto_failure = true; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 718 | code = ComputeMonomorphicStub(lookup, state, extra_ic_state, |
| 719 | object, name); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 720 | } else { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 721 | code = isolate()->stub_cache()->ComputeCallMegamorphic( |
| 722 | argc, kind_, extra_ic_state); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 723 | } |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 724 | } else { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 725 | code = ComputeMonomorphicStub(lookup, state, extra_ic_state, |
| 726 | object, name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 729 | // If there's no appropriate stub we simply avoid updating the caches. |
| 730 | if (code.is_null()) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 731 | |
| 732 | // Patch the call site depending on the state of the cache. |
| 733 | if (state == UNINITIALIZED || |
| 734 | state == PREMONOMORPHIC || |
| 735 | state == MONOMORPHIC || |
| 736 | state == MONOMORPHIC_PROTOTYPE_FAILURE) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 737 | set_target(*code); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 738 | } else if (state == MEGAMORPHIC) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 739 | // Cache code holding map should be consistent with |
| 740 | // GenerateMonomorphicCacheProbe. It is not the map which holds the stub. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 741 | Handle<JSObject> cache_object = object->IsJSObject() |
| 742 | ? Handle<JSObject>::cast(object) |
| 743 | : Handle<JSObject>(JSObject::cast(object->GetPrototype())); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 744 | // Update the stub cache. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 745 | isolate()->stub_cache()->Set(*name, cache_object->map(), *code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 748 | if (had_proto_failure) state = MONOMORPHIC_PROTOTYPE_FAILURE; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 749 | TRACE_IC(kind_ == Code::CALL_IC ? "CallIC" : "KeyedCallIC", |
| 750 | name, state, target()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 754 | MaybeObject* KeyedCallIC::LoadFunction(State state, |
| 755 | Handle<Object> object, |
| 756 | Handle<Object> key) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 757 | if (key->IsSymbol()) { |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 758 | return CallICBase::LoadFunction(state, |
| 759 | Code::kNoExtraICState, |
| 760 | object, |
| 761 | Handle<String>::cast(key)); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | if (object->IsUndefined() || object->IsNull()) { |
| 765 | return TypeError("non_object_property_call", object, key); |
| 766 | } |
| 767 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 768 | if (FLAG_use_ic && state != MEGAMORPHIC && object->IsHeapObject()) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 769 | int argc = target()->arguments_count(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 770 | Handle<Map> map = |
| 771 | isolate()->factory()->non_strict_arguments_elements_map(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 772 | if (object->IsJSObject() && |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 773 | Handle<JSObject>::cast(object)->elements()->map() == *map) { |
| 774 | Handle<Code> code = isolate()->stub_cache()->ComputeCallArguments( |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 775 | argc, Code::KEYED_CALL_IC); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 776 | set_target(*code); |
| 777 | TRACE_IC("KeyedCallIC", key, state, target()); |
| 778 | } else if (!object->IsAccessCheckNeeded()) { |
| 779 | Handle<Code> code = isolate()->stub_cache()->ComputeCallMegamorphic( |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 780 | argc, Code::KEYED_CALL_IC, Code::kNoExtraICState); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 781 | set_target(*code); |
| 782 | TRACE_IC("KeyedCallIC", key, state, target()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 783 | } |
| 784 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 785 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 786 | Handle<Object> result = GetProperty(object, key); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 787 | RETURN_IF_EMPTY_HANDLE(isolate(), result); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 788 | |
| 789 | // Make receiver an object if the callee requires it. Strict mode or builtin |
| 790 | // functions do not wrap the receiver, non-strict functions and objects |
| 791 | // called as functions do. |
| 792 | ReceiverToObjectIfRequired(result, object); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 793 | if (result->IsJSFunction()) return *result; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 794 | |
| 795 | result = TryCallAsFunction(result); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 796 | if (result->IsJSFunction()) return *result; |
| 797 | |
| 798 | return TypeError("property_not_function", object, key); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 802 | MaybeObject* LoadIC::Load(State state, |
| 803 | Handle<Object> object, |
| 804 | Handle<String> name) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 805 | // If the object is undefined or null it's illegal to try to get any |
| 806 | // of its properties; throw a TypeError in that case. |
| 807 | if (object->IsUndefined() || object->IsNull()) { |
| 808 | return TypeError("non_object_property_load", object, name); |
| 809 | } |
| 810 | |
| 811 | if (FLAG_use_ic) { |
| 812 | // Use specialized code for getting the length of strings and |
| 813 | // string wrapper objects. The length property of string wrapper |
| 814 | // objects is read-only and therefore always returns the length of |
| 815 | // the underlying string value. See ECMA-262 15.5.5.1. |
| 816 | if ((object->IsString() || object->IsStringWrapper()) && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 817 | name->Equals(isolate()->heap()->length_symbol())) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 818 | Handle<Code> stub; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 819 | if (state == UNINITIALIZED) { |
| 820 | stub = pre_monomorphic_stub(); |
| 821 | } else if (state == PREMONOMORPHIC) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 822 | stub = object->IsString() |
| 823 | ? isolate()->builtins()->LoadIC_StringLength() |
| 824 | : isolate()->builtins()->LoadIC_StringWrapperLength(); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 825 | } else if (state == MONOMORPHIC && object->IsStringWrapper()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 826 | stub = isolate()->builtins()->LoadIC_StringWrapperLength(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 827 | } else if (state != MEGAMORPHIC) { |
| 828 | stub = megamorphic_stub(); |
| 829 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 830 | if (!stub.is_null()) { |
| 831 | set_target(*stub); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 832 | #ifdef DEBUG |
| 833 | if (FLAG_trace_ic) PrintF("[LoadIC : +#length /string]\n"); |
| 834 | #endif |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 835 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 836 | // Get the string if we have a string wrapper object. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 837 | Handle<Object> string = object->IsJSValue() |
| 838 | ? Handle<Object>(Handle<JSValue>::cast(object)->value()) |
| 839 | : object; |
| 840 | return Smi::FromInt(String::cast(*string)->length()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | // Use specialized code for getting the length of arrays. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 844 | if (object->IsJSArray() && |
| 845 | name->Equals(isolate()->heap()->length_symbol())) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 846 | Handle<Code> stub; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 847 | if (state == UNINITIALIZED) { |
| 848 | stub = pre_monomorphic_stub(); |
| 849 | } else if (state == PREMONOMORPHIC) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 850 | stub = isolate()->builtins()->LoadIC_ArrayLength(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 851 | } else if (state != MEGAMORPHIC) { |
| 852 | stub = megamorphic_stub(); |
| 853 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 854 | if (!stub.is_null()) { |
| 855 | set_target(*stub); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 856 | #ifdef DEBUG |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 857 | if (FLAG_trace_ic) PrintF("[LoadIC : +#length /array]\n"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 858 | #endif |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 859 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 860 | return JSArray::cast(*object)->length(); |
| 861 | } |
| 862 | |
| 863 | // Use specialized code for getting prototype of functions. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 864 | if (object->IsJSFunction() && |
| 865 | name->Equals(isolate()->heap()->prototype_symbol()) && |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 866 | Handle<JSFunction>::cast(object)->should_have_prototype()) { |
| 867 | Handle<Code> stub; |
| 868 | if (state == UNINITIALIZED) { |
| 869 | stub = pre_monomorphic_stub(); |
| 870 | } else if (state == PREMONOMORPHIC) { |
| 871 | stub = isolate()->builtins()->LoadIC_FunctionPrototype(); |
| 872 | } else if (state != MEGAMORPHIC) { |
| 873 | stub = megamorphic_stub(); |
| 874 | } |
| 875 | if (!stub.is_null()) { |
| 876 | set_target(*stub); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 877 | #ifdef DEBUG |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 878 | if (FLAG_trace_ic) PrintF("[LoadIC : +#prototype /function]\n"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 879 | #endif |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 880 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 881 | return Accessors::FunctionGetPrototype(*object, 0); |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | // Check if the name is trivially convertible to an index and get |
| 886 | // the element if so. |
| 887 | uint32_t index; |
| 888 | if (name->AsArrayIndex(&index)) return object->GetElement(index); |
| 889 | |
| 890 | // Named lookup in the object. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 891 | LookupResult lookup(isolate()); |
| 892 | LookupForRead(object, name, &lookup); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 893 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 894 | // If we did not find a property, check if we need to throw an exception. |
| 895 | if (!lookup.IsProperty()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 896 | if (IsContextual(object)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 897 | return ReferenceError("not_defined", name); |
| 898 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 899 | LOG(isolate(), SuspectReadEvent(*name, *object)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 900 | } |
| 901 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 902 | // Update inline cache and stub cache. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 903 | if (FLAG_use_ic) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 904 | UpdateCaches(&lookup, state, object, name); |
| 905 | } |
| 906 | |
| 907 | PropertyAttributes attr; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 908 | if (lookup.IsFound() && |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 909 | (lookup.type() == INTERCEPTOR || lookup.type() == HANDLER)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 910 | // Get the property. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 911 | Handle<Object> result = |
| 912 | Object::GetProperty(object, object, &lookup, name, &attr); |
| 913 | RETURN_IF_EMPTY_HANDLE(isolate(), result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 914 | // If the property is not present, check if we need to throw an |
| 915 | // exception. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 916 | if (attr == ABSENT && IsContextual(object)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 917 | return ReferenceError("not_defined", name); |
| 918 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 919 | return *result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | // Get the property. |
| 923 | return object->GetProperty(*object, &lookup, *name, &attr); |
| 924 | } |
| 925 | |
| 926 | |
| 927 | void LoadIC::UpdateCaches(LookupResult* lookup, |
| 928 | State state, |
| 929 | Handle<Object> object, |
| 930 | Handle<String> name) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 931 | // Bail out if the result is not cacheable. |
| 932 | if (!lookup->IsCacheable()) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 933 | |
| 934 | // Loading properties from values is not common, so don't try to |
| 935 | // deal with non-JS objects here. |
| 936 | if (!object->IsJSObject()) return; |
| 937 | Handle<JSObject> receiver = Handle<JSObject>::cast(object); |
| 938 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 939 | if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 940 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 941 | // Compute the code stub for this load. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 942 | Handle<Code> code; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 943 | if (state == UNINITIALIZED) { |
| 944 | // This is the first time we execute this inline cache. |
| 945 | // Set the target to the pre monomorphic stub to delay |
| 946 | // setting the monomorphic state. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 947 | code = pre_monomorphic_stub(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 948 | } else if (!lookup->IsProperty()) { |
| 949 | // Nonexistent property. The result is undefined. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 950 | code = isolate()->stub_cache()->ComputeLoadNonexistent(name, receiver); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 951 | } else { |
| 952 | // Compute monomorphic stub. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 953 | Handle<JSObject> holder(lookup->holder()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 954 | switch (lookup->type()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 955 | case FIELD: |
| 956 | code = isolate()->stub_cache()->ComputeLoadField( |
| 957 | name, receiver, holder, lookup->GetFieldIndex()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 958 | break; |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 959 | case CONSTANT_FUNCTION: { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 960 | Handle<JSFunction> constant(lookup->GetConstantFunction()); |
| 961 | code = isolate()->stub_cache()->ComputeLoadConstant( |
| 962 | name, receiver, holder, constant); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 963 | break; |
| 964 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 965 | case NORMAL: |
| 966 | if (holder->IsGlobalObject()) { |
| 967 | Handle<GlobalObject> global = Handle<GlobalObject>::cast(holder); |
| 968 | Handle<JSGlobalPropertyCell> cell(global->GetPropertyCell(lookup)); |
| 969 | code = isolate()->stub_cache()->ComputeLoadGlobal( |
| 970 | name, receiver, global, cell, lookup->IsDontDelete()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 971 | } else { |
| 972 | // There is only one shared stub for loading normalized |
| 973 | // properties. It does not traverse the prototype chain, so the |
| 974 | // property must be found in the receiver for the stub to be |
| 975 | // applicable. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 976 | if (!holder.is_identical_to(receiver)) return; |
| 977 | code = isolate()->stub_cache()->ComputeLoadNormal(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 978 | } |
| 979 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 980 | case CALLBACKS: { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 981 | Handle<Object> callback_object(lookup->GetCallbackObject()); |
| 982 | if (!callback_object->IsAccessorInfo()) return; |
| 983 | Handle<AccessorInfo> callback = |
| 984 | Handle<AccessorInfo>::cast(callback_object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 985 | if (v8::ToCData<Address>(callback->getter()) == 0) return; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 986 | code = isolate()->stub_cache()->ComputeLoadCallback( |
| 987 | name, receiver, holder, callback); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 988 | break; |
| 989 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 990 | case INTERCEPTOR: |
| 991 | ASSERT(HasInterceptorGetter(*holder)); |
| 992 | code = isolate()->stub_cache()->ComputeLoadInterceptor( |
| 993 | name, receiver, holder); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 994 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 995 | default: |
| 996 | return; |
| 997 | } |
| 998 | } |
| 999 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 1000 | // Patch the call site depending on the state of the cache. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1001 | if (state == UNINITIALIZED || |
| 1002 | state == PREMONOMORPHIC || |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 1003 | state == MONOMORPHIC_PROTOTYPE_FAILURE) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1004 | set_target(*code); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 1005 | } else if (state == MONOMORPHIC) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1006 | // We are transitioning from monomorphic to megamorphic case. |
| 1007 | // Place the current monomorphic stub and stub compiled for |
| 1008 | // the receiver into stub cache. |
| 1009 | Map* map = target()->FindFirstMap(); |
| 1010 | if (map != NULL) { |
| 1011 | isolate()->stub_cache()->Set(*name, map, target()); |
| 1012 | } |
| 1013 | isolate()->stub_cache()->Set(*name, receiver->map(), *code); |
| 1014 | |
| 1015 | set_target(*megamorphic_stub()); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 1016 | } else if (state == MEGAMORPHIC) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1017 | // Cache code holding map should be consistent with |
| 1018 | // GenerateMonomorphicCacheProbe. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1019 | isolate()->stub_cache()->Set(*name, receiver->map(), *code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1022 | TRACE_IC("LoadIC", name, state, target()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1026 | Handle<Code> KeyedLoadIC::GetElementStubWithoutMapCheck( |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1027 | bool is_js_array, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1028 | ElementsKind elements_kind, |
| 1029 | KeyedAccessGrowMode grow_mode) { |
| 1030 | ASSERT(grow_mode == DO_NOT_ALLOW_JSARRAY_GROWTH); |
| 1031 | return KeyedLoadElementStub(elements_kind).GetCode(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1035 | Handle<Code> KeyedLoadIC::ComputePolymorphicStub( |
| 1036 | MapHandleList* receiver_maps, |
| 1037 | StrictModeFlag strict_mode, |
| 1038 | KeyedAccessGrowMode growth_mode) { |
| 1039 | CodeHandleList handler_ics(receiver_maps->length()); |
| 1040 | for (int i = 0; i < receiver_maps->length(); ++i) { |
| 1041 | Handle<Map> receiver_map = receiver_maps->at(i); |
| 1042 | Handle<Code> cached_stub = ComputeMonomorphicStubWithoutMapCheck( |
| 1043 | receiver_map, strict_mode, growth_mode); |
| 1044 | handler_ics.Add(cached_stub); |
| 1045 | } |
| 1046 | KeyedLoadStubCompiler compiler(isolate()); |
| 1047 | Handle<Code> code = compiler.CompileLoadPolymorphic( |
| 1048 | receiver_maps, &handler_ics); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1049 | isolate()->counters()->keyed_load_polymorphic_stubs()->Increment(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1050 | PROFILE(isolate(), |
| 1051 | CodeCreateEvent(Logger::KEYED_LOAD_MEGAMORPHIC_IC_TAG, *code, 0)); |
| 1052 | return code; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1056 | MaybeObject* KeyedLoadIC::Load(State state, |
| 1057 | Handle<Object> object, |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1058 | Handle<Object> key, |
| 1059 | bool force_generic_stub) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1060 | // Check for values that can be converted into a symbol. |
| 1061 | // TODO(1295): Remove this code. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1062 | if (key->IsHeapNumber() && |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1063 | isnan(Handle<HeapNumber>::cast(key)->value())) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1064 | key = isolate()->factory()->nan_symbol(); |
| 1065 | } else if (key->IsUndefined()) { |
| 1066 | key = isolate()->factory()->undefined_symbol(); |
| 1067 | } |
| 1068 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1069 | if (key->IsSymbol()) { |
| 1070 | Handle<String> name = Handle<String>::cast(key); |
| 1071 | |
| 1072 | // If the object is undefined or null it's illegal to try to get any |
| 1073 | // of its properties; throw a TypeError in that case. |
| 1074 | if (object->IsUndefined() || object->IsNull()) { |
| 1075 | return TypeError("non_object_property_load", object, name); |
| 1076 | } |
| 1077 | |
| 1078 | if (FLAG_use_ic) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1079 | // TODO(1073): don't ignore the current stub state. |
| 1080 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1081 | // Use specialized code for getting the length of strings. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1082 | if (object->IsString() && |
| 1083 | name->Equals(isolate()->heap()->length_symbol())) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1084 | Handle<String> string = Handle<String>::cast(object); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1085 | Handle<Code> code = |
| 1086 | isolate()->stub_cache()->ComputeKeyedLoadStringLength(name, string); |
| 1087 | ASSERT(!code.is_null()); |
| 1088 | set_target(*code); |
| 1089 | TRACE_IC("KeyedLoadIC", name, state, target()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1090 | return Smi::FromInt(string->length()); |
| 1091 | } |
| 1092 | |
| 1093 | // Use specialized code for getting the length of arrays. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1094 | if (object->IsJSArray() && |
| 1095 | name->Equals(isolate()->heap()->length_symbol())) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1096 | Handle<JSArray> array = Handle<JSArray>::cast(object); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1097 | Handle<Code> code = |
| 1098 | isolate()->stub_cache()->ComputeKeyedLoadArrayLength(name, array); |
| 1099 | ASSERT(!code.is_null()); |
| 1100 | set_target(*code); |
| 1101 | TRACE_IC("KeyedLoadIC", name, state, target()); |
| 1102 | return array->length(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | // Use specialized code for getting prototype of functions. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1106 | if (object->IsJSFunction() && |
| 1107 | name->Equals(isolate()->heap()->prototype_symbol()) && |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1108 | Handle<JSFunction>::cast(object)->should_have_prototype()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1109 | Handle<JSFunction> function = Handle<JSFunction>::cast(object); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1110 | Handle<Code> code = |
| 1111 | isolate()->stub_cache()->ComputeKeyedLoadFunctionPrototype( |
| 1112 | name, function); |
| 1113 | ASSERT(!code.is_null()); |
| 1114 | set_target(*code); |
| 1115 | TRACE_IC("KeyedLoadIC", name, state, target()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1116 | return Accessors::FunctionGetPrototype(*object, 0); |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | // Check if the name is trivially convertible to an index and get |
| 1121 | // the element or char if so. |
| 1122 | uint32_t index = 0; |
| 1123 | if (name->AsArrayIndex(&index)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1124 | // Rewrite to the generic keyed load stub. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1125 | if (FLAG_use_ic) set_target(*generic_stub()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1126 | return Runtime::GetElementOrCharAt(isolate(), object, index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | // Named lookup. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1130 | LookupResult lookup(isolate()); |
| 1131 | LookupForRead(object, name, &lookup); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1132 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1133 | // If we did not find a property, check if we need to throw an exception. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1134 | if (!lookup.IsProperty() && IsContextual(object)) { |
| 1135 | return ReferenceError("not_defined", name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1138 | if (FLAG_use_ic) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1139 | UpdateCaches(&lookup, state, object, name); |
| 1140 | } |
| 1141 | |
| 1142 | PropertyAttributes attr; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1143 | if (lookup.IsFound() && lookup.type() == INTERCEPTOR) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1144 | // Get the property. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1145 | Handle<Object> result = |
| 1146 | Object::GetProperty(object, object, &lookup, name, &attr); |
| 1147 | RETURN_IF_EMPTY_HANDLE(isolate(), result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1148 | // If the property is not present, check if we need to throw an |
| 1149 | // exception. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1150 | if (attr == ABSENT && IsContextual(object)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1151 | return ReferenceError("not_defined", name); |
| 1152 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1153 | return *result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | return object->GetProperty(*object, &lookup, *name, &attr); |
| 1157 | } |
| 1158 | |
| 1159 | // Do not use ICs for objects that require access checks (including |
| 1160 | // the global object). |
| 1161 | bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); |
| 1162 | |
| 1163 | if (use_ic) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1164 | Handle<Code> stub = generic_stub(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1165 | if (!force_generic_stub) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1166 | if (object->IsString() && key->IsNumber()) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1167 | if (state == UNINITIALIZED) { |
| 1168 | stub = string_stub(); |
| 1169 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1170 | } else if (object->IsJSObject()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1171 | Handle<JSObject> receiver = Handle<JSObject>::cast(object); |
| 1172 | if (receiver->elements()->map() == |
| 1173 | isolate()->heap()->non_strict_arguments_elements_map()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1174 | stub = non_strict_arguments_stub(); |
| 1175 | } else if (receiver->HasIndexedInterceptor()) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1176 | stub = indexed_interceptor_stub(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1177 | } else if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { |
| 1178 | stub = ComputeStub(receiver, LOAD, kNonStrictMode, stub); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1179 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1180 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1181 | } else { |
| 1182 | TRACE_GENERIC_IC("KeyedLoadIC", "force generic"); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1183 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1184 | if (!stub.is_null()) set_target(*stub); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1185 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1186 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1187 | TRACE_IC("KeyedLoadIC", key, state, target()); |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 1188 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1189 | // Get the property. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1190 | return Runtime::GetObjectProperty(isolate(), object, key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1194 | void KeyedLoadIC::UpdateCaches(LookupResult* lookup, |
| 1195 | State state, |
| 1196 | Handle<Object> object, |
| 1197 | Handle<String> name) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1198 | // Bail out if we didn't find a result. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1199 | if (!lookup->IsProperty() || !lookup->IsCacheable()) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1200 | |
| 1201 | if (!object->IsJSObject()) return; |
| 1202 | Handle<JSObject> receiver = Handle<JSObject>::cast(object); |
| 1203 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1204 | if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1205 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1206 | // Compute the code stub for this load. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1207 | Handle<Code> code; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1208 | |
| 1209 | if (state == UNINITIALIZED) { |
| 1210 | // This is the first time we execute this inline cache. |
| 1211 | // Set the target to the pre monomorphic stub to delay |
| 1212 | // setting the monomorphic state. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1213 | code = pre_monomorphic_stub(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1214 | } else { |
| 1215 | // Compute a monomorphic stub. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1216 | Handle<JSObject> holder(lookup->holder()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1217 | switch (lookup->type()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1218 | case FIELD: |
| 1219 | code = isolate()->stub_cache()->ComputeKeyedLoadField( |
| 1220 | name, receiver, holder, lookup->GetFieldIndex()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1221 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1222 | case CONSTANT_FUNCTION: { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1223 | Handle<JSFunction> constant(lookup->GetConstantFunction()); |
| 1224 | code = isolate()->stub_cache()->ComputeKeyedLoadConstant( |
| 1225 | name, receiver, holder, constant); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1226 | break; |
| 1227 | } |
| 1228 | case CALLBACKS: { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1229 | Handle<Object> callback_object(lookup->GetCallbackObject()); |
| 1230 | if (!callback_object->IsAccessorInfo()) return; |
| 1231 | Handle<AccessorInfo> callback = |
| 1232 | Handle<AccessorInfo>::cast(callback_object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1233 | if (v8::ToCData<Address>(callback->getter()) == 0) return; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1234 | code = isolate()->stub_cache()->ComputeKeyedLoadCallback( |
| 1235 | name, receiver, holder, callback); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1236 | break; |
| 1237 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1238 | case INTERCEPTOR: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1239 | ASSERT(HasInterceptorGetter(lookup->holder())); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1240 | code = isolate()->stub_cache()->ComputeKeyedLoadInterceptor( |
| 1241 | name, receiver, holder); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1242 | break; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1243 | default: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1244 | // Always rewrite to the generic case so that we do not |
| 1245 | // repeatedly try to rewrite. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1246 | code = generic_stub(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1247 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1248 | } |
| 1249 | } |
| 1250 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1251 | // Patch the call site depending on the state of the cache. Make |
| 1252 | // sure to always rewrite from monomorphic to megamorphic. |
| 1253 | ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); |
| 1254 | if (state == UNINITIALIZED || state == PREMONOMORPHIC) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1255 | set_target(*code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1256 | } else if (state == MONOMORPHIC) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1257 | set_target(*megamorphic_stub()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1260 | TRACE_IC("KeyedLoadIC", name, state, target()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | |
| 1264 | static bool StoreICableLookup(LookupResult* lookup) { |
| 1265 | // Bail out if we didn't find a result. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1266 | if (!lookup->IsFound() || lookup->type() == NULL_DESCRIPTOR) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1267 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1268 | // Bail out if inline caching is not allowed. |
| 1269 | if (!lookup->IsCacheable()) return false; |
| 1270 | |
| 1271 | // If the property is read-only, we leave the IC in its current state. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1272 | if (lookup->IsReadOnly()) return false; |
| 1273 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1274 | return true; |
| 1275 | } |
| 1276 | |
| 1277 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1278 | static bool LookupForWrite(Handle<JSObject> receiver, |
| 1279 | Handle<String> name, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1280 | LookupResult* lookup) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1281 | receiver->LocalLookup(*name, lookup); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1282 | if (!StoreICableLookup(lookup)) { |
| 1283 | return false; |
| 1284 | } |
| 1285 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1286 | if (lookup->type() == INTERCEPTOR && |
| 1287 | receiver->GetNamedInterceptor()->setter()->IsUndefined()) { |
| 1288 | receiver->LocalLookupRealNamedProperty(*name, lookup); |
| 1289 | return StoreICableLookup(lookup); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | return true; |
| 1293 | } |
| 1294 | |
| 1295 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1296 | MaybeObject* StoreIC::Store(State state, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1297 | StrictModeFlag strict_mode, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1298 | Handle<Object> object, |
| 1299 | Handle<String> name, |
| 1300 | Handle<Object> value) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1301 | if (!object->IsJSObject()) { |
| 1302 | // Handle proxies. |
| 1303 | if (object->IsJSProxy()) { |
| 1304 | return JSProxy::cast(*object)-> |
| 1305 | SetProperty(*name, *value, NONE, strict_mode); |
| 1306 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1307 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1308 | // If the object is undefined or null it's illegal to try to set any |
| 1309 | // properties on it; throw a TypeError in that case. |
| 1310 | if (object->IsUndefined() || object->IsNull()) { |
| 1311 | return TypeError("non_object_property_store", object, name); |
| 1312 | } |
| 1313 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1314 | // The length property of string values is read-only. Throw in strict mode. |
| 1315 | if (strict_mode == kStrictMode && object->IsString() && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1316 | name->Equals(isolate()->heap()->length_symbol())) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1317 | return TypeError("strict_read_only_property", object, name); |
| 1318 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1319 | // Ignore other stores where the receiver is not a JSObject. |
| 1320 | // TODO(1475): Must check prototype chains of object wrappers. |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1321 | return *value; |
| 1322 | } |
| 1323 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1324 | Handle<JSObject> receiver = Handle<JSObject>::cast(object); |
| 1325 | |
| 1326 | // Check if the given name is an array index. |
| 1327 | uint32_t index; |
| 1328 | if (name->AsArrayIndex(&index)) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1329 | Handle<Object> result = |
| 1330 | JSObject::SetElement(receiver, index, value, NONE, strict_mode); |
| 1331 | RETURN_IF_EMPTY_HANDLE(isolate(), result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1332 | return *value; |
| 1333 | } |
| 1334 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1335 | // Use specialized code for setting the length of arrays with fast |
| 1336 | // properties. Slow properties might indicate redefinition of the |
| 1337 | // length property. |
| 1338 | if (receiver->IsJSArray() && |
| 1339 | name->Equals(isolate()->heap()->length_symbol()) && |
| 1340 | Handle<JSArray>::cast(receiver)->AllowsSetElementsLength() && |
| 1341 | receiver->HasFastProperties()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1342 | #ifdef DEBUG |
| 1343 | if (FLAG_trace_ic) PrintF("[StoreIC : +#length /array]\n"); |
| 1344 | #endif |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1345 | Handle<Code> stub = (strict_mode == kStrictMode) |
| 1346 | ? isolate()->builtins()->StoreIC_ArrayLength_Strict() |
| 1347 | : isolate()->builtins()->StoreIC_ArrayLength(); |
| 1348 | set_target(*stub); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1349 | return receiver->SetProperty(*name, *value, NONE, strict_mode); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1350 | } |
| 1351 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1352 | // Lookup the property locally in the receiver. |
| 1353 | if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1354 | LookupResult lookup(isolate()); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 1355 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1356 | if (LookupForWrite(receiver, name, &lookup)) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1357 | // Generate a stub for this store. |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1358 | UpdateCaches(&lookup, state, strict_mode, receiver, name, value); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1359 | } else { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1360 | // Strict mode doesn't allow setting non-existent global property |
| 1361 | // or an assignment to a read only property. |
| 1362 | if (strict_mode == kStrictMode) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1363 | if (lookup.IsProperty() && lookup.IsReadOnly()) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1364 | return TypeError("strict_read_only_property", object, name); |
| 1365 | } else if (IsContextual(object)) { |
| 1366 | return ReferenceError("not_defined", name); |
| 1367 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1368 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1369 | } |
| 1370 | } |
| 1371 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1372 | if (receiver->IsJSGlobalProxy()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1373 | // TODO(ulan): find out why we patch this site even with --no-use-ic |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1374 | // Generate a generic stub that goes to the runtime when we see a global |
| 1375 | // proxy as receiver. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1376 | Handle<Code> stub = (strict_mode == kStrictMode) |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1377 | ? global_proxy_stub_strict() |
| 1378 | : global_proxy_stub(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1379 | if (target() != *stub) { |
| 1380 | set_target(*stub); |
| 1381 | TRACE_IC("StoreIC", name, state, target()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1382 | } |
| 1383 | } |
| 1384 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1385 | // Set the property. |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1386 | return receiver->SetProperty(*name, *value, NONE, strict_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | |
| 1390 | void StoreIC::UpdateCaches(LookupResult* lookup, |
| 1391 | State state, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1392 | StrictModeFlag strict_mode, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1393 | Handle<JSObject> receiver, |
| 1394 | Handle<String> name, |
| 1395 | Handle<Object> value) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1396 | ASSERT(!receiver->IsJSGlobalProxy()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1397 | ASSERT(StoreICableLookup(lookup)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1398 | // These are not cacheable, so we never see such LookupResults here. |
| 1399 | ASSERT(lookup->type() != HANDLER); |
| 1400 | // We get only called for properties or transitions, see StoreICableLookup. |
| 1401 | ASSERT(lookup->type() != NULL_DESCRIPTOR); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1402 | |
| 1403 | // If the property has a non-field type allowing map transitions |
| 1404 | // where there is extra room in the object, we leave the IC in its |
| 1405 | // current state. |
| 1406 | PropertyType type = lookup->type(); |
| 1407 | |
| 1408 | // Compute the code stub for this store; used for rewriting to |
| 1409 | // monomorphic state and making sure that the code stub is in the |
| 1410 | // stub cache. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1411 | Handle<Code> code; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1412 | switch (type) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1413 | case FIELD: |
| 1414 | code = isolate()->stub_cache()->ComputeStoreField(name, |
| 1415 | receiver, |
| 1416 | lookup->GetFieldIndex(), |
| 1417 | Handle<Map>::null(), |
| 1418 | strict_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1419 | break; |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 1420 | case MAP_TRANSITION: { |
| 1421 | if (lookup->GetAttributes() != NONE) return; |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 1422 | Handle<Map> transition(lookup->GetTransitionMap()); |
| 1423 | int index = transition->PropertyIndexFor(*name); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1424 | code = isolate()->stub_cache()->ComputeStoreField( |
| 1425 | name, receiver, index, transition, strict_mode); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 1426 | break; |
| 1427 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1428 | case NORMAL: |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1429 | if (receiver->IsGlobalObject()) { |
| 1430 | // The stub generated for the global object picks the value directly |
| 1431 | // from the property cell. So the property must be directly on the |
| 1432 | // global object. |
| 1433 | Handle<GlobalObject> global = Handle<GlobalObject>::cast(receiver); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1434 | Handle<JSGlobalPropertyCell> cell(global->GetPropertyCell(lookup)); |
| 1435 | code = isolate()->stub_cache()->ComputeStoreGlobal( |
| 1436 | name, global, cell, strict_mode); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1437 | } else { |
| 1438 | if (lookup->holder() != *receiver) return; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1439 | code = isolate()->stub_cache()->ComputeStoreNormal(strict_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1440 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1441 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1442 | case CALLBACKS: { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1443 | Handle<Object> callback_object(lookup->GetCallbackObject()); |
| 1444 | if (!callback_object->IsAccessorInfo()) return; |
| 1445 | Handle<AccessorInfo> callback = |
| 1446 | Handle<AccessorInfo>::cast(callback_object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1447 | if (v8::ToCData<Address>(callback->setter()) == 0) return; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1448 | code = isolate()->stub_cache()->ComputeStoreCallback( |
| 1449 | name, receiver, callback, strict_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1450 | break; |
| 1451 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1452 | case INTERCEPTOR: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1453 | ASSERT(!receiver->GetNamedInterceptor()->setter()->IsUndefined()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1454 | code = isolate()->stub_cache()->ComputeStoreInterceptor( |
| 1455 | name, receiver, strict_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1456 | break; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1457 | case CONSTANT_FUNCTION: |
| 1458 | case CONSTANT_TRANSITION: |
| 1459 | case ELEMENTS_TRANSITION: |
| 1460 | return; |
| 1461 | case HANDLER: |
| 1462 | case NULL_DESCRIPTOR: |
| 1463 | UNREACHABLE(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1464 | return; |
| 1465 | } |
| 1466 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1467 | // Patch the call site depending on the state of the cache. |
| 1468 | if (state == UNINITIALIZED || state == MONOMORPHIC_PROTOTYPE_FAILURE) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1469 | set_target(*code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1470 | } else if (state == MONOMORPHIC) { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 1471 | // Only move to megamorphic if the target changes. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1472 | if (target() != *code) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1473 | set_target((strict_mode == kStrictMode) |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1474 | ? megamorphic_stub_strict() |
| 1475 | : megamorphic_stub()); |
| 1476 | } |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 1477 | } else if (state == MEGAMORPHIC) { |
| 1478 | // Update the stub cache. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1479 | isolate()->stub_cache()->Set(*name, receiver->map(), *code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1482 | TRACE_IC("StoreIC", name, state, target()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1486 | static bool AddOneReceiverMapIfMissing(MapHandleList* receiver_maps, |
| 1487 | Handle<Map> new_receiver_map) { |
| 1488 | ASSERT(!new_receiver_map.is_null()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1489 | for (int current = 0; current < receiver_maps->length(); ++current) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1490 | if (!receiver_maps->at(current).is_null() && |
| 1491 | receiver_maps->at(current).is_identical_to(new_receiver_map)) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1492 | return false; |
| 1493 | } |
| 1494 | } |
| 1495 | receiver_maps->Add(new_receiver_map); |
| 1496 | return true; |
| 1497 | } |
| 1498 | |
| 1499 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1500 | void KeyedIC::GetReceiverMapsForStub(Handle<Code> stub, |
| 1501 | MapHandleList* result) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1502 | ASSERT(stub->is_inline_cache_stub()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1503 | if (!string_stub().is_null() && stub.is_identical_to(string_stub())) { |
| 1504 | return result->Add(isolate()->factory()->string_map()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1505 | } else if (stub->is_keyed_load_stub() || stub->is_keyed_store_stub()) { |
| 1506 | if (stub->ic_state() == MONOMORPHIC) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1507 | result->Add(Handle<Map>(stub->FindFirstMap())); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1508 | } else { |
| 1509 | ASSERT(stub->ic_state() == MEGAMORPHIC); |
| 1510 | AssertNoAllocation no_allocation; |
| 1511 | int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1512 | for (RelocIterator it(*stub, mask); !it.done(); it.next()) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1513 | RelocInfo* info = it.rinfo(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1514 | Handle<Object> object(info->target_object()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1515 | ASSERT(object->IsMap()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1516 | AddOneReceiverMapIfMissing(result, Handle<Map>::cast(object)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1517 | } |
| 1518 | } |
| 1519 | } |
| 1520 | } |
| 1521 | |
| 1522 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1523 | Handle<Code> KeyedIC::ComputeStub(Handle<JSObject> receiver, |
| 1524 | StubKind stub_kind, |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1525 | StrictModeFlag strict_mode, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1526 | Handle<Code> generic_stub) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1527 | State ic_state = target()->ic_state(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1528 | KeyedAccessGrowMode grow_mode = IsGrowStubKind(stub_kind) |
| 1529 | ? ALLOW_JSARRAY_GROWTH |
| 1530 | : DO_NOT_ALLOW_JSARRAY_GROWTH; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1531 | |
| 1532 | // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS |
| 1533 | // via megamorphic stubs, since they don't have a map in their relocation info |
| 1534 | // and so the stubs can't be harvested for the object needed for a map check. |
| 1535 | if (target()->type() != NORMAL) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1536 | TRACE_GENERIC_IC("KeyedIC", "non-NORMAL target type"); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1537 | return generic_stub; |
| 1538 | } |
| 1539 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1540 | bool monomorphic = false; |
| 1541 | MapHandleList target_receiver_maps; |
| 1542 | if (ic_state != UNINITIALIZED && ic_state != PREMONOMORPHIC) { |
| 1543 | GetReceiverMapsForStub(Handle<Code>(target()), &target_receiver_maps); |
| 1544 | } |
| 1545 | if (!IsTransitionStubKind(stub_kind)) { |
| 1546 | if (ic_state == UNINITIALIZED || ic_state == PREMONOMORPHIC) { |
| 1547 | monomorphic = true; |
| 1548 | } else { |
| 1549 | if (ic_state == MONOMORPHIC) { |
| 1550 | // The first time a receiver is seen that is a transitioned version of |
| 1551 | // the previous monomorphic receiver type, assume the new ElementsKind |
| 1552 | // is the monomorphic type. This benefits global arrays that only |
| 1553 | // transition once, and all call sites accessing them are faster if they |
| 1554 | // remain monomorphic. If this optimistic assumption is not true, the IC |
| 1555 | // will miss again and it will become polymorphic and support both the |
| 1556 | // untransitioned and transitioned maps. |
| 1557 | monomorphic = IsMoreGeneralElementsKindTransition( |
| 1558 | target_receiver_maps.at(0)->elements_kind(), |
| 1559 | receiver->GetElementsKind()); |
| 1560 | } |
| 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | if (monomorphic) { |
| 1565 | return ComputeMonomorphicStub( |
| 1566 | receiver, stub_kind, strict_mode, generic_stub); |
| 1567 | } |
| 1568 | ASSERT(target() != *generic_stub); |
| 1569 | |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 1570 | // Determine the list of receiver maps that this call site has seen, |
| 1571 | // adding the map that was just encountered. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1572 | Handle<Map> receiver_map(receiver->map()); |
| 1573 | bool map_added = |
| 1574 | AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); |
| 1575 | if (IsTransitionStubKind(stub_kind)) { |
| 1576 | Handle<Map> new_map = ComputeTransitionedMap(receiver, stub_kind); |
| 1577 | map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, new_map); |
| 1578 | } |
| 1579 | if (!map_added) { |
| 1580 | // If the miss wasn't due to an unseen map, a polymorphic stub |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1581 | // won't help, use the generic stub. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1582 | TRACE_GENERIC_IC("KeyedIC", "same map added twice"); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1583 | return generic_stub; |
| 1584 | } |
| 1585 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1586 | // If the maximum number of receiver maps has been exceeded, use the generic |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1587 | // version of the IC. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1588 | if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1589 | TRACE_GENERIC_IC("KeyedIC", "max polymorph exceeded"); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1590 | return generic_stub; |
| 1591 | } |
| 1592 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1593 | if ((Code::GetKeyedAccessGrowMode(target()->extra_ic_state()) == |
| 1594 | ALLOW_JSARRAY_GROWTH)) { |
| 1595 | grow_mode = ALLOW_JSARRAY_GROWTH; |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 1596 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1597 | |
| 1598 | Handle<PolymorphicCodeCache> cache = |
| 1599 | isolate()->factory()->polymorphic_code_cache(); |
| 1600 | Code::ExtraICState extra_state = Code::ComputeExtraICState(grow_mode, |
| 1601 | strict_mode); |
| 1602 | Code::Flags flags = Code::ComputeFlags(kind(), MEGAMORPHIC, extra_state); |
| 1603 | Handle<Object> probe = cache->Lookup(&target_receiver_maps, flags); |
| 1604 | if (probe->IsCode()) return Handle<Code>::cast(probe); |
| 1605 | |
| 1606 | Handle<Code> stub = |
| 1607 | ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode); |
| 1608 | PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1609 | return stub; |
| 1610 | } |
| 1611 | |
| 1612 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1613 | Handle<Code> KeyedIC::ComputeMonomorphicStubWithoutMapCheck( |
| 1614 | Handle<Map> receiver_map, |
| 1615 | StrictModeFlag strict_mode, |
| 1616 | KeyedAccessGrowMode grow_mode) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1617 | if ((receiver_map->instance_type() & kNotStringTag) == 0) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1618 | ASSERT(!string_stub().is_null()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1619 | return string_stub(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1620 | } else { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1621 | ASSERT(receiver_map->has_dictionary_elements() || |
| 1622 | receiver_map->has_fast_elements() || |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1623 | receiver_map->has_fast_smi_only_elements() || |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1624 | receiver_map->has_fast_double_elements() || |
| 1625 | receiver_map->has_external_array_elements()); |
| 1626 | bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; |
| 1627 | return GetElementStubWithoutMapCheck(is_js_array, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1628 | receiver_map->elements_kind(), |
| 1629 | grow_mode); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1630 | } |
| 1631 | } |
| 1632 | |
| 1633 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1634 | Handle<Code> KeyedIC::ComputeMonomorphicStub(Handle<JSObject> receiver, |
| 1635 | StubKind stub_kind, |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1636 | StrictModeFlag strict_mode, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1637 | Handle<Code> generic_stub) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1638 | if (receiver->HasFastElements() || |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1639 | receiver->HasFastSmiOnlyElements() || |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1640 | receiver->HasExternalArrayElements() || |
| 1641 | receiver->HasFastDoubleElements() || |
| 1642 | receiver->HasDictionaryElements()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1643 | return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( |
| 1644 | receiver, stub_kind, strict_mode); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1645 | } else { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1646 | return generic_stub; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1647 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1648 | } |
| 1649 | |
| 1650 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1651 | Handle<Map> KeyedIC::ComputeTransitionedMap(Handle<JSObject> receiver, |
| 1652 | StubKind stub_kind) { |
| 1653 | switch (stub_kind) { |
| 1654 | case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: |
| 1655 | case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: |
| 1656 | case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: |
| 1657 | case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: |
| 1658 | return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); |
| 1659 | break; |
| 1660 | case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: |
| 1661 | case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: |
| 1662 | return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); |
| 1663 | break; |
| 1664 | default: |
| 1665 | UNREACHABLE(); |
| 1666 | return Handle<Map>::null(); |
| 1667 | } |
| 1668 | } |
| 1669 | |
| 1670 | |
| 1671 | Handle<Code> KeyedStoreIC::GetElementStubWithoutMapCheck( |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1672 | bool is_js_array, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1673 | ElementsKind elements_kind, |
| 1674 | KeyedAccessGrowMode grow_mode) { |
| 1675 | return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
| 1678 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1679 | Handle<Code> KeyedStoreIC::ComputePolymorphicStub( |
| 1680 | MapHandleList* receiver_maps, |
| 1681 | StrictModeFlag strict_mode, |
| 1682 | KeyedAccessGrowMode grow_mode) { |
| 1683 | // Collect MONOMORPHIC stubs for all target_receiver_maps. |
| 1684 | CodeHandleList handler_ics(receiver_maps->length()); |
| 1685 | MapHandleList transitioned_maps(receiver_maps->length()); |
| 1686 | for (int i = 0; i < receiver_maps->length(); ++i) { |
| 1687 | Handle<Map> receiver_map(receiver_maps->at(i)); |
| 1688 | Handle<Code> cached_stub; |
| 1689 | Handle<Map> transitioned_map = |
| 1690 | receiver_map->FindTransitionedMap(receiver_maps); |
| 1691 | if (!transitioned_map.is_null()) { |
| 1692 | cached_stub = ElementsTransitionAndStoreStub( |
| 1693 | receiver_map->elements_kind(), // original elements_kind |
| 1694 | transitioned_map->elements_kind(), |
| 1695 | receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array |
| 1696 | strict_mode, grow_mode).GetCode(); |
| 1697 | } else { |
| 1698 | cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, |
| 1699 | strict_mode, |
| 1700 | grow_mode); |
| 1701 | } |
| 1702 | ASSERT(!cached_stub.is_null()); |
| 1703 | handler_ics.Add(cached_stub); |
| 1704 | transitioned_maps.Add(transitioned_map); |
| 1705 | } |
| 1706 | KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); |
| 1707 | Handle<Code> code = compiler.CompileStorePolymorphic( |
| 1708 | receiver_maps, &handler_ics, &transitioned_maps); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1709 | isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1710 | PROFILE(isolate(), |
| 1711 | CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); |
| 1712 | return code; |
| 1713 | } |
| 1714 | |
| 1715 | |
| 1716 | KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle<JSObject> receiver, |
| 1717 | Handle<Object> key, |
| 1718 | Handle<Object> value) { |
| 1719 | ASSERT(key->IsSmi()); |
| 1720 | int index = Smi::cast(*key)->value(); |
| 1721 | bool allow_growth = receiver->IsJSArray() && |
| 1722 | JSArray::cast(*receiver)->length()->IsSmi() && |
| 1723 | index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); |
| 1724 | |
| 1725 | if (allow_growth) { |
| 1726 | // Handle growing array in stub if necessary. |
| 1727 | if (receiver->HasFastSmiOnlyElements()) { |
| 1728 | if (value->IsHeapNumber()) { |
| 1729 | return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; |
| 1730 | } |
| 1731 | if (value->IsHeapObject()) { |
| 1732 | return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; |
| 1733 | } |
| 1734 | } else if (receiver->HasFastDoubleElements()) { |
| 1735 | if (!value->IsSmi() && !value->IsHeapNumber()) { |
| 1736 | return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; |
| 1737 | } |
| 1738 | } |
| 1739 | return STORE_AND_GROW_NO_TRANSITION; |
| 1740 | } else { |
| 1741 | // Handle only in-bounds elements accesses. |
| 1742 | if (receiver->HasFastSmiOnlyElements()) { |
| 1743 | if (value->IsHeapNumber()) { |
| 1744 | return STORE_TRANSITION_SMI_TO_DOUBLE; |
| 1745 | } else if (value->IsHeapObject()) { |
| 1746 | return STORE_TRANSITION_SMI_TO_OBJECT; |
| 1747 | } |
| 1748 | } else if (receiver->HasFastDoubleElements()) { |
| 1749 | if (!value->IsSmi() && !value->IsHeapNumber()) { |
| 1750 | return STORE_TRANSITION_DOUBLE_TO_OBJECT; |
| 1751 | } |
| 1752 | } |
| 1753 | return STORE_NO_TRANSITION; |
| 1754 | } |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 1755 | } |
| 1756 | |
| 1757 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1758 | MaybeObject* KeyedStoreIC::Store(State state, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1759 | StrictModeFlag strict_mode, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1760 | Handle<Object> object, |
| 1761 | Handle<Object> key, |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1762 | Handle<Object> value, |
| 1763 | bool force_generic) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1764 | if (key->IsSymbol()) { |
| 1765 | Handle<String> name = Handle<String>::cast(key); |
| 1766 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1767 | // Handle proxies. |
| 1768 | if (object->IsJSProxy()) { |
| 1769 | return JSProxy::cast(*object)->SetProperty( |
| 1770 | *name, *value, NONE, strict_mode); |
| 1771 | } |
| 1772 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1773 | // If the object is undefined or null it's illegal to try to set any |
| 1774 | // properties on it; throw a TypeError in that case. |
| 1775 | if (object->IsUndefined() || object->IsNull()) { |
| 1776 | return TypeError("non_object_property_store", object, name); |
| 1777 | } |
| 1778 | |
| 1779 | // Ignore stores where the receiver is not a JSObject. |
| 1780 | if (!object->IsJSObject()) return *value; |
| 1781 | Handle<JSObject> receiver = Handle<JSObject>::cast(object); |
| 1782 | |
| 1783 | // Check if the given name is an array index. |
| 1784 | uint32_t index; |
| 1785 | if (name->AsArrayIndex(&index)) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1786 | Handle<Object> result = |
| 1787 | JSObject::SetElement(receiver, index, value, NONE, strict_mode); |
| 1788 | RETURN_IF_EMPTY_HANDLE(isolate(), result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1789 | return *value; |
| 1790 | } |
| 1791 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1792 | // Update inline cache and stub cache. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1793 | if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { |
| 1794 | LookupResult lookup(isolate()); |
| 1795 | if (LookupForWrite(receiver, name, &lookup)) { |
| 1796 | UpdateCaches(&lookup, state, strict_mode, receiver, name, value); |
| 1797 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
| 1800 | // Set the property. |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1801 | return receiver->SetProperty(*name, *value, NONE, strict_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1802 | } |
| 1803 | |
| 1804 | // Do not use ICs for objects that require access checks (including |
| 1805 | // the global object). |
| 1806 | bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); |
| 1807 | ASSERT(!(use_ic && object->IsJSGlobalProxy())); |
| 1808 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1809 | if (use_ic) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1810 | Handle<Code> stub = (strict_mode == kStrictMode) |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1811 | ? generic_stub_strict() |
| 1812 | : generic_stub(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1813 | if (object->IsJSObject()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1814 | Handle<JSObject> receiver = Handle<JSObject>::cast(object); |
| 1815 | if (receiver->elements()->map() == |
| 1816 | isolate()->heap()->non_strict_arguments_elements_map()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1817 | stub = non_strict_arguments_stub(); |
| 1818 | } else if (!force_generic) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1819 | if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { |
| 1820 | StubKind stub_kind = GetStubKind(receiver, key, value); |
| 1821 | stub = ComputeStub(receiver, stub_kind, strict_mode, stub); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1822 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1823 | } else { |
| 1824 | TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1825 | } |
| 1826 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1827 | if (!stub.is_null()) set_target(*stub); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1828 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1829 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1830 | TRACE_IC("KeyedStoreIC", key, state, target()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1831 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1832 | // Set the property. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1833 | return Runtime::SetObjectProperty( |
| 1834 | isolate(), object , key, value, NONE, strict_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | |
| 1838 | void KeyedStoreIC::UpdateCaches(LookupResult* lookup, |
| 1839 | State state, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1840 | StrictModeFlag strict_mode, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1841 | Handle<JSObject> receiver, |
| 1842 | Handle<String> name, |
| 1843 | Handle<Object> value) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1844 | ASSERT(!receiver->IsJSGlobalProxy()); |
| 1845 | ASSERT(StoreICableLookup(lookup)); |
| 1846 | // These are not cacheable, so we never see such LookupResults here. |
| 1847 | ASSERT(lookup->type() != HANDLER); |
| 1848 | // We get only called for properties or transitions, see StoreICableLookup. |
| 1849 | ASSERT(lookup->type() != NULL_DESCRIPTOR); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1850 | |
| 1851 | // If the property has a non-field type allowing map transitions |
| 1852 | // where there is extra room in the object, we leave the IC in its |
| 1853 | // current state. |
| 1854 | PropertyType type = lookup->type(); |
| 1855 | |
| 1856 | // Compute the code stub for this store; used for rewriting to |
| 1857 | // monomorphic state and making sure that the code stub is in the |
| 1858 | // stub cache. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1859 | Handle<Code> code; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1860 | |
| 1861 | switch (type) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1862 | case FIELD: |
| 1863 | code = isolate()->stub_cache()->ComputeKeyedStoreField( |
| 1864 | name, receiver, lookup->GetFieldIndex(), |
| 1865 | Handle<Map>::null(), strict_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1866 | break; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1867 | case MAP_TRANSITION: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1868 | if (lookup->GetAttributes() == NONE) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1869 | Handle<Map> transition(lookup->GetTransitionMap()); |
| 1870 | int index = transition->PropertyIndexFor(*name); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1871 | code = isolate()->stub_cache()->ComputeKeyedStoreField( |
| 1872 | name, receiver, index, transition, strict_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1873 | break; |
| 1874 | } |
| 1875 | // fall through. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1876 | case NORMAL: |
| 1877 | case CONSTANT_FUNCTION: |
| 1878 | case CALLBACKS: |
| 1879 | case INTERCEPTOR: |
| 1880 | case CONSTANT_TRANSITION: |
| 1881 | case ELEMENTS_TRANSITION: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1882 | // Always rewrite to the generic case so that we do not |
| 1883 | // repeatedly try to rewrite. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1884 | code = (strict_mode == kStrictMode) |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1885 | ? generic_stub_strict() |
| 1886 | : generic_stub(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1887 | break; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1888 | case HANDLER: |
| 1889 | case NULL_DESCRIPTOR: |
| 1890 | UNREACHABLE(); |
| 1891 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1894 | ASSERT(!code.is_null()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1895 | |
| 1896 | // Patch the call site depending on the state of the cache. Make |
| 1897 | // sure to always rewrite from monomorphic to megamorphic. |
| 1898 | ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); |
| 1899 | if (state == UNINITIALIZED || state == PREMONOMORPHIC) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1900 | set_target(*code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1901 | } else if (state == MONOMORPHIC) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1902 | set_target((strict_mode == kStrictMode) |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1903 | ? *megamorphic_stub_strict() |
| 1904 | : *megamorphic_stub()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1905 | } |
| 1906 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1907 | TRACE_IC("KeyedStoreIC", name, state, target()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1908 | } |
| 1909 | |
| 1910 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1911 | #undef TRACE_IC |
| 1912 | |
| 1913 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1914 | // ---------------------------------------------------------------------------- |
| 1915 | // Static IC stub generators. |
| 1916 | // |
| 1917 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1918 | // Used from ic-<arch>.cc. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1919 | RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1920 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1921 | ASSERT(args.length() == 2); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1922 | CallIC ic(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1923 | IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 1924 | Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); |
| 1925 | MaybeObject* maybe_result = ic.LoadFunction(state, |
| 1926 | extra_ic_state, |
| 1927 | args.at<Object>(0), |
| 1928 | args.at<String>(1)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1929 | // Result could be a function or a failure. |
| 1930 | JSFunction* raw_function = NULL; |
| 1931 | if (!maybe_result->To(&raw_function)) return maybe_result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1932 | |
| 1933 | // The first time the inline cache is updated may be the first time the |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1934 | // function it references gets called. If the function is lazily compiled |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1935 | // then the first call will trigger a compilation. We check for this case |
| 1936 | // and we do the compilation immediately, instead of waiting for the stub |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1937 | // currently attached to the JSFunction object to trigger compilation. |
| 1938 | if (raw_function->is_compiled()) return raw_function; |
| 1939 | |
| 1940 | Handle<JSFunction> function(raw_function); |
| 1941 | JSFunction::CompileLazy(function, CLEAR_EXCEPTION); |
| 1942 | return *function; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
| 1945 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1946 | // Used from ic-<arch>.cc. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1947 | RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1948 | HandleScope scope(isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1949 | ASSERT(args.length() == 2); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1950 | KeyedCallIC ic(isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1951 | IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1952 | MaybeObject* maybe_result = |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1953 | ic.LoadFunction(state, args.at<Object>(0), args.at<Object>(1)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1954 | // Result could be a function or a failure. |
| 1955 | JSFunction* raw_function = NULL; |
| 1956 | if (!maybe_result->To(&raw_function)) return maybe_result; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1957 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1958 | if (raw_function->is_compiled()) return raw_function; |
| 1959 | |
| 1960 | Handle<JSFunction> function(raw_function); |
| 1961 | JSFunction::CompileLazy(function, CLEAR_EXCEPTION); |
| 1962 | return *function; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1963 | } |
| 1964 | |
| 1965 | |
| 1966 | // Used from ic-<arch>.cc. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1967 | RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1968 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1969 | ASSERT(args.length() == 2); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1970 | LoadIC ic(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1971 | IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1972 | return ic.Load(state, args.at<Object>(0), args.at<String>(1)); |
| 1973 | } |
| 1974 | |
| 1975 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1976 | // Used from ic-<arch>.cc |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1977 | RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1978 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1979 | ASSERT(args.length() == 2); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1980 | KeyedLoadIC ic(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1981 | IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1982 | return ic.Load(state, args.at<Object>(0), args.at<Object>(1), false); |
| 1983 | } |
| 1984 | |
| 1985 | |
| 1986 | RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1987 | HandleScope scope(isolate); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1988 | ASSERT(args.length() == 2); |
| 1989 | KeyedLoadIC ic(isolate); |
| 1990 | IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); |
| 1991 | return ic.Load(state, args.at<Object>(0), args.at<Object>(1), true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1992 | } |
| 1993 | |
| 1994 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1995 | // Used from ic-<arch>.cc. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1996 | RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1997 | HandleScope scope; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1998 | ASSERT(args.length() == 3); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1999 | StoreIC ic(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2000 | IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 2001 | Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 2002 | return ic.Store(state, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2003 | Code::GetStrictMode(extra_ic_state), |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 2004 | args.at<Object>(0), |
| 2005 | args.at<String>(1), |
| 2006 | args.at<Object>(2)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2007 | } |
| 2008 | |
| 2009 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2010 | RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2011 | NoHandleAllocation nha; |
| 2012 | |
| 2013 | ASSERT(args.length() == 2); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2014 | JSArray* receiver = JSArray::cast(args[0]); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2015 | Object* len = args[1]; |
| 2016 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2017 | // The generated code should filter out non-Smis before we get here. |
| 2018 | ASSERT(len->IsSmi()); |
| 2019 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2020 | #ifdef DEBUG |
| 2021 | // The length property has to be a writable callback property. |
| 2022 | LookupResult debug_lookup(isolate); |
| 2023 | receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); |
| 2024 | ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); |
| 2025 | #endif |
| 2026 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2027 | Object* result; |
| 2028 | { MaybeObject* maybe_result = receiver->SetElementsLength(len); |
| 2029 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2030 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2031 | return len; |
| 2032 | } |
| 2033 | |
| 2034 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2035 | // Extend storage is called in a store inline cache when |
| 2036 | // it is necessary to extend the properties array of a |
| 2037 | // JSObject. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2038 | RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2039 | NoHandleAllocation na; |
| 2040 | ASSERT(args.length() == 3); |
| 2041 | |
| 2042 | // Convert the parameters |
| 2043 | JSObject* object = JSObject::cast(args[0]); |
| 2044 | Map* transition = Map::cast(args[1]); |
| 2045 | Object* value = args[2]; |
| 2046 | |
| 2047 | // Check the object has run out out property space. |
| 2048 | ASSERT(object->HasFastProperties()); |
| 2049 | ASSERT(object->map()->unused_property_fields() == 0); |
| 2050 | |
| 2051 | // Expand the properties array. |
| 2052 | FixedArray* old_storage = object->properties(); |
| 2053 | int new_unused = transition->unused_property_fields(); |
| 2054 | int new_size = old_storage->length() + new_unused + 1; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2055 | Object* result; |
| 2056 | { MaybeObject* maybe_result = old_storage->CopySize(new_size); |
| 2057 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2058 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2059 | FixedArray* new_storage = FixedArray::cast(result); |
| 2060 | new_storage->set(old_storage->length(), value); |
| 2061 | |
| 2062 | // Set the new property value and do the map transition. |
| 2063 | object->set_properties(new_storage); |
| 2064 | object->set_map(transition); |
| 2065 | |
| 2066 | // Return the stored value. |
| 2067 | return value; |
| 2068 | } |
| 2069 | |
| 2070 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2071 | // Used from ic-<arch>.cc. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2072 | RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2073 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2074 | ASSERT(args.length() == 3); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2075 | KeyedStoreIC ic(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2076 | IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 2077 | Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); |
| 2078 | return ic.Store(state, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2079 | Code::GetStrictMode(extra_ic_state), |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 2080 | args.at<Object>(0), |
| 2081 | args.at<Object>(1), |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2082 | args.at<Object>(2), |
| 2083 | false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2084 | } |
| 2085 | |
| 2086 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2087 | RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { |
| 2088 | NoHandleAllocation na; |
| 2089 | ASSERT(args.length() == 3); |
| 2090 | KeyedStoreIC ic(isolate); |
| 2091 | Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); |
| 2092 | Handle<Object> object = args.at<Object>(0); |
| 2093 | Handle<Object> key = args.at<Object>(1); |
| 2094 | Handle<Object> value = args.at<Object>(2); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2095 | StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2096 | return Runtime::SetObjectProperty(isolate, |
| 2097 | object, |
| 2098 | key, |
| 2099 | value, |
| 2100 | NONE, |
| 2101 | strict_mode); |
| 2102 | } |
| 2103 | |
| 2104 | |
| 2105 | RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2106 | HandleScope scope(isolate); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2107 | ASSERT(args.length() == 3); |
| 2108 | KeyedStoreIC ic(isolate); |
| 2109 | IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); |
| 2110 | Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); |
| 2111 | return ic.Store(state, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2112 | Code::GetStrictMode(extra_ic_state), |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2113 | args.at<Object>(0), |
| 2114 | args.at<Object>(1), |
| 2115 | args.at<Object>(2), |
| 2116 | true); |
| 2117 | } |
| 2118 | |
| 2119 | |
| 2120 | void UnaryOpIC::patch(Code* code) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2121 | set_target(code); |
| 2122 | } |
| 2123 | |
| 2124 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2125 | const char* UnaryOpIC::GetName(TypeInfo type_info) { |
| 2126 | switch (type_info) { |
| 2127 | case UNINITIALIZED: return "Uninitialized"; |
| 2128 | case SMI: return "Smi"; |
| 2129 | case HEAP_NUMBER: return "HeapNumbers"; |
| 2130 | case GENERIC: return "Generic"; |
| 2131 | default: return "Invalid"; |
| 2132 | } |
| 2133 | } |
| 2134 | |
| 2135 | |
| 2136 | UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { |
| 2137 | switch (type_info) { |
| 2138 | case UNINITIALIZED: |
| 2139 | return ::v8::internal::UNINITIALIZED; |
| 2140 | case SMI: |
| 2141 | case HEAP_NUMBER: |
| 2142 | return MONOMORPHIC; |
| 2143 | case GENERIC: |
| 2144 | return MEGAMORPHIC; |
| 2145 | } |
| 2146 | UNREACHABLE(); |
| 2147 | return ::v8::internal::UNINITIALIZED; |
| 2148 | } |
| 2149 | |
| 2150 | UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle<Object> operand) { |
| 2151 | ::v8::internal::TypeInfo operand_type = |
| 2152 | ::v8::internal::TypeInfo::TypeFromValue(operand); |
| 2153 | if (operand_type.IsSmi()) { |
| 2154 | return SMI; |
| 2155 | } else if (operand_type.IsNumber()) { |
| 2156 | return HEAP_NUMBER; |
| 2157 | } else { |
| 2158 | return GENERIC; |
| 2159 | } |
| 2160 | } |
| 2161 | |
| 2162 | |
| 2163 | UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( |
| 2164 | UnaryOpIC::TypeInfo current_type, |
| 2165 | UnaryOpIC::TypeInfo previous_type) { |
| 2166 | switch (previous_type) { |
| 2167 | case UnaryOpIC::UNINITIALIZED: |
| 2168 | return current_type; |
| 2169 | case UnaryOpIC::SMI: |
| 2170 | return (current_type == UnaryOpIC::GENERIC) |
| 2171 | ? UnaryOpIC::GENERIC |
| 2172 | : UnaryOpIC::HEAP_NUMBER; |
| 2173 | case UnaryOpIC::HEAP_NUMBER: |
| 2174 | return UnaryOpIC::GENERIC; |
| 2175 | case UnaryOpIC::GENERIC: |
| 2176 | // We should never do patching if we are in GENERIC state. |
| 2177 | UNREACHABLE(); |
| 2178 | return UnaryOpIC::GENERIC; |
| 2179 | } |
| 2180 | UNREACHABLE(); |
| 2181 | return UnaryOpIC::GENERIC; |
| 2182 | } |
| 2183 | |
| 2184 | |
| 2185 | void BinaryOpIC::patch(Code* code) { |
| 2186 | set_target(code); |
| 2187 | } |
| 2188 | |
| 2189 | |
| 2190 | const char* BinaryOpIC::GetName(TypeInfo type_info) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2191 | switch (type_info) { |
| 2192 | case UNINITIALIZED: return "Uninitialized"; |
| 2193 | case SMI: return "SMI"; |
| 2194 | case INT32: return "Int32s"; |
| 2195 | case HEAP_NUMBER: return "HeapNumbers"; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2196 | case ODDBALL: return "Oddball"; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2197 | case BOTH_STRING: return "BothStrings"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2198 | case STRING: return "Strings"; |
| 2199 | case GENERIC: return "Generic"; |
| 2200 | default: return "Invalid"; |
| 2201 | } |
| 2202 | } |
| 2203 | |
| 2204 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2205 | BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2206 | switch (type_info) { |
| 2207 | case UNINITIALIZED: |
| 2208 | return ::v8::internal::UNINITIALIZED; |
| 2209 | case SMI: |
| 2210 | case INT32: |
| 2211 | case HEAP_NUMBER: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2212 | case ODDBALL: |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2213 | case BOTH_STRING: |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2214 | case STRING: |
| 2215 | return MONOMORPHIC; |
| 2216 | case GENERIC: |
| 2217 | return MEGAMORPHIC; |
| 2218 | } |
| 2219 | UNREACHABLE(); |
| 2220 | return ::v8::internal::UNINITIALIZED; |
| 2221 | } |
| 2222 | |
| 2223 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2224 | BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, |
| 2225 | BinaryOpIC::TypeInfo y) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2226 | if (x == UNINITIALIZED) return y; |
| 2227 | if (y == UNINITIALIZED) return x; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2228 | if (x == y) return x; |
| 2229 | if (x == BOTH_STRING && y == STRING) return STRING; |
| 2230 | if (x == STRING && y == BOTH_STRING) return STRING; |
| 2231 | if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { |
| 2232 | return GENERIC; |
| 2233 | } |
| 2234 | if (x > y) return x; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2235 | return y; |
| 2236 | } |
| 2237 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2238 | |
| 2239 | BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle<Object> left, |
| 2240 | Handle<Object> right) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2241 | ::v8::internal::TypeInfo left_type = |
| 2242 | ::v8::internal::TypeInfo::TypeFromValue(left); |
| 2243 | ::v8::internal::TypeInfo right_type = |
| 2244 | ::v8::internal::TypeInfo::TypeFromValue(right); |
| 2245 | |
| 2246 | if (left_type.IsSmi() && right_type.IsSmi()) { |
| 2247 | return SMI; |
| 2248 | } |
| 2249 | |
| 2250 | if (left_type.IsInteger32() && right_type.IsInteger32()) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 2251 | // Platforms with 32-bit Smis have no distinct INT32 type. |
| 2252 | if (kSmiValueSize == 32) return SMI; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2253 | return INT32; |
| 2254 | } |
| 2255 | |
| 2256 | if (left_type.IsNumber() && right_type.IsNumber()) { |
| 2257 | return HEAP_NUMBER; |
| 2258 | } |
| 2259 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2260 | // Patching for fast string ADD makes sense even if only one of the |
| 2261 | // arguments is a string. |
| 2262 | if (left_type.IsString()) { |
| 2263 | return right_type.IsString() ? BOTH_STRING : STRING; |
| 2264 | } else if (right_type.IsString()) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2265 | return STRING; |
| 2266 | } |
| 2267 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2268 | // Check for oddball objects. |
| 2269 | if (left->IsUndefined() && right->IsNumber()) return ODDBALL; |
| 2270 | if (left->IsNumber() && right->IsUndefined()) return ODDBALL; |
| 2271 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2272 | return GENERIC; |
| 2273 | } |
| 2274 | |
| 2275 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2276 | RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { |
| 2277 | ASSERT(args.length() == 4); |
| 2278 | |
| 2279 | HandleScope scope(isolate); |
| 2280 | Handle<Object> operand = args.at<Object>(0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2281 | Token::Value op = static_cast<Token::Value>(args.smi_at(1)); |
| 2282 | UnaryOverwriteMode mode = static_cast<UnaryOverwriteMode>(args.smi_at(2)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2283 | UnaryOpIC::TypeInfo previous_type = |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2284 | static_cast<UnaryOpIC::TypeInfo>(args.smi_at(3)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2285 | |
| 2286 | UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); |
| 2287 | type = UnaryOpIC::ComputeNewType(type, previous_type); |
| 2288 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2289 | UnaryOpStub stub(op, mode, type); |
| 2290 | Handle<Code> code = stub.GetCode(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2291 | if (!code.is_null()) { |
| 2292 | if (FLAG_trace_ic) { |
| 2293 | PrintF("[UnaryOpIC (%s->%s)#%s]\n", |
| 2294 | UnaryOpIC::GetName(previous_type), |
| 2295 | UnaryOpIC::GetName(type), |
| 2296 | Token::Name(op)); |
| 2297 | } |
| 2298 | UnaryOpIC ic(isolate); |
| 2299 | ic.patch(*code); |
| 2300 | } |
| 2301 | |
| 2302 | Handle<JSBuiltinsObject> builtins = Handle<JSBuiltinsObject>( |
| 2303 | isolate->thread_local_top()->context_->builtins(), isolate); |
| 2304 | Object* builtin = NULL; // Initialization calms down the compiler. |
| 2305 | switch (op) { |
| 2306 | case Token::SUB: |
| 2307 | builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); |
| 2308 | break; |
| 2309 | case Token::BIT_NOT: |
| 2310 | builtin = builtins->javascript_builtin(Builtins::BIT_NOT); |
| 2311 | break; |
| 2312 | default: |
| 2313 | UNREACHABLE(); |
| 2314 | } |
| 2315 | |
| 2316 | Handle<JSFunction> builtin_function(JSFunction::cast(builtin), isolate); |
| 2317 | |
| 2318 | bool caught_exception; |
| 2319 | Handle<Object> result = Execution::Call(builtin_function, operand, 0, NULL, |
| 2320 | &caught_exception); |
| 2321 | if (caught_exception) { |
| 2322 | return Failure::Exception(); |
| 2323 | } |
| 2324 | return *result; |
| 2325 | } |
| 2326 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2327 | RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2328 | ASSERT(args.length() == 5); |
| 2329 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2330 | HandleScope scope(isolate); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2331 | Handle<Object> left = args.at<Object>(0); |
| 2332 | Handle<Object> right = args.at<Object>(1); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2333 | int key = args.smi_at(2); |
| 2334 | Token::Value op = static_cast<Token::Value>(args.smi_at(3)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2335 | BinaryOpIC::TypeInfo previous_type = |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2336 | static_cast<BinaryOpIC::TypeInfo>(args.smi_at(4)); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2337 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2338 | BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); |
| 2339 | type = BinaryOpIC::JoinTypes(type, previous_type); |
| 2340 | BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; |
| 2341 | if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && |
| 2342 | op != Token::ADD) { |
| 2343 | type = BinaryOpIC::GENERIC; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2344 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2345 | if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { |
| 2346 | if (op == Token::DIV || |
| 2347 | op == Token::MUL || |
| 2348 | op == Token::SHR || |
| 2349 | kSmiValueSize == 32) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2350 | // Arithmetic on two Smi inputs has yielded a heap number. |
| 2351 | // That is the only way to get here from the Smi stub. |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 2352 | // With 32-bit Smis, all overflows give heap numbers, but with |
| 2353 | // 31-bit Smis, most operations overflow to int32 results. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2354 | result_type = BinaryOpIC::HEAP_NUMBER; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2355 | } else { |
| 2356 | // Other operations on SMIs that overflow yield int32s. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2357 | result_type = BinaryOpIC::INT32; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2358 | } |
| 2359 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2360 | if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2361 | // We must be here because an operation on two INT32 types overflowed. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2362 | result_type = BinaryOpIC::HEAP_NUMBER; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2363 | } |
| 2364 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2365 | BinaryOpStub stub(key, type, result_type); |
| 2366 | Handle<Code> code = stub.GetCode(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2367 | if (!code.is_null()) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2368 | if (FLAG_trace_ic) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2369 | PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", |
| 2370 | BinaryOpIC::GetName(previous_type), |
| 2371 | BinaryOpIC::GetName(type), |
| 2372 | BinaryOpIC::GetName(result_type), |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2373 | Token::Name(op)); |
| 2374 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2375 | BinaryOpIC ic(isolate); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 2376 | ic.patch(*code); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2377 | |
| 2378 | // Activate inlined smi code. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2379 | if (previous_type == BinaryOpIC::UNINITIALIZED) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2380 | PatchInlinedSmiCode(ic.address()); |
| 2381 | } |
| 2382 | } |
| 2383 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2384 | Handle<JSBuiltinsObject> builtins = Handle<JSBuiltinsObject>( |
| 2385 | isolate->thread_local_top()->context_->builtins(), isolate); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2386 | Object* builtin = NULL; // Initialization calms down the compiler. |
| 2387 | switch (op) { |
| 2388 | case Token::ADD: |
| 2389 | builtin = builtins->javascript_builtin(Builtins::ADD); |
| 2390 | break; |
| 2391 | case Token::SUB: |
| 2392 | builtin = builtins->javascript_builtin(Builtins::SUB); |
| 2393 | break; |
| 2394 | case Token::MUL: |
| 2395 | builtin = builtins->javascript_builtin(Builtins::MUL); |
| 2396 | break; |
| 2397 | case Token::DIV: |
| 2398 | builtin = builtins->javascript_builtin(Builtins::DIV); |
| 2399 | break; |
| 2400 | case Token::MOD: |
| 2401 | builtin = builtins->javascript_builtin(Builtins::MOD); |
| 2402 | break; |
| 2403 | case Token::BIT_AND: |
| 2404 | builtin = builtins->javascript_builtin(Builtins::BIT_AND); |
| 2405 | break; |
| 2406 | case Token::BIT_OR: |
| 2407 | builtin = builtins->javascript_builtin(Builtins::BIT_OR); |
| 2408 | break; |
| 2409 | case Token::BIT_XOR: |
| 2410 | builtin = builtins->javascript_builtin(Builtins::BIT_XOR); |
| 2411 | break; |
| 2412 | case Token::SHR: |
| 2413 | builtin = builtins->javascript_builtin(Builtins::SHR); |
| 2414 | break; |
| 2415 | case Token::SAR: |
| 2416 | builtin = builtins->javascript_builtin(Builtins::SAR); |
| 2417 | break; |
| 2418 | case Token::SHL: |
| 2419 | builtin = builtins->javascript_builtin(Builtins::SHL); |
| 2420 | break; |
| 2421 | default: |
| 2422 | UNREACHABLE(); |
| 2423 | } |
| 2424 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2425 | Handle<JSFunction> builtin_function(JSFunction::cast(builtin), isolate); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2426 | |
| 2427 | bool caught_exception; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2428 | Handle<Object> builtin_args[] = { right }; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2429 | Handle<Object> result = Execution::Call(builtin_function, |
| 2430 | left, |
| 2431 | ARRAY_SIZE(builtin_args), |
| 2432 | builtin_args, |
| 2433 | &caught_exception); |
| 2434 | if (caught_exception) { |
| 2435 | return Failure::Exception(); |
| 2436 | } |
| 2437 | return *result; |
| 2438 | } |
| 2439 | |
| 2440 | |
| 2441 | Handle<Code> CompareIC::GetUninitialized(Token::Value op) { |
| 2442 | ICCompareStub stub(op, UNINITIALIZED); |
| 2443 | return stub.GetCode(); |
| 2444 | } |
| 2445 | |
| 2446 | |
| 2447 | CompareIC::State CompareIC::ComputeState(Code* target) { |
| 2448 | int key = target->major_key(); |
| 2449 | if (key == CodeStub::Compare) return GENERIC; |
| 2450 | ASSERT(key == CodeStub::CompareIC); |
| 2451 | return static_cast<State>(target->compare_state()); |
| 2452 | } |
| 2453 | |
| 2454 | |
| 2455 | const char* CompareIC::GetStateName(State state) { |
| 2456 | switch (state) { |
| 2457 | case UNINITIALIZED: return "UNINITIALIZED"; |
| 2458 | case SMIS: return "SMIS"; |
| 2459 | case HEAP_NUMBERS: return "HEAP_NUMBERS"; |
| 2460 | case OBJECTS: return "OBJECTS"; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2461 | case KNOWN_OBJECTS: return "OBJECTS"; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2462 | case SYMBOLS: return "SYMBOLS"; |
| 2463 | case STRINGS: return "STRINGS"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2464 | case GENERIC: return "GENERIC"; |
| 2465 | default: |
| 2466 | UNREACHABLE(); |
| 2467 | return NULL; |
| 2468 | } |
| 2469 | } |
| 2470 | |
| 2471 | |
| 2472 | CompareIC::State CompareIC::TargetState(State state, |
| 2473 | bool has_inlined_smi_code, |
| 2474 | Handle<Object> x, |
| 2475 | Handle<Object> y) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2476 | switch (state) { |
| 2477 | case UNINITIALIZED: |
| 2478 | if (x->IsSmi() && y->IsSmi()) return SMIS; |
| 2479 | if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; |
| 2480 | if (Token::IsOrderedRelationalCompareOp(op_)) { |
| 2481 | // Ordered comparisons treat undefined as NaN, so the |
| 2482 | // HEAP_NUMBER stub will do the right thing. |
| 2483 | if ((x->IsNumber() && y->IsUndefined()) || |
| 2484 | (y->IsNumber() && x->IsUndefined())) { |
| 2485 | return HEAP_NUMBERS; |
| 2486 | } |
| 2487 | } |
| 2488 | if (x->IsSymbol() && y->IsSymbol()) { |
| 2489 | // We compare symbols as strings if we need to determine |
| 2490 | // the order in a non-equality compare. |
| 2491 | return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; |
| 2492 | } |
| 2493 | if (x->IsString() && y->IsString()) return STRINGS; |
| 2494 | if (!Token::IsEqualityOp(op_)) return GENERIC; |
| 2495 | if (x->IsJSObject() && y->IsJSObject()) { |
| 2496 | if (Handle<JSObject>::cast(x)->map() == |
| 2497 | Handle<JSObject>::cast(y)->map() && |
| 2498 | Token::IsEqualityOp(op_)) { |
| 2499 | return KNOWN_OBJECTS; |
| 2500 | } else { |
| 2501 | return OBJECTS; |
| 2502 | } |
| 2503 | } |
| 2504 | return GENERIC; |
| 2505 | case SMIS: |
| 2506 | return has_inlined_smi_code && x->IsNumber() && y->IsNumber() |
| 2507 | ? HEAP_NUMBERS |
| 2508 | : GENERIC; |
| 2509 | case SYMBOLS: |
| 2510 | ASSERT(Token::IsEqualityOp(op_)); |
| 2511 | return x->IsString() && y->IsString() ? STRINGS : GENERIC; |
| 2512 | case HEAP_NUMBERS: |
| 2513 | case STRINGS: |
| 2514 | case OBJECTS: |
| 2515 | case KNOWN_OBJECTS: |
| 2516 | case GENERIC: |
| 2517 | return GENERIC; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2518 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2519 | UNREACHABLE(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2520 | return GENERIC; |
| 2521 | } |
| 2522 | |
| 2523 | |
| 2524 | // Used from ic_<arch>.cc. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2525 | RUNTIME_FUNCTION(Code*, CompareIC_Miss) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2526 | NoHandleAllocation na; |
| 2527 | ASSERT(args.length() == 3); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2528 | CompareIC ic(isolate, static_cast<Token::Value>(args.smi_at(2))); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2529 | ic.UpdateCaches(args.at<Object>(0), args.at<Object>(1)); |
| 2530 | return ic.target(); |
| 2531 | } |
| 2532 | |
| 2533 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 2534 | RUNTIME_FUNCTION(MaybeObject*, ToBoolean_Patch) { |
| 2535 | ASSERT(args.length() == 3); |
| 2536 | |
| 2537 | HandleScope scope(isolate); |
| 2538 | Handle<Object> object = args.at<Object>(0); |
| 2539 | Register tos = Register::from_code(args.smi_at(1)); |
| 2540 | ToBooleanStub::Types old_types(args.smi_at(2)); |
| 2541 | |
| 2542 | ToBooleanStub::Types new_types(old_types); |
| 2543 | bool to_boolean_value = new_types.Record(object); |
| 2544 | old_types.TraceTransition(new_types); |
| 2545 | |
| 2546 | ToBooleanStub stub(tos, new_types); |
| 2547 | Handle<Code> code = stub.GetCode(); |
| 2548 | ToBooleanIC ic(isolate); |
| 2549 | ic.patch(*code); |
| 2550 | return Smi::FromInt(to_boolean_value ? 1 : 0); |
| 2551 | } |
| 2552 | |
| 2553 | |
| 2554 | void ToBooleanIC::patch(Code* code) { |
| 2555 | set_target(code); |
| 2556 | } |
| 2557 | |
| 2558 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2559 | static const Address IC_utilities[] = { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2560 | #define ADDR(name) FUNCTION_ADDR(name), |
| 2561 | IC_UTIL_LIST(ADDR) |
| 2562 | NULL |
| 2563 | #undef ADDR |
| 2564 | }; |
| 2565 | |
| 2566 | |
| 2567 | Address IC::AddressFromUtilityId(IC::UtilityId id) { |
| 2568 | return IC_utilities[id]; |
| 2569 | } |
| 2570 | |
| 2571 | |
| 2572 | } } // namespace v8::internal |