code-stubs.h 32.3 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4 5 6 7

#ifndef V8_CODE_STUBS_H_
#define V8_CODE_STUBS_H_

8 9
#include "src/allocation.h"
#include "src/assembler.h"
10
#include "src/factory.h"
11
#include "src/globals.h"
12
#include "src/interface-descriptors.h"
13
#include "src/macro-assembler.h"
14
#include "src/ostreams.h"
15
#include "src/type-hints.h"
16

17 18
namespace v8 {
namespace internal {
19

20 21 22 23 24 25 26
// Forward declarations.
class CodeStubAssembler;
namespace compiler {
class CodeAssemblerLabel;
class CodeAssemblerState;
class Node;
}
27

28
// List of code stubs used on all platforms.
29 30 31 32 33 34 35 36 37 38 39 40
#define CODE_STUB_LIST_ALL_PLATFORMS(V)     \
  /* --- PlatformCodeStubs --- */           \
  V(ArrayConstructor)                       \
  V(CallApiCallback)                        \
  V(CallApiGetter)                          \
  V(CEntry)                                 \
  V(DoubleToI)                              \
  V(InternalArrayConstructor)               \
  V(JSEntry)                                \
  V(MathPow)                                \
  V(ProfileEntryHook)                       \
  /* --- TurboFanCodeStubs --- */           \
41 42
  V(StoreSlowElement)                       \
  V(StoreInArrayLiteralSlow)                \
43 44 45 46 47 48 49 50 51 52 53 54 55 56
  V(ArrayNoArgumentConstructor)             \
  V(ArraySingleArgumentConstructor)         \
  V(ArrayNArgumentsConstructor)             \
  V(InternalArrayNoArgumentConstructor)     \
  V(InternalArraySingleArgumentConstructor) \
  V(ElementsTransitionAndStore)             \
  V(KeyedLoadSloppyArguments)               \
  V(KeyedStoreSloppyArguments)              \
  V(StringAdd)                              \
  V(GetProperty)                            \
  V(StoreFastElement)                       \
  V(StoreInterceptor)                       \
  V(TransitionElementsKind)                 \
  V(LoadIndexedInterceptor)
57

58 59
// List of code stubs only used on ARM 32 bits platforms.
#if V8_TARGET_ARCH_ARM
60
#define CODE_STUB_LIST_ARM(V) V(DirectCEntry)
61

62 63 64 65
#else
#define CODE_STUB_LIST_ARM(V)
#endif

66 67
// List of code stubs only used on ARM 64 bits platforms.
#if V8_TARGET_ARCH_ARM64
68
#define CODE_STUB_LIST_ARM64(V) V(DirectCEntry)
69

70 71 72 73
#else
#define CODE_STUB_LIST_ARM64(V)
#endif

74 75
// List of code stubs only used on PPC platforms.
#ifdef V8_TARGET_ARCH_PPC
76
#define CODE_STUB_LIST_PPC(V) V(DirectCEntry)
77 78 79 80
#else
#define CODE_STUB_LIST_PPC(V)
#endif

81
// List of code stubs only used on MIPS platforms.
82
#if V8_TARGET_ARCH_MIPS
83
#define CODE_STUB_LIST_MIPS(V) V(DirectCEntry)
84
#elif V8_TARGET_ARCH_MIPS64
85
#define CODE_STUB_LIST_MIPS(V) V(DirectCEntry)
86 87 88 89
#else
#define CODE_STUB_LIST_MIPS(V)
#endif

90 91
// List of code stubs only used on S390 platforms.
#ifdef V8_TARGET_ARCH_S390
92
#define CODE_STUB_LIST_S390(V) V(DirectCEntry)
93 94 95 96
#else
#define CODE_STUB_LIST_S390(V)
#endif

97
// Combined list of code stubs.
98 99 100 101 102
#define CODE_STUB_LIST(V)         \
  CODE_STUB_LIST_ALL_PLATFORMS(V) \
  CODE_STUB_LIST_ARM(V)           \
  CODE_STUB_LIST_ARM64(V)         \
  CODE_STUB_LIST_PPC(V)           \
103 104
  CODE_STUB_LIST_MIPS(V)          \
  CODE_STUB_LIST_S390(V)
105

106 107
static const int kHasReturnedMinusZeroSentinel = 1;

108
class CodeStub : public ZoneObject {
109 110
 public:
  enum Major {
111 112 113
    // TODO(mvstanton): eliminate the NoCache key by getting rid
    //                  of the non-monomorphic-cache.
    NoCache = 0,  // marker for stubs that do custom caching]
114 115 116
#define DEF_ENUM(name) name,
    CODE_STUB_LIST(DEF_ENUM)
#undef DEF_ENUM
117 118 119 120
    NUMBER_OF_IDS
  };

  // Retrieve the code for the stub. Generate the code if needed.
121
  Handle<Code> GetCode();
122 123 124

  static Major MajorKeyFromKey(uint32_t key) {
    return static_cast<Major>(MajorKeyBits::decode(key));
125
  }
126
  static uint32_t MinorKeyFromKey(uint32_t key) {
127
    return MinorKeyBits::decode(key);
128
  }
129 130

  // Gets the major key from a code object that is a code stub or binary op IC.
131
  static Major GetMajorKey(Code* code_stub);
132

133 134
  static uint32_t NoCacheKey() { return MajorKeyBits::encode(NoCache); }

135
  static const char* MajorName(Major major_key);
136

137
  explicit CodeStub(Isolate* isolate) : minor_key_(0), isolate_(isolate) {}
138 139
  virtual ~CodeStub() {}

140 141
  static void GenerateStubsAheadOfTime(Isolate* isolate);
  static void GenerateFPStubs(Isolate* isolate);
142

143 144 145 146 147 148 149 150
  // Some stubs put untagged junk on the stack that cannot be scanned by the
  // GC.  This means that we must be statically sure that no GC can occur while
  // they are running.  If that is the case they should override this to return
  // true, which will cause an assertion if we try to call something that can
  // GC or if we try to put a stack frame on top of the junk, which would not
  // result in a traversable stack.
  virtual bool SometimesSetsUpAFrame() { return true; }

151
  // Lookup the code in the (possibly custom) cache.
152
  bool FindCodeInCache(Code** code_out);
153

154
  virtual CallInterfaceDescriptor GetCallInterfaceDescriptor() const = 0;
155

156 157 158
  virtual int GetStackParameterCount() const {
    return GetCallInterfaceDescriptor().GetStackParameterCount();
  }
159

160
  virtual void InitializeDescriptor(CodeStubDescriptor* descriptor) {}
161

162 163
  static void InitializeDescriptor(Isolate* isolate, uint32_t key,
                                   CodeStubDescriptor* desc);
164

165 166
  static MaybeHandle<Code> GetCode(Isolate* isolate, uint32_t key);

167
  // Returns information for computing the number key.
168
  virtual Major MajorKey() const = 0;
169
  uint32_t MinorKey() const { return minor_key_; }
170

171
  friend std::ostream& operator<<(std::ostream& os, const CodeStub& s) {
172 173 174
    s.PrintName(os);
    return os;
  }
175

176
  Isolate* isolate() const { return isolate_; }
177 178 179 180 181
  void set_isolate(Isolate* isolate) {
    DCHECK_NOT_NULL(isolate);
    DCHECK(isolate_ == nullptr || isolate_ == isolate);
    isolate_ = isolate;
  }
182

183 184
  void DeleteStubFromCacheForTesting();

185
 protected:
186 187
  CodeStub(uint32_t key, Isolate* isolate)
      : minor_key_(MinorKeyFromKey(key)), isolate_(isolate) {}
188

189
  // Generates the assembler code for the stub.
190
  virtual Handle<Code> GenerateCode() = 0;
191

192 193
  // Returns whether the code generated for this stub needs to be allocated as
  // a fixed (non-moveable) code object.
194
  virtual Movability NeedsImmovableCode() { return kMovable; }
195

196 197 198
  virtual void PrintName(std::ostream& os) const;        // NOLINT
  virtual void PrintBaseName(std::ostream& os) const;    // NOLINT
  virtual void PrintState(std::ostream& os) const { ; }  // NOLINT
199

200 201
  // Computes the key based on major and minor.
  uint32_t GetKey() {
202
    DCHECK(static_cast<int>(MajorKey()) < NUMBER_OF_IDS);
203 204 205
    return MinorKeyBits::encode(MinorKey()) | MajorKeyBits::encode(MajorKey());
  }

206 207
  uint32_t minor_key_;

208
 private:
209 210
  // Perform bookkeeping required after code generation when stub code is
  // initially generated.
211
  void RecordCodeGeneration(Handle<Code> code);
212

213 214 215 216
  // Activate newly generated stub. Is called after
  // registering stub in the stub cache.
  virtual void Activate(Code* code) { }

217 218 219 220 221 222
  // We use this dispatch to statically instantiate the correct code stub for
  // the given stub key and call the passed function with that code stub.
  typedef void (*DispatchedCall)(CodeStub* stub, void** value_out);
  static void Dispatch(Isolate* isolate, uint32_t key, void** value_out,
                       DispatchedCall call);

223 224
  static void GetCodeDispatchCall(CodeStub* stub, void** value_out);

225
  STATIC_ASSERT(NUMBER_OF_IDS < (1 << kStubMajorKeyBits));
226 227 228
  class MajorKeyBits: public BitField<uint32_t, 0, kStubMajorKeyBits> {};
  class MinorKeyBits: public BitField<uint32_t,
      kStubMajorKeyBits, kStubMinorKeyBits> {};  // NOLINT
229 230

  friend class BreakPointIterator;
231 232

  Isolate* isolate_;
233 234
};

235

236 237 238 239 240 241 242 243
#define DEFINE_CODE_STUB_BASE(NAME, SUPER)                      \
 public:                                                        \
  NAME(uint32_t key, Isolate* isolate) : SUPER(key, isolate) {} \
                                                                \
 private:                                                       \
  DISALLOW_COPY_AND_ASSIGN(NAME)


244
#define DEFINE_CODE_STUB(NAME, SUPER)                      \
245
 public:                                                   \
246
  inline Major MajorKey() const override { return NAME; }; \
247
                                                           \
248 249 250
  DEFINE_CODE_STUB_BASE(NAME##Stub, SUPER)


251 252
#define DEFINE_PLATFORM_CODE_STUB(NAME, SUPER)  \
 private:                                       \
253
  void Generate(MacroAssembler* masm) override; \
254 255 256
  DEFINE_CODE_STUB(NAME, SUPER)


257 258 259
#define DEFINE_TURBOFAN_CODE_STUB(NAME, SUPER)                               \
 public:                                                                     \
  void GenerateAssembly(compiler::CodeAssemblerState* state) const override; \
260 261
  DEFINE_CODE_STUB(NAME, SUPER)

262 263
#define DEFINE_CALL_INTERFACE_DESCRIPTOR(NAME)                          \
 public:                                                                \
264
  typedef NAME##Descriptor Descriptor;                                  \
265
  CallInterfaceDescriptor GetCallInterfaceDescriptor() const override { \
266
    return Descriptor(isolate());                                       \
267 268
  }

269 270 271
// There are some code stubs we just can't describe right now with a
// CallInterfaceDescriptor. Isolate behavior for those cases with this macro.
// An attempt to retrieve a descriptor will fail.
272 273 274 275 276
#define DEFINE_NULL_CALL_INTERFACE_DESCRIPTOR()                         \
 public:                                                                \
  CallInterfaceDescriptor GetCallInterfaceDescriptor() const override { \
    UNREACHABLE();                                                      \
    return CallInterfaceDescriptor();                                   \
277 278
  }

279

280 281 282
class PlatformCodeStub : public CodeStub {
 public:
  // Retrieve the code for the stub. Generate the code if needed.
283
  Handle<Code> GenerateCode() override;
284 285

 protected:
286 287
  explicit PlatformCodeStub(Isolate* isolate) : CodeStub(isolate) {}

288 289
  // Generates the assembler code for the stub.
  virtual void Generate(MacroAssembler* masm) = 0;
290

291
  // Generates the exception handler table for the stub.
292
  virtual int GenerateHandlerTable(MacroAssembler* masm);
293

294
  DEFINE_CODE_STUB_BASE(PlatformCodeStub, CodeStub);
295 296 297
};


298
enum StubFunctionMode { NOT_JS_FUNCTION_STUB_MODE, JS_FUNCTION_STUB_MODE };
299

300

301
class CodeStubDescriptor {
302
 public:
303
  explicit CodeStubDescriptor(CodeStub* stub);
304

305
  CodeStubDescriptor(Isolate* isolate, uint32_t stub_key);
306

307
  void Initialize(Address deoptimization_handler = nullptr,
308 309
                  int hint_stack_parameter_count = -1,
                  StubFunctionMode function_mode = NOT_JS_FUNCTION_STUB_MODE);
310
  void Initialize(Register stack_parameter_count,
311
                  Address deoptimization_handler = nullptr,
312
                  int hint_stack_parameter_count = -1,
313
                  StubFunctionMode function_mode = NOT_JS_FUNCTION_STUB_MODE);
314

315 316 317
  void SetMissHandler(Runtime::FunctionId id) {
    miss_handler_id_ = id;
    miss_handler_ = ExternalReference(Runtime::FunctionForId(id), isolate_);
318
    has_miss_handler_ = true;
319 320
    // Our miss handler infrastructure doesn't currently support
    // variable stack parameter counts.
321
    DCHECK(!stack_parameter_count_.is_valid());
322 323
  }

324
  void set_call_descriptor(CallInterfaceDescriptor d) { call_descriptor_ = d; }
325
  CallInterfaceDescriptor call_descriptor() const { return call_descriptor_; }
326

327 328
  int GetRegisterParameterCount() const {
    return call_descriptor().GetRegisterParameterCount();
329 330
  }

331 332 333 334 335 336 337 338
  int GetStackParameterCount() const {
    return call_descriptor().GetStackParameterCount();
  }

  int GetParameterCount() const {
    return call_descriptor().GetParameterCount();
  }

339 340 341 342
  Register GetRegisterParameter(int index) const {
    return call_descriptor().GetRegisterParameter(index);
  }

343
  MachineType GetParameterType(int index) const {
344
    return call_descriptor().GetParameterType(index);
345 346
  }

347
  ExternalReference miss_handler() const {
348
    DCHECK(has_miss_handler_);
349 350 351
    return miss_handler_;
  }

352 353 354 355 356
  Runtime::FunctionId miss_handler_id() const {
    DCHECK(has_miss_handler_);
    return miss_handler_id_;
  }

357
  bool has_miss_handler() const {
358 359 360
    return has_miss_handler_;
  }

361
  int GetHandlerParameterCount() const {
362 363
    int params = GetParameterCount();
    if (PassesArgumentsToDeoptimizationHandler()) {
364 365 366 367 368
      params += 1;
    }
    return params;
  }

369 370 371 372 373
  int hint_stack_parameter_count() const { return hint_stack_parameter_count_; }
  Register stack_parameter_count() const { return stack_parameter_count_; }
  StubFunctionMode function_mode() const { return function_mode_; }
  Address deoptimization_handler() const { return deoptimization_handler_; }

374
 private:
375 376 377 378
  bool PassesArgumentsToDeoptimizationHandler() const {
    return stack_parameter_count_.is_valid();
  }

379
  Isolate* isolate_;
380
  CallInterfaceDescriptor call_descriptor_;
381 382 383 384 385 386 387 388
  Register stack_parameter_count_;
  // If hint_stack_parameter_count_ > 0, the code stub can optimize the
  // return sequence. Default value is -1, which means it is ignored.
  int hint_stack_parameter_count_;
  StubFunctionMode function_mode_;

  Address deoptimization_handler_;

389
  ExternalReference miss_handler_;
390
  Runtime::FunctionId miss_handler_id_;
391
  bool has_miss_handler_;
392 393 394
};


395 396 397 398 399
class TurboFanCodeStub : public CodeStub {
 public:
  // Retrieve the code for the stub. Generate the code if needed.
  Handle<Code> GenerateCode() override;

400
  int GetStackParameterCount() const override {
401 402 403
    return GetCallInterfaceDescriptor().GetStackParameterCount();
  }

404 405 406
 protected:
  explicit TurboFanCodeStub(Isolate* isolate) : CodeStub(isolate) {}

407
  virtual void GenerateAssembly(compiler::CodeAssemblerState* state) const = 0;
408 409

 private:
410
  DEFINE_CODE_STUB_BASE(TurboFanCodeStub, CodeStub);
411 412
};

413 414
}  // namespace internal
}  // namespace v8
415 416 417

#if V8_TARGET_ARCH_IA32
#elif V8_TARGET_ARCH_X64
418
#elif V8_TARGET_ARCH_ARM64
419
#include "src/arm64/code-stubs-arm64.h"
420
#elif V8_TARGET_ARCH_ARM
421
#include "src/arm/code-stubs-arm.h"
422 423
#elif V8_TARGET_ARCH_PPC
#include "src/ppc/code-stubs-ppc.h"
424
#elif V8_TARGET_ARCH_MIPS
425
#include "src/mips/code-stubs-mips.h"
426 427
#elif V8_TARGET_ARCH_MIPS64
#include "src/mips64/code-stubs-mips64.h"
428 429
#elif V8_TARGET_ARCH_S390
#include "src/s390/code-stubs-s390.h"
430 431 432 433 434 435 436
#else
#error Unsupported target architecture.
#endif

namespace v8 {
namespace internal {

437 438 439 440
class StoreInterceptorStub : public TurboFanCodeStub {
 public:
  explicit StoreInterceptorStub(Isolate* isolate) : TurboFanCodeStub(isolate) {}

441
  DEFINE_CALL_INTERFACE_DESCRIPTOR(StoreWithVector);
442
  DEFINE_TURBOFAN_CODE_STUB(StoreInterceptor, TurboFanCodeStub);
443 444
};

445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
class TransitionElementsKindStub : public TurboFanCodeStub {
 public:
  TransitionElementsKindStub(Isolate* isolate, ElementsKind from_kind,
                             ElementsKind to_kind, bool is_jsarray)
      : TurboFanCodeStub(isolate) {
    set_sub_minor_key(FromKindBits::encode(from_kind) |
                      ToKindBits::encode(to_kind) |
                      IsJSArrayBits::encode(is_jsarray));
  }

  void set_sub_minor_key(uint32_t key) { minor_key_ = key; }

  uint32_t sub_minor_key() const { return minor_key_; }

  ElementsKind from_kind() const {
    return FromKindBits::decode(sub_minor_key());
  }

  ElementsKind to_kind() const { return ToKindBits::decode(sub_minor_key()); }

  bool is_jsarray() const { return IsJSArrayBits::decode(sub_minor_key()); }

 private:
  class ToKindBits : public BitField<ElementsKind, 0, 8> {};
  class FromKindBits : public BitField<ElementsKind, ToKindBits::kNext, 8> {};
  class IsJSArrayBits : public BitField<bool, FromKindBits::kNext, 1> {};

  DEFINE_CALL_INTERFACE_DESCRIPTOR(TransitionElementsKind);
  DEFINE_TURBOFAN_CODE_STUB(TransitionElementsKind, TurboFanCodeStub);
};

476 477 478 479 480 481 482 483 484
class LoadIndexedInterceptorStub : public TurboFanCodeStub {
 public:
  explicit LoadIndexedInterceptorStub(Isolate* isolate)
      : TurboFanCodeStub(isolate) {}

  DEFINE_CALL_INTERFACE_DESCRIPTOR(LoadWithVector);
  DEFINE_TURBOFAN_CODE_STUB(LoadIndexedInterceptor, TurboFanCodeStub);
};

485 486 487 488 489 490 491 492 493
// ES6 [[Get]] operation.
class GetPropertyStub : public TurboFanCodeStub {
 public:
  explicit GetPropertyStub(Isolate* isolate) : TurboFanCodeStub(isolate) {}

  DEFINE_CALL_INTERFACE_DESCRIPTOR(GetProperty);
  DEFINE_TURBOFAN_CODE_STUB(GetProperty, TurboFanCodeStub);
};

494

495 496 497 498 499 500 501
enum AllocationSiteOverrideMode {
  DONT_OVERRIDE,
  DISABLE_ALLOCATION_SITES,
  LAST_ALLOCATION_SITE_OVERRIDE_MODE = DISABLE_ALLOCATION_SITES
};


502 503 504 505 506
class ArrayConstructorStub: public PlatformCodeStub {
 public:
  explicit ArrayConstructorStub(Isolate* isolate);

 private:
507 508
  void GenerateDispatchToArrayStub(MacroAssembler* masm,
                                   AllocationSiteOverrideMode mode);
509

510
  DEFINE_CALL_INTERFACE_DESCRIPTOR(ArrayConstructor);
511
  DEFINE_PLATFORM_CODE_STUB(ArrayConstructor, PlatformCodeStub);
512 513 514
};


515 516 517 518 519 520
class InternalArrayConstructorStub: public PlatformCodeStub {
 public:
  explicit InternalArrayConstructorStub(Isolate* isolate);

 private:
  void GenerateCase(MacroAssembler* masm, ElementsKind kind);
521

522
  DEFINE_CALL_INTERFACE_DESCRIPTOR(ArrayNArgumentsConstructor);
523
  DEFINE_PLATFORM_CODE_STUB(InternalArrayConstructor, PlatformCodeStub);
524 525 526
};


527
class MathPowStub: public PlatformCodeStub {
528
 public:
529
  enum ExponentType { INTEGER, DOUBLE, TAGGED };
530

531
  MathPowStub(Isolate* isolate, ExponentType exponent_type)
532 533 534 535
      : PlatformCodeStub(isolate) {
    minor_key_ = ExponentTypeBits::encode(exponent_type);
  }

536
  CallInterfaceDescriptor GetCallInterfaceDescriptor() const override {
537
    if (exponent_type() == TAGGED) {
538 539 540
      return MathPowTaggedDescriptor(isolate());
    } else if (exponent_type() == INTEGER) {
      return MathPowIntegerDescriptor(isolate());
541 542 543 544
    } else {
      // A CallInterfaceDescriptor doesn't specify double registers (yet).
      DCHECK_EQ(DOUBLE, exponent_type());
      return ContextOnlyDescriptor(isolate());
545 546 547
    }
  }

548
 private:
549 550 551 552 553 554
  ExponentType exponent_type() const {
    return ExponentTypeBits::decode(minor_key_);
  }

  class ExponentTypeBits : public BitField<ExponentType, 0, 2> {};

555
  DEFINE_PLATFORM_CODE_STUB(MathPow, PlatformCodeStub);
556 557
};

558
class KeyedLoadSloppyArgumentsStub : public TurboFanCodeStub {
559 560
 public:
  explicit KeyedLoadSloppyArgumentsStub(Isolate* isolate)
561
      : TurboFanCodeStub(isolate) {}
562

563
 protected:
564
  DEFINE_CALL_INTERFACE_DESCRIPTOR(LoadWithVector);
565
  DEFINE_TURBOFAN_CODE_STUB(KeyedLoadSloppyArguments, TurboFanCodeStub);
566 567 568
};


569 570
class CommonStoreModeBits : public BitField<KeyedAccessStoreMode, 0, 3> {};

571
class KeyedStoreSloppyArgumentsStub : public TurboFanCodeStub {
572
 public:
573 574
  explicit KeyedStoreSloppyArgumentsStub(Isolate* isolate,
                                         KeyedAccessStoreMode mode)
575 576
      : TurboFanCodeStub(isolate) {
    minor_key_ = CommonStoreModeBits::encode(mode);
577
  }
578

579
 protected:
580
  DEFINE_CALL_INTERFACE_DESCRIPTOR(StoreWithVector);
581
  DEFINE_TURBOFAN_CODE_STUB(KeyedStoreSloppyArguments, TurboFanCodeStub);
582 583
};

vogelheim's avatar
vogelheim committed
584
class CallApiCallbackStub : public PlatformCodeStub {
585
 public:
586
  static const int kArgBits = 7;
vogelheim's avatar
vogelheim committed
587
  static const int kArgMax = (1 << kArgBits) - 1;
588

589
  CallApiCallbackStub(Isolate* isolate, int argc)
590
      : PlatformCodeStub(isolate) {
591 592 593
    CHECK_LE(0, argc);
    CHECK_LE(argc, kArgMax);
    minor_key_ = ArgumentBits::encode(argc);
594 595
  }

596
 private:
597
  int argc() const { return ArgumentBits::decode(minor_key_); }
598

599
  class ArgumentBits : public BitField<int, 0, kArgBits> {};
600

601
  DEFINE_CALL_INTERFACE_DESCRIPTOR(ApiCallback);
vogelheim's avatar
vogelheim committed
602
  DEFINE_PLATFORM_CODE_STUB(CallApiCallback, PlatformCodeStub);
603 604 605
};


dcarney@chromium.org's avatar
dcarney@chromium.org committed
606 607
class CallApiGetterStub : public PlatformCodeStub {
 public:
608
  explicit CallApiGetterStub(Isolate* isolate) : PlatformCodeStub(isolate) {}
dcarney@chromium.org's avatar
dcarney@chromium.org committed
609

610
  DEFINE_CALL_INTERFACE_DESCRIPTOR(ApiGetter);
611
  DEFINE_PLATFORM_CODE_STUB(CallApiGetter, PlatformCodeStub);
dcarney@chromium.org's avatar
dcarney@chromium.org committed
612
};
613 614


615
class StringAddStub final : public TurboFanCodeStub {
616
 public:
617
  StringAddStub(Isolate* isolate, StringAddFlags flags,
618
                PretenureFlag pretenure_flag)
619 620 621
      : TurboFanCodeStub(isolate) {
    minor_key_ = (StringAddFlagsBits::encode(flags) |
                  PretenureFlagBits::encode(pretenure_flag));
622
  }
623 624

  StringAddFlags flags() const {
625
    return StringAddFlagsBits::decode(minor_key_);
626 627 628
  }

  PretenureFlag pretenure_flag() const {
629
    return PretenureFlagBits::decode(minor_key_);
630 631 632
  }

 private:
633 634
  class StringAddFlagsBits : public BitField<StringAddFlags, 0, 3> {};
  class PretenureFlagBits : public BitField<PretenureFlag, 3, 1> {};
635

636
  void PrintBaseName(std::ostream& os) const override;  // NOLINT
637

638
  DEFINE_CALL_INTERFACE_DESCRIPTOR(StringAdd);
639
  DEFINE_TURBOFAN_CODE_STUB(StringAdd, TurboFanCodeStub);
640 641 642
};


643
class CEntryStub : public PlatformCodeStub {
644
 public:
645
  CEntryStub(Isolate* isolate, int result_size,
646
             SaveFPRegsMode save_doubles = kDontSaveFPRegs,
647
             ArgvMode argv_mode = kArgvOnStack, bool builtin_exit_frame = false)
648
      : PlatformCodeStub(isolate) {
649
    minor_key_ = SaveDoublesBits::encode(save_doubles == kSaveFPRegs) |
650
                 FrameTypeBits::encode(builtin_exit_frame) |
651
                 ArgvMode::encode(argv_mode == kArgvInRegister);
652
    DCHECK(result_size == 1 || result_size == 2);
653 654
    minor_key_ = ResultSizeBits::update(minor_key_, result_size);
  }
655

656 657
  // The version of this stub that doesn't save doubles is generated ahead of
  // time, so it's OK to call it from other stubs that can't cope with GC during
658 659
  // their code generation.  On machines that always have gp registers (x64) we
  // can generate both variants ahead of time.
660
  static void GenerateAheadOfTime(Isolate* isolate);
661

662
 private:
663
  bool save_doubles() const { return SaveDoublesBits::decode(minor_key_); }
664
  bool argv_in_register() const { return ArgvMode::decode(minor_key_); }
665
  bool is_builtin_exit() const { return FrameTypeBits::decode(minor_key_); }
666
  int result_size() const { return ResultSizeBits::decode(minor_key_); }
667

668
  Movability NeedsImmovableCode() override;
669 670

  class SaveDoublesBits : public BitField<bool, 0, 1> {};
671
  class ArgvMode : public BitField<bool, 1, 1> {};
672 673
  class FrameTypeBits : public BitField<bool, 2, 1> {};
  class ResultSizeBits : public BitField<int, 3, 3> {};
674

675
  DEFINE_NULL_CALL_INTERFACE_DESCRIPTOR();
676
  DEFINE_PLATFORM_CODE_STUB(CEntry, PlatformCodeStub);
677 678
};

679
class JSEntryStub : public PlatformCodeStub {
680
 public:
681
  enum class SpecialTarget { kNone, kRunMicrotasks };
682 683
  JSEntryStub(Isolate* isolate, StackFrame::Type type)
      : PlatformCodeStub(isolate) {
684
    DCHECK(type == StackFrame::ENTRY || type == StackFrame::CONSTRUCT_ENTRY);
685 686 687 688 689 690 691 692
    minor_key_ = StackFrameTypeBits::encode(type) |
                 SpecialTargetBits::encode(SpecialTarget::kNone);
  }

  JSEntryStub(Isolate* isolate, SpecialTarget target)
      : PlatformCodeStub(isolate) {
    minor_key_ = StackFrameTypeBits::encode(StackFrame::ENTRY) |
                 SpecialTargetBits::encode(target);
693
  }
694 695

 private:
696
  int GenerateHandlerTable(MacroAssembler* masm) override;
697

698
  void PrintName(std::ostream& os) const override {  // NOLINT
699 700 701
    os << (type() == StackFrame::ENTRY ? "JSEntryStub"
                                       : "JSConstructEntryStub");
  }
702

703 704 705
  StackFrame::Type type() const {
    return StackFrameTypeBits::decode(minor_key_);
  }
706

707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
  SpecialTarget special_target() const {
    return SpecialTargetBits::decode(minor_key_);
  }

  Handle<Code> EntryTrampoline() {
    switch (special_target()) {
      case SpecialTarget::kNone:
        return (type() == StackFrame::CONSTRUCT_ENTRY)
                   ? BUILTIN_CODE(isolate(), JSConstructEntryTrampoline)
                   : BUILTIN_CODE(isolate(), JSEntryTrampoline);
      case SpecialTarget::kRunMicrotasks:
        return BUILTIN_CODE(isolate(), RunMicrotasks);
    }
    UNREACHABLE();
    return Handle<Code>();
  }

724
  class StackFrameTypeBits : public BitField<StackFrame::Type, 0, 5> {};
725 726
  class SpecialTargetBits
      : public BitField<SpecialTarget, StackFrameTypeBits::kNext, 1> {};
727

728
  int handler_offset_;
729

730
  DEFINE_NULL_CALL_INTERFACE_DESCRIPTOR();
731
  DEFINE_PLATFORM_CODE_STUB(JSEntry, PlatformCodeStub);
732 733
};

734

735 736 737 738 739 740 741 742 743
enum ReceiverCheckMode {
  // We don't know anything about the receiver.
  RECEIVER_IS_UNKNOWN,

  // We know the receiver is a string.
  RECEIVER_IS_STRING
};


744 745 746 747 748 749 750 751
enum EmbedMode {
  // The code being generated is part of an IC handler, which may MISS
  // to an IC in failure cases.
  PART_OF_IC_HANDLER,

  NOT_PART_OF_IC_HANDLER
};

752 753
class DoubleToIStub : public PlatformCodeStub {
 public:
754
  DoubleToIStub(Isolate* isolate, Register destination)
755
      : PlatformCodeStub(isolate) {
756 757
    minor_key_ = DestinationRegisterBits::encode(destination.code()) |
                 SSE3Bits::encode(CpuFeatures::IsSupported(SSE3) ? 1 : 0);
758 759
  }

760
  bool SometimesSetsUpAFrame() override { return false; }
761

762 763 764
 private:
  Register destination() const {
    return Register::from_code(DestinationRegisterBits::decode(minor_key_));
765 766 767 768
  }

  static const int kBitsPerRegisterNumber = 6;
  STATIC_ASSERT((1L << kBitsPerRegisterNumber) >= Register::kNumRegisters);
769
  class DestinationRegisterBits
770 771
      : public BitField<int, 0, kBitsPerRegisterNumber> {};
  class SSE3Bits : public BitField<int, kBitsPerRegisterNumber, 1> {};
772

773
  DEFINE_NULL_CALL_INTERFACE_DESCRIPTOR();
774
  DEFINE_PLATFORM_CODE_STUB(DoubleToI, PlatformCodeStub);
775 776
};

777
class StoreFastElementStub : public TurboFanCodeStub {
778
 public:
779 780
  StoreFastElementStub(Isolate* isolate, bool is_js_array,
                       ElementsKind elements_kind, KeyedAccessStoreMode mode)
781 782 783 784
      : TurboFanCodeStub(isolate) {
    minor_key_ = CommonStoreModeBits::encode(mode) |
                 ElementsKindBits::encode(elements_kind) |
                 IsJSArrayBits::encode(is_js_array);
785 786
  }

787 788
  static void GenerateAheadOfTime(Isolate* isolate);

789
  bool is_js_array() const { return IsJSArrayBits::decode(minor_key_); }
790 791

  ElementsKind elements_kind() const {
792
    return ElementsKindBits::decode(minor_key_);
793 794 795
  }

  KeyedAccessStoreMode store_mode() const {
796
    return CommonStoreModeBits::decode(minor_key_);
797 798 799
  }

 private:
800 801 802
  class ElementsKindBits
      : public BitField<ElementsKind, CommonStoreModeBits::kNext, 8> {};
  class IsJSArrayBits : public BitField<bool, ElementsKindBits::kNext, 1> {};
803

804
  DEFINE_CALL_INTERFACE_DESCRIPTOR(StoreWithVector);
805
  DEFINE_TURBOFAN_CODE_STUB(StoreFastElement, TurboFanCodeStub);
806 807 808
};


809 810 811
class CommonArrayConstructorStub : public TurboFanCodeStub {
 protected:
  CommonArrayConstructorStub(Isolate* isolate, ElementsKind kind,
812
                             AllocationSiteOverrideMode override_mode);
813 814 815 816 817

  void set_sub_minor_key(uint32_t key) { minor_key_ = key; }

  uint32_t sub_minor_key() const { return minor_key_; }

818 819 820 821
  CommonArrayConstructorStub(uint32_t key, Isolate* isolate)
      : TurboFanCodeStub(key, isolate) {}

 public:
822 823 824 825 826 827
  ElementsKind elements_kind() const {
    return ElementsKindBits::decode(sub_minor_key());
  }

  AllocationSiteOverrideMode override_mode() const {
    return AllocationSiteOverrideModeBits::decode(sub_minor_key());
828 829
  }

830 831
  static void GenerateStubsAheadOfTime(Isolate* isolate);

832
 private:
833 834
  // Ensure data fits within available bits.
  STATIC_ASSERT(LAST_ALLOCATION_SITE_OVERRIDE_MODE == 1);
835

836 837 838
  class ElementsKindBits : public BitField<ElementsKind, 0, 8> {};
  class AllocationSiteOverrideModeBits
      : public BitField<AllocationSiteOverrideMode, 8, 1> {};  // NOLINT
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
};

class ArrayNoArgumentConstructorStub : public CommonArrayConstructorStub {
 public:
  ArrayNoArgumentConstructorStub(
      Isolate* isolate, ElementsKind kind,
      AllocationSiteOverrideMode override_mode = DONT_OVERRIDE)
      : CommonArrayConstructorStub(isolate, kind, override_mode) {}

 private:
  void PrintName(std::ostream& os) const override {  // NOLINT
    os << "ArrayNoArgumentConstructorStub";
  }

  DEFINE_CALL_INTERFACE_DESCRIPTOR(ArrayNoArgumentConstructor);
  DEFINE_TURBOFAN_CODE_STUB(ArrayNoArgumentConstructor,
                            CommonArrayConstructorStub);
};

class InternalArrayNoArgumentConstructorStub
    : public CommonArrayConstructorStub {
 public:
  InternalArrayNoArgumentConstructorStub(Isolate* isolate, ElementsKind kind)
      : CommonArrayConstructorStub(isolate, kind, DONT_OVERRIDE) {}

 private:
  void PrintName(std::ostream& os) const override {  // NOLINT
    os << "InternalArrayNoArgumentConstructorStub";
  }
868 869

  DEFINE_CALL_INTERFACE_DESCRIPTOR(ArrayNoArgumentConstructor);
870 871
  DEFINE_TURBOFAN_CODE_STUB(InternalArrayNoArgumentConstructor,
                            CommonArrayConstructorStub);
872
};
873

874
class ArraySingleArgumentConstructorStub : public CommonArrayConstructorStub {
875
 public:
876
  ArraySingleArgumentConstructorStub(
877
      Isolate* isolate, ElementsKind kind,
878
      AllocationSiteOverrideMode override_mode = DONT_OVERRIDE)
879
      : CommonArrayConstructorStub(isolate, kind, override_mode) {}
880 881

 private:
882
  void PrintName(std::ostream& os) const override {  // NOLINT
883
    os << "ArraySingleArgumentConstructorStub";
884 885
  }

886 887 888
  DEFINE_CALL_INTERFACE_DESCRIPTOR(ArraySingleArgumentConstructor);
  DEFINE_TURBOFAN_CODE_STUB(ArraySingleArgumentConstructor,
                            CommonArrayConstructorStub);
889 890
};

891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
class InternalArraySingleArgumentConstructorStub
    : public CommonArrayConstructorStub {
 public:
  InternalArraySingleArgumentConstructorStub(Isolate* isolate,
                                             ElementsKind kind)
      : CommonArrayConstructorStub(isolate, kind, DONT_OVERRIDE) {}

 private:
  void PrintName(std::ostream& os) const override {  // NOLINT
    os << "InternalArraySingleArgumentConstructorStub";
  }

  DEFINE_CALL_INTERFACE_DESCRIPTOR(ArraySingleArgumentConstructor);
  DEFINE_TURBOFAN_CODE_STUB(InternalArraySingleArgumentConstructor,
                            CommonArrayConstructorStub);
};
907

908
class ArrayNArgumentsConstructorStub : public PlatformCodeStub {
909
 public:
910 911
  explicit ArrayNArgumentsConstructorStub(Isolate* isolate)
      : PlatformCodeStub(isolate) {}
912

913 914
  CallInterfaceDescriptor GetCallInterfaceDescriptor() const override {
    return ArrayNArgumentsConstructorDescriptor(isolate());
915
  }
916 917

 private:
918
  DEFINE_PLATFORM_CODE_STUB(ArrayNArgumentsConstructor, PlatformCodeStub);
919 920
};

921
class StoreSlowElementStub : public TurboFanCodeStub {
danno@chromium.org's avatar
danno@chromium.org committed
922
 public:
923 924 925
  StoreSlowElementStub(Isolate* isolate, KeyedAccessStoreMode mode)
      : TurboFanCodeStub(isolate) {
    minor_key_ = CommonStoreModeBits::encode(mode);
926
  }
danno@chromium.org's avatar
danno@chromium.org committed
927

928
 private:
929
  DEFINE_CALL_INTERFACE_DESCRIPTOR(StoreWithVector);
930
  DEFINE_TURBOFAN_CODE_STUB(StoreSlowElement, TurboFanCodeStub);
danno@chromium.org's avatar
danno@chromium.org committed
931 932
};

933 934 935 936 937 938 939 940 941 942 943 944
class StoreInArrayLiteralSlowStub : public TurboFanCodeStub {
 public:
  StoreInArrayLiteralSlowStub(Isolate* isolate, KeyedAccessStoreMode mode)
      : TurboFanCodeStub(isolate) {
    minor_key_ = CommonStoreModeBits::encode(mode);
  }

 private:
  DEFINE_CALL_INTERFACE_DESCRIPTOR(StoreWithVector);
  DEFINE_TURBOFAN_CODE_STUB(StoreInArrayLiteralSlow, TurboFanCodeStub);
};

945
class ElementsTransitionAndStoreStub : public TurboFanCodeStub {
946
 public:
947 948
  ElementsTransitionAndStoreStub(Isolate* isolate, ElementsKind from_kind,
                                 ElementsKind to_kind, bool is_jsarray,
949
                                 KeyedAccessStoreMode store_mode)
950 951 952 953
      : TurboFanCodeStub(isolate) {
    minor_key_ = CommonStoreModeBits::encode(store_mode) |
                 FromBits::encode(from_kind) | ToBits::encode(to_kind) |
                 IsJSArrayBits::encode(is_jsarray);
954
  }
955

956 957 958
  ElementsKind from_kind() const { return FromBits::decode(minor_key_); }
  ElementsKind to_kind() const { return ToBits::decode(minor_key_); }
  bool is_jsarray() const { return IsJSArrayBits::decode(minor_key_); }
959
  KeyedAccessStoreMode store_mode() const {
960
    return CommonStoreModeBits::decode(minor_key_);
961
  }
962 963

 private:
964 965
  class FromBits
      : public BitField<ElementsKind, CommonStoreModeBits::kNext, 8> {};
966 967
  class ToBits : public BitField<ElementsKind, 11, 8> {};
  class IsJSArrayBits : public BitField<bool, 19, 1> {};
968

969 970
  DEFINE_CALL_INTERFACE_DESCRIPTOR(StoreTransition);
  DEFINE_TURBOFAN_CODE_STUB(ElementsTransitionAndStore, TurboFanCodeStub);
971 972 973
};


974
class ProfileEntryHookStub : public PlatformCodeStub {
975
 public:
976
  explicit ProfileEntryHookStub(Isolate* isolate) : PlatformCodeStub(isolate) {}
977 978

  // The profile entry hook function is not allowed to cause a GC.
979
  bool SometimesSetsUpAFrame() override { return false; }
980 981 982

  // Generates a call to the entry hook if it's enabled.
  static void MaybeCallEntryHook(MacroAssembler* masm);
983
  static void MaybeCallEntryHookDelayed(TurboAssembler* tasm, Zone* zone);
984 985 986

 private:
  static void EntryHookTrampoline(intptr_t function,
987 988
                                  intptr_t stack_pointer,
                                  Isolate* isolate);
989

990 991 992
  // ProfileEntryHookStub is called at the start of a function, so it has the
  // same register set.
  DEFINE_CALL_INTERFACE_DESCRIPTOR(CallFunction)
993
  DEFINE_PLATFORM_CODE_STUB(ProfileEntryHook, PlatformCodeStub);
994 995
};

996

997
#undef DEFINE_CALL_INTERFACE_DESCRIPTOR
998
#undef DEFINE_PLATFORM_CODE_STUB
999 1000
#undef DEFINE_CODE_STUB
#undef DEFINE_CODE_STUB_BASE
1001

1002 1003
}  // namespace internal
}  // namespace v8
1004 1005

#endif  // V8_CODE_STUBS_H_