From 08bd0b92116468b52776a55e1fb9dcd479c4a42d Mon Sep 17 00:00:00 2001 From: JF Bastien Date: Wed, 26 Aug 2015 22:09:54 +0000 Subject: [PATCH] WebAssembly: handle private/internal globals. Things of note: - Other linkage types aren't handled yet. We'll figure it out with dynamic linking. - Special LLVM globals are either ignored, or error out for now. - TLS isn't supported yet (WebAssembly will have threads later). - There currently isn't a syntax for alignment, I left it in a comment so it's easy to hook up. - Undef is convereted to whatever the type's appropriate null value is. - assert versus report_fatal_error: follow what other AsmPrinters do, and assert only on what should have been caught elsewhere. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@246092 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp | 118 ++++++++++++++++++----- test/CodeGen/WebAssembly/global.ll | 37 +++++++ 2 files changed, 133 insertions(+), 22 deletions(-) create mode 100644 test/CodeGen/WebAssembly/global.ll diff --git a/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp b/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp index aa7734118a6..ea42470ab00 100644 --- a/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp +++ b/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp @@ -68,6 +68,8 @@ private: // AsmPrinter Implementation. //===------------------------------------------------------------------===// + void EmitGlobalVariable(const GlobalVariable *GV) override; + void EmitConstantPool() override; void EmitFunctionEntryLabel() override; void EmitFunctionBodyStart() override; @@ -79,12 +81,14 @@ private: } // end anonymous namespace //===----------------------------------------------------------------------===// +// Helpers. +//===----------------------------------------------------------------------===// // Untyped, lower-case version of the opcode's name matching the names // WebAssembly opcodes are expected to have. The tablegen names are uppercase // and suffixed with their type (after an underscore). -static SmallString<32> Name(const WebAssemblyInstrInfo *TII, - const MachineInstr *MI) { +static SmallString<32> OpcodeName(const WebAssemblyInstrInfo *TII, + const MachineInstr *MI) { std::string N(StringRef(TII->getName(MI->getOpcode())).lower()); std::string::size_type End = N.rfind('_'); End = std::string::npos == End ? N.length() : End; @@ -93,7 +97,7 @@ static SmallString<32> Name(const WebAssemblyInstrInfo *TII, static std::string toSymbol(StringRef S) { return ("$" + S).str(); } -static const char *toType(const Type *Ty) { +static const char *toString(const Type *Ty) { switch (Ty->getTypeID()) { default: break; case Type::FloatTyID: return "f32"; @@ -110,6 +114,91 @@ static const char *toType(const Type *Ty) { return ""; } +static std::string toString(const APFloat &FP) { + static const size_t BufBytes = 128; + char buf[BufBytes]; + if (FP.isNaN()) + assert((FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics())) || + FP.bitwiseIsEqual( + APFloat::getQNaN(FP.getSemantics(), /*Negative=*/true))) && + "convertToHexString handles neither SNaN nor NaN payloads"); + // Use C99's hexadecimal floating-point representation. + auto Written = FP.convertToHexString( + buf, /*hexDigits=*/0, /*upperCase=*/false, APFloat::rmNearestTiesToEven); + (void)Written; + assert(Written != 0); + assert(Written < BufBytes); + return buf; +} + +//===----------------------------------------------------------------------===// +// WebAssemblyAsmPrinter Implementation. +//===----------------------------------------------------------------------===// + +void WebAssemblyAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) { + SmallString<128> Str; + raw_svector_ostream OS(Str); + StringRef Name = GV->getName(); + DEBUG(dbgs() << "Global " << Name << '\n'); + + if (!GV->hasInitializer()) { + DEBUG(dbgs() << " Skipping declaration.\n"); + return; + } + + // Check to see if this is a special global used by LLVM. + static const char *Ignored[] = {"llvm.used", "llvm.metadata"}; + for (const char *I : Ignored) + if (Name == I) + return; + // FIXME: Handle the following globals. + static const char *Unhandled[] = {"llvm.global_ctors", "llvm.global_dtors"}; + for (const char *U : Unhandled) + if (Name == U) + report_fatal_error("Unhandled global"); + if (Name.startswith("llvm.")) + report_fatal_error("Unknown LLVM-internal global"); + + if (GV->isThreadLocal()) + report_fatal_error("TLS isn't yet supported by WebAssembly"); + + const DataLayout &DL = getDataLayout(); + const Constant *Init = GV->getInitializer(); + if (isa(Init)) + Init = Constant::getNullValue(Init->getType()); + unsigned Align = DL.getPrefTypeAlignment(Init->getType()); + + switch (GV->getLinkage()) { + case GlobalValue::InternalLinkage: + case GlobalValue::PrivateLinkage: + break; + case GlobalValue::AppendingLinkage: + case GlobalValue::LinkOnceAnyLinkage: + case GlobalValue::LinkOnceODRLinkage: + case GlobalValue::WeakAnyLinkage: + case GlobalValue::WeakODRLinkage: + case GlobalValue::ExternalLinkage: + case GlobalValue::CommonLinkage: + report_fatal_error("Linkage types other than internal and private aren't " + "supported by WebAssembly"); + default: + llvm_unreachable("Unknown linkage type"); + return; + } + + OS << "(global " << toSymbol(Name) << ' ' << toString(Init->getType()) << ' '; + if (const auto *C = dyn_cast(Init)) { + assert(C->getBitWidth() <= 64 && "Printing wider types unimplemented"); + OS << C->getZExtValue(); + } else if (const auto *C = dyn_cast(Init)) { + OS << toString(C->getValueAPF()); + } else { + assert(false && "Only integer and floating-point constants are supported"); + } + OS << ") ;; align " << Align << "\n"; + OutStreamer->EmitRawText(OS.str()); +} + void WebAssemblyAsmPrinter::EmitConstantPool() { assert(MF->getConstantPool()->getConstants().empty() && "WebAssembly disables constant pools"); @@ -139,10 +228,10 @@ void WebAssemblyAsmPrinter::EmitFunctionBodyStart() { raw_svector_ostream OS(Str); const Function *F = MF->getFunction(); for (const Argument &A : F->args()) - OS << " (param " << toType(A.getType()) << ')'; + OS << " (param " << toString(A.getType()) << ')'; const Type *Rt = F->getReturnType(); if (!Rt->isVoidTy()) - OS << " (result " << toType(Rt) << ')'; + OS << " (result " << toString(Rt) << ')'; OS << '\n'; OutStreamer->EmitRawText(OS.str()); } @@ -171,7 +260,7 @@ void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) { OS << "(setlocal @" << TargetRegisterInfo::virtReg2Index(Reg) << ' '; } - OS << '(' << Name(TII, MI); + OS << '(' << OpcodeName(TII, MI); for (const MachineOperand &MO : MI->uses()) switch (MO.getType()) { default: @@ -186,22 +275,7 @@ void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) { OS << ' ' << MO.getImm(); } break; case MachineOperand::MO_FPImmediate: { - static const size_t BufBytes = 128; - char buf[BufBytes]; - APFloat FP = MO.getFPImm()->getValueAPF(); - if (FP.isNaN()) - assert((FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics())) || - FP.bitwiseIsEqual( - APFloat::getQNaN(FP.getSemantics(), /*Negative=*/true))) && - "convertToHexString handles neither SNaN nor NaN payloads"); - // Use C99's hexadecimal floating-point representation. - auto Written = - FP.convertToHexString(buf, /*hexDigits=*/0, /*upperCase=*/false, - APFloat::rmNearestTiesToEven); - (void)Written; - assert(Written != 0); - assert(Written < BufBytes); - OS << ' ' << buf; + OS << ' ' << toString(MO.getFPImm()->getValueAPF()); } break; case MachineOperand::MO_GlobalAddress: { OS << ' ' << toSymbol(MO.getGlobal()->getName()); diff --git a/test/CodeGen/WebAssembly/global.ll b/test/CodeGen/WebAssembly/global.ll new file mode 100644 index 00000000000..669f212cf80 --- /dev/null +++ b/test/CodeGen/WebAssembly/global.ll @@ -0,0 +1,37 @@ +; RUN: llc < %s -asm-verbose=false | FileCheck %s + +; Test that globals assemble as expected. + +target datalayout = "e-p:32:32-i64:64-v128:8:128-n32:64-S128" +target triple = "wasm32-unknown-unknown" + +; CHECK-NOT: llvm.used +; CHECK-NOT: llvm.metadata +@llvm.used = appending global [1 x i32*] [i32* @g], section "llvm.metadata" + +@g = private global i32 1337; ; CHECK: (global $g i32 1337) + +@ud = internal global i32 undef; ; CHECK: (global $ud i32 0) +@nil = internal global i32 zeroinitializer; ; CHECK: (global $nil i32 0) +@z = internal global i32 0; ; CHECK: (global $z i32 0) +@one = internal global i32 1; ; CHECK: (global $one i32 1) +@answer = internal global i32 42; ; CHECK: (global $answer i32 42) +@u32max = internal global i32 -1; ; CHECK: (global $u32max i32 4294967295) + +@ud64 = internal global i64 undef; ; CHECK: (global $ud64 i64 0) +@nil64 = internal global i64 zeroinitializer; ; CHECK: (global $nil64 i64 0) +@z64 = internal global i64 0; ; CHECK: (global $z64 i64 0) +@twoP32 = internal global i64 4294967296; ; CHECK: (global $twoP32 i64 4294967296) +@u64max = internal global i64 -1; ; CHECK: (global $u64max i64 18446744073709551615) + +@f32ud = internal global float undef; ; CHECK: (global $f32ud f32 0x0p0) +@f32nil = internal global float zeroinitializer; ; CHECK: (global $f32nil f32 0x0p0) +@f32z = internal global float 0.0; ; CHECK: (global $f32z f32 0x0p0) +@f32nz = internal global float -0.0; ; CHECK: (global $f32nz f32 -0x0p0) +@f32two = internal global float 2.0; ; CHECK: (global $f32two f32 0x1p1) + +@f64ud = internal global double undef; ; CHECK: (global $f64ud f64 0x0p0) +@f64nil = internal global double zeroinitializer; ; CHECK: (global $f64nil f64 0x0p0) +@f64z = internal global double 0.0; ; CHECK: (global $f64z f64 0x0p0) +@f64nz = internal global double -0.0; ; CHECK: (global $f64nz f64 -0x0p0) +@f64two = internal global double 2.0; ; CHECK: (global $f64two f64 0x1p1) -- 2.11.0