2015-07-23 05:28:15 +08:00
|
|
|
//===-- WebAssemblyAsmPrinter.cpp - WebAssembly LLVM assembly writer ------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// \brief This file contains a printer that converts from our internal
|
|
|
|
/// representation of machine-dependent LLVM code to the WebAssembly assembly
|
|
|
|
/// language.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "WebAssembly.h"
|
|
|
|
#include "WebAssemblyMachineFunctionInfo.h"
|
|
|
|
#include "WebAssemblyRegisterInfo.h"
|
|
|
|
#include "WebAssemblySubtarget.h"
|
|
|
|
#include "InstPrinter/WebAssemblyInstPrinter.h"
|
|
|
|
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
|
|
|
|
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
2015-08-26 07:19:49 +08:00
|
|
|
#include "llvm/CodeGen/MachineConstantPool.h"
|
2015-07-23 05:28:15 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/DebugInfo.h"
|
|
|
|
#include "llvm/MC/MCStreamer.h"
|
2015-08-26 06:58:05 +08:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2015-07-23 05:28:15 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "asm-printer"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class WebAssemblyAsmPrinter final : public AsmPrinter {
|
2015-09-01 06:24:11 +08:00
|
|
|
bool hasAddr64;
|
2015-08-01 01:53:38 +08:00
|
|
|
const WebAssemblyInstrInfo *TII;
|
|
|
|
|
2015-07-23 05:28:15 +08:00
|
|
|
public:
|
|
|
|
WebAssemblyAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
|
2015-08-01 01:53:38 +08:00
|
|
|
: AsmPrinter(TM, std::move(Streamer)), TII(nullptr) {}
|
2015-07-23 05:28:15 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
const char *getPassName() const override {
|
|
|
|
return "WebAssembly Assembly Printer";
|
|
|
|
}
|
|
|
|
|
|
|
|
//===------------------------------------------------------------------===//
|
|
|
|
// MachineFunctionPass Implementation.
|
|
|
|
//===------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AsmPrinter::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
2015-08-01 01:53:38 +08:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override {
|
2015-09-01 06:24:11 +08:00
|
|
|
const auto &Subtarget = MF.getSubtarget<WebAssemblySubtarget>();
|
|
|
|
hasAddr64 = Subtarget.hasAddr64();
|
|
|
|
TII = Subtarget.getInstrInfo();
|
2015-08-01 01:53:38 +08:00
|
|
|
return AsmPrinter::runOnMachineFunction(MF);
|
2015-07-23 05:28:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===------------------------------------------------------------------===//
|
|
|
|
// AsmPrinter Implementation.
|
|
|
|
//===------------------------------------------------------------------===//
|
|
|
|
|
2015-08-27 06:09:54 +08:00
|
|
|
void EmitGlobalVariable(const GlobalVariable *GV) override;
|
|
|
|
|
2015-08-26 07:19:49 +08:00
|
|
|
void EmitConstantPool() override;
|
2015-08-26 06:58:05 +08:00
|
|
|
void EmitFunctionEntryLabel() override;
|
|
|
|
void EmitFunctionBodyStart() override;
|
|
|
|
void EmitFunctionBodyEnd() override;
|
|
|
|
|
2015-07-23 05:28:15 +08:00
|
|
|
void EmitInstruction(const MachineInstr *MI) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2015-08-27 06:09:54 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Helpers.
|
2015-07-23 05:28:15 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-08-07 09:57:03 +08:00
|
|
|
// 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).
|
2015-08-27 06:09:54 +08:00
|
|
|
static SmallString<32> OpcodeName(const WebAssemblyInstrInfo *TII,
|
|
|
|
const MachineInstr *MI) {
|
2015-08-07 09:57:03 +08:00
|
|
|
std::string N(StringRef(TII->getName(MI->getOpcode())).lower());
|
2015-08-25 05:03:24 +08:00
|
|
|
std::string::size_type End = N.rfind('_');
|
2015-08-07 09:57:03 +08:00
|
|
|
End = std::string::npos == End ? N.length() : End;
|
|
|
|
return SmallString<32>(&N[0], &N[End]);
|
|
|
|
}
|
|
|
|
|
2015-08-25 06:16:48 +08:00
|
|
|
static std::string toSymbol(StringRef S) { return ("$" + S).str(); }
|
|
|
|
|
2015-08-27 06:09:54 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-09-01 06:24:11 +08:00
|
|
|
static const char *toString(const Type *Ty, bool hasAddr64) {
|
|
|
|
switch (Ty->getTypeID()) {
|
|
|
|
default: break;
|
|
|
|
// Treat all pointers as the underlying integer into linear memory.
|
|
|
|
case Type::PointerTyID: return hasAddr64 ? "i64" : "i32";
|
|
|
|
case Type::FloatTyID: return "f32";
|
|
|
|
case Type::DoubleTyID: return "f64";
|
|
|
|
case Type::IntegerTyID:
|
|
|
|
switch (Ty->getIntegerBitWidth()) {
|
|
|
|
case 8: return "i8";
|
|
|
|
case 16: return "i16";
|
|
|
|
case 32: return "i32";
|
|
|
|
case 64: return "i64";
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DEBUG(dbgs() << "Invalid type "; Ty->print(dbgs()); dbgs() << '\n');
|
|
|
|
llvm_unreachable("invalid type");
|
|
|
|
return "<invalid>";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-27 06:09:54 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// 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<UndefValue>(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 "
|
2015-08-27 07:03:07 +08:00
|
|
|
"supported by WebAssembly yet");
|
2015-08-27 06:09:54 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("Unknown linkage type");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-01 06:24:11 +08:00
|
|
|
OS << "(global " << toSymbol(Name) << ' '
|
|
|
|
<< toString(Init->getType(), hasAddr64) << ' ';
|
2015-08-27 06:09:54 +08:00
|
|
|
if (const auto *C = dyn_cast<ConstantInt>(Init)) {
|
|
|
|
assert(C->getBitWidth() <= 64 && "Printing wider types unimplemented");
|
|
|
|
OS << C->getZExtValue();
|
|
|
|
} else if (const auto *C = dyn_cast<ConstantFP>(Init)) {
|
|
|
|
OS << toString(C->getValueAPF());
|
|
|
|
} else {
|
|
|
|
assert(false && "Only integer and floating-point constants are supported");
|
|
|
|
}
|
|
|
|
OS << ") ;; align " << Align << "\n";
|
|
|
|
OutStreamer->EmitRawText(OS.str());
|
|
|
|
}
|
|
|
|
|
2015-08-26 07:19:49 +08:00
|
|
|
void WebAssemblyAsmPrinter::EmitConstantPool() {
|
|
|
|
assert(MF->getConstantPool()->getConstants().empty() &&
|
|
|
|
"WebAssembly disables constant pools");
|
|
|
|
}
|
|
|
|
|
2015-08-26 06:58:05 +08:00
|
|
|
void WebAssemblyAsmPrinter::EmitFunctionEntryLabel() {
|
|
|
|
SmallString<128> Str;
|
|
|
|
raw_svector_ostream OS(Str);
|
|
|
|
|
|
|
|
CurrentFnSym->redefineIfPossible();
|
|
|
|
|
|
|
|
// The function label could have already been emitted if two symbols end up
|
|
|
|
// conflicting due to asm renaming. Detect this and emit an error.
|
|
|
|
if (CurrentFnSym->isVariable())
|
|
|
|
report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
|
|
|
|
"' is a protected alias");
|
|
|
|
if (CurrentFnSym->isDefined())
|
|
|
|
report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
|
|
|
|
"' label emitted multiple times to assembly file");
|
|
|
|
|
|
|
|
OS << "(func " << toSymbol(CurrentFnSym->getName());
|
|
|
|
OutStreamer->EmitRawText(OS.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebAssemblyAsmPrinter::EmitFunctionBodyStart() {
|
|
|
|
SmallString<128> Str;
|
|
|
|
raw_svector_ostream OS(Str);
|
|
|
|
const Function *F = MF->getFunction();
|
|
|
|
for (const Argument &A : F->args())
|
2015-09-01 06:24:11 +08:00
|
|
|
OS << " (param " << toString(A.getType(), hasAddr64) << ')';
|
2015-08-26 06:58:05 +08:00
|
|
|
const Type *Rt = F->getReturnType();
|
|
|
|
if (!Rt->isVoidTy())
|
2015-09-01 06:24:11 +08:00
|
|
|
OS << " (result " << toString(Rt, hasAddr64) << ')';
|
2015-08-26 06:58:05 +08:00
|
|
|
OS << '\n';
|
|
|
|
OutStreamer->EmitRawText(OS.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebAssemblyAsmPrinter::EmitFunctionBodyEnd() {
|
|
|
|
SmallString<128> Str;
|
|
|
|
raw_svector_ostream OS(Str);
|
|
|
|
OS << ") ;; end func " << toSymbol(CurrentFnSym->getName()) << '\n';
|
|
|
|
OutStreamer->EmitRawText(OS.str());
|
|
|
|
}
|
|
|
|
|
2015-07-23 05:28:15 +08:00
|
|
|
void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
|
2015-08-25 06:16:48 +08:00
|
|
|
DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
|
2015-07-23 05:28:15 +08:00
|
|
|
SmallString<128> Str;
|
|
|
|
raw_svector_ostream OS(Str);
|
|
|
|
|
2015-08-01 01:53:38 +08:00
|
|
|
unsigned NumDefs = MI->getDesc().getNumDefs();
|
|
|
|
assert(NumDefs <= 1 &&
|
|
|
|
"Instructions with multiple result values not implemented");
|
|
|
|
|
2015-08-25 05:19:48 +08:00
|
|
|
OS << '\t';
|
|
|
|
|
2015-08-01 01:53:38 +08:00
|
|
|
if (NumDefs != 0) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(0);
|
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
OS << "(setlocal @" << TargetRegisterInfo::virtReg2Index(Reg) << ' ';
|
|
|
|
}
|
|
|
|
|
2015-09-09 08:52:47 +08:00
|
|
|
if (MI->getOpcode() == WebAssembly::COPY) {
|
|
|
|
OS << '@' << TargetRegisterInfo::virtReg2Index(MI->getOperand(1).getReg());
|
|
|
|
} else {
|
|
|
|
OS << '(' << OpcodeName(TII, MI);
|
|
|
|
for (const MachineOperand &MO : MI->uses())
|
|
|
|
switch (MO.getType()) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unexpected machine operand type");
|
|
|
|
case MachineOperand::MO_Register: {
|
|
|
|
if (MO.isImplicit())
|
|
|
|
continue;
|
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
OS << " @" << TargetRegisterInfo::virtReg2Index(Reg);
|
|
|
|
} break;
|
|
|
|
case MachineOperand::MO_Immediate: {
|
|
|
|
OS << ' ' << MO.getImm();
|
|
|
|
} break;
|
|
|
|
case MachineOperand::MO_FPImmediate: {
|
|
|
|
OS << ' ' << toString(MO.getFPImm()->getValueAPF());
|
|
|
|
} break;
|
|
|
|
case MachineOperand::MO_GlobalAddress: {
|
|
|
|
OS << ' ' << toSymbol(MO.getGlobal()->getName());
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
OS << ')';
|
|
|
|
}
|
2015-08-01 01:53:38 +08:00
|
|
|
|
|
|
|
if (NumDefs != 0)
|
|
|
|
OS << ')';
|
|
|
|
|
|
|
|
OS << '\n';
|
|
|
|
|
2015-07-23 05:28:15 +08:00
|
|
|
OutStreamer->EmitRawText(OS.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force static initialization.
|
|
|
|
extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
|
|
|
|
RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32);
|
|
|
|
RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(TheWebAssemblyTarget64);
|
|
|
|
}
|