aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/MC/WasmObjectWriter.cpp
diff options
context:
space:
mode:
authorPaulo Matos <pmatos@linki.tools>2020-10-13 07:13:10 -0700
committerSam Clegg <sbc@chromium.org>2020-10-13 07:52:23 -0700
commit388fb67b0dd7f41630ab6c3400c96843852319af (patch)
treea6112bdbd46ee04adc7f68afe0387d8113867d3a /llvm/lib/MC/WasmObjectWriter.cpp
parenta8f1790fdb8ce1c53f024870cd51f32724d4c55f (diff)
downloadllvm-388fb67b0dd7f41630ab6c3400c96843852319af.zip
llvm-388fb67b0dd7f41630ab6c3400c96843852319af.tar.gz
llvm-388fb67b0dd7f41630ab6c3400c96843852319af.tar.bz2
[WebAssembly] Added .tabletype to asm and multiple table support in obj files
Adds more testing in basic-assembly.s and a new test tables.s. Adds support to yaml reading and writing of tables as well. Differential Revision: https://reviews.llvm.org/D88815
Diffstat (limited to 'llvm/lib/MC/WasmObjectWriter.cpp')
-rw-r--r--llvm/lib/MC/WasmObjectWriter.cpp45
1 files changed, 42 insertions, 3 deletions
diff --git a/llvm/lib/MC/WasmObjectWriter.cpp b/llvm/lib/MC/WasmObjectWriter.cpp
index a0e65aa6..4e673f2 100644
--- a/llvm/lib/MC/WasmObjectWriter.cpp
+++ b/llvm/lib/MC/WasmObjectWriter.cpp
@@ -194,8 +194,8 @@ class WasmObjectWriter : public MCObjectWriter {
// Maps function symbols to the table element index space. Used
// for TABLE_INDEX relocation types (i.e. address taken functions).
DenseMap<const MCSymbolWasm *, uint32_t> TableIndices;
- // Maps function/global symbols to the function/global/event/section index
- // space.
+ // Maps function/global/table symbols to the
+ // function/global/table/event/section index space.
DenseMap<const MCSymbolWasm *, uint32_t> WasmIndices;
DenseMap<const MCSymbolWasm *, uint32_t> GOTIndices;
// Maps data symbols to the Wasm segment and offset/size with the segment.
@@ -218,6 +218,9 @@ class WasmObjectWriter : public MCObjectWriter {
SmallVector<WasmDataSegment, 4> DataSegments;
unsigned NumFunctionImports = 0;
unsigned NumGlobalImports = 0;
+ // NumTableImports is initialized to 1 to account for the hardcoded import of
+ // __indirect_function_table
+ unsigned NumTableImports = 1;
unsigned NumEventImports = 0;
uint32_t SectionCount = 0;
@@ -267,6 +270,7 @@ private:
SectionFunctions.clear();
NumFunctionImports = 0;
NumGlobalImports = 0;
+ NumTableImports = 1;
MCObjectWriter::reset();
}
@@ -316,6 +320,7 @@ private:
uint32_t writeDataSection(const MCAsmLayout &Layout);
void writeEventSection(ArrayRef<wasm::WasmEventType> Events);
void writeGlobalSection(ArrayRef<wasm::WasmGlobal> Globals);
+ void writeTableSection(ArrayRef<wasm::WasmTable> Tables);
void writeRelocSection(uint32_t SectionIndex, StringRef Name,
std::vector<WasmRelocationEntry> &Relocations);
void writeLinkingMetaDataSection(
@@ -827,6 +832,24 @@ void WasmObjectWriter::writeGlobalSection(ArrayRef<wasm::WasmGlobal> Globals) {
endSection(Section);
}
+void WasmObjectWriter::writeTableSection(ArrayRef<wasm::WasmTable> Tables) {
+ if (Tables.empty())
+ return;
+
+ SectionBookkeeping Section;
+ startSection(Section, wasm::WASM_SEC_TABLE);
+
+ encodeULEB128(Tables.size(), W->OS);
+ for (const wasm::WasmTable &Table : Tables) {
+ encodeULEB128(Table.ElemType, W->OS);
+ encodeULEB128(Table.Limits.Flags, W->OS);
+ encodeULEB128(Table.Limits.Initial, W->OS);
+ if (Table.Limits.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX)
+ encodeULEB128(Table.Limits.Maximum, W->OS);
+ }
+ endSection(Section);
+}
+
void WasmObjectWriter::writeExportSection(ArrayRef<wasm::WasmExport> Exports) {
if (Exports.empty())
return;
@@ -1003,6 +1026,7 @@ void WasmObjectWriter::writeLinkingMetaDataSection(
case wasm::WASM_SYMBOL_TYPE_FUNCTION:
case wasm::WASM_SYMBOL_TYPE_GLOBAL:
case wasm::WASM_SYMBOL_TYPE_EVENT:
+ case wasm::WASM_SYMBOL_TYPE_TABLE:
encodeULEB128(Sym.ElementIndex, W->OS);
if ((Sym.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0 ||
(Sym.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0)
@@ -1292,6 +1316,7 @@ uint64_t WasmObjectWriter::writeOneObject(MCAssembler &Asm,
SmallVector<wasm::WasmExport, 4> Exports;
SmallVector<wasm::WasmEventType, 1> Events;
SmallVector<wasm::WasmGlobal, 1> Globals;
+ SmallVector<wasm::WasmTable, 1> Tables;
SmallVector<wasm::WasmSymbolInfo, 4> SymbolInfos;
SmallVector<std::pair<uint16_t, uint32_t>, 2> InitFuncs;
std::map<StringRef, std::vector<WasmComdatEntry>> Comdats;
@@ -1494,6 +1519,20 @@ uint64_t WasmObjectWriter::writeOneObject(MCAssembler &Asm,
LLVM_DEBUG(dbgs() << " -> global index: "
<< WasmIndices.find(&WS)->second << "\n");
}
+ } else if (WS.isTable()) {
+ if (WS.isDefined()) {
+ assert(WasmIndices.count(&WS) == 0);
+ wasm::WasmTable Table;
+ Table.ElemType = static_cast<uint8_t>(WS.getTableType());
+ Table.Index = NumTableImports + Tables.size();
+ // FIXME: Work on custom limits is ongoing
+ Table.Limits = {wasm::WASM_LIMITS_FLAG_NONE, 0, 0};
+
+ WasmIndices[&WS] = Table.Index;
+ Tables.push_back(Table);
+ }
+ LLVM_DEBUG(dbgs() << " -> table index: " << WasmIndices.find(&WS)->second
+ << "\n");
} else if (WS.isEvent()) {
// C++ exception symbol (__cpp_exception)
unsigned Index;
@@ -1715,10 +1754,10 @@ uint64_t WasmObjectWriter::writeOneObject(MCAssembler &Asm,
writeTypeSection(Signatures);
writeImportSection(Imports, DataSize, TableElems.size());
writeFunctionSection(Functions);
- // Skip the "table" section; we import the table instead.
// Skip the "memory" section; we import the memory instead.
writeEventSection(Events);
writeGlobalSection(Globals);
+ writeTableSection(Tables);
writeExportSection(Exports);
writeElemSection(TableElems);
writeDataCountSection();