Skip to content

Commit 22728a7

Browse files
author
Zoltan Herczeg
committed
Support table initializers
1 parent 126985f commit 22728a7

Some content is hidden

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

48 files changed

+2060
-130
lines changed

include/wabt/binary-reader-logging.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,13 @@ class BinaryReaderLogging : public BinaryReaderDelegate {
9696

9797
Result BeginTableSection(Offset size) override;
9898
Result OnTableCount(Index count) override;
99-
Result OnTable(Index index,
100-
Type elem_type,
101-
const Limits* elem_limits) override;
99+
Result BeginTable(Index index,
100+
Type elem_type,
101+
const Limits* elem_limits,
102+
bool has_init_expr) override;
103+
Result BeginTableInitExpr(Index index) override;
104+
Result EndTableInitExpr(Index index) override;
105+
Result EndTable(Index index) override;
102106
Result EndTableSection() override;
103107

104108
Result BeginMemorySection(Offset size) override;

include/wabt/binary-reader-nop.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,15 @@ class BinaryReaderNop : public BinaryReaderDelegate {
121121
/* Table section */
122122
Result BeginTableSection(Offset size) override { return Result::Ok; }
123123
Result OnTableCount(Index count) override { return Result::Ok; }
124-
Result OnTable(Index index,
125-
Type elem_type,
126-
const Limits* elem_limits) override {
124+
Result BeginTable(Index index,
125+
Type elem_type,
126+
const Limits* elem_limits,
127+
bool has_init_expr) override {
127128
return Result::Ok;
128129
}
130+
Result BeginTableInitExpr(Index index) override { return Result::Ok; }
131+
Result EndTableInitExpr(Index index) override { return Result::Ok; }
132+
Result EndTable(Index index) override { return Result::Ok; }
129133
Result EndTableSection() override { return Result::Ok; }
130134

131135
/* Memory section */

include/wabt/binary-reader.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,13 @@ class BinaryReaderDelegate {
156156
/* Table section */
157157
virtual Result BeginTableSection(Offset size) = 0;
158158
virtual Result OnTableCount(Index count) = 0;
159-
virtual Result OnTable(Index index,
160-
Type elem_type,
161-
const Limits* elem_limits) = 0;
159+
virtual Result BeginTable(Index index,
160+
Type elem_type,
161+
const Limits* elem_limits,
162+
bool has_init_expr) = 0;
163+
virtual Result BeginTableInitExpr(Index index) = 0;
164+
virtual Result EndTableInitExpr(Index index) = 0;
165+
virtual Result EndTable(Index index) = 0;
162166
virtual Result EndTableSection() = 0;
163167

164168
/* Memory section */

include/wabt/interp/interp-inl.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -422,12 +422,6 @@ void RequireType(ValueType type) {
422422
assert(HasType<T>(type));
423423
}
424424

425-
inline bool TypesMatch(ValueType expected, ValueType actual) {
426-
// Currently there is no subtyping, so expected and actual must match
427-
// exactly. In the future this may be expanded.
428-
return expected == actual;
429-
}
430-
431425
//// Value ////
432426
inline Value WABT_VECTORCALL Value::Make(s32 val) { Value res; res.i32_ = val; res.SetType(ValueType::I32); return res; }
433427
inline Value WABT_VECTORCALL Value::Make(u32 val) { Value res; res.i32_ = val; res.SetType(ValueType::I32); return res; }
@@ -682,8 +676,8 @@ inline bool Table::classof(const Object* obj) {
682676
}
683677

684678
// static
685-
inline Table::Ptr Table::New(Store& store, TableType type) {
686-
return store.Alloc<Table>(store, type);
679+
inline Table::Ptr Table::New(Store& store, TableType type, Ref init_ref) {
680+
return store.Alloc<Table>(store, type, init_ref);
687681
}
688682

689683
inline const ExternType& Table::extern_type() {

include/wabt/interp/interp.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ using u32x2 = Simd<u32, 2>;
157157

158158
//// Types ////
159159

160+
bool TypesMatch(ValueType expected, ValueType actual);
161+
162+
//// Limits ////
163+
160164
bool CanGrow(const Limits&, u32 old_size, u32 delta, u32* new_size);
161165
Result Match(const Limits& expected,
162166
const Limits& actual,
@@ -331,6 +335,7 @@ struct FuncDesc {
331335

332336
struct TableDesc {
333337
TableType type;
338+
FuncDesc init_func;
334339
};
335340

336341
struct MemoryDesc {
@@ -815,7 +820,7 @@ class Table : public Extern {
815820
static const char* GetTypeName() { return "Table"; }
816821
using Ptr = RefPtr<Table>;
817822

818-
static Table::Ptr New(Store&, TableType);
823+
static Table::Ptr New(Store&, TableType, Ref);
819824

820825
Result Match(Store&, const ImportType&, Trap::Ptr* out_trap) override;
821826

@@ -847,7 +852,7 @@ class Table : public Extern {
847852

848853
private:
849854
friend Store;
850-
explicit Table(Store&, TableType);
855+
explicit Table(Store&, TableType, Ref);
851856
void Mark(Store&) override;
852857

853858
TableType type_;

include/wabt/ir.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,13 +985,14 @@ struct Table {
985985
std::string name;
986986
Limits elem_limits;
987987
Type elem_type;
988+
ExprList init_expr;
988989
};
989990

990991
using ExprListVector = std::vector<ExprList>;
991992

992993
struct ElemSegment {
993994
explicit ElemSegment(std::string_view name) : name(name) {}
994-
uint8_t GetFlags(const Module*) const;
995+
uint8_t GetFlags(const Module*, bool) const;
995996

996997
SegmentKind kind = SegmentKind::Active;
997998
std::string name;

include/wabt/shared-validator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class SharedValidator {
7676
Result EndTypeSection();
7777

7878
Result OnFunction(const Location&, Var sig_var);
79-
Result OnTable(const Location&, Type elem_type, const Limits&);
79+
Result OnTable(const Location&, Type elem_type, const Limits&, bool, bool);
8080
Result OnMemory(const Location&, const Limits&, uint32_t page_size);
8181
Result OnGlobalImport(const Location&, Type type, bool mutable_);
8282
Result OnGlobal(const Location&, Type type, bool mutable_);

src/apply-names.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class NameApplier : public ExprVisitor::DelegateNop {
102102
Result VisitGlobal(Global* global);
103103
Result VisitTag(Tag* tag);
104104
Result VisitExport(Index export_index, Export* export_);
105+
Result VisitTable(Table* table);
105106
Result VisitElemSegment(Index elem_segment_index, ElemSegment* segment);
106107
Result VisitDataSegment(Index data_segment_index, DataSegment* segment);
107108
Result VisitStart(Var* start_var);
@@ -559,6 +560,11 @@ Result NameApplier::VisitExport(Index export_index, Export* export_) {
559560
return Result::Ok;
560561
}
561562

563+
Result NameApplier::VisitTable(Table* table) {
564+
CHECK_RESULT(visitor_.VisitExprList(table->init_expr));
565+
return Result::Ok;
566+
}
567+
562568
Result NameApplier::VisitElemSegment(Index elem_segment_index,
563569
ElemSegment* segment) {
564570
CHECK_RESULT(UseNameForTableVar(&segment->table_var));
@@ -594,6 +600,8 @@ Result NameApplier::VisitModule(Module* module) {
594600
CHECK_RESULT(VisitTag(module->tags[i]));
595601
for (size_t i = 0; i < module->exports.size(); ++i)
596602
CHECK_RESULT(VisitExport(i, module->exports[i]));
603+
for (size_t i = 0; i < module->tables.size(); ++i)
604+
CHECK_RESULT(VisitTable(module->tables[i]));
597605
for (size_t i = 0; i < module->elem_segments.size(); ++i)
598606
CHECK_RESULT(VisitElemSegment(i, module->elem_segments[i]));
599607
for (size_t i = 0; i < module->data_segments.size(); ++i)

src/binary-reader-ir.cc

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,12 @@ class BinaryReaderIR : public BinaryReaderNop {
144144
Result OnFunction(Index index, Index sig_index) override;
145145

146146
Result OnTableCount(Index count) override;
147-
Result OnTable(Index index,
148-
Type elem_type,
149-
const Limits* elem_limits) override;
147+
Result BeginTable(Index index,
148+
Type elem_type,
149+
const Limits* elem_limits,
150+
bool) override;
151+
Result BeginTableInitExpr(Index index) override;
152+
Result EndTableInitExpr(Index index) override;
150153

151154
Result OnMemoryCount(Index count) override;
152155
Result OnMemory(Index index,
@@ -700,9 +703,10 @@ Result BinaryReaderIR::OnTableCount(Index count) {
700703
return Result::Ok;
701704
}
702705

703-
Result BinaryReaderIR::OnTable(Index index,
704-
Type elem_type,
705-
const Limits* elem_limits) {
706+
Result BinaryReaderIR::BeginTable(Index index,
707+
Type elem_type,
708+
const Limits* elem_limits,
709+
bool) {
706710
auto field = std::make_unique<TableModuleField>(GetLocation());
707711
Table& table = field->table;
708712
table.elem_limits = *elem_limits;
@@ -712,6 +716,16 @@ Result BinaryReaderIR::OnTable(Index index,
712716
return Result::Ok;
713717
}
714718

719+
Result BinaryReaderIR::BeginTableInitExpr(Index index) {
720+
assert(index == module_->tables.size() - 1);
721+
Table* table = module_->tables[index];
722+
return BeginInitExpr(&table->init_expr);
723+
}
724+
725+
Result BinaryReaderIR::EndTableInitExpr(Index index) {
726+
return EndInitExpr();
727+
}
728+
715729
Result BinaryReaderIR::OnMemoryCount(Index count) {
716730
WABT_TRY
717731
module_->memories.reserve(module_->num_memory_imports + count);

src/binary-reader-logging.cc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,15 @@ Result BinaryReaderLogging::OnImportTag(Index import_index,
255255
sig_index);
256256
}
257257

258-
Result BinaryReaderLogging::OnTable(Index index,
259-
Type elem_type,
260-
const Limits* elem_limits) {
258+
Result BinaryReaderLogging::BeginTable(Index index,
259+
Type elem_type,
260+
const Limits* elem_limits,
261+
bool has_init_expr) {
261262
char buf[100];
262263
SPrintLimits(buf, sizeof(buf), elem_limits);
263264
LOGF("OnTable(index: %" PRIindex ", elem_type: %s, %s)\n", index,
264265
elem_type.GetName().c_str(), buf);
265-
return reader_->OnTable(index, elem_type, elem_limits);
266+
return reader_->BeginTable(index, elem_type, elem_limits, has_init_expr);
266267
}
267268

268269
Result BinaryReaderLogging::OnMemory(Index index,
@@ -821,6 +822,9 @@ DEFINE_END(EndFunctionSection)
821822

822823
DEFINE_BEGIN(BeginTableSection)
823824
DEFINE_INDEX(OnTableCount)
825+
DEFINE_INDEX(BeginTableInitExpr)
826+
DEFINE_INDEX(EndTableInitExpr)
827+
DEFINE_INDEX(EndTable)
824828
DEFINE_END(EndTableSection)
825829

826830
DEFINE_BEGIN(BeginMemorySection)

0 commit comments

Comments
 (0)