ic.h 20.5 KB
Newer Older
1
// Copyright 2006-2009 the V8 project authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef V8_IC_H_
#define V8_IC_H_

31
#include "macro-assembler.h"
32

33 34
namespace v8 {
namespace internal {
35

36

37 38
// IC_UTIL_LIST defines all utility functions called from generated
// inline caching code. The argument for the macro, ICU, is the function name.
39 40 41 42
#define IC_UTIL_LIST(ICU)                             \
  ICU(LoadIC_Miss)                                    \
  ICU(KeyedLoadIC_Miss)                               \
  ICU(CallIC_Miss)                                    \
43
  ICU(KeyedCallIC_Miss)                               \
44
  ICU(StoreIC_Miss)                                   \
45
  ICU(StoreIC_ArrayLength)                            \
46 47 48 49 50 51 52 53
  ICU(SharedStoreIC_ExtendStorage)                    \
  ICU(KeyedStoreIC_Miss)                              \
  /* Utilities for IC stubs. */                       \
  ICU(LoadCallbackProperty)                           \
  ICU(StoreCallbackProperty)                          \
  ICU(LoadPropertyWithInterceptorOnly)                \
  ICU(LoadPropertyWithInterceptorForLoad)             \
  ICU(LoadPropertyWithInterceptorForCall)             \
54
  ICU(KeyedLoadPropertyWithInterceptor)               \
55
  ICU(StoreInterceptorProperty)                       \
56
  ICU(TypeRecordingUnaryOp_Patch)                     \
57 58
  ICU(TypeRecordingBinaryOp_Patch)                    \
  ICU(CompareIC_Miss)
59
//
60 61
// IC is the base class for LoadIC, StoreIC, CallIC, KeyedLoadIC,
// and KeyedStoreIC.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
//
class IC {
 public:

  // The ids for utility called from the generated code.
  enum UtilityId {
  #define CONST_NAME(name) k##name,
    IC_UTIL_LIST(CONST_NAME)
  #undef CONST_NAME
    kUtilityCount
  };

  // Looks up the address of the named utility.
  static Address AddressFromUtilityId(UtilityId id);

  // Alias the inline cache state type to make the IC code more readable.
  typedef InlineCacheState State;

  // The IC code is either invoked with no extra frames on the stack
  // or with a single extra frame for supporting calls.
  enum FrameDepth {
    NO_EXTRA_FRAME = 0,
    EXTRA_CALL_FRAME = 1
  };

  // Construct the IC structure with the given number of extra
  // JavaScript frames on the stack.
89
  IC(FrameDepth depth, Isolate* isolate);
90 91 92 93 94

  // Get the call-site target; used for determining the state.
  Code* target() { return GetTargetAtAddress(address()); }
  inline Address address();

95 96
  // Compute the current IC state based on the target stub, receiver and name.
  static State StateFrom(Code* target, Object* receiver, Object* name);
97 98 99 100 101 102 103

  // Clear the inline cache to initial state.
  static void Clear(Address address);

  // Computes the reloc info for this IC. This is a fairly expensive
  // operation as it has to search through the heap to find the code
  // object that contains this IC site.
104
  RelocInfo::Mode ComputeMode();
105 106 107

  // Returns if this IC is for contextual (no explicit receiver)
  // access to properties.
108 109 110 111 112 113 114 115 116 117
  bool IsContextual(Handle<Object> receiver) {
    if (receiver->IsGlobalObject()) {
      return SlowIsContextual();
    } else {
      ASSERT(!SlowIsContextual());
      return false;
    }
  }

  bool SlowIsContextual() {
118 119
    return ComputeMode() == RelocInfo::CODE_TARGET_CONTEXT;
  }
120

121 122 123 124 125 126
  // Determines which map must be used for keeping the code stub.
  // These methods should not be called with undefined or null.
  static inline InlineCacheHolderFlag GetCodeCacheForObject(Object* object,
                                                            JSObject* holder);
  static inline InlineCacheHolderFlag GetCodeCacheForObject(JSObject* object,
                                                            JSObject* holder);
127 128
  static inline JSObject* GetCodeCacheHolder(Object* object,
                                             InlineCacheHolderFlag holder);
129 130 131 132

 protected:
  Address fp() const { return fp_; }
  Address pc() const { return *pc_address_; }
133
  Isolate* isolate() const { return isolate_; }
134

135
#ifdef ENABLE_DEBUGGER_SUPPORT
136 137 138
  // Computes the address in the original code when the code running is
  // containing break points (calls to DebugBreakXXX builtins).
  Address OriginalCodeAddress();
139
#endif
140 141 142 143 144 145

  // Set the call-site target.
  void set_target(Code* code) { SetTargetAtAddress(address(), code); }

#ifdef DEBUG
  static void TraceIC(const char* type,
146
                      Handle<Object> name,
147
                      State old_state,
148 149
                      Code* new_target,
                      const char* extra_info = "");
150 151
#endif

152 153 154 155
  Failure* TypeError(const char* type,
                     Handle<Object> object,
                     Handle<Object> key);
  Failure* ReferenceError(const char* type, Handle<String> name);
156 157 158 159 160 161 162 163 164 165 166 167 168 169

  // Access the target code for the given IC address.
  static inline Code* GetTargetAtAddress(Address address);
  static inline void SetTargetAtAddress(Address address, Code* target);

 private:
  // Frame pointer for the frame that uses (calls) the IC.
  Address fp_;

  // All access to the program counter of an IC structure is indirect
  // to make the code GC safe. This feature is crucial since
  // GetProperty and SetProperty are called and they in turn might
  // invoke the garbage collector.
  Address* pc_address_;
170

171 172
  Isolate* isolate_;

173
  DISALLOW_IMPLICIT_CONSTRUCTORS(IC);
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
};


// An IC_Utility encapsulates IC::UtilityId. It exists mainly because you
// cannot make forward declarations to an enum.
class IC_Utility {
 public:
  explicit IC_Utility(IC::UtilityId id)
    : address_(IC::AddressFromUtilityId(id)), id_(id) {}

  Address address() const { return address_; }

  IC::UtilityId id() const { return id_; }
 private:
  Address address_;
  IC::UtilityId id_;
};


193 194
class CallICBase: public IC {
 protected:
195 196
  CallICBase(Code::Kind kind, Isolate* isolate)
      : IC(EXTRA_CALL_FRAME, isolate), kind_(kind) {}
197

198
 public:
199
  MUST_USE_RESULT MaybeObject* LoadFunction(State state,
200
                                            Code::ExtraICState extra_ic_state,
201 202
                                            Handle<Object> object,
                                            Handle<String> name);
203

204 205
 protected:
  Code::Kind kind_;
206

207 208 209 210 211 212 213 214 215 216 217
  bool TryUpdateExtraICState(LookupResult* lookup,
                             Handle<Object> object,
                             Code::ExtraICState* extra_ic_state);

  MUST_USE_RESULT MaybeObject* ComputeMonomorphicStub(
      LookupResult* lookup,
      State state,
      Code::ExtraICState extra_ic_state,
      Handle<Object> object,
      Handle<String> name);

218 219 220
  // Update the inline cache and the global stub cache based on the
  // lookup result.
  void UpdateCaches(LookupResult* lookup,
221
                    State state,
222
                    Code::ExtraICState extra_ic_state,
223 224 225
                    Handle<Object> object,
                    Handle<String> name);

226 227 228 229 230
  // Returns a JSFunction if the object can be called as a function,
  // and patches the stack to be ready for the call.
  // Otherwise, it returns the undefined value.
  Object* TryCallAsFunction(Object* object);

231
  void ReceiverToObjectIfRequired(Handle<Object> callee, Handle<Object> object);
232

233 234 235 236 237
  static void Clear(Address address, Code* target);
  friend class IC;
};


238 239
class CallIC: public CallICBase {
 public:
240 241 242
  explicit CallIC(Isolate* isolate) : CallICBase(Code::CALL_IC, isolate) {
    ASSERT(target()->is_call_stub());
  }
243 244 245 246 247 248 249 250 251 252 253 254 255

  // Code generator routines.
  static void GenerateInitialize(MacroAssembler* masm, int argc) {
    GenerateMiss(masm, argc);
  }
  static void GenerateMiss(MacroAssembler* masm, int argc);
  static void GenerateMegamorphic(MacroAssembler* masm, int argc);
  static void GenerateNormal(MacroAssembler* masm, int argc);
};


class KeyedCallIC: public CallICBase {
 public:
256 257
  explicit KeyedCallIC(Isolate* isolate)
      : CallICBase(Code::KEYED_CALL_IC, isolate) {
258 259 260
    ASSERT(target()->is_keyed_call_stub());
  }

261 262 263
  MUST_USE_RESULT MaybeObject* LoadFunction(State state,
                                            Handle<Object> object,
                                            Handle<Object> key);
264 265 266 267 268 269 270 271 272 273 274

  // Code generator routines.
  static void GenerateInitialize(MacroAssembler* masm, int argc) {
    GenerateMiss(masm, argc);
  }
  static void GenerateMiss(MacroAssembler* masm, int argc);
  static void GenerateMegamorphic(MacroAssembler* masm, int argc);
  static void GenerateNormal(MacroAssembler* masm, int argc);
};


275 276
class LoadIC: public IC {
 public:
277 278 279
  explicit LoadIC(Isolate* isolate) : IC(NO_EXTRA_FRAME, isolate) {
    ASSERT(target()->is_load_stub());
  }
280

281 282 283
  MUST_USE_RESULT MaybeObject* Load(State state,
                                    Handle<Object> object,
                                    Handle<String> name);
284 285

  // Code generator routines.
286 287 288 289
  static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
  static void GeneratePreMonomorphic(MacroAssembler* masm) {
    GenerateMiss(masm);
  }
290 291 292 293 294 295
  static void GenerateMiss(MacroAssembler* masm);
  static void GenerateMegamorphic(MacroAssembler* masm);
  static void GenerateNormal(MacroAssembler* masm);

  // Specialized code generator routines.
  static void GenerateArrayLength(MacroAssembler* masm);
296 297
  static void GenerateStringLength(MacroAssembler* masm,
                                   bool support_wrappers);
298 299 300 301 302 303
  static void GenerateFunctionPrototype(MacroAssembler* masm);

 private:
  // Update the inline cache and the global stub cache based on the
  // lookup result.
  void UpdateCaches(LookupResult* lookup,
304
                    State state,
305 306 307 308
                    Handle<Object> object,
                    Handle<String> name);

  // Stub accessors.
309 310
  Code* megamorphic_stub() {
    return isolate()->builtins()->builtin(
311
        Builtins::kLoadIC_Megamorphic);
312 313
  }
  static Code* initialize_stub() {
314
    return Isolate::Current()->builtins()->builtin(
315
        Builtins::kLoadIC_Initialize);
316
  }
317 318
  Code* pre_monomorphic_stub() {
    return isolate()->builtins()->builtin(
319
        Builtins::kLoadIC_PreMonomorphic);
320 321 322
  }

  static void Clear(Address address, Code* target);
323

324 325 326 327 328 329
  friend class IC;
};


class KeyedLoadIC: public IC {
 public:
330
  explicit KeyedLoadIC(Isolate* isolate) : IC(NO_EXTRA_FRAME, isolate) {
danno@chromium.org's avatar
danno@chromium.org committed
331
    ASSERT(target()->is_keyed_load_stub() ||
332
           target()->is_external_array_load_stub());
333
  }
334

335 336 337
  MUST_USE_RESULT MaybeObject* Load(State state,
                                    Handle<Object> object,
                                    Handle<Object> key);
338 339 340

  // Code generator routines.
  static void GenerateMiss(MacroAssembler* masm);
341 342 343 344 345
  static void GenerateRuntimeGetProperty(MacroAssembler* masm);
  static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
  static void GeneratePreMonomorphic(MacroAssembler* masm) {
    GenerateMiss(masm);
  }
346
  static void GenerateGeneric(MacroAssembler* masm);
347
  static void GenerateString(MacroAssembler* masm);
348

349
  static void GenerateIndexedInterceptor(MacroAssembler* masm);
350

351 352 353 354 355 356 357
  // Bit mask to be tested against bit field for the cases when
  // generic stub should go into slow case.
  // Access check is necessary explicitly since generic stub does not perform
  // map checks.
  static const int kSlowCaseBitFieldMask =
      (1 << Map::kIsAccessCheckNeeded) | (1 << Map::kHasIndexedInterceptor);

358
 private:
359 360
  // Update the inline cache.
  void UpdateCaches(LookupResult* lookup,
361
                    State state,
362 363 364 365 366
                    Handle<Object> object,
                    Handle<String> name);

  // Stub accessors.
  static Code* initialize_stub() {
367
    return Isolate::Current()->builtins()->builtin(
368
        Builtins::kKeyedLoadIC_Initialize);
369
  }
370 371
  Code* megamorphic_stub() {
    return isolate()->builtins()->builtin(
372
        Builtins::kKeyedLoadIC_Generic);
373
  }
374 375
  Code* generic_stub() {
    return isolate()->builtins()->builtin(
376
        Builtins::kKeyedLoadIC_Generic);
377
  }
378 379
  Code* pre_monomorphic_stub() {
    return isolate()->builtins()->builtin(
380
        Builtins::kKeyedLoadIC_PreMonomorphic);
381
  }
382 383
  Code* string_stub() {
    return isolate()->builtins()->builtin(
384
        Builtins::kKeyedLoadIC_String);
385
  }
386

387 388
  Code* indexed_interceptor_stub() {
    return isolate()->builtins()->builtin(
389
        Builtins::kKeyedLoadIC_IndexedInterceptor);
390 391
  }

392
  static void Clear(Address address, Code* target);
393

394 395 396 397 398 399
  friend class IC;
};


class StoreIC: public IC {
 public:
400 401 402
  explicit StoreIC(Isolate* isolate) : IC(NO_EXTRA_FRAME, isolate) {
    ASSERT(target()->is_store_stub());
  }
403

404
  MUST_USE_RESULT MaybeObject* Store(State state,
405
                                     StrictModeFlag strict_mode,
406 407 408
                                     Handle<Object> object,
                                     Handle<String> name,
                                     Handle<Object> value);
409 410

  // Code generators for stub routines. Only called once at startup.
411
  static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
412
  static void GenerateMiss(MacroAssembler* masm);
413
  static void GenerateMegamorphic(MacroAssembler* masm,
414
                                  StrictModeFlag strict_mode);
415
  static void GenerateArrayLength(MacroAssembler* masm);
416
  static void GenerateNormal(MacroAssembler* masm);
417 418
  static void GenerateGlobalProxy(MacroAssembler* masm,
                                  StrictModeFlag strict_mode);
419 420 421 422 423

 private:
  // Update the inline cache and the global stub cache based on the
  // lookup result.
  void UpdateCaches(LookupResult* lookup,
424
                    State state,
425
                    StrictModeFlag strict_mode,
426
                    Handle<JSObject> receiver,
427 428 429
                    Handle<String> name,
                    Handle<Object> value);

430 431 432 433 434 435 436
  void set_target(Code* code) {
    // Strict mode must be preserved across IC patching.
    ASSERT((code->extra_ic_state() & kStrictMode) ==
           (target()->extra_ic_state() & kStrictMode));
    IC::set_target(code);
  }

437
  // Stub accessors.
438 439
  Code* megamorphic_stub() {
    return isolate()->builtins()->builtin(
440
        Builtins::kStoreIC_Megamorphic);
441
  }
442 443
  Code* megamorphic_stub_strict() {
    return isolate()->builtins()->builtin(
444
        Builtins::kStoreIC_Megamorphic_Strict);
445
  }
446
  static Code* initialize_stub() {
447
    return Isolate::Current()->builtins()->builtin(
448
        Builtins::kStoreIC_Initialize);
449
  }
450
  static Code* initialize_stub_strict() {
451
    return Isolate::Current()->builtins()->builtin(
452
        Builtins::kStoreIC_Initialize_Strict);
453
  }
454 455
  Code* global_proxy_stub() {
    return isolate()->builtins()->builtin(
456
        Builtins::kStoreIC_GlobalProxy);
457
  }
458 459
  Code* global_proxy_stub_strict() {
    return isolate()->builtins()->builtin(
460
        Builtins::kStoreIC_GlobalProxy_Strict);
461
  }
462 463

  static void Clear(Address address, Code* target);
464

465 466 467 468 469 470
  friend class IC;
};


class KeyedStoreIC: public IC {
 public:
471
  explicit KeyedStoreIC(Isolate* isolate) : IC(NO_EXTRA_FRAME, isolate) { }
472

473
  MUST_USE_RESULT MaybeObject* Store(State state,
474
                                     StrictModeFlag strict_mode,
475 476 477
                                     Handle<Object> object,
                                     Handle<Object> name,
                                     Handle<Object> value);
478 479

  // Code generators for stub routines.  Only called once at startup.
480
  static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
481
  static void GenerateMiss(MacroAssembler* masm);
482 483 484
  static void GenerateRuntimeSetProperty(MacroAssembler* masm,
                                         StrictModeFlag strict_mode);
  static void GenerateGeneric(MacroAssembler* masm, StrictModeFlag strict_mode);
485 486 487 488

 private:
  // Update the inline cache.
  void UpdateCaches(LookupResult* lookup,
489
                    State state,
490
                    StrictModeFlag strict_mode,
491 492 493 494
                    Handle<JSObject> receiver,
                    Handle<String> name,
                    Handle<Object> value);

495 496 497 498 499 500 501
  void set_target(Code* code) {
    // Strict mode must be preserved across IC patching.
    ASSERT((code->extra_ic_state() & kStrictMode) ==
           (target()->extra_ic_state() & kStrictMode));
    IC::set_target(code);
  }

502 503
  // Stub accessors.
  static Code* initialize_stub() {
504
    return Isolate::Current()->builtins()->builtin(
505
        Builtins::kKeyedStoreIC_Initialize);
506
  }
507 508
  Code* megamorphic_stub() {
    return isolate()->builtins()->builtin(
509
        Builtins::kKeyedStoreIC_Generic);
510
  }
511 512
  static Code* initialize_stub_strict() {
    return Isolate::Current()->builtins()->builtin(
513
        Builtins::kKeyedStoreIC_Initialize_Strict);
514
  }
515 516
  Code* megamorphic_stub_strict() {
    return isolate()->builtins()->builtin(
517
        Builtins::kKeyedStoreIC_Generic_Strict);
518
  }
519 520
  Code* generic_stub() {
    return isolate()->builtins()->builtin(
521
        Builtins::kKeyedStoreIC_Generic);
522
  }
523 524
  Code* generic_stub_strict() {
    return isolate()->builtins()->builtin(
525
        Builtins::kKeyedStoreIC_Generic_Strict);
526
  }
527 528

  static void Clear(Address address, Code* target);
529

530 531 532 533
  friend class IC;
};


534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
class TRUnaryOpIC: public IC {
 public:

  // sorted: increasingly more unspecific (ignoring UNINITIALIZED)
  // TODO(svenpanne) Using enums+switch is an antipattern, use a class instead.
  enum TypeInfo {
    UNINITIALIZED,
    SMI,
    HEAP_NUMBER,
    GENERIC
  };

  explicit TRUnaryOpIC(Isolate* isolate) : IC(NO_EXTRA_FRAME, isolate) { }

  void patch(Code* code);

  static const char* GetName(TypeInfo type_info);

  static State ToState(TypeInfo type_info);

  static TypeInfo GetTypeInfo(Handle<Object> operand);

  static TypeInfo JoinTypes(TypeInfo x, TypeInfo y);
};


560 561 562 563 564 565 566 567 568
// Type Recording BinaryOpIC, that records the types of the inputs and outputs.
class TRBinaryOpIC: public IC {
 public:

  enum TypeInfo {
    UNINITIALIZED,
    SMI,
    INT32,
    HEAP_NUMBER,
569
    ODDBALL,
570
    BOTH_STRING,  // Only used for addition operation.
571 572 573 574
    STRING,  // Only used for addition operation.  At least one string operand.
    GENERIC
  };

575
  explicit TRBinaryOpIC(Isolate* isolate) : IC(NO_EXTRA_FRAME, isolate) { }
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598

  void patch(Code* code);

  static const char* GetName(TypeInfo type_info);

  static State ToState(TypeInfo type_info);

  static TypeInfo GetTypeInfo(Handle<Object> left, Handle<Object> right);

  static TypeInfo JoinTypes(TypeInfo x, TypeInfo y);
};


class CompareIC: public IC {
 public:
  enum State {
    UNINITIALIZED,
    SMIS,
    HEAP_NUMBERS,
    OBJECTS,
    GENERIC
  };

599 600
  CompareIC(Isolate* isolate, Token::Value op)
      : IC(EXTRA_CALL_FRAME, isolate), op_(op) { }
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616

  // Update the inline cache for the given operands.
  void UpdateCaches(Handle<Object> x, Handle<Object> y);

  // Factory method for getting an uninitialized compare stub.
  static Handle<Code> GetUninitialized(Token::Value op);

  // Helper function for computing the condition for a compare operation.
  static Condition ComputeCondition(Token::Value op);

  // Helper function for determining the state of a compare IC.
  static State ComputeState(Code* target);

  static const char* GetStateName(State state);

 private:
617 618
  State TargetState(State state, bool has_inlined_smi_code,
                    Handle<Object> x, Handle<Object> y);
619 620 621 622 623 624 625 626

  bool strict() const { return op_ == Token::EQ_STRICT; }
  Condition GetCondition() const { return ComputeCondition(op_); }
  State GetState() { return ComputeState(target()); }

  Token::Value op_;
};

627 628
// Helper for TRBinaryOpIC and CompareIC.
void PatchInlinedSmiCode(Address address);
629

630 631 632
} }  // namespace v8::internal

#endif  // V8_IC_H_