2015-10-01 17:22:58 +00:00
|
|
|
// Copyright 2015 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_INTERPRETER_BYTECODE_TRAITS_H_
|
|
|
|
#define V8_INTERPRETER_BYTECODE_TRAITS_H_
|
|
|
|
|
|
|
|
#include "src/interpreter/bytecodes.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace interpreter {
|
|
|
|
|
|
|
|
// TODO(rmcilroy): consider simplifying this to avoid the template magic.
|
|
|
|
|
|
|
|
// Template helpers to deduce the number of operands each bytecode has.
|
|
|
|
#define OPERAND_TERM OperandType::kNone, OperandType::kNone, OperandType::kNone
|
|
|
|
|
2016-03-21 17:08:21 +00:00
|
|
|
template <OperandTypeInfo>
|
|
|
|
struct OperandTypeInfoTraits {
|
|
|
|
static const bool kIsScalable = false;
|
|
|
|
static const bool kIsUnsigned = false;
|
|
|
|
static const OperandSize kUnscaledSize = OperandSize::kNone;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define DECLARE_OPERAND_TYPE_INFO(Name, Scalable, Unsigned, BaseSize) \
|
|
|
|
template <> \
|
|
|
|
struct OperandTypeInfoTraits<OperandTypeInfo::k##Name> { \
|
|
|
|
static const bool kIsScalable = Scalable; \
|
|
|
|
static const bool kIsUnsigned = Unsigned; \
|
|
|
|
static const OperandSize kUnscaledSize = BaseSize; \
|
|
|
|
};
|
|
|
|
OPERAND_TYPE_INFO_LIST(DECLARE_OPERAND_TYPE_INFO)
|
|
|
|
#undef DECLARE_OPERAND_TYPE_INFO
|
|
|
|
|
2015-10-01 17:22:58 +00:00
|
|
|
template <OperandType>
|
2016-03-21 17:08:21 +00:00
|
|
|
struct OperandTraits {
|
|
|
|
typedef OperandTypeInfoTraits<OperandTypeInfo::kNone> TypeInfo;
|
|
|
|
};
|
2015-10-01 17:22:58 +00:00
|
|
|
|
2016-03-21 17:08:21 +00:00
|
|
|
#define DECLARE_OPERAND_TYPE_TRAITS(Name, InfoType) \
|
|
|
|
template <> \
|
|
|
|
struct OperandTraits<OperandType::k##Name> { \
|
|
|
|
typedef OperandTypeInfoTraits<InfoType> TypeInfo; \
|
2015-10-01 17:22:58 +00:00
|
|
|
};
|
2016-03-21 17:08:21 +00:00
|
|
|
OPERAND_TYPE_LIST(DECLARE_OPERAND_TYPE_TRAITS)
|
|
|
|
#undef DECLARE_OPERAND_TYPE_TRAITS
|
2015-10-01 17:22:58 +00:00
|
|
|
|
2016-01-26 13:55:28 +00:00
|
|
|
template <OperandType>
|
|
|
|
struct RegisterOperandTraits {
|
|
|
|
static const int kIsRegisterOperand = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define DECLARE_REGISTER_OPERAND(Name, _) \
|
|
|
|
template <> \
|
|
|
|
struct RegisterOperandTraits<OperandType::k##Name> { \
|
|
|
|
static const int kIsRegisterOperand = 1; \
|
|
|
|
};
|
|
|
|
REGISTER_OPERAND_TYPE_LIST(DECLARE_REGISTER_OPERAND)
|
|
|
|
#undef DECLARE_REGISTER_OPERAND
|
2015-10-01 17:22:58 +00:00
|
|
|
|
|
|
|
template <OperandType... Args>
|
|
|
|
struct BytecodeTraits {};
|
|
|
|
|
|
|
|
template <OperandType operand_0, OperandType operand_1, OperandType operand_2,
|
|
|
|
OperandType operand_3>
|
|
|
|
struct BytecodeTraits<operand_0, operand_1, operand_2, operand_3,
|
|
|
|
OPERAND_TERM> {
|
|
|
|
static OperandType GetOperandType(int i) {
|
|
|
|
DCHECK(0 <= i && i < kOperandCount);
|
|
|
|
const OperandType kOperands[] = {operand_0, operand_1, operand_2,
|
|
|
|
operand_3};
|
|
|
|
return kOperands[i];
|
|
|
|
}
|
|
|
|
|
2016-01-26 13:55:28 +00:00
|
|
|
template <OperandType ot>
|
|
|
|
static inline bool HasAnyOperandsOfType() {
|
|
|
|
return operand_0 == ot || operand_1 == ot || operand_2 == ot ||
|
|
|
|
operand_3 == ot;
|
|
|
|
}
|
|
|
|
|
2016-03-21 17:08:21 +00:00
|
|
|
static inline bool IsScalable() {
|
|
|
|
return (OperandTraits<operand_0>::TypeInfo::kIsScalable |
|
|
|
|
OperandTraits<operand_1>::TypeInfo::kIsScalable |
|
|
|
|
OperandTraits<operand_2>::TypeInfo::kIsScalable |
|
|
|
|
OperandTraits<operand_3>::TypeInfo::kIsScalable);
|
|
|
|
}
|
|
|
|
|
2015-10-01 17:22:58 +00:00
|
|
|
static const int kOperandCount = 4;
|
2016-01-26 13:55:28 +00:00
|
|
|
static const int kRegisterOperandCount =
|
|
|
|
RegisterOperandTraits<operand_0>::kIsRegisterOperand +
|
|
|
|
RegisterOperandTraits<operand_1>::kIsRegisterOperand +
|
|
|
|
RegisterOperandTraits<operand_2>::kIsRegisterOperand +
|
|
|
|
RegisterOperandTraits<operand_3>::kIsRegisterOperand;
|
|
|
|
static const int kRegisterOperandBitmap =
|
|
|
|
RegisterOperandTraits<operand_0>::kIsRegisterOperand +
|
|
|
|
(RegisterOperandTraits<operand_1>::kIsRegisterOperand << 1) +
|
|
|
|
(RegisterOperandTraits<operand_2>::kIsRegisterOperand << 2) +
|
|
|
|
(RegisterOperandTraits<operand_3>::kIsRegisterOperand << 3);
|
2015-10-01 17:22:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <OperandType operand_0, OperandType operand_1, OperandType operand_2>
|
|
|
|
struct BytecodeTraits<operand_0, operand_1, operand_2, OPERAND_TERM> {
|
|
|
|
static inline OperandType GetOperandType(int i) {
|
|
|
|
DCHECK(0 <= i && i <= 2);
|
|
|
|
const OperandType kOperands[] = {operand_0, operand_1, operand_2};
|
|
|
|
return kOperands[i];
|
|
|
|
}
|
|
|
|
|
2016-01-26 13:55:28 +00:00
|
|
|
template <OperandType ot>
|
|
|
|
static inline bool HasAnyOperandsOfType() {
|
|
|
|
return operand_0 == ot || operand_1 == ot || operand_2 == ot;
|
|
|
|
}
|
|
|
|
|
2016-03-21 17:08:21 +00:00
|
|
|
static inline bool IsScalable() {
|
|
|
|
return (OperandTraits<operand_0>::TypeInfo::kIsScalable |
|
|
|
|
OperandTraits<operand_1>::TypeInfo::kIsScalable |
|
|
|
|
OperandTraits<operand_2>::TypeInfo::kIsScalable);
|
|
|
|
}
|
|
|
|
|
2015-10-01 17:22:58 +00:00
|
|
|
static const int kOperandCount = 3;
|
2016-01-26 13:55:28 +00:00
|
|
|
static const int kRegisterOperandCount =
|
|
|
|
RegisterOperandTraits<operand_0>::kIsRegisterOperand +
|
|
|
|
RegisterOperandTraits<operand_1>::kIsRegisterOperand +
|
|
|
|
RegisterOperandTraits<operand_2>::kIsRegisterOperand;
|
|
|
|
static const int kRegisterOperandBitmap =
|
|
|
|
RegisterOperandTraits<operand_0>::kIsRegisterOperand +
|
|
|
|
(RegisterOperandTraits<operand_1>::kIsRegisterOperand << 1) +
|
|
|
|
(RegisterOperandTraits<operand_2>::kIsRegisterOperand << 2);
|
2015-10-01 17:22:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <OperandType operand_0, OperandType operand_1>
|
|
|
|
struct BytecodeTraits<operand_0, operand_1, OPERAND_TERM> {
|
|
|
|
static inline OperandType GetOperandType(int i) {
|
|
|
|
DCHECK(0 <= i && i < kOperandCount);
|
|
|
|
const OperandType kOperands[] = {operand_0, operand_1};
|
|
|
|
return kOperands[i];
|
|
|
|
}
|
|
|
|
|
2016-01-26 13:55:28 +00:00
|
|
|
template <OperandType ot>
|
|
|
|
static inline bool HasAnyOperandsOfType() {
|
|
|
|
return operand_0 == ot || operand_1 == ot;
|
|
|
|
}
|
|
|
|
|
2016-03-21 17:08:21 +00:00
|
|
|
static inline bool IsScalable() {
|
|
|
|
return (OperandTraits<operand_0>::TypeInfo::kIsScalable |
|
|
|
|
OperandTraits<operand_1>::TypeInfo::kIsScalable);
|
|
|
|
}
|
|
|
|
|
2015-10-01 17:22:58 +00:00
|
|
|
static const int kOperandCount = 2;
|
2016-01-26 13:55:28 +00:00
|
|
|
static const int kRegisterOperandCount =
|
|
|
|
RegisterOperandTraits<operand_0>::kIsRegisterOperand +
|
|
|
|
RegisterOperandTraits<operand_1>::kIsRegisterOperand;
|
|
|
|
static const int kRegisterOperandBitmap =
|
|
|
|
RegisterOperandTraits<operand_0>::kIsRegisterOperand +
|
|
|
|
(RegisterOperandTraits<operand_1>::kIsRegisterOperand << 1);
|
2015-10-01 17:22:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <OperandType operand_0>
|
|
|
|
struct BytecodeTraits<operand_0, OPERAND_TERM> {
|
|
|
|
static inline OperandType GetOperandType(int i) {
|
|
|
|
DCHECK(i == 0);
|
|
|
|
return operand_0;
|
|
|
|
}
|
|
|
|
|
2016-01-26 13:55:28 +00:00
|
|
|
template <OperandType ot>
|
|
|
|
static inline bool HasAnyOperandsOfType() {
|
|
|
|
return operand_0 == ot;
|
|
|
|
}
|
|
|
|
|
2016-03-21 17:08:21 +00:00
|
|
|
static inline bool IsScalable() {
|
|
|
|
return OperandTraits<operand_0>::TypeInfo::kIsScalable;
|
|
|
|
}
|
|
|
|
|
2015-10-01 17:22:58 +00:00
|
|
|
static const int kOperandCount = 1;
|
2016-01-26 13:55:28 +00:00
|
|
|
static const int kRegisterOperandCount =
|
|
|
|
RegisterOperandTraits<operand_0>::kIsRegisterOperand;
|
|
|
|
static const int kRegisterOperandBitmap =
|
|
|
|
RegisterOperandTraits<operand_0>::kIsRegisterOperand;
|
2015-10-01 17:22:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct BytecodeTraits<OperandType::kNone, OPERAND_TERM> {
|
|
|
|
static inline OperandType GetOperandType(int i) {
|
|
|
|
UNREACHABLE();
|
|
|
|
return OperandType::kNone;
|
|
|
|
}
|
|
|
|
|
2016-01-26 13:55:28 +00:00
|
|
|
template <OperandType ot>
|
|
|
|
static inline bool HasAnyOperandsOfType() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-21 17:08:21 +00:00
|
|
|
static inline bool IsScalable() { return false; }
|
|
|
|
|
2015-10-01 17:22:58 +00:00
|
|
|
static const int kOperandCount = 0;
|
2016-01-26 13:55:28 +00:00
|
|
|
static const int kRegisterOperandCount = 0;
|
|
|
|
static const int kRegisterOperandBitmap = 0;
|
2015-10-01 17:22:58 +00:00
|
|
|
};
|
|
|
|
|
2016-03-21 17:08:21 +00:00
|
|
|
template <bool>
|
|
|
|
struct OperandScaler {
|
|
|
|
static int Multiply(int size, int operand_scale) { return 0; }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct OperandScaler<false> {
|
|
|
|
static int Multiply(int size, int operand_scale) { return size; }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct OperandScaler<true> {
|
|
|
|
static int Multiply(int size, int operand_scale) {
|
|
|
|
return size * operand_scale;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static OperandSize ScaledOperandSize(OperandType operand_type,
|
|
|
|
OperandScale operand_scale) {
|
|
|
|
switch (operand_type) {
|
|
|
|
#define CASE(Name, TypeInfo) \
|
|
|
|
case OperandType::k##Name: { \
|
|
|
|
OperandSize base_size = OperandTypeInfoTraits<TypeInfo>::kUnscaledSize; \
|
|
|
|
int size = \
|
|
|
|
OperandScaler<OperandTypeInfoTraits<TypeInfo>::kIsScalable>::Multiply( \
|
|
|
|
static_cast<int>(base_size), static_cast<int>(operand_scale)); \
|
|
|
|
OperandSize operand_size = static_cast<OperandSize>(size); \
|
|
|
|
DCHECK(operand_size == OperandSize::kByte || \
|
|
|
|
operand_size == OperandSize::kShort || \
|
|
|
|
operand_size == OperandSize::kQuad); \
|
|
|
|
return operand_size; \
|
|
|
|
}
|
|
|
|
OPERAND_TYPE_LIST(CASE)
|
|
|
|
#undef CASE
|
|
|
|
}
|
|
|
|
UNREACHABLE();
|
|
|
|
return OperandSize::kNone;
|
|
|
|
}
|
|
|
|
|
2015-10-01 17:22:58 +00:00
|
|
|
} // namespace interpreter
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
|
|
|
|
|
|
|
#endif // V8_INTERPRETER_BYTECODE_TRAITS_H_
|