blob: 8d84901b3a2f524c6ab094d8c0817e5094afb75a [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"
Dan Gohmane51c0582015-10-06 00:27:55 +000024#include "llvm/ADT/StringExtras.h"
Dan Gohman754cd112015-11-11 01:33:02 +000025#include "llvm/CodeGen/Analysis.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000026#include "llvm/CodeGen/AsmPrinter.h"
JF Bastien54be3b12015-08-25 23:19:49 +000027#include "llvm/CodeGen/MachineConstantPool.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000028#include "llvm/CodeGen/MachineInstr.h"
29#include "llvm/IR/DataLayout.h"
Dan Gohmancf4748f2015-11-12 17:04:33 +000030#include "llvm/MC/MCContext.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000031#include "llvm/MC/MCStreamer.h"
JF Bastienb6091df2015-08-25 22:58:05 +000032#include "llvm/MC/MCSymbol.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000033#include "llvm/Support/Debug.h"
34#include "llvm/Support/TargetRegistry.h"
35#include "llvm/Support/raw_ostream.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000036using namespace llvm;
37
38#define DEBUG_TYPE "asm-printer"
39
40namespace {
41
42class WebAssemblyAsmPrinter final : public AsmPrinter {
JF Bastien1d20a5e2015-10-16 00:53:49 +000043 const MachineRegisterInfo *MRI;
Dan Gohmancf4748f2015-11-12 17:04:33 +000044 const WebAssemblyFunctionInfo *MFI;
JF Bastien600aee92015-07-31 17:53:38 +000045
JF Bastienb9073fb2015-07-22 21:28:15 +000046public:
47 WebAssemblyAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
Dan Gohmancf4748f2015-11-12 17:04:33 +000048 : AsmPrinter(TM, std::move(Streamer)), MRI(nullptr), MFI(nullptr) {}
JF Bastienb9073fb2015-07-22 21:28:15 +000049
50private:
51 const char *getPassName() const override {
52 return "WebAssembly Assembly Printer";
53 }
54
55 //===------------------------------------------------------------------===//
56 // MachineFunctionPass Implementation.
57 //===------------------------------------------------------------------===//
58
JF Bastien600aee92015-07-31 17:53:38 +000059 bool runOnMachineFunction(MachineFunction &MF) override {
JF Bastien1d20a5e2015-10-16 00:53:49 +000060 MRI = &MF.getRegInfo();
Dan Gohmancf4748f2015-11-12 17:04:33 +000061 MFI = MF.getInfo<WebAssemblyFunctionInfo>();
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
Dan Gohman950a13c2015-09-16 16:51:30 +000069 void EmitJumpTableInfo() override;
JF Bastien54be3b12015-08-25 23:19:49 +000070 void EmitConstantPool() override;
JF Bastienb6091df2015-08-25 22:58:05 +000071 void EmitFunctionBodyStart() override;
JF Bastienb9073fb2015-07-22 21:28:15 +000072 void EmitInstruction(const MachineInstr *MI) override;
Dan Gohman26c67652016-01-11 23:38:05 +000073 const MCExpr *lowerConstant(const Constant *CV) 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 {
Derek Schuff83717cc2015-12-16 20:43:08 +000093 const TargetRegisterClass *TRC =
Dan Gohman35e4a282016-01-08 01:06:00 +000094 TargetRegisterInfo::isVirtualRegister(RegNo)
95 ? MRI->getRegClass(RegNo)
96 : MRI->getTargetRegisterInfo()->getMinimalPhysRegClass(RegNo);
JF Bastien1d20a5e2015-10-16 00:53:49 +000097 for (MVT T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64})
98 if (TRC->hasType(T))
Dan Gohman53828fd2015-11-23 16:50:18 +000099 return T;
JF Bastien1d20a5e2015-10-16 00:53:49 +0000100 DEBUG(errs() << "Unknown type for register number: " << RegNo);
101 llvm_unreachable("Unknown register type");
Dan Gohman53828fd2015-11-23 16:50:18 +0000102 return MVT::Other;
JF Bastien1d20a5e2015-10-16 00:53:49 +0000103}
104
JF Bastien1d20a5e2015-10-16 00:53:49 +0000105std::string WebAssemblyAsmPrinter::regToString(const MachineOperand &MO) {
106 unsigned RegNo = MO.getReg();
Dan Gohmand9625272015-11-20 03:13:31 +0000107 assert(TargetRegisterInfo::isVirtualRegister(RegNo) &&
108 "Unlowered physical register encountered during assembly printing");
Dan Gohman4ba48162015-11-18 16:12:01 +0000109 assert(!MFI->isVRegStackified(RegNo));
Dan Gohman058fce52015-11-13 00:21:05 +0000110 unsigned WAReg = MFI->getWAReg(RegNo);
111 assert(WAReg != WebAssemblyFunctionInfo::UnusedReg);
Dan Gohman4ba48162015-11-18 16:12:01 +0000112 return '$' + utostr(WAReg);
Dan Gohmane51c0582015-10-06 00:27:55 +0000113}
114
Dan Gohman754cd112015-11-11 01:33:02 +0000115const char *WebAssemblyAsmPrinter::toString(MVT VT) const {
Dan Gohman5e0886b2015-12-06 19:42:29 +0000116 return WebAssembly::TypeToString(VT);
JF Bastien73ff6af2015-08-31 22:24:11 +0000117}
118
JF Bastien45479f62015-08-26 22:09:54 +0000119//===----------------------------------------------------------------------===//
120// WebAssemblyAsmPrinter Implementation.
121//===----------------------------------------------------------------------===//
122
JF Bastien54be3b12015-08-25 23:19:49 +0000123void WebAssemblyAsmPrinter::EmitConstantPool() {
124 assert(MF->getConstantPool()->getConstants().empty() &&
125 "WebAssembly disables constant pools");
126}
127
Dan Gohman950a13c2015-09-16 16:51:30 +0000128void WebAssemblyAsmPrinter::EmitJumpTableInfo() {
129 // Nothing to do; jump tables are incorporated into the instruction stream.
130}
131
Dan Gohman7a6b9822015-11-29 22:32:02 +0000132static void ComputeLegalValueVTs(const Function &F, const TargetMachine &TM,
133 Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
134 const DataLayout &DL(F.getParent()->getDataLayout());
Derek Schuff46e33162015-11-16 21:12:41 +0000135 const WebAssemblyTargetLowering &TLI =
136 *TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering();
137 SmallVector<EVT, 4> VTs;
138 ComputeValueVTs(TLI, DL, Ty, VTs);
139
140 for (EVT VT : VTs) {
141 unsigned NumRegs = TLI.getNumRegisters(F.getContext(), VT);
142 MVT RegisterVT = TLI.getRegisterType(F.getContext(), VT);
143 for (unsigned i = 0; i != NumRegs; ++i)
144 ValueVTs.push_back(RegisterVT);
145 }
146}
147
JF Bastienb6091df2015-08-25 22:58:05 +0000148void WebAssemblyAsmPrinter::EmitFunctionBodyStart() {
Dan Gohman53828fd2015-11-23 16:50:18 +0000149 if (!MFI->getParams().empty()) {
150 MCInst Param;
151 Param.setOpcode(WebAssembly::PARAM);
152 for (MVT VT : MFI->getParams())
153 Param.addOperand(MCOperand::createImm(VT.SimpleTy));
154 EmitToStreamer(*OutStreamer, Param);
155 }
Dan Gohmane51c0582015-10-06 00:27:55 +0000156
Derek Schuff46e33162015-11-16 21:12:41 +0000157 SmallVector<MVT, 4> ResultVTs;
158 const Function &F(*MF->getFunction());
159 ComputeLegalValueVTs(F, TM, F.getReturnType(), ResultVTs);
160 // If the return type needs to be legalized it will get converted into
161 // passing a pointer.
Dan Gohman53828fd2015-11-23 16:50:18 +0000162 if (ResultVTs.size() == 1) {
163 MCInst Result;
164 Result.setOpcode(WebAssembly::RESULT);
165 Result.addOperand(MCOperand::createImm(ResultVTs.front().SimpleTy));
166 EmitToStreamer(*OutStreamer, Result);
167 }
JF Bastienb6091df2015-08-25 22:58:05 +0000168
Dan Gohman53828fd2015-11-23 16:50:18 +0000169 bool AnyWARegs = false;
170 MCInst Local;
171 Local.setOpcode(WebAssembly::LOCAL);
JF Bastien1d20a5e2015-10-16 00:53:49 +0000172 for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) {
173 unsigned VReg = TargetRegisterInfo::index2VirtReg(Idx);
Dan Gohman4ba48162015-11-18 16:12:01 +0000174 unsigned WAReg = MFI->getWAReg(VReg);
175 // Don't declare unused registers.
176 if (WAReg == WebAssemblyFunctionInfo::UnusedReg)
177 continue;
178 // Don't redeclare parameters.
179 if (WAReg < MFI->getParams().size())
180 continue;
181 // Don't declare stackified registers.
182 if (int(WAReg) < 0)
183 continue;
Dan Gohman53828fd2015-11-23 16:50:18 +0000184 Local.addOperand(MCOperand::createImm(getRegType(VReg).SimpleTy));
185 AnyWARegs = true;
JF Bastien1d20a5e2015-10-16 00:53:49 +0000186 }
Derek Schuff83717cc2015-12-16 20:43:08 +0000187 auto &PhysRegs = MFI->getPhysRegs();
188 for (unsigned PReg = 0; PReg < PhysRegs.size(); ++PReg) {
189 if (PhysRegs[PReg] == -1U)
190 continue;
191 Local.addOperand(MCOperand::createImm(getRegType(PReg).SimpleTy));
Derek Schuff45cd5a72015-12-16 20:43:06 +0000192 AnyWARegs = true;
193 }
Dan Gohman53828fd2015-11-23 16:50:18 +0000194 if (AnyWARegs)
195 EmitToStreamer(*OutStreamer, Local);
JF Bastien1d20a5e2015-10-16 00:53:49 +0000196
Dan Gohmane51c0582015-10-06 00:27:55 +0000197 AsmPrinter::EmitFunctionBodyStart();
JF Bastienb6091df2015-08-25 22:58:05 +0000198}
199
JF Bastienb9073fb2015-07-22 21:28:15 +0000200void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
JF Bastienaf111db2015-08-24 22:16:48 +0000201 DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
JF Bastienb9073fb2015-07-22 21:28:15 +0000202
Dan Gohmane51c0582015-10-06 00:27:55 +0000203 switch (MI->getOpcode()) {
Dan Gohmane51c0582015-10-06 00:27:55 +0000204 case WebAssembly::ARGUMENT_I32:
205 case WebAssembly::ARGUMENT_I64:
206 case WebAssembly::ARGUMENT_F32:
207 case WebAssembly::ARGUMENT_F64:
Dan Gohmancf4748f2015-11-12 17:04:33 +0000208 // These represent values which are live into the function entry, so there's
209 // no instruction to emit.
Dan Gohmane51c0582015-10-06 00:27:55 +0000210 break;
Dan Gohmanf6857222015-11-23 19:12:37 +0000211 case WebAssembly::LOOP_END:
212 // This is a no-op which just exists to tell AsmPrinter.cpp that there's a
213 // fallthrough which nevertheless requires a label for the destination here.
214 break;
Dan Gohmane51c0582015-10-06 00:27:55 +0000215 default: {
Dan Gohmancf4748f2015-11-12 17:04:33 +0000216 WebAssemblyMCInstLower MCInstLowering(OutContext, *this);
217 MCInst TmpInst;
218 MCInstLowering.Lower(MI, TmpInst);
219 EmitToStreamer(*OutStreamer, TmpInst);
Dan Gohmane51c0582015-10-06 00:27:55 +0000220 break;
221 }
Dan Gohman4f52e002015-09-09 00:52:47 +0000222 }
JF Bastienb9073fb2015-07-22 21:28:15 +0000223}
224
Dan Gohman26c67652016-01-11 23:38:05 +0000225const MCExpr *WebAssemblyAsmPrinter::lowerConstant(const Constant *CV) {
226 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
227 if (GV->getValueType()->isFunctionTy())
228 return MCSymbolRefExpr::create(
229 getSymbol(GV), MCSymbolRefExpr::VK_WebAssembly_FUNCTION, OutContext);
230 return AsmPrinter::lowerConstant(CV);
231}
232
Dan Gohmanf19ed562015-11-13 01:42:29 +0000233bool WebAssemblyAsmPrinter::PrintAsmOperand(const MachineInstr *MI,
234 unsigned OpNo, unsigned AsmVariant,
235 const char *ExtraCode,
236 raw_ostream &OS) {
237 if (AsmVariant != 0)
238 report_fatal_error("There are no defined alternate asm variants");
239
Dan Gohman30a42bf2015-12-16 17:15:17 +0000240 // First try the generic code, which knows about modifiers like 'c' and 'n'.
241 if (!AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS))
242 return false;
243
Dan Gohmanf19ed562015-11-13 01:42:29 +0000244 if (!ExtraCode) {
245 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohman30a42bf2015-12-16 17:15:17 +0000246 switch (MO.getType()) {
247 case MachineOperand::MO_Immediate:
Dan Gohmanf19ed562015-11-13 01:42:29 +0000248 OS << MO.getImm();
Dan Gohman30a42bf2015-12-16 17:15:17 +0000249 return false;
250 case MachineOperand::MO_Register:
Dan Gohmanf19ed562015-11-13 01:42:29 +0000251 OS << regToString(MO);
Dan Gohman30a42bf2015-12-16 17:15:17 +0000252 return false;
253 case MachineOperand::MO_GlobalAddress:
254 getSymbol(MO.getGlobal())->print(OS, MAI);
255 printOffset(MO.getOffset(), OS);
256 return false;
257 case MachineOperand::MO_ExternalSymbol:
258 GetExternalSymbolSymbol(MO.getSymbolName())->print(OS, MAI);
259 printOffset(MO.getOffset(), OS);
260 return false;
261 case MachineOperand::MO_MachineBasicBlock:
262 MO.getMBB()->getSymbol()->print(OS, MAI);
263 return false;
264 default:
265 break;
266 }
Dan Gohmanf19ed562015-11-13 01:42:29 +0000267 }
268
Dan Gohman30a42bf2015-12-16 17:15:17 +0000269 return true;
Dan Gohmanf19ed562015-11-13 01:42:29 +0000270}
271
272bool WebAssemblyAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
273 unsigned OpNo,
274 unsigned AsmVariant,
275 const char *ExtraCode,
276 raw_ostream &OS) {
277 if (AsmVariant != 0)
278 report_fatal_error("There are no defined alternate asm variants");
279
280 if (!ExtraCode) {
Dan Gohmane2831b42015-12-16 18:14:49 +0000281 // TODO: For now, we just hard-code 0 as the constant offset; teach
282 // SelectInlineAsmMemoryOperand how to do address mode matching.
283 OS << "0(" + regToString(MI->getOperand(OpNo)) + ')';
Dan Gohmanf19ed562015-11-13 01:42:29 +0000284 return false;
285 }
286
287 return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
288}
289
JF Bastienb9073fb2015-07-22 21:28:15 +0000290// Force static initialization.
291extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
292 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32);
293 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(TheWebAssemblyTarget64);
294}