[WebAssembly] Emit .import_global assembler directives

Support a new assembler directive, .import_global, to declare imported
global variables (i.e. those with external linkage and no
initializer). The linker turns these into wasm imports.

Patch by Jacob Gravelle

Differential Revision: https://reviews.llvm.org/D26875

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@288296 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Derek Schuff 2016-12-01 00:11:15 +00:00
parent 29c7b3a03e
commit a95cded1a3
4 changed files with 20 additions and 0 deletions

View File

@ -80,6 +80,10 @@ void WebAssemblyTargetAsmStreamer::emitIndirectFunctionType(
OS << '\n';
}
void WebAssemblyTargetAsmStreamer::emitGlobalImport(StringRef name) {
OS << "\t.import_global\t" << name << '\n';
}
void WebAssemblyTargetAsmStreamer::emitIndIdx(const MCExpr *Value) {
OS << "\t.indidx \t" << *Value << '\n';
}
@ -111,3 +115,6 @@ void WebAssemblyTargetELFStreamer::emitIndirectFunctionType(
// Nothing to emit here. TODO: Re-design how linking works and re-evaluate
// whether it's necessary for .o files to declare indirect function types.
}
void WebAssemblyTargetELFStreamer::emitGlobalImport(StringRef name) {
}

View File

@ -45,6 +45,8 @@ public:
}
/// .indidx
virtual void emitIndIdx(const MCExpr *Value) = 0;
/// .import_global
virtual void emitGlobalImport(StringRef name) = 0;
};
/// This part is for ascii assembly output
@ -62,6 +64,7 @@ public:
SmallVectorImpl<MVT> &Params,
SmallVectorImpl<MVT> &Results) override;
void emitIndIdx(const MCExpr *Value) override;
void emitGlobalImport(StringRef name) override;
};
/// This part is for ELF object output
@ -77,6 +80,7 @@ public:
SmallVectorImpl<MVT> &Params,
SmallVectorImpl<MVT> &Results) override;
void emitIndIdx(const MCExpr *Value) override;
void emitGlobalImport(StringRef name) override;
};
} // end namespace llvm

View File

@ -133,6 +133,11 @@ void WebAssemblyAsmPrinter::EmitEndOfAsmFile(Module &M) {
Results);
}
}
for (const auto &G : M.globals()) {
if (!G.hasInitializer() && G.hasExternalLinkage()) {
getTargetStreamer()->emitGlobalImport(G.getGlobalIdentifier());
}
}
}
void WebAssemblyAsmPrinter::EmitConstantPool() {

View File

@ -8,3 +8,7 @@ target triple = "wasm32-unknown-unknown"
define void @foo() {
ret void
}
; Check import directives - must be at the end of the file
; CHECK: .import_global bar{{$}}
@bar = external global i32