llvm.org GIT mirror llvm / f07a1d4
[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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@319797 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 2 years ago
5 changed file(s) with 29 addition(s) and 21 deletion(s). Raw diff Collapse all Expand all
286286 void writeLinkingMetaDataSection(
287287 ArrayRef Segments, uint32_t DataSize,
288288 SmallVector, 4> SymbolFlags,
289 bool HasStackPointer, uint32_t StackPointerGlobal);
289 Optional StackPointerGlobal);
290290
291291 uint32_t getProvisionalValue(const WasmRelocationEntry &RelEntry);
292292 void applyRelocations(ArrayRef Relocations,
928928 void WasmObjectWriter::writeLinkingMetaDataSection(
929929 ArrayRef Segments, uint32_t DataSize,
930930 SmallVector, 4> SymbolFlags,
931 bool HasStackPointer, uint32_t StackPointerGlobal) {
931 Optional StackPointerGlobal) {
932932 SectionBookkeeping Section;
933933 startSection(Section, wasm::WASM_SEC_CUSTOM, "linking");
934934 SectionBookkeeping SubSection;
935935
936 if (HasStackPointer) {
936 if (StackPointerGlobal.hasValue()) {
937937 startSection(SubSection, wasm::WASM_STACK_POINTER);
938 encodeULEB128(StackPointerGlobal, getStream()); // id
938 encodeULEB128(StackPointerGlobal.getValue(), getStream()); // id
939939 endSection(SubSection);
940940 }
941941
10091009 SmallPtrSet IsAddressTaken;
10101010 unsigned NumFuncImports = 0;
10111011 SmallVector DataSegments;
1012 uint32_t StackPointerGlobal = 0;
1012 Optional StackPointerGlobalName;
1013 Optional StackPointerGlobal;
10131014 uint32_t DataSize = 0;
1014 bool HasStackPointer = false;
10151015
10161016 // Populate the IsAddressTaken set.
10171017 for (const WasmRelocationEntry &RelEntry : CodeRelocations) {
11421142 if (!DataFrag.getFixups().empty())
11431143 report_fatal_error("fixups not supported in .stack_pointer");
11441144 const SmallVectorImpl &Contents = DataFrag.getContents();
1145 if (Contents.size() != 4)
1146 report_fatal_error("only one entry supported in .stack_pointer");
1147 HasStackPointer = true;
1148 StackPointerGlobal = NumGlobalImports + *(const int32_t *)Contents.data();
1145 StackPointerGlobalName = StringRef(Contents.data(), Contents.size());
11491146 }
11501147
11511148 for (MCSection &Sec : Asm) {
12541251 SymbolIndices[&WS] = Index;
12551252 DEBUG(dbgs() << " -> global index: " << Index << "\n");
12561253 Globals.push_back(Global);
1254
1255 if (StackPointerGlobalName.hasValue() &&
1256 WS.getName() == StackPointerGlobalName.getValue())
1257 StackPointerGlobal = Index;
12571258 }
12581259
12591260 // If the symbol is visible outside this translation unit, export it.
13301331 writeCodeRelocSection();
13311332 writeDataRelocSection();
13321333 writeLinkingMetaDataSection(DataSegments, DataSize, SymbolFlags,
1333 HasStackPointer, StackPointerGlobal);
1334 StackPointerGlobal);
13341335
13351336 // TODO: Translate the .comment section to the output.
13361337 // TODO: Translate debug sections to the output.
107107 }
108108 }
109109
110 void WebAssemblyTargetAsmStreamer::emitStackPointer(uint32_t Index) {
111 OS << "\t.stack_pointer\t" << Index << '\n';
110 void WebAssemblyTargetAsmStreamer::emitStackPointer(MCSymbol *Symbol) {
111 OS << "\t.stack_pointer\t" << Symbol->getName() << '\n';
112112 }
113113
114114 void WebAssemblyTargetAsmStreamer::emitEndFunc() { OS << "\t.endfunc\n"; }
157157 }
158158
159159 void WebAssemblyTargetELFStreamer::emitStackPointer(
160 uint32_t Index) {
160 MCSymbol *Symbol) {
161161 llvm_unreachable(".stack_pointer encoding not yet implemented");
162162 }
163163
237237 Streamer.PopSection();
238238 }
239239
240 void WebAssemblyTargetWasmStreamer::emitStackPointer(uint32_t Index) {
240 void WebAssemblyTargetWasmStreamer::emitStackPointer(MCSymbol *Symbol) {
241241 Streamer.PushSection();
242242 Streamer.SwitchSection(Streamer.getContext().getWasmSection(
243243 ".stack_pointer", SectionKind::getMetadata()));
244 Streamer.EmitIntValue(Index, 4);
244 Streamer.EmitBytes(Symbol->getName());
245245 Streamer.PopSection();
246246 }
247247
276276 }
277277
278278 void WebAssemblyTargetWasmStreamer::emitGlobalImport(StringRef name) {
279 }
279 llvm_unreachable(".global_import is not needed for direct wasm output");
280 }
3939 /// .globalvar
4040 virtual void emitGlobal(ArrayRef Globals) = 0;
4141 /// .stack_pointer
42 virtual void emitStackPointer(uint32_t Index) = 0;
42 virtual void emitStackPointer(MCSymbol *Symbol) = 0;
4343 /// .endfunc
4444 virtual void emitEndFunc() = 0;
4545 /// .functype
6666 void emitResult(MCSymbol *Symbol, ArrayRef Types) override;
6767 void emitLocal(ArrayRef Types) override;
6868 void emitGlobal(ArrayRef Globals) override;
69 void emitStackPointer(uint32_t Index) override;
69 void emitStackPointer(MCSymbol *Symbol) override;
7070 void emitEndFunc() override;
7171 void emitIndirectFunctionType(MCSymbol *Symbol,
7272 SmallVectorImpl &Params,
8484 void emitResult(MCSymbol *Symbol, ArrayRef Types) override;
8585 void emitLocal(ArrayRef Types) override;
8686 void emitGlobal(ArrayRef Globals) override;
87 void emitStackPointer(uint32_t Index) override;
87 void emitStackPointer(MCSymbol *Symbol) override;
8888 void emitEndFunc() override;
8989 void emitIndirectFunctionType(MCSymbol *Symbol,
9090 SmallVectorImpl &Params,
102102 void emitResult(MCSymbol *Symbol, ArrayRef Types) override;
103103 void emitLocal(ArrayRef Types) override;
104104 void emitGlobal(ArrayRef Globals) override;
105 void emitStackPointer(uint32_t Index) override;
105 void emitStackPointer(MCSymbol *Symbol) override;
106106 void emitEndFunc() override;
107107 void emitIndirectFunctionType(MCSymbol *Symbol,
108108 SmallVectorImpl &Params,
7777 //===----------------------------------------------------------------------===//
7878
7979 void WebAssemblyAsmPrinter::EmitEndOfAsmFile(Module &M) {
80 // Declare the stack pointer.
81 getTargetStreamer()->emitStackPointer(
82 GetExternalSymbolSymbol("__stack_pointer"));
83
8084 for (const auto &F : M) {
8185 // Emit function type info for all undefined functions
8286 if (F.isDeclarationForLinker() && !F.isIntrinsic()) {
146146 call void @somefunc(i32* %static)
147147 ret void
148148 }
149
150 ; CHECK: .stack_pointer __stack_pointer