Skip to content

Commit c94e5a1

Browse files
author
Zoltan Herczeg
committed
Implement changes of function reference proposal
1 parent 5e81f6a commit c94e5a1

27 files changed

+431
-222
lines changed

include/wabt/ir.h

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace wabt {
3636

3737
struct Module;
3838

39-
enum class VarType {
39+
enum class VarType : uint16_t {
4040
Index,
4141
Name,
4242
};
@@ -45,6 +45,7 @@ struct Var {
4545
explicit Var();
4646
explicit Var(Index index, const Location& loc);
4747
explicit Var(std::string_view name, const Location& loc);
48+
explicit Var(Type type, const Location& loc);
4849
Var(Var&&);
4950
Var(const Var&);
5051
Var& operator=(const Var&);
@@ -54,6 +55,7 @@ struct Var {
5455
VarType type() const { return type_; }
5556
bool is_index() const { return type_ == VarType::Index; }
5657
bool is_name() const { return type_ == VarType::Name; }
58+
bool has_opt_type() const { return opt_type_ < 0; }
5759

5860
Index index() const {
5961
assert(is_index());
@@ -63,17 +65,25 @@ struct Var {
6365
assert(is_name());
6466
return name_;
6567
}
68+
Type::Enum opt_type() const {
69+
assert(has_opt_type());
70+
return static_cast<Type::Enum>(opt_type_);
71+
}
6672

6773
void set_index(Index);
6874
void set_name(std::string&&);
6975
void set_name(std::string_view);
76+
void set_opt_type(Type::Enum);
77+
Type to_type() const;
7078

7179
Location loc;
7280

7381
private:
7482
void Destroy();
7583

7684
VarType type_;
85+
// Can be set to Type::Enum types, 0 represent no optional type.
86+
int16_t opt_type_;
7787
union {
7888
Index index_;
7989
std::string name_;
@@ -544,10 +554,10 @@ using MemoryCopyExpr = MemoryBinaryExpr<ExprType::MemoryCopy>;
544554
template <ExprType TypeEnum>
545555
class RefTypeExpr : public ExprMixin<TypeEnum> {
546556
public:
547-
RefTypeExpr(Type type, const Location& loc = Location())
557+
RefTypeExpr(Var type, const Location& loc = Location())
548558
: ExprMixin<TypeEnum>(loc), type(type) {}
549559

550-
Type type;
560+
Var type;
551561
};
552562

553563
using RefNullExpr = RefTypeExpr<ExprType::RefNull>;
@@ -734,9 +744,7 @@ class CallRefExpr : public ExprMixin<ExprType::CallRef> {
734744
explicit CallRefExpr(const Location& loc = Location())
735745
: ExprMixin<ExprType::CallRef>(loc) {}
736746

737-
// This field is setup only during Validate phase,
738-
// so keep that in mind when you use it.
739-
Var function_type_index;
747+
Var function_type;
740748
};
741749

742750
template <ExprType TypeEnum>
@@ -924,6 +932,8 @@ struct Func {
924932

925933
std::string name;
926934
FuncDeclaration decl;
935+
// Contains references with unknown indicies.
936+
TypeVector local_type_list;
927937
LocalTypes local_types;
928938
BindingHash bindings;
929939
ExprList exprs;
@@ -941,7 +951,7 @@ struct Global {
941951
explicit Global(std::string_view name) : name(name) {}
942952

943953
std::string name;
944-
Type type = Type::Void;
954+
Var type;
945955
bool mutable_ = false;
946956
ExprList init_expr;
947957
};

include/wabt/leb128.h

Lines changed: 1 addition & 0 deletions
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

Lines changed: 3 additions & 14 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 18 additions & 3 deletions
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

Lines changed: 19 additions & 8 deletions
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
@@ -63,20 +65,24 @@ class Type {
6365
: enum_(static_cast<Enum>(code)), type_index_(kInvalidIndex) {}
6466
Type(Enum e) : enum_(e), type_index_(kInvalidIndex) {}
6567
Type(Enum e, Index type_index) : enum_(e), type_index_(type_index) {
66-
assert(e == Enum::Reference);
68+
assert(e == Enum::Reference || e == Enum::Ref ||
69+
e == Enum::RefNull || type_index == kInvalidIndex);
6770
}
6871
constexpr operator Enum() const { return enum_; }
6972

70-
bool IsRef() const {
73+
bool IsNullableRef() const {
7174
return enum_ == Type::ExternRef || enum_ == Type::FuncRef ||
72-
enum_ == Type::Reference || enum_ == Type::ExnRef;
75+
enum_ == Type::Reference || enum_ == Type::ExnRef ||
76+
enum_ == Type::RefNull;
7377
}
7478

75-
bool IsReferenceWithIndex() const { return enum_ == Type::Reference; }
79+
bool IsRef() const {
80+
return IsNullableRef() || enum_ == Type::Ref;
81+
}
7682

77-
bool IsNullableRef() const {
78-
// Currently all reftypes are nullable
79-
return IsRef();
83+
bool IsReferenceWithIndex() const {
84+
return enum_ == Type::Reference || enum_ == Type::Ref ||
85+
enum_ == Type::RefNull;
8086
}
8187

8288
std::string GetName() const {
@@ -95,7 +101,10 @@ class Type {
95101
case Type::Any: return "any";
96102
case Type::ExternRef: return "externref";
97103
case Type::Reference:
104+
case Type::Ref:
98105
return StringPrintf("(ref %d)", type_index_);
106+
case Type::RefNull:
107+
return StringPrintf("(ref null %d)", type_index_);
99108
default:
100109
return StringPrintf("<type_index[%d]>", enum_);
101110
}
@@ -132,7 +141,7 @@ class Type {
132141
}
133142

134143
Index GetReferenceIndex() const {
135-
assert(enum_ == Enum::Reference);
144+
assert(IsReferenceWithIndex());
136145
return type_index_;
137146
}
138147

@@ -151,6 +160,8 @@ class Type {
151160
case Type::ExnRef:
152161
case Type::ExternRef:
153162
case Type::Reference:
163+
case Type::Ref:
164+
case Type::RefNull:
154165
return TypeVector(this, this + 1);
155166

156167
default:

include/wabt/wast-parser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class WastParser {
138138
Result ParseValueTypeList(
139139
TypeVector* out_type_list,
140140
std::unordered_map<uint32_t, std::string>* type_names);
141-
Result ParseRefKind(Type* out_type);
141+
Result ParseRefKind(Var* out_type);
142142
Result ParseRefType(Type* out_type);
143143
bool ParseRefTypeOpt(Type* out_type);
144144
Result ParseQuotedText(std::string* text, bool check_utf8 = true);

src/binary-reader-ir.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ Result BinaryReaderIR::OnImportGlobal(Index import_index,
650650
auto import = std::make_unique<GlobalImport>();
651651
import->module_name = module_name;
652652
import->field_name = field_name;
653-
import->global.type = type;
653+
import->global.type = Var(type, GetLocation());
654654
import->global.mutable_ = mutable_;
655655
module_->AppendField(
656656
std::make_unique<ImportModuleField>(std::move(import), GetLocation()));
@@ -739,7 +739,7 @@ Result BinaryReaderIR::OnGlobalCount(Index count) {
739739
Result BinaryReaderIR::BeginGlobal(Index index, Type type, bool mutable_) {
740740
auto field = std::make_unique<GlobalModuleField>(GetLocation());
741741
Global& global = field->global;
742-
global.type = type;
742+
global.type = Var(type, GetLocation());
743743
global.mutable_ = mutable_;
744744
module_->AppendField(std::move(field));
745745
module_->features_used.simd |= (type == Type::V128);
@@ -1153,7 +1153,7 @@ Result BinaryReaderIR::OnRefFuncExpr(Index func_index) {
11531153

11541154
Result BinaryReaderIR::OnRefNullExpr(Type type) {
11551155
module_->features_used.exceptions |= (type == Type::ExnRef);
1156-
return AppendExpr(std::make_unique<RefNullExpr>(type));
1156+
return AppendExpr(std::make_unique<RefNullExpr>(Var(type, GetLocation())));
11571157
}
11581158

11591159
Result BinaryReaderIR::OnRefIsNullExpr() {

src/binary-reader-logging.cc

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -719,10 +719,15 @@ Result BinaryReaderLogging::OnGenericCustomSection(std::string_view name,
719719
return reader_->name(value); \
720720
}
721721

722-
#define DEFINE_TYPE(name) \
723-
Result BinaryReaderLogging::name(Type type) { \
724-
LOGF(#name "(%s)\n", type.GetName().c_str()); \
725-
return reader_->name(type); \
722+
#define DEFINE_TYPE(name) \
723+
Result BinaryReaderLogging::name(Type type) { \
724+
if (!type.IsReferenceWithIndex()) { \
725+
LOGF(#name "(%s)\n", type.GetName().c_str()); \
726+
} else { \
727+
LOGF(#name "(%s %" PRIindex ")\n", type.GetName().c_str(), \
728+
type.GetReferenceIndex()); \
729+
} \
730+
return reader_->name(type); \
726731
}
727732

728733
#define DEFINE_INDEX_DESC(name, desc) \

src/binary-reader.cc

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ class BinaryReader {
113113
[[nodiscard]] Result ReadU32Leb128(uint32_t* out_value, const char* desc);
114114
[[nodiscard]] Result ReadU64Leb128(uint64_t* out_value, const char* desc);
115115
[[nodiscard]] Result ReadS32Leb128(uint32_t* out_value, const char* desc);
116+
[[nodiscard]] Result ReadS33Leb128(uint64_t* out_value, const char* desc);
116117
[[nodiscard]] Result ReadS64Leb128(uint64_t* out_value, const char* desc);
117118
[[nodiscard]] Result ReadType(Type* out_value, const char* desc);
118119
[[nodiscard]] Result ReadRefType(Type* out_value, const char* desc);
@@ -353,6 +354,15 @@ Result BinaryReader::ReadS32Leb128(uint32_t* out_value, const char* desc) {
353354
return Result::Ok;
354355
}
355356

357+
Result BinaryReader::ReadS33Leb128(uint64_t* out_value, const char* desc) {
358+
const uint8_t* p = state_.data + state_.offset;
359+
const uint8_t* end = state_.data + read_end_;
360+
size_t bytes_read = wabt::ReadS33Leb128(p, end, out_value);
361+
ERROR_UNLESS(bytes_read > 0, "unable to read i33 leb128: %s", desc);
362+
state_.offset += bytes_read;
363+
return Result::Ok;
364+
}
365+
356366
Result BinaryReader::ReadS64Leb128(uint64_t* out_value, const char* desc) {
357367
const uint8_t* p = state_.data + state_.offset;
358368
const uint8_t* end = state_.data + read_end_;
@@ -573,6 +583,8 @@ bool BinaryReader::IsConcreteType(Type type) {
573583
return options_.features.exceptions_enabled();
574584

575585
case Type::Reference:
586+
case Type::Ref:
587+
case Type::RefNull:
576588
return options_.features.function_references_enabled();
577589

578590
default:
@@ -1913,8 +1925,16 @@ Result BinaryReader::ReadInstructions(Offset end_offset, const char* context) {
19131925
}
19141926

19151927
case Opcode::RefNull: {
1928+
uint64_t heap_type;
19161929
Type type;
1917-
CHECK_RESULT(ReadRefType(&type, "ref.null type"));
1930+
CHECK_RESULT(ReadS33Leb128(&heap_type, "ref.null type"));
1931+
1932+
if (static_cast<int64_t>(heap_type) < 0) {
1933+
type = Type(static_cast<int32_t>(heap_type));
1934+
} else {
1935+
type = Type(Type::RefNull, static_cast<Index>(heap_type));
1936+
}
1937+
19181938
CALLBACK(OnRefNullExpr, type);
19191939
CALLBACK(OnOpcodeType, type);
19201940
break;

0 commit comments

Comments
 (0)