Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new compound Signature, Struct and Array types #3012

Merged
merged 57 commits into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
927bd6d
Refactor types to represent arbitrary complex types
dcodeIO Aug 1, 2020
e84664f
fix
dcodeIO Aug 1, 2020
185f887
fix
dcodeIO Aug 1, 2020
415353f
I know nothing
dcodeIO Aug 1, 2020
941bde4
layers
dcodeIO Aug 1, 2020
37eb81f
unify printing
dcodeIO Aug 1, 2020
f4067bd
tuple terminology, Tuple::toString via wrapping
dcodeIO Aug 2, 2020
5146da2
make nullable a conditional property of specific TypeDefs
dcodeIO Aug 2, 2020
7299028
remove now unused function parameters
dcodeIO Aug 2, 2020
a8dce05
move 'null' to TypeDef when printing
dcodeIO Aug 2, 2020
e006f21
ValueType -> ID, efficient lookups
dcodeIO Aug 2, 2020
a41c3d5
Extract kind
tlively Aug 3, 2020
2764af7
rename Type::ID to Type::BasicID
dcodeIO Aug 3, 2020
8e159d1
rename ID -> BasicID in other comments
dcodeIO Aug 3, 2020
f1a25a4
avoid using default cases
dcodeIO Aug 3, 2020
1e901fc
Merge remote-tracking branch 'tlively/dcodeIO-gc-extract-kind' into g…
dcodeIO Aug 3, 2020
34c2b55
fix, remove getKind
dcodeIO Aug 3, 2020
b4ffb98
pass Signature by value
dcodeIO Aug 3, 2020
12183ec
packed field types
dcodeIO Aug 4, 2020
e2f3281
copy assignment for TupleDef
dcodeIO Aug 4, 2020
fbb0897
remove unnecessary Field constructor
dcodeIO Aug 4, 2020
cda1056
add isNullable predicate
dcodeIO Aug 4, 2020
5ba1afc
use Type instead of BasicID in isPacked check
dcodeIO Aug 4, 2020
8bf1906
document TypeDefs of lists of a single type
dcodeIO Aug 4, 2020
eb11e41
fix copy assignment as suggested
dcodeIO Aug 4, 2020
3761ed5
revert separate printing of Field::PackedType
dcodeIO Aug 4, 2020
7da7012
simplify TypeDef copy assignment
dcodeIO Aug 5, 2020
c2cf485
Merge branch 'master' into gc-type-refactor
dcodeIO Aug 15, 2020
06cb1a3
use wasm::hash
dcodeIO Aug 15, 2020
dac491d
Merge branch 'master' into gc-type-refactor
dcodeIO Aug 17, 2020
656f0a3
Merge branch 'master' into gc-type-refactor
dcodeIO Aug 17, 2020
adb4332
drop extra Tuple wrapper, clarify nested Ref concept
dcodeIO Aug 18, 2020
7e46013
Update src/wasm-type.h
dcodeIO Aug 18, 2020
8aa4b5d
Update src/wasm-type.h
dcodeIO Aug 18, 2020
4ed19aa
Update src/wasm-type.h
dcodeIO Aug 18, 2020
fc05734
Update src/wasm/wasm-type.cpp
dcodeIO Aug 18, 2020
94e2f61
Update src/wasm/wasm-type.cpp
dcodeIO Aug 18, 2020
959aba1
Update src/wasm/wasm-type.cpp
dcodeIO Aug 18, 2020
e7eeb50
add getDef for readability, remove PackedType storage type
dcodeIO Aug 18, 2020
b72890c
fix
dcodeIO Aug 18, 2020
a9affcf
keep getDef internal as suggested
dcodeIO Aug 18, 2020
40f3a1a
move TypeDef definition to wasm-type.cpp
dcodeIO Aug 18, 2020
accd33d
initial test
dcodeIO Aug 18, 2020
d92e416
parens it is
dcodeIO Aug 18, 2020
e8e111d
Merge branch 'master' into gc-type-refactor
dcodeIO Aug 20, 2020
9517da1
Update src/wasm-type.h
dcodeIO Aug 20, 2020
c20b61f
add an assert
dcodeIO Aug 20, 2020
f6b9f83
revert Tuple move constructor until expert feedback
dcodeIO Aug 20, 2020
0de9d01
remove unnecessary this reference
dcodeIO Aug 20, 2020
0cdc596
Merge branch 'master' into gc-type-refactor
dcodeIO Aug 20, 2020
85fb78f
move remains of TypeDef to wasm-type.cpp, rename to TypeInfo and impl…
dcodeIO Aug 21, 2020
cecf38a
apply dangerous half-knowledge
dcodeIO Aug 21, 2020
fa620d9
interesting tests
dcodeIO Aug 21, 2020
9d206af
revert dangerous half-knowledge
dcodeIO Aug 23, 2020
b8d6faf
Update src/wasm-type.h
dcodeIO Aug 24, 2020
fd81477
Update src/wasm-type.h
dcodeIO Aug 24, 2020
5d617c9
Merge branch 'master' into gc-type-refactor
dcodeIO Aug 24, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/passes/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ struct SExprType {
static std::ostream& operator<<(std::ostream& o, const SExprType& localType) {
Type type = localType.type;
if (type.isTuple()) {
const std::vector<Type>& types = type.expand();
o << '(' << types[0];
for (size_t i = 1; i < types.size(); ++i) {
o << ' ' << types[i];
o << '(';
auto sep = "";
for (auto& t : type) {
o << sep << t;
sep = " ";
}
o << ')';
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/tools/wasm2c-wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ int main(int argc, char** argv) {
};

ret += wasm2cSignature(result);
if (func->sig.params.isMulti()) {
if (func->sig.params.isTuple()) {
for (auto& param : func->sig.params) {
ret += wasm2cSignature(param);
}
Expand Down
32 changes: 3 additions & 29 deletions src/wasm-type.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,6 @@ class Type {
// Construct from array description
explicit Type(const Array&, bool nullable);

// Accessors
size_t size() const;
const TypeList& expand() const;

// Predicates
// Compound Concrete
// Type Basic │ Single│
Expand Down Expand Up @@ -210,35 +206,13 @@ class Type {
assert(parent == other.parent);
return index - other.index;
}
const value_type& operator*() const {
if (parent->isMulti()) {
return (*(std::vector<Type>*)parent->getID())[index];
} else {
// see TODO in Type::end()
assert(index == 0 && parent->id != Type::none && "Index out of bounds");
return *parent;
}
}
const value_type& operator*() const;
};

Iterator begin() const { return Iterator(this, 0); }
Iterator end() const {
if (isMulti()) {
return Iterator(this, (*(std::vector<Type>*)getID()).size());
} else {
// TODO: unreachable is special and expands to {unreachable} currently.
// see also: https://github.com/WebAssembly/binaryen/issues/3062
return Iterator(this, size_t(id != Type::none));
}
}
Iterator end() const;
size_t size() const { return end() - begin(); }
const Type& operator[](size_t i) const {
if (isMulti()) {
return (*(std::vector<Type>*)getID())[i];
} else {
return *begin();
}
}
const Type& operator[](size_t i) const;
};

// Wrapper type for formatting types as "(param i32 i64 f32)"
Expand Down
82 changes: 37 additions & 45 deletions src/wasm/wasm-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,21 +222,6 @@ namespace {

std::mutex mutex;

// Maps basic types to tuples of the single basic type.
std::array<TypeList, Type::_last_basic_id + 1> basicTuples = {{
{},
{Type::unreachable},
{Type::i32},
{Type::i64},
{Type::f32},
{Type::f64},
{Type::v128},
{Type::funcref},
{Type::externref},
{Type::nullref},
{Type::exnref},
}};

// Track unique_ptrs for constructed types to avoid leaks
std::vector<std::unique_ptr<TypeDef>> constructedTypes;

Expand Down Expand Up @@ -349,25 +334,11 @@ bool Type::isNullable() const {
}
}

size_t Type::size() const { return expand().size(); }

const TypeList& Type::expand() const {
if (isBasic()) {
return basicTuples[id];
} else {
auto* typeDef = getDef(*this);
assert(typeDef->isTuple() && "can only expand tuple types");
return typeDef->tuple.types;
}
}

bool Type::operator<(const Type& other) const {
const TypeList& these = expand();
const TypeList& others = other.expand();
return std::lexicographical_compare(these.begin(),
these.end(),
others.begin(),
others.end(),
return std::lexicographical_compare((*this).begin(),
(*this).end(),
dcodeIO marked this conversation as resolved.
Show resolved Hide resolved
other.begin(),
other.end(),
[](const Type& a, const Type& b) {
TODO_SINGLE_COMPOUND(a);
TODO_SINGLE_COMPOUND(b);
Expand Down Expand Up @@ -409,7 +380,8 @@ unsigned Type::getByteSize() const {
}

Type Type::reinterpret() const {
auto singleType = *(*this).begin();
assert(!isTuple() && "Unexpected tuple type");
auto& singleType = *(*this).begin();
dcodeIO marked this conversation as resolved.
Show resolved Hide resolved
switch (singleType.getBasic()) {
case Type::i32:
return f32;
Expand All @@ -419,16 +391,9 @@ Type Type::reinterpret() const {
return i32;
case Type::f64:
return i64;
case Type::v128:
case Type::funcref:
case Type::externref:
case Type::nullref:
case Type::exnref:
case Type::none:
case Type::unreachable:
default:
WASM_UNREACHABLE("invalid type");
}
WASM_UNREACHABLE("invalid type");
}

FeatureSet Type::getFeatures() const {
Expand Down Expand Up @@ -483,9 +448,7 @@ bool Type::isSubType(Type left, Type right) {
return true;
}
if (left.isTuple() && right.isTuple()) {
const auto& leftElems = left.expand();
const auto& rightElems = right.expand();
if (leftElems.size() != rightElems.size()) {
if (left.size() != right.size()) {
return false;
}
for (size_t i = 0; i < left.size(); ++i) {
Expand Down Expand Up @@ -534,6 +497,35 @@ Type Type::getLeastUpperBound(Type a, Type b) {
return Type::externref;
}

Type::Iterator Type::end() const {
if (isTuple()) {
return Iterator(this, getDef(*this)->tuple.types.size());
} else {
// TODO: unreachable is special and expands to {unreachable} currently.
// see also: https://github.com/WebAssembly/binaryen/issues/3062
return Iterator(this, size_t(id != Type::none));
}
}

const Type& Type::Iterator::operator*() const {
if (parent->isTuple()) {
return getDef(*parent)->tuple.types[index];
} else {
// TODO: see comment in Type::end()
assert(index == 0 && parent->id != Type::none && "Index out of bounds");
return *parent;
}
}

const Type& Type::operator[](size_t index) const {
if (isTuple()) {
return getDef(*this)->tuple.types[index];
} else {
assert(index == 0 && "Index out of bounds");
return *begin();
}
}

namespace {

std::ostream&
Expand Down