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
|
2018-05-01 23:54:18 +08:00
|
|
|
/// This file contains a printer that converts from our internal
|
2015-07-23 05:28:15 +08:00
|
|
|
/// representation of machine-dependent LLVM code to the WebAssembly assembly
|
|
|
|
/// language.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-02-25 07:18:00 +08:00
|
|
|
#include "WebAssemblyAsmPrinter.h"
|
2015-11-13 01:04:33 +08:00
|
|
|
#include "InstPrinter/WebAssemblyInstPrinter.h"
|
|
|
|
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
|
2016-01-13 04:30:51 +08:00
|
|
|
#include "MCTargetDesc/WebAssemblyTargetStreamer.h"
|
2016-08-02 06:25:02 +08:00
|
|
|
#include "WebAssembly.h"
|
2015-11-13 01:04:33 +08:00
|
|
|
#include "WebAssemblyMCInstLower.h"
|
2015-07-23 05:28:15 +08:00
|
|
|
#include "WebAssemblyMachineFunctionInfo.h"
|
|
|
|
#include "WebAssemblyRegisterInfo.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"
|
2017-02-25 07:46:05 +08:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfoImpls.h"
|
2015-07-23 05:28:15 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2017-02-25 07:18:00 +08:00
|
|
|
#include "llvm/IR/GlobalVariable.h"
|
2015-11-13 01:04:33 +08:00
|
|
|
#include "llvm/MC/MCContext.h"
|
2018-04-06 01:01:39 +08:00
|
|
|
#include "llvm/MC/MCSectionWasm.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"
|
2018-04-06 01:01:39 +08:00
|
|
|
#include "llvm/MC/MCSymbolWasm.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"
|
|
|
|
|
2015-08-27 06:09:54 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Helpers.
|
2015-07-23 05:28:15 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-11-24 00:50:18 +08:00
|
|
|
MVT WebAssemblyAsmPrinter::getRegType(unsigned RegNo) const {
|
2017-04-25 03:51:12 +08:00
|
|
|
const TargetRegisterInfo *TRI = Subtarget->getRegisterInfo();
|
2016-05-10 12:24:02 +08:00
|
|
|
const TargetRegisterClass *TRC = MRI->getRegClass(RegNo);
|
2016-08-03 07:16:09 +08:00
|
|
|
for (MVT T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64, MVT::v16i8, MVT::v8i16,
|
2018-08-08 05:24:01 +08:00
|
|
|
MVT::v4i32, MVT::v2i64, MVT::v4f32, MVT::v2f64})
|
2017-04-25 03:51:12 +08:00
|
|
|
if (TRI->isTypeLegalForClass(*TRC, T))
|
2015-11-24 00:50:18 +08:00
|
|
|
return T;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(errs() << "Unknown type for register number: " << RegNo);
|
2015-10-16 08:53:49 +08:00
|
|
|
llvm_unreachable("Unknown register type");
|
2015-11-24 00:50:18 +08:00
|
|
|
return MVT::Other;
|
2015-10-16 08:53:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-01-25 23:12:05 +08:00
|
|
|
WebAssemblyTargetStreamer *WebAssemblyAsmPrinter::getTargetStreamer() {
|
2016-01-13 04:30:51 +08:00
|
|
|
MCTargetStreamer *TS = OutStreamer->getTargetStreamer();
|
|
|
|
return static_cast<WebAssemblyTargetStreamer *>(TS);
|
2015-09-01 06:24:11 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 06:09:54 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// WebAssemblyAsmPrinter Implementation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2015-11-17 05:12:41 +08:00
|
|
|
|
2016-06-04 02:34:36 +08:00
|
|
|
void WebAssemblyAsmPrinter::EmitEndOfAsmFile(Module &M) {
|
|
|
|
for (const auto &F : M) {
|
|
|
|
// Emit function type info for all undefined functions
|
|
|
|
if (F.isDeclarationForLinker() && !F.isIntrinsic()) {
|
2016-10-07 06:29:32 +08:00
|
|
|
SmallVector<MVT, 4> Results;
|
|
|
|
SmallVector<MVT, 4> Params;
|
|
|
|
ComputeSignatureVTs(F, TM, Params, Results);
|
2018-02-13 05:41:12 +08:00
|
|
|
MCSymbol *Sym = getSymbol(&F);
|
2018-02-10 07:13:22 +08:00
|
|
|
getTargetStreamer()->emitIndirectFunctionType(Sym, Params, Results);
|
|
|
|
|
2018-02-13 05:41:12 +08:00
|
|
|
if (TM.getTargetTriple().isOSBinFormatWasm() &&
|
|
|
|
F.hasFnAttribute("wasm-import-module")) {
|
|
|
|
MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Sym);
|
2018-02-10 07:13:22 +08:00
|
|
|
StringRef Name = F.getFnAttribute("wasm-import-module")
|
|
|
|
.getValueAsString();
|
2018-02-13 05:41:12 +08:00
|
|
|
getTargetStreamer()->emitImportModule(WasmSym, Name);
|
2018-02-10 07:13:22 +08:00
|
|
|
}
|
2016-06-04 02:34:36 +08:00
|
|
|
}
|
|
|
|
}
|
2016-12-01 08:11:15 +08:00
|
|
|
for (const auto &G : M.globals()) {
|
|
|
|
if (!G.hasInitializer() && G.hasExternalLinkage()) {
|
2017-12-07 08:14:30 +08:00
|
|
|
if (G.getValueType()->isSized()) {
|
|
|
|
uint16_t Size = M.getDataLayout().getTypeAllocSize(G.getValueType());
|
|
|
|
OutStreamer->emitELFSize(getSymbol(&G),
|
|
|
|
MCConstantExpr::create(Size, OutContext));
|
|
|
|
}
|
2016-12-01 08:11:15 +08:00
|
|
|
}
|
|
|
|
}
|
2018-04-06 01:01:39 +08:00
|
|
|
|
|
|
|
if (const NamedMDNode *Named = M.getNamedMetadata("wasm.custom_sections")) {
|
|
|
|
for (const Metadata *MD : Named->operands()) {
|
|
|
|
const MDTuple *Tuple = dyn_cast<MDTuple>(MD);
|
|
|
|
if (!Tuple || Tuple->getNumOperands() != 2)
|
|
|
|
continue;
|
|
|
|
const MDString *Name = dyn_cast<MDString>(Tuple->getOperand(0));
|
|
|
|
const MDString *Contents = dyn_cast<MDString>(Tuple->getOperand(1));
|
|
|
|
if (!Name || !Contents)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
OutStreamer->PushSection();
|
|
|
|
std::string SectionName = (".custom_section." + Name->getString()).str();
|
|
|
|
MCSectionWasm *mySection =
|
|
|
|
OutContext.getWasmSection(SectionName, SectionKind::getMetadata());
|
|
|
|
OutStreamer->SwitchSection(mySection);
|
|
|
|
OutStreamer->EmitBytes(Contents->getString());
|
|
|
|
OutStreamer->PopSection();
|
|
|
|
}
|
|
|
|
}
|
2016-06-04 02:34:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebAssemblyAsmPrinter::EmitConstantPool() {
|
|
|
|
assert(MF->getConstantPool()->getConstants().empty() &&
|
|
|
|
"WebAssembly disables constant pools");
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2017-02-25 07:18:00 +08:00
|
|
|
getTargetStreamer()->emitParam(CurrentFnSym, MFI->getParams());
|
2015-10-06 08:27:55 +08:00
|
|
|
|
2015-11-17 05:12:41 +08:00
|
|
|
SmallVector<MVT, 4> ResultVTs;
|
2017-12-16 07:52:06 +08:00
|
|
|
const Function &F = MF->getFunction();
|
2016-08-02 06:25:02 +08:00
|
|
|
|
|
|
|
// Emit the function index.
|
|
|
|
if (MDNode *Idx = F.getMetadata("wasm.index")) {
|
|
|
|
assert(Idx->getNumOperands() == 1);
|
|
|
|
|
|
|
|
getTargetStreamer()->emitIndIdx(AsmPrinter::lowerConstant(
|
|
|
|
cast<ConstantAsMetadata>(Idx->getOperand(0))->getValue()));
|
|
|
|
}
|
|
|
|
|
2015-11-17 05:12:41 +08:00
|
|
|
ComputeLegalValueVTs(F, TM, F.getReturnType(), ResultVTs);
|
2016-01-13 04:30:51 +08:00
|
|
|
|
2015-11-17 05:12:41 +08:00
|
|
|
// If the return type needs to be legalized it will get converted into
|
|
|
|
// passing a pointer.
|
2016-01-13 04:30:51 +08:00
|
|
|
if (ResultVTs.size() == 1)
|
2017-02-25 07:18:00 +08:00
|
|
|
getTargetStreamer()->emitResult(CurrentFnSym, ResultVTs);
|
|
|
|
else
|
|
|
|
getTargetStreamer()->emitResult(CurrentFnSym, ArrayRef<MVT>());
|
|
|
|
|
2016-10-25 07:27:49 +08:00
|
|
|
getTargetStreamer()->emitLocal(MFI->getLocals());
|
2015-10-16 08:53:49 +08:00
|
|
|
|
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) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_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:
|
2016-08-03 07:16:09 +08:00
|
|
|
case WebAssembly::ARGUMENT_v16i8:
|
|
|
|
case WebAssembly::ARGUMENT_v8i16:
|
|
|
|
case WebAssembly::ARGUMENT_v4i32:
|
2018-08-08 05:24:01 +08:00
|
|
|
case WebAssembly::ARGUMENT_v2i64:
|
2016-08-03 07:16:09 +08:00
|
|
|
case WebAssembly::ARGUMENT_v4f32:
|
2018-08-08 05:24:01 +08:00
|
|
|
case WebAssembly::ARGUMENT_v2f64:
|
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;
|
2016-05-21 08:21:56 +08:00
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_I32:
|
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_I64:
|
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_F32:
|
2016-08-03 07:16:09 +08:00
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_F64:
|
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_v16i8:
|
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_v8i16:
|
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_v4i32:
|
2018-08-08 05:24:01 +08:00
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_v2i64:
|
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_v4f32:
|
2018-08-14 07:12:49 +08:00
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_v2f64: {
|
2016-05-21 08:21:56 +08:00
|
|
|
// These instructions represent the implicit return at the end of a
|
2018-08-14 07:12:49 +08:00
|
|
|
// function body. The operand is always a pop.
|
|
|
|
assert(MFI->isVRegStackified(MI->getOperand(0).getReg()));
|
|
|
|
|
2016-05-21 08:21:56 +08:00
|
|
|
if (isVerbose()) {
|
2018-08-14 07:12:49 +08:00
|
|
|
OutStreamer->AddComment("fallthrough-return: $pop" +
|
|
|
|
Twine(MFI->getWARegStackId(
|
|
|
|
MFI->getWAReg(MI->getOperand(0).getReg()))));
|
2016-05-21 08:21:56 +08:00
|
|
|
OutStreamer->AddBlankLine();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_VOID:
|
|
|
|
// This instruction represents the implicit return at the end of a
|
|
|
|
// function body with no return value.
|
|
|
|
if (isVerbose()) {
|
2018-08-14 07:12:49 +08:00
|
|
|
OutStreamer->AddComment("fallthrough-return");
|
2016-05-21 08:21:56 +08:00
|
|
|
OutStreamer->AddBlankLine();
|
|
|
|
}
|
|
|
|
break;
|
2015-10-06 08:27:55 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-01-12 07:38:05 +08:00
|
|
|
const MCExpr *WebAssemblyAsmPrinter::lowerConstant(const Constant *CV) {
|
|
|
|
if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
|
2017-06-13 09:42:21 +08:00
|
|
|
if (GV->getValueType()->isFunctionTy()) {
|
2016-01-12 07:38:05 +08:00
|
|
|
return MCSymbolRefExpr::create(
|
2017-07-06 04:25:08 +08:00
|
|
|
getSymbol(GV), MCSymbolRefExpr::VK_WebAssembly_FUNCTION, OutContext);
|
2017-06-13 09:42:21 +08:00
|
|
|
}
|
2016-01-12 07:38:05 +08:00
|
|
|
return AsmPrinter::lowerConstant(CV);
|
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
|
2015-12-17 01:15:17 +08:00
|
|
|
// First try the generic code, which knows about modifiers like 'c' and 'n'.
|
|
|
|
if (!AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS))
|
|
|
|
return false;
|
|
|
|
|
2015-11-13 09:42:29 +08:00
|
|
|
if (!ExtraCode) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(OpNo);
|
2015-12-17 01:15:17 +08:00
|
|
|
switch (MO.getType()) {
|
|
|
|
case MachineOperand::MO_Immediate:
|
2015-11-13 09:42:29 +08:00
|
|
|
OS << MO.getImm();
|
2015-12-17 01:15:17 +08:00
|
|
|
return false;
|
|
|
|
case MachineOperand::MO_Register:
|
2015-11-13 09:42:29 +08:00
|
|
|
OS << regToString(MO);
|
2015-12-17 01:15:17 +08:00
|
|
|
return false;
|
|
|
|
case MachineOperand::MO_GlobalAddress:
|
|
|
|
getSymbol(MO.getGlobal())->print(OS, MAI);
|
|
|
|
printOffset(MO.getOffset(), OS);
|
|
|
|
return false;
|
|
|
|
case MachineOperand::MO_ExternalSymbol:
|
|
|
|
GetExternalSymbolSymbol(MO.getSymbolName())->print(OS, MAI);
|
|
|
|
printOffset(MO.getOffset(), OS);
|
|
|
|
return false;
|
|
|
|
case MachineOperand::MO_MachineBasicBlock:
|
|
|
|
MO.getMBB()->getSymbol()->print(OS, MAI);
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2015-11-13 09:42:29 +08:00
|
|
|
}
|
|
|
|
|
2015-12-17 01:15:17 +08:00
|
|
|
return true;
|
2015-11-13 09:42:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
2017-11-09 03:18:08 +08:00
|
|
|
// The current approach to inline asm is that "r" constraints are expressed
|
|
|
|
// as local indices, rather than values on the operand stack. This simplifies
|
|
|
|
// using "r" as it eliminates the need to push and pop the values in a
|
|
|
|
// particular order, however it also makes it impossible to have an "m"
|
|
|
|
// constraint. So we don't support it.
|
2015-11-13 09:42:29 +08:00
|
|
|
|
|
|
|
return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
|
|
|
|
}
|
|
|
|
|
2015-07-23 05:28:15 +08:00
|
|
|
// Force static initialization.
|
|
|
|
extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
|
2016-10-10 07:00:34 +08:00
|
|
|
RegisterAsmPrinter<WebAssemblyAsmPrinter> X(getTheWebAssemblyTarget32());
|
|
|
|
RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(getTheWebAssemblyTarget64());
|
2015-07-23 05:28:15 +08:00
|
|
|
}
|