blob: 361d7cfae3ce33f28f2998b20de9feea9f84f166 [file] [log] [blame]
// Copyright 2012 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_CODE_STUBS_H_
#define V8_CODE_STUBS_H_
#include "src/frames.h"
#include "src/interface-descriptors.h"
#include "src/type-hints.h"
namespace v8 {
namespace internal {
// Forward declarations.
class CodeStubDescriptor;
class Isolate;
class MacroAssembler;
class TurboAssembler;
namespace compiler {
class CodeAssemblerState;
}
// List of code stubs used on all platforms.
#define CODE_STUB_LIST(V)
static const int kHasReturnedMinusZeroSentinel = 1;
class CodeStub : public ZoneObject {
public:
enum Major {
// TODO(mvstanton): eliminate the NoCache key by getting rid
// of the non-monomorphic-cache.
NoCache = 0, // marker for stubs that do custom caching]
#define DEF_ENUM(name) name,
CODE_STUB_LIST(DEF_ENUM)
#undef DEF_ENUM
NUMBER_OF_IDS
};
// Retrieve the code for the stub. Generate the code if needed.
Handle<Code> GetCode();
static Major MajorKeyFromKey(uint32_t key) {
return static_cast<Major>(MajorKeyBits::decode(key));
}
static uint32_t MinorKeyFromKey(uint32_t key) {
return MinorKeyBits::decode(key);
}
// Gets the major key from a code object that is a code stub or binary op IC.
static Major GetMajorKey(const Code code_stub);
static uint32_t NoCacheKey() { return MajorKeyBits::encode(NoCache); }
static const char* MajorName(Major major_key);
explicit CodeStub(Isolate* isolate) : minor_key_(0), isolate_(isolate) {}
virtual ~CodeStub() = default;
// 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; }
// Lookup the code in the (possibly custom) cache.
bool FindCodeInCache(Code* code_out);
virtual CallInterfaceDescriptor GetCallInterfaceDescriptor() const = 0;
virtual int GetStackParameterCount() const {
return GetCallInterfaceDescriptor().GetStackParameterCount();
}
static void InitializeDescriptor(Isolate* isolate, uint32_t key,
CodeStubDescriptor* desc);
static MaybeHandle<Code> GetCode(Isolate* isolate, uint32_t key);
// Returns information for computing the number key.
virtual Major MajorKey() const = 0;
uint32_t MinorKey() const { return minor_key_; }
friend std::ostream& operator<<(std::ostream& os, const CodeStub& s) {
s.PrintName(os);
return os;
}
Isolate* isolate() const { return isolate_; }
void set_isolate(Isolate* isolate) {
DCHECK_NOT_NULL(isolate);
DCHECK(isolate_ == nullptr || isolate_ == isolate);
isolate_ = isolate;
}
void DeleteStubFromCacheForTesting();
protected:
CodeStub(uint32_t key, Isolate* isolate)
: minor_key_(MinorKeyFromKey(key)), isolate_(isolate) {}
// Generates the assembler code for the stub.
virtual Handle<Code> GenerateCode() = 0;
// Returns whether the code generated for this stub needs to be allocated as
// a fixed (non-moveable) code object.
// TODO(jgruber): Only required by DirectCEntryStub. Can be removed when/if
// that is ported to a builtin.
virtual Movability NeedsImmovableCode() { return kMovable; }
virtual void PrintName(std::ostream& os) const; // NOLINT
virtual void PrintBaseName(std::ostream& os) const; // NOLINT
virtual void PrintState(std::ostream& os) const { ; } // NOLINT
// Computes the key based on major and minor.
uint32_t GetKey() {
DCHECK(static_cast<int>(MajorKey()) < NUMBER_OF_IDS);
return MinorKeyBits::encode(MinorKey()) | MajorKeyBits::encode(MajorKey());
}
uint32_t minor_key_;
private:
// Perform bookkeeping required after code generation when stub code is
// initially generated.
void RecordCodeGeneration(Handle<Code> code);
// 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);
static void GetCodeDispatchCall(CodeStub* stub, void** value_out);
STATIC_ASSERT(NUMBER_OF_IDS < (1 << kStubMajorKeyBits));
class MajorKeyBits: public BitField<uint32_t, 0, kStubMajorKeyBits> {};
class MinorKeyBits: public BitField<uint32_t,
kStubMajorKeyBits, kStubMinorKeyBits> {}; // NOLINT
friend class BreakPointIterator;
Isolate* isolate_;
};
#define DEFINE_CODE_STUB_BASE(NAME, SUPER) \
public: \
NAME(uint32_t key, Isolate* isolate) : SUPER(key, isolate) {} \
\
private: \
DISALLOW_COPY_AND_ASSIGN(NAME)
#define DEFINE_CODE_STUB(NAME, SUPER) \
public: \
inline Major MajorKey() const override { return NAME; }; \
\
DEFINE_CODE_STUB_BASE(NAME##Stub, SUPER)
#define DEFINE_PLATFORM_CODE_STUB(NAME, SUPER) \
private: \
void Generate(MacroAssembler* masm) override; \
DEFINE_CODE_STUB(NAME, SUPER)
#define DEFINE_TURBOFAN_CODE_STUB(NAME, SUPER) \
public: \
void GenerateAssembly(compiler::CodeAssemblerState* state) const override; \
DEFINE_CODE_STUB(NAME, SUPER)
#define DEFINE_CALL_INTERFACE_DESCRIPTOR(NAME) \
public: \
typedef NAME##Descriptor Descriptor; \
CallInterfaceDescriptor GetCallInterfaceDescriptor() const override { \
return Descriptor(); \
}
// 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.
#define DEFINE_NULL_CALL_INTERFACE_DESCRIPTOR() \
public: \
CallInterfaceDescriptor GetCallInterfaceDescriptor() const override { \
UNREACHABLE(); \
return CallInterfaceDescriptor(); \
}
class PlatformCodeStub : public CodeStub {
public:
// Retrieve the code for the stub. Generate the code if needed.
Handle<Code> GenerateCode() override;
protected:
explicit PlatformCodeStub(Isolate* isolate) : CodeStub(isolate) {}
// Generates the assembler code for the stub.
virtual void Generate(MacroAssembler* masm) = 0;
// Generates the exception handler table for the stub.
virtual int GenerateHandlerTable(MacroAssembler* masm);
DEFINE_CODE_STUB_BASE(PlatformCodeStub, CodeStub);
};
enum StubFunctionMode { NOT_JS_FUNCTION_STUB_MODE, JS_FUNCTION_STUB_MODE };
class CodeStubDescriptor {
public:
explicit CodeStubDescriptor(CodeStub* stub);
CodeStubDescriptor(Isolate* isolate, uint32_t stub_key);
void Initialize(Address deoptimization_handler = kNullAddress,
int hint_stack_parameter_count = -1,
StubFunctionMode function_mode = NOT_JS_FUNCTION_STUB_MODE);
void Initialize(Register stack_parameter_count,
Address deoptimization_handler = kNullAddress,
int hint_stack_parameter_count = -1,
StubFunctionMode function_mode = NOT_JS_FUNCTION_STUB_MODE);
void SetMissHandler(Runtime::FunctionId id) {
miss_handler_id_ = id;
miss_handler_ = ExternalReference::Create(Runtime::FunctionForId(id));
has_miss_handler_ = true;
// Our miss handler infrastructure doesn't currently support
// variable stack parameter counts.
DCHECK(!stack_parameter_count_.is_valid());
}
void set_call_descriptor(CallInterfaceDescriptor d) {
call_descriptor_ = std::move(d);
}
CallInterfaceDescriptor call_descriptor() const { return call_descriptor_; }
int GetRegisterParameterCount() const {
return call_descriptor().GetRegisterParameterCount();
}
int GetStackParameterCount() const {
return call_descriptor().GetStackParameterCount();
}
int GetParameterCount() const {
return call_descriptor().GetParameterCount();
}
Register GetRegisterParameter(int index) const {
return call_descriptor().GetRegisterParameter(index);
}
MachineType GetParameterType(int index) const {
return call_descriptor().GetParameterType(index);
}
ExternalReference miss_handler() const {
DCHECK(has_miss_handler_);
return miss_handler_;
}
Runtime::FunctionId miss_handler_id() const {
DCHECK(has_miss_handler_);
return miss_handler_id_;
}
bool has_miss_handler() const {
return has_miss_handler_;
}
int GetHandlerParameterCount() const {
int params = GetParameterCount();
if (PassesArgumentsToDeoptimizationHandler()) {
params += 1;
}
return params;
}
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_; }
private:
bool PassesArgumentsToDeoptimizationHandler() const {
return stack_parameter_count_.is_valid();
}
Isolate* isolate_;
CallInterfaceDescriptor call_descriptor_;
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_;
ExternalReference miss_handler_;
Runtime::FunctionId miss_handler_id_;
bool has_miss_handler_;
};
} // namespace internal
} // namespace v8
#undef DEFINE_CALL_INTERFACE_DESCRIPTOR
#undef DEFINE_PLATFORM_CODE_STUB
#undef DEFINE_CODE_STUB
#undef DEFINE_CODE_STUB_BASE
#endif // V8_CODE_STUBS_H_