blob: e853b76d0dd43183a65e63e0fec732d3d62a9b79 [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 {
JF Bastien1d20a5e2015-10-16 00:53:49 +000094 const TargetRegisterClass *TRC = MRI->getRegClass(RegNo);
95 for (MVT T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64})
96 if (TRC->hasType(T))
Dan Gohman53828fd2015-11-23 16:50:18 +000097 return T;
JF Bastien1d20a5e2015-10-16 00:53:49 +000098 DEBUG(errs() << "Unknown type for register number: " << RegNo);
99 llvm_unreachable("Unknown register type");
Dan Gohman53828fd2015-11-23 16:50:18 +0000100 return MVT::Other;
JF Bastien1d20a5e2015-10-16 00:53:49 +0000101}
102
JF Bastien1d20a5e2015-10-16 00:53:49 +0000103std::string WebAssemblyAsmPrinter::regToString(const MachineOperand &MO) {
104 unsigned RegNo = MO.getReg();
Dan Gohmand9625272015-11-20 03:13:31 +0000105 assert(TargetRegisterInfo::isVirtualRegister(RegNo) &&
106 "Unlowered physical register encountered during assembly printing");
Dan Gohman4ba48162015-11-18 16:12:01 +0000107 assert(!MFI->isVRegStackified(RegNo));
Dan Gohman058fce52015-11-13 00:21:05 +0000108 unsigned WAReg = MFI->getWAReg(RegNo);
109 assert(WAReg != WebAssemblyFunctionInfo::UnusedReg);
Dan Gohman4ba48162015-11-18 16:12:01 +0000110 return '$' + utostr(WAReg);
Dan Gohmane51c0582015-10-06 00:27:55 +0000111}
112
Dan Gohman754cd112015-11-11 01:33:02 +0000113const char *WebAssemblyAsmPrinter::toString(MVT VT) const {
Dan Gohman5e0886b2015-12-06 19:42:29 +0000114 return WebAssembly::TypeToString(VT);
JF Bastien73ff6af2015-08-31 22:24:11 +0000115}
116
JF Bastien45479f62015-08-26 22:09:54 +0000117//===----------------------------------------------------------------------===//
118// WebAssemblyAsmPrinter Implementation.
119//===----------------------------------------------------------------------===//
120
JF Bastien54be3b12015-08-25 23:19:49 +0000121void WebAssemblyAsmPrinter::EmitConstantPool() {
122 assert(MF->getConstantPool()->getConstants().empty() &&
123 "WebAssembly disables constant pools");
124}
125
Dan Gohman950a13c2015-09-16 16:51:30 +0000126void WebAssemblyAsmPrinter::EmitJumpTableInfo() {
127 // Nothing to do; jump tables are incorporated into the instruction stream.
128}
129
Dan Gohman7a6b9822015-11-29 22:32:02 +0000130static void ComputeLegalValueVTs(const Function &F, const TargetMachine &TM,
131 Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
132 const DataLayout &DL(F.getParent()->getDataLayout());
Derek Schuff46e33162015-11-16 21:12:41 +0000133 const WebAssemblyTargetLowering &TLI =
134 *TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering();
135 SmallVector<EVT, 4> VTs;
136 ComputeValueVTs(TLI, DL, Ty, VTs);
137
138 for (EVT VT : VTs) {
139 unsigned NumRegs = TLI.getNumRegisters(F.getContext(), VT);
140 MVT RegisterVT = TLI.getRegisterType(F.getContext(), VT);
141 for (unsigned i = 0; i != NumRegs; ++i)
142 ValueVTs.push_back(RegisterVT);
143 }
144}
145
JF Bastienb6091df2015-08-25 22:58:05 +0000146void WebAssemblyAsmPrinter::EmitFunctionBodyStart() {
Dan Gohman53828fd2015-11-23 16:50:18 +0000147 if (!MFI->getParams().empty()) {
148 MCInst Param;
149 Param.setOpcode(WebAssembly::PARAM);
150 for (MVT VT : MFI->getParams())
151 Param.addOperand(MCOperand::createImm(VT.SimpleTy));
152 EmitToStreamer(*OutStreamer, Param);
153 }
Dan Gohmane51c0582015-10-06 00:27:55 +0000154
Derek Schuff46e33162015-11-16 21:12:41 +0000155 SmallVector<MVT, 4> ResultVTs;
156 const Function &F(*MF->getFunction());
157 ComputeLegalValueVTs(F, TM, F.getReturnType(), ResultVTs);
158 // If the return type needs to be legalized it will get converted into
159 // passing a pointer.
Dan Gohman53828fd2015-11-23 16:50:18 +0000160 if (ResultVTs.size() == 1) {
161 MCInst Result;
162 Result.setOpcode(WebAssembly::RESULT);
163 Result.addOperand(MCOperand::createImm(ResultVTs.front().SimpleTy));
164 EmitToStreamer(*OutStreamer, Result);
165 }
JF Bastienb6091df2015-08-25 22:58:05 +0000166
Dan Gohman53828fd2015-11-23 16:50:18 +0000167 bool AnyWARegs = false;
168 MCInst Local;
169 Local.setOpcode(WebAssembly::LOCAL);
JF Bastien1d20a5e2015-10-16 00:53:49 +0000170 for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) {
171 unsigned VReg = TargetRegisterInfo::index2VirtReg(Idx);
Dan Gohman4ba48162015-11-18 16:12:01 +0000172 unsigned WAReg = MFI->getWAReg(VReg);
173 // Don't declare unused registers.
174 if (WAReg == WebAssemblyFunctionInfo::UnusedReg)
175 continue;
176 // Don't redeclare parameters.
177 if (WAReg < MFI->getParams().size())
178 continue;
179 // Don't declare stackified registers.
180 if (int(WAReg) < 0)
181 continue;
Dan Gohman53828fd2015-11-23 16:50:18 +0000182 Local.addOperand(MCOperand::createImm(getRegType(VReg).SimpleTy));
183 AnyWARegs = true;
JF Bastien1d20a5e2015-10-16 00:53:49 +0000184 }
Derek Schuff45cd5a72015-12-16 20:43:06 +0000185 if (MF->getFrameInfo()->getStackSize() > 0) {
186 // TODO: wasm64
187 Local.addOperand(MCOperand::createImm(MVT::i32));
188 AnyWARegs = true;
189 }
Dan Gohman53828fd2015-11-23 16:50:18 +0000190 if (AnyWARegs)
191 EmitToStreamer(*OutStreamer, Local);
JF Bastien1d20a5e2015-10-16 00:53:49 +0000192
Dan Gohmane51c0582015-10-06 00:27:55 +0000193 AsmPrinter::EmitFunctionBodyStart();
JF Bastienb6091df2015-08-25 22:58:05 +0000194}
195
JF Bastienb9073fb2015-07-22 21:28:15 +0000196void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
JF Bastienaf111db2015-08-24 22:16:48 +0000197 DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
JF Bastienb9073fb2015-07-22 21:28:15 +0000198
Dan Gohmane51c0582015-10-06 00:27:55 +0000199 switch (MI->getOpcode()) {
Dan Gohmane51c0582015-10-06 00:27:55 +0000200 case WebAssembly::ARGUMENT_I32:
201 case WebAssembly::ARGUMENT_I64:
202 case WebAssembly::ARGUMENT_F32:
203 case WebAssembly::ARGUMENT_F64:
Dan Gohmancf4748f2015-11-12 17:04:33 +0000204 // These represent values which are live into the function entry, so there's
205 // no instruction to emit.
Dan Gohmane51c0582015-10-06 00:27:55 +0000206 break;
Dan Gohmanf6857222015-11-23 19:12:37 +0000207 case WebAssembly::LOOP_END:
208 // This is a no-op which just exists to tell AsmPrinter.cpp that there's a
209 // fallthrough which nevertheless requires a label for the destination here.
210 break;
Dan Gohmane51c0582015-10-06 00:27:55 +0000211 default: {
Dan Gohmancf4748f2015-11-12 17:04:33 +0000212 WebAssemblyMCInstLower MCInstLowering(OutContext, *this);
213 MCInst TmpInst;
214 MCInstLowering.Lower(MI, TmpInst);
215 EmitToStreamer(*OutStreamer, TmpInst);
Dan Gohmane51c0582015-10-06 00:27:55 +0000216 break;
217 }
Dan Gohman4f52e002015-09-09 00:52:47 +0000218 }
JF Bastienb9073fb2015-07-22 21:28:15 +0000219}
220
Dan Gohmanf19ed562015-11-13 01:42:29 +0000221bool WebAssemblyAsmPrinter::PrintAsmOperand(const MachineInstr *MI,
222 unsigned OpNo, unsigned AsmVariant,
223 const char *ExtraCode,
224 raw_ostream &OS) {
225 if (AsmVariant != 0)
226 report_fatal_error("There are no defined alternate asm variants");
227
Dan Gohman30a42bf2015-12-16 17:15:17 +0000228 // First try the generic code, which knows about modifiers like 'c' and 'n'.
229 if (!AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS))
230 return false;
231
Dan Gohmanf19ed562015-11-13 01:42:29 +0000232 if (!ExtraCode) {
233 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohman30a42bf2015-12-16 17:15:17 +0000234 switch (MO.getType()) {
235 case MachineOperand::MO_Immediate:
Dan Gohmanf19ed562015-11-13 01:42:29 +0000236 OS << MO.getImm();
Dan Gohman30a42bf2015-12-16 17:15:17 +0000237 return false;
238 case MachineOperand::MO_Register:
Dan Gohmanf19ed562015-11-13 01:42:29 +0000239 OS << regToString(MO);
Dan Gohman30a42bf2015-12-16 17:15:17 +0000240 return false;
241 case MachineOperand::MO_GlobalAddress:
242 getSymbol(MO.getGlobal())->print(OS, MAI);
243 printOffset(MO.getOffset(), OS);
244 return false;
245 case MachineOperand::MO_ExternalSymbol:
246 GetExternalSymbolSymbol(MO.getSymbolName())->print(OS, MAI);
247 printOffset(MO.getOffset(), OS);
248 return false;
249 case MachineOperand::MO_MachineBasicBlock:
250 MO.getMBB()->getSymbol()->print(OS, MAI);
251 return false;
252 default:
253 break;
254 }
Dan Gohmanf19ed562015-11-13 01:42:29 +0000255 }
256
Dan Gohman30a42bf2015-12-16 17:15:17 +0000257 return true;
Dan Gohmanf19ed562015-11-13 01:42:29 +0000258}
259
260bool WebAssemblyAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
261 unsigned OpNo,
262 unsigned AsmVariant,
263 const char *ExtraCode,
264 raw_ostream &OS) {
265 if (AsmVariant != 0)
266 report_fatal_error("There are no defined alternate asm variants");
267
268 if (!ExtraCode) {
Dan Gohmane2831b42015-12-16 18:14:49 +0000269 // TODO: For now, we just hard-code 0 as the constant offset; teach
270 // SelectInlineAsmMemoryOperand how to do address mode matching.
271 OS << "0(" + regToString(MI->getOperand(OpNo)) + ')';
Dan Gohmanf19ed562015-11-13 01:42:29 +0000272 return false;
273 }
274
275 return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
276}
277
JF Bastienb9073fb2015-07-22 21:28:15 +0000278// Force static initialization.
279extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
280 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32);
281 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(TheWebAssemblyTarget64);
282}