blob: 084c4ed277b71dcf994964c1aea6d0680c8c8cd5 [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"
Derek Schuff45cd5a72015-12-16 20:43:06 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000030#include "llvm/CodeGen/MachineInstr.h"
31#include "llvm/IR/DataLayout.h"
Dan Gohmancf4748f2015-11-12 17:04:33 +000032#include "llvm/MC/MCContext.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000033#include "llvm/MC/MCStreamer.h"
JF Bastienb6091df2015-08-25 22:58:05 +000034#include "llvm/MC/MCSymbol.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000035#include "llvm/Support/Debug.h"
36#include "llvm/Support/TargetRegistry.h"
37#include "llvm/Support/raw_ostream.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000038using namespace llvm;
39
40#define DEBUG_TYPE "asm-printer"
41
42namespace {
43
44class WebAssemblyAsmPrinter final : public AsmPrinter {
JF Bastien1d20a5e2015-10-16 00:53:49 +000045 const MachineRegisterInfo *MRI;
Dan Gohmancf4748f2015-11-12 17:04:33 +000046 const WebAssemblyFunctionInfo *MFI;
JF Bastien600aee92015-07-31 17:53:38 +000047
JF Bastienb9073fb2015-07-22 21:28:15 +000048public:
49 WebAssemblyAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
Dan Gohmancf4748f2015-11-12 17:04:33 +000050 : AsmPrinter(TM, std::move(Streamer)), MRI(nullptr), MFI(nullptr) {}
JF Bastienb9073fb2015-07-22 21:28:15 +000051
52private:
53 const char *getPassName() const override {
54 return "WebAssembly Assembly Printer";
55 }
56
57 //===------------------------------------------------------------------===//
58 // MachineFunctionPass Implementation.
59 //===------------------------------------------------------------------===//
60
JF Bastien600aee92015-07-31 17:53:38 +000061 bool runOnMachineFunction(MachineFunction &MF) override {
JF Bastien1d20a5e2015-10-16 00:53:49 +000062 MRI = &MF.getRegInfo();
Dan Gohmancf4748f2015-11-12 17:04:33 +000063 MFI = MF.getInfo<WebAssemblyFunctionInfo>();
JF Bastien600aee92015-07-31 17:53:38 +000064 return AsmPrinter::runOnMachineFunction(MF);
JF Bastienb9073fb2015-07-22 21:28:15 +000065 }
66
67 //===------------------------------------------------------------------===//
68 // AsmPrinter Implementation.
69 //===------------------------------------------------------------------===//
70
Dan Gohman950a13c2015-09-16 16:51:30 +000071 void EmitJumpTableInfo() override;
JF Bastien54be3b12015-08-25 23:19:49 +000072 void EmitConstantPool() override;
JF Bastienb6091df2015-08-25 22:58:05 +000073 void EmitFunctionBodyStart() override;
JF Bastienb9073fb2015-07-22 21:28:15 +000074 void EmitInstruction(const MachineInstr *MI) override;
Dan Gohmanf19ed562015-11-13 01:42:29 +000075 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
76 unsigned AsmVariant, const char *ExtraCode,
77 raw_ostream &OS) override;
78 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
79 unsigned AsmVariant, const char *ExtraCode,
80 raw_ostream &OS) override;
Dan Gohman979840d2015-09-23 16:59:10 +000081
Dan Gohman53828fd2015-11-23 16:50:18 +000082 MVT getRegType(unsigned RegNo) const;
Dan Gohman754cd112015-11-11 01:33:02 +000083 const char *toString(MVT VT) const;
JF Bastien1d20a5e2015-10-16 00:53:49 +000084 std::string regToString(const MachineOperand &MO);
JF Bastienb9073fb2015-07-22 21:28:15 +000085};
86
87} // end anonymous namespace
88
89//===----------------------------------------------------------------------===//
JF Bastien45479f62015-08-26 22:09:54 +000090// Helpers.
91//===----------------------------------------------------------------------===//
JF Bastienb9073fb2015-07-22 21:28:15 +000092
Dan Gohman53828fd2015-11-23 16:50:18 +000093MVT WebAssemblyAsmPrinter::getRegType(unsigned RegNo) const {
Derek Schuff83717cc2015-12-16 20:43:08 +000094 const TargetRegisterClass *TRC =
95 TargetRegisterInfo::isVirtualRegister(RegNo) ?
96 MRI->getRegClass(RegNo) :
97 MRI->getTargetRegisterInfo()->getMinimalPhysRegClass(RegNo);
JF Bastien1d20a5e2015-10-16 00:53:49 +000098 for (MVT T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64})
99 if (TRC->hasType(T))
Dan Gohman53828fd2015-11-23 16:50:18 +0000100 return T;
JF Bastien1d20a5e2015-10-16 00:53:49 +0000101 DEBUG(errs() << "Unknown type for register number: " << RegNo);
102 llvm_unreachable("Unknown register type");
Dan Gohman53828fd2015-11-23 16:50:18 +0000103 return MVT::Other;
JF Bastien1d20a5e2015-10-16 00:53:49 +0000104}
105
JF Bastien1d20a5e2015-10-16 00:53:49 +0000106std::string WebAssemblyAsmPrinter::regToString(const MachineOperand &MO) {
107 unsigned RegNo = MO.getReg();
Dan Gohmand9625272015-11-20 03:13:31 +0000108 assert(TargetRegisterInfo::isVirtualRegister(RegNo) &&
109 "Unlowered physical register encountered during assembly printing");
Dan Gohman4ba48162015-11-18 16:12:01 +0000110 assert(!MFI->isVRegStackified(RegNo));
Dan Gohman058fce52015-11-13 00:21:05 +0000111 unsigned WAReg = MFI->getWAReg(RegNo);
112 assert(WAReg != WebAssemblyFunctionInfo::UnusedReg);
Dan Gohman4ba48162015-11-18 16:12:01 +0000113 return '$' + utostr(WAReg);
Dan Gohmane51c0582015-10-06 00:27:55 +0000114}
115
Dan Gohman754cd112015-11-11 01:33:02 +0000116const char *WebAssemblyAsmPrinter::toString(MVT VT) const {
Dan Gohman5e0886b2015-12-06 19:42:29 +0000117 return WebAssembly::TypeToString(VT);
JF Bastien73ff6af2015-08-31 22:24:11 +0000118}
119
JF Bastien45479f62015-08-26 22:09:54 +0000120//===----------------------------------------------------------------------===//
121// WebAssemblyAsmPrinter Implementation.
122//===----------------------------------------------------------------------===//
123
JF Bastien54be3b12015-08-25 23:19:49 +0000124void WebAssemblyAsmPrinter::EmitConstantPool() {
125 assert(MF->getConstantPool()->getConstants().empty() &&
126 "WebAssembly disables constant pools");
127}
128
Dan Gohman950a13c2015-09-16 16:51:30 +0000129void WebAssemblyAsmPrinter::EmitJumpTableInfo() {
130 // Nothing to do; jump tables are incorporated into the instruction stream.
131}
132
Dan Gohman7a6b9822015-11-29 22:32:02 +0000133static void ComputeLegalValueVTs(const Function &F, const TargetMachine &TM,
134 Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
135 const DataLayout &DL(F.getParent()->getDataLayout());
Derek Schuff46e33162015-11-16 21:12:41 +0000136 const WebAssemblyTargetLowering &TLI =
137 *TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering();
138 SmallVector<EVT, 4> VTs;
139 ComputeValueVTs(TLI, DL, Ty, VTs);
140
141 for (EVT VT : VTs) {
142 unsigned NumRegs = TLI.getNumRegisters(F.getContext(), VT);
143 MVT RegisterVT = TLI.getRegisterType(F.getContext(), VT);
144 for (unsigned i = 0; i != NumRegs; ++i)
145 ValueVTs.push_back(RegisterVT);
146 }
147}
148
JF Bastienb6091df2015-08-25 22:58:05 +0000149void WebAssemblyAsmPrinter::EmitFunctionBodyStart() {
Dan Gohman53828fd2015-11-23 16:50:18 +0000150 if (!MFI->getParams().empty()) {
151 MCInst Param;
152 Param.setOpcode(WebAssembly::PARAM);
153 for (MVT VT : MFI->getParams())
154 Param.addOperand(MCOperand::createImm(VT.SimpleTy));
155 EmitToStreamer(*OutStreamer, Param);
156 }
Dan Gohmane51c0582015-10-06 00:27:55 +0000157
Derek Schuff46e33162015-11-16 21:12:41 +0000158 SmallVector<MVT, 4> ResultVTs;
159 const Function &F(*MF->getFunction());
160 ComputeLegalValueVTs(F, TM, F.getReturnType(), ResultVTs);
161 // If the return type needs to be legalized it will get converted into
162 // passing a pointer.
Dan Gohman53828fd2015-11-23 16:50:18 +0000163 if (ResultVTs.size() == 1) {
164 MCInst Result;
165 Result.setOpcode(WebAssembly::RESULT);
166 Result.addOperand(MCOperand::createImm(ResultVTs.front().SimpleTy));
167 EmitToStreamer(*OutStreamer, Result);
168 }
JF Bastienb6091df2015-08-25 22:58:05 +0000169
Dan Gohman53828fd2015-11-23 16:50:18 +0000170 bool AnyWARegs = false;
171 MCInst Local;
172 Local.setOpcode(WebAssembly::LOCAL);
JF Bastien1d20a5e2015-10-16 00:53:49 +0000173 for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) {
174 unsigned VReg = TargetRegisterInfo::index2VirtReg(Idx);
Dan Gohman4ba48162015-11-18 16:12:01 +0000175 unsigned WAReg = MFI->getWAReg(VReg);
176 // Don't declare unused registers.
177 if (WAReg == WebAssemblyFunctionInfo::UnusedReg)
178 continue;
179 // Don't redeclare parameters.
180 if (WAReg < MFI->getParams().size())
181 continue;
182 // Don't declare stackified registers.
183 if (int(WAReg) < 0)
184 continue;
Dan Gohman53828fd2015-11-23 16:50:18 +0000185 Local.addOperand(MCOperand::createImm(getRegType(VReg).SimpleTy));
186 AnyWARegs = true;
JF Bastien1d20a5e2015-10-16 00:53:49 +0000187 }
Derek Schuff83717cc2015-12-16 20:43:08 +0000188 auto &PhysRegs = MFI->getPhysRegs();
189 for (unsigned PReg = 0; PReg < PhysRegs.size(); ++PReg) {
190 if (PhysRegs[PReg] == -1U)
191 continue;
192 Local.addOperand(MCOperand::createImm(getRegType(PReg).SimpleTy));
Derek Schuff45cd5a72015-12-16 20:43:06 +0000193 AnyWARegs = true;
194 }
Dan Gohman53828fd2015-11-23 16:50:18 +0000195 if (AnyWARegs)
196 EmitToStreamer(*OutStreamer, Local);
JF Bastien1d20a5e2015-10-16 00:53:49 +0000197
Dan Gohmane51c0582015-10-06 00:27:55 +0000198 AsmPrinter::EmitFunctionBodyStart();
JF Bastienb6091df2015-08-25 22:58:05 +0000199}
200
JF Bastienb9073fb2015-07-22 21:28:15 +0000201void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
JF Bastienaf111db2015-08-24 22:16:48 +0000202 DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
JF Bastienb9073fb2015-07-22 21:28:15 +0000203
Dan Gohmane51c0582015-10-06 00:27:55 +0000204 switch (MI->getOpcode()) {
Dan Gohmane51c0582015-10-06 00:27:55 +0000205 case WebAssembly::ARGUMENT_I32:
206 case WebAssembly::ARGUMENT_I64:
207 case WebAssembly::ARGUMENT_F32:
208 case WebAssembly::ARGUMENT_F64:
Dan Gohmancf4748f2015-11-12 17:04:33 +0000209 // These represent values which are live into the function entry, so there's
210 // no instruction to emit.
Dan Gohmane51c0582015-10-06 00:27:55 +0000211 break;
Dan Gohmanf6857222015-11-23 19:12:37 +0000212 case WebAssembly::LOOP_END:
213 // This is a no-op which just exists to tell AsmPrinter.cpp that there's a
214 // fallthrough which nevertheless requires a label for the destination here.
215 break;
Dan Gohmane51c0582015-10-06 00:27:55 +0000216 default: {
Dan Gohmancf4748f2015-11-12 17:04:33 +0000217 WebAssemblyMCInstLower MCInstLowering(OutContext, *this);
218 MCInst TmpInst;
219 MCInstLowering.Lower(MI, TmpInst);
220 EmitToStreamer(*OutStreamer, TmpInst);
Dan Gohmane51c0582015-10-06 00:27:55 +0000221 break;
222 }
Dan Gohman4f52e002015-09-09 00:52:47 +0000223 }
JF Bastienb9073fb2015-07-22 21:28:15 +0000224}
225
Dan Gohmanf19ed562015-11-13 01:42:29 +0000226bool WebAssemblyAsmPrinter::PrintAsmOperand(const MachineInstr *MI,
227 unsigned OpNo, unsigned AsmVariant,
228 const char *ExtraCode,
229 raw_ostream &OS) {
230 if (AsmVariant != 0)
231 report_fatal_error("There are no defined alternate asm variants");
232
Dan Gohman30a42bf2015-12-16 17:15:17 +0000233 // First try the generic code, which knows about modifiers like 'c' and 'n'.
234 if (!AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS))
235 return false;
236
Dan Gohmanf19ed562015-11-13 01:42:29 +0000237 if (!ExtraCode) {
238 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohman30a42bf2015-12-16 17:15:17 +0000239 switch (MO.getType()) {
240 case MachineOperand::MO_Immediate:
Dan Gohmanf19ed562015-11-13 01:42:29 +0000241 OS << MO.getImm();
Dan Gohman30a42bf2015-12-16 17:15:17 +0000242 return false;
243 case MachineOperand::MO_Register:
Dan Gohmanf19ed562015-11-13 01:42:29 +0000244 OS << regToString(MO);
Dan Gohman30a42bf2015-12-16 17:15:17 +0000245 return false;
246 case MachineOperand::MO_GlobalAddress:
247 getSymbol(MO.getGlobal())->print(OS, MAI);
248 printOffset(MO.getOffset(), OS);
249 return false;
250 case MachineOperand::MO_ExternalSymbol:
251 GetExternalSymbolSymbol(MO.getSymbolName())->print(OS, MAI);
252 printOffset(MO.getOffset(), OS);
253 return false;
254 case MachineOperand::MO_MachineBasicBlock:
255 MO.getMBB()->getSymbol()->print(OS, MAI);
256 return false;
257 default:
258 break;
259 }
Dan Gohmanf19ed562015-11-13 01:42:29 +0000260 }
261
Dan Gohman30a42bf2015-12-16 17:15:17 +0000262 return true;
Dan Gohmanf19ed562015-11-13 01:42:29 +0000263}
264
265bool WebAssemblyAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
266 unsigned OpNo,
267 unsigned AsmVariant,
268 const char *ExtraCode,
269 raw_ostream &OS) {
270 if (AsmVariant != 0)
271 report_fatal_error("There are no defined alternate asm variants");
272
273 if (!ExtraCode) {
Dan Gohmane2831b42015-12-16 18:14:49 +0000274 // TODO: For now, we just hard-code 0 as the constant offset; teach
275 // SelectInlineAsmMemoryOperand how to do address mode matching.
276 OS << "0(" + regToString(MI->getOperand(OpNo)) + ')';
Dan Gohmanf19ed562015-11-13 01:42:29 +0000277 return false;
278 }
279
280 return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
281}
282
JF Bastienb9073fb2015-07-22 21:28:15 +0000283// Force static initialization.
284extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
285 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32);
286 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(TheWebAssemblyTarget64);
287}