blob: 4216fb6ebe25acdee0bd4168050840b8b24f2dc3 [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 {
Dan Gohmane419a7c2015-08-24 16:46:31 +000061 TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
JF Bastien600aee92015-07-31 17:53:38 +000062 return AsmPrinter::runOnMachineFunction(MF);
JF Bastienb9073fb2015-07-22 21:28:15 +000063 }
64
65 //===------------------------------------------------------------------===//
66 // AsmPrinter Implementation.
67 //===------------------------------------------------------------------===//
68
69 void EmitInstruction(const MachineInstr *MI) override;
70};
71
72} // end anonymous namespace
73
74//===----------------------------------------------------------------------===//
75
JF Bastien315cc062015-08-07 01:57:03 +000076// Untyped, lower-case version of the opcode's name matching the names
77// WebAssembly opcodes are expected to have. The tablegen names are uppercase
78// and suffixed with their type (after an underscore).
79static SmallString<32> Name(const WebAssemblyInstrInfo *TII,
80 const MachineInstr *MI) {
81 std::string N(StringRef(TII->getName(MI->getOpcode())).lower());
82 std::string::size_type End = N.find('_');
83 End = std::string::npos == End ? N.length() : End;
84 return SmallString<32>(&N[0], &N[End]);
85}
86
JF Bastienb9073fb2015-07-22 21:28:15 +000087void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
88 SmallString<128> Str;
89 raw_svector_ostream OS(Str);
90
JF Bastien600aee92015-07-31 17:53:38 +000091 unsigned NumDefs = MI->getDesc().getNumDefs();
92 assert(NumDefs <= 1 &&
93 "Instructions with multiple result values not implemented");
94
95 if (NumDefs != 0) {
96 const MachineOperand &MO = MI->getOperand(0);
97 unsigned Reg = MO.getReg();
98 OS << "(setlocal @" << TargetRegisterInfo::virtReg2Index(Reg) << ' ';
99 }
100
JF Bastien4a642252015-08-10 22:36:48 +0000101 OS << '(' << Name(TII, MI);
102 for (const MachineOperand &MO : MI->uses())
103 switch (MO.getType()) {
104 default:
105 llvm_unreachable("unexpected machine operand type");
106 case MachineOperand::MO_Register: {
107 if (MO.isImplicit())
JF Bastien600aee92015-07-31 17:53:38 +0000108 continue;
109 unsigned Reg = MO.getReg();
110 OS << " @" << TargetRegisterInfo::virtReg2Index(Reg);
JF Bastien4a642252015-08-10 22:36:48 +0000111 } break;
112 case MachineOperand::MO_Immediate: {
113 OS << ' ' << MO.getImm();
114 } break;
115 case MachineOperand::MO_FPImmediate: {
116 static const size_t BufBytes = 128;
117 char buf[BufBytes];
118 APFloat FP = MO.getFPImm()->getValueAPF();
JF Bastiene73ce682015-08-11 00:49:20 +0000119 if (FP.isNaN())
120 assert((FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics())) ||
121 FP.bitwiseIsEqual(
122 APFloat::getQNaN(FP.getSemantics(), /*Negative=*/true))) &&
123 "convertToHexString handles neither SNaN nor NaN payloads");
JF Bastien4a642252015-08-10 22:36:48 +0000124 // Use C99's hexadecimal floating-point representation.
125 auto Written =
126 FP.convertToHexString(buf, /*hexDigits=*/0, /*upperCase=*/false,
127 APFloat::rmNearestTiesToEven);
JF Bastien11bf0da2015-08-11 04:52:24 +0000128 (void)Written;
JF Bastien4a642252015-08-10 22:36:48 +0000129 assert(Written != 0);
130 assert(Written < BufBytes);
131 OS << ' ' << buf;
132 } break;
JF Bastien600aee92015-07-31 17:53:38 +0000133 }
134 OS << ')';
135
136 if (NumDefs != 0)
137 OS << ')';
138
139 OS << '\n';
140
JF Bastienb9073fb2015-07-22 21:28:15 +0000141 OutStreamer->EmitRawText(OS.str());
142}
143
144// Force static initialization.
145extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
146 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32);
147 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(TheWebAssemblyTarget64);
148}