V8. ASM-2-WASM. New type system.

This CL introduces the new type system for the ASM
type-checker/validator.

BUG=

Review-Url: https://codereview.chromium.org/2045703007
Cr-Commit-Position: refs/heads/master@{#36942}
This commit is contained in:
jpp 2016-06-13 13:41:10 -07:00 committed by Commit bot
parent 85c2c8d847
commit 73eacf6b90
7 changed files with 1096 additions and 0 deletions

View File

@ -1492,6 +1492,8 @@ v8_source_set("v8_base") {
"src/version.h",
"src/vm-state-inl.h",
"src/vm-state.h",
"src/wasm/asm-types.cc",
"src/wasm/asm-types.h",
"src/wasm/asm-wasm-builder.cc",
"src/wasm/asm-wasm-builder.h",
"src/wasm/ast-decoder.cc",

View File

@ -1147,6 +1147,8 @@
'version.h',
'vm-state-inl.h',
'vm-state.h',
'wasm/asm-types.cc',
'wasm/asm-types.h',
'wasm/asm-wasm-builder.cc',
'wasm/asm-wasm-builder.h',
'wasm/ast-decoder.cc',

275
src/wasm/asm-types.cc Normal file
View File

@ -0,0 +1,275 @@
// Copyright 2016 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.
#include "src/v8.h"
#include "src/wasm/asm-types.h"
namespace v8 {
namespace internal {
namespace wasm {
AsmCallableType* AsmType::AsCallableType() {
DCHECK(this->AsFunctionType() != nullptr ||
this->AsOverloadedFunctionType() != nullptr);
return reinterpret_cast<AsmCallableType*>(this);
}
std::string AsmType::Name() {
AsmValueType* avt = this->AsValueType();
if (avt != nullptr) {
switch (avt->Bitset()) {
#define RETURN_TYPE_NAME(CamelName, string_name, number, parent_types) \
case AsmValueType::kAsm##CamelName: \
return string_name;
FOR_EACH_ASM_VALUE_TYPE_LIST(RETURN_TYPE_NAME)
#undef RETURN_TYPE_NAME
default:
UNREACHABLE();
}
}
return this->AsCallableType()->Name();
}
bool AsmType::IsExactly(AsmType* that) {
// TODO(jpp): maybe this can become this == that.
AsmValueType* avt = this->AsValueType();
if (avt != nullptr) {
AsmValueType* tavt = that->AsValueType();
if (tavt == nullptr) {
return false;
}
return avt->Bitset() == tavt->Bitset();
}
// TODO(jpp): is it useful to allow non-value types to be tested with
// IsExactly?
return that == this;
}
bool AsmType::IsA(AsmType* that) {
// IsA is used for querying inheritance relationships. Therefore it is only
// meaningful for basic types.
AsmValueType* tavt = that->AsValueType();
if (tavt != nullptr) {
AsmValueType* avt = this->AsValueType();
if (avt == nullptr) {
return false;
}
return (avt->Bitset() & tavt->Bitset()) == tavt->Bitset();
}
// TODO(jpp): is it useful to allow non-value types to be tested with IsA?
return that == this;
}
int32_t AsmType::ElementSizeInBytes() {
auto* value = AsValueType();
if (value == nullptr) {
return AsmType::kNotHeapType;
}
switch (value->Bitset()) {
case AsmValueType::kAsmInt8Array:
case AsmValueType::kAsmUint8Array:
return 1;
case AsmValueType::kAsmInt16Array:
case AsmValueType::kAsmUint16Array:
return 2;
case AsmValueType::kAsmInt32Array:
case AsmValueType::kAsmUint32Array:
case AsmValueType::kAsmFloat32Array:
return 4;
case AsmValueType::kAsmFloat64Array:
return 8;
default:
return AsmType::kNotHeapType;
}
}
AsmType* AsmType::LoadType() {
auto* value = AsValueType();
if (value == nullptr) {
return AsmType::None();
}
switch (value->Bitset()) {
case AsmValueType::kAsmInt8Array:
case AsmValueType::kAsmUint8Array:
case AsmValueType::kAsmInt16Array:
case AsmValueType::kAsmUint16Array:
case AsmValueType::kAsmInt32Array:
case AsmValueType::kAsmUint32Array:
return AsmType::Intish();
case AsmValueType::kAsmFloat32Array:
return AsmType::FloatQ();
case AsmValueType::kAsmFloat64Array:
return AsmType::DoubleQ();
default:
return AsmType::None();
}
}
AsmType* AsmType::StoreType() {
auto* value = AsValueType();
if (value == nullptr) {
return AsmType::None();
}
switch (value->Bitset()) {
case AsmValueType::kAsmInt8Array:
case AsmValueType::kAsmUint8Array:
case AsmValueType::kAsmInt16Array:
case AsmValueType::kAsmUint16Array:
case AsmValueType::kAsmInt32Array:
case AsmValueType::kAsmUint32Array:
return AsmType::Intish();
case AsmValueType::kAsmFloat32Array:
return AsmType::FloatishDoubleQ();
case AsmValueType::kAsmFloat64Array:
return AsmType::FloatQDoubleQ();
default:
return AsmType::None();
}
}
std::string AsmFunctionType::Name() {
std::string ret;
ret += "(";
for (size_t ii = 0; ii < args_.size(); ++ii) {
ret += args_[ii]->Name();
if (ii != args_.size() - 1) {
ret += ", ";
}
}
if (IsMinMaxType()) {
DCHECK_EQ(args_.size(), 2);
ret += "...";
}
ret += ") -> ";
ret += return_type_->Name();
return ret;
}
namespace {
class AsmFroundType final : public AsmFunctionType {
public:
bool IsFroundType() const override { return true; }
private:
friend AsmType;
AsmFroundType(Zone* zone, AsmType* src)
: AsmFunctionType(zone, AsmType::Float()) {
AddArgument(src);
}
};
} // namespace
AsmType* AsmType::FroundType(Zone* zone, AsmType* src) {
DCHECK(src->AsValueType() != nullptr);
auto* Fround = new (zone) AsmFroundType(zone, src);
return reinterpret_cast<AsmType*>(Fround);
}
namespace {
class AsmMinMaxType final : public AsmFunctionType {
public:
bool IsMinMaxType() const override { return true; }
private:
friend AsmType;
AsmMinMaxType(Zone* zone, AsmType* type) : AsmFunctionType(zone, type) {
AddArgument(type);
AddArgument(type);
}
AsmType* ValidateCall(AsmType* function_type) override {
auto* callable = function_type->AsFunctionType();
if (callable == nullptr) {
return nullptr;
}
if (!ReturnType()->IsExactly(callable->ReturnType())) {
return AsmType::None();
}
if (callable->Arguments().size() < 2) {
return AsmType::None();
}
for (size_t ii = 0; ii < Arguments().size(); ++ii) {
if (!Arguments()[0]->IsExactly(callable->Arguments()[ii])) {
return AsmType::None();
}
}
return ReturnType();
}
};
} // namespace
AsmType* AsmType::MinMaxType(Zone* zone, AsmType* type) {
DCHECK(type->AsValueType() != nullptr);
auto* MinMax = new (zone) AsmMinMaxType(zone, type);
return reinterpret_cast<AsmType*>(MinMax);
}
AsmType* AsmFunctionType::ValidateCall(AsmType* function_type) {
auto* callable = function_type->AsFunctionType();
if (callable == nullptr) {
return nullptr;
}
if (!return_type_->IsExactly(callable->return_type_)) {
return AsmType::None();
}
if (args_.size() != callable->args_.size()) {
return AsmType::None();
}
for (size_t ii = 0; ii < args_.size(); ++ii) {
if (!args_[ii]->IsExactly(callable->args_[ii])) {
return AsmType::None();
}
}
return return_type_;
}
std::string AsmOverloadedFunctionType::Name() {
std::string ret;
for (size_t ii = 0; ii < overloads_.size(); ++ii) {
if (ii != 0) {
ret += " /\\ ";
}
ret += overloads_[ii]->Name();
}
return ret;
}
AsmType* AsmOverloadedFunctionType::ValidateCall(AsmType* function_type) {
auto* callable = function_type->AsFunctionType();
if (callable == nullptr) {
return AsmType::None();
}
for (size_t ii = 0; ii < overloads_.size(); ++ii) {
auto* validated_type =
overloads_[ii]->AsCallableType()->ValidateCall(function_type);
if (validated_type != AsmType::None()) {
return validated_type;
}
}
return AsmType::None();
}
void AsmOverloadedFunctionType::AddOverload(AsmType* overload) {
DCHECK(overload->AsFunctionType() != nullptr);
overloads_.push_back(overload);
}
} // namespace wasm
} // namespace internal
} // namespace v8

255
src/wasm/asm-types.h Normal file
View File

@ -0,0 +1,255 @@
// Copyright 2016 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 SRC_WASM_ASM_TYPES_H_
#define SRC_WASM_ASM_TYPES_H_
#include <string>
#include <type_traits>
#include "src/base/macros.h"
#include "src/zone-containers.h"
#include "src/zone.h"
namespace v8 {
namespace internal {
namespace wasm {
class AsmType;
class AsmFunctionType;
class AsmOverloadedFunctionType;
// List of V(CamelName, string_name, number, parent_types)
#define FOR_EACH_ASM_VALUE_TYPE_LIST(V) \
/* These tags are not types that are expressable in the asm source. They */ \
/* are used to express semantic information about the types they tag. */ \
V(Heap, "[]", 1, 0) \
/*The following are actual types that appear in the asm source. */ \
V(Void, "void", 2, 0) \
V(Extern, "extern", 3, 0) \
V(DoubleQ, "double?", 4, 0) \
V(Double, "double", 5, kAsmDoubleQ | kAsmExtern) \
V(Intish, "intish", 6, 0) \
V(Int, "int", 7, kAsmIntish) \
V(Signed, "signed", 8, kAsmInt | kAsmExtern) \
V(Unsigned, "unsigned", 9, kAsmInt) \
V(FixNum, "fixnum", 10, kAsmSigned | kAsmUnsigned) \
V(Floatish, "floatish", 11, 0) \
V(FloatQ, "float?", 12, kAsmFloatish) \
V(Float, "float", 13, kAsmFloatQ) \
/* Types used for expressing the Heap accesses. */ \
V(Uint8Array, "Uint8Array", 14, kAsmHeap) \
V(Int8Array, "Int8Array", 15, kAsmHeap) \
V(Uint16Array, "Uint16Array", 16, kAsmHeap) \
V(Int16Array, "Int16Array", 17, kAsmHeap) \
V(Uint32Array, "Uint32Array", 18, kAsmHeap) \
V(Int32Array, "Int32Array", 19, kAsmHeap) \
V(Float32Array, "Float32Array", 20, kAsmHeap) \
V(Float64Array, "Float64Array", 21, kAsmHeap) \
V(FloatishDoubleQ, "floatish|double?", 22, kAsmFloatish | kAsmDoubleQ) \
V(FloatQDoubleQ, "float?|double?", 23, kAsmFloatQ | kAsmDoubleQ) \
/* None is used to represent errors in the type checker. */ \
V(None, "<none>", 31, 0)
// List of V(CamelName)
#define FOR_EACH_ASM_CALLABLE_TYPE_LIST(V) \
V(FunctionType) \
V(OverloadedFunctionType)
class AsmValueType {
public:
typedef uint32_t bitset_t;
enum : uint32_t {
#define DEFINE_TAG(CamelName, string_name, number, parent_types) \
kAsm##CamelName = ((1u << (number)) | (parent_types)),
FOR_EACH_ASM_VALUE_TYPE_LIST(DEFINE_TAG)
#undef DEFINE_TAG
kAsmUnknown = 0,
kAsmValueTypeTag = 1u
};
private:
friend class AsmType;
static AsmValueType* AsValueType(AsmType* type) {
if ((reinterpret_cast<uintptr_t>(type) & kAsmValueTypeTag) ==
kAsmValueTypeTag) {
return reinterpret_cast<AsmValueType*>(type);
}
return nullptr;
}
bitset_t Bitset() const {
DCHECK((reinterpret_cast<uintptr_t>(this) & kAsmValueTypeTag) ==
kAsmValueTypeTag);
return static_cast<bitset_t>(reinterpret_cast<uintptr_t>(this) &
~kAsmValueTypeTag);
}
static AsmType* New(bitset_t bits) {
DCHECK_EQ((bits & kAsmValueTypeTag), 0);
return reinterpret_cast<AsmType*>(
static_cast<uintptr_t>(bits | kAsmValueTypeTag));
}
// AsmValueTypes can't be created except through AsmValueType::New.
DISALLOW_IMPLICIT_CONSTRUCTORS(AsmValueType);
};
class AsmCallableType : public ZoneObject {
public:
virtual std::string Name() = 0;
virtual AsmType* ValidateCall(AsmType* function_type) = 0;
#define DECLARE_CAST(CamelName) \
virtual Asm##CamelName* As##CamelName() { return nullptr; }
FOR_EACH_ASM_CALLABLE_TYPE_LIST(DECLARE_CAST)
#undef DECLARE_CAST
protected:
AsmCallableType() = default;
virtual ~AsmCallableType() = default;
private:
DISALLOW_COPY_AND_ASSIGN(AsmCallableType);
};
class AsmFunctionType : public AsmCallableType {
public:
AsmFunctionType* AsFunctionType() final { return this; }
void AddArgument(AsmType* type) { args_.push_back(type); }
const ZoneVector<AsmType*> Arguments() const { return args_; }
AsmType* ReturnType() const { return return_type_; }
virtual bool IsMinMaxType() const { return false; }
virtual bool IsFroundType() const { return false; }
protected:
AsmFunctionType(Zone* zone, AsmType* return_type)
: return_type_(return_type), args_(zone) {}
private:
friend AsmType;
std::string Name() override;
AsmType* ValidateCall(AsmType* function_type) override;
AsmType* return_type_;
ZoneVector<AsmType*> args_;
DISALLOW_COPY_AND_ASSIGN(AsmFunctionType);
};
class AsmOverloadedFunctionType final : public AsmCallableType {
public:
AsmOverloadedFunctionType* AsOverloadedFunctionType() override {
return this;
}
void AddOverload(AsmType* overload);
private:
friend AsmType;
explicit AsmOverloadedFunctionType(Zone* zone) : overloads_(zone) {}
std::string Name() override;
AsmType* ValidateCall(AsmType* function_type) override;
ZoneVector<AsmType*> overloads_;
DISALLOW_IMPLICIT_CONSTRUCTORS(AsmOverloadedFunctionType);
};
class AsmType {
public:
#define DEFINE_CONSTRUCTOR(CamelName, string_name, number, parent_types) \
static AsmType* CamelName() { \
return AsmValueType::New(AsmValueType::kAsm##CamelName); \
}
FOR_EACH_ASM_VALUE_TYPE_LIST(DEFINE_CONSTRUCTOR)
#undef DEFINE_CONSTRUCTOR
#define DEFINE_CAST(CamelCase) \
Asm##CamelCase* As##CamelCase() { \
if (AsValueType() != nullptr) { \
return nullptr; \
} \
return reinterpret_cast<AsmCallableType*>(this)->As##CamelCase(); \
}
FOR_EACH_ASM_CALLABLE_TYPE_LIST(DEFINE_CAST)
#undef DEFINE_CAST
AsmValueType* AsValueType() { return AsmValueType::AsValueType(this); }
AsmCallableType* AsCallableType();
// A function returning ret. Callers still need to invoke AddArgument with the
// returned type to fully create this type.
static AsmType* Function(Zone* zone, AsmType* ret) {
AsmFunctionType* f = new (zone) AsmFunctionType(zone, ret);
return reinterpret_cast<AsmType*>(f);
}
// Overloaded function types. Not creatable by asm source, but useful to
// represent the overloaded stdlib functions.
static AsmType* OverloadedFunction(Zone* zone) {
auto* f = new (zone) AsmOverloadedFunctionType(zone);
return reinterpret_cast<AsmType*>(f);
}
// The type for fround(src).
static AsmType* FroundType(Zone* zone, AsmType* src);
// The (variadic) type for min and max.
static AsmType* MinMaxType(Zone* zone, AsmType* type);
std::string Name();
// IsExactly returns true if this is the exact same type as that. For
// non-value types (e.g., callables), this returns this == that.
bool IsExactly(AsmType* that);
// IsA is used to query whether this is an instance of that (i.e., if this is
// a type derived from that.) For non-value types (e.g., callables), this
// returns this == that.
bool IsA(AsmType* that);
// Types allowed in return statements. void is the type for returns without
// an expression.
bool IsReturnType() {
return this == AsmType::Void() || this == AsmType::Double() ||
this == AsmType::Signed() || this == AsmType::Float();
}
// Types allowed to be parameters in asm functions.
bool IsParameterType() {
return this == AsmType::Double() || this == AsmType::Int() ||
this == AsmType::Float();
}
// Types allowed to be compared using the comparison operators.
bool IsComparableType() {
return this == AsmType::Double() || this == AsmType::Signed() ||
this == AsmType::Unsigned() || this == AsmType::Float();
}
// The following methods are meant to be used for inspecting the traits of
// element types for the heap view types.
enum : int32_t { kNotHeapType = -1 };
// Returns the element size if this is a heap type. Otherwise returns
// kNotHeapType.
int32_t ElementSizeInBytes();
// Returns the load type if this is a heap type. AsmType::None is returned if
// this is not a heap type.
AsmType* LoadType();
// Returns the store type if this is a heap type. AsmType::None is returned if
// this is not a heap type.
AsmType* StoreType();
};
} // namespace wasm
} // namespace internal
} // namespace v8
#endif // SRC_WASM_ASM_TYPES_H_

View File

@ -106,6 +106,7 @@ executable("unittests") {
"run-all-unittests.cc",
"test-utils.cc",
"test-utils.h",
"wasm/asm-types-unittest.cc",
"wasm/ast-decoder-unittest.cc",
"wasm/control-transfer-unittest.cc",
"wasm/decoder-unittest.cc",

View File

@ -122,6 +122,7 @@
'run-all-unittests.cc',
'test-utils.h',
'test-utils.cc',
'wasm/asm-types-unittest.cc',
'wasm/ast-decoder-unittest.cc',
'wasm/control-transfer-unittest.cc',
'wasm/decoder-unittest.cc',

View File

@ -0,0 +1,560 @@
// Copyright 2016 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.
#include "src/wasm/asm-types.h"
#include <unordered_map>
#include <unordered_set>
#include "src/base/macros.h"
#include "test/unittests/test-utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace v8 {
namespace internal {
namespace wasm {
namespace {
using ::testing::StrEq;
class AsmTypeTest : public TestWithZone {
public:
using Type = AsmType;
AsmTypeTest()
: parents_({
{Type::Uint8Array(), {Type::Heap()}},
{Type::Int8Array(), {Type::Heap()}},
{Type::Uint16Array(), {Type::Heap()}},
{Type::Int16Array(), {Type::Heap()}},
{Type::Uint32Array(), {Type::Heap()}},
{Type::Int32Array(), {Type::Heap()}},
{Type::Float32Array(), {Type::Heap()}},
{Type::Float64Array(), {Type::Heap()}},
{Type::FloatishDoubleQ(), {Type::Floatish(), Type::DoubleQ()}},
{Type::FloatQDoubleQ(),
{Type::FloatQ(), Type::Floatish(), Type::DoubleQ()}},
{Type::Float(), {Type::FloatQ(), Type::Floatish()}},
{Type::FloatQ(), {Type::Floatish()}},
{Type::FixNum(),
{Type::Signed(), Type::Extern(), Type::Unsigned(), Type::Int(),
Type::Intish()}},
{Type::Unsigned(), {Type::Int(), Type::Intish()}},
{Type::Signed(), {Type::Extern(), Type::Int(), Type::Intish()}},
{Type::Int(), {Type::Intish()}},
{Type::Double(), {Type::DoubleQ(), Type::Extern()}},
}) {}
protected:
std::unordered_set<Type*> ParentsOf(Type* derived) const {
const auto parents_iter = parents_.find(derived);
if (parents_iter == parents_.end()) {
return std::unordered_set<Type*>();
}
return parents_iter->second;
}
class FunctionTypeBuilder {
public:
FunctionTypeBuilder(FunctionTypeBuilder&& b)
: function_type_(b.function_type_) {
b.function_type_ = nullptr;
}
FunctionTypeBuilder& operator=(FunctionTypeBuilder&& b) {
if (this != &b) {
function_type_ = b.function_type_;
b.function_type_ = nullptr;
}
return *this;
}
FunctionTypeBuilder(Zone* zone, Type* return_type)
: function_type_(Type::Function(zone, return_type)) {}
private:
static void AddAllArguments(AsmFunctionType*) {}
template <typename Arg, typename... Others>
static void AddAllArguments(AsmFunctionType* function_type, Arg* arg,
Others... others) {
CHECK(function_type != nullptr);
function_type->AddArgument((*arg)());
AddAllArguments(function_type, others...);
}
public:
template <typename... Args>
Type* operator()(Args... args) {
Type* ret = function_type_;
function_type_ = nullptr;
AddAllArguments(ret->AsFunctionType(), args...);
return ret;
}
private:
Type* function_type_;
};
FunctionTypeBuilder Function(Type* (*return_type)()) {
return FunctionTypeBuilder(zone(), (*return_type)());
}
template <typename... Overloads>
Type* Overload(Overloads... overloads) {
auto* ret = Type::OverloadedFunction(zone());
AddAllOverloads(ret->AsOverloadedFunctionType(), overloads...);
return ret;
}
private:
static void AddAllOverloads(AsmOverloadedFunctionType*) {}
template <typename Overload, typename... Others>
static void AddAllOverloads(AsmOverloadedFunctionType* function,
Overload* overload, Others... others) {
CHECK(function != nullptr);
function->AddOverload(overload);
AddAllOverloads(function, others...);
}
const std::unordered_map<Type*, std::unordered_set<Type*>> parents_;
};
// AsmValueTypeParents expose the bitmasks for the parents for each value type
// in asm's type system. It inherits from AsmValueType so that the kAsm<Foo>
// members are available when expanding the FOR_EACH_ASM_VALUE_TYPE_LIST macro.
class AsmValueTypeParents : private AsmValueType {
public:
enum : uint32_t {
#define V(CamelName, string_name, number, parent_types) \
CamelName = parent_types,
FOR_EACH_ASM_VALUE_TYPE_LIST(V)
#undef V
};
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(AsmValueTypeParents);
};
TEST_F(AsmTypeTest, ValidateBits) {
// Generic validation tests for the bits in the type system's type
// definitions.
std::unordered_set<Type*> seen_types;
std::unordered_set<uint32_t> seen_numbers;
uint32_t total_types = 0;
#define V(CamelName, string_name, number, parent_types) \
do { \
++total_types; \
seen_types.insert(Type::CamelName()); \
seen_numbers.insert(number); \
/* Every ASM type must have a valid number. */ \
EXPECT_NE(0, number) << Type::CamelName()->Name(); \
/* Inheritance cycles - unlikely, but we're paranoid and check for it */ \
/* anyways.*/ \
EXPECT_EQ(0, (1 << (number)) & AsmValueTypeParents::CamelName); \
} while (0);
FOR_EACH_ASM_VALUE_TYPE_LIST(V)
#undef V
// At least one type was expanded.
EXPECT_GT(total_types, 0u);
// Each value type is unique.
EXPECT_EQ(total_types, seen_types.size());
// Each number is unique.
EXPECT_EQ(total_types, seen_numbers.size());
}
TEST_F(AsmTypeTest, SaneParentsMap) {
// This test ensures our parents map contains all the parents types that are
// specified in the types' declaration. It does not report bogus inheritance.
// Handy-dandy lambda for counting bits. Code borrowed from stack overflow.
auto NumberOfSetBits = [](uintptr_t parent_mask) -> uint32_t {
uint32_t parent_mask32 = static_cast<uint32_t>(parent_mask);
CHECK_EQ(parent_mask, parent_mask32);
parent_mask32 = parent_mask32 - ((parent_mask32 >> 1) & 0x55555555);
parent_mask32 =
(parent_mask32 & 0x33333333) + ((parent_mask32 >> 2) & 0x33333333);
return (((parent_mask32 + (parent_mask32 >> 4)) & 0x0F0F0F0F) *
0x01010101) >>
24;
};
#define V(CamelName, string_name, number, parent_types) \
do { \
const uintptr_t parents = \
reinterpret_cast<uintptr_t>(Type::CamelName()) & ~(1 << (number)); \
EXPECT_EQ(NumberOfSetBits(parents), \
1 + ParentsOf(Type::CamelName()).size()) \
<< Type::CamelName()->Name() << ", parents " \
<< reinterpret_cast<void*>(parents) << ", type " \
<< static_cast<void*>(Type::CamelName()); \
} while (0);
FOR_EACH_ASM_VALUE_TYPE_LIST(V)
#undef V
}
TEST_F(AsmTypeTest, Names) {
#define V(CamelName, string_name, number, parent_types) \
do { \
EXPECT_THAT(Type::CamelName()->Name(), StrEq(string_name)); \
} while (0);
FOR_EACH_ASM_VALUE_TYPE_LIST(V)
#undef V
EXPECT_THAT(Function(Type::Int)(Type::Double, Type::Float)->Name(),
StrEq("(double, float) -> int"));
EXPECT_THAT(Overload(Function(Type::Int)(Type::Double, Type::Float),
Function(Type::Int)(Type::Int))
->Name(),
StrEq("(double, float) -> int /\\ (int) -> int"));
EXPECT_THAT(Type::FroundType(zone(), Type::Int())->Name(),
StrEq("(int) -> float"));
EXPECT_THAT(Type::FroundType(zone(), Type::Floatish())->Name(),
StrEq("(floatish) -> float"));
EXPECT_THAT(Type::FroundType(zone(), Type::DoubleQ())->Name(),
StrEq("(double?) -> float"));
EXPECT_THAT(Type::MinMaxType(zone(), Type::Int())->Name(),
StrEq("(int, int...) -> int"));
EXPECT_THAT(Type::MinMaxType(zone(), Type::Floatish())->Name(),
StrEq("(floatish, floatish...) -> floatish"));
EXPECT_THAT(Type::MinMaxType(zone(), Type::DoubleQ())->Name(),
StrEq("(double?, double?...) -> double?"));
}
TEST_F(AsmTypeTest, IsExactly) {
Type* test_types[] = {
#define CREATE(CamelName, string_name, number, parent_types) Type::CamelName(),
FOR_EACH_ASM_VALUE_TYPE_LIST(CREATE)
#undef CREATE
Function(Type::Int)(Type::Double),
Function(Type::Int)(Type::DoubleQ),
Overload(Function(Type::Int)(Type::Double)),
Function(Type::Int)(Type::Int, Type::Int),
Type::MinMaxType(zone(), Type::Int()), Function(Type::Int)(Type::Float),
Type::FroundType(zone(), Type::Int()),
};
for (size_t ii = 0; ii < arraysize(test_types); ++ii) {
for (size_t jj = 0; jj < arraysize(test_types); ++jj) {
EXPECT_EQ(ii == jj, test_types[ii]->IsExactly(test_types[jj]))
<< test_types[ii]->Name()
<< ((ii == jj) ? " is not exactly " : " is exactly ")
<< test_types[jj]->Name();
}
}
}
TEST_F(AsmTypeTest, IsA) {
Type* test_types[] = {
#define CREATE(CamelName, string_name, number, parent_types) Type::CamelName(),
FOR_EACH_ASM_VALUE_TYPE_LIST(CREATE)
#undef CREATE
Function(Type::Int)(Type::Double),
Function(Type::Int)(Type::DoubleQ),
Overload(Function(Type::Int)(Type::Double)),
Function(Type::Int)(Type::Int, Type::Int),
Type::MinMaxType(zone(), Type::Int()), Function(Type::Int)(Type::Float),
Type::FroundType(zone(), Type::Int()),
};
for (size_t ii = 0; ii < arraysize(test_types); ++ii) {
for (size_t jj = 0; jj < arraysize(test_types); ++jj) {
const bool Expected =
(ii == jj) || ParentsOf(test_types[ii]).count(test_types[jj]) != 0;
EXPECT_EQ(Expected, test_types[ii]->IsA(test_types[jj]))
<< test_types[ii]->Name() << (Expected ? " is not a " : " is a ")
<< test_types[jj]->Name();
}
}
}
TEST_F(AsmTypeTest, ValidateCall) {
auto* min_max_int = Type::MinMaxType(zone(), Type::Int());
auto* i2i = Function(Type::Int)(Type::Int);
auto* ii2i = Function(Type::Int)(Type::Int, Type::Int);
auto* iii2i = Function(Type::Int)(Type::Int, Type::Int, Type::Int);
auto* iiii2i =
Function(Type::Int)(Type::Int, Type::Int, Type::Int, Type::Int);
EXPECT_EQ(Type::Int(),
min_max_int->AsCallableType()->ValidateCall(min_max_int));
EXPECT_EQ(Type::Int(), min_max_int->AsCallableType()->ValidateCall(ii2i));
EXPECT_EQ(Type::Int(), min_max_int->AsCallableType()->ValidateCall(iii2i));
EXPECT_EQ(Type::Int(), min_max_int->AsCallableType()->ValidateCall(iiii2i));
EXPECT_EQ(Type::None(), min_max_int->AsCallableType()->ValidateCall(i2i));
auto* min_max_double = Type::MinMaxType(zone(), Type::Double());
auto* d2d = Function(Type::Double)(Type::Double);
auto* dd2d = Function(Type::Double)(Type::Double, Type::Double);
auto* ddd2d =
Function(Type::Double)(Type::Double, Type::Double, Type::Double);
auto* dddd2d = Function(Type::Double)(Type::Double, Type::Double,
Type::Double, Type::Double);
EXPECT_EQ(Type::Double(),
min_max_double->AsCallableType()->ValidateCall(min_max_double));
EXPECT_EQ(Type::Double(),
min_max_double->AsCallableType()->ValidateCall(dd2d));
EXPECT_EQ(Type::Double(),
min_max_double->AsCallableType()->ValidateCall(ddd2d));
EXPECT_EQ(Type::Double(),
min_max_double->AsCallableType()->ValidateCall(dddd2d));
EXPECT_EQ(Type::None(), min_max_double->AsCallableType()->ValidateCall(d2d));
auto* min_max = Overload(min_max_int, min_max_double);
EXPECT_EQ(Type::None(), min_max->AsCallableType()->ValidateCall(min_max));
EXPECT_EQ(Type::None(), min_max->AsCallableType()->ValidateCall(i2i));
EXPECT_EQ(Type::None(), min_max->AsCallableType()->ValidateCall(d2d));
EXPECT_EQ(Type::Int(), min_max->AsCallableType()->ValidateCall(min_max_int));
EXPECT_EQ(Type::Int(), min_max->AsCallableType()->ValidateCall(ii2i));
EXPECT_EQ(Type::Int(), min_max->AsCallableType()->ValidateCall(iii2i));
EXPECT_EQ(Type::Int(), min_max->AsCallableType()->ValidateCall(iiii2i));
EXPECT_EQ(Type::Double(),
min_max->AsCallableType()->ValidateCall(min_max_double));
EXPECT_EQ(Type::Double(), min_max->AsCallableType()->ValidateCall(dd2d));
EXPECT_EQ(Type::Double(), min_max->AsCallableType()->ValidateCall(ddd2d));
EXPECT_EQ(Type::Double(), min_max->AsCallableType()->ValidateCall(dddd2d));
auto* fround_floatish = Type::FroundType(zone(), Type::Floatish());
auto* fround_floatq = Type::FroundType(zone(), Type::FloatQ());
auto* fround_float = Type::FroundType(zone(), Type::Float());
auto* fround_doubleq = Type::FroundType(zone(), Type::DoubleQ());
auto* fround_double = Type::FroundType(zone(), Type::Double());
auto* fround_signed = Type::FroundType(zone(), Type::Signed());
auto* fround_unsigned = Type::FroundType(zone(), Type::Unsigned());
auto* fround_fixnum = Type::FroundType(zone(), Type::FixNum());
auto* fround =
Overload(fround_floatish, fround_floatq, fround_float, fround_doubleq,
fround_double, fround_signed, fround_unsigned, fround_fixnum);
EXPECT_EQ(Type::Float(), fround->AsCallableType()->ValidateCall(
Function(Type::Float)(Type::Floatish)));
EXPECT_EQ(Type::Float(), fround->AsCallableType()->ValidateCall(
Function(Type::Float)(Type::FloatQ)));
EXPECT_EQ(Type::Float(), fround->AsCallableType()->ValidateCall(
Function(Type::Float)(Type::Float)));
EXPECT_EQ(Type::Float(), fround->AsCallableType()->ValidateCall(
Function(Type::Float)(Type::DoubleQ)));
EXPECT_EQ(Type::Float(), fround->AsCallableType()->ValidateCall(
Function(Type::Float)(Type::Double)));
EXPECT_EQ(Type::Float(), fround->AsCallableType()->ValidateCall(
Function(Type::Float)(Type::Signed)));
EXPECT_EQ(Type::Float(), fround->AsCallableType()->ValidateCall(
Function(Type::Float)(Type::Unsigned)));
EXPECT_EQ(Type::Float(), fround->AsCallableType()->ValidateCall(
Function(Type::Float)(Type::FixNum)));
auto* idf2v = Function(Type::Void)(Type::Int, Type::Double, Type::Float);
auto* i2d = Function(Type::Double)(Type::Int);
auto* i2f = Function(Type::Float)(Type::Int);
auto* fi2d = Function(Type::Double)(Type::Float, Type::Int);
auto* idif2i =
Function(Type::Int)(Type::Int, Type::Double, Type::Int, Type::Float);
auto* overload = Overload(idf2v, i2f, /*i2d missing, */ fi2d, idif2i);
EXPECT_EQ(Type::Void(), overload->AsCallableType()->ValidateCall(idf2v));
EXPECT_EQ(Type::Float(), overload->AsCallableType()->ValidateCall(i2f));
EXPECT_EQ(Type::Double(), overload->AsCallableType()->ValidateCall(fi2d));
EXPECT_EQ(Type::Int(), overload->AsCallableType()->ValidateCall(idif2i));
EXPECT_EQ(Type::None(), overload->AsCallableType()->ValidateCall(i2d));
EXPECT_EQ(Type::None(), i2f->AsCallableType()->ValidateCall(i2d));
}
TEST_F(AsmTypeTest, IsReturnType) {
Type* test_types[] = {
#define CREATE(CamelName, string_name, number, parent_types) Type::CamelName(),
FOR_EACH_ASM_VALUE_TYPE_LIST(CREATE)
#undef CREATE
Function(Type::Int)(Type::Double),
Function(Type::Int)(Type::DoubleQ),
Overload(Function(Type::Int)(Type::Double)),
Function(Type::Int)(Type::Int, Type::Int),
Type::MinMaxType(zone(), Type::Int()), Function(Type::Int)(Type::Float),
Type::FroundType(zone(), Type::Int()),
};
std::unordered_set<Type*> return_types{
Type::Double(), Type::Signed(), Type::Float(), Type::Void(),
};
for (size_t ii = 0; ii < arraysize(test_types); ++ii) {
const bool IsReturnType = return_types.count(test_types[ii]);
EXPECT_EQ(IsReturnType, test_types[ii]->IsReturnType())
<< test_types[ii]->Name()
<< (IsReturnType ? " is not a return type" : " is a return type");
}
}
TEST_F(AsmTypeTest, IsParameterType) {
Type* test_types[] = {
#define CREATE(CamelName, string_name, number, parent_types) Type::CamelName(),
FOR_EACH_ASM_VALUE_TYPE_LIST(CREATE)
#undef CREATE
Function(Type::Int)(Type::Double),
Function(Type::Int)(Type::DoubleQ),
Overload(Function(Type::Int)(Type::Double)),
Function(Type::Int)(Type::Int, Type::Int),
Type::MinMaxType(zone(), Type::Int()), Function(Type::Int)(Type::Float),
Type::FroundType(zone(), Type::Int()),
};
std::unordered_set<Type*> parameter_types{
Type::Double(), Type::Int(), Type::Float(),
};
for (size_t ii = 0; ii < arraysize(test_types); ++ii) {
const bool IsParameterType = parameter_types.count(test_types[ii]);
EXPECT_EQ(IsParameterType, test_types[ii]->IsParameterType())
<< test_types[ii]->Name()
<< (IsParameterType ? " is not a parameter type"
: " is a parameter type");
}
}
TEST_F(AsmTypeTest, IsComparableType) {
Type* test_types[] = {
#define CREATE(CamelName, string_name, number, parent_types) Type::CamelName(),
FOR_EACH_ASM_VALUE_TYPE_LIST(CREATE)
#undef CREATE
Function(Type::Int)(Type::Double),
Function(Type::Int)(Type::DoubleQ),
Overload(Function(Type::Int)(Type::Double)),
Function(Type::Int)(Type::Int, Type::Int),
Type::MinMaxType(zone(), Type::Int()), Function(Type::Int)(Type::Float),
Type::FroundType(zone(), Type::Int()),
};
std::unordered_set<Type*> comparable_types{
Type::Double(), Type::Signed(), Type::Unsigned(), Type::Float(),
};
for (size_t ii = 0; ii < arraysize(test_types); ++ii) {
const bool IsComparableType = comparable_types.count(test_types[ii]);
EXPECT_EQ(IsComparableType, test_types[ii]->IsComparableType())
<< test_types[ii]->Name()
<< (IsComparableType ? " is not a comparable type"
: " is a comparable type");
}
}
TEST_F(AsmTypeTest, ElementSizeInBytes) {
Type* test_types[] = {
#define CREATE(CamelName, string_name, number, parent_types) Type::CamelName(),
FOR_EACH_ASM_VALUE_TYPE_LIST(CREATE)
#undef CREATE
Function(Type::Int)(Type::Double),
Function(Type::Int)(Type::DoubleQ),
Overload(Function(Type::Int)(Type::Double)),
Function(Type::Int)(Type::Int, Type::Int),
Type::MinMaxType(zone(), Type::Int()), Function(Type::Int)(Type::Float),
Type::FroundType(zone(), Type::Int()),
};
auto ElementSizeInBytesForType = [](Type* type) -> int32_t {
if (type == Type::Int8Array() || type == Type::Uint8Array()) {
return 1;
}
if (type == Type::Int16Array() || type == Type::Uint16Array()) {
return 2;
}
if (type == Type::Int32Array() || type == Type::Uint32Array() ||
type == Type::Float32Array()) {
return 4;
}
if (type == Type::Float64Array()) {
return 8;
}
return -1;
};
for (size_t ii = 0; ii < arraysize(test_types); ++ii) {
EXPECT_EQ(ElementSizeInBytesForType(test_types[ii]),
test_types[ii]->ElementSizeInBytes());
}
}
TEST_F(AsmTypeTest, LoadType) {
Type* test_types[] = {
#define CREATE(CamelName, string_name, number, parent_types) Type::CamelName(),
FOR_EACH_ASM_VALUE_TYPE_LIST(CREATE)
#undef CREATE
Function(Type::Int)(Type::Double),
Function(Type::Int)(Type::DoubleQ),
Overload(Function(Type::Int)(Type::Double)),
Function(Type::Int)(Type::Int, Type::Int),
Type::MinMaxType(zone(), Type::Int()), Function(Type::Int)(Type::Float),
Type::FroundType(zone(), Type::Int()),
};
auto LoadTypeForType = [](Type* type) -> Type* {
if (type == Type::Int8Array() || type == Type::Uint8Array() ||
type == Type::Int16Array() || type == Type::Uint16Array() ||
type == Type::Int32Array() || type == Type::Uint32Array()) {
return Type::Intish();
}
if (type == Type::Float32Array()) {
return Type::FloatQ();
}
if (type == Type::Float64Array()) {
return Type::DoubleQ();
}
return Type::None();
};
for (size_t ii = 0; ii < arraysize(test_types); ++ii) {
EXPECT_EQ(LoadTypeForType(test_types[ii]), test_types[ii]->LoadType());
}
}
TEST_F(AsmTypeTest, StoreType) {
Type* test_types[] = {
#define CREATE(CamelName, string_name, number, parent_types) Type::CamelName(),
FOR_EACH_ASM_VALUE_TYPE_LIST(CREATE)
#undef CREATE
Function(Type::Int)(Type::Double),
Function(Type::Int)(Type::DoubleQ),
Overload(Function(Type::Int)(Type::Double)),
Function(Type::Int)(Type::Int, Type::Int),
Type::MinMaxType(zone(), Type::Int()), Function(Type::Int)(Type::Float),
Type::FroundType(zone(), Type::Int()),
};
auto StoreTypeForType = [](Type* type) -> Type* {
if (type == Type::Int8Array() || type == Type::Uint8Array() ||
type == Type::Int16Array() || type == Type::Uint16Array() ||
type == Type::Int32Array() || type == Type::Uint32Array()) {
return Type::Intish();
}
if (type == Type::Float32Array()) {
return Type::FloatishDoubleQ();
}
if (type == Type::Float64Array()) {
return Type::FloatQDoubleQ();
}
return Type::None();
};
for (size_t ii = 0; ii < arraysize(test_types); ++ii) {
EXPECT_EQ(StoreTypeForType(test_types[ii]), test_types[ii]->StoreType())
<< test_types[ii]->Name();
}
}
} // namespace
} // namespace wasm
} // namespace internal
} // namespace v8