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"
|
2015-10-06 08:27:55 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2015-07-23 05:28:15 +08:00
|
|
|
#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-08-01 01:53:38 +08:00
|
|
|
const WebAssemblyInstrInfo *TII;
|
2015-10-16 08:53:49 +08:00
|
|
|
const MachineRegisterInfo *MRI;
|
2015-10-06 08:27:55 +08:00
|
|
|
unsigned NumArgs;
|
2015-08-01 01:53:38 +08:00
|
|
|
|
2015-07-23 05:28:15 +08:00
|
|
|
public:
|
|
|
|
WebAssemblyAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
|
2015-10-16 08:53:49 +08:00
|
|
|
: AsmPrinter(TM, std::move(Streamer)), TII(nullptr), MRI(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>();
|
|
|
|
TII = Subtarget.getInstrInfo();
|
2015-10-16 08:53:49 +08:00
|
|
|
MRI = &MF.getRegInfo();
|
2015-10-06 08:27:55 +08:00
|
|
|
NumArgs = MF.getInfo<WebAssemblyFunctionInfo>()->getNumArguments();
|
2015-08-01 01:53:38 +08:00
|
|
|
return AsmPrinter::runOnMachineFunction(MF);
|
2015-07-23 05:28:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===------------------------------------------------------------------===//
|
|
|
|
// AsmPrinter Implementation.
|
|
|
|
//===------------------------------------------------------------------===//
|
|
|
|
|
2015-09-17 00:51:30 +08:00
|
|
|
void EmitJumpTableInfo() override;
|
2015-08-26 07:19:49 +08:00
|
|
|
void EmitConstantPool() override;
|
2015-08-26 06:58:05 +08:00
|
|
|
void EmitFunctionBodyStart() override;
|
2015-07-23 05:28:15 +08:00
|
|
|
void EmitInstruction(const MachineInstr *MI) override;
|
2015-10-21 10:23:09 +08:00
|
|
|
void EmitEndOfAsmFile(Module &M) override;
|
2015-09-24 00:59:10 +08:00
|
|
|
|
2015-10-16 08:53:49 +08:00
|
|
|
std::string getRegTypeName(unsigned RegNo) const;
|
2015-09-24 00:59:10 +08:00
|
|
|
static std::string toString(const APFloat &APF);
|
|
|
|
const char *toString(Type *Ty) const;
|
2015-10-16 08:53:49 +08:00
|
|
|
std::string regToString(const MachineOperand &MO);
|
|
|
|
std::string argToString(const MachineOperand &MO);
|
2015-07-23 05:28:15 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2015-08-27 06:09:54 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Helpers.
|
2015-07-23 05:28:15 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-10-16 08:53:49 +08:00
|
|
|
// Operand type (if any), followed by the 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
|
2015-10-29 12:10:52 +08:00
|
|
|
// underscore). Conversions are additionally prefixed with their input type
|
|
|
|
// (before a double underscore).
|
2015-10-16 08:53:49 +08:00
|
|
|
static std::string 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-10-16 08:53:49 +08:00
|
|
|
std::string::size_type Len = N.length();
|
|
|
|
std::string::size_type Under = N.rfind('_');
|
|
|
|
bool HasType = std::string::npos != Under;
|
|
|
|
std::string::size_type NameEnd = HasType ? Under : Len;
|
|
|
|
std::string Name(&N[0], &N[NameEnd]);
|
2015-10-20 09:26:54 +08:00
|
|
|
if (!HasType)
|
|
|
|
return Name;
|
2015-10-22 10:32:50 +08:00
|
|
|
for (const char *typelessOpcode : { "return", "call", "br_if" })
|
2015-10-20 09:26:54 +08:00
|
|
|
if (Name == typelessOpcode)
|
|
|
|
return Name;
|
2015-10-29 12:10:52 +08:00
|
|
|
std::string Type(&N[NameEnd + 1], &N[Len]);
|
|
|
|
std::string::size_type DoubleUnder = Name.find("__");
|
|
|
|
bool IsConv = std::string::npos != DoubleUnder;
|
|
|
|
if (!IsConv)
|
|
|
|
return Type + '.' + Name;
|
|
|
|
std::string InType(&Name[0], &Name[DoubleUnder]);
|
|
|
|
return Type + '.' + std::string(&Name[DoubleUnder + 2], &Name[NameEnd]) +
|
|
|
|
'/' + InType;
|
2015-08-07 09:57:03 +08:00
|
|
|
}
|
|
|
|
|
2015-08-25 06:16:48 +08:00
|
|
|
static std::string toSymbol(StringRef S) { return ("$" + S).str(); }
|
|
|
|
|
2015-10-16 08:53:49 +08:00
|
|
|
std::string WebAssemblyAsmPrinter::getRegTypeName(unsigned RegNo) const {
|
|
|
|
const TargetRegisterClass *TRC = MRI->getRegClass(RegNo);
|
|
|
|
for (MVT T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64})
|
|
|
|
if (TRC->hasType(T))
|
|
|
|
return EVT(T).getEVTString();
|
|
|
|
DEBUG(errs() << "Unknown type for register number: " << RegNo);
|
|
|
|
llvm_unreachable("Unknown register type");
|
|
|
|
return "?";
|
|
|
|
}
|
|
|
|
|
2015-09-24 00:59:10 +08:00
|
|
|
std::string WebAssemblyAsmPrinter::toString(const APFloat &FP) {
|
2015-08-27 06:09:54 +08:00
|
|
|
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-10-16 08:53:49 +08:00
|
|
|
std::string WebAssemblyAsmPrinter::regToString(const MachineOperand &MO) {
|
|
|
|
unsigned RegNo = MO.getReg();
|
2015-10-06 08:27:55 +08:00
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(RegNo))
|
|
|
|
return WebAssemblyInstPrinter::getRegisterName(RegNo);
|
|
|
|
|
|
|
|
// WebAssembly arguments and local variables are in the same index space, and
|
|
|
|
// there are no explicit varargs, so we just add the number of arguments to
|
|
|
|
// the virtual register number to get the local variable number.
|
2015-10-16 08:53:49 +08:00
|
|
|
return utostr(TargetRegisterInfo::virtReg2Index(RegNo) + NumArgs);
|
2015-10-06 08:27:55 +08:00
|
|
|
}
|
|
|
|
|
2015-10-16 08:53:49 +08:00
|
|
|
std::string WebAssemblyAsmPrinter::argToString(const MachineOperand &MO) {
|
|
|
|
unsigned ArgNo = MO.getImm();
|
2015-10-06 08:27:55 +08:00
|
|
|
// Same as above, but we don't need to add NumArgs here.
|
2015-10-16 08:53:49 +08:00
|
|
|
return utostr(ArgNo);
|
2015-10-06 08:27:55 +08:00
|
|
|
}
|
|
|
|
|
2015-09-24 00:59:10 +08:00
|
|
|
const char *WebAssemblyAsmPrinter::toString(Type *Ty) const {
|
2015-09-01 06:24:11 +08:00
|
|
|
switch (Ty->getTypeID()) {
|
2015-09-24 00:59:10 +08:00
|
|
|
default:
|
|
|
|
break;
|
2015-09-01 06:24:11 +08:00
|
|
|
// Treat all pointers as the underlying integer into linear memory.
|
2015-09-24 00:59:10 +08:00
|
|
|
case Type::PointerTyID:
|
|
|
|
switch (getPointerSize()) {
|
|
|
|
case 4:
|
|
|
|
return "i32";
|
|
|
|
case 8:
|
|
|
|
return "i64";
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unsupported pointer size");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Type::FloatTyID:
|
|
|
|
return "f32";
|
|
|
|
case Type::DoubleTyID:
|
|
|
|
return "f64";
|
2015-09-01 06:24:11 +08:00
|
|
|
case Type::IntegerTyID:
|
|
|
|
switch (Ty->getIntegerBitWidth()) {
|
2015-09-24 00:59:10 +08:00
|
|
|
case 8:
|
|
|
|
return "i8";
|
|
|
|
case 16:
|
|
|
|
return "i16";
|
|
|
|
case 32:
|
|
|
|
return "i32";
|
|
|
|
case 64:
|
|
|
|
return "i64";
|
|
|
|
default:
|
|
|
|
break;
|
2015-09-01 06:24:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-08-26 07:19:49 +08:00
|
|
|
void WebAssemblyAsmPrinter::EmitConstantPool() {
|
|
|
|
assert(MF->getConstantPool()->getConstants().empty() &&
|
|
|
|
"WebAssembly disables constant pools");
|
|
|
|
}
|
|
|
|
|
2015-09-17 00:51:30 +08:00
|
|
|
void WebAssemblyAsmPrinter::EmitJumpTableInfo() {
|
|
|
|
// Nothing to do; jump tables are incorporated into the instruction stream.
|
|
|
|
}
|
|
|
|
|
2015-08-26 06:58:05 +08:00
|
|
|
void WebAssemblyAsmPrinter::EmitFunctionBodyStart() {
|
|
|
|
const Function *F = MF->getFunction();
|
2015-09-24 00:59:10 +08:00
|
|
|
Type *Rt = F->getReturnType();
|
2015-10-16 08:53:49 +08:00
|
|
|
SmallString<128> Str;
|
|
|
|
raw_svector_ostream OS(Str);
|
|
|
|
bool First = true;
|
2015-10-06 08:27:55 +08:00
|
|
|
|
2015-09-09 23:13:36 +08:00
|
|
|
if (!Rt->isVoidTy() || !F->arg_empty()) {
|
2015-10-06 08:27:55 +08:00
|
|
|
for (const Argument &A : F->args()) {
|
2015-10-16 08:53:49 +08:00
|
|
|
OS << (First ? "" : "\n") << "\t.param " << toString(A.getType());
|
2015-10-06 08:27:55 +08:00
|
|
|
First = false;
|
|
|
|
}
|
|
|
|
if (!Rt->isVoidTy()) {
|
2015-10-16 08:53:49 +08:00
|
|
|
OS << (First ? "" : "\n") << "\t.result " << toString(Rt);
|
2015-10-06 08:27:55 +08:00
|
|
|
First = false;
|
|
|
|
}
|
2015-09-09 23:13:36 +08:00
|
|
|
}
|
2015-08-26 06:58:05 +08:00
|
|
|
|
2015-10-16 08:53:49 +08:00
|
|
|
bool FirstVReg = true;
|
|
|
|
for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) {
|
|
|
|
unsigned VReg = TargetRegisterInfo::index2VirtReg(Idx);
|
2015-10-17 08:25:38 +08:00
|
|
|
// FIXME: Don't skip dead virtual registers for now: that would require
|
|
|
|
// remapping all locals' numbers.
|
|
|
|
//if (!MRI->use_empty(VReg)) {
|
2015-10-16 08:53:49 +08:00
|
|
|
if (FirstVReg) {
|
|
|
|
OS << (First ? "" : "\n") << "\t.local ";
|
|
|
|
First = false;
|
|
|
|
}
|
|
|
|
OS << (FirstVReg ? "" : ", ") << getRegTypeName(VReg);
|
|
|
|
FirstVReg = false;
|
2015-10-17 08:25:38 +08:00
|
|
|
//}
|
2015-10-16 08:53:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!First)
|
|
|
|
OutStreamer->EmitRawText(OS.str());
|
2015-10-06 08:27:55 +08:00
|
|
|
AsmPrinter::EmitFunctionBodyStart();
|
2015-08-26 06:58:05 +08:00
|
|
|
}
|
|
|
|
|
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-10-06 08:27:55 +08:00
|
|
|
switch (MI->getOpcode()) {
|
|
|
|
case TargetOpcode::COPY:
|
2015-10-16 08:53:49 +08:00
|
|
|
OS << "get_local " << regToString(MI->getOperand(1));
|
2015-10-06 08:27:55 +08:00
|
|
|
break;
|
|
|
|
case WebAssembly::GLOBAL:
|
|
|
|
// TODO: wasm64
|
|
|
|
OS << "i32.const " << toSymbol(MI->getOperand(1).getGlobal()->getName());
|
|
|
|
break;
|
|
|
|
case WebAssembly::ARGUMENT_I32:
|
|
|
|
case WebAssembly::ARGUMENT_I64:
|
|
|
|
case WebAssembly::ARGUMENT_F32:
|
|
|
|
case WebAssembly::ARGUMENT_F64:
|
2015-10-16 08:53:49 +08:00
|
|
|
OS << "get_local " << argToString(MI->getOperand(1));
|
2015-10-06 08:27:55 +08:00
|
|
|
break;
|
|
|
|
case WebAssembly::Immediate_I32:
|
|
|
|
OS << "i32.const " << MI->getOperand(1).getImm();
|
|
|
|
break;
|
|
|
|
case WebAssembly::Immediate_I64:
|
|
|
|
OS << "i64.const " << MI->getOperand(1).getImm();
|
|
|
|
break;
|
|
|
|
case WebAssembly::Immediate_F32:
|
|
|
|
OS << "f32.const " << toString(MI->getOperand(1).getFPImm()->getValueAPF());
|
|
|
|
break;
|
|
|
|
case WebAssembly::Immediate_F64:
|
|
|
|
OS << "f64.const " << toString(MI->getOperand(1).getFPImm()->getValueAPF());
|
|
|
|
break;
|
|
|
|
default: {
|
|
|
|
OS << OpcodeName(TII, MI);
|
|
|
|
bool NeedComma = false;
|
|
|
|
for (const MachineOperand &MO : MI->uses()) {
|
|
|
|
if (MO.isReg() && MO.isImplicit())
|
|
|
|
continue;
|
|
|
|
if (NeedComma)
|
|
|
|
OS << ',';
|
|
|
|
NeedComma = true;
|
|
|
|
OS << ' ';
|
2015-09-09 08:52:47 +08:00
|
|
|
switch (MO.getType()) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unexpected machine operand type");
|
2015-10-06 08:27:55 +08:00
|
|
|
case MachineOperand::MO_Register:
|
2015-10-16 08:53:49 +08:00
|
|
|
OS << "(get_local " << regToString(MO) << ')';
|
2015-10-06 08:27:55 +08:00
|
|
|
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;
|
|
|
|
case MachineOperand::MO_MachineBasicBlock:
|
|
|
|
OS << toSymbol(MO.getMBB()->getSymbol()->getName());
|
|
|
|
break;
|
2015-09-09 08:52:47 +08:00
|
|
|
}
|
2015-10-06 08:27:55 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2015-09-09 08:52:47 +08:00
|
|
|
}
|
2015-08-01 01:53:38 +08:00
|
|
|
|
2015-07-23 05:28:15 +08:00
|
|
|
OutStreamer->EmitRawText(OS.str());
|
2015-10-06 08:27:55 +08:00
|
|
|
|
|
|
|
if (NumDefs != 0) {
|
|
|
|
SmallString<128> Str;
|
|
|
|
raw_svector_ostream OS(Str);
|
2015-10-16 08:53:49 +08:00
|
|
|
const MachineOperand &Operand = MI->getOperand(0);
|
|
|
|
OS << "\tset_local " << regToString(Operand) << ", pop";
|
2015-10-06 08:27:55 +08:00
|
|
|
OutStreamer->EmitRawText(OS.str());
|
|
|
|
}
|
2015-07-23 05:28:15 +08:00
|
|
|
}
|
|
|
|
|
2015-10-21 10:23:09 +08:00
|
|
|
void WebAssemblyAsmPrinter::EmitEndOfAsmFile(Module &M) {
|
|
|
|
SmallString<128> Str;
|
|
|
|
raw_svector_ostream OS(Str);
|
|
|
|
for (const Function &F : M)
|
|
|
|
if (F.isDeclarationForLinker()) {
|
|
|
|
assert(F.hasName() && "imported functions must have a name");
|
|
|
|
if (F.getName().startswith("llvm."))
|
|
|
|
continue;
|
|
|
|
if (Str.empty())
|
|
|
|
OS << "\t.imports\n";
|
|
|
|
Type *Rt = F.getReturnType();
|
|
|
|
OS << "\t.import " << toSymbol(F.getName()) << " \"\" \"" << F.getName()
|
|
|
|
<< "\"";
|
|
|
|
for (const Argument &A : F.args())
|
|
|
|
OS << " (param " << toString(A.getType()) << ')';
|
|
|
|
if (!Rt->isVoidTy())
|
|
|
|
OS << " (result " << toString(Rt) << ')';
|
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
OutStreamer->EmitRawText(OS.str());
|
|
|
|
}
|
|
|
|
|
2015-07-23 05:28:15 +08:00
|
|
|
// Force static initialization.
|
|
|
|
extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
|
|
|
|
RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32);
|
|
|
|
RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(TheWebAssemblyTarget64);
|
|
|
|
}
|