blob: a3157f40a664a2366435249e832befadb602042d [file] [log] [blame]
JF Bastienb9073fb2015-07-22 21:28:15 +00001//===-- WebAssemblyAsmPrinter.cpp - WebAssembly LLVM assembly writer ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief This file contains a printer that converts from our internal
12/// representation of machine-dependent LLVM code to the WebAssembly assembly
13/// language.
14///
15//===----------------------------------------------------------------------===//
16
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"
23
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"
33
34using namespace llvm;
35
36#define DEBUG_TYPE "asm-printer"
37
38namespace {
39
40class WebAssemblyAsmPrinter final : public AsmPrinter {
JF Bastien600aee92015-07-31 17:53:38 +000041 const WebAssemblyInstrInfo *TII;
42
JF Bastienb9073fb2015-07-22 21:28:15 +000043public:
44 WebAssemblyAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
JF Bastien600aee92015-07-31 17:53:38 +000045 : AsmPrinter(TM, std::move(Streamer)), TII(nullptr) {}
JF Bastienb9073fb2015-07-22 21:28:15 +000046
47private:
48 const char *getPassName() const override {
49 return "WebAssembly Assembly Printer";
50 }
51
52 //===------------------------------------------------------------------===//
53 // MachineFunctionPass Implementation.
54 //===------------------------------------------------------------------===//
55
56 void getAnalysisUsage(AnalysisUsage &AU) const override {
57 AsmPrinter::getAnalysisUsage(AU);
58 }
59
JF Bastien600aee92015-07-31 17:53:38 +000060 bool runOnMachineFunction(MachineFunction &MF) override {
61 TII = static_cast<const WebAssemblyInstrInfo *>(
62 MF.getSubtarget().getInstrInfo());
63 return AsmPrinter::runOnMachineFunction(MF);
JF Bastienb9073fb2015-07-22 21:28:15 +000064 }
65
66 //===------------------------------------------------------------------===//
67 // AsmPrinter Implementation.
68 //===------------------------------------------------------------------===//
69
70 void EmitInstruction(const MachineInstr *MI) override;
71};
72
73} // end anonymous namespace
74
75//===----------------------------------------------------------------------===//
76
77void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
78 SmallString<128> Str;
79 raw_svector_ostream OS(Str);
80
JF Bastien600aee92015-07-31 17:53:38 +000081 unsigned NumDefs = MI->getDesc().getNumDefs();
82 assert(NumDefs <= 1 &&
83 "Instructions with multiple result values not implemented");
84
85 if (NumDefs != 0) {
86 const MachineOperand &MO = MI->getOperand(0);
87 unsigned Reg = MO.getReg();
88 OS << "(setlocal @" << TargetRegisterInfo::virtReg2Index(Reg) << ' ';
89 }
90
91 OS << '(';
92
93 bool PrintOperands = true;
JF Bastienb9073fb2015-07-22 21:28:15 +000094 switch (MI->getOpcode()) {
JF Bastien8f9aea02015-08-01 04:48:44 +000095 case WebAssembly::ARGUMENT_Int32:
96 case WebAssembly::ARGUMENT_Int64:
97 case WebAssembly::ARGUMENT_Float32:
98 case WebAssembly::ARGUMENT_Float64:
JF Bastien600aee92015-07-31 17:53:38 +000099 OS << "argument " << MI->getOperand(1).getImm();
100 PrintOperands = false;
101 break;
JF Bastien8f9aea02015-08-01 04:48:44 +0000102 case WebAssembly::RETURN_Int32:
103 case WebAssembly::RETURN_Int64:
104 case WebAssembly::RETURN_Float32:
105 case WebAssembly::RETURN_Float64:
106 case WebAssembly::RETURN_VOID:
107 // FIXME This is here only so "return" prints nicely, instead of printing
108 // the isel name. Other operations have the same problem, fix this in
109 // a generic way instead.
110 OS << "return";
111 break;
JF Bastienb9073fb2015-07-22 21:28:15 +0000112 default:
JF Bastien600aee92015-07-31 17:53:38 +0000113 OS << TII->getName(MI->getOpcode());
JF Bastienb9073fb2015-07-22 21:28:15 +0000114 break;
115 }
116
JF Bastien600aee92015-07-31 17:53:38 +0000117 if (PrintOperands)
118 for (const MachineOperand &MO : MI->uses()) {
119 if (MO.isReg() && MO.isImplicit())
120 continue;
121 unsigned Reg = MO.getReg();
122 OS << " @" << TargetRegisterInfo::virtReg2Index(Reg);
123 }
124 OS << ')';
125
126 if (NumDefs != 0)
127 OS << ')';
128
129 OS << '\n';
130
JF Bastienb9073fb2015-07-22 21:28:15 +0000131 OutStreamer->EmitRawText(OS.str());
132}
133
134// Force static initialization.
135extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
136 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32);
137 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(TheWebAssemblyTarget64);
138}