blob: 78945757ba046460cc196608ed5a000789e05929 [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
Dan Gohmancf4748f2015-11-12 17:04:33 +000017#include "InstPrinter/WebAssemblyInstPrinter.h"
18#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
Dan Gohman3469ee12016-01-12 20:30:51 +000019#include "MCTargetDesc/WebAssemblyTargetStreamer.h"
Derek Schuffc64d7652016-08-01 22:25:02 +000020#include "WebAssembly.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
Derek Schuff5859a9ed2016-06-03 18:34:36 +000070 void EmitEndOfAsmFile(Module &M) override;
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;
Dan Gohman3469ee12016-01-12 20:30:51 +000074 void EmitFunctionBodyEnd() override;
JF Bastienb9073fb2015-07-22 21:28:15 +000075 void EmitInstruction(const MachineInstr *MI) override;
Dan Gohman26c67652016-01-11 23:38:05 +000076 const MCExpr *lowerConstant(const Constant *CV) override;
Dan Gohmanf19ed562015-11-13 01:42:29 +000077 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
78 unsigned AsmVariant, const char *ExtraCode,
79 raw_ostream &OS) override;
80 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
81 unsigned AsmVariant, const char *ExtraCode,
82 raw_ostream &OS) override;
Dan Gohman979840d2015-09-23 16:59:10 +000083
Dan Gohman53828fd2015-11-23 16:50:18 +000084 MVT getRegType(unsigned RegNo) const;
Dan Gohman754cd112015-11-11 01:33:02 +000085 const char *toString(MVT VT) const;
JF Bastien1d20a5e2015-10-16 00:53:49 +000086 std::string regToString(const MachineOperand &MO);
Dan Gohman3469ee12016-01-12 20:30:51 +000087 WebAssemblyTargetStreamer *getTargetStreamer();
JF Bastienb9073fb2015-07-22 21:28:15 +000088};
89
90} // end anonymous namespace
91
92//===----------------------------------------------------------------------===//
JF Bastien45479f62015-08-26 22:09:54 +000093// Helpers.
94//===----------------------------------------------------------------------===//
JF Bastienb9073fb2015-07-22 21:28:15 +000095
Dan Gohman53828fd2015-11-23 16:50:18 +000096MVT WebAssemblyAsmPrinter::getRegType(unsigned RegNo) const {
Dan Gohman0cfb5f82016-05-10 04:24:02 +000097 const TargetRegisterClass *TRC = MRI->getRegClass(RegNo);
Derek Schuff39bf39f2016-08-02 23:16:09 +000098 for (MVT T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64, MVT::v16i8, MVT::v8i16,
99 MVT::v4i32, MVT::v4f32})
JF Bastien1d20a5e2015-10-16 00:53:49 +0000100 if (TRC->hasType(T))
Dan Gohman53828fd2015-11-23 16:50:18 +0000101 return T;
JF Bastien1d20a5e2015-10-16 00:53:49 +0000102 DEBUG(errs() << "Unknown type for register number: " << RegNo);
103 llvm_unreachable("Unknown register type");
Dan Gohman53828fd2015-11-23 16:50:18 +0000104 return MVT::Other;
JF Bastien1d20a5e2015-10-16 00:53:49 +0000105}
106
Dan Gohman3469ee12016-01-12 20:30:51 +0000107const char *WebAssemblyAsmPrinter::toString(MVT VT) const {
108 return WebAssembly::TypeToString(VT);
109}
110
JF Bastien1d20a5e2015-10-16 00:53:49 +0000111std::string WebAssemblyAsmPrinter::regToString(const MachineOperand &MO) {
112 unsigned RegNo = MO.getReg();
Dan Gohmand9625272015-11-20 03:13:31 +0000113 assert(TargetRegisterInfo::isVirtualRegister(RegNo) &&
114 "Unlowered physical register encountered during assembly printing");
Dan Gohman4ba48162015-11-18 16:12:01 +0000115 assert(!MFI->isVRegStackified(RegNo));
Dan Gohman058fce52015-11-13 00:21:05 +0000116 unsigned WAReg = MFI->getWAReg(RegNo);
117 assert(WAReg != WebAssemblyFunctionInfo::UnusedReg);
Dan Gohman4ba48162015-11-18 16:12:01 +0000118 return '$' + utostr(WAReg);
Dan Gohmane51c0582015-10-06 00:27:55 +0000119}
120
Dan Gohmanec977b02016-01-25 15:12:05 +0000121WebAssemblyTargetStreamer *WebAssemblyAsmPrinter::getTargetStreamer() {
Dan Gohman3469ee12016-01-12 20:30:51 +0000122 MCTargetStreamer *TS = OutStreamer->getTargetStreamer();
123 return static_cast<WebAssemblyTargetStreamer *>(TS);
JF Bastien73ff6af2015-08-31 22:24:11 +0000124}
125
JF Bastien45479f62015-08-26 22:09:54 +0000126//===----------------------------------------------------------------------===//
127// WebAssemblyAsmPrinter Implementation.
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
Derek Schuff5859a9ed2016-06-03 18:34:36 +0000145void WebAssemblyAsmPrinter::EmitEndOfAsmFile(Module &M) {
146 for (const auto &F : M) {
147 // Emit function type info for all undefined functions
148 if (F.isDeclarationForLinker() && !F.isIntrinsic()) {
149 SmallVector<MVT, 4> SignatureVTs;
150 ComputeLegalValueVTs(F, TM, F.getReturnType(), SignatureVTs);
151 size_t NumResults = SignatureVTs.size();
152 if (SignatureVTs.size() > 1) {
153 // WebAssembly currently can't lower returns of multiple values without
154 // demoting to sret (see WebAssemblyTargetLowering::CanLowerReturn). So
155 // replace multiple return values with a pointer parameter.
156 SignatureVTs.clear();
157 SignatureVTs.push_back(
158 MVT::getIntegerVT(M.getDataLayout().getPointerSizeInBits()));
159 NumResults = 0;
160 }
161
162 for (auto &Arg : F.args()) {
163 ComputeLegalValueVTs(F, TM, Arg.getType(), SignatureVTs);
164 }
165
166 getTargetStreamer()->emitIndirectFunctionType(F.getName(), SignatureVTs,
167 NumResults);
168 }
169 }
170}
171
172void WebAssemblyAsmPrinter::EmitConstantPool() {
173 assert(MF->getConstantPool()->getConstants().empty() &&
174 "WebAssembly disables constant pools");
175}
176
177void WebAssemblyAsmPrinter::EmitJumpTableInfo() {
178 // Nothing to do; jump tables are incorporated into the instruction stream.
179}
180
JF Bastienb6091df2015-08-25 22:58:05 +0000181void WebAssemblyAsmPrinter::EmitFunctionBodyStart() {
Dan Gohman3469ee12016-01-12 20:30:51 +0000182 if (!MFI->getParams().empty())
183 getTargetStreamer()->emitParam(MFI->getParams());
Dan Gohmane51c0582015-10-06 00:27:55 +0000184
Derek Schuff46e33162015-11-16 21:12:41 +0000185 SmallVector<MVT, 4> ResultVTs;
186 const Function &F(*MF->getFunction());
Derek Schuffc64d7652016-08-01 22:25:02 +0000187
188 // Emit the function index.
189 if (MDNode *Idx = F.getMetadata("wasm.index")) {
190 assert(Idx->getNumOperands() == 1);
191
192 getTargetStreamer()->emitIndIdx(AsmPrinter::lowerConstant(
193 cast<ConstantAsMetadata>(Idx->getOperand(0))->getValue()));
194 }
195
Derek Schuff46e33162015-11-16 21:12:41 +0000196 ComputeLegalValueVTs(F, TM, F.getReturnType(), ResultVTs);
Dan Gohman3469ee12016-01-12 20:30:51 +0000197
Derek Schuff46e33162015-11-16 21:12:41 +0000198 // If the return type needs to be legalized it will get converted into
199 // passing a pointer.
Dan Gohman3469ee12016-01-12 20:30:51 +0000200 if (ResultVTs.size() == 1)
201 getTargetStreamer()->emitResult(ResultVTs);
JF Bastienb6091df2015-08-25 22:58:05 +0000202
Dan Gohman53828fd2015-11-23 16:50:18 +0000203 bool AnyWARegs = false;
Dan Gohman3469ee12016-01-12 20:30:51 +0000204 SmallVector<MVT, 16> LocalTypes;
JF Bastien1d20a5e2015-10-16 00:53:49 +0000205 for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) {
206 unsigned VReg = TargetRegisterInfo::index2VirtReg(Idx);
Dan Gohman4ba48162015-11-18 16:12:01 +0000207 unsigned WAReg = MFI->getWAReg(VReg);
208 // Don't declare unused registers.
209 if (WAReg == WebAssemblyFunctionInfo::UnusedReg)
210 continue;
211 // Don't redeclare parameters.
212 if (WAReg < MFI->getParams().size())
213 continue;
214 // Don't declare stackified registers.
215 if (int(WAReg) < 0)
216 continue;
Dan Gohman3469ee12016-01-12 20:30:51 +0000217 LocalTypes.push_back(getRegType(VReg));
Dan Gohman53828fd2015-11-23 16:50:18 +0000218 AnyWARegs = true;
JF Bastien1d20a5e2015-10-16 00:53:49 +0000219 }
Dan Gohman53828fd2015-11-23 16:50:18 +0000220 if (AnyWARegs)
Dan Gohman3469ee12016-01-12 20:30:51 +0000221 getTargetStreamer()->emitLocal(LocalTypes);
JF Bastien1d20a5e2015-10-16 00:53:49 +0000222
Dan Gohmane51c0582015-10-06 00:27:55 +0000223 AsmPrinter::EmitFunctionBodyStart();
JF Bastienb6091df2015-08-25 22:58:05 +0000224}
225
Dan Gohman3469ee12016-01-12 20:30:51 +0000226void WebAssemblyAsmPrinter::EmitFunctionBodyEnd() {
227 getTargetStreamer()->emitEndFunc();
228}
229
JF Bastienb9073fb2015-07-22 21:28:15 +0000230void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
JF Bastienaf111db2015-08-24 22:16:48 +0000231 DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
JF Bastienb9073fb2015-07-22 21:28:15 +0000232
Dan Gohmane51c0582015-10-06 00:27:55 +0000233 switch (MI->getOpcode()) {
Dan Gohmane51c0582015-10-06 00:27:55 +0000234 case WebAssembly::ARGUMENT_I32:
235 case WebAssembly::ARGUMENT_I64:
236 case WebAssembly::ARGUMENT_F32:
237 case WebAssembly::ARGUMENT_F64:
Derek Schuff39bf39f2016-08-02 23:16:09 +0000238 case WebAssembly::ARGUMENT_v16i8:
239 case WebAssembly::ARGUMENT_v8i16:
240 case WebAssembly::ARGUMENT_v4i32:
241 case WebAssembly::ARGUMENT_v4f32:
Dan Gohmancf4748f2015-11-12 17:04:33 +0000242 // These represent values which are live into the function entry, so there's
243 // no instruction to emit.
Dan Gohmane51c0582015-10-06 00:27:55 +0000244 break;
Dan Gohmanb7c24002016-05-21 00:21:56 +0000245 case WebAssembly::FALLTHROUGH_RETURN_I32:
246 case WebAssembly::FALLTHROUGH_RETURN_I64:
247 case WebAssembly::FALLTHROUGH_RETURN_F32:
Derek Schuff39bf39f2016-08-02 23:16:09 +0000248 case WebAssembly::FALLTHROUGH_RETURN_F64:
249 case WebAssembly::FALLTHROUGH_RETURN_v16i8:
250 case WebAssembly::FALLTHROUGH_RETURN_v8i16:
251 case WebAssembly::FALLTHROUGH_RETURN_v4i32:
252 case WebAssembly::FALLTHROUGH_RETURN_v4f32: {
Dan Gohmanb7c24002016-05-21 00:21:56 +0000253 // These instructions represent the implicit return at the end of a
254 // function body. The operand is always a pop.
255 assert(MFI->isVRegStackified(MI->getOperand(0).getReg()));
256
257 if (isVerbose()) {
258 OutStreamer->AddComment("fallthrough-return: $pop" +
259 utostr(MFI->getWARegStackId(
260 MFI->getWAReg(MI->getOperand(0).getReg()))));
261 OutStreamer->AddBlankLine();
262 }
263 break;
264 }
265 case WebAssembly::FALLTHROUGH_RETURN_VOID:
266 // This instruction represents the implicit return at the end of a
267 // function body with no return value.
268 if (isVerbose()) {
269 OutStreamer->AddComment("fallthrough-return");
270 OutStreamer->AddBlankLine();
271 }
272 break;
Dan Gohmane51c0582015-10-06 00:27:55 +0000273 default: {
Dan Gohmancf4748f2015-11-12 17:04:33 +0000274 WebAssemblyMCInstLower MCInstLowering(OutContext, *this);
275 MCInst TmpInst;
276 MCInstLowering.Lower(MI, TmpInst);
277 EmitToStreamer(*OutStreamer, TmpInst);
Dan Gohmane51c0582015-10-06 00:27:55 +0000278 break;
279 }
Dan Gohman4f52e002015-09-09 00:52:47 +0000280 }
JF Bastienb9073fb2015-07-22 21:28:15 +0000281}
282
Dan Gohman26c67652016-01-11 23:38:05 +0000283const MCExpr *WebAssemblyAsmPrinter::lowerConstant(const Constant *CV) {
284 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
285 if (GV->getValueType()->isFunctionTy())
286 return MCSymbolRefExpr::create(
287 getSymbol(GV), MCSymbolRefExpr::VK_WebAssembly_FUNCTION, OutContext);
288 return AsmPrinter::lowerConstant(CV);
289}
290
Dan Gohmanf19ed562015-11-13 01:42:29 +0000291bool WebAssemblyAsmPrinter::PrintAsmOperand(const MachineInstr *MI,
292 unsigned OpNo, unsigned AsmVariant,
293 const char *ExtraCode,
294 raw_ostream &OS) {
295 if (AsmVariant != 0)
296 report_fatal_error("There are no defined alternate asm variants");
297
Dan Gohman30a42bf2015-12-16 17:15:17 +0000298 // First try the generic code, which knows about modifiers like 'c' and 'n'.
299 if (!AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS))
300 return false;
301
Dan Gohmanf19ed562015-11-13 01:42:29 +0000302 if (!ExtraCode) {
303 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohman30a42bf2015-12-16 17:15:17 +0000304 switch (MO.getType()) {
305 case MachineOperand::MO_Immediate:
Dan Gohmanf19ed562015-11-13 01:42:29 +0000306 OS << MO.getImm();
Dan Gohman30a42bf2015-12-16 17:15:17 +0000307 return false;
308 case MachineOperand::MO_Register:
Dan Gohmanf19ed562015-11-13 01:42:29 +0000309 OS << regToString(MO);
Dan Gohman30a42bf2015-12-16 17:15:17 +0000310 return false;
311 case MachineOperand::MO_GlobalAddress:
312 getSymbol(MO.getGlobal())->print(OS, MAI);
313 printOffset(MO.getOffset(), OS);
314 return false;
315 case MachineOperand::MO_ExternalSymbol:
316 GetExternalSymbolSymbol(MO.getSymbolName())->print(OS, MAI);
317 printOffset(MO.getOffset(), OS);
318 return false;
319 case MachineOperand::MO_MachineBasicBlock:
320 MO.getMBB()->getSymbol()->print(OS, MAI);
321 return false;
322 default:
323 break;
324 }
Dan Gohmanf19ed562015-11-13 01:42:29 +0000325 }
326
Dan Gohman30a42bf2015-12-16 17:15:17 +0000327 return true;
Dan Gohmanf19ed562015-11-13 01:42:29 +0000328}
329
330bool WebAssemblyAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
331 unsigned OpNo,
332 unsigned AsmVariant,
333 const char *ExtraCode,
334 raw_ostream &OS) {
335 if (AsmVariant != 0)
336 report_fatal_error("There are no defined alternate asm variants");
337
338 if (!ExtraCode) {
Dan Gohmane2831b42015-12-16 18:14:49 +0000339 // TODO: For now, we just hard-code 0 as the constant offset; teach
340 // SelectInlineAsmMemoryOperand how to do address mode matching.
341 OS << "0(" + regToString(MI->getOperand(OpNo)) + ')';
Dan Gohmanf19ed562015-11-13 01:42:29 +0000342 return false;
343 }
344
345 return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
346}
347
JF Bastienb9073fb2015-07-22 21:28:15 +0000348// Force static initialization.
349extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
350 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32);
351 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(TheWebAssemblyTarget64);
352}