blob: 64128bf031ead94e098fd4d852ffc924c5862f42 [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 {
Dan Gohman0cfb5f82016-05-10 04:24:02 +000096 const TargetRegisterClass *TRC = MRI->getRegClass(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
Dan Gohman3469ee12016-01-12 20:30:51 +0000105const char *WebAssemblyAsmPrinter::toString(MVT VT) const {
106 return WebAssembly::TypeToString(VT);
107}
108
JF Bastien1d20a5e2015-10-16 00:53:49 +0000109std::string WebAssemblyAsmPrinter::regToString(const MachineOperand &MO) {
110 unsigned RegNo = MO.getReg();
Dan Gohmand9625272015-11-20 03:13:31 +0000111 assert(TargetRegisterInfo::isVirtualRegister(RegNo) &&
112 "Unlowered physical register encountered during assembly printing");
Dan Gohman4ba48162015-11-18 16:12:01 +0000113 assert(!MFI->isVRegStackified(RegNo));
Dan Gohman058fce52015-11-13 00:21:05 +0000114 unsigned WAReg = MFI->getWAReg(RegNo);
115 assert(WAReg != WebAssemblyFunctionInfo::UnusedReg);
Dan Gohman4ba48162015-11-18 16:12:01 +0000116 return '$' + utostr(WAReg);
Dan Gohmane51c0582015-10-06 00:27:55 +0000117}
118
Dan Gohmanec977b02016-01-25 15:12:05 +0000119WebAssemblyTargetStreamer *WebAssemblyAsmPrinter::getTargetStreamer() {
Dan Gohman3469ee12016-01-12 20:30:51 +0000120 MCTargetStreamer *TS = OutStreamer->getTargetStreamer();
121 return static_cast<WebAssemblyTargetStreamer *>(TS);
JF Bastien73ff6af2015-08-31 22:24:11 +0000122}
123
JF Bastien45479f62015-08-26 22:09:54 +0000124//===----------------------------------------------------------------------===//
125// WebAssemblyAsmPrinter Implementation.
126//===----------------------------------------------------------------------===//
127
JF Bastien54be3b12015-08-25 23:19:49 +0000128void WebAssemblyAsmPrinter::EmitConstantPool() {
129 assert(MF->getConstantPool()->getConstants().empty() &&
130 "WebAssembly disables constant pools");
131}
132
Dan Gohman950a13c2015-09-16 16:51:30 +0000133void WebAssemblyAsmPrinter::EmitJumpTableInfo() {
134 // Nothing to do; jump tables are incorporated into the instruction stream.
135}
136
Dan Gohman7a6b9822015-11-29 22:32:02 +0000137static void ComputeLegalValueVTs(const Function &F, const TargetMachine &TM,
138 Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
139 const DataLayout &DL(F.getParent()->getDataLayout());
Derek Schuff46e33162015-11-16 21:12:41 +0000140 const WebAssemblyTargetLowering &TLI =
141 *TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering();
142 SmallVector<EVT, 4> VTs;
143 ComputeValueVTs(TLI, DL, Ty, VTs);
144
145 for (EVT VT : VTs) {
146 unsigned NumRegs = TLI.getNumRegisters(F.getContext(), VT);
147 MVT RegisterVT = TLI.getRegisterType(F.getContext(), VT);
148 for (unsigned i = 0; i != NumRegs; ++i)
149 ValueVTs.push_back(RegisterVT);
150 }
151}
152
JF Bastienb6091df2015-08-25 22:58:05 +0000153void WebAssemblyAsmPrinter::EmitFunctionBodyStart() {
Dan Gohman3469ee12016-01-12 20:30:51 +0000154 if (!MFI->getParams().empty())
155 getTargetStreamer()->emitParam(MFI->getParams());
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);
Dan Gohman3469ee12016-01-12 20:30:51 +0000160
Derek Schuff46e33162015-11-16 21:12:41 +0000161 // If the return type needs to be legalized it will get converted into
162 // passing a pointer.
Dan Gohman3469ee12016-01-12 20:30:51 +0000163 if (ResultVTs.size() == 1)
164 getTargetStreamer()->emitResult(ResultVTs);
JF Bastienb6091df2015-08-25 22:58:05 +0000165
Dan Gohman53828fd2015-11-23 16:50:18 +0000166 bool AnyWARegs = false;
Dan Gohman3469ee12016-01-12 20:30:51 +0000167 SmallVector<MVT, 16> LocalTypes;
JF Bastien1d20a5e2015-10-16 00:53:49 +0000168 for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) {
169 unsigned VReg = TargetRegisterInfo::index2VirtReg(Idx);
Dan Gohman4ba48162015-11-18 16:12:01 +0000170 unsigned WAReg = MFI->getWAReg(VReg);
171 // Don't declare unused registers.
172 if (WAReg == WebAssemblyFunctionInfo::UnusedReg)
173 continue;
174 // Don't redeclare parameters.
175 if (WAReg < MFI->getParams().size())
176 continue;
177 // Don't declare stackified registers.
178 if (int(WAReg) < 0)
179 continue;
Dan Gohman3469ee12016-01-12 20:30:51 +0000180 LocalTypes.push_back(getRegType(VReg));
Dan Gohman53828fd2015-11-23 16:50:18 +0000181 AnyWARegs = true;
JF Bastien1d20a5e2015-10-16 00:53:49 +0000182 }
Dan Gohman53828fd2015-11-23 16:50:18 +0000183 if (AnyWARegs)
Dan Gohman3469ee12016-01-12 20:30:51 +0000184 getTargetStreamer()->emitLocal(LocalTypes);
JF Bastien1d20a5e2015-10-16 00:53:49 +0000185
Dan Gohmane51c0582015-10-06 00:27:55 +0000186 AsmPrinter::EmitFunctionBodyStart();
JF Bastienb6091df2015-08-25 22:58:05 +0000187}
188
Dan Gohman3469ee12016-01-12 20:30:51 +0000189void WebAssemblyAsmPrinter::EmitFunctionBodyEnd() {
190 getTargetStreamer()->emitEndFunc();
191}
192
JF Bastienb9073fb2015-07-22 21:28:15 +0000193void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
JF Bastienaf111db2015-08-24 22:16:48 +0000194 DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
JF Bastienb9073fb2015-07-22 21:28:15 +0000195
Dan Gohmane51c0582015-10-06 00:27:55 +0000196 switch (MI->getOpcode()) {
Dan Gohmane51c0582015-10-06 00:27:55 +0000197 case WebAssembly::ARGUMENT_I32:
198 case WebAssembly::ARGUMENT_I64:
199 case WebAssembly::ARGUMENT_F32:
200 case WebAssembly::ARGUMENT_F64:
Dan Gohmancf4748f2015-11-12 17:04:33 +0000201 // These represent values which are live into the function entry, so there's
202 // no instruction to emit.
Dan Gohmane51c0582015-10-06 00:27:55 +0000203 break;
Dan Gohmanb7c24002016-05-21 00:21:56 +0000204 case WebAssembly::FALLTHROUGH_RETURN_I32:
205 case WebAssembly::FALLTHROUGH_RETURN_I64:
206 case WebAssembly::FALLTHROUGH_RETURN_F32:
207 case WebAssembly::FALLTHROUGH_RETURN_F64: {
208 // These instructions represent the implicit return at the end of a
209 // function body. The operand is always a pop.
210 assert(MFI->isVRegStackified(MI->getOperand(0).getReg()));
211
212 if (isVerbose()) {
213 OutStreamer->AddComment("fallthrough-return: $pop" +
214 utostr(MFI->getWARegStackId(
215 MFI->getWAReg(MI->getOperand(0).getReg()))));
216 OutStreamer->AddBlankLine();
217 }
218 break;
219 }
220 case WebAssembly::FALLTHROUGH_RETURN_VOID:
221 // This instruction represents the implicit return at the end of a
222 // function body with no return value.
223 if (isVerbose()) {
224 OutStreamer->AddComment("fallthrough-return");
225 OutStreamer->AddBlankLine();
226 }
227 break;
Dan Gohmane51c0582015-10-06 00:27:55 +0000228 default: {
Dan Gohmancf4748f2015-11-12 17:04:33 +0000229 WebAssemblyMCInstLower MCInstLowering(OutContext, *this);
230 MCInst TmpInst;
231 MCInstLowering.Lower(MI, TmpInst);
232 EmitToStreamer(*OutStreamer, TmpInst);
Dan Gohmane51c0582015-10-06 00:27:55 +0000233 break;
234 }
Dan Gohman4f52e002015-09-09 00:52:47 +0000235 }
JF Bastienb9073fb2015-07-22 21:28:15 +0000236}
237
Dan Gohman26c67652016-01-11 23:38:05 +0000238const MCExpr *WebAssemblyAsmPrinter::lowerConstant(const Constant *CV) {
239 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
240 if (GV->getValueType()->isFunctionTy())
241 return MCSymbolRefExpr::create(
242 getSymbol(GV), MCSymbolRefExpr::VK_WebAssembly_FUNCTION, OutContext);
243 return AsmPrinter::lowerConstant(CV);
244}
245
Dan Gohmanf19ed562015-11-13 01:42:29 +0000246bool WebAssemblyAsmPrinter::PrintAsmOperand(const MachineInstr *MI,
247 unsigned OpNo, unsigned AsmVariant,
248 const char *ExtraCode,
249 raw_ostream &OS) {
250 if (AsmVariant != 0)
251 report_fatal_error("There are no defined alternate asm variants");
252
Dan Gohman30a42bf2015-12-16 17:15:17 +0000253 // First try the generic code, which knows about modifiers like 'c' and 'n'.
254 if (!AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS))
255 return false;
256
Dan Gohmanf19ed562015-11-13 01:42:29 +0000257 if (!ExtraCode) {
258 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohman30a42bf2015-12-16 17:15:17 +0000259 switch (MO.getType()) {
260 case MachineOperand::MO_Immediate:
Dan Gohmanf19ed562015-11-13 01:42:29 +0000261 OS << MO.getImm();
Dan Gohman30a42bf2015-12-16 17:15:17 +0000262 return false;
263 case MachineOperand::MO_Register:
Dan Gohmanf19ed562015-11-13 01:42:29 +0000264 OS << regToString(MO);
Dan Gohman30a42bf2015-12-16 17:15:17 +0000265 return false;
266 case MachineOperand::MO_GlobalAddress:
267 getSymbol(MO.getGlobal())->print(OS, MAI);
268 printOffset(MO.getOffset(), OS);
269 return false;
270 case MachineOperand::MO_ExternalSymbol:
271 GetExternalSymbolSymbol(MO.getSymbolName())->print(OS, MAI);
272 printOffset(MO.getOffset(), OS);
273 return false;
274 case MachineOperand::MO_MachineBasicBlock:
275 MO.getMBB()->getSymbol()->print(OS, MAI);
276 return false;
277 default:
278 break;
279 }
Dan Gohmanf19ed562015-11-13 01:42:29 +0000280 }
281
Dan Gohman30a42bf2015-12-16 17:15:17 +0000282 return true;
Dan Gohmanf19ed562015-11-13 01:42:29 +0000283}
284
285bool WebAssemblyAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
286 unsigned OpNo,
287 unsigned AsmVariant,
288 const char *ExtraCode,
289 raw_ostream &OS) {
290 if (AsmVariant != 0)
291 report_fatal_error("There are no defined alternate asm variants");
292
293 if (!ExtraCode) {
Dan Gohmane2831b42015-12-16 18:14:49 +0000294 // TODO: For now, we just hard-code 0 as the constant offset; teach
295 // SelectInlineAsmMemoryOperand how to do address mode matching.
296 OS << "0(" + regToString(MI->getOperand(OpNo)) + ')';
Dan Gohmanf19ed562015-11-13 01:42:29 +0000297 return false;
298 }
299
300 return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
301}
302
JF Bastienb9073fb2015-07-22 21:28:15 +0000303// Force static initialization.
304extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
305 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32);
306 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(TheWebAssemblyTarget64);
307}