1 //===-- WebAssemblyAsmPrinter.cpp - WebAssembly LLVM assembly writer ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This file contains a printer that converts from our internal
12 /// representation of machine-dependent LLVM code to the WebAssembly assembly
15 //===----------------------------------------------------------------------===//
17 #include "WebAssembly.h"
18 #include "WebAssemblyMachineFunctionInfo.h"
19 #include "WebAssemblyRegisterInfo.h"
20 #include "WebAssemblySubtarget.h"
21 #include "InstPrinter/WebAssemblyInstPrinter.h"
22 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
24 #include "llvm/ADT/SmallString.h"
25 #include "llvm/CodeGen/AsmPrinter.h"
26 #include "llvm/CodeGen/MachineInstr.h"
27 #include "llvm/IR/DataLayout.h"
28 #include "llvm/IR/DebugInfo.h"
29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/TargetRegistry.h"
32 #include "llvm/Support/raw_ostream.h"
36 #define DEBUG_TYPE "asm-printer"
40 class WebAssemblyAsmPrinter final : public AsmPrinter {
41 const WebAssemblyInstrInfo *TII;
44 WebAssemblyAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
45 : AsmPrinter(TM, std::move(Streamer)), TII(nullptr) {}
48 const char *getPassName() const override {
49 return "WebAssembly Assembly Printer";
52 //===------------------------------------------------------------------===//
53 // MachineFunctionPass Implementation.
54 //===------------------------------------------------------------------===//
56 void getAnalysisUsage(AnalysisUsage &AU) const override {
57 AsmPrinter::getAnalysisUsage(AU);
60 bool runOnMachineFunction(MachineFunction &MF) override {
61 TII = static_cast<const WebAssemblyInstrInfo *>(
62 MF.getSubtarget().getInstrInfo());
63 return AsmPrinter::runOnMachineFunction(MF);
66 //===------------------------------------------------------------------===//
67 // AsmPrinter Implementation.
68 //===------------------------------------------------------------------===//
70 void EmitInstruction(const MachineInstr *MI) override;
73 } // end anonymous namespace
75 //===----------------------------------------------------------------------===//
77 // Untyped, lower-case version of the opcode's name matching the names
78 // WebAssembly opcodes are expected to have. The tablegen names are uppercase
79 // and suffixed with their type (after an underscore).
80 static SmallString<32> Name(const WebAssemblyInstrInfo *TII,
81 const MachineInstr *MI) {
82 std::string N(StringRef(TII->getName(MI->getOpcode())).lower());
83 std::string::size_type End = N.find('_');
84 End = std::string::npos == End ? N.length() : End;
85 return SmallString<32>(&N[0], &N[End]);
88 void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
90 raw_svector_ostream OS(Str);
92 unsigned NumDefs = MI->getDesc().getNumDefs();
93 assert(NumDefs <= 1 &&
94 "Instructions with multiple result values not implemented");
97 const MachineOperand &MO = MI->getOperand(0);
98 unsigned Reg = MO.getReg();
99 OS << "(setlocal @" << TargetRegisterInfo::virtReg2Index(Reg) << ' ';
104 bool PrintOperands = true;
105 switch (MI->getOpcode()) {
106 case WebAssembly::ARGUMENT_Int32:
107 case WebAssembly::ARGUMENT_Int64:
108 case WebAssembly::ARGUMENT_Float32:
109 case WebAssembly::ARGUMENT_Float64:
110 OS << Name(TII, MI) << ' ' << MI->getOperand(1).getImm();
111 PrintOperands = false;
119 for (const MachineOperand &MO : MI->uses()) {
120 if (MO.isReg() && MO.isImplicit())
122 unsigned Reg = MO.getReg();
123 OS << " @" << TargetRegisterInfo::virtReg2Index(Reg);
132 OutStreamer->EmitRawText(OS.str());
135 // Force static initialization.
136 extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
137 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32);
138 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(TheWebAssemblyTarget64);