Skip to content

Commit 4895680

Browse files
committed
Clean up
1 parent 9ab1faf commit 4895680

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

src/c-writer.cc

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ class CWriter {
266266
bool IsTopLabelUsed() const;
267267
void PopLabel();
268268

269+
static constexpr bool HasNonNullInitializers(Type);
269270
static constexpr char MangleType(Type);
270271
static constexpr char MangleField(ModuleFieldType);
271272
static std::string MangleTypes(const TypeVector&);
@@ -637,6 +638,19 @@ void CWriter::PopLabel() {
637638
label_stack_.pop_back();
638639
}
639640

641+
// static
642+
constexpr bool CWriter::HasNonNullInitializers(Type type) {
643+
// clang-format off
644+
switch (type) {
645+
case Type::FuncRef: return true;
646+
case Type::ExternRef: return false;
647+
case Type::ExnRef: return false;
648+
default:
649+
WABT_UNREACHABLE;
650+
}
651+
// clang-format on
652+
}
653+
640654
// static
641655
constexpr char CWriter::MangleType(Type type) {
642656
// clang-format off
@@ -2289,10 +2303,8 @@ void CWriter::WriteElemInitializerDecls() {
22892303
continue;
22902304
}
22912305

2292-
if (elem_segment->elem_type == Type::ExternRef ||
2293-
elem_segment->elem_type == Type::ExnRef) {
2294-
// no need to store externref elem initializers because only
2295-
// ref.null is possible
2306+
if (!HasNonNullInitializers(elem_segment->elem_type)) {
2307+
// no need to store these initializers because only ref.null is possible
22962308
continue;
22972309
}
22982310

@@ -2362,10 +2374,8 @@ void CWriter::WriteElemInitializers() {
23622374
continue;
23632375
}
23642376

2365-
if (elem_segment->elem_type == Type::ExternRef ||
2366-
elem_segment->elem_type == Type::ExnRef) {
2367-
// no need to store externref elem initializers because only
2368-
// ref.null is possible
2377+
if (!HasNonNullInitializers(elem_segment->elem_type)) {
2378+
// no need to store these initializers because only ref.null is possible
23692379
continue;
23702380
}
23712381

@@ -2466,16 +2476,15 @@ void CWriter::WriteElemInitializers() {
24662476
void CWriter::WriteElemTableInit(bool active_initialization,
24672477
const ElemSegment* src_segment,
24682478
const Table* dst_table) {
2469-
assert(dst_table->elem_type == Type::FuncRef ||
2470-
dst_table->elem_type == Type::ExternRef ||
2471-
dst_table->elem_type == Type::ExnRef);
2479+
assert(dst_table->elem_type.IsRef() &&
2480+
dst_table->elem_type != Type::Reference);
24722481
assert(dst_table->elem_type == src_segment->elem_type);
24732482

24742483
Write(GetReferenceTypeName(dst_table->elem_type), "_table_init(",
24752484
ExternalInstancePtr(ModuleFieldType::Table, dst_table->name), ", ");
24762485

24772486
// elem segment exprs needed only for funcref tables
2478-
// because externref tables can only be initialized with ref.null
2487+
// because externref and exnref tables can only be initialized with ref.null
24792488
if (dst_table->elem_type == Type::FuncRef) {
24802489
if (src_segment->elem_exprs.empty()) {
24812490
Write("NULL, ");
@@ -3260,8 +3269,7 @@ void CWriter::WriteLocals(const std::vector<std::string>& index_to_name) {
32603269
func_->local_types, [](auto x) { return x; },
32613270
[&](Index local_index, Type local_type) {
32623271
Write(DefineParamName(index_to_name[num_params + local_index]), " = ");
3263-
if (local_type == Type::FuncRef || local_type == Type::ExternRef ||
3264-
local_type == Type::ExnRef) {
3272+
if (local_type.IsRef()) {
32653273
Write(GetReferenceNullValue(local_type));
32663274
} else if (local_type == Type::V128) {
32673275
Write("simde_wasm_i64x2_make(0, 0)");

0 commit comments

Comments
 (0)