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"
|
2019-01-17 10:29:55 +08:00
|
|
|
#include "llvm/ADT/SmallSet.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"
|
2019-01-18 10:47:48 +08:00
|
|
|
#include "llvm/IR/DebugInfoMetadata.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) {
|
2018-11-02 08:45:00 +08:00
|
|
|
for (auto &It : OutContext.getSymbols()) {
|
2018-11-14 10:46:21 +08:00
|
|
|
// Emit a .globaltype and .eventtype declaration.
|
2018-11-02 08:45:00 +08:00
|
|
|
auto Sym = cast<MCSymbolWasm>(It.getValue());
|
2018-11-14 10:46:21 +08:00
|
|
|
if (Sym->getType() == wasm::WASM_SYMBOL_TYPE_GLOBAL)
|
2018-11-02 08:45:00 +08:00
|
|
|
getTargetStreamer()->emitGlobalType(Sym);
|
2018-11-14 10:46:21 +08:00
|
|
|
else if (Sym->getType() == wasm::WASM_SYMBOL_TYPE_EVENT)
|
|
|
|
getTargetStreamer()->emitEventType(Sym);
|
2018-11-02 08:45:00 +08:00
|
|
|
}
|
|
|
|
|
2016-06-04 02:34:36 +08:00
|
|
|
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;
|
2018-10-04 06:22:48 +08:00
|
|
|
ComputeSignatureVTs(F.getFunctionType(), F, TM, Params, Results);
|
|
|
|
auto *Sym = cast<MCSymbolWasm>(getSymbol(&F));
|
2018-12-11 08:53:59 +08:00
|
|
|
Sym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
|
2018-10-04 06:22:48 +08:00
|
|
|
if (!Sym->getSignature()) {
|
|
|
|
auto Signature = SignatureFromMVTs(Results, Params);
|
|
|
|
Sym->setSignature(Signature.get());
|
|
|
|
addSignature(std::move(Signature));
|
|
|
|
}
|
|
|
|
// FIXME: this was originally intended for post-linking and was only used
|
|
|
|
// for imports that were only called indirectly (i.e. s2wasm could not
|
|
|
|
// infer the type from a call). With object files it applies to all
|
|
|
|
// imports. so fix the names and the tests, or rethink how import
|
|
|
|
// delcarations work in asm files.
|
2018-11-20 01:10:36 +08:00
|
|
|
getTargetStreamer()->emitFunctionType(Sym);
|
2018-02-10 07:13:22 +08:00
|
|
|
|
2018-02-13 05:41:12 +08:00
|
|
|
if (TM.getTargetTriple().isOSBinFormatWasm() &&
|
|
|
|
F.hasFnAttribute("wasm-import-module")) {
|
2018-09-05 09:27:38 +08:00
|
|
|
StringRef Name =
|
|
|
|
F.getFnAttribute("wasm-import-module").getValueAsString();
|
2018-12-11 08:53:59 +08:00
|
|
|
Sym->setModuleName(Name);
|
2018-10-04 06:22:48 +08:00
|
|
|
getTargetStreamer()->emitImportModule(Sym, Name);
|
2018-02-10 07:13:22 +08:00
|
|
|
}
|
2016-06-04 02:34:36 +08:00
|
|
|
}
|
|
|
|
}
|
2018-11-02 08:45:00 +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();
|
|
|
|
}
|
|
|
|
}
|
2019-01-17 10:29:55 +08:00
|
|
|
|
2019-01-18 10:47:48 +08:00
|
|
|
EmitProducerInfo(M);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebAssemblyAsmPrinter::EmitProducerInfo(Module &M) {
|
|
|
|
llvm::SmallVector<std::pair<std::string, std::string>, 4> Languages;
|
|
|
|
if (const NamedMDNode *Debug = M.getNamedMetadata("llvm.dbg.cu")) {
|
|
|
|
llvm::SmallSet<StringRef, 4> SeenLanguages;
|
|
|
|
for (size_t i = 0, e = Debug->getNumOperands(); i < e; ++i) {
|
|
|
|
const auto *CU = cast<DICompileUnit>(Debug->getOperand(i));
|
|
|
|
StringRef Language = dwarf::LanguageString(CU->getSourceLanguage());
|
|
|
|
Language.consume_front("DW_LANG_");
|
|
|
|
if (SeenLanguages.insert(Language).second)
|
|
|
|
Languages.emplace_back(Language.str(), "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::SmallVector<std::pair<std::string, std::string>, 4> Tools;
|
2019-01-17 10:29:55 +08:00
|
|
|
if (const NamedMDNode *Ident = M.getNamedMetadata("llvm.ident")) {
|
|
|
|
llvm::SmallSet<StringRef, 4> SeenTools;
|
|
|
|
for (size_t i = 0, e = Ident->getNumOperands(); i < e; ++i) {
|
|
|
|
const auto *S = cast<MDString>(Ident->getOperand(i)->getOperand(0));
|
|
|
|
std::pair<StringRef, StringRef> Field = S->getString().split("version");
|
|
|
|
StringRef Name = Field.first.trim();
|
|
|
|
StringRef Version = Field.second.trim();
|
2019-01-18 10:47:48 +08:00
|
|
|
if (SeenTools.insert(Name).second)
|
|
|
|
Tools.emplace_back(Name.str(), Version.str());
|
2019-01-17 10:29:55 +08:00
|
|
|
}
|
2019-01-18 10:47:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int FieldCount = int(!Languages.empty()) + int(!Tools.empty());
|
|
|
|
if (FieldCount != 0) {
|
2019-01-17 10:29:55 +08:00
|
|
|
MCSectionWasm *Producers = OutContext.getWasmSection(
|
|
|
|
".custom_section.producers", SectionKind::getMetadata());
|
|
|
|
OutStreamer->PushSection();
|
|
|
|
OutStreamer->SwitchSection(Producers);
|
2019-01-18 10:47:48 +08:00
|
|
|
OutStreamer->EmitULEB128IntValue(FieldCount);
|
|
|
|
for (auto &Producers : {std::make_pair("language", &Languages),
|
|
|
|
std::make_pair("processed-by", &Tools)}) {
|
|
|
|
if (Producers.second->empty())
|
|
|
|
continue;
|
|
|
|
OutStreamer->EmitULEB128IntValue(strlen(Producers.first));
|
|
|
|
OutStreamer->EmitBytes(Producers.first);
|
|
|
|
OutStreamer->EmitULEB128IntValue(Producers.second->size());
|
|
|
|
for (auto &Producer : *Producers.second) {
|
|
|
|
OutStreamer->EmitULEB128IntValue(Producer.first.size());
|
|
|
|
OutStreamer->EmitBytes(Producer.first);
|
|
|
|
OutStreamer->EmitULEB128IntValue(Producer.second.size());
|
|
|
|
OutStreamer->EmitBytes(Producer.second);
|
|
|
|
}
|
2019-01-17 10:29:55 +08:00
|
|
|
}
|
|
|
|
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-12-16 07:52:06 +08:00
|
|
|
const Function &F = MF->getFunction();
|
2018-10-04 06:22:48 +08:00
|
|
|
SmallVector<MVT, 1> ResultVTs;
|
|
|
|
SmallVector<MVT, 4> ParamVTs;
|
|
|
|
ComputeSignatureVTs(F.getFunctionType(), F, TM, ParamVTs, ResultVTs);
|
|
|
|
auto Signature = SignatureFromMVTs(ResultVTs, ParamVTs);
|
|
|
|
auto *WasmSym = cast<MCSymbolWasm>(CurrentFnSym);
|
|
|
|
WasmSym->setSignature(Signature.get());
|
|
|
|
addSignature(std::move(Signature));
|
2018-12-11 08:53:59 +08:00
|
|
|
WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
|
2018-10-04 06:22:48 +08:00
|
|
|
|
|
|
|
// FIXME: clean up how params and results are emitted (use signatures)
|
2018-11-20 01:10:36 +08:00
|
|
|
getTargetStreamer()->emitFunctionType(WasmSym);
|
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()));
|
|
|
|
}
|
|
|
|
|
2018-11-20 01:10:36 +08:00
|
|
|
SmallVector<wasm::ValType, 16> Locals;
|
|
|
|
ValTypesFromMVTs(MFI->getLocals(), Locals);
|
|
|
|
getTargetStreamer()->emitLocal(Locals);
|
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()) {
|
2018-10-13 15:09:10 +08:00
|
|
|
case WebAssembly::ARGUMENT_i32:
|
|
|
|
case WebAssembly::ARGUMENT_i32_S:
|
|
|
|
case WebAssembly::ARGUMENT_i64:
|
|
|
|
case WebAssembly::ARGUMENT_i64_S:
|
|
|
|
case WebAssembly::ARGUMENT_f32:
|
|
|
|
case WebAssembly::ARGUMENT_f32_S:
|
|
|
|
case WebAssembly::ARGUMENT_f64:
|
|
|
|
case WebAssembly::ARGUMENT_f64_S:
|
2016-08-03 07:16:09 +08:00
|
|
|
case WebAssembly::ARGUMENT_v16i8:
|
2018-08-27 23:45:51 +08:00
|
|
|
case WebAssembly::ARGUMENT_v16i8_S:
|
2016-08-03 07:16:09 +08:00
|
|
|
case WebAssembly::ARGUMENT_v8i16:
|
2018-08-27 23:45:51 +08:00
|
|
|
case WebAssembly::ARGUMENT_v8i16_S:
|
2016-08-03 07:16:09 +08:00
|
|
|
case WebAssembly::ARGUMENT_v4i32:
|
2018-08-27 23:45:51 +08:00
|
|
|
case WebAssembly::ARGUMENT_v4i32_S:
|
2018-08-08 05:24:01 +08:00
|
|
|
case WebAssembly::ARGUMENT_v2i64:
|
2018-08-27 23:45:51 +08:00
|
|
|
case WebAssembly::ARGUMENT_v2i64_S:
|
2016-08-03 07:16:09 +08:00
|
|
|
case WebAssembly::ARGUMENT_v4f32:
|
2018-08-27 23:45:51 +08:00
|
|
|
case WebAssembly::ARGUMENT_v4f32_S:
|
2018-08-08 05:24:01 +08:00
|
|
|
case WebAssembly::ARGUMENT_v2f64:
|
2018-08-27 23:45:51 +08:00
|
|
|
case WebAssembly::ARGUMENT_v2f64_S:
|
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:
|
2018-08-27 23:45:51 +08:00
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_I32_S:
|
2016-05-21 08:21:56 +08:00
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_I64:
|
2018-08-27 23:45:51 +08:00
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_I64_S:
|
2016-05-21 08:21:56 +08:00
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_F32:
|
2018-08-27 23:45:51 +08:00
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_F32_S:
|
2016-08-03 07:16:09 +08:00
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_F64:
|
2018-08-27 23:45:51 +08:00
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_F64_S:
|
2016-08-03 07:16:09 +08:00
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_v16i8:
|
2018-08-27 23:45:51 +08:00
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_v16i8_S:
|
2016-08-03 07:16:09 +08:00
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_v8i16:
|
2018-08-27 23:45:51 +08:00
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_v8i16_S:
|
2016-08-03 07:16:09 +08:00
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_v4i32:
|
2018-08-27 23:45:51 +08:00
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_v4i32_S:
|
2018-08-08 05:24:01 +08:00
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_v2i64:
|
2018-08-27 23:45:51 +08:00
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_v2i64_S:
|
2018-08-08 05:24:01 +08:00
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_v4f32:
|
2018-08-27 23:45:51 +08:00
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_v4f32_S:
|
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_v2f64:
|
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_v2f64_S: {
|
2016-05-21 08:21:56 +08:00
|
|
|
// These instructions represent the implicit return at the end of a
|
2018-08-27 23:45:51 +08:00
|
|
|
// function body. Always pops one value off the stack.
|
2016-05-21 08:21:56 +08:00
|
|
|
if (isVerbose()) {
|
2018-08-27 23:45:51 +08:00
|
|
|
OutStreamer->AddComment("fallthrough-return-value");
|
2016-05-21 08:21:56 +08:00
|
|
|
OutStreamer->AddBlankLine();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_VOID:
|
2018-08-27 23:45:51 +08:00
|
|
|
case WebAssembly::FALLTHROUGH_RETURN_VOID_S:
|
2016-05-21 08:21:56 +08:00
|
|
|
// This instruction represents the implicit return at the end of a
|
|
|
|
// function body with no return value.
|
|
|
|
if (isVerbose()) {
|
2018-08-27 23:45:51 +08:00
|
|
|
OutStreamer->AddComment("fallthrough-return-void");
|
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:
|
2018-08-27 23:45:51 +08:00
|
|
|
// FIXME: only opcode that still contains registers, as required by
|
|
|
|
// MachineInstr::getDebugVariable().
|
|
|
|
assert(MI->getOpcode() == WebAssembly::INLINEASM);
|
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
|
|
|
}
|