blob: 509573e47c3f1237864c12ac3866354878486912 [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"
Dan Gohman3469ee12016-01-12 20:30:51 +000020#include "MCTargetDesc/WebAssemblyTargetStreamer.h"
Dan Gohmancf4748f2015-11-12 17:04:33 +000021#include "WebAssemblyMCInstLower.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000022#include "WebAssemblyMachineFunctionInfo.h"
23#include "WebAssemblyRegisterInfo.h"
24#include "WebAssemblySubtarget.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;
Dan Gohman3469ee12016-01-12 20:30:51 +000073 void EmitFunctionBodyEnd() override;
JF Bastienb9073fb2015-07-22 21:28:15 +000074 void EmitInstruction(const MachineInstr *MI) override;
Dan Gohman26c67652016-01-11 23:38:05 +000075 const MCExpr *lowerConstant(const Constant *CV) override;
Dan Gohmanf19ed562015-11-13 01:42:29 +000076 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
77 unsigned AsmVariant, const char *ExtraCode,
78 raw_ostream &OS) override;
79 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
80 unsigned AsmVariant, const char *ExtraCode,
81 raw_ostream &OS) override;
Dan Gohman979840d2015-09-23 16:59:10 +000082
Dan Gohman53828fd2015-11-23 16:50:18 +000083 MVT getRegType(unsigned RegNo) const;
Dan Gohman754cd112015-11-11 01:33:02 +000084 const char *toString(MVT VT) const;
JF Bastien1d20a5e2015-10-16 00:53:49 +000085 std::string regToString(const MachineOperand &MO);
Dan Gohman3469ee12016-01-12 20:30:51 +000086 WebAssemblyTargetStreamer *getTargetStreamer();
JF Bastienb9073fb2015-07-22 21:28:15 +000087};
88
89} // end anonymous namespace
90
91//===----------------------------------------------------------------------===//
JF Bastien45479f62015-08-26 22:09:54 +000092// Helpers.
93//===----------------------------------------------------------------------===//
JF Bastienb9073fb2015-07-22 21:28:15 +000094
Dan Gohman53828fd2015-11-23 16:50:18 +000095MVT WebAssemblyAsmPrinter::getRegType(unsigned RegNo) const {
Derek Schuff83717cc2015-12-16 20:43:08 +000096 const TargetRegisterClass *TRC =
Dan Gohman35e4a282016-01-08 01:06:00 +000097 TargetRegisterInfo::isVirtualRegister(RegNo)
98 ? MRI->getRegClass(RegNo)
99 : MRI->getTargetRegisterInfo()->getMinimalPhysRegClass(RegNo);
JF Bastien1d20a5e2015-10-16 00:53:49 +0000100 for (MVT T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64})
101 if (TRC->hasType(T))
Dan Gohman53828fd2015-11-23 16:50:18 +0000102 return T;
JF Bastien1d20a5e2015-10-16 00:53:49 +0000103 DEBUG(errs() << "Unknown type for register number: " << RegNo);
104 llvm_unreachable("Unknown register type");
Dan Gohman53828fd2015-11-23 16:50:18 +0000105 return MVT::Other;
JF Bastien1d20a5e2015-10-16 00:53:49 +0000106}
107
Dan Gohman3469ee12016-01-12 20:30:51 +0000108const char *WebAssemblyAsmPrinter::toString(MVT VT) const {
109 return WebAssembly::TypeToString(VT);
110}
111
JF Bastien1d20a5e2015-10-16 00:53:49 +0000112std::string WebAssemblyAsmPrinter::regToString(const MachineOperand &MO) {
113 unsigned RegNo = MO.getReg();
Dan Gohmand9625272015-11-20 03:13:31 +0000114 assert(TargetRegisterInfo::isVirtualRegister(RegNo) &&
115 "Unlowered physical register encountered during assembly printing");
Dan Gohman4ba48162015-11-18 16:12:01 +0000116 assert(!MFI->isVRegStackified(RegNo));
Dan Gohman058fce52015-11-13 00:21:05 +0000117 unsigned WAReg = MFI->getWAReg(RegNo);
118 assert(WAReg != WebAssemblyFunctionInfo::UnusedReg);
Dan Gohman4ba48162015-11-18 16:12:01 +0000119 return '$' + utostr(WAReg);
Dan Gohmane51c0582015-10-06 00:27:55 +0000120}
121
Dan Gohmanec977b02016-01-25 15:12:05 +0000122WebAssemblyTargetStreamer *WebAssemblyAsmPrinter::getTargetStreamer() {
Dan Gohman3469ee12016-01-12 20:30:51 +0000123 MCTargetStreamer *TS = OutStreamer->getTargetStreamer();
124 return static_cast<WebAssemblyTargetStreamer *>(TS);
JF Bastien73ff6af2015-08-31 22:24:11 +0000125}
126
JF Bastien45479f62015-08-26 22:09:54 +0000127//===----------------------------------------------------------------------===//
128// WebAssemblyAsmPrinter Implementation.
129//===----------------------------------------------------------------------===//
130
JF Bastien54be3b12015-08-25 23:19:49 +0000131void WebAssemblyAsmPrinter::EmitConstantPool() {
132 assert(MF->getConstantPool()->getConstants().empty() &&
133 "WebAssembly disables constant pools");
134}
135
Dan Gohman950a13c2015-09-16 16:51:30 +0000136void WebAssemblyAsmPrinter::EmitJumpTableInfo() {
137 // Nothing to do; jump tables are incorporated into the instruction stream.
138}
139
Dan Gohman7a6b9822015-11-29 22:32:02 +0000140static void ComputeLegalValueVTs(const Function &F, const TargetMachine &TM,
141 Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
142 const DataLayout &DL(F.getParent()->getDataLayout());
Derek Schuff46e33162015-11-16 21:12:41 +0000143 const WebAssemblyTargetLowering &TLI =
144 *TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering();
145 SmallVector<EVT, 4> VTs;
146 ComputeValueVTs(TLI, DL, Ty, VTs);
147
148 for (EVT VT : VTs) {
149 unsigned NumRegs = TLI.getNumRegisters(F.getContext(), VT);
150 MVT RegisterVT = TLI.getRegisterType(F.getContext(), VT);
151 for (unsigned i = 0; i != NumRegs; ++i)
152 ValueVTs.push_back(RegisterVT);
153 }
154}
155
JF Bastienb6091df2015-08-25 22:58:05 +0000156void WebAssemblyAsmPrinter::EmitFunctionBodyStart() {
Dan Gohman3469ee12016-01-12 20:30:51 +0000157 if (!MFI->getParams().empty())
158 getTargetStreamer()->emitParam(MFI->getParams());
Dan Gohmane51c0582015-10-06 00:27:55 +0000159
Derek Schuff46e33162015-11-16 21:12:41 +0000160 SmallVector<MVT, 4> ResultVTs;
161 const Function &F(*MF->getFunction());
162 ComputeLegalValueVTs(F, TM, F.getReturnType(), ResultVTs);
Dan Gohman3469ee12016-01-12 20:30:51 +0000163
Derek Schuff46e33162015-11-16 21:12:41 +0000164 // If the return type needs to be legalized it will get converted into
165 // passing a pointer.
Dan Gohman3469ee12016-01-12 20:30:51 +0000166 if (ResultVTs.size() == 1)
167 getTargetStreamer()->emitResult(ResultVTs);
JF Bastienb6091df2015-08-25 22:58:05 +0000168
Dan Gohman53828fd2015-11-23 16:50:18 +0000169 bool AnyWARegs = false;
Dan Gohman3469ee12016-01-12 20:30:51 +0000170 SmallVector<MVT, 16> LocalTypes;
JF Bastien1d20a5e2015-10-16 00:53:49 +0000171 for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) {
172 unsigned VReg = TargetRegisterInfo::index2VirtReg(Idx);
Dan Gohman4ba48162015-11-18 16:12:01 +0000173 unsigned WAReg = MFI->getWAReg(VReg);
174 // Don't declare unused registers.
175 if (WAReg == WebAssemblyFunctionInfo::UnusedReg)
176 continue;
177 // Don't redeclare parameters.
178 if (WAReg < MFI->getParams().size())
179 continue;
180 // Don't declare stackified registers.
181 if (int(WAReg) < 0)
182 continue;
Dan Gohman3469ee12016-01-12 20:30:51 +0000183 LocalTypes.push_back(getRegType(VReg));
Dan Gohman53828fd2015-11-23 16:50:18 +0000184 AnyWARegs = true;
JF Bastien1d20a5e2015-10-16 00:53:49 +0000185 }
Derek Schuff83717cc2015-12-16 20:43:08 +0000186 auto &PhysRegs = MFI->getPhysRegs();
187 for (unsigned PReg = 0; PReg < PhysRegs.size(); ++PReg) {
188 if (PhysRegs[PReg] == -1U)
189 continue;
Dan Gohman3469ee12016-01-12 20:30:51 +0000190 LocalTypes.push_back(getRegType(PReg));
Derek Schuff45cd5a72015-12-16 20:43:06 +0000191 AnyWARegs = true;
192 }
Dan Gohman53828fd2015-11-23 16:50:18 +0000193 if (AnyWARegs)
Dan Gohman3469ee12016-01-12 20:30:51 +0000194 getTargetStreamer()->emitLocal(LocalTypes);
JF Bastien1d20a5e2015-10-16 00:53:49 +0000195
Dan Gohmane51c0582015-10-06 00:27:55 +0000196 AsmPrinter::EmitFunctionBodyStart();
JF Bastienb6091df2015-08-25 22:58:05 +0000197}
198
Dan Gohman3469ee12016-01-12 20:30:51 +0000199void WebAssemblyAsmPrinter::EmitFunctionBodyEnd() {
200 getTargetStreamer()->emitEndFunc();
201}
202
JF Bastienb9073fb2015-07-22 21:28:15 +0000203void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
JF Bastienaf111db2015-08-24 22:16:48 +0000204 DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
JF Bastienb9073fb2015-07-22 21:28:15 +0000205
Dan Gohmane51c0582015-10-06 00:27:55 +0000206 switch (MI->getOpcode()) {
Dan Gohmane51c0582015-10-06 00:27:55 +0000207 case WebAssembly::ARGUMENT_I32:
208 case WebAssembly::ARGUMENT_I64:
209 case WebAssembly::ARGUMENT_F32:
210 case WebAssembly::ARGUMENT_F64:
Dan Gohmancf4748f2015-11-12 17:04:33 +0000211 // These represent values which are live into the function entry, so there's
212 // no instruction to emit.
Dan Gohmane51c0582015-10-06 00:27:55 +0000213 break;
Dan Gohmane51c0582015-10-06 00:27:55 +0000214 default: {
Dan Gohmancf4748f2015-11-12 17:04:33 +0000215 WebAssemblyMCInstLower MCInstLowering(OutContext, *this);
216 MCInst TmpInst;
217 MCInstLowering.Lower(MI, TmpInst);
218 EmitToStreamer(*OutStreamer, TmpInst);
Dan Gohmane51c0582015-10-06 00:27:55 +0000219 break;
220 }
Dan Gohman4f52e002015-09-09 00:52:47 +0000221 }
JF Bastienb9073fb2015-07-22 21:28:15 +0000222}
223
Dan Gohman26c67652016-01-11 23:38:05 +0000224const MCExpr *WebAssemblyAsmPrinter::lowerConstant(const Constant *CV) {
225 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
226 if (GV->getValueType()->isFunctionTy())
227 return MCSymbolRefExpr::create(
228 getSymbol(GV), MCSymbolRefExpr::VK_WebAssembly_FUNCTION, OutContext);
229 return AsmPrinter::lowerConstant(CV);
230}
231
Dan Gohmanf19ed562015-11-13 01:42:29 +0000232bool WebAssemblyAsmPrinter::PrintAsmOperand(const MachineInstr *MI,
233 unsigned OpNo, unsigned AsmVariant,
234 const char *ExtraCode,
235 raw_ostream &OS) {
236 if (AsmVariant != 0)
237 report_fatal_error("There are no defined alternate asm variants");
238
Dan Gohman30a42bf2015-12-16 17:15:17 +0000239 // First try the generic code, which knows about modifiers like 'c' and 'n'.
240 if (!AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS))
241 return false;
242
Dan Gohmanf19ed562015-11-13 01:42:29 +0000243 if (!ExtraCode) {
244 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohman30a42bf2015-12-16 17:15:17 +0000245 switch (MO.getType()) {
246 case MachineOperand::MO_Immediate:
Dan Gohmanf19ed562015-11-13 01:42:29 +0000247 OS << MO.getImm();
Dan Gohman30a42bf2015-12-16 17:15:17 +0000248 return false;
249 case MachineOperand::MO_Register:
Dan Gohmanf19ed562015-11-13 01:42:29 +0000250 OS << regToString(MO);
Dan Gohman30a42bf2015-12-16 17:15:17 +0000251 return false;
252 case MachineOperand::MO_GlobalAddress:
253 getSymbol(MO.getGlobal())->print(OS, MAI);
254 printOffset(MO.getOffset(), OS);
255 return false;
256 case MachineOperand::MO_ExternalSymbol:
257 GetExternalSymbolSymbol(MO.getSymbolName())->print(OS, MAI);
258 printOffset(MO.getOffset(), OS);
259 return false;
260 case MachineOperand::MO_MachineBasicBlock:
261 MO.getMBB()->getSymbol()->print(OS, MAI);
262 return false;
263 default:
264 break;
265 }
Dan Gohmanf19ed562015-11-13 01:42:29 +0000266 }
267
Dan Gohman30a42bf2015-12-16 17:15:17 +0000268 return true;
Dan Gohmanf19ed562015-11-13 01:42:29 +0000269}
270
271bool WebAssemblyAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
272 unsigned OpNo,
273 unsigned AsmVariant,
274 const char *ExtraCode,
275 raw_ostream &OS) {
276 if (AsmVariant != 0)
277 report_fatal_error("There are no defined alternate asm variants");
278
279 if (!ExtraCode) {
Dan Gohmane2831b42015-12-16 18:14:49 +0000280 // TODO: For now, we just hard-code 0 as the constant offset; teach
281 // SelectInlineAsmMemoryOperand how to do address mode matching.
282 OS << "0(" + regToString(MI->getOperand(OpNo)) + ')';
Dan Gohmanf19ed562015-11-13 01:42:29 +0000283 return false;
284 }
285
286 return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
287}
288
JF Bastienb9073fb2015-07-22 21:28:15 +0000289// Force static initialization.
290extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
291 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32);
292 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(TheWebAssemblyTarget64);
293}