e0e9a3b14bc28d84ddae9ac5873ceb99a191988c
[oota-llvm.git] / lib / Target / WebAssembly / WebAssemblyAsmPrinter.cpp
1 //===-- 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
34 using namespace llvm;
35
36 #define DEBUG_TYPE "asm-printer"
37
38 namespace {
39
40 class WebAssemblyAsmPrinter final : public AsmPrinter {
41   const WebAssemblyInstrInfo *TII;
42
43 public:
44   WebAssemblyAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
45       : AsmPrinter(TM, std::move(Streamer)), TII(nullptr) {}
46
47 private:
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
60   bool runOnMachineFunction(MachineFunction &MF) override {
61     TII = static_cast<const WebAssemblyInstrInfo *>(
62         MF.getSubtarget().getInstrInfo());
63     return AsmPrinter::runOnMachineFunction(MF);
64   }
65
66   //===------------------------------------------------------------------===//
67   // AsmPrinter Implementation.
68   //===------------------------------------------------------------------===//
69
70   void EmitInstruction(const MachineInstr *MI) override;
71 };
72
73 } // end anonymous namespace
74
75 //===----------------------------------------------------------------------===//
76
77 void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
78   SmallString<128> Str;
79   raw_svector_ostream OS(Str);
80
81   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;
94   switch (MI->getOpcode()) {
95   case WebAssembly::ARGUMENT:
96     OS << "argument " << MI->getOperand(1).getImm();
97     PrintOperands = false;
98     break;
99   default:
100     OS << TII->getName(MI->getOpcode());
101     break;
102   }
103
104   if (PrintOperands)
105     for (const MachineOperand &MO : MI->uses()) {
106       if (MO.isReg() && MO.isImplicit())
107         continue;
108       unsigned Reg = MO.getReg();
109       OS << " @" << TargetRegisterInfo::virtReg2Index(Reg);
110     }
111   OS << ')';
112
113   if (NumDefs != 0)
114     OS << ')';
115
116   OS << '\n';
117
118   OutStreamer->EmitRawText(OS.str());
119 }
120
121 // Force static initialization.
122 extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
123   RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32);
124   RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(TheWebAssemblyTarget64);
125 }