1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-23 03:02:36 +01:00

[WebAssembly] Implement WASM_STACK_POINTER.

Use the .stack_pointer directive to implement WASM_STACK_POINTER for
specifying a global variable to be the stack pointer.

llvm-svn: 319797
This commit is contained in:
Dan Gohman 2017-12-05 17:23:43 +00:00
parent e031bf30bf
commit 2aaf751ad1
5 changed files with 28 additions and 20 deletions

View File

@ -287,7 +287,7 @@ private:
void writeLinkingMetaDataSection(
ArrayRef<WasmDataSegment> Segments, uint32_t DataSize,
SmallVector<std::pair<StringRef, uint32_t>, 4> SymbolFlags,
bool HasStackPointer, uint32_t StackPointerGlobal);
Optional<uint32_t> StackPointerGlobal);
uint32_t getProvisionalValue(const WasmRelocationEntry &RelEntry);
void applyRelocations(ArrayRef<WasmRelocationEntry> Relocations,
@ -929,14 +929,14 @@ void WasmObjectWriter::writeDataRelocSection() {
void WasmObjectWriter::writeLinkingMetaDataSection(
ArrayRef<WasmDataSegment> Segments, uint32_t DataSize,
SmallVector<std::pair<StringRef, uint32_t>, 4> SymbolFlags,
bool HasStackPointer, uint32_t StackPointerGlobal) {
Optional<uint32_t> StackPointerGlobal) {
SectionBookkeeping Section;
startSection(Section, wasm::WASM_SEC_CUSTOM, "linking");
SectionBookkeeping SubSection;
if (HasStackPointer) {
if (StackPointerGlobal.hasValue()) {
startSection(SubSection, wasm::WASM_STACK_POINTER);
encodeULEB128(StackPointerGlobal, getStream()); // id
encodeULEB128(StackPointerGlobal.getValue(), getStream()); // id
endSection(SubSection);
}
@ -1010,9 +1010,9 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm,
SmallPtrSet<const MCSymbolWasm *, 4> IsAddressTaken;
unsigned NumFuncImports = 0;
SmallVector<WasmDataSegment, 4> DataSegments;
uint32_t StackPointerGlobal = 0;
Optional<StringRef> StackPointerGlobalName;
Optional<uint32_t> StackPointerGlobal;
uint32_t DataSize = 0;
bool HasStackPointer = false;
// Populate the IsAddressTaken set.
for (const WasmRelocationEntry &RelEntry : CodeRelocations) {
@ -1143,10 +1143,7 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm,
if (!DataFrag.getFixups().empty())
report_fatal_error("fixups not supported in .stack_pointer");
const SmallVectorImpl<char> &Contents = DataFrag.getContents();
if (Contents.size() != 4)
report_fatal_error("only one entry supported in .stack_pointer");
HasStackPointer = true;
StackPointerGlobal = NumGlobalImports + *(const int32_t *)Contents.data();
StackPointerGlobalName = StringRef(Contents.data(), Contents.size());
}
for (MCSection &Sec : Asm) {
@ -1255,6 +1252,10 @@ 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.
@ -1331,7 +1332,7 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm,
writeCodeRelocSection();
writeDataRelocSection();
writeLinkingMetaDataSection(DataSegments, DataSize, SymbolFlags,
HasStackPointer, StackPointerGlobal);
StackPointerGlobal);
// TODO: Translate the .comment section to the output.
// TODO: Translate debug sections to the output.

View File

@ -108,8 +108,8 @@ void WebAssemblyTargetAsmStreamer::emitGlobal(
}
}
void WebAssemblyTargetAsmStreamer::emitStackPointer(uint32_t Index) {
OS << "\t.stack_pointer\t" << Index << '\n';
void WebAssemblyTargetAsmStreamer::emitStackPointer(MCSymbol *Symbol) {
OS << "\t.stack_pointer\t" << Symbol->getName() << '\n';
}
void WebAssemblyTargetAsmStreamer::emitEndFunc() { OS << "\t.endfunc\n"; }
@ -158,7 +158,7 @@ void WebAssemblyTargetELFStreamer::emitGlobal(
}
void WebAssemblyTargetELFStreamer::emitStackPointer(
uint32_t Index) {
MCSymbol *Symbol) {
llvm_unreachable(".stack_pointer encoding not yet implemented");
}
@ -238,11 +238,11 @@ void WebAssemblyTargetWasmStreamer::emitGlobal(
Streamer.PopSection();
}
void WebAssemblyTargetWasmStreamer::emitStackPointer(uint32_t Index) {
void WebAssemblyTargetWasmStreamer::emitStackPointer(MCSymbol *Symbol) {
Streamer.PushSection();
Streamer.SwitchSection(Streamer.getContext().getWasmSection(
".stack_pointer", SectionKind::getMetadata()));
Streamer.EmitIntValue(Index, 4);
Streamer.EmitBytes(Symbol->getName());
Streamer.PopSection();
}
@ -277,4 +277,5 @@ void WebAssemblyTargetWasmStreamer::emitIndirectFunctionType(
}
void WebAssemblyTargetWasmStreamer::emitGlobalImport(StringRef name) {
llvm_unreachable(".global_import is not needed for direct wasm output");
}

View File

@ -40,7 +40,7 @@ public:
/// .globalvar
virtual void emitGlobal(ArrayRef<wasm::Global> Globals) = 0;
/// .stack_pointer
virtual void emitStackPointer(uint32_t Index) = 0;
virtual void emitStackPointer(MCSymbol *Symbol) = 0;
/// .endfunc
virtual void emitEndFunc() = 0;
/// .functype
@ -67,7 +67,7 @@ public:
void emitResult(MCSymbol *Symbol, ArrayRef<MVT> Types) override;
void emitLocal(ArrayRef<MVT> Types) override;
void emitGlobal(ArrayRef<wasm::Global> Globals) override;
void emitStackPointer(uint32_t Index) override;
void emitStackPointer(MCSymbol *Symbol) override;
void emitEndFunc() override;
void emitIndirectFunctionType(MCSymbol *Symbol,
SmallVectorImpl<MVT> &Params,
@ -85,7 +85,7 @@ public:
void emitResult(MCSymbol *Symbol, ArrayRef<MVT> Types) override;
void emitLocal(ArrayRef<MVT> Types) override;
void emitGlobal(ArrayRef<wasm::Global> Globals) override;
void emitStackPointer(uint32_t Index) override;
void emitStackPointer(MCSymbol *Symbol) override;
void emitEndFunc() override;
void emitIndirectFunctionType(MCSymbol *Symbol,
SmallVectorImpl<MVT> &Params,
@ -103,7 +103,7 @@ public:
void emitResult(MCSymbol *Symbol, ArrayRef<MVT> Types) override;
void emitLocal(ArrayRef<MVT> Types) override;
void emitGlobal(ArrayRef<wasm::Global> Globals) override;
void emitStackPointer(uint32_t Index) override;
void emitStackPointer(MCSymbol *Symbol) override;
void emitEndFunc() override;
void emitIndirectFunctionType(MCSymbol *Symbol,
SmallVectorImpl<MVT> &Params,

View File

@ -78,6 +78,10 @@ WebAssemblyTargetStreamer *WebAssemblyAsmPrinter::getTargetStreamer() {
//===----------------------------------------------------------------------===//
void WebAssemblyAsmPrinter::EmitEndOfAsmFile(Module &M) {
// Declare the stack pointer.
getTargetStreamer()->emitStackPointer(
GetExternalSymbolSymbol("__stack_pointer"));
for (const auto &F : M) {
// Emit function type info for all undefined functions
if (F.isDeclarationForLinker() && !F.isIntrinsic()) {

View File

@ -147,3 +147,5 @@ entry:
call void @somefunc(i32* %static)
ret void
}
; CHECK: .stack_pointer __stack_pointer