@@ -106,7 +106,7 @@ class Writer {
106
106
std::vector<const Symbol *> ImportedSymbols;
107
107
unsigned NumImportedFunctions = 0 ;
108
108
unsigned NumImportedGlobals = 0 ;
109
- std::vector<Symbol *> ExportedSymbols ;
109
+ std::vector<WasmExport> Exports ;
110
110
std::vector<const DefinedData *> DefinedFakeGlobals;
111
111
std::vector<InputGlobal *> InputGlobals;
112
112
std::vector<InputFunction *> InputFunctions;
@@ -264,41 +264,15 @@ void Writer::createTableSection() {
264
264
}
265
265
266
266
void Writer::createExportSection () {
267
- bool ExportMemory = !Config->Relocatable && !Config->ImportMemory ;
268
- bool ExportTable = !Config->Relocatable && Config->ExportTable ;
269
-
270
- uint32_t NumExports =
271
- (ExportMemory ? 1 : 0 ) + (ExportTable ? 1 : 0 ) + ExportedSymbols.size ();
272
- if (!NumExports)
267
+ if (!Exports.size ())
273
268
return ;
274
269
275
270
SyntheticSection *Section = createSyntheticSection (WASM_SEC_EXPORT);
276
271
raw_ostream &OS = Section->getStream ();
277
272
278
- writeUleb128 (OS, NumExports, " export count" );
279
-
280
- if (ExportMemory)
281
- writeExport (OS, {" memory" , WASM_EXTERNAL_MEMORY, 0 });
282
- if (ExportTable)
283
- writeExport (OS, {kFunctionTableName , WASM_EXTERNAL_TABLE, 0 });
284
-
285
- unsigned FakeGlobalIndex = NumImportedGlobals + InputGlobals.size ();
286
-
287
- for (const Symbol *Sym : ExportedSymbols) {
288
- StringRef Name = Sym->getName ();
289
- WasmExport Export;
290
- DEBUG (dbgs () << " Export: " << Name << " \n " );
291
-
292
- if (auto *F = dyn_cast<DefinedFunction>(Sym))
293
- Export = {Name, WASM_EXTERNAL_FUNCTION, F->getFunctionIndex ()};
294
- else if (auto *G = dyn_cast<DefinedGlobal>(Sym))
295
- Export = {Name, WASM_EXTERNAL_GLOBAL, G->getGlobalIndex ()};
296
- else if (isa<DefinedData>(Sym))
297
- Export = {Name, WASM_EXTERNAL_GLOBAL, FakeGlobalIndex++};
298
- else
299
- llvm_unreachable (" unexpected symbol type" );
273
+ writeUleb128 (OS, Exports.size (), " export count" );
274
+ for (const WasmExport &Export : Exports)
300
275
writeExport (OS, Export);
301
- }
302
276
}
303
277
304
278
void Writer::calculateCustomSections () {
@@ -755,6 +729,14 @@ void Writer::calculateExports() {
755
729
if (Config->Relocatable )
756
730
return ;
757
731
732
+ if (!Config->Relocatable && !Config->ImportMemory )
733
+ Exports.push_back (WasmExport{" memory" , WASM_EXTERNAL_MEMORY, 0 });
734
+
735
+ if (!Config->Relocatable && Config->ExportTable )
736
+ Exports.push_back (WasmExport{kFunctionTableName , WASM_EXTERNAL_TABLE, 0 });
737
+
738
+ unsigned FakeGlobalIndex = NumImportedGlobals + InputGlobals.size ();
739
+
758
740
for (Symbol *Sym : Symtab->getSymbols ()) {
759
741
if (!Sym->isDefined ())
760
742
continue ;
@@ -763,11 +745,20 @@ void Writer::calculateExports() {
763
745
if (!Sym->isLive ())
764
746
continue ;
765
747
766
- DEBUG (dbgs () << " exporting sym: " << Sym->getName () << " \n " );
767
-
768
- if (auto *D = dyn_cast<DefinedData>(Sym))
748
+ StringRef Name = Sym->getName ();
749
+ WasmExport Export;
750
+ if (auto *F = dyn_cast<DefinedFunction>(Sym)) {
751
+ Export = {Name, WASM_EXTERNAL_FUNCTION, F->getFunctionIndex ()};
752
+ } else if (auto *G = dyn_cast<DefinedGlobal>(Sym)) {
753
+ Export = {Name, WASM_EXTERNAL_GLOBAL, G->getGlobalIndex ()};
754
+ } else {
755
+ auto *D = cast<DefinedData>(Sym);
769
756
DefinedFakeGlobals.emplace_back (D);
770
- ExportedSymbols.emplace_back (Sym);
757
+ Export = {Name, WASM_EXTERNAL_GLOBAL, FakeGlobalIndex++};
758
+ }
759
+
760
+ DEBUG (dbgs () << " Export: " << Name << " \n " );
761
+ Exports.push_back (Export);
771
762
}
772
763
}
773
764
0 commit comments