@@ -94,8 +94,9 @@ struct GlobalName {
94
94
const std::string& name;
95
95
};
96
96
97
- struct ExternalPtr : GlobalName {
98
- using GlobalName::GlobalName;
97
+ struct TagSymbol : GlobalName {
98
+ explicit TagSymbol (const std::string& name)
99
+ : GlobalName(ModuleFieldType::Tag, name) {}
99
100
};
100
101
101
102
struct ExternalRef : GlobalName {
@@ -142,11 +143,6 @@ struct SignedType {
142
143
Type type;
143
144
};
144
145
145
- struct ResultType {
146
- explicit ResultType (const TypeVector& types) : types(types) {}
147
- const TypeVector& types;
148
- };
149
-
150
146
struct TryCatchLabel {
151
147
TryCatchLabel (const std::string& name, size_t try_catch_stack_size)
152
148
: name(name), try_catch_stack_size(try_catch_stack_size), used(false ) {}
@@ -267,17 +263,18 @@ class CWriter {
267
263
static std::string Mangle (std::string_view name, bool double_underscores);
268
264
static std::string MangleName (std::string_view);
269
265
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 ;
273
269
std::string ModuleInstanceTypeName () const ;
274
270
static std::string ModuleInstanceTypeName (std::string_view module_name);
275
271
void ClaimName (SymbolSet& set,
276
272
SymbolMap& map,
277
273
char type_suffix,
278
274
std::string_view wasm_name,
279
275
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 ;
281
278
std::string ClaimUniqueName (SymbolSet& set,
282
279
SymbolMap& map,
283
280
char type_suffix,
@@ -334,7 +331,7 @@ class CWriter {
334
331
void Write (const ParamName&);
335
332
void Write (const LabelName&);
336
333
void Write (const GlobalName&);
337
- void Write (const ExternalPtr &);
334
+ void Write (const TagSymbol &);
338
335
void Write (const ExternalRef&);
339
336
void Write (const ExternalInstancePtr&);
340
337
void Write (const ExternalInstanceRef&);
@@ -345,7 +342,7 @@ class CWriter {
345
342
void Write (const LabelDecl&);
346
343
void Write (const GlobalInstanceVar&);
347
344
void Write (const StackVar&);
348
- void Write (const ResultType &);
345
+ void Write (const TypeVector &);
349
346
void Write (const Const&);
350
347
void WriteInitExpr (const ExprList&);
351
348
void WriteInitExprTerminal (const Expr*);
@@ -648,7 +645,7 @@ std::string CWriter::MangleTagTypes(const TypeVector& types) {
648
645
}
649
646
650
647
/* The C symbol for an export from this module. */
651
- std::string CWriter::ExportName (std::string_view export_name) {
648
+ std::string CWriter::ExportName (std::string_view export_name) const {
652
649
return kGlobalSymbolPrefix + module_prefix_ + ' _' + MangleName (export_name);
653
650
}
654
651
@@ -817,7 +814,7 @@ void CWriter::ClaimName(SymbolSet& set,
817
814
* an integer to the symbol if necessary.
818
815
*/
819
816
std::string CWriter::FindUniqueName (SymbolSet& set,
820
- std::string_view proposed_name) {
817
+ std::string_view proposed_name) const {
821
818
std::string unique{proposed_name};
822
819
if (set.find (unique) != set.end ()) {
823
820
std::string base = unique + " _" ;
@@ -1052,7 +1049,7 @@ void CWriter::Write(const GlobalName& name) {
1052
1049
Write (GetGlobalName (name.type , name.name ));
1053
1050
}
1054
1051
1055
- void CWriter::Write (const ExternalPtr & name) {
1052
+ void CWriter::Write (const TagSymbol & name) {
1056
1053
if (!IsImport (name.name )) {
1057
1054
Write (" &" );
1058
1055
}
@@ -1067,10 +1064,10 @@ void CWriter::Write(const ExternalInstancePtr& name) {
1067
1064
}
1068
1065
1069
1066
void CWriter::Write (const ExternalRef& name) {
1070
- if (IsImport (name.name )) {
1071
- Write (" (*" , GlobalName (name), " )" );
1072
- } else {
1067
+ if (name.type == ModuleFieldType::Func || !IsImport (name.name )) {
1073
1068
Write (GlobalName (name));
1069
+ } else {
1070
+ Write (" (*" , GlobalName (name), " )" );
1074
1071
}
1075
1072
}
1076
1073
@@ -1193,13 +1190,13 @@ void CWriter::Write(SignedType type) {
1193
1190
// clang-format on
1194
1191
}
1195
1192
1196
- void CWriter::Write (const ResultType& rt ) {
1197
- if (rt. types .empty ()) {
1193
+ void CWriter::Write (const TypeVector& types ) {
1194
+ if (types.empty ()) {
1198
1195
Write (" void" );
1199
- } else if (rt. types .size () == 1 ) {
1200
- Write (rt. types [0 ]);
1196
+ } else if (types.size () == 1 ) {
1197
+ Write (types[0 ]);
1201
1198
} else {
1202
- Write (" struct " , MangleMultivalueTypes (rt. types ));
1199
+ Write (" struct " , MangleMultivalueTypes (types));
1203
1200
}
1204
1201
}
1205
1202
@@ -1380,16 +1377,15 @@ void CWriter::WriteInitExprTerminal(const Expr* expr) {
1380
1377
1381
1378
Write (" (wasm_rt_funcref_t){" , FuncTypeExpr (func_type), " , " ,
1382
1379
" (wasm_rt_function_ptr_t)" ,
1383
- ExternalPtr (ModuleFieldType::Func, func->name ), " , " );
1380
+ ExternalRef (ModuleFieldType::Func, func->name ), " , " );
1384
1381
1385
1382
if (IsImport (func->name )) {
1386
1383
Write (" instance->" , GlobalName (ModuleFieldType::Import,
1387
1384
import_module_sym_map_[func->name ]));
1388
1385
} else {
1389
1386
Write (" instance" );
1390
1387
}
1391
-
1392
- Write (" };" , Newline ());
1388
+ Write (" }" );
1393
1389
} break ;
1394
1390
1395
1391
case ExprType::RefNull:
@@ -1785,7 +1781,7 @@ void CWriter::WriteFuncDeclarations() {
1785
1781
1786
1782
void CWriter::WriteFuncDeclaration (const FuncDeclaration& decl,
1787
1783
const std::string& name) {
1788
- Write (ResultType ( decl.sig .result_types ) , " " , name, " (" );
1784
+ Write (decl.sig .result_types , " " , name, " (" );
1789
1785
Write (ModuleInstanceTypeName (), " *" );
1790
1786
WriteParamTypes (decl);
1791
1787
Write (" )" );
@@ -1794,15 +1790,15 @@ void CWriter::WriteFuncDeclaration(const FuncDeclaration& decl,
1794
1790
void CWriter::WriteImportFuncDeclaration (const FuncDeclaration& decl,
1795
1791
const std::string& module_name,
1796
1792
const std::string& name) {
1797
- Write (ResultType ( decl.sig .result_types ) , " " , name, " (" );
1793
+ Write (decl.sig .result_types , " " , name, " (" );
1798
1794
Write (" struct " , ModuleInstanceTypeName (module_name), " *" );
1799
1795
WriteParamTypes (decl);
1800
1796
Write (" )" );
1801
1797
}
1802
1798
1803
1799
void CWriter::WriteCallIndirectFuncDeclaration (const FuncDeclaration& decl,
1804
1800
const std::string& name) {
1805
- Write (ResultType ( decl.sig .result_types ) , " " , name, " (void*" );
1801
+ Write (decl.sig .result_types , " " , name, " (void*" );
1806
1802
WriteParamTypes (decl);
1807
1803
Write (" )" );
1808
1804
}
@@ -2153,7 +2149,7 @@ void CWriter::WriteElemInitializers() {
2153
2149
const Func* func = module_->GetFunc (cast<RefFuncExpr>(&expr)->var );
2154
2150
const FuncType* func_type = module_->GetFuncType (func->decl .type_var );
2155
2151
Write (" {" , FuncTypeExpr (func_type), " , (wasm_rt_function_ptr_t)" ,
2156
- ExternalPtr (ModuleFieldType::Func, func->name ), " , " );
2152
+ ExternalRef (ModuleFieldType::Func, func->name ), " , " );
2157
2153
if (IsImport (func->name )) {
2158
2154
Write (" offsetof(" , ModuleInstanceTypeName (), " , " ,
2159
2155
GlobalName (ModuleFieldType::Import,
@@ -2284,8 +2280,7 @@ void CWriter::WriteExports(CWriterPhase kind) {
2284
2280
local_syms_ = global_syms_;
2285
2281
local_sym_map_.clear ();
2286
2282
stack_var_sym_map_.clear ();
2287
- Write (ResultType (func_->decl .sig .result_types ), " " , mangled_name,
2288
- " (" );
2283
+ Write (func_->decl .sig .result_types , " " , mangled_name, " (" );
2289
2284
MakeTypeBindingReverseMapping (func_->GetNumParamsAndLocals (),
2290
2285
func_->bindings , &index_to_name);
2291
2286
WriteParams (index_to_name);
@@ -2337,8 +2332,10 @@ void CWriter::WriteExports(CWriterPhase kind) {
2337
2332
switch (export_->kind ) {
2338
2333
case ExternalKind::Func: {
2339
2334
Write (OpenBrace ());
2340
- Write (" return " , ExternalRef (ModuleFieldType::Func, internal_name),
2341
- " (" );
2335
+ if (func_->GetNumResults () > 0 ) {
2336
+ Write (" return " );
2337
+ }
2338
+ Write (ExternalRef (ModuleFieldType::Func, internal_name), " (" );
2342
2339
2343
2340
if (IsImport (internal_name)) {
2344
2341
Write (" instance->" ,
@@ -2381,8 +2378,7 @@ void CWriter::WriteExports(CWriterPhase kind) {
2381
2378
break ;
2382
2379
2383
2380
case ExternalKind::Tag:
2384
- Write (" = " , ExternalPtr (ModuleFieldType::Tag, internal_name), " ;" ,
2385
- Newline ());
2381
+ Write (" = " , TagSymbol (internal_name), " ;" , Newline ());
2386
2382
break ;
2387
2383
2388
2384
default :
@@ -2485,7 +2481,7 @@ void CWriter::WriteInitInstanceImport() {
2485
2481
Write (" , struct " , ModuleInstanceTypeName (import_module_name), " * " ,
2486
2482
GlobalName (ModuleFieldType::Import, import_module_name));
2487
2483
}
2488
- Write (" )" , OpenBrace ());
2484
+ Write (" ) " , OpenBrace ());
2489
2485
2490
2486
for (const auto & import_module : import_func_module_set_) {
2491
2487
Write (" instance->" , GlobalName (ModuleFieldType::Import, import_module),
@@ -2638,7 +2634,7 @@ void CWriter::Write(const Func& func) {
2638
2634
Write (Newline ());
2639
2635
2640
2636
PushFuncSection ();
2641
- Write (ResultType ( func.decl .sig .result_types ) , " " ,
2637
+ Write (func.decl .sig .result_types , " " ,
2642
2638
GlobalName (ModuleFieldType::Func, func.name ), " (" );
2643
2639
WriteParamsAndLocals ();
2644
2640
Write (" FUNC_PROLOGUE;" , Newline ());
@@ -2661,7 +2657,7 @@ void CWriter::Write(const Func& func) {
2661
2657
Write (" return " , StackVar (0 ), " ;" , Newline ());
2662
2658
} else if (num_results >= 2 ) {
2663
2659
Write (OpenBrace ());
2664
- Write (ResultType ( func.decl .sig .result_types ) , " tmp;" , Newline ());
2660
+ Write (func.decl .sig .result_types , " tmp;" , Newline ());
2665
2661
for (Index i = 0 ; i < num_results; ++i) {
2666
2662
Type type = func.GetResultType (i);
2667
2663
Writef (" tmp.%c%d = " , MangleType (type), i);
@@ -2905,8 +2901,7 @@ void CWriter::Write(const Catch& c) {
2905
2901
}
2906
2902
2907
2903
Write (" if (wasm_rt_exception_tag() == " ,
2908
- ExternalPtr (ModuleFieldType::Tag, module_->GetTag (c.var )->name ), " ) " ,
2909
- OpenBrace ());
2904
+ TagSymbol (module_->GetTag (c.var )->name ), " ) " , OpenBrace ());
2910
2905
2911
2906
const Tag* tag = module_->GetTag (c.var );
2912
2907
const FuncDeclaration& tag_type = tag->decl ;
@@ -3383,7 +3378,7 @@ void CWriter::Write(const ExprList& exprs) {
3383
3378
3384
3379
Write (StackVar (0 ), " = (wasm_rt_funcref_t){" , FuncTypeExpr (func_type),
3385
3380
" , (wasm_rt_function_ptr_t)" ,
3386
- ExternalPtr (ModuleFieldType::Func, func->name ), " , " );
3381
+ ExternalRef (ModuleFieldType::Func, func->name ), " , " );
3387
3382
3388
3383
if (IsImport (func->name )) {
3389
3384
Write (" instance->" , GlobalName (ModuleFieldType::Import,
@@ -3511,12 +3506,10 @@ void CWriter::Write(const ExprList& exprs) {
3511
3506
3512
3507
Index num_params = tag->decl .GetNumParams ();
3513
3508
if (num_params == 0 ) {
3514
- Write (" wasm_rt_load_exception(" ,
3515
- ExternalPtr (ModuleFieldType::Tag, tag->name ), " , 0, NULL);" ,
3509
+ Write (" wasm_rt_load_exception(" , TagSymbol (tag->name ), " , 0, NULL);" ,
3516
3510
Newline ());
3517
3511
} else if (num_params == 1 ) {
3518
- Write (" wasm_rt_load_exception(" ,
3519
- ExternalPtr (ModuleFieldType::Tag, tag->name ), " , sizeof(" ,
3512
+ Write (" wasm_rt_load_exception(" , TagSymbol (tag->name ), " , sizeof(" ,
3520
3513
tag->decl .GetParamType (0 ), " ), &" , StackVar (0 ), " );" ,
3521
3514
Newline ());
3522
3515
} else {
@@ -3527,8 +3520,7 @@ void CWriter::Write(const ExprList& exprs) {
3527
3520
Write (StackVar (i), " , " );
3528
3521
}
3529
3522
Write (" };" , Newline ());
3530
- Write (" wasm_rt_load_exception(" ,
3531
- ExternalPtr (ModuleFieldType::Tag, tag->name ),
3523
+ Write (" wasm_rt_load_exception(" , TagSymbol (tag->name ),
3532
3524
" , sizeof(tmp), &tmp);" , Newline ());
3533
3525
Write (CloseBrace (), Newline ());
3534
3526
}
0 commit comments