summaryrefslogtreecommitdiffstats
path: root/llvm/lib/MC/WasmObjectWriter.cpp
diff options
context:
space:
mode:
authorDan Gohman <dan433584@gmail.com>2017-12-05 18:29:48 +0000
committerDan Gohman <dan433584@gmail.com>2017-12-05 18:29:48 +0000
commit32ce5ca07c5f0df389d023eee16005e6eb6bd160 (patch)
tree92f230efca8e3b8dfcd8802881326e23ab7b7681 /llvm/lib/MC/WasmObjectWriter.cpp
parent74de7e07916d3efb16fdabc3d2cfca085d026896 (diff)
downloadbcm5719-llvm-32ce5ca07c5f0df389d023eee16005e6eb6bd160.tar.gz
bcm5719-llvm-32ce5ca07c5f0df389d023eee16005e6eb6bd160.zip
[WebAssembly] Make stack-pointer imports mutable.
This is not currently valid by the wasm spec, however: - It replaces doing set_global on an immutable global, which is also not valid. - It's expected be valid in the near future: https://github.com/WebAssembly/threads/blob/master/proposals/threads/Globals.md - This only occurs before linking, so a fully linked object will be valid. llvm-svn: 319810
Diffstat (limited to 'llvm/lib/MC/WasmObjectWriter.cpp')
-rw-r--r--llvm/lib/MC/WasmObjectWriter.cpp87
1 files changed, 47 insertions, 40 deletions
diff --git a/llvm/lib/MC/WasmObjectWriter.cpp b/llvm/lib/MC/WasmObjectWriter.cpp
index 88b960e457a..0e93eb835c0 100644
--- a/llvm/lib/MC/WasmObjectWriter.cpp
+++ b/llvm/lib/MC/WasmObjectWriter.cpp
@@ -115,6 +115,7 @@ struct WasmImport {
StringRef FieldName;
unsigned Kind;
int32_t Type;
+ bool IsMutable;
};
// A wasm function to be written into the function section.
@@ -681,7 +682,7 @@ void WasmObjectWriter::writeImportSection(ArrayRef<WasmImport> Imports) {
break;
case wasm::WASM_EXTERNAL_GLOBAL:
encodeSLEB128(int32_t(Import.Type), getStream());
- encodeULEB128(0, getStream()); // mutability
+ encodeULEB128(int32_t(Import.IsMutable), getStream());
break;
default:
llvm_unreachable("unsupported import kind");
@@ -1036,41 +1037,6 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm,
}
}
- // Populate FunctionTypeIndices and Imports.
- for (const MCSymbol &S : Asm.symbols()) {
- const auto &WS = static_cast<const MCSymbolWasm &>(S);
-
- // Register types for all functions, including those with private linkage
- // (making them
- // because wasm always needs a type signature.
- if (WS.isFunction())
- registerFunctionType(WS);
-
- if (WS.isTemporary())
- continue;
-
- // If the symbol is not defined in this translation unit, import it.
- if (!WS.isDefined(/*SetUsed=*/false)) {
- WasmImport Import;
- Import.ModuleName = WS.getModuleName();
- Import.FieldName = WS.getName();
-
- if (WS.isFunction()) {
- Import.Kind = wasm::WASM_EXTERNAL_FUNCTION;
- Import.Type = getFunctionType(WS);
- SymbolIndices[&WS] = NumFuncImports;
- ++NumFuncImports;
- } else {
- Import.Kind = wasm::WASM_EXTERNAL_GLOBAL;
- Import.Type = int32_t(PtrType);
- SymbolIndices[&WS] = NumGlobalImports;
- ++NumGlobalImports;
- }
-
- Imports.push_back(Import);
- }
- }
-
// In the special .global_variables section, we've encoded global
// variables used by the function. Translate them into the Globals
// list.
@@ -1146,6 +1112,51 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm,
StackPointerGlobalName = StringRef(Contents.data(), Contents.size());
}
+ // Populate FunctionTypeIndices and Imports.
+ for (const MCSymbol &S : Asm.symbols()) {
+ const auto &WS = static_cast<const MCSymbolWasm &>(S);
+
+ // Register types for all functions, including those with private linkage
+ // (making them
+ // because wasm always needs a type signature.
+ if (WS.isFunction())
+ registerFunctionType(WS);
+
+ if (WS.isTemporary())
+ continue;
+
+ // If the symbol is not defined in this translation unit, import it.
+ if (!WS.isDefined(/*SetUsed=*/false)) {
+ WasmImport Import;
+ Import.ModuleName = WS.getModuleName();
+ Import.FieldName = WS.getName();
+
+ if (WS.isFunction()) {
+ Import.Kind = wasm::WASM_EXTERNAL_FUNCTION;
+ Import.Type = getFunctionType(WS);
+ SymbolIndices[&WS] = NumFuncImports;
+ ++NumFuncImports;
+ } else {
+ Import.Kind = wasm::WASM_EXTERNAL_GLOBAL;
+ Import.Type = int32_t(PtrType);
+ Import.IsMutable = false;
+ SymbolIndices[&WS] = NumGlobalImports;
+
+ // If this global is the stack pointer, make it mutable and remember it
+ // so that we can emit metadata for it.
+ if (StackPointerGlobalName.hasValue() &&
+ WS.getName() == StackPointerGlobalName.getValue()) {
+ Import.IsMutable = true;
+ StackPointerGlobal = NumGlobalImports;
+ }
+
+ ++NumGlobalImports;
+ }
+
+ Imports.push_back(Import);
+ }
+ }
+
for (MCSection &Sec : Asm) {
auto &Section = static_cast<MCSectionWasm &>(Sec);
if (!Section.isWasmData())
@@ -1252,10 +1263,6 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm,
SymbolIndices[&WS] = Index;
DEBUG(dbgs() << " -> global index: " << Index << "\n");
Globals.push_back(Global);
-
- if (StackPointerGlobalName.hasValue() &&
- WS.getName() == StackPointerGlobalName.getValue())
- StackPointerGlobal = Index;
}
// If the symbol is visible outside this translation unit, export it.
OpenPOWER on IntegriCloud