Skip to content

Commit 792e9cc

Browse files
author
Zoltan Herczeg
committed
Type as int64
1 parent 31bab2e commit 792e9cc

File tree

6 files changed

+31
-31
lines changed

6 files changed

+31
-31
lines changed

Diff for: include/wabt/type.h

+23-20
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ using TypeVector = std::vector<Type>;
3434
class Type {
3535
public:
3636
// Matches binary format, do not change.
37-
enum Enum : int32_t {
37+
enum Enum : int64_t {
3838
I32 = -0x01, // 0x7f
3939
I64 = -0x02, // 0x7e
4040
F32 = -0x03, // 0x7d
@@ -45,7 +45,7 @@ class Type {
4545
ExnRef = -0x17, // 0x69
4646
FuncRef = -0x10, // 0x70
4747
ExternRef = -0x11, // 0x6f
48-
Reference = -0x15, // 0x6b
48+
Reference = -0x15, // 0x6b : upper 32 bit contains an index
4949
Func = -0x20, // 0x60
5050
Struct = -0x21, // 0x5f
5151
Array = -0x22, // 0x5e
@@ -59,28 +59,32 @@ class Type {
5959
};
6060

6161
Type() = default; // Provided so Type can be member of a union.
62-
Type(int32_t code)
63-
: enum_(static_cast<Enum>(code)), type_index_(kInvalidIndex) {}
64-
Type(Enum e) : enum_(e), type_index_(kInvalidIndex) {}
65-
Type(Enum e, Index type_index) : enum_(e), type_index_(type_index) {
66-
assert(e == Enum::Reference);
62+
Type(int32_t code) : enum_(code) {}
63+
Type(Enum e) : enum_(static_cast<int64_t>(e)) {}
64+
Type(Enum e, Index type_index) {
65+
assert(Type(e).IsReferenceWithIndex());
66+
enum_ = static_cast<uint32_t>(e) | (static_cast<int64_t>(type_index) << 32);
6767
}
68-
constexpr operator Enum() const { return enum_; }
68+
// Returns the full type, including any properties (e.g. index or nullable)
69+
constexpr operator Enum() const { return static_cast<Enum>(enum_); }
70+
// Returns the type only, excluding any properties (e.g. index or nullable).
71+
constexpr Enum code() const { return static_cast<Enum>(static_cast<int32_t>(enum_)); }
6972

7073
bool IsRef() const {
71-
return enum_ == Type::ExternRef || enum_ == Type::FuncRef ||
72-
enum_ == Type::Reference || enum_ == Type::ExnRef;
74+
Enum type = code();
75+
return type == Type::ExternRef || type == Type::FuncRef ||
76+
type == Type::Reference || type == Type::ExnRef;
7377
}
7478

75-
bool IsReferenceWithIndex() const { return enum_ == Type::Reference; }
79+
bool IsReferenceWithIndex() const { return code() == Type::Reference; }
7680

7781
bool IsNullableRef() const {
7882
// Currently all reftypes are nullable
7983
return IsRef();
8084
}
8185

8286
std::string GetName() const {
83-
switch (enum_) {
87+
switch (code()) {
8488
case Type::I32: return "i32";
8589
case Type::I64: return "i64";
8690
case Type::F32: return "f32";
@@ -95,14 +99,14 @@ class Type {
9599
case Type::Any: return "any";
96100
case Type::ExternRef: return "externref";
97101
case Type::Reference:
98-
return StringPrintf("(ref %d)", type_index_);
102+
return StringPrintf("(ref %d)", GetReferenceIndex());
99103
default:
100-
return StringPrintf("<type_index[%d]>", enum_);
104+
return StringPrintf("<type_index[%d]>", static_cast<int>(code()));
101105
}
102106
}
103107

104108
const char* GetRefKindName() const {
105-
switch (enum_) {
109+
switch (code()) {
106110
case Type::FuncRef: return "func";
107111
case Type::ExternRef: return "extern";
108112
case Type::ExnRef: return "exn";
@@ -132,13 +136,13 @@ class Type {
132136
}
133137

134138
Index GetReferenceIndex() const {
135-
assert(enum_ == Enum::Reference);
136-
return type_index_;
139+
assert(IsReferenceWithIndex());
140+
return static_cast<Index>(enum_ >> 32);
137141
}
138142

139143
TypeVector GetInlineVector() const {
140144
assert(!IsIndex());
141-
switch (enum_) {
145+
switch (code()) {
142146
case Type::Void:
143147
return TypeVector();
144148

@@ -159,8 +163,7 @@ class Type {
159163
}
160164

161165
private:
162-
Enum enum_;
163-
Index type_index_; // Only used for for Type::Reference
166+
int64_t enum_;
164167
};
165168

166169
} // namespace wabt

Diff for: src/binary-reader.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ Result BinaryReader::ReadS64Leb128(uint64_t* out_value, const char* desc) {
365365
Result BinaryReader::ReadType(Type* out_value, const char* desc) {
366366
uint32_t type = 0;
367367
CHECK_RESULT(ReadS32Leb128(&type, desc));
368-
if (static_cast<Type::Enum>(type) == Type::Reference) {
368+
if (type == static_cast<uint32_t>(Type::Reference)) {
369369
uint32_t heap_type = 0;
370370
CHECK_RESULT(ReadS32Leb128(&heap_type, desc));
371371
*out_value = Type(Type::Reference, heap_type);
@@ -555,7 +555,7 @@ Result BinaryReader::ReadField(TypeMut* out_value) {
555555
}
556556

557557
bool BinaryReader::IsConcreteType(Type type) {
558-
switch (type) {
558+
switch (type.code()) {
559559
case Type::I32:
560560
case Type::I64:
561561
case Type::F32:

Diff for: src/c-writer.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -2493,7 +2493,7 @@ void CWriter::WriteElemTableInit(bool active_initialization,
24932493
const ElemSegment* src_segment,
24942494
const Table* dst_table) {
24952495
assert(dst_table->elem_type.IsRef() &&
2496-
dst_table->elem_type != Type::Reference);
2496+
dst_table->elem_type.code() != Type::Reference);
24972497
assert(dst_table->elem_type == src_segment->elem_type);
24982498

24992499
Write(GetReferenceTypeName(dst_table->elem_type), "_table_init(",

Diff for: src/interp/interp-util.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace wabt {
2424
namespace interp {
2525

2626
std::string TypedValueToString(const TypedValue& tv) {
27-
switch (tv.type) {
27+
switch (tv.type.code()) {
2828
case Type::I32:
2929
return StringPrintf("i32:%u", tv.value.Get<s32>());
3030

Diff for: src/type-checker.cc

+2-6
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ std::string TypesToString(const TypeVector& types,
3333
Type ty = types[i];
3434
// NOTE: Reference (and GetName) is also used by (e.g.) objdump, which does
3535
// not apply validation. do this here so as to not break that.
36-
if (ty == Type::Reference && ty.GetReferenceIndex() == kInvalidIndex) {
36+
if (ty.code() == Type::Reference &&
37+
ty.GetReferenceIndex() == kInvalidIndex) {
3738
result += "reference";
3839
} else {
3940
result += types[i].GetName();
@@ -233,11 +234,6 @@ Result TypeChecker::CheckType(Type actual, Type expected) {
233234
return Result::Ok;
234235
}
235236

236-
if (expected == Type::Reference && actual == Type::Reference) {
237-
return expected.GetReferenceIndex() == actual.GetReferenceIndex()
238-
? Result::Ok
239-
: Result::Error;
240-
}
241237
if (actual != expected) {
242238
return Result::Error;
243239
}

Diff for: src/wast-parser.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,8 @@ void ResolveTypeName(
324324
Type& type,
325325
Index index,
326326
const std::unordered_map<uint32_t, std::string>& bindings) {
327-
if (type != Type::Reference || type.GetReferenceIndex() != kInvalidIndex) {
327+
if (type.code() != Type::Reference ||
328+
type.GetReferenceIndex() != kInvalidIndex) {
328329
return;
329330
}
330331

0 commit comments

Comments
 (0)