Skip to content

Commit

Permalink
Create explicit class in the IR for initializer expressions. NFC
Browse files Browse the repository at this point in the history
Rather than treating an init expression as a raw expressions list wrap
it into its own class.  While this does nothing now I plan to followup
by attaching an end_loc (like we do for Block) so that we can have the
validator handle checking the correct placement of the END instruction.

Its also good for readability to have an explict class for this class
of expression list.
  • Loading branch information
sbc100 committed Dec 3, 2021
1 parent 0dfa83c commit da53bef
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 83 deletions.
10 changes: 5 additions & 5 deletions src/apply-names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ Result NameApplier::VisitFunc(Index func_index, Func* func) {
}

Result NameApplier::VisitGlobal(Global* global) {
CHECK_RESULT(visitor_.VisitExprList(global->init_expr));
CHECK_RESULT(visitor_.VisitExprList(global->init_expr.exprs));
return Result::Ok;
}

Expand All @@ -460,9 +460,9 @@ Result NameApplier::VisitExport(Index export_index, Export* export_) {
Result NameApplier::VisitElemSegment(Index elem_segment_index,
ElemSegment* segment) {
CHECK_RESULT(UseNameForTableVar(&segment->table_var));
CHECK_RESULT(visitor_.VisitExprList(segment->offset));
for (ExprList& elem_expr : segment->elem_exprs) {
Expr* expr = &elem_expr.front();
CHECK_RESULT(visitor_.VisitExprList(segment->offset.exprs));
for (InitExpr& init_expr : segment->elem_exprs) {
Expr* expr = &init_expr.exprs.front();
if (expr->type() == ExprType::RefFunc) {
CHECK_RESULT(UseNameForFuncVar(&cast<RefFuncExpr>(expr)->var));
}
Expand All @@ -473,7 +473,7 @@ Result NameApplier::VisitElemSegment(Index elem_segment_index,
Result NameApplier::VisitDataSegment(Index data_segment_index,
DataSegment* segment) {
CHECK_RESULT(UseNameForMemoryVar(&segment->memory_var));
CHECK_RESULT(visitor_.VisitExprList(segment->offset));
CHECK_RESULT(visitor_.VisitExprList(segment->offset.exprs));
return Result::Ok;
}

Expand Down
12 changes: 6 additions & 6 deletions src/binary-reader-ir.cc
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ class BinaryReaderIR : public BinaryReaderNop {

Func* current_func_ = nullptr;
std::vector<LabelNode> label_stack_;
ExprList* current_init_expr_ = nullptr;
InitExpr* current_init_expr_ = nullptr;
bool current_init_expr_ended_ = false;
const char* filename_;
};
Expand Down Expand Up @@ -369,7 +369,7 @@ Result BinaryReaderIR::TopLabelExpr(LabelNode** label, Expr** expr) {
Result BinaryReaderIR::AppendExpr(std::unique_ptr<Expr> expr) {
expr->loc = GetLocation();
if (current_init_expr_) {
current_init_expr_->push_back(std::move(expr));
current_init_expr_->exprs.push_back(std::move(expr));
} else {
LabelNode* label;
CHECK_RESULT(TopLabel(&label));
Expand Down Expand Up @@ -1200,8 +1200,8 @@ Result BinaryReaderIR::OnElemSegmentElemExpr_RefNull(Index segment_index,
assert(segment_index == module_->elem_segments.size() - 1);
ElemSegment* segment = module_->elem_segments[segment_index];
Location loc = GetLocation();
ExprList init_expr;
init_expr.push_back(MakeUnique<RefNullExpr>(type, loc));
InitExpr init_expr;
init_expr.exprs.push_back(MakeUnique<RefNullExpr>(type, loc));
segment->elem_exprs.push_back(std::move(init_expr));
return Result::Ok;
}
Expand All @@ -1211,8 +1211,8 @@ Result BinaryReaderIR::OnElemSegmentElemExpr_RefFunc(Index segment_index,
assert(segment_index == module_->elem_segments.size() - 1);
ElemSegment* segment = module_->elem_segments[segment_index];
Location loc = GetLocation();
ExprList init_expr;
init_expr.push_back(MakeUnique<RefFuncExpr>(Var(func_index, loc), loc));
InitExpr init_expr;
init_expr.exprs.push_back(MakeUnique<RefFuncExpr>(Var(func_index, loc), loc));
segment->elem_exprs.push_back(std::move(init_expr));
return Result::Ok;
}
Expand Down
14 changes: 7 additions & 7 deletions src/binary-writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ class BinaryWriter {
const char* desc);
void WriteExpr(const Func* func, const Expr* expr);
void WriteExprList(const Func* func, const ExprList& exprs);
void WriteInitExpr(const ExprList& expr);
void WriteInitExpr(const InitExpr& expr);
void WriteFuncLocals(const Func* func, const LocalTypes& local_types);
void WriteFunc(const Func* func);
void WriteTable(const Table* table);
Expand Down Expand Up @@ -1112,8 +1112,8 @@ void BinaryWriter::WriteExprList(const Func* func, const ExprList& exprs) {
}
}

void BinaryWriter::WriteInitExpr(const ExprList& expr) {
WriteExprList(nullptr, expr);
void BinaryWriter::WriteInitExpr(const InitExpr& expr) {
WriteExprList(nullptr, expr.exprs);
WriteOpcode(stream_, Opcode::End);
}

Expand Down Expand Up @@ -1539,13 +1539,13 @@ Result BinaryWriter::WriteModule() {
// preceeded by length
WriteU32Leb128(stream_, segment->elem_exprs.size(), "num elems");
if (flags & SegUseElemExprs) {
for (const ExprList& elem_expr : segment->elem_exprs) {
for (const InitExpr& elem_expr : segment->elem_exprs) {
WriteInitExpr(elem_expr);
}
} else {
for (const ExprList& elem_expr : segment->elem_exprs) {
assert(elem_expr.size() == 1);
const Expr* expr = &elem_expr.front();
for (const InitExpr& elem_expr : segment->elem_exprs) {
assert(elem_expr.exprs.size() == 1);
const Expr* expr = &elem_expr.exprs.front();
assert(expr->type() == ExprType::RefFunc);
WriteU32Leb128(stream_,
module_->GetFuncIndex(cast<RefFuncExpr>(expr)->var),
Expand Down
14 changes: 8 additions & 6 deletions src/c-writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class CWriter {
void Write(const StackVar&);
void Write(const ResultType&);
void Write(const Const&);
void WriteInitExpr(const ExprList&);
void WriteInitExpr(const InitExpr&);
std::string GenerateHeaderGuard() const;
void WriteSourceTop();
void WriteMultivalueTypes();
Expand Down Expand Up @@ -782,7 +782,9 @@ void CWriter::Write(const Const& const_) {
}
}

void CWriter::WriteInitExpr(const ExprList& expr_list) {
void CWriter::WriteInitExpr(const InitExpr& init_expr) {
const ExprList& expr_list = init_expr.exprs;

if (expr_list.empty())
return;

Expand Down Expand Up @@ -981,7 +983,7 @@ void CWriter::WriteGlobals() {
for (const Global* global : module_->globals) {
bool is_import = global_index < module_->num_global_imports;
if (!is_import) {
assert(!global->init_expr.empty());
assert(!global->init_expr.exprs.empty());
Write(GlobalName(global->name), " = ");
WriteInitExpr(global->init_expr);
Write(";", Newline());
Expand Down Expand Up @@ -1111,11 +1113,11 @@ void CWriter::WriteElemInitializers() {
Write(";", Newline());

size_t i = 0;
for (const ExprList& elem_expr : elem_segment->elem_exprs) {
for (const InitExpr& elem_expr : elem_segment->elem_exprs) {
// We don't support the bulk-memory proposal here, so we know that we
// don't have any passive segments (where ref.null can be used).
assert(elem_expr.size() == 1);
const Expr* expr = &elem_expr.front();
assert(elem_expr.exprs.size() == 1);
const Expr* expr = &elem_expr.exprs.front();
assert(expr->type() == ExprType::RefFunc);
const Func* func = module_->GetFunc(cast<RefFuncExpr>(expr)->var);
Index func_type_index = module_->GetFuncTypeIndex(func->decl.type_var);
Expand Down
8 changes: 4 additions & 4 deletions src/decompiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@ struct Decompiler {
// FIXME: make this less expensive with a binary search or whatever.
for (auto dat : mc.module.data_segments) {
uint64_t dat_base;
if (dat->offset.size() == 1 &&
ConstIntVal(&dat->offset.front(), dat_base) &&
if (dat->offset.exprs.size() == 1 &&
ConstIntVal(&dat->offset.exprs.front(), dat_base) &&
abs_base >= dat_base &&
abs_base < dat_base + dat->data.size()) {
// We are inside the range of this data segment!
Expand Down Expand Up @@ -719,7 +719,7 @@ struct Decompiler {
CheckImportExport(s, ExternalKind::Global, global_index, g->name);
s += cat("global ", g->name, ":", GetDecompTypeName(g->type));
if (!is_import) {
s += cat(" = ", InitExp(g->init_expr));
s += cat(" = ", InitExp(g->init_expr.exprs));
}
s += ";\n";
global_index++;
Expand All @@ -745,7 +745,7 @@ struct Decompiler {

// Data.
for (auto dat : mc.module.data_segments) {
s += cat("data ", dat->name, "(offset: ", InitExp(dat->offset), ") =");
s += cat("data ", dat->name, "(offset: ", InitExp(dat->offset.exprs), ") =");
auto ds = BinaryToString(dat->data);
if (ds.size() > target_exp_width / 2) {
s += "\n";
Expand Down
4 changes: 2 additions & 2 deletions src/ir.cc
Original file line number Diff line number Diff line change
Expand Up @@ -667,8 +667,8 @@ uint8_t ElemSegment::GetFlags(const Module* module) const {
all_ref_func =
all_ref_func &&
std::all_of(elem_exprs.begin(), elem_exprs.end(),
[](const ExprList& elem_expr) {
return elem_expr.front().type() == ExprType::RefFunc;
[](const InitExpr& elem_expr) {
return elem_expr.exprs.front().type() == ExprType::RefFunc;
});
if (!all_ref_func) {
flags |= SegUseElemExprs;
Expand Down
17 changes: 12 additions & 5 deletions src/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,13 @@ struct Block {
Location end_loc;
};

struct InitExpr {
InitExpr() = default;
explicit InitExpr(ExprList exprs) : exprs(std::move(exprs)) {}

ExprList exprs;
};

struct Catch {
explicit Catch(const Location& loc = Location()) : loc(loc) {}
explicit Catch(const Var& var, const Location& loc = Location())
Expand Down Expand Up @@ -835,7 +842,7 @@ struct Global {
std::string name;
Type type = Type::Void;
bool mutable_ = false;
ExprList init_expr;
InitExpr init_expr;
};

struct Table {
Expand All @@ -847,7 +854,7 @@ struct Table {
Type elem_type;
};

typedef std::vector<ExprList> ExprListVector;
typedef std::vector<InitExpr> InitExprVector;

struct ElemSegment {
explicit ElemSegment(string_view name) : name(name.to_string()) {}
Expand All @@ -857,8 +864,8 @@ struct ElemSegment {
std::string name;
Var table_var;
Type elem_type;
ExprList offset;
ExprListVector elem_exprs;
InitExpr offset;
InitExprVector elem_exprs;
};

struct Memory {
Expand All @@ -875,7 +882,7 @@ struct DataSegment {
SegmentKind kind = SegmentKind::Active;
std::string name;
Var memory_var;
ExprList offset;
InitExpr offset;
std::vector<uint8_t> data;
};

Expand Down
14 changes: 7 additions & 7 deletions src/resolve-names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ void NameResolver::VisitExport(Export* export_) {
}

void NameResolver::VisitGlobal(Global* global) {
visitor_.VisitExprList(global->init_expr);
visitor_.VisitExprList(global->init_expr.exprs);
}

void NameResolver::VisitTag(Tag* tag) {
Expand All @@ -525,18 +525,18 @@ void NameResolver::VisitTag(Tag* tag) {

void NameResolver::VisitElemSegment(ElemSegment* segment) {
ResolveTableVar(&segment->table_var);
visitor_.VisitExprList(segment->offset);
for (ExprList& elem_expr : segment->elem_exprs) {
if (elem_expr.size() == 1 &&
elem_expr.front().type() == ExprType::RefFunc) {
ResolveFuncVar(&cast<RefFuncExpr>(&elem_expr.front())->var);
visitor_.VisitExprList(segment->offset.exprs);
for (InitExpr& elem_expr : segment->elem_exprs) {
if (elem_expr.exprs.size() == 1 &&
elem_expr.exprs.front().type() == ExprType::RefFunc) {
ResolveFuncVar(&cast<RefFuncExpr>(&elem_expr.exprs.front())->var);
}
}
}

void NameResolver::VisitDataSegment(DataSegment* segment) {
ResolveMemoryVar(&segment->memory_var);
visitor_.VisitExprList(segment->offset);
visitor_.VisitExprList(segment->offset.exprs);
}

Result NameResolver::VisitModule(Module* module) {
Expand Down
22 changes: 11 additions & 11 deletions src/validator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -731,8 +731,8 @@ Result Validator::CheckModule() {
result_ |=
validator_.OnGlobal(field.loc, f->global.type, f->global.mutable_);

if (f->global.init_expr.size() == 1) {
const Expr* expr = &f->global.init_expr.front();
if (f->global.init_expr.exprs.size() == 1) {
const Expr* expr = &f->global.init_expr.exprs.front();

switch (expr->type()) {
case ExprType::Const:
Expand Down Expand Up @@ -798,8 +798,8 @@ Result Validator::CheckModule() {
validator_.OnElemSegmentElemType(f->elem_segment.elem_type);

// Init expr.
if (f->elem_segment.offset.size() == 1) {
const Expr* expr = &f->elem_segment.offset.front();
if (f->elem_segment.offset.exprs.size() == 1) {
const Expr* expr = &f->elem_segment.offset.exprs.front();

switch (expr->type()) {
case ExprType::Const:
Expand All @@ -818,14 +818,14 @@ Result Validator::CheckModule() {
result_ |= validator_.OnElemSegmentInitExpr_Other(expr->loc);
break;
}
} else if (f->elem_segment.offset.size() > 1) {
} else if (f->elem_segment.offset.exprs.size() > 1) {
result_ |= validator_.OnElemSegmentInitExpr_Other(field.loc);
}

// Element expr.
for (auto&& elem_expr : f->elem_segment.elem_exprs) {
if (elem_expr.size() == 1) {
const Expr* expr = &elem_expr.front();
if (elem_expr.exprs.size() == 1) {
const Expr* expr = &elem_expr.exprs.front();
switch (expr->type()) {
case ExprType::RefNull:
result_ |= validator_.OnElemSegmentElemExpr_RefNull(
Expand All @@ -839,7 +839,7 @@ Result Validator::CheckModule() {
result_ |= validator_.OnElemSegmentElemExpr_Other(expr->loc);
break;
}
} else if (elem_expr.size() > 1) {
} else if (elem_expr.exprs.size() > 1) {
result_ |= validator_.OnElemSegmentElemExpr_Other(field.loc);
}
}
Expand Down Expand Up @@ -873,8 +873,8 @@ Result Validator::CheckModule() {
field.loc, f->data_segment.memory_var, f->data_segment.kind);

// Init expr.
if (f->data_segment.offset.size() == 1) {
const Expr* expr = &f->data_segment.offset.front();
if (f->data_segment.offset.exprs.size() == 1) {
const Expr* expr = &f->data_segment.offset.exprs.front();

switch (expr->type()) {
case ExprType::Const:
Expand All @@ -893,7 +893,7 @@ Result Validator::CheckModule() {
result_ |= validator_.OnDataSegmentInitExpr_Other(expr->loc);
break;
}
} else if (f->data_segment.offset.size() > 1) {
} else if (f->data_segment.offset.exprs.size() > 1) {
result_ |= validator_.OnDataSegmentInitExpr_Other(field.loc);
}
}
Expand Down
Loading

0 comments on commit da53bef

Please sign in to comment.