Skip to content

Commit 99caef2

Browse files
committed
c-writer.cc: cleanup handling of functions & tags (NFC)
- eliminate redundant import_syms_ SymbolSet - eliminate ResultType struct - replace ExternalPtr (only used for tags) with TagPtr - refer to functions with bare name (no need for * or &) - don't use "return" for functions returning void (fixes warning on MSVC) - add missing "static" on ExportName + some missing "const"s - fix spacing around init_instance_import
1 parent f64cc5c commit 99caef2

File tree

4 files changed

+68
-83
lines changed

4 files changed

+68
-83
lines changed

src/c-writer.cc

Lines changed: 57 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ struct GlobalName {
9494
const std::string& name;
9595
};
9696

97-
struct ExternalPtr : GlobalName {
98-
using GlobalName::GlobalName;
97+
struct TagPtr : GlobalName {
98+
explicit TagPtr(const std::string& name)
99+
: GlobalName(ModuleFieldType::Tag, name) {}
99100
};
100101

101102
struct ExternalRef : GlobalName {
@@ -142,11 +143,6 @@ struct SignedType {
142143
Type type;
143144
};
144145

145-
struct ResultType {
146-
explicit ResultType(const TypeVector& types) : types(types) {}
147-
const TypeVector& types;
148-
};
149-
150146
struct TryCatchLabel {
151147
TryCatchLabel(const std::string& name, size_t try_catch_stack_size)
152148
: name(name), try_catch_stack_size(try_catch_stack_size), used(false) {}
@@ -267,17 +263,18 @@ class CWriter {
267263
static std::string Mangle(std::string_view name, bool double_underscores);
268264
static std::string MangleName(std::string_view);
269265
static std::string MangleModuleName(std::string_view);
270-
std::string ExportName(std::string_view module_name,
271-
std::string_view export_name);
272-
std::string ExportName(std::string_view export_name);
266+
static std::string ExportName(std::string_view module_name,
267+
std::string_view export_name);
268+
std::string ExportName(std::string_view export_name) const;
273269
std::string ModuleInstanceTypeName() const;
274270
static std::string ModuleInstanceTypeName(std::string_view module_name);
275271
void ClaimName(SymbolSet& set,
276272
SymbolMap& map,
277273
char type_suffix,
278274
std::string_view wasm_name,
279275
const std::string& c_name);
280-
std::string FindUniqueName(SymbolSet& set, std::string_view proposed_name);
276+
std::string FindUniqueName(SymbolSet& set,
277+
std::string_view proposed_name) const;
281278
std::string ClaimUniqueName(SymbolSet& set,
282279
SymbolMap& map,
283280
char type_suffix,
@@ -334,7 +331,7 @@ class CWriter {
334331
void Write(const ParamName&);
335332
void Write(const LabelName&);
336333
void Write(const GlobalName&);
337-
void Write(const ExternalPtr&);
334+
void Write(const TagPtr&);
338335
void Write(const ExternalRef&);
339336
void Write(const ExternalInstancePtr&);
340337
void Write(const ExternalInstanceRef&);
@@ -345,7 +342,7 @@ class CWriter {
345342
void Write(const LabelDecl&);
346343
void Write(const GlobalInstanceVar&);
347344
void Write(const StackVar&);
348-
void Write(const ResultType&);
345+
void Write(const TypeVector&);
349346
void Write(const Const&);
350347
void WriteInitExpr(const ExprList&);
351348
void WriteInitExprTerminal(const Expr*);
@@ -453,6 +450,8 @@ class CWriter {
453450

454451
void PushFuncSection(std::string_view include_condition = "");
455452

453+
bool IsImport(const std::string& name) const;
454+
456455
const WriteCOptions& options_;
457456
const Module* module_ = nullptr;
458457
const Func* func_ = nullptr;
@@ -473,7 +472,6 @@ class CWriter {
473472
StackVarSymbolMap stack_var_sym_map_;
474473
SymbolSet global_syms_;
475474
SymbolSet local_syms_;
476-
SymbolSet import_syms_;
477475
TypeVector type_stack_;
478476
std::vector<Label> label_stack_;
479477
std::vector<TryCatchLabel> try_catch_stack_;
@@ -647,7 +645,7 @@ std::string CWriter::MangleTagTypes(const TypeVector& types) {
647645
}
648646

649647
/* The C symbol for an export from this module. */
650-
std::string CWriter::ExportName(std::string_view export_name) {
648+
std::string CWriter::ExportName(std::string_view export_name) const {
651649
return kGlobalSymbolPrefix + module_prefix_ + '_' + MangleName(export_name);
652650
}
653651

@@ -816,7 +814,7 @@ void CWriter::ClaimName(SymbolSet& set,
816814
* an integer to the symbol if necessary.
817815
*/
818816
std::string CWriter::FindUniqueName(SymbolSet& set,
819-
std::string_view proposed_name) {
817+
std::string_view proposed_name) const {
820818
std::string unique{proposed_name};
821819
if (set.find(unique) != set.end()) {
822820
std::string base = unique + "_";
@@ -878,7 +876,6 @@ void CWriter::DefineImportName(const Import* import,
878876
break;
879877
}
880878

881-
import_syms_.insert(name);
882879
import_module_sym_map_.emplace(name, import->module_name);
883880

884881
const std::string mangled = ExportName(module, field_name);
@@ -1052,34 +1049,30 @@ void CWriter::Write(const GlobalName& name) {
10521049
Write(GetGlobalName(name.type, name.name));
10531050
}
10541051

1055-
void CWriter::Write(const ExternalPtr& name) {
1056-
bool is_import = import_syms_.count(name.name) != 0;
1057-
if (!is_import) {
1052+
void CWriter::Write(const TagPtr& name) {
1053+
if (!IsImport(name.name)) {
10581054
Write("&");
10591055
}
10601056
Write(GlobalName(name));
10611057
}
10621058

10631059
void CWriter::Write(const ExternalInstancePtr& name) {
1064-
bool is_import = import_syms_.count(name.name) != 0;
1065-
if (!is_import) {
1060+
if (!IsImport(name.name)) {
10661061
Write("&");
10671062
}
10681063
Write("instance->", GlobalName(name));
10691064
}
10701065

10711066
void CWriter::Write(const ExternalRef& name) {
1072-
bool is_import = import_syms_.count(name.name) != 0;
1073-
if (is_import) {
1074-
Write("(*", GlobalName(name), ")");
1075-
} else {
1067+
if (name.type == ModuleFieldType::Func || !IsImport(name.name)) {
10761068
Write(GlobalName(name));
1069+
} else {
1070+
Write("(*", GlobalName(name), ")");
10771071
}
10781072
}
10791073

10801074
void CWriter::Write(const ExternalInstanceRef& name) {
1081-
bool is_import = import_syms_.count(name.name) != 0;
1082-
if (is_import) {
1075+
if (IsImport(name.name)) {
10831076
Write("(*instance->", GlobalName(name), ")");
10841077
} else {
10851078
Write("instance->", GlobalName(name));
@@ -1197,13 +1190,13 @@ void CWriter::Write(SignedType type) {
11971190
// clang-format on
11981191
}
11991192

1200-
void CWriter::Write(const ResultType& rt) {
1201-
if (rt.types.empty()) {
1193+
void CWriter::Write(const TypeVector& types) {
1194+
if (types.empty()) {
12021195
Write("void");
1203-
} else if (rt.types.size() == 1) {
1204-
Write(rt.types[0]);
1196+
} else if (types.size() == 1) {
1197+
Write(types[0]);
12051198
} else {
1206-
Write("struct ", MangleMultivalueTypes(rt.types));
1199+
Write("struct ", MangleMultivalueTypes(types));
12071200
}
12081201
}
12091202

@@ -1384,17 +1377,15 @@ void CWriter::WriteInitExprTerminal(const Expr* expr) {
13841377

13851378
Write("(wasm_rt_funcref_t){", FuncTypeExpr(func_type), ", ",
13861379
"(wasm_rt_function_ptr_t)",
1387-
ExternalPtr(ModuleFieldType::Func, func->name), ", ");
1380+
ExternalRef(ModuleFieldType::Func, func->name), ", ");
13881381

1389-
bool is_import = import_module_sym_map_.count(func->name) != 0;
1390-
if (is_import) {
1382+
if (IsImport(func->name)) {
13911383
Write("instance->", GlobalName(ModuleFieldType::Import,
13921384
import_module_sym_map_[func->name]));
13931385
} else {
13941386
Write("instance");
13951387
}
1396-
1397-
Write("};", Newline());
1388+
Write("}");
13981389
} break;
13991390

14001391
case ExprType::RefNull:
@@ -1790,7 +1781,7 @@ void CWriter::WriteFuncDeclarations() {
17901781

17911782
void CWriter::WriteFuncDeclaration(const FuncDeclaration& decl,
17921783
const std::string& name) {
1793-
Write(ResultType(decl.sig.result_types), " ", name, "(");
1784+
Write(decl.sig.result_types, " ", name, "(");
17941785
Write(ModuleInstanceTypeName(), "*");
17951786
WriteParamTypes(decl);
17961787
Write(")");
@@ -1799,15 +1790,15 @@ void CWriter::WriteFuncDeclaration(const FuncDeclaration& decl,
17991790
void CWriter::WriteImportFuncDeclaration(const FuncDeclaration& decl,
18001791
const std::string& module_name,
18011792
const std::string& name) {
1802-
Write(ResultType(decl.sig.result_types), " ", name, "(");
1793+
Write(decl.sig.result_types, " ", name, "(");
18031794
Write("struct ", ModuleInstanceTypeName(module_name), "*");
18041795
WriteParamTypes(decl);
18051796
Write(")");
18061797
}
18071798

18081799
void CWriter::WriteCallIndirectFuncDeclaration(const FuncDeclaration& decl,
18091800
const std::string& name) {
1810-
Write(ResultType(decl.sig.result_types), " ", name, "(void*");
1801+
Write(decl.sig.result_types, " ", name, "(void*");
18111802
WriteParamTypes(decl);
18121803
Write(")");
18131804
}
@@ -2158,9 +2149,8 @@ void CWriter::WriteElemInitializers() {
21582149
const Func* func = module_->GetFunc(cast<RefFuncExpr>(&expr)->var);
21592150
const FuncType* func_type = module_->GetFuncType(func->decl.type_var);
21602151
Write("{", FuncTypeExpr(func_type), ", (wasm_rt_function_ptr_t)",
2161-
ExternalPtr(ModuleFieldType::Func, func->name), ", ");
2162-
const bool is_import = import_module_sym_map_.count(func->name) != 0;
2163-
if (is_import) {
2152+
ExternalRef(ModuleFieldType::Func, func->name), ", ");
2153+
if (IsImport(func->name)) {
21642154
Write("offsetof(", ModuleInstanceTypeName(), ", ",
21652155
GlobalName(ModuleFieldType::Import,
21662156
import_module_sym_map_[func->name]),
@@ -2290,8 +2280,7 @@ void CWriter::WriteExports(CWriterPhase kind) {
22902280
local_syms_ = global_syms_;
22912281
local_sym_map_.clear();
22922282
stack_var_sym_map_.clear();
2293-
Write(ResultType(func_->decl.sig.result_types), " ", mangled_name,
2294-
"(");
2283+
Write(func_->decl.sig.result_types, " ", mangled_name, "(");
22952284
MakeTypeBindingReverseMapping(func_->GetNumParamsAndLocals(),
22962285
func_->bindings, &index_to_name);
22972286
WriteParams(index_to_name);
@@ -2343,11 +2332,12 @@ void CWriter::WriteExports(CWriterPhase kind) {
23432332
switch (export_->kind) {
23442333
case ExternalKind::Func: {
23452334
Write(OpenBrace());
2346-
Write("return ", ExternalRef(ModuleFieldType::Func, internal_name),
2347-
"(");
2335+
if (func_->GetNumResults() > 0) {
2336+
Write("return ");
2337+
}
2338+
Write(ExternalRef(ModuleFieldType::Func, internal_name), "(");
23482339

2349-
bool is_import = import_module_sym_map_.count(internal_name) != 0;
2350-
if (is_import) {
2340+
if (IsImport(internal_name)) {
23512341
Write("instance->",
23522342
GlobalName(ModuleFieldType::Import,
23532343
import_module_sym_map_[internal_name]));
@@ -2388,8 +2378,7 @@ void CWriter::WriteExports(CWriterPhase kind) {
23882378
break;
23892379

23902380
case ExternalKind::Tag:
2391-
Write("= ", ExternalPtr(ModuleFieldType::Tag, internal_name), ";",
2392-
Newline());
2381+
Write("= ", TagPtr(internal_name), ";", Newline());
23932382
break;
23942383

23952384
default:
@@ -2435,9 +2424,7 @@ void CWriter::WriteInit() {
24352424

24362425
for (Var* var : module_->starts) {
24372426
Write(ExternalRef(ModuleFieldType::Func, module_->GetFunc(*var)->name));
2438-
bool is_import =
2439-
import_module_sym_map_.count(module_->GetFunc(*var)->name) != 0;
2440-
if (is_import) {
2427+
if (IsImport(module_->GetFunc(*var)->name)) {
24412428
Write("(instance->",
24422429
GlobalName(ModuleFieldType::Import,
24432430
import_module_sym_map_[module_->GetFunc(*var)->name]),
@@ -2494,7 +2481,7 @@ void CWriter::WriteInitInstanceImport() {
24942481
Write(", struct ", ModuleInstanceTypeName(import_module_name), "* ",
24952482
GlobalName(ModuleFieldType::Import, import_module_name));
24962483
}
2497-
Write(")", OpenBrace());
2484+
Write(") ", OpenBrace());
24982485

24992486
for (const auto& import_module : import_func_module_set_) {
25002487
Write("instance->", GlobalName(ModuleFieldType::Import, import_module),
@@ -2623,6 +2610,10 @@ void CWriter::PushFuncSection(std::string_view include_condition) {
26232610
stream_ = &func_sections_.back().second;
26242611
}
26252612

2613+
bool CWriter::IsImport(const std::string& name) const {
2614+
return import_module_sym_map_.count(name);
2615+
}
2616+
26262617
void CWriter::Write(const Func& func) {
26272618
func_ = &func;
26282619
local_syms_.clear();
@@ -2643,7 +2634,7 @@ void CWriter::Write(const Func& func) {
26432634
Write(Newline());
26442635

26452636
PushFuncSection();
2646-
Write(ResultType(func.decl.sig.result_types), " ",
2637+
Write(func.decl.sig.result_types, " ",
26472638
GlobalName(ModuleFieldType::Func, func.name), "(");
26482639
WriteParamsAndLocals();
26492640
Write("FUNC_PROLOGUE;", Newline());
@@ -2666,7 +2657,7 @@ void CWriter::Write(const Func& func) {
26662657
Write("return ", StackVar(0), ";", Newline());
26672658
} else if (num_results >= 2) {
26682659
Write(OpenBrace());
2669-
Write(ResultType(func.decl.sig.result_types), " tmp;", Newline());
2660+
Write(func.decl.sig.result_types, " tmp;", Newline());
26702661
for (Index i = 0; i < num_results; ++i) {
26712662
Type type = func.GetResultType(i);
26722663
Writef("tmp.%c%d = ", MangleType(type), i);
@@ -2909,9 +2900,8 @@ void CWriter::Write(const Catch& c) {
29092900
return;
29102901
}
29112902

2912-
Write("if (wasm_rt_exception_tag() == ",
2913-
ExternalPtr(ModuleFieldType::Tag, module_->GetTag(c.var)->name), ") ",
2914-
OpenBrace());
2903+
Write("if (wasm_rt_exception_tag() == ", TagPtr(module_->GetTag(c.var)->name),
2904+
") ", OpenBrace());
29152905

29162906
const Tag* tag = module_->GetTag(c.var);
29172907
const FuncDeclaration& tag_type = tag->decl;
@@ -3060,8 +3050,7 @@ void CWriter::Write(const ExprList& exprs) {
30603050

30613051
assert(var.is_name());
30623052
Write(ExternalRef(ModuleFieldType::Func, var.name()), "(");
3063-
bool is_import = import_module_sym_map_.count(func.name) != 0;
3064-
if (is_import) {
3053+
if (IsImport(func.name)) {
30653054
Write("instance->", GlobalName(ModuleFieldType::Import,
30663055
import_module_sym_map_[func.name]));
30673056
} else {
@@ -3389,10 +3378,9 @@ void CWriter::Write(const ExprList& exprs) {
33893378

33903379
Write(StackVar(0), " = (wasm_rt_funcref_t){", FuncTypeExpr(func_type),
33913380
", (wasm_rt_function_ptr_t)",
3392-
ExternalPtr(ModuleFieldType::Func, func->name), ", ");
3381+
ExternalRef(ModuleFieldType::Func, func->name), ", ");
33933382

3394-
bool is_import = import_module_sym_map_.count(func->name) != 0;
3395-
if (is_import) {
3383+
if (IsImport(func->name)) {
33963384
Write("instance->", GlobalName(ModuleFieldType::Import,
33973385
import_module_sym_map_[func->name]));
33983386
} else {
@@ -3518,12 +3506,10 @@ void CWriter::Write(const ExprList& exprs) {
35183506

35193507
Index num_params = tag->decl.GetNumParams();
35203508
if (num_params == 0) {
3521-
Write("wasm_rt_load_exception(",
3522-
ExternalPtr(ModuleFieldType::Tag, tag->name), ", 0, NULL);",
3509+
Write("wasm_rt_load_exception(", TagPtr(tag->name), ", 0, NULL);",
35233510
Newline());
35243511
} else if (num_params == 1) {
3525-
Write("wasm_rt_load_exception(",
3526-
ExternalPtr(ModuleFieldType::Tag, tag->name), ", sizeof(",
3512+
Write("wasm_rt_load_exception(", TagPtr(tag->name), ", sizeof(",
35273513
tag->decl.GetParamType(0), "), &", StackVar(0), ");",
35283514
Newline());
35293515
} else {
@@ -3534,8 +3520,7 @@ void CWriter::Write(const ExprList& exprs) {
35343520
Write(StackVar(i), ", ");
35353521
}
35363522
Write("};", Newline());
3537-
Write("wasm_rt_load_exception(",
3538-
ExternalPtr(ModuleFieldType::Tag, tag->name),
3523+
Write("wasm_rt_load_exception(", TagPtr(tag->name),
35393524
", sizeof(tmp), &tmp);", Newline());
35403525
Write(CloseBrace(), Newline());
35413526
}

test/wasm2c/check-imports.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ static void init_memories(w2c_test* instance) {
694694
}
695695

696696
static const wasm_elem_segment_expr_t elem_segment_exprs_w2c_test_e0[] = {
697-
{w2c_test_t1, (wasm_rt_function_ptr_t)&w2c_test_f1, 0},
697+
{w2c_test_t1, (wasm_rt_function_ptr_t)w2c_test_f1, 0},
698698
};
699699

700700
static void init_tables(w2c_test* instance) {
@@ -704,7 +704,7 @@ static void init_tables(w2c_test* instance) {
704704
static void init_elem_instances(w2c_test *instance) {
705705
}
706706

707-
static void init_instance_import(w2c_test* instance, struct w2c_env* w2c_env_instance){
707+
static void init_instance_import(w2c_test* instance, struct w2c_env* w2c_env_instance) {
708708
instance->w2c_env_0x5F_indirect_function_table = w2c_env_0x5F_indirect_function_table(w2c_env_instance);
709709
instance->w2c_env_0x5F_linear_memory = w2c_env_0x5F_linear_memory(w2c_env_instance);
710710
}

0 commit comments

Comments
 (0)