Skip to content

Implement GC basics #2607

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
30 changes: 23 additions & 7 deletions include/wabt/binary-reader-logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,20 @@ class BinaryReaderLogging : public BinaryReaderDelegate {

Result BeginTypeSection(Offset size) override;
Result OnTypeCount(Index count) override;
Result OnRecursiveType(Index first_type_index, Index type_count) override;
Result OnFuncType(Index index,
Index param_count,
Type* param_types,
Index result_count,
Type* result_types) override;
Result OnStructType(Index index, Index field_count, TypeMut* fields) override;
Result OnArrayType(Index index, TypeMut field) override;
Type* result_types,
GCTypeExtension* gc_ext) override;
Result OnStructType(Index index,
Index field_count,
TypeMut* fields,
GCTypeExtension* gc_ext) override;
Result OnArrayType(Index index,
TypeMut field,
GCTypeExtension* gc_ext) override;
Result EndTypeSection() override;

Result BeginImportSection(Offset size) override;
Expand Down Expand Up @@ -96,9 +103,13 @@ class BinaryReaderLogging : public BinaryReaderDelegate {

Result BeginTableSection(Offset size) override;
Result OnTableCount(Index count) override;
Result OnTable(Index index,
Type elem_type,
const Limits* elem_limits) override;
Result BeginTable(Index index,
Type elem_type,
const Limits* elem_limits,
bool has_init_expr) override;
Result BeginTableInitExpr(Index index) override;
Result EndTableInitExpr(Index index) override;
Result EndTable(Index index) override;
Result EndTableSection() override;

Result BeginMemorySection(Offset size) override;
Expand Down Expand Up @@ -174,14 +185,16 @@ class BinaryReaderLogging : public BinaryReaderDelegate {
Result OnBlockExpr(Type sig_type) override;
Result OnBrExpr(Index depth) override;
Result OnBrIfExpr(Index depth) override;
Result OnBrOnNonNullExpr(Index depth) override;
Result OnBrOnNullExpr(Index depth) override;
Result OnBrTableExpr(Index num_targets,
Index* target_depths,
Index default_target_depth) override;
Result OnCallExpr(Index func_index) override;
Result OnCatchExpr(Index tag_index) override;
Result OnCatchAllExpr() override;
Result OnCallIndirectExpr(Index sig_index, Index table_index) override;
Result OnCallRefExpr() override;
Result OnCallRefExpr(Type sig_type) override;
Result OnCompareExpr(Opcode opcode) override;
Result OnConvertExpr(Opcode opcode) override;
Result OnDelegateExpr(Index depth) override;
Expand Down Expand Up @@ -218,12 +231,14 @@ class BinaryReaderLogging : public BinaryReaderDelegate {
Result OnTableGrowExpr(Index table) override;
Result OnTableSizeExpr(Index table) override;
Result OnTableFillExpr(Index table) override;
Result OnRefAsNonNullExpr() override;
Result OnRefFuncExpr(Index index) override;
Result OnRefNullExpr(Type type) override;
Result OnRefIsNullExpr() override;
Result OnNopExpr() override;
Result OnRethrowExpr(Index depth) override;
Result OnReturnCallExpr(Index func_index) override;
Result OnReturnCallRefExpr(Type sig_type) override;
Result OnReturnCallIndirectExpr(Index sig_index, Index table_index) override;
Result OnReturnExpr() override;
Result OnSelectExpr(Index result_count, Type* result_types) override;
Expand Down Expand Up @@ -424,6 +439,7 @@ class BinaryReaderLogging : public BinaryReaderDelegate {
void LogType(Type type);
void LogTypes(Index type_count, Type* types);
void LogTypes(TypeVector& types);
void LogGCInfo(GCTypeExtension* gc_ext);
void LogField(TypeMut field);

Stream* stream_;
Expand Down
31 changes: 24 additions & 7 deletions include/wabt/binary-reader-nop.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,29 @@ class BinaryReaderNop : public BinaryReaderDelegate {

/* Type section */
Result BeginTypeSection(Offset size) override { return Result::Ok; }
Result OnRecursiveType(Index first_type_index, Index type_count) override {
return Result::Ok;
}
Result OnTypeCount(Index count) override { return Result::Ok; }
Result OnFuncType(Index index,
Index param_count,
Type* param_types,
Index result_count,
Type* result_types) override {
Type* result_types,
GCTypeExtension* gc_ext) override {
return Result::Ok;
}
Result OnStructType(Index index,
Index field_count,
TypeMut* fields) override {
TypeMut* fields,
GCTypeExtension* gc_ext) override {
return Result::Ok;
}
Result OnArrayType(Index index,
TypeMut field,
GCTypeExtension* gc_ext) override {
return Result::Ok;
}
Result OnArrayType(Index index, TypeMut field) override { return Result::Ok; }
Result EndTypeSection() override { return Result::Ok; }

/* Import section */
Expand Down Expand Up @@ -121,11 +130,15 @@ class BinaryReaderNop : public BinaryReaderDelegate {
/* Table section */
Result BeginTableSection(Offset size) override { return Result::Ok; }
Result OnTableCount(Index count) override { return Result::Ok; }
Result OnTable(Index index,
Type elem_type,
const Limits* elem_limits) override {
Result BeginTable(Index index,
Type elem_type,
const Limits* elem_limits,
bool has_init_expr) override {
return Result::Ok;
}
Result BeginTableInitExpr(Index index) override { return Result::Ok; }
Result EndTableInitExpr(Index index) override { return Result::Ok; }
Result EndTable(Index index) override { return Result::Ok; }
Result EndTableSection() override { return Result::Ok; }

/* Memory section */
Expand Down Expand Up @@ -241,6 +254,8 @@ class BinaryReaderNop : public BinaryReaderDelegate {
Result OnBlockExpr(Type sig_type) override { return Result::Ok; }
Result OnBrExpr(Index depth) override { return Result::Ok; }
Result OnBrIfExpr(Index depth) override { return Result::Ok; }
Result OnBrOnNonNullExpr(Index depth) override { return Result::Ok; }
Result OnBrOnNullExpr(Index depth) override { return Result::Ok; }
Result OnBrTableExpr(Index num_targets,
Index* target_depths,
Index default_target_depth) override {
Expand All @@ -250,7 +265,7 @@ class BinaryReaderNop : public BinaryReaderDelegate {
Result OnCallIndirectExpr(Index sig_index, Index table_index) override {
return Result::Ok;
}
Result OnCallRefExpr() override { return Result::Ok; }
Result OnCallRefExpr(Type sig_type) override { return Result::Ok; }
Result OnCatchExpr(Index tag_index) override { return Result::Ok; }
Result OnCatchAllExpr() override { return Result::Ok; }
Result OnCompareExpr(Opcode opcode) override { return Result::Ok; }
Expand Down Expand Up @@ -299,6 +314,7 @@ class BinaryReaderNop : public BinaryReaderDelegate {
Result OnTableGrowExpr(Index table_index) override { return Result::Ok; }
Result OnTableSizeExpr(Index table_index) override { return Result::Ok; }
Result OnTableFillExpr(Index table_index) override { return Result::Ok; }
Result OnRefAsNonNullExpr() override { return Result::Ok; }
Result OnRefFuncExpr(Index func_index) override { return Result::Ok; }
Result OnRefNullExpr(Type type) override { return Result::Ok; }
Result OnRefIsNullExpr() override { return Result::Ok; }
Expand All @@ -308,6 +324,7 @@ class BinaryReaderNop : public BinaryReaderDelegate {
Result OnReturnCallIndirectExpr(Index sig_index, Index table_index) override {
return Result::Ok;
}
Result OnReturnCallRefExpr(Type sig_type) override { return Result::Ok; }
Result OnReturnExpr() override { return Result::Ok; }
Result OnSelectExpr(Index result_count, Type* result_types) override {
return Result::Ok;
Expand Down
36 changes: 28 additions & 8 deletions include/wabt/binary-reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,20 @@ struct ReadBinaryOptions {
bool skip_function_bodies = false;
};

// TODO: Move somewhere else?
// TODO: Move both TypeMut and GCTypeInformation somewhere else?
struct TypeMut {
Type type;
bool mutable_;
};
using TypeMutVector = std::vector<TypeMut>;

// Garbage Collector specific type information
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a core part of the patch. It contains the (sub ...) part of the type. It is declared as a structure, because it is not mandatory.

struct GCTypeExtension {
bool is_final_sub_type;
Index sub_type_count;
Index* sub_types;
};

struct CatchClause {
CatchKind kind;
Index tag;
Expand Down Expand Up @@ -99,15 +106,20 @@ class BinaryReaderDelegate {
/* Type section */
virtual Result BeginTypeSection(Offset size) = 0;
virtual Result OnTypeCount(Index count) = 0;
virtual Result OnRecursiveType(Index first_type_index, Index type_count) = 0;
virtual Result OnFuncType(Index index,
Index param_count,
Type* param_types,
Index result_count,
Type* result_types) = 0;
Type* result_types,
GCTypeExtension* gc_ext) = 0;
virtual Result OnStructType(Index index,
Index field_count,
TypeMut* fields) = 0;
virtual Result OnArrayType(Index index, TypeMut field) = 0;
TypeMut* fields,
GCTypeExtension* gc_ext) = 0;
virtual Result OnArrayType(Index index,
TypeMut field,
GCTypeExtension* gc_ext) = 0;
virtual Result EndTypeSection() = 0;

/* Import section */
Expand Down Expand Up @@ -156,9 +168,13 @@ class BinaryReaderDelegate {
/* Table section */
virtual Result BeginTableSection(Offset size) = 0;
virtual Result OnTableCount(Index count) = 0;
virtual Result OnTable(Index index,
Type elem_type,
const Limits* elem_limits) = 0;
virtual Result BeginTable(Index index,
Type elem_type,
const Limits* elem_limits,
bool has_init_expr) = 0;
virtual Result BeginTableInitExpr(Index index) = 0;
virtual Result EndTableInitExpr(Index index) = 0;
virtual Result EndTable(Index index) = 0;
virtual Result EndTableSection() = 0;

/* Memory section */
Expand Down Expand Up @@ -250,12 +266,14 @@ class BinaryReaderDelegate {
virtual Result OnBlockExpr(Type sig_type) = 0;
virtual Result OnBrExpr(Index depth) = 0;
virtual Result OnBrIfExpr(Index depth) = 0;
virtual Result OnBrOnNonNullExpr(Index depth) = 0;
virtual Result OnBrOnNullExpr(Index depth) = 0;
virtual Result OnBrTableExpr(Index num_targets,
Index* target_depths,
Index default_target_depth) = 0;
virtual Result OnCallExpr(Index func_index) = 0;
virtual Result OnCallIndirectExpr(Index sig_index, Index table_index) = 0;
virtual Result OnCallRefExpr() = 0;
virtual Result OnCallRefExpr(Type sig_type) = 0;
virtual Result OnCatchExpr(Index tag_index) = 0;
virtual Result OnCatchAllExpr() = 0;
virtual Result OnCompareExpr(Opcode opcode) = 0;
Expand Down Expand Up @@ -294,6 +312,7 @@ class BinaryReaderDelegate {
virtual Result OnTableGrowExpr(Index table_index) = 0;
virtual Result OnTableSizeExpr(Index table_index) = 0;
virtual Result OnTableFillExpr(Index table_index) = 0;
virtual Result OnRefAsNonNullExpr() = 0;
virtual Result OnRefFuncExpr(Index func_index) = 0;
virtual Result OnRefNullExpr(Type type) = 0;
virtual Result OnRefIsNullExpr() = 0;
Expand All @@ -303,6 +322,7 @@ class BinaryReaderDelegate {
virtual Result OnReturnCallExpr(Index func_index) = 0;
virtual Result OnReturnCallIndirectExpr(Index sig_index,
Index table_index) = 0;
virtual Result OnReturnCallRefExpr(Type sig_type) = 0;
virtual Result OnSelectExpr(Index result_count, Type* result_types) = 0;
virtual Result OnStoreExpr(Opcode opcode,
Index memidx,
Expand Down
8 changes: 8 additions & 0 deletions include/wabt/expr-visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class ExprVisitor::Delegate {
virtual Result EndBlockExpr(BlockExpr*) = 0;
virtual Result OnBrExpr(BrExpr*) = 0;
virtual Result OnBrIfExpr(BrIfExpr*) = 0;
virtual Result OnBrOnNonNullExpr(BrOnNonNullExpr*) = 0;
virtual Result OnBrOnNullExpr(BrOnNullExpr*) = 0;
virtual Result OnBrTableExpr(BrTableExpr*) = 0;
virtual Result BeginTryTableExpr(TryTableExpr*) = 0;
virtual Result EndTryTableExpr(TryTableExpr*) = 0;
Expand Down Expand Up @@ -109,13 +111,15 @@ class ExprVisitor::Delegate {
virtual Result OnTableGrowExpr(TableGrowExpr*) = 0;
virtual Result OnTableSizeExpr(TableSizeExpr*) = 0;
virtual Result OnTableFillExpr(TableFillExpr*) = 0;
virtual Result OnRefAsNonNullExpr(RefAsNonNullExpr*) = 0;
virtual Result OnRefFuncExpr(RefFuncExpr*) = 0;
virtual Result OnRefNullExpr(RefNullExpr*) = 0;
virtual Result OnRefIsNullExpr(RefIsNullExpr*) = 0;
virtual Result OnNopExpr(NopExpr*) = 0;
virtual Result OnReturnExpr(ReturnExpr*) = 0;
virtual Result OnReturnCallExpr(ReturnCallExpr*) = 0;
virtual Result OnReturnCallIndirectExpr(ReturnCallIndirectExpr*) = 0;
virtual Result OnReturnCallRefExpr(ReturnCallRefExpr*) = 0;
virtual Result OnSelectExpr(SelectExpr*) = 0;
virtual Result OnStoreExpr(StoreExpr*) = 0;
virtual Result OnUnaryExpr(UnaryExpr*) = 0;
Expand Down Expand Up @@ -150,6 +154,8 @@ class ExprVisitor::DelegateNop : public ExprVisitor::Delegate {
Result EndBlockExpr(BlockExpr*) override { return Result::Ok; }
Result OnBrExpr(BrExpr*) override { return Result::Ok; }
Result OnBrIfExpr(BrIfExpr*) override { return Result::Ok; }
Result OnBrOnNonNullExpr(BrOnNonNullExpr*) override { return Result::Ok; };
Result OnBrOnNullExpr(BrOnNullExpr*) override { return Result::Ok; };
Result OnBrTableExpr(BrTableExpr*) override { return Result::Ok; }
Result BeginTryTableExpr(TryTableExpr*) override { return Result::Ok; }
Result EndTryTableExpr(TryTableExpr*) override { return Result::Ok; }
Expand Down Expand Up @@ -186,6 +192,7 @@ class ExprVisitor::DelegateNop : public ExprVisitor::Delegate {
Result OnTableGrowExpr(TableGrowExpr*) override { return Result::Ok; }
Result OnTableSizeExpr(TableSizeExpr*) override { return Result::Ok; }
Result OnTableFillExpr(TableFillExpr*) override { return Result::Ok; }
Result OnRefAsNonNullExpr(RefAsNonNullExpr*) override { return Result::Ok; }
Result OnRefFuncExpr(RefFuncExpr*) override { return Result::Ok; }
Result OnRefNullExpr(RefNullExpr*) override { return Result::Ok; }
Result OnRefIsNullExpr(RefIsNullExpr*) override { return Result::Ok; }
Expand All @@ -195,6 +202,7 @@ class ExprVisitor::DelegateNop : public ExprVisitor::Delegate {
Result OnReturnCallIndirectExpr(ReturnCallIndirectExpr*) override {
return Result::Ok;
}
Result OnReturnCallRefExpr(ReturnCallRefExpr*) override { return Result::Ok; }
Result OnSelectExpr(SelectExpr*) override { return Result::Ok; }
Result OnStoreExpr(StoreExpr*) override { return Result::Ok; }
Result OnUnaryExpr(UnaryExpr*) override { return Result::Ok; }
Expand Down
35 changes: 26 additions & 9 deletions include/wabt/interp/interp-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,30 @@ inline bool FuncType::classof(const ExternType* type) {
}

inline FuncType::FuncType(ValueTypes params, ValueTypes results)
: ExternType(ExternKind::Func), params(params), results(results) {}
: ExternType(ExternKind::Func),
kind(FuncType::TypeKind::Func),
canonical_index(kInvalidIndex),
canonical_sub_index(kInvalidIndex),
is_final_sub_type(true),
recursive_start(0),
recursive_count(0),
params(params),
results(results),
func_types(nullptr) {}

inline FuncType::FuncType(TypeKind kind, ValueTypes params, ValueTypes results)
: ExternType(ExternKind::Func),
kind(kind),
canonical_index(kInvalidIndex),
canonical_sub_index(kInvalidIndex),
is_final_sub_type(true),
recursive_start(0),
recursive_count(0),
params(params),
results(results),
func_types(nullptr) {
assert((kind == TypeKind::Struct || kind == TypeKind::Array) && params.size() == results.size());
}

//// TableType ////
// static
Expand Down Expand Up @@ -422,12 +445,6 @@ void RequireType(ValueType type) {
assert(HasType<T>(type));
}

inline bool TypesMatch(ValueType expected, ValueType actual) {
// Currently there is no subtyping, so expected and actual must match
// exactly. In the future this may be expanded.
return expected == actual;
}

//// Value ////
inline Value WABT_VECTORCALL Value::Make(s32 val) { Value res; res.i32_ = val; res.SetType(ValueType::I32); return res; }
inline Value WABT_VECTORCALL Value::Make(u32 val) { Value res; res.i32_ = val; res.SetType(ValueType::I32); return res; }
Expand Down Expand Up @@ -682,8 +699,8 @@ inline bool Table::classof(const Object* obj) {
}

// static
inline Table::Ptr Table::New(Store& store, TableType type) {
return store.Alloc<Table>(store, type);
inline Table::Ptr Table::New(Store& store, TableType type, Ref init_ref) {
return store.Alloc<Table>(store, type, init_ref);
}

inline const ExternType& Table::extern_type() {
Expand Down
Loading
Loading