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"
|
2015-11-13 01:04:33 +08:00
|
|
|
#include "InstPrinter/WebAssemblyInstPrinter.h"
|
|
|
|
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
|
|
|
|
#include "WebAssemblyMCInstLower.h"
|
2015-07-23 05:28:15 +08:00
|
|
|
#include "WebAssemblyMachineFunctionInfo.h"
|
|
|
|
#include "WebAssemblyRegisterInfo.h"
|
|
|
|
#include "WebAssemblySubtarget.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
2015-10-06 08:27:55 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2015-11-11 09:33:02 +08:00
|
|
|
#include "llvm/CodeGen/Analysis.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"
|
2015-11-13 01:04:33 +08:00
|
|
|
#include "llvm/MC/MCContext.h"
|
2015-07-23 05:28:15 +08:00
|
|
|
#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-10-16 08:53:49 +08:00
|
|
|
const MachineRegisterInfo *MRI;
|
2015-11-13 01:04:33 +08:00
|
|
|
const WebAssemblyFunctionInfo *MFI;
|
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-11-13 01:04:33 +08:00
|
|
|
: AsmPrinter(TM, std::move(Streamer)), MRI(nullptr), MFI(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-10-16 08:53:49 +08:00
|
|
|
MRI = &MF.getRegInfo();
|
2015-11-13 01:04:33 +08:00
|
|
|
MFI = MF.getInfo<WebAssemblyFunctionInfo>();
|
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-11-13 09:42:29 +08:00
|
|
|
bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
|
|
|
|
unsigned AsmVariant, const char *ExtraCode,
|
|
|
|
raw_ostream &OS) override;
|
|
|
|
bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
|
|
|
|
unsigned AsmVariant, const char *ExtraCode,
|
|
|
|
raw_ostream &OS) override;
|
2015-09-24 00:59:10 +08:00
|
|
|
|
2015-10-16 08:53:49 +08:00
|
|
|
std::string getRegTypeName(unsigned RegNo) const;
|
2015-11-11 09:33:02 +08:00
|
|
|
const char *toString(MVT VT) const;
|
2015-10-16 08:53:49 +08:00
|
|
|
std::string regToString(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
|
|
|
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 "?";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string WebAssemblyAsmPrinter::regToString(const MachineOperand &MO) {
|
|
|
|
unsigned RegNo = MO.getReg();
|
2015-11-20 11:13:31 +08:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(RegNo) &&
|
|
|
|
"Unlowered physical register encountered during assembly printing");
|
2015-11-19 00:12:01 +08:00
|
|
|
assert(!MFI->isVRegStackified(RegNo));
|
2015-11-13 08:21:05 +08:00
|
|
|
unsigned WAReg = MFI->getWAReg(RegNo);
|
|
|
|
assert(WAReg != WebAssemblyFunctionInfo::UnusedReg);
|
2015-11-19 00:12:01 +08:00
|
|
|
return '$' + utostr(WAReg);
|
2015-10-06 08:27:55 +08:00
|
|
|
}
|
|
|
|
|
2015-11-11 09:33:02 +08:00
|
|
|
const char *WebAssemblyAsmPrinter::toString(MVT VT) const {
|
|
|
|
switch (VT.SimpleTy) {
|
2015-09-24 00:59:10 +08:00
|
|
|
default:
|
|
|
|
break;
|
2015-11-11 09:33:02 +08:00
|
|
|
case MVT::f32:
|
2015-09-24 00:59:10 +08:00
|
|
|
return "f32";
|
2015-11-11 09:33:02 +08:00
|
|
|
case MVT::f64:
|
2015-09-24 00:59:10 +08:00
|
|
|
return "f64";
|
2015-11-11 09:33:02 +08:00
|
|
|
case MVT::i32:
|
|
|
|
return "i32";
|
|
|
|
case MVT::i64:
|
|
|
|
return "i64";
|
2015-09-01 06:24:11 +08:00
|
|
|
}
|
2015-11-11 09:33:02 +08:00
|
|
|
DEBUG(dbgs() << "Invalid type " << EVT(VT).getEVTString() << '\n');
|
2015-09-01 06:24:11 +08:00
|
|
|
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-11-17 05:12:41 +08:00
|
|
|
static void ComputeLegalValueVTs(const Function &F,
|
|
|
|
const TargetMachine &TM,
|
|
|
|
Type *Ty,
|
|
|
|
SmallVectorImpl<MVT> &ValueVTs) {
|
|
|
|
const DataLayout& DL(F.getParent()->getDataLayout());
|
|
|
|
const WebAssemblyTargetLowering &TLI =
|
|
|
|
*TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering();
|
|
|
|
SmallVector<EVT, 4> VTs;
|
|
|
|
ComputeValueVTs(TLI, DL, Ty, VTs);
|
|
|
|
|
|
|
|
for (EVT VT : VTs) {
|
|
|
|
unsigned NumRegs = TLI.getNumRegisters(F.getContext(), VT);
|
|
|
|
MVT RegisterVT = TLI.getRegisterType(F.getContext(), VT);
|
|
|
|
for (unsigned i = 0; i != NumRegs; ++i)
|
|
|
|
ValueVTs.push_back(RegisterVT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-26 06:58:05 +08:00
|
|
|
void WebAssemblyAsmPrinter::EmitFunctionBodyStart() {
|
2015-10-16 08:53:49 +08:00
|
|
|
SmallString<128> Str;
|
|
|
|
raw_svector_ostream OS(Str);
|
2015-10-06 08:27:55 +08:00
|
|
|
|
2015-11-13 01:04:33 +08:00
|
|
|
for (MVT VT : MFI->getParams())
|
|
|
|
OS << "\t" ".param " << toString(VT) << '\n';
|
2015-11-17 05:12:41 +08:00
|
|
|
|
|
|
|
SmallVector<MVT, 4> ResultVTs;
|
|
|
|
const Function &F(*MF->getFunction());
|
|
|
|
ComputeLegalValueVTs(F, TM, F.getReturnType(), ResultVTs);
|
|
|
|
// If the return type needs to be legalized it will get converted into
|
|
|
|
// passing a pointer.
|
|
|
|
if (ResultVTs.size() == 1)
|
|
|
|
OS << "\t" ".result " << toString(ResultVTs.front()) << '\n';
|
2015-08-26 06:58:05 +08:00
|
|
|
|
2015-11-19 00:12:01 +08:00
|
|
|
bool FirstWAReg = true;
|
2015-10-16 08:53:49 +08:00
|
|
|
for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) {
|
|
|
|
unsigned VReg = TargetRegisterInfo::index2VirtReg(Idx);
|
2015-11-19 00:12:01 +08:00
|
|
|
unsigned WAReg = MFI->getWAReg(VReg);
|
|
|
|
// Don't declare unused registers.
|
|
|
|
if (WAReg == WebAssemblyFunctionInfo::UnusedReg)
|
|
|
|
continue;
|
|
|
|
// Don't redeclare parameters.
|
|
|
|
if (WAReg < MFI->getParams().size())
|
|
|
|
continue;
|
|
|
|
// Don't declare stackified registers.
|
|
|
|
if (int(WAReg) < 0)
|
|
|
|
continue;
|
|
|
|
if (FirstWAReg)
|
|
|
|
OS << "\t" ".local ";
|
|
|
|
else
|
|
|
|
OS << ", ";
|
|
|
|
OS << getRegTypeName(VReg);
|
|
|
|
FirstWAReg = false;
|
2015-10-16 08:53:49 +08:00
|
|
|
}
|
2015-11-19 00:12:01 +08:00
|
|
|
if (!FirstWAReg)
|
2015-11-11 09:33:02 +08:00
|
|
|
OS << '\n';
|
2015-10-16 08:53:49 +08:00
|
|
|
|
2015-11-11 09:33:02 +08:00
|
|
|
// EmitRawText appends a newline, so strip off the last newline.
|
|
|
|
StringRef Text = OS.str();
|
|
|
|
if (!Text.empty())
|
|
|
|
OutStreamer->EmitRawText(Text.substr(0, Text.size() - 1));
|
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
|
|
|
|
2015-10-06 08:27:55 +08:00
|
|
|
switch (MI->getOpcode()) {
|
|
|
|
case WebAssembly::ARGUMENT_I32:
|
|
|
|
case WebAssembly::ARGUMENT_I64:
|
|
|
|
case WebAssembly::ARGUMENT_F32:
|
|
|
|
case WebAssembly::ARGUMENT_F64:
|
2015-11-13 01:04:33 +08:00
|
|
|
// These represent values which are live into the function entry, so there's
|
|
|
|
// no instruction to emit.
|
2015-10-06 08:27:55 +08:00
|
|
|
break;
|
|
|
|
default: {
|
2015-11-13 01:04:33 +08:00
|
|
|
WebAssemblyMCInstLower MCInstLowering(OutContext, *this);
|
|
|
|
MCInst TmpInst;
|
|
|
|
MCInstLowering.Lower(MI, TmpInst);
|
|
|
|
EmitToStreamer(*OutStreamer, TmpInst);
|
2015-10-06 08:27:55 +08:00
|
|
|
break;
|
|
|
|
}
|
2015-09-09 08:52:47 +08:00
|
|
|
}
|
2015-07-23 05:28:15 +08:00
|
|
|
}
|
|
|
|
|
2015-10-21 10:23:09 +08:00
|
|
|
void WebAssemblyAsmPrinter::EmitEndOfAsmFile(Module &M) {
|
2015-11-11 09:33:02 +08:00
|
|
|
const DataLayout &DL = M.getDataLayout();
|
|
|
|
|
2015-10-31 00:41:21 +08:00
|
|
|
SmallString<128> Str;
|
|
|
|
raw_svector_ostream OS(Str);
|
2015-10-21 10:23:09 +08:00
|
|
|
for (const Function &F : M)
|
|
|
|
if (F.isDeclarationForLinker()) {
|
|
|
|
assert(F.hasName() && "imported functions must have a name");
|
2015-11-11 09:33:02 +08:00
|
|
|
if (F.isIntrinsic())
|
2015-10-31 00:41:21 +08:00
|
|
|
continue;
|
2015-10-21 10:23:09 +08:00
|
|
|
if (Str.empty())
|
2015-10-31 00:41:21 +08:00
|
|
|
OS << "\t.imports\n";
|
2015-11-11 09:33:02 +08:00
|
|
|
|
2015-11-13 01:04:33 +08:00
|
|
|
MCSymbol *Sym = OutStreamer->getContext().getOrCreateSymbol(F.getName());
|
|
|
|
OS << "\t.import " << *Sym << " \"\" " << *Sym;
|
2015-11-11 09:33:02 +08:00
|
|
|
|
|
|
|
const WebAssemblyTargetLowering &TLI =
|
|
|
|
*TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering();
|
|
|
|
|
|
|
|
// If we need to legalize the return type, it'll get converted into
|
|
|
|
// passing a pointer.
|
|
|
|
bool SawParam = false;
|
|
|
|
SmallVector<MVT, 4> ResultVTs;
|
2015-11-17 05:12:41 +08:00
|
|
|
ComputeLegalValueVTs(F, TM, F.getReturnType(), ResultVTs);
|
2015-11-11 09:33:02 +08:00
|
|
|
if (ResultVTs.size() > 1) {
|
|
|
|
ResultVTs.clear();
|
|
|
|
OS << " (param " << toString(TLI.getPointerTy(DL));
|
|
|
|
SawParam = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const Argument &A : F.args()) {
|
|
|
|
SmallVector<MVT, 4> ParamVTs;
|
2015-11-17 05:12:41 +08:00
|
|
|
ComputeLegalValueVTs(F, TM, A.getType(), ParamVTs);
|
|
|
|
for (MVT VT : ParamVTs) {
|
2015-11-11 09:33:02 +08:00
|
|
|
if (!SawParam) {
|
|
|
|
OS << " (param";
|
|
|
|
SawParam = true;
|
|
|
|
}
|
2015-11-17 05:12:41 +08:00
|
|
|
OS << ' ' << toString(VT);
|
2015-11-11 09:33:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (SawParam)
|
|
|
|
OS << ')';
|
|
|
|
|
2015-11-17 05:12:41 +08:00
|
|
|
for (MVT VT : ResultVTs)
|
|
|
|
OS << " (result " << toString(VT) << ')';
|
2015-11-11 09:33:02 +08:00
|
|
|
|
2015-10-21 10:23:09 +08:00
|
|
|
OS << '\n';
|
2015-10-31 00:41:21 +08:00
|
|
|
}
|
2015-11-11 09:33:02 +08:00
|
|
|
|
|
|
|
StringRef Text = OS.str();
|
|
|
|
if (!Text.empty())
|
|
|
|
OutStreamer->EmitRawText(Text.substr(0, Text.size() - 1));
|
2015-10-21 10:23:09 +08:00
|
|
|
}
|
|
|
|
|
2015-11-13 09:42:29 +08:00
|
|
|
bool WebAssemblyAsmPrinter::PrintAsmOperand(const MachineInstr *MI,
|
|
|
|
unsigned OpNo, unsigned AsmVariant,
|
|
|
|
const char *ExtraCode,
|
|
|
|
raw_ostream &OS) {
|
|
|
|
if (AsmVariant != 0)
|
|
|
|
report_fatal_error("There are no defined alternate asm variants");
|
|
|
|
|
|
|
|
if (!ExtraCode) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(OpNo);
|
|
|
|
if (MO.isImm())
|
|
|
|
OS << MO.getImm();
|
|
|
|
else
|
|
|
|
OS << regToString(MO);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebAssemblyAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
|
|
|
|
unsigned OpNo,
|
|
|
|
unsigned AsmVariant,
|
|
|
|
const char *ExtraCode,
|
|
|
|
raw_ostream &OS) {
|
|
|
|
if (AsmVariant != 0)
|
|
|
|
report_fatal_error("There are no defined alternate asm variants");
|
|
|
|
|
|
|
|
if (!ExtraCode) {
|
|
|
|
OS << regToString(MI->getOperand(OpNo));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
|
|
|
|
}
|
|
|
|
|
2015-07-23 05:28:15 +08:00
|
|
|
// Force static initialization.
|
|
|
|
extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
|
|
|
|
RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32);
|
|
|
|
RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(TheWebAssemblyTarget64);
|
|
|
|
}
|