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