Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2006-2009 the V8 project authors. All rights reserved. |
| 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 "api.h" |
| 31 | #include "arguments.h" |
| 32 | #include "ic-inl.h" |
| 33 | #include "stub-cache.h" |
| 34 | |
| 35 | namespace v8 { |
| 36 | namespace internal { |
| 37 | |
| 38 | // ----------------------------------------------------------------------- |
| 39 | // StubCache implementation. |
| 40 | |
| 41 | |
| 42 | StubCache::Entry StubCache::primary_[StubCache::kPrimaryTableSize]; |
| 43 | StubCache::Entry StubCache::secondary_[StubCache::kSecondaryTableSize]; |
| 44 | |
| 45 | void StubCache::Initialize(bool create_heap_objects) { |
| 46 | ASSERT(IsPowerOf2(kPrimaryTableSize)); |
| 47 | ASSERT(IsPowerOf2(kSecondaryTableSize)); |
| 48 | if (create_heap_objects) { |
| 49 | HandleScope scope; |
| 50 | Clear(); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | |
| 55 | Code* StubCache::Set(String* name, Map* map, Code* code) { |
| 56 | // Get the flags from the code. |
| 57 | Code::Flags flags = Code::RemoveTypeFromFlags(code->flags()); |
| 58 | |
| 59 | // Validate that the name does not move on scavenge, and that we |
| 60 | // can use identity checks instead of string equality checks. |
| 61 | ASSERT(!Heap::InNewSpace(name)); |
| 62 | ASSERT(name->IsSymbol()); |
| 63 | |
| 64 | // The state bits are not important to the hash function because |
| 65 | // the stub cache only contains monomorphic stubs. Make sure that |
| 66 | // the bits are the least significant so they will be the ones |
| 67 | // masked out. |
| 68 | ASSERT(Code::ExtractICStateFromFlags(flags) == MONOMORPHIC); |
| 69 | ASSERT(Code::kFlagsICStateShift == 0); |
| 70 | |
| 71 | // Make sure that the code type is not included in the hash. |
| 72 | ASSERT(Code::ExtractTypeFromFlags(flags) == 0); |
| 73 | |
| 74 | // Compute the primary entry. |
| 75 | int primary_offset = PrimaryOffset(name, flags, map); |
| 76 | Entry* primary = entry(primary_, primary_offset); |
| 77 | Code* hit = primary->value; |
| 78 | |
| 79 | // If the primary entry has useful data in it, we retire it to the |
| 80 | // secondary cache before overwriting it. |
| 81 | if (hit != Builtins::builtin(Builtins::Illegal)) { |
| 82 | Code::Flags primary_flags = Code::RemoveTypeFromFlags(hit->flags()); |
| 83 | int secondary_offset = |
| 84 | SecondaryOffset(primary->key, primary_flags, primary_offset); |
| 85 | Entry* secondary = entry(secondary_, secondary_offset); |
| 86 | *secondary = *primary; |
| 87 | } |
| 88 | |
| 89 | // Update primary cache. |
| 90 | primary->key = name; |
| 91 | primary->value = code; |
| 92 | return code; |
| 93 | } |
| 94 | |
| 95 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 96 | Object* StubCache::ComputeLoadNonexistent(String* name, JSObject* receiver) { |
| 97 | // If no global objects are present in the prototype chain, the load |
| 98 | // nonexistent IC stub can be shared for all names for a given map |
| 99 | // and we use the empty string for the map cache in that case. If |
| 100 | // there are global objects involved, we need to check global |
| 101 | // property cells in the stub and therefore the stub will be |
| 102 | // specific to the name. |
| 103 | String* cache_name = Heap::empty_string(); |
| 104 | if (receiver->IsGlobalObject()) cache_name = name; |
| 105 | JSObject* last = receiver; |
| 106 | while (last->GetPrototype() != Heap::null_value()) { |
| 107 | last = JSObject::cast(last->GetPrototype()); |
| 108 | if (last->IsGlobalObject()) cache_name = name; |
| 109 | } |
| 110 | // Compile the stub that is either shared for all names or |
| 111 | // name specific if there are global objects involved. |
| 112 | Code::Flags flags = |
| 113 | Code::ComputeMonomorphicFlags(Code::LOAD_IC, NONEXISTENT); |
| 114 | Object* code = receiver->map()->FindInCodeCache(cache_name, flags); |
| 115 | if (code->IsUndefined()) { |
| 116 | LoadStubCompiler compiler; |
| 117 | code = compiler.CompileLoadNonexistent(cache_name, receiver, last); |
| 118 | if (code->IsFailure()) return code; |
| 119 | PROFILE(CodeCreateEvent(Logger::LOAD_IC_TAG, Code::cast(code), cache_name)); |
| 120 | Object* result = |
| 121 | receiver->map()->UpdateCodeCache(cache_name, Code::cast(code)); |
| 122 | if (result->IsFailure()) return result; |
| 123 | } |
| 124 | return Set(name, receiver->map(), Code::cast(code)); |
| 125 | } |
| 126 | |
| 127 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 128 | Object* StubCache::ComputeLoadField(String* name, |
| 129 | JSObject* receiver, |
| 130 | JSObject* holder, |
| 131 | int field_index) { |
| 132 | Code::Flags flags = Code::ComputeMonomorphicFlags(Code::LOAD_IC, FIELD); |
| 133 | Object* code = receiver->map()->FindInCodeCache(name, flags); |
| 134 | if (code->IsUndefined()) { |
| 135 | LoadStubCompiler compiler; |
| 136 | code = compiler.CompileLoadField(receiver, holder, field_index, name); |
| 137 | if (code->IsFailure()) return code; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 138 | PROFILE(CodeCreateEvent(Logger::LOAD_IC_TAG, Code::cast(code), name)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 139 | Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code)); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 140 | if (result->IsFailure()) return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 141 | } |
| 142 | return Set(name, receiver->map(), Code::cast(code)); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | Object* StubCache::ComputeLoadCallback(String* name, |
| 147 | JSObject* receiver, |
| 148 | JSObject* holder, |
| 149 | AccessorInfo* callback) { |
| 150 | ASSERT(v8::ToCData<Address>(callback->getter()) != 0); |
| 151 | Code::Flags flags = Code::ComputeMonomorphicFlags(Code::LOAD_IC, CALLBACKS); |
| 152 | Object* code = receiver->map()->FindInCodeCache(name, flags); |
| 153 | if (code->IsUndefined()) { |
| 154 | LoadStubCompiler compiler; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 155 | code = compiler.CompileLoadCallback(name, receiver, holder, callback); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 156 | if (code->IsFailure()) return code; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 157 | PROFILE(CodeCreateEvent(Logger::LOAD_IC_TAG, Code::cast(code), name)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 158 | Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code)); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 159 | if (result->IsFailure()) return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 160 | } |
| 161 | return Set(name, receiver->map(), Code::cast(code)); |
| 162 | } |
| 163 | |
| 164 | |
| 165 | Object* StubCache::ComputeLoadConstant(String* name, |
| 166 | JSObject* receiver, |
| 167 | JSObject* holder, |
| 168 | Object* value) { |
| 169 | Code::Flags flags = |
| 170 | Code::ComputeMonomorphicFlags(Code::LOAD_IC, CONSTANT_FUNCTION); |
| 171 | Object* code = receiver->map()->FindInCodeCache(name, flags); |
| 172 | if (code->IsUndefined()) { |
| 173 | LoadStubCompiler compiler; |
| 174 | code = compiler.CompileLoadConstant(receiver, holder, value, name); |
| 175 | if (code->IsFailure()) return code; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 176 | PROFILE(CodeCreateEvent(Logger::LOAD_IC_TAG, Code::cast(code), name)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 177 | Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code)); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 178 | if (result->IsFailure()) return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 179 | } |
| 180 | return Set(name, receiver->map(), Code::cast(code)); |
| 181 | } |
| 182 | |
| 183 | |
| 184 | Object* StubCache::ComputeLoadInterceptor(String* name, |
| 185 | JSObject* receiver, |
| 186 | JSObject* holder) { |
| 187 | Code::Flags flags = Code::ComputeMonomorphicFlags(Code::LOAD_IC, INTERCEPTOR); |
| 188 | Object* code = receiver->map()->FindInCodeCache(name, flags); |
| 189 | if (code->IsUndefined()) { |
| 190 | LoadStubCompiler compiler; |
| 191 | code = compiler.CompileLoadInterceptor(receiver, holder, name); |
| 192 | if (code->IsFailure()) return code; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 193 | PROFILE(CodeCreateEvent(Logger::LOAD_IC_TAG, Code::cast(code), name)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 194 | Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code)); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 195 | if (result->IsFailure()) return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 196 | } |
| 197 | return Set(name, receiver->map(), Code::cast(code)); |
| 198 | } |
| 199 | |
| 200 | |
| 201 | Object* StubCache::ComputeLoadNormal(String* name, JSObject* receiver) { |
| 202 | Code* code = Builtins::builtin(Builtins::LoadIC_Normal); |
| 203 | return Set(name, receiver->map(), code); |
| 204 | } |
| 205 | |
| 206 | |
| 207 | Object* StubCache::ComputeLoadGlobal(String* name, |
| 208 | JSObject* receiver, |
| 209 | GlobalObject* holder, |
| 210 | JSGlobalPropertyCell* cell, |
| 211 | bool is_dont_delete) { |
| 212 | Code::Flags flags = Code::ComputeMonomorphicFlags(Code::LOAD_IC, NORMAL); |
| 213 | Object* code = receiver->map()->FindInCodeCache(name, flags); |
| 214 | if (code->IsUndefined()) { |
| 215 | LoadStubCompiler compiler; |
| 216 | code = compiler.CompileLoadGlobal(receiver, |
| 217 | holder, |
| 218 | cell, |
| 219 | name, |
| 220 | is_dont_delete); |
| 221 | if (code->IsFailure()) return code; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 222 | PROFILE(CodeCreateEvent(Logger::LOAD_IC_TAG, Code::cast(code), name)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 223 | Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code)); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 224 | if (result->IsFailure()) return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 225 | } |
| 226 | return Set(name, receiver->map(), Code::cast(code)); |
| 227 | } |
| 228 | |
| 229 | |
| 230 | Object* StubCache::ComputeKeyedLoadField(String* name, |
| 231 | JSObject* receiver, |
| 232 | JSObject* holder, |
| 233 | int field_index) { |
| 234 | Code::Flags flags = Code::ComputeMonomorphicFlags(Code::KEYED_LOAD_IC, FIELD); |
| 235 | Object* code = receiver->map()->FindInCodeCache(name, flags); |
| 236 | if (code->IsUndefined()) { |
| 237 | KeyedLoadStubCompiler compiler; |
| 238 | code = compiler.CompileLoadField(name, receiver, holder, field_index); |
| 239 | if (code->IsFailure()) return code; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 240 | PROFILE(CodeCreateEvent(Logger::KEYED_LOAD_IC_TAG, Code::cast(code), name)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 241 | Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code)); |
| 242 | if (result->IsFailure()) return result; |
| 243 | } |
| 244 | return code; |
| 245 | } |
| 246 | |
| 247 | |
| 248 | Object* StubCache::ComputeKeyedLoadConstant(String* name, |
| 249 | JSObject* receiver, |
| 250 | JSObject* holder, |
| 251 | Object* value) { |
| 252 | Code::Flags flags = |
| 253 | Code::ComputeMonomorphicFlags(Code::KEYED_LOAD_IC, CONSTANT_FUNCTION); |
| 254 | Object* code = receiver->map()->FindInCodeCache(name, flags); |
| 255 | if (code->IsUndefined()) { |
| 256 | KeyedLoadStubCompiler compiler; |
| 257 | code = compiler.CompileLoadConstant(name, receiver, holder, value); |
| 258 | if (code->IsFailure()) return code; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 259 | PROFILE(CodeCreateEvent(Logger::KEYED_LOAD_IC_TAG, Code::cast(code), name)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 260 | Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code)); |
| 261 | if (result->IsFailure()) return result; |
| 262 | } |
| 263 | return code; |
| 264 | } |
| 265 | |
| 266 | |
| 267 | Object* StubCache::ComputeKeyedLoadInterceptor(String* name, |
| 268 | JSObject* receiver, |
| 269 | JSObject* holder) { |
| 270 | Code::Flags flags = |
| 271 | Code::ComputeMonomorphicFlags(Code::KEYED_LOAD_IC, INTERCEPTOR); |
| 272 | Object* code = receiver->map()->FindInCodeCache(name, flags); |
| 273 | if (code->IsUndefined()) { |
| 274 | KeyedLoadStubCompiler compiler; |
| 275 | code = compiler.CompileLoadInterceptor(receiver, holder, name); |
| 276 | if (code->IsFailure()) return code; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 277 | PROFILE(CodeCreateEvent(Logger::KEYED_LOAD_IC_TAG, Code::cast(code), name)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 278 | Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code)); |
| 279 | if (result->IsFailure()) return result; |
| 280 | } |
| 281 | return code; |
| 282 | } |
| 283 | |
| 284 | |
| 285 | Object* StubCache::ComputeKeyedLoadCallback(String* name, |
| 286 | JSObject* receiver, |
| 287 | JSObject* holder, |
| 288 | AccessorInfo* callback) { |
| 289 | Code::Flags flags = |
| 290 | Code::ComputeMonomorphicFlags(Code::KEYED_LOAD_IC, CALLBACKS); |
| 291 | Object* code = receiver->map()->FindInCodeCache(name, flags); |
| 292 | if (code->IsUndefined()) { |
| 293 | KeyedLoadStubCompiler compiler; |
| 294 | code = compiler.CompileLoadCallback(name, receiver, holder, callback); |
| 295 | if (code->IsFailure()) return code; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 296 | PROFILE(CodeCreateEvent(Logger::KEYED_LOAD_IC_TAG, Code::cast(code), name)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 297 | Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code)); |
| 298 | if (result->IsFailure()) return result; |
| 299 | } |
| 300 | return code; |
| 301 | } |
| 302 | |
| 303 | |
| 304 | |
| 305 | Object* StubCache::ComputeKeyedLoadArrayLength(String* name, |
| 306 | JSArray* receiver) { |
| 307 | Code::Flags flags = |
| 308 | Code::ComputeMonomorphicFlags(Code::KEYED_LOAD_IC, CALLBACKS); |
| 309 | Object* code = receiver->map()->FindInCodeCache(name, flags); |
| 310 | if (code->IsUndefined()) { |
| 311 | KeyedLoadStubCompiler compiler; |
| 312 | code = compiler.CompileLoadArrayLength(name); |
| 313 | if (code->IsFailure()) return code; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 314 | PROFILE(CodeCreateEvent(Logger::KEYED_LOAD_IC_TAG, Code::cast(code), name)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 315 | Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code)); |
| 316 | if (result->IsFailure()) return result; |
| 317 | } |
| 318 | return code; |
| 319 | } |
| 320 | |
| 321 | |
| 322 | Object* StubCache::ComputeKeyedLoadStringLength(String* name, |
| 323 | String* receiver) { |
| 324 | Code::Flags flags = |
| 325 | Code::ComputeMonomorphicFlags(Code::KEYED_LOAD_IC, CALLBACKS); |
| 326 | Object* code = receiver->map()->FindInCodeCache(name, flags); |
| 327 | if (code->IsUndefined()) { |
| 328 | KeyedLoadStubCompiler compiler; |
| 329 | code = compiler.CompileLoadStringLength(name); |
| 330 | if (code->IsFailure()) return code; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 331 | PROFILE(CodeCreateEvent(Logger::KEYED_LOAD_IC_TAG, Code::cast(code), name)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 332 | Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code)); |
| 333 | if (result->IsFailure()) return result; |
| 334 | } |
| 335 | return code; |
| 336 | } |
| 337 | |
| 338 | |
| 339 | Object* StubCache::ComputeKeyedLoadFunctionPrototype(String* name, |
| 340 | JSFunction* receiver) { |
| 341 | Code::Flags flags = |
| 342 | Code::ComputeMonomorphicFlags(Code::KEYED_LOAD_IC, CALLBACKS); |
| 343 | Object* code = receiver->map()->FindInCodeCache(name, flags); |
| 344 | if (code->IsUndefined()) { |
| 345 | KeyedLoadStubCompiler compiler; |
| 346 | code = compiler.CompileLoadFunctionPrototype(name); |
| 347 | if (code->IsFailure()) return code; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 348 | PROFILE(CodeCreateEvent(Logger::KEYED_LOAD_IC_TAG, Code::cast(code), name)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 349 | Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code)); |
| 350 | if (result->IsFailure()) return result; |
| 351 | } |
| 352 | return code; |
| 353 | } |
| 354 | |
| 355 | |
| 356 | Object* StubCache::ComputeStoreField(String* name, |
| 357 | JSObject* receiver, |
| 358 | int field_index, |
| 359 | Map* transition) { |
| 360 | PropertyType type = (transition == NULL) ? FIELD : MAP_TRANSITION; |
| 361 | Code::Flags flags = Code::ComputeMonomorphicFlags(Code::STORE_IC, type); |
| 362 | Object* code = receiver->map()->FindInCodeCache(name, flags); |
| 363 | if (code->IsUndefined()) { |
| 364 | StoreStubCompiler compiler; |
| 365 | code = compiler.CompileStoreField(receiver, field_index, transition, name); |
| 366 | if (code->IsFailure()) return code; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 367 | PROFILE(CodeCreateEvent(Logger::STORE_IC_TAG, Code::cast(code), name)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 368 | Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code)); |
| 369 | if (result->IsFailure()) return result; |
| 370 | } |
| 371 | return Set(name, receiver->map(), Code::cast(code)); |
| 372 | } |
| 373 | |
| 374 | |
| 375 | Object* StubCache::ComputeStoreGlobal(String* name, |
| 376 | GlobalObject* receiver, |
| 377 | JSGlobalPropertyCell* cell) { |
| 378 | Code::Flags flags = Code::ComputeMonomorphicFlags(Code::STORE_IC, NORMAL); |
| 379 | Object* code = receiver->map()->FindInCodeCache(name, flags); |
| 380 | if (code->IsUndefined()) { |
| 381 | StoreStubCompiler compiler; |
| 382 | code = compiler.CompileStoreGlobal(receiver, cell, name); |
| 383 | if (code->IsFailure()) return code; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 384 | PROFILE(CodeCreateEvent(Logger::LOAD_IC_TAG, Code::cast(code), name)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 385 | Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code)); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 386 | if (result->IsFailure()) return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 387 | } |
| 388 | return Set(name, receiver->map(), Code::cast(code)); |
| 389 | } |
| 390 | |
| 391 | |
| 392 | Object* StubCache::ComputeStoreCallback(String* name, |
| 393 | JSObject* receiver, |
| 394 | AccessorInfo* callback) { |
| 395 | ASSERT(v8::ToCData<Address>(callback->setter()) != 0); |
| 396 | Code::Flags flags = Code::ComputeMonomorphicFlags(Code::STORE_IC, CALLBACKS); |
| 397 | Object* code = receiver->map()->FindInCodeCache(name, flags); |
| 398 | if (code->IsUndefined()) { |
| 399 | StoreStubCompiler compiler; |
| 400 | code = compiler.CompileStoreCallback(receiver, callback, name); |
| 401 | if (code->IsFailure()) return code; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 402 | PROFILE(CodeCreateEvent(Logger::STORE_IC_TAG, Code::cast(code), name)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 403 | Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code)); |
| 404 | if (result->IsFailure()) return result; |
| 405 | } |
| 406 | return Set(name, receiver->map(), Code::cast(code)); |
| 407 | } |
| 408 | |
| 409 | |
| 410 | Object* StubCache::ComputeStoreInterceptor(String* name, |
| 411 | JSObject* receiver) { |
| 412 | Code::Flags flags = |
| 413 | Code::ComputeMonomorphicFlags(Code::STORE_IC, INTERCEPTOR); |
| 414 | Object* code = receiver->map()->FindInCodeCache(name, flags); |
| 415 | if (code->IsUndefined()) { |
| 416 | StoreStubCompiler compiler; |
| 417 | code = compiler.CompileStoreInterceptor(receiver, name); |
| 418 | if (code->IsFailure()) return code; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 419 | PROFILE(CodeCreateEvent(Logger::STORE_IC_TAG, Code::cast(code), name)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 420 | Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code)); |
| 421 | if (result->IsFailure()) return result; |
| 422 | } |
| 423 | return Set(name, receiver->map(), Code::cast(code)); |
| 424 | } |
| 425 | |
| 426 | |
| 427 | Object* StubCache::ComputeKeyedStoreField(String* name, JSObject* receiver, |
| 428 | int field_index, Map* transition) { |
| 429 | PropertyType type = (transition == NULL) ? FIELD : MAP_TRANSITION; |
| 430 | Code::Flags flags = Code::ComputeMonomorphicFlags(Code::KEYED_STORE_IC, type); |
| 431 | Object* code = receiver->map()->FindInCodeCache(name, flags); |
| 432 | if (code->IsUndefined()) { |
| 433 | KeyedStoreStubCompiler compiler; |
| 434 | code = compiler.CompileStoreField(receiver, field_index, transition, name); |
| 435 | if (code->IsFailure()) return code; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 436 | PROFILE(CodeCreateEvent( |
| 437 | Logger::KEYED_STORE_IC_TAG, Code::cast(code), name)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 438 | Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code)); |
| 439 | if (result->IsFailure()) return result; |
| 440 | } |
| 441 | return code; |
| 442 | } |
| 443 | |
| 444 | |
| 445 | Object* StubCache::ComputeCallConstant(int argc, |
| 446 | InLoopFlag in_loop, |
| 447 | String* name, |
| 448 | Object* object, |
| 449 | JSObject* holder, |
| 450 | JSFunction* function) { |
| 451 | // Compute the check type and the map. |
| 452 | Map* map = IC::GetCodeCacheMapForObject(object); |
| 453 | |
| 454 | // Compute check type based on receiver/holder. |
| 455 | StubCompiler::CheckType check = StubCompiler::RECEIVER_MAP_CHECK; |
| 456 | if (object->IsString()) { |
| 457 | check = StubCompiler::STRING_CHECK; |
| 458 | } else if (object->IsNumber()) { |
| 459 | check = StubCompiler::NUMBER_CHECK; |
| 460 | } else if (object->IsBoolean()) { |
| 461 | check = StubCompiler::BOOLEAN_CHECK; |
| 462 | } |
| 463 | |
| 464 | Code::Flags flags = |
| 465 | Code::ComputeMonomorphicFlags(Code::CALL_IC, |
| 466 | CONSTANT_FUNCTION, |
| 467 | in_loop, |
| 468 | argc); |
| 469 | Object* code = map->FindInCodeCache(name, flags); |
| 470 | if (code->IsUndefined()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 471 | // If the function hasn't been compiled yet, we cannot do it now |
| 472 | // because it may cause GC. To avoid this issue, we return an |
| 473 | // internal error which will make sure we do not update any |
| 474 | // caches. |
| 475 | if (!function->is_compiled()) return Failure::InternalError(); |
| 476 | // Compile the stub - only create stubs for fully compiled functions. |
| 477 | CallStubCompiler compiler(argc, in_loop); |
| 478 | code = compiler.CompileCallConstant(object, holder, function, name, check); |
| 479 | if (code->IsFailure()) return code; |
| 480 | ASSERT_EQ(flags, Code::cast(code)->flags()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 481 | PROFILE(CodeCreateEvent(Logger::CALL_IC_TAG, Code::cast(code), name)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 482 | Object* result = map->UpdateCodeCache(name, Code::cast(code)); |
| 483 | if (result->IsFailure()) return result; |
| 484 | } |
| 485 | return Set(name, map, Code::cast(code)); |
| 486 | } |
| 487 | |
| 488 | |
| 489 | Object* StubCache::ComputeCallField(int argc, |
| 490 | InLoopFlag in_loop, |
| 491 | String* name, |
| 492 | Object* object, |
| 493 | JSObject* holder, |
| 494 | int index) { |
| 495 | // Compute the check type and the map. |
| 496 | Map* map = IC::GetCodeCacheMapForObject(object); |
| 497 | |
| 498 | // TODO(1233596): We cannot do receiver map check for non-JS objects |
| 499 | // because they may be represented as immediates without a |
| 500 | // map. Instead, we check against the map in the holder. |
| 501 | if (object->IsNumber() || object->IsBoolean() || object->IsString()) { |
| 502 | object = holder; |
| 503 | } |
| 504 | |
| 505 | Code::Flags flags = Code::ComputeMonomorphicFlags(Code::CALL_IC, |
| 506 | FIELD, |
| 507 | in_loop, |
| 508 | argc); |
| 509 | Object* code = map->FindInCodeCache(name, flags); |
| 510 | if (code->IsUndefined()) { |
| 511 | CallStubCompiler compiler(argc, in_loop); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 512 | code = compiler.CompileCallField(JSObject::cast(object), |
| 513 | holder, |
| 514 | index, |
| 515 | name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 516 | if (code->IsFailure()) return code; |
| 517 | ASSERT_EQ(flags, Code::cast(code)->flags()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 518 | PROFILE(CodeCreateEvent(Logger::CALL_IC_TAG, Code::cast(code), name)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 519 | Object* result = map->UpdateCodeCache(name, Code::cast(code)); |
| 520 | if (result->IsFailure()) return result; |
| 521 | } |
| 522 | return Set(name, map, Code::cast(code)); |
| 523 | } |
| 524 | |
| 525 | |
| 526 | Object* StubCache::ComputeCallInterceptor(int argc, |
| 527 | String* name, |
| 528 | Object* object, |
| 529 | JSObject* holder) { |
| 530 | // Compute the check type and the map. |
| 531 | // If the object is a value, we use the prototype map for the cache. |
| 532 | Map* map = IC::GetCodeCacheMapForObject(object); |
| 533 | |
| 534 | // TODO(1233596): We cannot do receiver map check for non-JS objects |
| 535 | // because they may be represented as immediates without a |
| 536 | // map. Instead, we check against the map in the holder. |
| 537 | if (object->IsNumber() || object->IsBoolean() || object->IsString()) { |
| 538 | object = holder; |
| 539 | } |
| 540 | |
| 541 | Code::Flags flags = |
| 542 | Code::ComputeMonomorphicFlags(Code::CALL_IC, |
| 543 | INTERCEPTOR, |
| 544 | NOT_IN_LOOP, |
| 545 | argc); |
| 546 | Object* code = map->FindInCodeCache(name, flags); |
| 547 | if (code->IsUndefined()) { |
| 548 | CallStubCompiler compiler(argc, NOT_IN_LOOP); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 549 | code = compiler.CompileCallInterceptor(JSObject::cast(object), |
| 550 | holder, |
| 551 | name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 552 | if (code->IsFailure()) return code; |
| 553 | ASSERT_EQ(flags, Code::cast(code)->flags()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 554 | PROFILE(CodeCreateEvent(Logger::CALL_IC_TAG, Code::cast(code), name)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 555 | Object* result = map->UpdateCodeCache(name, Code::cast(code)); |
| 556 | if (result->IsFailure()) return result; |
| 557 | } |
| 558 | return Set(name, map, Code::cast(code)); |
| 559 | } |
| 560 | |
| 561 | |
| 562 | Object* StubCache::ComputeCallNormal(int argc, |
| 563 | InLoopFlag in_loop, |
| 564 | String* name, |
| 565 | JSObject* receiver) { |
| 566 | Object* code = ComputeCallNormal(argc, in_loop); |
| 567 | if (code->IsFailure()) return code; |
| 568 | return Set(name, receiver->map(), Code::cast(code)); |
| 569 | } |
| 570 | |
| 571 | |
| 572 | Object* StubCache::ComputeCallGlobal(int argc, |
| 573 | InLoopFlag in_loop, |
| 574 | String* name, |
| 575 | JSObject* receiver, |
| 576 | GlobalObject* holder, |
| 577 | JSGlobalPropertyCell* cell, |
| 578 | JSFunction* function) { |
| 579 | Code::Flags flags = |
| 580 | Code::ComputeMonomorphicFlags(Code::CALL_IC, NORMAL, in_loop, argc); |
| 581 | Object* code = receiver->map()->FindInCodeCache(name, flags); |
| 582 | if (code->IsUndefined()) { |
| 583 | // If the function hasn't been compiled yet, we cannot do it now |
| 584 | // because it may cause GC. To avoid this issue, we return an |
| 585 | // internal error which will make sure we do not update any |
| 586 | // caches. |
| 587 | if (!function->is_compiled()) return Failure::InternalError(); |
| 588 | CallStubCompiler compiler(argc, in_loop); |
| 589 | code = compiler.CompileCallGlobal(receiver, holder, cell, function, name); |
| 590 | if (code->IsFailure()) return code; |
| 591 | ASSERT_EQ(flags, Code::cast(code)->flags()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 592 | PROFILE(CodeCreateEvent(Logger::CALL_IC_TAG, Code::cast(code), name)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 593 | Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code)); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 594 | if (result->IsFailure()) return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 595 | } |
| 596 | return Set(name, receiver->map(), Code::cast(code)); |
| 597 | } |
| 598 | |
| 599 | |
| 600 | static Object* GetProbeValue(Code::Flags flags) { |
| 601 | // Use raw_unchecked... so we don't get assert failures during GC. |
| 602 | NumberDictionary* dictionary = Heap::raw_unchecked_non_monomorphic_cache(); |
| 603 | int entry = dictionary->FindEntry(flags); |
| 604 | if (entry != -1) return dictionary->ValueAt(entry); |
| 605 | return Heap::raw_unchecked_undefined_value(); |
| 606 | } |
| 607 | |
| 608 | |
| 609 | static Object* ProbeCache(Code::Flags flags) { |
| 610 | Object* probe = GetProbeValue(flags); |
| 611 | if (probe != Heap::undefined_value()) return probe; |
| 612 | // Seed the cache with an undefined value to make sure that any |
| 613 | // generated code object can always be inserted into the cache |
| 614 | // without causing allocation failures. |
| 615 | Object* result = |
| 616 | Heap::non_monomorphic_cache()->AtNumberPut(flags, |
| 617 | Heap::undefined_value()); |
| 618 | if (result->IsFailure()) return result; |
| 619 | Heap::public_set_non_monomorphic_cache(NumberDictionary::cast(result)); |
| 620 | return probe; |
| 621 | } |
| 622 | |
| 623 | |
| 624 | static Object* FillCache(Object* code) { |
| 625 | if (code->IsCode()) { |
| 626 | int entry = |
| 627 | Heap::non_monomorphic_cache()->FindEntry( |
| 628 | Code::cast(code)->flags()); |
| 629 | // The entry must be present see comment in ProbeCache. |
| 630 | ASSERT(entry != -1); |
| 631 | ASSERT(Heap::non_monomorphic_cache()->ValueAt(entry) == |
| 632 | Heap::undefined_value()); |
| 633 | Heap::non_monomorphic_cache()->ValueAtPut(entry, code); |
| 634 | CHECK(GetProbeValue(Code::cast(code)->flags()) == code); |
| 635 | } |
| 636 | return code; |
| 637 | } |
| 638 | |
| 639 | |
| 640 | Code* StubCache::FindCallInitialize(int argc, InLoopFlag in_loop) { |
| 641 | Code::Flags flags = |
| 642 | Code::ComputeFlags(Code::CALL_IC, in_loop, UNINITIALIZED, NORMAL, argc); |
| 643 | Object* result = ProbeCache(flags); |
| 644 | ASSERT(!result->IsUndefined()); |
| 645 | // This might be called during the marking phase of the collector |
| 646 | // hence the unchecked cast. |
| 647 | return reinterpret_cast<Code*>(result); |
| 648 | } |
| 649 | |
| 650 | |
| 651 | Object* StubCache::ComputeCallInitialize(int argc, InLoopFlag in_loop) { |
| 652 | Code::Flags flags = |
| 653 | Code::ComputeFlags(Code::CALL_IC, in_loop, UNINITIALIZED, NORMAL, argc); |
| 654 | Object* probe = ProbeCache(flags); |
| 655 | if (!probe->IsUndefined()) return probe; |
| 656 | StubCompiler compiler; |
| 657 | return FillCache(compiler.CompileCallInitialize(flags)); |
| 658 | } |
| 659 | |
| 660 | |
| 661 | Object* StubCache::ComputeCallPreMonomorphic(int argc, InLoopFlag in_loop) { |
| 662 | Code::Flags flags = |
| 663 | Code::ComputeFlags(Code::CALL_IC, in_loop, PREMONOMORPHIC, NORMAL, argc); |
| 664 | Object* probe = ProbeCache(flags); |
| 665 | if (!probe->IsUndefined()) return probe; |
| 666 | StubCompiler compiler; |
| 667 | return FillCache(compiler.CompileCallPreMonomorphic(flags)); |
| 668 | } |
| 669 | |
| 670 | |
| 671 | Object* StubCache::ComputeCallNormal(int argc, InLoopFlag in_loop) { |
| 672 | Code::Flags flags = |
| 673 | Code::ComputeFlags(Code::CALL_IC, in_loop, MONOMORPHIC, NORMAL, argc); |
| 674 | Object* probe = ProbeCache(flags); |
| 675 | if (!probe->IsUndefined()) return probe; |
| 676 | StubCompiler compiler; |
| 677 | return FillCache(compiler.CompileCallNormal(flags)); |
| 678 | } |
| 679 | |
| 680 | |
| 681 | Object* StubCache::ComputeCallMegamorphic(int argc, InLoopFlag in_loop) { |
| 682 | Code::Flags flags = |
| 683 | Code::ComputeFlags(Code::CALL_IC, in_loop, MEGAMORPHIC, NORMAL, argc); |
| 684 | Object* probe = ProbeCache(flags); |
| 685 | if (!probe->IsUndefined()) return probe; |
| 686 | StubCompiler compiler; |
| 687 | return FillCache(compiler.CompileCallMegamorphic(flags)); |
| 688 | } |
| 689 | |
| 690 | |
| 691 | Object* StubCache::ComputeCallMiss(int argc) { |
| 692 | Code::Flags flags = |
| 693 | Code::ComputeFlags(Code::STUB, NOT_IN_LOOP, MEGAMORPHIC, NORMAL, argc); |
| 694 | Object* probe = ProbeCache(flags); |
| 695 | if (!probe->IsUndefined()) return probe; |
| 696 | StubCompiler compiler; |
| 697 | return FillCache(compiler.CompileCallMiss(flags)); |
| 698 | } |
| 699 | |
| 700 | |
| 701 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 702 | Object* StubCache::ComputeCallDebugBreak(int argc) { |
| 703 | Code::Flags flags = |
| 704 | Code::ComputeFlags(Code::CALL_IC, NOT_IN_LOOP, DEBUG_BREAK, NORMAL, argc); |
| 705 | Object* probe = ProbeCache(flags); |
| 706 | if (!probe->IsUndefined()) return probe; |
| 707 | StubCompiler compiler; |
| 708 | return FillCache(compiler.CompileCallDebugBreak(flags)); |
| 709 | } |
| 710 | |
| 711 | |
| 712 | Object* StubCache::ComputeCallDebugPrepareStepIn(int argc) { |
| 713 | Code::Flags flags = |
| 714 | Code::ComputeFlags(Code::CALL_IC, |
| 715 | NOT_IN_LOOP, |
| 716 | DEBUG_PREPARE_STEP_IN, |
| 717 | NORMAL, |
| 718 | argc); |
| 719 | Object* probe = ProbeCache(flags); |
| 720 | if (!probe->IsUndefined()) return probe; |
| 721 | StubCompiler compiler; |
| 722 | return FillCache(compiler.CompileCallDebugPrepareStepIn(flags)); |
| 723 | } |
| 724 | #endif |
| 725 | |
| 726 | |
| 727 | Object* StubCache::ComputeLazyCompile(int argc) { |
| 728 | Code::Flags flags = |
| 729 | Code::ComputeFlags(Code::STUB, NOT_IN_LOOP, UNINITIALIZED, NORMAL, argc); |
| 730 | Object* probe = ProbeCache(flags); |
| 731 | if (!probe->IsUndefined()) return probe; |
| 732 | StubCompiler compiler; |
| 733 | Object* result = FillCache(compiler.CompileLazyCompile(flags)); |
| 734 | if (result->IsCode()) { |
| 735 | Code* code = Code::cast(result); |
| 736 | USE(code); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 737 | PROFILE(CodeCreateEvent(Logger::LAZY_COMPILE_TAG, |
| 738 | code, code->arguments_count())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 739 | } |
| 740 | return result; |
| 741 | } |
| 742 | |
| 743 | |
| 744 | void StubCache::Clear() { |
| 745 | for (int i = 0; i < kPrimaryTableSize; i++) { |
| 746 | primary_[i].key = Heap::empty_string(); |
| 747 | primary_[i].value = Builtins::builtin(Builtins::Illegal); |
| 748 | } |
| 749 | for (int j = 0; j < kSecondaryTableSize; j++) { |
| 750 | secondary_[j].key = Heap::empty_string(); |
| 751 | secondary_[j].value = Builtins::builtin(Builtins::Illegal); |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | |
| 756 | // ------------------------------------------------------------------------ |
| 757 | // StubCompiler implementation. |
| 758 | |
| 759 | |
| 760 | // Support function for computing call IC miss stubs. |
| 761 | Handle<Code> ComputeCallMiss(int argc) { |
| 762 | CALL_HEAP_FUNCTION(StubCache::ComputeCallMiss(argc), Code); |
| 763 | } |
| 764 | |
| 765 | |
| 766 | |
| 767 | Object* LoadCallbackProperty(Arguments args) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 768 | ASSERT(args[0]->IsJSObject()); |
| 769 | ASSERT(args[1]->IsJSObject()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 770 | AccessorInfo* callback = AccessorInfo::cast(args[2]); |
| 771 | Address getter_address = v8::ToCData<Address>(callback->getter()); |
| 772 | v8::AccessorGetter fun = FUNCTION_CAST<v8::AccessorGetter>(getter_address); |
| 773 | ASSERT(fun != NULL); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 774 | CustomArguments custom_args(callback->data(), |
| 775 | JSObject::cast(args[0]), |
| 776 | JSObject::cast(args[1])); |
| 777 | v8::AccessorInfo info(custom_args.end()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 778 | HandleScope scope; |
| 779 | v8::Handle<v8::Value> result; |
| 780 | { |
| 781 | // Leaving JavaScript. |
| 782 | VMState state(EXTERNAL); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 783 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 784 | state.set_external_callback(getter_address); |
| 785 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 786 | result = fun(v8::Utils::ToLocal(args.at<String>(4)), info); |
| 787 | } |
| 788 | RETURN_IF_SCHEDULED_EXCEPTION(); |
| 789 | if (result.IsEmpty()) return Heap::undefined_value(); |
| 790 | return *v8::Utils::OpenHandle(*result); |
| 791 | } |
| 792 | |
| 793 | |
| 794 | Object* StoreCallbackProperty(Arguments args) { |
| 795 | JSObject* recv = JSObject::cast(args[0]); |
| 796 | AccessorInfo* callback = AccessorInfo::cast(args[1]); |
| 797 | Address setter_address = v8::ToCData<Address>(callback->setter()); |
| 798 | v8::AccessorSetter fun = FUNCTION_CAST<v8::AccessorSetter>(setter_address); |
| 799 | ASSERT(fun != NULL); |
| 800 | Handle<String> name = args.at<String>(2); |
| 801 | Handle<Object> value = args.at<Object>(3); |
| 802 | HandleScope scope; |
| 803 | LOG(ApiNamedPropertyAccess("store", recv, *name)); |
| 804 | CustomArguments custom_args(callback->data(), recv, recv); |
| 805 | v8::AccessorInfo info(custom_args.end()); |
| 806 | { |
| 807 | // Leaving JavaScript. |
| 808 | VMState state(EXTERNAL); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 809 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 810 | state.set_external_callback(setter_address); |
| 811 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 812 | fun(v8::Utils::ToLocal(name), v8::Utils::ToLocal(value), info); |
| 813 | } |
| 814 | RETURN_IF_SCHEDULED_EXCEPTION(); |
| 815 | return *value; |
| 816 | } |
| 817 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 818 | |
| 819 | static const int kAccessorInfoOffsetInInterceptorArgs = 2; |
| 820 | |
| 821 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 822 | /** |
| 823 | * Attempts to load a property with an interceptor (which must be present), |
| 824 | * but doesn't search the prototype chain. |
| 825 | * |
| 826 | * Returns |Heap::no_interceptor_result_sentinel()| if interceptor doesn't |
| 827 | * provide any value for the given name. |
| 828 | */ |
| 829 | Object* LoadPropertyWithInterceptorOnly(Arguments args) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 830 | Handle<String> name_handle = args.at<String>(0); |
| 831 | Handle<InterceptorInfo> interceptor_info = args.at<InterceptorInfo>(1); |
| 832 | ASSERT(kAccessorInfoOffsetInInterceptorArgs == 2); |
| 833 | ASSERT(args[2]->IsJSObject()); // Receiver. |
| 834 | ASSERT(args[3]->IsJSObject()); // Holder. |
| 835 | ASSERT(args.length() == 5); // Last arg is data object. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 836 | |
| 837 | Address getter_address = v8::ToCData<Address>(interceptor_info->getter()); |
| 838 | v8::NamedPropertyGetter getter = |
| 839 | FUNCTION_CAST<v8::NamedPropertyGetter>(getter_address); |
| 840 | ASSERT(getter != NULL); |
| 841 | |
| 842 | { |
| 843 | // Use the interceptor getter. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 844 | v8::AccessorInfo info(args.arguments() - |
| 845 | kAccessorInfoOffsetInInterceptorArgs); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 846 | HandleScope scope; |
| 847 | v8::Handle<v8::Value> r; |
| 848 | { |
| 849 | // Leaving JavaScript. |
| 850 | VMState state(EXTERNAL); |
| 851 | r = getter(v8::Utils::ToLocal(name_handle), info); |
| 852 | } |
| 853 | RETURN_IF_SCHEDULED_EXCEPTION(); |
| 854 | if (!r.IsEmpty()) { |
| 855 | return *v8::Utils::OpenHandle(*r); |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | return Heap::no_interceptor_result_sentinel(); |
| 860 | } |
| 861 | |
| 862 | |
| 863 | static Object* ThrowReferenceError(String* name) { |
| 864 | // If the load is non-contextual, just return the undefined result. |
| 865 | // Note that both keyed and non-keyed loads may end up here, so we |
| 866 | // can't use either LoadIC or KeyedLoadIC constructors. |
| 867 | IC ic(IC::NO_EXTRA_FRAME); |
| 868 | ASSERT(ic.target()->is_load_stub() || ic.target()->is_keyed_load_stub()); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 869 | if (!ic.SlowIsContextual()) return Heap::undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 870 | |
| 871 | // Throw a reference error. |
| 872 | HandleScope scope; |
| 873 | Handle<String> name_handle(name); |
| 874 | Handle<Object> error = |
| 875 | Factory::NewReferenceError("not_defined", |
| 876 | HandleVector(&name_handle, 1)); |
| 877 | return Top::Throw(*error); |
| 878 | } |
| 879 | |
| 880 | |
| 881 | static Object* LoadWithInterceptor(Arguments* args, |
| 882 | PropertyAttributes* attrs) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 883 | Handle<String> name_handle = args->at<String>(0); |
| 884 | Handle<InterceptorInfo> interceptor_info = args->at<InterceptorInfo>(1); |
| 885 | ASSERT(kAccessorInfoOffsetInInterceptorArgs == 2); |
| 886 | Handle<JSObject> receiver_handle = args->at<JSObject>(2); |
| 887 | Handle<JSObject> holder_handle = args->at<JSObject>(3); |
| 888 | ASSERT(args->length() == 5); // Last arg is data object. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 889 | |
| 890 | Address getter_address = v8::ToCData<Address>(interceptor_info->getter()); |
| 891 | v8::NamedPropertyGetter getter = |
| 892 | FUNCTION_CAST<v8::NamedPropertyGetter>(getter_address); |
| 893 | ASSERT(getter != NULL); |
| 894 | |
| 895 | { |
| 896 | // Use the interceptor getter. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 897 | v8::AccessorInfo info(args->arguments() - |
| 898 | kAccessorInfoOffsetInInterceptorArgs); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 899 | HandleScope scope; |
| 900 | v8::Handle<v8::Value> r; |
| 901 | { |
| 902 | // Leaving JavaScript. |
| 903 | VMState state(EXTERNAL); |
| 904 | r = getter(v8::Utils::ToLocal(name_handle), info); |
| 905 | } |
| 906 | RETURN_IF_SCHEDULED_EXCEPTION(); |
| 907 | if (!r.IsEmpty()) { |
| 908 | *attrs = NONE; |
| 909 | return *v8::Utils::OpenHandle(*r); |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | Object* result = holder_handle->GetPropertyPostInterceptor( |
| 914 | *receiver_handle, |
| 915 | *name_handle, |
| 916 | attrs); |
| 917 | RETURN_IF_SCHEDULED_EXCEPTION(); |
| 918 | return result; |
| 919 | } |
| 920 | |
| 921 | |
| 922 | /** |
| 923 | * Loads a property with an interceptor performing post interceptor |
| 924 | * lookup if interceptor failed. |
| 925 | */ |
| 926 | Object* LoadPropertyWithInterceptorForLoad(Arguments args) { |
| 927 | PropertyAttributes attr = NONE; |
| 928 | Object* result = LoadWithInterceptor(&args, &attr); |
| 929 | if (result->IsFailure()) return result; |
| 930 | |
| 931 | // If the property is present, return it. |
| 932 | if (attr != ABSENT) return result; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 933 | return ThrowReferenceError(String::cast(args[0])); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | |
| 937 | Object* LoadPropertyWithInterceptorForCall(Arguments args) { |
| 938 | PropertyAttributes attr; |
| 939 | Object* result = LoadWithInterceptor(&args, &attr); |
| 940 | RETURN_IF_SCHEDULED_EXCEPTION(); |
| 941 | // This is call IC. In this case, we simply return the undefined result which |
| 942 | // will lead to an exception when trying to invoke the result as a |
| 943 | // function. |
| 944 | return result; |
| 945 | } |
| 946 | |
| 947 | |
| 948 | Object* StoreInterceptorProperty(Arguments args) { |
| 949 | JSObject* recv = JSObject::cast(args[0]); |
| 950 | String* name = String::cast(args[1]); |
| 951 | Object* value = args[2]; |
| 952 | ASSERT(recv->HasNamedInterceptor()); |
| 953 | PropertyAttributes attr = NONE; |
| 954 | Object* result = recv->SetPropertyWithInterceptor(name, value, attr); |
| 955 | return result; |
| 956 | } |
| 957 | |
| 958 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 959 | Object* KeyedLoadPropertyWithInterceptor(Arguments args) { |
| 960 | JSObject* receiver = JSObject::cast(args[0]); |
| 961 | uint32_t index = Smi::cast(args[1])->value(); |
| 962 | return receiver->GetElementWithInterceptor(receiver, index); |
| 963 | } |
| 964 | |
| 965 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 966 | Object* StubCompiler::CompileCallInitialize(Code::Flags flags) { |
| 967 | HandleScope scope; |
| 968 | int argc = Code::ExtractArgumentsCountFromFlags(flags); |
| 969 | CallIC::GenerateInitialize(masm(), argc); |
| 970 | Object* result = GetCodeWithFlags(flags, "CompileCallInitialize"); |
| 971 | if (!result->IsFailure()) { |
| 972 | Counters::call_initialize_stubs.Increment(); |
| 973 | Code* code = Code::cast(result); |
| 974 | USE(code); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 975 | PROFILE(CodeCreateEvent(Logger::CALL_INITIALIZE_TAG, |
| 976 | code, code->arguments_count())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 977 | } |
| 978 | return result; |
| 979 | } |
| 980 | |
| 981 | |
| 982 | Object* StubCompiler::CompileCallPreMonomorphic(Code::Flags flags) { |
| 983 | HandleScope scope; |
| 984 | int argc = Code::ExtractArgumentsCountFromFlags(flags); |
| 985 | // The code of the PreMonomorphic stub is the same as the code |
| 986 | // of the Initialized stub. They just differ on the code object flags. |
| 987 | CallIC::GenerateInitialize(masm(), argc); |
| 988 | Object* result = GetCodeWithFlags(flags, "CompileCallPreMonomorphic"); |
| 989 | if (!result->IsFailure()) { |
| 990 | Counters::call_premonomorphic_stubs.Increment(); |
| 991 | Code* code = Code::cast(result); |
| 992 | USE(code); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 993 | PROFILE(CodeCreateEvent(Logger::CALL_PRE_MONOMORPHIC_TAG, |
| 994 | code, code->arguments_count())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 995 | } |
| 996 | return result; |
| 997 | } |
| 998 | |
| 999 | |
| 1000 | Object* StubCompiler::CompileCallNormal(Code::Flags flags) { |
| 1001 | HandleScope scope; |
| 1002 | int argc = Code::ExtractArgumentsCountFromFlags(flags); |
| 1003 | CallIC::GenerateNormal(masm(), argc); |
| 1004 | Object* result = GetCodeWithFlags(flags, "CompileCallNormal"); |
| 1005 | if (!result->IsFailure()) { |
| 1006 | Counters::call_normal_stubs.Increment(); |
| 1007 | Code* code = Code::cast(result); |
| 1008 | USE(code); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1009 | PROFILE(CodeCreateEvent(Logger::CALL_NORMAL_TAG, |
| 1010 | code, code->arguments_count())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1011 | } |
| 1012 | return result; |
| 1013 | } |
| 1014 | |
| 1015 | |
| 1016 | Object* StubCompiler::CompileCallMegamorphic(Code::Flags flags) { |
| 1017 | HandleScope scope; |
| 1018 | int argc = Code::ExtractArgumentsCountFromFlags(flags); |
| 1019 | CallIC::GenerateMegamorphic(masm(), argc); |
| 1020 | Object* result = GetCodeWithFlags(flags, "CompileCallMegamorphic"); |
| 1021 | if (!result->IsFailure()) { |
| 1022 | Counters::call_megamorphic_stubs.Increment(); |
| 1023 | Code* code = Code::cast(result); |
| 1024 | USE(code); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1025 | PROFILE(CodeCreateEvent(Logger::CALL_MEGAMORPHIC_TAG, |
| 1026 | code, code->arguments_count())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1027 | } |
| 1028 | return result; |
| 1029 | } |
| 1030 | |
| 1031 | |
| 1032 | Object* StubCompiler::CompileCallMiss(Code::Flags flags) { |
| 1033 | HandleScope scope; |
| 1034 | int argc = Code::ExtractArgumentsCountFromFlags(flags); |
| 1035 | CallIC::GenerateMiss(masm(), argc); |
| 1036 | Object* result = GetCodeWithFlags(flags, "CompileCallMiss"); |
| 1037 | if (!result->IsFailure()) { |
| 1038 | Counters::call_megamorphic_stubs.Increment(); |
| 1039 | Code* code = Code::cast(result); |
| 1040 | USE(code); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1041 | PROFILE(CodeCreateEvent(Logger::CALL_MISS_TAG, |
| 1042 | code, code->arguments_count())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1043 | } |
| 1044 | return result; |
| 1045 | } |
| 1046 | |
| 1047 | |
| 1048 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 1049 | Object* StubCompiler::CompileCallDebugBreak(Code::Flags flags) { |
| 1050 | HandleScope scope; |
| 1051 | Debug::GenerateCallICDebugBreak(masm()); |
| 1052 | Object* result = GetCodeWithFlags(flags, "CompileCallDebugBreak"); |
| 1053 | if (!result->IsFailure()) { |
| 1054 | Code* code = Code::cast(result); |
| 1055 | USE(code); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1056 | PROFILE(CodeCreateEvent(Logger::CALL_DEBUG_BREAK_TAG, |
| 1057 | code, code->arguments_count())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1058 | } |
| 1059 | return result; |
| 1060 | } |
| 1061 | |
| 1062 | |
| 1063 | Object* StubCompiler::CompileCallDebugPrepareStepIn(Code::Flags flags) { |
| 1064 | HandleScope scope; |
| 1065 | // Use the same code for the the step in preparations as we do for |
| 1066 | // the miss case. |
| 1067 | int argc = Code::ExtractArgumentsCountFromFlags(flags); |
| 1068 | CallIC::GenerateMiss(masm(), argc); |
| 1069 | Object* result = GetCodeWithFlags(flags, "CompileCallDebugPrepareStepIn"); |
| 1070 | if (!result->IsFailure()) { |
| 1071 | Code* code = Code::cast(result); |
| 1072 | USE(code); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1073 | PROFILE(CodeCreateEvent(Logger::CALL_DEBUG_PREPARE_STEP_IN_TAG, |
| 1074 | code, code->arguments_count())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1075 | } |
| 1076 | return result; |
| 1077 | } |
| 1078 | #endif |
| 1079 | |
| 1080 | |
| 1081 | Object* StubCompiler::GetCodeWithFlags(Code::Flags flags, const char* name) { |
| 1082 | // Check for allocation failures during stub compilation. |
| 1083 | if (failure_->IsFailure()) return failure_; |
| 1084 | |
| 1085 | // Create code object in the heap. |
| 1086 | CodeDesc desc; |
| 1087 | masm_.GetCode(&desc); |
| 1088 | Object* result = Heap::CreateCode(desc, NULL, flags, masm_.CodeObject()); |
| 1089 | #ifdef ENABLE_DISASSEMBLER |
| 1090 | if (FLAG_print_code_stubs && !result->IsFailure()) { |
| 1091 | Code::cast(result)->Disassemble(name); |
| 1092 | } |
| 1093 | #endif |
| 1094 | return result; |
| 1095 | } |
| 1096 | |
| 1097 | |
| 1098 | Object* StubCompiler::GetCodeWithFlags(Code::Flags flags, String* name) { |
| 1099 | if (FLAG_print_code_stubs && (name != NULL)) { |
| 1100 | return GetCodeWithFlags(flags, *name->ToCString()); |
| 1101 | } |
| 1102 | return GetCodeWithFlags(flags, reinterpret_cast<char*>(NULL)); |
| 1103 | } |
| 1104 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1105 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 1106 | void StubCompiler::LookupPostInterceptor(JSObject* holder, |
| 1107 | String* name, |
| 1108 | LookupResult* lookup) { |
| 1109 | holder->LocalLookupRealNamedProperty(name, lookup); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1110 | if (!lookup->IsProperty()) { |
| 1111 | lookup->NotFound(); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 1112 | Object* proto = holder->GetPrototype(); |
| 1113 | if (proto != Heap::null_value()) { |
| 1114 | proto->Lookup(name, lookup); |
| 1115 | } |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1120 | |
| 1121 | Object* LoadStubCompiler::GetCode(PropertyType type, String* name) { |
| 1122 | Code::Flags flags = Code::ComputeMonomorphicFlags(Code::LOAD_IC, type); |
| 1123 | return GetCodeWithFlags(flags, name); |
| 1124 | } |
| 1125 | |
| 1126 | |
| 1127 | Object* KeyedLoadStubCompiler::GetCode(PropertyType type, String* name) { |
| 1128 | Code::Flags flags = Code::ComputeMonomorphicFlags(Code::KEYED_LOAD_IC, type); |
| 1129 | return GetCodeWithFlags(flags, name); |
| 1130 | } |
| 1131 | |
| 1132 | |
| 1133 | Object* StoreStubCompiler::GetCode(PropertyType type, String* name) { |
| 1134 | Code::Flags flags = Code::ComputeMonomorphicFlags(Code::STORE_IC, type); |
| 1135 | return GetCodeWithFlags(flags, name); |
| 1136 | } |
| 1137 | |
| 1138 | |
| 1139 | Object* KeyedStoreStubCompiler::GetCode(PropertyType type, String* name) { |
| 1140 | Code::Flags flags = Code::ComputeMonomorphicFlags(Code::KEYED_STORE_IC, type); |
| 1141 | return GetCodeWithFlags(flags, name); |
| 1142 | } |
| 1143 | |
| 1144 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame^] | 1145 | Object* CallStubCompiler::CompileCustomCall(int generator_id, |
| 1146 | Object* object, |
| 1147 | JSObject* holder, |
| 1148 | JSFunction* function, |
| 1149 | String* fname, |
| 1150 | CheckType check) { |
| 1151 | ASSERT(generator_id >= 0 && generator_id < kNumCallGenerators); |
| 1152 | switch (generator_id) { |
| 1153 | #define CALL_GENERATOR_CASE(ignored1, ignored2, name) \ |
| 1154 | case k##name##CallGenerator: \ |
| 1155 | return CallStubCompiler::Compile##name##Call(object, \ |
| 1156 | holder, \ |
| 1157 | function, \ |
| 1158 | fname, \ |
| 1159 | check); |
| 1160 | CUSTOM_CALL_IC_GENERATORS(CALL_GENERATOR_CASE) |
| 1161 | #undef CALL_GENERATOR_CASE |
| 1162 | } |
| 1163 | UNREACHABLE(); |
| 1164 | return Heap::undefined_value(); |
| 1165 | } |
| 1166 | |
| 1167 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1168 | Object* CallStubCompiler::GetCode(PropertyType type, String* name) { |
| 1169 | int argc = arguments_.immediate(); |
| 1170 | Code::Flags flags = Code::ComputeMonomorphicFlags(Code::CALL_IC, |
| 1171 | type, |
| 1172 | in_loop_, |
| 1173 | argc); |
| 1174 | return GetCodeWithFlags(flags, name); |
| 1175 | } |
| 1176 | |
| 1177 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame^] | 1178 | Object* CallStubCompiler::GetCode(JSFunction* function) { |
| 1179 | String* function_name = NULL; |
| 1180 | if (function->shared()->name()->IsString()) { |
| 1181 | function_name = String::cast(function->shared()->name()); |
| 1182 | } |
| 1183 | return GetCode(CONSTANT_FUNCTION, function_name); |
| 1184 | } |
| 1185 | |
| 1186 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1187 | Object* ConstructStubCompiler::GetCode() { |
| 1188 | Code::Flags flags = Code::ComputeFlags(Code::STUB); |
| 1189 | Object* result = GetCodeWithFlags(flags, "ConstructStub"); |
| 1190 | if (!result->IsFailure()) { |
| 1191 | Code* code = Code::cast(result); |
| 1192 | USE(code); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1193 | PROFILE(CodeCreateEvent(Logger::STUB_TAG, code, "ConstructStub")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1194 | } |
| 1195 | return result; |
| 1196 | } |
| 1197 | |
| 1198 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1199 | CallOptimization::CallOptimization(LookupResult* lookup) { |
| 1200 | if (!lookup->IsProperty() || !lookup->IsCacheable() || |
| 1201 | lookup->type() != CONSTANT_FUNCTION) { |
| 1202 | Initialize(NULL); |
| 1203 | } else { |
| 1204 | // We only optimize constant function calls. |
| 1205 | Initialize(lookup->GetConstantFunction()); |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | CallOptimization::CallOptimization(JSFunction* function) { |
| 1210 | Initialize(function); |
| 1211 | } |
| 1212 | |
| 1213 | |
| 1214 | int CallOptimization::GetPrototypeDepthOfExpectedType(JSObject* object, |
| 1215 | JSObject* holder) const { |
| 1216 | ASSERT(is_simple_api_call_); |
| 1217 | if (expected_receiver_type_ == NULL) return 0; |
| 1218 | int depth = 0; |
| 1219 | while (object != holder) { |
| 1220 | if (object->IsInstanceOf(expected_receiver_type_)) return depth; |
| 1221 | object = JSObject::cast(object->GetPrototype()); |
| 1222 | ++depth; |
| 1223 | } |
| 1224 | if (holder->IsInstanceOf(expected_receiver_type_)) return depth; |
| 1225 | return kInvalidProtoDepth; |
| 1226 | } |
| 1227 | |
| 1228 | |
| 1229 | void CallOptimization::Initialize(JSFunction* function) { |
| 1230 | constant_function_ = NULL; |
| 1231 | is_simple_api_call_ = false; |
| 1232 | expected_receiver_type_ = NULL; |
| 1233 | api_call_info_ = NULL; |
| 1234 | |
| 1235 | if (function == NULL || !function->is_compiled()) return; |
| 1236 | |
| 1237 | constant_function_ = function; |
| 1238 | AnalyzePossibleApiFunction(function); |
| 1239 | } |
| 1240 | |
| 1241 | |
| 1242 | void CallOptimization::AnalyzePossibleApiFunction(JSFunction* function) { |
| 1243 | SharedFunctionInfo* sfi = function->shared(); |
| 1244 | if (!sfi->IsApiFunction()) return; |
| 1245 | FunctionTemplateInfo* info = sfi->get_api_func_data(); |
| 1246 | |
| 1247 | // Require a C++ callback. |
| 1248 | if (info->call_code()->IsUndefined()) return; |
| 1249 | api_call_info_ = CallHandlerInfo::cast(info->call_code()); |
| 1250 | |
| 1251 | // Accept signatures that either have no restrictions at all or |
| 1252 | // only have restrictions on the receiver. |
| 1253 | if (!info->signature()->IsUndefined()) { |
| 1254 | SignatureInfo* signature = SignatureInfo::cast(info->signature()); |
| 1255 | if (!signature->args()->IsUndefined()) return; |
| 1256 | if (!signature->receiver()->IsUndefined()) { |
| 1257 | expected_receiver_type_ = |
| 1258 | FunctionTemplateInfo::cast(signature->receiver()); |
| 1259 | } |
| 1260 | } |
| 1261 | |
| 1262 | is_simple_api_call_ = true; |
| 1263 | } |
| 1264 | |
| 1265 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1266 | } } // namespace v8::internal |