Skip to content

Commit df78d5e

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

File tree

11 files changed

+127
-110
lines changed

11 files changed

+127
-110
lines changed

include/wabt/ir.h

Lines changed: 10 additions & 4 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
};
@@ -54,6 +54,7 @@ struct Var {
5454
VarType type() const { return type_; }
5555
bool is_index() const { return type_ == VarType::Index; }
5656
bool is_name() const { return type_ == VarType::Name; }
57+
bool has_opt_type() const { return opt_type_ < 0; }
5758

5859
Index index() const {
5960
assert(is_index());
@@ -63,17 +64,24 @@ struct Var {
6364
assert(is_name());
6465
return name_;
6566
}
67+
Type::Enum opt_type() const {
68+
assert(has_opt_type());
69+
return static_cast<Type::Enum>(opt_type_);
70+
}
6671

6772
void set_index(Index);
6873
void set_name(std::string&&);
6974
void set_name(std::string_view);
75+
void set_opt_type(Type::Enum);
7076

7177
Location loc;
7278

7379
private:
7480
void Destroy();
7581

7682
VarType type_;
83+
// Can be set to Type::Enum types, 0 represent no optional type.
84+
int16_t opt_type_;
7785
union {
7886
Index index_;
7987
std::string name_;
@@ -734,9 +742,7 @@ class CallRefExpr : public ExprMixin<ExprType::CallRef> {
734742
explicit CallRefExpr(const Location& loc = Location())
735743
: ExprMixin<ExprType::CallRef>(loc) {}
736744

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

742748
template <ExprType TypeEnum>

include/wabt/shared-validator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class SharedValidator {
140140
Result EndBrTable(const Location&);
141141
Result OnCall(const Location&, Var func_var);
142142
Result OnCallIndirect(const Location&, Var sig_var, Var table_var);
143-
Result OnCallRef(const Location&, Index* function_type_index);
143+
Result OnCallRef(const Location&, Var function_type_var);
144144
Result OnCatch(const Location&, Var tag_var, bool is_catch_all);
145145
Result OnCompare(const Location&, Opcode);
146146
Result OnConst(const Location&, Type);

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.h

Lines changed: 4 additions & 1 deletion
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+
HeapRef = -0x1c, // 0x64
50+
HeapNullRef = -0x1d, // 0x63
4951
Func = -0x20, // 0x60
5052
Struct = -0x21, // 0x5f
5153
Array = -0x22, // 0x5e
@@ -63,7 +65,8 @@ 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::HeapRef ||
69+
e == Enum::HeapNullRef || type_index == kInvalidIndex);
6770
}
6871
constexpr operator Enum() const { return enum_; }
6972

src/ir-util.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ ModuleContext::Arities ModuleContext::GetExprArity(const Expr& expr) const {
140140
}
141141

142142
case ExprType::CallRef: {
143-
const Var& var = cast<CallRefExpr>(&expr)->function_type_index;
143+
const Var& var = cast<CallRefExpr>(&expr)->function_type;
144144
return {GetFuncParamCount(var) + 1, GetFuncResultCount(var)};
145145
}
146146

src/ir.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,10 +591,10 @@ void MakeTypeBindingReverseMapping(
591591
Var::Var() : Var(kInvalidIndex, Location()) {}
592592

593593
Var::Var(Index index, const Location& loc)
594-
: loc(loc), type_(VarType::Index), index_(index) {}
594+
: loc(loc), type_(VarType::Index), opt_type_(0), index_(index) {}
595595

596596
Var::Var(std::string_view name, const Location& loc)
597-
: loc(loc), type_(VarType::Name), name_(name) {}
597+
: loc(loc), type_(VarType::Name), opt_type_(0), name_(name) {}
598598

599599
Var::Var(Var&& rhs) : Var() {
600600
*this = std::move(rhs);
@@ -644,6 +644,12 @@ void Var::set_name(std::string_view name) {
644644
set_name(std::string(name));
645645
}
646646

647+
void Var::set_opt_type(Type::Enum type) {
648+
assert(static_cast<int32_t>(type) < 0 &&
649+
static_cast<int32_t>(type) >= INT16_MIN);
650+
opt_type_ = static_cast<int16_t>(type);
651+
}
652+
647653
void Var::Destroy() {
648654
if (is_name()) {
649655
Destruct(name_);

src/lexer-keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ mut, TokenType::Mut
556556
nan:arithmetic, TokenType::NanArithmetic
557557
nan:canonical, TokenType::NanCanonical
558558
nop, TokenType::Nop, Opcode::Nop
559+
null, TokenType::Null
559560
offset, TokenType::Offset
560561
output, TokenType::Output
561562
pagesize, TokenType::PageSize

0 commit comments

Comments
 (0)