Skip to content

Commit 3e533cb

Browse files
author
Zoltan Herczeg
committed
Implement full reference support for call_ref, ref_null and variables
Support named references for globals, locals, tables, elems Support named references for call_ref, ref_null
1 parent 94422ea commit 3e533cb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+883
-431
lines changed

include/wabt/binary-reader-logging.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ class BinaryReaderLogging : public BinaryReaderDelegate {
181181
Result OnCatchExpr(Index tag_index) override;
182182
Result OnCatchAllExpr() override;
183183
Result OnCallIndirectExpr(Index sig_index, Index table_index) override;
184-
Result OnCallRefExpr() override;
184+
Result OnCallRefExpr(Type sig_type) override;
185185
Result OnCompareExpr(Opcode opcode) override;
186186
Result OnConvertExpr(Opcode opcode) override;
187187
Result OnDelegateExpr(Index depth) override;

include/wabt/binary-reader-nop.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ class BinaryReaderNop : public BinaryReaderDelegate {
250250
Result OnCallIndirectExpr(Index sig_index, Index table_index) override {
251251
return Result::Ok;
252252
}
253-
Result OnCallRefExpr() override { return Result::Ok; }
253+
Result OnCallRefExpr(Type sig_type) override { return Result::Ok; }
254254
Result OnCatchExpr(Index tag_index) override { return Result::Ok; }
255255
Result OnCatchAllExpr() override { return Result::Ok; }
256256
Result OnCompareExpr(Opcode opcode) override { return Result::Ok; }

include/wabt/binary-reader.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ class BinaryReaderDelegate {
255255
Index default_target_depth) = 0;
256256
virtual Result OnCallExpr(Index func_index) = 0;
257257
virtual Result OnCallIndirectExpr(Index sig_index, Index table_index) = 0;
258-
virtual Result OnCallRefExpr() = 0;
258+
virtual Result OnCallRefExpr(Type sig_type) = 0;
259259
virtual Result OnCatchExpr(Index tag_index) = 0;
260260
virtual Result OnCatchAllExpr() = 0;
261261
virtual Result OnCompareExpr(Opcode opcode) = 0;

include/wabt/ir.h

+34-11
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,41 @@ namespace wabt {
3636

3737
struct Module;
3838

39-
enum class VarType {
39+
enum class VarType : uint16_t {
4040
Index,
4141
Name,
4242
};
4343

4444
struct Var {
45+
// Var can represent variables or types.
46+
47+
// Represent a variable:
48+
// has_opt_type() is false
49+
// Only used by wast-parser
50+
51+
// Represent a type:
52+
// has_opt_type() is true, is_index() is true
53+
// type can be get by to_type()
54+
// Binary reader only constructs this variant
55+
56+
// Represent both a variable and a type:
57+
// has_opt_type() is true, is_name() is true
58+
// A reference, which index is unknown
59+
// Only used by wast-parser
60+
4561
explicit Var();
4662
explicit Var(Index index, const Location& loc);
4763
explicit Var(std::string_view name, const Location& loc);
64+
explicit Var(Type type, const Location& loc);
4865
Var(Var&&);
4966
Var(const Var&);
5067
Var& operator=(const Var&);
5168
Var& operator=(Var&&);
5269
~Var();
5370

54-
VarType type() const { return type_; }
5571
bool is_index() const { return type_ == VarType::Index; }
5672
bool is_name() const { return type_ == VarType::Name; }
73+
bool has_opt_type() const { return opt_type_ < 0; }
5774

5875
Index index() const {
5976
assert(is_index());
@@ -63,17 +80,25 @@ struct Var {
6380
assert(is_name());
6481
return name_;
6582
}
83+
Type::Enum opt_type() const {
84+
assert(has_opt_type());
85+
return static_cast<Type::Enum>(opt_type_);
86+
}
6687

6788
void set_index(Index);
6889
void set_name(std::string&&);
6990
void set_name(std::string_view);
91+
void set_opt_type(Type::Enum);
92+
Type to_type() const;
7093

7194
Location loc;
7295

7396
private:
7497
void Destroy();
7598

7699
VarType type_;
100+
// Can be set to Type::Enum types, Type::Any represent no optional type.
101+
int16_t opt_type_;
77102
union {
78103
Index index_;
79104
std::string name_;
@@ -556,10 +581,10 @@ using MemoryCopyExpr = MemoryBinaryExpr<ExprType::MemoryCopy>;
556581
template <ExprType TypeEnum>
557582
class RefTypeExpr : public ExprMixin<TypeEnum> {
558583
public:
559-
RefTypeExpr(Type type, const Location& loc = Location())
584+
RefTypeExpr(Var type, const Location& loc = Location())
560585
: ExprMixin<TypeEnum>(loc), type(type) {}
561586

562-
Type type;
587+
Var type;
563588
};
564589

565590
using RefNullExpr = RefTypeExpr<ExprType::RefNull>;
@@ -746,9 +771,7 @@ class CallRefExpr : public ExprMixin<ExprType::CallRef> {
746771
explicit CallRefExpr(const Location& loc = Location())
747772
: ExprMixin<ExprType::CallRef>(loc) {}
748773

749-
// This field is setup only during Validate phase,
750-
// so keep that in mind when you use it.
751-
Var function_type_index;
774+
Var sig_type;
752775
};
753776

754777
template <ExprType TypeEnum>
@@ -958,18 +981,18 @@ struct Global {
958981
explicit Global(std::string_view name) : name(name) {}
959982

960983
std::string name;
961-
Type type = Type::Void;
984+
Var type;
962985
bool mutable_ = false;
963986
ExprList init_expr;
964987
};
965988

966989
struct Table {
967990
explicit Table(std::string_view name)
968-
: name(name), elem_type(Type::FuncRef) {}
991+
: name(name), elem_type(Type::FuncRef, Location()) {}
969992

970993
std::string name;
971994
Limits elem_limits;
972-
Type elem_type;
995+
Var elem_type;
973996
};
974997

975998
using ExprListVector = std::vector<ExprList>;
@@ -981,7 +1004,7 @@ struct ElemSegment {
9811004
SegmentKind kind = SegmentKind::Active;
9821005
std::string name;
9831006
Var table_var;
984-
Type elem_type;
1007+
Var elem_type;
9851008
ExprList offset;
9861009
ExprListVector elem_exprs;
9871010
};

include/wabt/leb128.h

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ void WriteS32Leb128(Stream* stream, T value, const char* desc) {
6363
size_t ReadU32Leb128(const uint8_t* p, const uint8_t* end, uint32_t* out_value);
6464
size_t ReadU64Leb128(const uint8_t* p, const uint8_t* end, uint64_t* out_value);
6565
size_t ReadS32Leb128(const uint8_t* p, const uint8_t* end, uint32_t* out_value);
66+
size_t ReadS33Leb128(const uint8_t* p, const uint8_t* end, uint64_t* out_value);
6667
size_t ReadS64Leb128(const uint8_t* p, const uint8_t* end, uint64_t* out_value);
6768

6869
} // namespace wabt

include/wabt/shared-validator.h

+3-14
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ struct ValidateOptions {
4343
class SharedValidator {
4444
public:
4545
WABT_DISALLOW_COPY_AND_ASSIGN(SharedValidator);
46+
using FuncType = TypeChecker::FuncType;
4647
SharedValidator(Errors*, const ValidateOptions& options);
4748

4849
// TODO: Move into SharedValidator?
@@ -140,7 +141,7 @@ class SharedValidator {
140141
Result EndBrTable(const Location&);
141142
Result OnCall(const Location&, Var func_var);
142143
Result OnCallIndirect(const Location&, Var sig_var, Var table_var);
143-
Result OnCallRef(const Location&, Index* function_type_index);
144+
Result OnCallRef(const Location&, Var function_type_var);
144145
Result OnCatch(const Location&, Var tag_var, bool is_catch_all);
145146
Result OnCompare(const Location&, Opcode);
146147
Result OnConst(const Location&, Type);
@@ -177,7 +178,7 @@ class SharedValidator {
177178
Result OnNop(const Location&);
178179
Result OnRefFunc(const Location&, Var func_var);
179180
Result OnRefIsNull(const Location&);
180-
Result OnRefNull(const Location&, Type type);
181+
Result OnRefNull(const Location&, Var func_type_var);
181182
Result OnRethrow(const Location&, Var depth);
182183
Result OnReturnCall(const Location&, Var func_var);
183184
Result OnReturnCallIndirect(const Location&, Var sig_var, Var table_var);
@@ -220,18 +221,6 @@ class SharedValidator {
220221
Result OnUnreachable(const Location&);
221222

222223
private:
223-
struct FuncType {
224-
FuncType() = default;
225-
FuncType(const TypeVector& params,
226-
const TypeVector& results,
227-
Index type_index)
228-
: params(params), results(results), type_index(type_index) {}
229-
230-
TypeVector params;
231-
TypeVector results;
232-
Index type_index;
233-
};
234-
235224
struct StructType {
236225
StructType() = default;
237226
StructType(const TypeMutVector& fields) : fields(fields) {}

include/wabt/token.def

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ WABT_TOKEN(Module, "module")
5555
WABT_TOKEN(Mut, "mut")
5656
WABT_TOKEN(NanArithmetic, "nan:arithmetic")
5757
WABT_TOKEN(NanCanonical, "nan:canonical")
58+
WABT_TOKEN(Null, "null")
5859
WABT_TOKEN(Offset, "offset")
5960
WABT_TOKEN(Output, "output")
6061
WABT_TOKEN(PageSize, "pagesize")

include/wabt/type-checker.h

+18-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <functional>
2121
#include <type_traits>
2222
#include <vector>
23+
#include <map>
2324

2425
#include "wabt/common.h"
2526
#include "wabt/feature.h"
@@ -31,6 +32,18 @@ class TypeChecker {
3132
public:
3233
using ErrorCallback = std::function<void(const char* msg)>;
3334

35+
struct FuncType {
36+
FuncType() = default;
37+
FuncType(const TypeVector& params,
38+
const TypeVector& results,
39+
Index type_index)
40+
: params(params), results(results), type_index(type_index) {}
41+
42+
TypeVector params;
43+
TypeVector results;
44+
Index type_index;
45+
};
46+
3447
struct Label {
3548
Label(LabelType,
3649
const TypeVector& param_types,
@@ -48,7 +61,8 @@ class TypeChecker {
4861
bool unreachable;
4962
};
5063

51-
explicit TypeChecker(const Features& features) : features_(features) {}
64+
explicit TypeChecker(const Features& features, std::map<Index, FuncType>& func_types)
65+
: features_(features), func_types_(func_types) {}
5266

5367
void set_error_callback(const ErrorCallback& error_callback) {
5468
error_callback_ = error_callback;
@@ -80,7 +94,7 @@ class TypeChecker {
8094
Result OnCallIndirect(const TypeVector& param_types,
8195
const TypeVector& result_types,
8296
const Limits& table_limits);
83-
Result OnIndexedFuncRef(Index* out_index);
97+
Result OnCallRef(Type);
8498
Result OnReturnCall(const TypeVector& param_types,
8599
const TypeVector& result_types);
86100
Result OnReturnCallIndirect(const TypeVector& param_types,
@@ -141,7 +155,7 @@ class TypeChecker {
141155
Result BeginInitExpr(Type type);
142156
Result EndInitExpr();
143157

144-
static Result CheckType(Type actual, Type expected);
158+
Result CheckType(Type actual, Type expected);
145159

146160
private:
147161
void WABT_PRINTF_FORMAT(2, 3) PrintError(const char* fmt, ...);
@@ -210,6 +224,7 @@ class TypeChecker {
210224
// to represent "any".
211225
TypeVector* br_table_sig_ = nullptr;
212226
Features features_;
227+
std::map<Index, FuncType>& func_types_;
213228
};
214229

215230
} // namespace wabt

include/wabt/type.h

+41-10
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class Type {
4646
FuncRef = -0x10, // 0x70
4747
ExternRef = -0x11, // 0x6f
4848
Reference = -0x15, // 0x6b
49+
Ref = -0x1c, // 0x64
50+
RefNull = -0x1d, // 0x63
4951
Func = -0x20, // 0x60
5052
Struct = -0x21, // 0x5f
5153
Array = -0x22, // 0x5e
@@ -58,6 +60,12 @@ class Type {
5860
I32U = 7, // Not actually specified, but used internally with load/store
5961
};
6062

63+
// Used by FuncRef / ExternRef
64+
enum GenericReferenceType : uint32_t {
65+
ReferenceOrNull = 0,
66+
ReferenceNonNull = 1,
67+
};
68+
6169
Type() = default; // Provided so Type can be member of a union.
6270
Type(int32_t code)
6371
: enum_(static_cast<Enum>(code)), type_index_(0) {
@@ -67,7 +75,7 @@ class Type {
6775
assert(!EnumIsReferenceWithIndex(enum_));
6876
}
6977
Type(Enum e, Index type_index) : enum_(e), type_index_(type_index) {
70-
assert(EnumIsReferenceWithIndex(e));
78+
assert(EnumIsReferenceWithIndex(e) || (IsNonTypedRef() && (type_index_ == ReferenceOrNull || type_index_ == ReferenceNonNull)));
7179
}
7280
constexpr operator Enum() const { return enum_; }
7381

@@ -90,14 +98,25 @@ class Type {
9098

9199
bool IsRef() const {
92100
return enum_ == Type::ExternRef || enum_ == Type::FuncRef ||
93-
enum_ == Type::Reference || enum_ == Type::ExnRef;
101+
enum_ == Type::Reference || enum_ == Type::ExnRef ||
102+
enum_ == Type::RefNull || enum_ == Type::Ref;
103+
}
104+
105+
bool IsNullableRef() const {
106+
return enum_ == Type::Reference || enum_ == Type::ExnRef ||
107+
enum_ == Type::RefNull ||
108+
((enum_ == Type::ExternRef || enum_ == Type::FuncRef) && type_index_ == ReferenceOrNull);
94109
}
95110

96111
bool IsReferenceWithIndex() const { return EnumIsReferenceWithIndex(enum_); }
97112

98-
bool IsNullableRef() const {
99-
// Currently all reftypes are nullable
100-
return IsRef();
113+
bool IsNonTypedRef() const {
114+
return EnumIsNonTypedRef(enum_);
115+
}
116+
117+
bool IsNullableNonTypedRef() const {
118+
assert(EnumIsNonTypedRef(enum_));
119+
return type_index_ == ReferenceOrNull;
101120
}
102121

103122
std::string GetName() const {
@@ -110,13 +129,18 @@ class Type {
110129
case Type::I8: return "i8";
111130
case Type::I16: return "i16";
112131
case Type::ExnRef: return "exnref";
113-
case Type::FuncRef: return "funcref";
114132
case Type::Func: return "func";
115133
case Type::Void: return "void";
116134
case Type::Any: return "any";
117-
case Type::ExternRef: return "externref";
135+
case Type::FuncRef:
136+
return type_index_ == ReferenceOrNull ? "funcref" : "(ref func)";
137+
case Type::ExternRef:
138+
return type_index_ == ReferenceOrNull ? "externref" : "(ref extern)";
118139
case Type::Reference:
140+
case Type::Ref:
119141
return StringPrintf("(ref %d)", type_index_);
142+
case Type::RefNull:
143+
return StringPrintf("(ref null %d)", type_index_);
120144
default:
121145
return StringPrintf("<type_index[%d]>", enum_);
122146
}
@@ -153,7 +177,7 @@ class Type {
153177
}
154178

155179
Index GetReferenceIndex() const {
156-
assert(enum_ == Enum::Reference);
180+
assert(IsReferenceWithIndex());
157181
return type_index_;
158182
}
159183

@@ -172,18 +196,25 @@ class Type {
172196
case Type::ExnRef:
173197
case Type::ExternRef:
174198
case Type::Reference:
199+
case Type::Ref:
200+
case Type::RefNull:
175201
return TypeVector(this, this + 1);
176202

177203
default:
178204
WABT_UNREACHABLE;
179205
}
180206
}
181207

182-
private:
183208
static bool EnumIsReferenceWithIndex(Enum value) {
184-
return value == Type::Reference;
209+
return value == Type::Reference || value == Type::Ref ||
210+
value == Type::RefNull;
185211
}
186212

213+
static bool EnumIsNonTypedRef(Enum value) {
214+
return value == Type::ExternRef || value == Type::FuncRef;
215+
}
216+
217+
private:
187218
Enum enum_;
188219
// This index is 0 for non-references, so a zeroed
189220
// memory area represents a valid Type::Any type.

0 commit comments

Comments
 (0)