@@ -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 TagPtr : GlobalName {
98
+ explicit TagPtr (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 TagPtr &);
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*);
@@ -453,6 +450,8 @@ class CWriter {
453
450
454
451
void PushFuncSection (std::string_view include_condition = " " );
455
452
453
+ bool IsImport (const std::string& name) const ;
454
+
456
455
const WriteCOptions& options_;
457
456
const Module* module_ = nullptr ;
458
457
const Func* func_ = nullptr ;
@@ -473,7 +472,6 @@ class CWriter {
473
472
StackVarSymbolMap stack_var_sym_map_;
474
473
SymbolSet global_syms_;
475
474
SymbolSet local_syms_;
476
- SymbolSet import_syms_;
477
475
TypeVector type_stack_;
478
476
std::vector<Label> label_stack_;
479
477
std::vector<TryCatchLabel> try_catch_stack_;
@@ -647,7 +645,7 @@ std::string CWriter::MangleTagTypes(const TypeVector& types) {
647
645
}
648
646
649
647
/* 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 {
651
649
return kGlobalSymbolPrefix + module_prefix_ + ' _' + MangleName (export_name);
652
650
}
653
651
@@ -816,7 +814,7 @@ void CWriter::ClaimName(SymbolSet& set,
816
814
* an integer to the symbol if necessary.
817
815
*/
818
816
std::string CWriter::FindUniqueName (SymbolSet& set,
819
- std::string_view proposed_name) {
817
+ std::string_view proposed_name) const {
820
818
std::string unique{proposed_name};
821
819
if (set.find (unique) != set.end ()) {
822
820
std::string base = unique + " _" ;
@@ -878,7 +876,6 @@ void CWriter::DefineImportName(const Import* import,
878
876
break ;
879
877
}
880
878
881
- import_syms_.insert (name);
882
879
import_module_sym_map_.emplace (name, import->module_name );
883
880
884
881
const std::string mangled = ExportName (module, field_name);
@@ -1052,34 +1049,30 @@ 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) {
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 )) {
1058
1054
Write (" &" );
1059
1055
}
1060
1056
Write (GlobalName (name));
1061
1057
}
1062
1058
1063
1059
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 )) {
1066
1061
Write (" &" );
1067
1062
}
1068
1063
Write (" instance->" , GlobalName (name));
1069
1064
}
1070
1065
1071
1066
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 )) {
1076
1068
Write (GlobalName (name));
1069
+ } else {
1070
+ Write (" (*" , GlobalName (name), " )" );
1077
1071
}
1078
1072
}
1079
1073
1080
1074
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 )) {
1083
1076
Write (" (*instance->" , GlobalName (name), " )" );
1084
1077
} else {
1085
1078
Write (" instance->" , GlobalName (name));
@@ -1197,13 +1190,13 @@ void CWriter::Write(SignedType type) {
1197
1190
// clang-format on
1198
1191
}
1199
1192
1200
- void CWriter::Write (const ResultType& rt ) {
1201
- if (rt. types .empty ()) {
1193
+ void CWriter::Write (const TypeVector& types ) {
1194
+ if (types.empty ()) {
1202
1195
Write (" void" );
1203
- } else if (rt. types .size () == 1 ) {
1204
- Write (rt. types [0 ]);
1196
+ } else if (types.size () == 1 ) {
1197
+ Write (types[0 ]);
1205
1198
} else {
1206
- Write (" struct " , MangleMultivalueTypes (rt. types ));
1199
+ Write (" struct " , MangleMultivalueTypes (types));
1207
1200
}
1208
1201
}
1209
1202
@@ -1384,17 +1377,15 @@ void CWriter::WriteInitExprTerminal(const Expr* expr) {
1384
1377
1385
1378
Write (" (wasm_rt_funcref_t){" , FuncTypeExpr (func_type), " , " ,
1386
1379
" (wasm_rt_function_ptr_t)" ,
1387
- ExternalPtr (ModuleFieldType::Func, func->name ), " , " );
1380
+ ExternalRef (ModuleFieldType::Func, func->name ), " , " );
1388
1381
1389
- bool is_import = import_module_sym_map_.count (func->name ) != 0 ;
1390
- if (is_import) {
1382
+ if (IsImport (func->name )) {
1391
1383
Write (" instance->" , GlobalName (ModuleFieldType::Import,
1392
1384
import_module_sym_map_[func->name ]));
1393
1385
} else {
1394
1386
Write (" instance" );
1395
1387
}
1396
-
1397
- Write (" };" , Newline ());
1388
+ Write (" }" );
1398
1389
} break ;
1399
1390
1400
1391
case ExprType::RefNull:
@@ -1790,7 +1781,7 @@ void CWriter::WriteFuncDeclarations() {
1790
1781
1791
1782
void CWriter::WriteFuncDeclaration (const FuncDeclaration& decl,
1792
1783
const std::string& name) {
1793
- Write (ResultType ( decl.sig .result_types ) , " " , name, " (" );
1784
+ Write (decl.sig .result_types , " " , name, " (" );
1794
1785
Write (ModuleInstanceTypeName (), " *" );
1795
1786
WriteParamTypes (decl);
1796
1787
Write (" )" );
@@ -1799,15 +1790,15 @@ void CWriter::WriteFuncDeclaration(const FuncDeclaration& decl,
1799
1790
void CWriter::WriteImportFuncDeclaration (const FuncDeclaration& decl,
1800
1791
const std::string& module_name,
1801
1792
const std::string& name) {
1802
- Write (ResultType ( decl.sig .result_types ) , " " , name, " (" );
1793
+ Write (decl.sig .result_types , " " , name, " (" );
1803
1794
Write (" struct " , ModuleInstanceTypeName (module_name), " *" );
1804
1795
WriteParamTypes (decl);
1805
1796
Write (" )" );
1806
1797
}
1807
1798
1808
1799
void CWriter::WriteCallIndirectFuncDeclaration (const FuncDeclaration& decl,
1809
1800
const std::string& name) {
1810
- Write (ResultType ( decl.sig .result_types ) , " " , name, " (void*" );
1801
+ Write (decl.sig .result_types , " " , name, " (void*" );
1811
1802
WriteParamTypes (decl);
1812
1803
Write (" )" );
1813
1804
}
@@ -2158,9 +2149,8 @@ void CWriter::WriteElemInitializers() {
2158
2149
const Func* func = module_->GetFunc (cast<RefFuncExpr>(&expr)->var );
2159
2150
const FuncType* func_type = module_->GetFuncType (func->decl .type_var );
2160
2151
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 )) {
2164
2154
Write (" offsetof(" , ModuleInstanceTypeName (), " , " ,
2165
2155
GlobalName (ModuleFieldType::Import,
2166
2156
import_module_sym_map_[func->name ]),
@@ -2290,8 +2280,7 @@ void CWriter::WriteExports(CWriterPhase kind) {
2290
2280
local_syms_ = global_syms_;
2291
2281
local_sym_map_.clear ();
2292
2282
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, " (" );
2295
2284
MakeTypeBindingReverseMapping (func_->GetNumParamsAndLocals (),
2296
2285
func_->bindings , &index_to_name);
2297
2286
WriteParams (index_to_name);
@@ -2343,11 +2332,12 @@ void CWriter::WriteExports(CWriterPhase kind) {
2343
2332
switch (export_->kind ) {
2344
2333
case ExternalKind::Func: {
2345
2334
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), " (" );
2348
2339
2349
- bool is_import = import_module_sym_map_.count (internal_name) != 0 ;
2350
- if (is_import) {
2340
+ if (IsImport (internal_name)) {
2351
2341
Write (" instance->" ,
2352
2342
GlobalName (ModuleFieldType::Import,
2353
2343
import_module_sym_map_[internal_name]));
@@ -2388,8 +2378,7 @@ void CWriter::WriteExports(CWriterPhase kind) {
2388
2378
break ;
2389
2379
2390
2380
case ExternalKind::Tag:
2391
- Write (" = " , ExternalPtr (ModuleFieldType::Tag, internal_name), " ;" ,
2392
- Newline ());
2381
+ Write (" = " , TagPtr (internal_name), " ;" , Newline ());
2393
2382
break ;
2394
2383
2395
2384
default :
@@ -2435,9 +2424,7 @@ void CWriter::WriteInit() {
2435
2424
2436
2425
for (Var* var : module_->starts ) {
2437
2426
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 )) {
2441
2428
Write (" (instance->" ,
2442
2429
GlobalName (ModuleFieldType::Import,
2443
2430
import_module_sym_map_[module_->GetFunc (*var)->name ]),
@@ -2494,7 +2481,7 @@ void CWriter::WriteInitInstanceImport() {
2494
2481
Write (" , struct " , ModuleInstanceTypeName (import_module_name), " * " ,
2495
2482
GlobalName (ModuleFieldType::Import, import_module_name));
2496
2483
}
2497
- Write (" )" , OpenBrace ());
2484
+ Write (" ) " , OpenBrace ());
2498
2485
2499
2486
for (const auto & import_module : import_func_module_set_) {
2500
2487
Write (" instance->" , GlobalName (ModuleFieldType::Import, import_module),
@@ -2623,6 +2610,10 @@ void CWriter::PushFuncSection(std::string_view include_condition) {
2623
2610
stream_ = &func_sections_.back ().second ;
2624
2611
}
2625
2612
2613
+ bool CWriter::IsImport (const std::string& name) const {
2614
+ return import_module_sym_map_.count (name);
2615
+ }
2616
+
2626
2617
void CWriter::Write (const Func& func) {
2627
2618
func_ = &func;
2628
2619
local_syms_.clear ();
@@ -2643,7 +2634,7 @@ void CWriter::Write(const Func& func) {
2643
2634
Write (Newline ());
2644
2635
2645
2636
PushFuncSection ();
2646
- Write (ResultType ( func.decl .sig .result_types ) , " " ,
2637
+ Write (func.decl .sig .result_types , " " ,
2647
2638
GlobalName (ModuleFieldType::Func, func.name ), " (" );
2648
2639
WriteParamsAndLocals ();
2649
2640
Write (" FUNC_PROLOGUE;" , Newline ());
@@ -2666,7 +2657,7 @@ void CWriter::Write(const Func& func) {
2666
2657
Write (" return " , StackVar (0 ), " ;" , Newline ());
2667
2658
} else if (num_results >= 2 ) {
2668
2659
Write (OpenBrace ());
2669
- Write (ResultType ( func.decl .sig .result_types ) , " tmp;" , Newline ());
2660
+ Write (func.decl .sig .result_types , " tmp;" , Newline ());
2670
2661
for (Index i = 0 ; i < num_results; ++i) {
2671
2662
Type type = func.GetResultType (i);
2672
2663
Writef (" tmp.%c%d = " , MangleType (type), i);
@@ -2909,9 +2900,8 @@ void CWriter::Write(const Catch& c) {
2909
2900
return ;
2910
2901
}
2911
2902
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 ());
2915
2905
2916
2906
const Tag* tag = module_->GetTag (c.var );
2917
2907
const FuncDeclaration& tag_type = tag->decl ;
@@ -3060,8 +3050,7 @@ void CWriter::Write(const ExprList& exprs) {
3060
3050
3061
3051
assert (var.is_name ());
3062
3052
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 )) {
3065
3054
Write (" instance->" , GlobalName (ModuleFieldType::Import,
3066
3055
import_module_sym_map_[func.name ]));
3067
3056
} else {
@@ -3389,10 +3378,9 @@ void CWriter::Write(const ExprList& exprs) {
3389
3378
3390
3379
Write (StackVar (0 ), " = (wasm_rt_funcref_t){" , FuncTypeExpr (func_type),
3391
3380
" , (wasm_rt_function_ptr_t)" ,
3392
- ExternalPtr (ModuleFieldType::Func, func->name ), " , " );
3381
+ ExternalRef (ModuleFieldType::Func, func->name ), " , " );
3393
3382
3394
- bool is_import = import_module_sym_map_.count (func->name ) != 0 ;
3395
- if (is_import) {
3383
+ if (IsImport (func->name )) {
3396
3384
Write (" instance->" , GlobalName (ModuleFieldType::Import,
3397
3385
import_module_sym_map_[func->name ]));
3398
3386
} else {
@@ -3518,12 +3506,10 @@ void CWriter::Write(const ExprList& exprs) {
3518
3506
3519
3507
Index num_params = tag->decl .GetNumParams ();
3520
3508
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);" ,
3523
3510
Newline ());
3524
3511
} 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(" ,
3527
3513
tag->decl .GetParamType (0 ), " ), &" , StackVar (0 ), " );" ,
3528
3514
Newline ());
3529
3515
} else {
@@ -3534,8 +3520,7 @@ void CWriter::Write(const ExprList& exprs) {
3534
3520
Write (StackVar (i), " , " );
3535
3521
}
3536
3522
Write (" };" , Newline ());
3537
- Write (" wasm_rt_load_exception(" ,
3538
- ExternalPtr (ModuleFieldType::Tag, tag->name ),
3523
+ Write (" wasm_rt_load_exception(" , TagPtr (tag->name ),
3539
3524
" , sizeof(tmp), &tmp);" , Newline ());
3540
3525
Write (CloseBrace (), Newline ());
3541
3526
}
0 commit comments