blob: 079c864c7bc1c15e9a7bc5c8d238fad2829ea0af [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"
Dan Gohmancf4748f2015-11-12 17:04:33 +000018#include "InstPrinter/WebAssemblyInstPrinter.h"
19#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
20#include "WebAssemblyMCInstLower.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000021#include "WebAssemblyMachineFunctionInfo.h"
22#include "WebAssemblyRegisterInfo.h"
23#include "WebAssemblySubtarget.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000024#include "llvm/ADT/SmallString.h"
Dan Gohmane51c0582015-10-06 00:27:55 +000025#include "llvm/ADT/StringExtras.h"
Dan Gohman754cd112015-11-11 01:33:02 +000026#include "llvm/CodeGen/Analysis.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000027#include "llvm/CodeGen/AsmPrinter.h"
JF Bastien54be3b12015-08-25 23:19:49 +000028#include "llvm/CodeGen/MachineConstantPool.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000029#include "llvm/CodeGen/MachineInstr.h"
30#include "llvm/IR/DataLayout.h"
Dan Gohmancf4748f2015-11-12 17:04:33 +000031#include "llvm/MC/MCContext.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000032#include "llvm/MC/MCStreamer.h"
JF Bastienb6091df2015-08-25 22:58:05 +000033#include "llvm/MC/MCSymbol.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000034#include "llvm/Support/Debug.h"
35#include "llvm/Support/TargetRegistry.h"
36#include "llvm/Support/raw_ostream.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000037using namespace llvm;
38
39#define DEBUG_TYPE "asm-printer"
40
41namespace {
42
43class WebAssemblyAsmPrinter final : public AsmPrinter {
JF Bastien1d20a5e2015-10-16 00:53:49 +000044 const MachineRegisterInfo *MRI;
Dan Gohmancf4748f2015-11-12 17:04:33 +000045 const WebAssemblyFunctionInfo *MFI;
JF Bastien600aee92015-07-31 17:53:38 +000046
JF Bastienb9073fb2015-07-22 21:28:15 +000047public:
48 WebAssemblyAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
Dan Gohmancf4748f2015-11-12 17:04:33 +000049 : AsmPrinter(TM, std::move(Streamer)), MRI(nullptr), MFI(nullptr) {}
JF Bastienb9073fb2015-07-22 21:28:15 +000050
51private:
52 const char *getPassName() const override {
53 return "WebAssembly Assembly Printer";
54 }
55
56 //===------------------------------------------------------------------===//
57 // MachineFunctionPass Implementation.
58 //===------------------------------------------------------------------===//
59
JF Bastien600aee92015-07-31 17:53:38 +000060 bool runOnMachineFunction(MachineFunction &MF) override {
JF Bastien1d20a5e2015-10-16 00:53:49 +000061 MRI = &MF.getRegInfo();
Dan Gohmancf4748f2015-11-12 17:04:33 +000062 MFI = MF.getInfo<WebAssemblyFunctionInfo>();
JF Bastien600aee92015-07-31 17:53:38 +000063 return AsmPrinter::runOnMachineFunction(MF);
JF Bastienb9073fb2015-07-22 21:28:15 +000064 }
65
66 //===------------------------------------------------------------------===//
67 // AsmPrinter Implementation.
68 //===------------------------------------------------------------------===//
69
Dan Gohman950a13c2015-09-16 16:51:30 +000070 void EmitJumpTableInfo() override;
JF Bastien54be3b12015-08-25 23:19:49 +000071 void EmitConstantPool() override;
JF Bastienb6091df2015-08-25 22:58:05 +000072 void EmitFunctionBodyStart() override;
JF Bastienb9073fb2015-07-22 21:28:15 +000073 void EmitInstruction(const MachineInstr *MI) override;
Dan Gohmanf19ed562015-11-13 01:42:29 +000074 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
75 unsigned AsmVariant, const char *ExtraCode,
76 raw_ostream &OS) override;
77 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
78 unsigned AsmVariant, const char *ExtraCode,
79 raw_ostream &OS) override;
Dan Gohman979840d2015-09-23 16:59:10 +000080
Dan Gohman53828fd2015-11-23 16:50:18 +000081 MVT getRegType(unsigned RegNo) const;
Dan Gohman754cd112015-11-11 01:33:02 +000082 const char *toString(MVT VT) const;
JF Bastien1d20a5e2015-10-16 00:53:49 +000083 std::string regToString(const MachineOperand &MO);
JF Bastienb9073fb2015-07-22 21:28:15 +000084};
85
86} // end anonymous namespace
87
88//===----------------------------------------------------------------------===//
JF Bastien45479f62015-08-26 22:09:54 +000089// Helpers.
90//===----------------------------------------------------------------------===//
JF Bastienb9073fb2015-07-22 21:28:15 +000091
Dan Gohman53828fd2015-11-23 16:50:18 +000092MVT WebAssemblyAsmPrinter::getRegType(unsigned RegNo) const {
JF Bastien1d20a5e2015-10-16 00:53:49 +000093 const TargetRegisterClass *TRC = MRI->getRegClass(RegNo);
94 for (MVT T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64})
95 if (TRC->hasType(T))
Dan Gohman53828fd2015-11-23 16:50:18 +000096 return T;
JF Bastien1d20a5e2015-10-16 00:53:49 +000097 DEBUG(errs() << "Unknown type for register number: " << RegNo);
98 llvm_unreachable("Unknown register type");
Dan Gohman53828fd2015-11-23 16:50:18 +000099 return MVT::Other;
JF Bastien1d20a5e2015-10-16 00:53:49 +0000100}
101
JF Bastien1d20a5e2015-10-16 00:53:49 +0000102std::string WebAssemblyAsmPrinter::regToString(const MachineOperand &MO) {
103 unsigned RegNo = MO.getReg();
Dan Gohmand9625272015-11-20 03:13:31 +0000104 assert(TargetRegisterInfo::isVirtualRegister(RegNo) &&
105 "Unlowered physical register encountered during assembly printing");
Dan Gohman4ba48162015-11-18 16:12:01 +0000106 assert(!MFI->isVRegStackified(RegNo));
Dan Gohman058fce52015-11-13 00:21:05 +0000107 unsigned WAReg = MFI->getWAReg(RegNo);
108 assert(WAReg != WebAssemblyFunctionInfo::UnusedReg);
Dan Gohman4ba48162015-11-18 16:12:01 +0000109 return '$' + utostr(WAReg);
Dan Gohmane51c0582015-10-06 00:27:55 +0000110}
111
Dan Gohman754cd112015-11-11 01:33:02 +0000112const char *WebAssemblyAsmPrinter::toString(MVT VT) const {
Dan Gohman5e0886b2015-12-06 19:42:29 +0000113 return WebAssembly::TypeToString(VT);
JF Bastien73ff6af2015-08-31 22:24:11 +0000114}
115
JF Bastien45479f62015-08-26 22:09:54 +0000116//===----------------------------------------------------------------------===//
117// WebAssemblyAsmPrinter Implementation.
118//===----------------------------------------------------------------------===//
119
JF Bastien54be3b12015-08-25 23:19:49 +0000120void WebAssemblyAsmPrinter::EmitConstantPool() {
121 assert(MF->getConstantPool()->getConstants().empty() &&
122 "WebAssembly disables constant pools");
123}
124
Dan Gohman950a13c2015-09-16 16:51:30 +0000125void WebAssemblyAsmPrinter::EmitJumpTableInfo() {
126 // Nothing to do; jump tables are incorporated into the instruction stream.
127}
128
Dan Gohman7a6b9822015-11-29 22:32:02 +0000129static void ComputeLegalValueVTs(const Function &F, const TargetMachine &TM,
130 Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
131 const DataLayout &DL(F.getParent()->getDataLayout());
Derek Schuff46e33162015-11-16 21:12:41 +0000132 const WebAssemblyTargetLowering &TLI =
133 *TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering();
134 SmallVector<EVT, 4> VTs;
135 ComputeValueVTs(TLI, DL, Ty, VTs);
136
137 for (EVT VT : VTs) {
138 unsigned NumRegs = TLI.getNumRegisters(F.getContext(), VT);
139 MVT RegisterVT = TLI.getRegisterType(F.getContext(), VT);
140 for (unsigned i = 0; i != NumRegs; ++i)
141 ValueVTs.push_back(RegisterVT);
142 }
143}
144
JF Bastienb6091df2015-08-25 22:58:05 +0000145void WebAssemblyAsmPrinter::EmitFunctionBodyStart() {
Dan Gohman53828fd2015-11-23 16:50:18 +0000146 if (!MFI->getParams().empty()) {
147 MCInst Param;
148 Param.setOpcode(WebAssembly::PARAM);
149 for (MVT VT : MFI->getParams())
150 Param.addOperand(MCOperand::createImm(VT.SimpleTy));
151 EmitToStreamer(*OutStreamer, Param);
152 }
Dan Gohmane51c0582015-10-06 00:27:55 +0000153
Derek Schuff46e33162015-11-16 21:12:41 +0000154 SmallVector<MVT, 4> ResultVTs;
155 const Function &F(*MF->getFunction());
156 ComputeLegalValueVTs(F, TM, F.getReturnType(), ResultVTs);
157 // If the return type needs to be legalized it will get converted into
158 // passing a pointer.
Dan Gohman53828fd2015-11-23 16:50:18 +0000159 if (ResultVTs.size() == 1) {
160 MCInst Result;
161 Result.setOpcode(WebAssembly::RESULT);
162 Result.addOperand(MCOperand::createImm(ResultVTs.front().SimpleTy));
163 EmitToStreamer(*OutStreamer, Result);
164 }
JF Bastienb6091df2015-08-25 22:58:05 +0000165
Dan Gohman53828fd2015-11-23 16:50:18 +0000166 bool AnyWARegs = false;
167 MCInst Local;
168 Local.setOpcode(WebAssembly::LOCAL);
JF Bastien1d20a5e2015-10-16 00:53:49 +0000169 for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) {
170 unsigned VReg = TargetRegisterInfo::index2VirtReg(Idx);
Dan Gohman4ba48162015-11-18 16:12:01 +0000171 unsigned WAReg = MFI->getWAReg(VReg);
172 // Don't declare unused registers.
173 if (WAReg == WebAssemblyFunctionInfo::UnusedReg)
174 continue;
175 // Don't redeclare parameters.
176 if (WAReg < MFI->getParams().size())
177 continue;
178 // Don't declare stackified registers.
179 if (int(WAReg) < 0)
180 continue;
Dan Gohman53828fd2015-11-23 16:50:18 +0000181 Local.addOperand(MCOperand::createImm(getRegType(VReg).SimpleTy));
182 AnyWARegs = true;
JF Bastien1d20a5e2015-10-16 00:53:49 +0000183 }
Dan Gohman53828fd2015-11-23 16:50:18 +0000184 if (AnyWARegs)
185 EmitToStreamer(*OutStreamer, Local);
JF Bastien1d20a5e2015-10-16 00:53:49 +0000186
Dan Gohmane51c0582015-10-06 00:27:55 +0000187 AsmPrinter::EmitFunctionBodyStart();
JF Bastienb6091df2015-08-25 22:58:05 +0000188}
189
JF Bastienb9073fb2015-07-22 21:28:15 +0000190void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
JF Bastienaf111db2015-08-24 22:16:48 +0000191 DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
JF Bastienb9073fb2015-07-22 21:28:15 +0000192
Dan Gohmane51c0582015-10-06 00:27:55 +0000193 switch (MI->getOpcode()) {
Dan Gohmane51c0582015-10-06 00:27:55 +0000194 case WebAssembly::ARGUMENT_I32:
195 case WebAssembly::ARGUMENT_I64:
196 case WebAssembly::ARGUMENT_F32:
197 case WebAssembly::ARGUMENT_F64:
Dan Gohmancf4748f2015-11-12 17:04:33 +0000198 // These represent values which are live into the function entry, so there's
199 // no instruction to emit.
Dan Gohmane51c0582015-10-06 00:27:55 +0000200 break;
Dan Gohmanf6857222015-11-23 19:12:37 +0000201 case WebAssembly::LOOP_END:
202 // This is a no-op which just exists to tell AsmPrinter.cpp that there's a
203 // fallthrough which nevertheless requires a label for the destination here.
204 break;
Dan Gohmane51c0582015-10-06 00:27:55 +0000205 default: {
Dan Gohmancf4748f2015-11-12 17:04:33 +0000206 WebAssemblyMCInstLower MCInstLowering(OutContext, *this);
207 MCInst TmpInst;
208 MCInstLowering.Lower(MI, TmpInst);
209 EmitToStreamer(*OutStreamer, TmpInst);
Dan Gohmane51c0582015-10-06 00:27:55 +0000210 break;
211 }
Dan Gohman4f52e002015-09-09 00:52:47 +0000212 }
JF Bastienb9073fb2015-07-22 21:28:15 +0000213}
214
Dan Gohmanf19ed562015-11-13 01:42:29 +0000215bool WebAssemblyAsmPrinter::PrintAsmOperand(const MachineInstr *MI,
216 unsigned OpNo, unsigned AsmVariant,
217 const char *ExtraCode,
218 raw_ostream &OS) {
219 if (AsmVariant != 0)
220 report_fatal_error("There are no defined alternate asm variants");
221
Dan Gohman30a42bf2015-12-16 17:15:17 +0000222 // First try the generic code, which knows about modifiers like 'c' and 'n'.
223 if (!AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS))
224 return false;
225
Dan Gohmanf19ed562015-11-13 01:42:29 +0000226 if (!ExtraCode) {
227 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohman30a42bf2015-12-16 17:15:17 +0000228 switch (MO.getType()) {
229 case MachineOperand::MO_Immediate:
Dan Gohmanf19ed562015-11-13 01:42:29 +0000230 OS << MO.getImm();
Dan Gohman30a42bf2015-12-16 17:15:17 +0000231 return false;
232 case MachineOperand::MO_Register:
Dan Gohmanf19ed562015-11-13 01:42:29 +0000233 OS << regToString(MO);
Dan Gohman30a42bf2015-12-16 17:15:17 +0000234 return false;
235 case MachineOperand::MO_GlobalAddress:
236 getSymbol(MO.getGlobal())->print(OS, MAI);
237 printOffset(MO.getOffset(), OS);
238 return false;
239 case MachineOperand::MO_ExternalSymbol:
240 GetExternalSymbolSymbol(MO.getSymbolName())->print(OS, MAI);
241 printOffset(MO.getOffset(), OS);
242 return false;
243 case MachineOperand::MO_MachineBasicBlock:
244 MO.getMBB()->getSymbol()->print(OS, MAI);
245 return false;
246 default:
247 break;
248 }
Dan Gohmanf19ed562015-11-13 01:42:29 +0000249 }
250
Dan Gohman30a42bf2015-12-16 17:15:17 +0000251 return true;
Dan Gohmanf19ed562015-11-13 01:42:29 +0000252}
253
254bool WebAssemblyAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
255 unsigned OpNo,
256 unsigned AsmVariant,
257 const char *ExtraCode,
258 raw_ostream &OS) {
259 if (AsmVariant != 0)
260 report_fatal_error("There are no defined alternate asm variants");
261
262 if (!ExtraCode) {
Dan Gohmane2831b42015-12-16 18:14:49 +0000263 // TODO: For now, we just hard-code 0 as the constant offset; teach
264 // SelectInlineAsmMemoryOperand how to do address mode matching.
265 OS << "0(" + regToString(MI->getOperand(OpNo)) + ')';
Dan Gohmanf19ed562015-11-13 01:42:29 +0000266 return false;
267 }
268
269 return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
270}
271
JF Bastienb9073fb2015-07-22 21:28:15 +0000272// Force static initialization.
273extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
274 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32);
275 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(TheWebAssemblyTarget64);
276}