blob: 40eed593f07e386a438c84698f9ec60073ed786f [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 Gohmand934cb82017-02-24 23:18:00 +000017#include "WebAssemblyAsmPrinter.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"
Derek Schuffc64d7652016-08-01 22:25:02 +000021#include "WebAssembly.h"
Dan Gohmancf4748f2015-11-12 17:04:33 +000022#include "WebAssemblyMCInstLower.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000023#include "WebAssemblyMachineFunctionInfo.h"
24#include "WebAssemblyRegisterInfo.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"
Dan Gohman82607f52017-02-24 23:46:05 +000030#include "llvm/CodeGen/MachineModuleInfoImpls.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000031#include "llvm/IR/DataLayout.h"
Dan Gohmand934cb82017-02-24 23:18:00 +000032#include "llvm/IR/GlobalVariable.h"
Dan Gohmancf4748f2015-11-12 17:04:33 +000033#include "llvm/MC/MCContext.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000034#include "llvm/MC/MCStreamer.h"
JF Bastienb6091df2015-08-25 22:58:05 +000035#include "llvm/MC/MCSymbol.h"
Sam Clegg7736855d2017-06-13 01:42:21 +000036#include "llvm/MC/MCSymbolWasm.h"
37#include "llvm/MC/MCSymbolELF.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000038#include "llvm/Support/Debug.h"
39#include "llvm/Support/TargetRegistry.h"
40#include "llvm/Support/raw_ostream.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000041using namespace llvm;
42
43#define DEBUG_TYPE "asm-printer"
44
JF Bastienb9073fb2015-07-22 21:28:15 +000045//===----------------------------------------------------------------------===//
JF Bastien45479f62015-08-26 22:09:54 +000046// Helpers.
47//===----------------------------------------------------------------------===//
JF Bastienb9073fb2015-07-22 21:28:15 +000048
Dan Gohman53828fd2015-11-23 16:50:18 +000049MVT WebAssemblyAsmPrinter::getRegType(unsigned RegNo) const {
Krzysztof Parzyszekc8e8e2a2017-04-24 19:51:12 +000050 const TargetRegisterInfo *TRI = Subtarget->getRegisterInfo();
Dan Gohman0cfb5f82016-05-10 04:24:02 +000051 const TargetRegisterClass *TRC = MRI->getRegClass(RegNo);
Derek Schuff39bf39f2016-08-02 23:16:09 +000052 for (MVT T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64, MVT::v16i8, MVT::v8i16,
53 MVT::v4i32, MVT::v4f32})
Krzysztof Parzyszekc8e8e2a2017-04-24 19:51:12 +000054 if (TRI->isTypeLegalForClass(*TRC, T))
Dan Gohman53828fd2015-11-23 16:50:18 +000055 return T;
JF Bastien1d20a5e2015-10-16 00:53:49 +000056 DEBUG(errs() << "Unknown type for register number: " << RegNo);
57 llvm_unreachable("Unknown register type");
Dan Gohman53828fd2015-11-23 16:50:18 +000058 return MVT::Other;
JF Bastien1d20a5e2015-10-16 00:53:49 +000059}
60
JF Bastien1d20a5e2015-10-16 00:53:49 +000061std::string WebAssemblyAsmPrinter::regToString(const MachineOperand &MO) {
62 unsigned RegNo = MO.getReg();
Dan Gohmand9625272015-11-20 03:13:31 +000063 assert(TargetRegisterInfo::isVirtualRegister(RegNo) &&
64 "Unlowered physical register encountered during assembly printing");
Dan Gohman4ba48162015-11-18 16:12:01 +000065 assert(!MFI->isVRegStackified(RegNo));
Dan Gohman058fce52015-11-13 00:21:05 +000066 unsigned WAReg = MFI->getWAReg(RegNo);
67 assert(WAReg != WebAssemblyFunctionInfo::UnusedReg);
Dan Gohman4ba48162015-11-18 16:12:01 +000068 return '$' + utostr(WAReg);
Dan Gohmane51c0582015-10-06 00:27:55 +000069}
70
Dan Gohmanec977b02016-01-25 15:12:05 +000071WebAssemblyTargetStreamer *WebAssemblyAsmPrinter::getTargetStreamer() {
Dan Gohman3469ee12016-01-12 20:30:51 +000072 MCTargetStreamer *TS = OutStreamer->getTargetStreamer();
73 return static_cast<WebAssemblyTargetStreamer *>(TS);
JF Bastien73ff6af2015-08-31 22:24:11 +000074}
75
JF Bastien45479f62015-08-26 22:09:54 +000076//===----------------------------------------------------------------------===//
77// WebAssemblyAsmPrinter Implementation.
78//===----------------------------------------------------------------------===//
Derek Schuff46e33162015-11-16 21:12:41 +000079
Derek Schuff5859a9ed2016-06-03 18:34:36 +000080void WebAssemblyAsmPrinter::EmitEndOfAsmFile(Module &M) {
81 for (const auto &F : M) {
82 // Emit function type info for all undefined functions
83 if (F.isDeclarationForLinker() && !F.isIntrinsic()) {
Dan Gohman2726b882016-10-06 22:29:32 +000084 SmallVector<MVT, 4> Results;
85 SmallVector<MVT, 4> Params;
86 ComputeSignatureVTs(F, TM, Params, Results);
Dan Gohmandb1916a2018-02-09 23:13:22 +000087 MCSymbolWasm *Sym = cast<MCSymbolWasm>(getSymbol(&F));
88 getTargetStreamer()->emitIndirectFunctionType(Sym, Params, Results);
89
90 if (F.hasFnAttribute("wasm-import-module")) {
91 StringRef Name = F.getFnAttribute("wasm-import-module")
92 .getValueAsString();
93 getTargetStreamer()->emitImportModule(Sym, Name);
94 }
Derek Schuff5859a9ed2016-06-03 18:34:36 +000095 }
96 }
Derek Schuff7747d703e2016-12-01 00:11:15 +000097 for (const auto &G : M.globals()) {
98 if (!G.hasInitializer() && G.hasExternalLinkage()) {
Dan Gohman5cf64732017-12-07 00:14:30 +000099 if (G.getValueType()->isSized()) {
100 uint16_t Size = M.getDataLayout().getTypeAllocSize(G.getValueType());
101 if (TM.getTargetTriple().isOSBinFormatELF())
102 getTargetStreamer()->emitGlobalImport(G.getGlobalIdentifier());
103 OutStreamer->emitELFSize(getSymbol(&G),
104 MCConstantExpr::create(Size, OutContext));
105 }
Derek Schuff7747d703e2016-12-01 00:11:15 +0000106 }
107 }
Derek Schuff5859a9ed2016-06-03 18:34:36 +0000108}
109
110void WebAssemblyAsmPrinter::EmitConstantPool() {
111 assert(MF->getConstantPool()->getConstants().empty() &&
112 "WebAssembly disables constant pools");
113}
114
115void WebAssemblyAsmPrinter::EmitJumpTableInfo() {
116 // Nothing to do; jump tables are incorporated into the instruction stream.
117}
118
JF Bastienb6091df2015-08-25 22:58:05 +0000119void WebAssemblyAsmPrinter::EmitFunctionBodyStart() {
Dan Gohmand934cb82017-02-24 23:18:00 +0000120 getTargetStreamer()->emitParam(CurrentFnSym, MFI->getParams());
Dan Gohmane51c0582015-10-06 00:27:55 +0000121
Derek Schuff46e33162015-11-16 21:12:41 +0000122 SmallVector<MVT, 4> ResultVTs;
David Blaikie21109242017-12-15 23:52:06 +0000123 const Function &F = MF->getFunction();
Derek Schuffc64d7652016-08-01 22:25:02 +0000124
125 // Emit the function index.
126 if (MDNode *Idx = F.getMetadata("wasm.index")) {
127 assert(Idx->getNumOperands() == 1);
128
129 getTargetStreamer()->emitIndIdx(AsmPrinter::lowerConstant(
130 cast<ConstantAsMetadata>(Idx->getOperand(0))->getValue()));
131 }
132
Derek Schuff46e33162015-11-16 21:12:41 +0000133 ComputeLegalValueVTs(F, TM, F.getReturnType(), ResultVTs);
Dan Gohman3469ee12016-01-12 20:30:51 +0000134
Derek Schuff46e33162015-11-16 21:12:41 +0000135 // If the return type needs to be legalized it will get converted into
136 // passing a pointer.
Dan Gohman3469ee12016-01-12 20:30:51 +0000137 if (ResultVTs.size() == 1)
Dan Gohmand934cb82017-02-24 23:18:00 +0000138 getTargetStreamer()->emitResult(CurrentFnSym, ResultVTs);
139 else
140 getTargetStreamer()->emitResult(CurrentFnSym, ArrayRef<MVT>());
JF Bastienb6091df2015-08-25 22:58:05 +0000141
Dan Gohmand934cb82017-02-24 23:18:00 +0000142 if (TM.getTargetTriple().isOSBinFormatELF()) {
143 assert(MFI->getLocals().empty());
144 for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) {
145 unsigned VReg = TargetRegisterInfo::index2VirtReg(Idx);
146 unsigned WAReg = MFI->getWAReg(VReg);
147 // Don't declare unused registers.
148 if (WAReg == WebAssemblyFunctionInfo::UnusedReg)
149 continue;
150 // Don't redeclare parameters.
151 if (WAReg < MFI->getParams().size())
152 continue;
153 // Don't declare stackified registers.
154 if (int(WAReg) < 0)
155 continue;
156 MFI->addLocal(getRegType(VReg));
157 }
JF Bastien1d20a5e2015-10-16 00:53:49 +0000158 }
Dan Gohman3acb1872016-10-24 23:27:49 +0000159
160 getTargetStreamer()->emitLocal(MFI->getLocals());
JF Bastien1d20a5e2015-10-16 00:53:49 +0000161
Dan Gohmane51c0582015-10-06 00:27:55 +0000162 AsmPrinter::EmitFunctionBodyStart();
JF Bastienb6091df2015-08-25 22:58:05 +0000163}
164
Dan Gohman3469ee12016-01-12 20:30:51 +0000165void WebAssemblyAsmPrinter::EmitFunctionBodyEnd() {
Dan Gohmand934cb82017-02-24 23:18:00 +0000166 if (TM.getTargetTriple().isOSBinFormatELF())
167 getTargetStreamer()->emitEndFunc();
Dan Gohman3469ee12016-01-12 20:30:51 +0000168}
169
JF Bastienb9073fb2015-07-22 21:28:15 +0000170void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
JF Bastienaf111db2015-08-24 22:16:48 +0000171 DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
JF Bastienb9073fb2015-07-22 21:28:15 +0000172
Dan Gohmane51c0582015-10-06 00:27:55 +0000173 switch (MI->getOpcode()) {
Dan Gohmane51c0582015-10-06 00:27:55 +0000174 case WebAssembly::ARGUMENT_I32:
175 case WebAssembly::ARGUMENT_I64:
176 case WebAssembly::ARGUMENT_F32:
177 case WebAssembly::ARGUMENT_F64:
Derek Schuff39bf39f2016-08-02 23:16:09 +0000178 case WebAssembly::ARGUMENT_v16i8:
179 case WebAssembly::ARGUMENT_v8i16:
180 case WebAssembly::ARGUMENT_v4i32:
181 case WebAssembly::ARGUMENT_v4f32:
Dan Gohmancf4748f2015-11-12 17:04:33 +0000182 // These represent values which are live into the function entry, so there's
183 // no instruction to emit.
Dan Gohmane51c0582015-10-06 00:27:55 +0000184 break;
Dan Gohmanb7c24002016-05-21 00:21:56 +0000185 case WebAssembly::FALLTHROUGH_RETURN_I32:
186 case WebAssembly::FALLTHROUGH_RETURN_I64:
187 case WebAssembly::FALLTHROUGH_RETURN_F32:
Derek Schuff39bf39f2016-08-02 23:16:09 +0000188 case WebAssembly::FALLTHROUGH_RETURN_F64:
189 case WebAssembly::FALLTHROUGH_RETURN_v16i8:
190 case WebAssembly::FALLTHROUGH_RETURN_v8i16:
191 case WebAssembly::FALLTHROUGH_RETURN_v4i32:
192 case WebAssembly::FALLTHROUGH_RETURN_v4f32: {
Dan Gohmanb7c24002016-05-21 00:21:56 +0000193 // These instructions represent the implicit return at the end of a
194 // function body. The operand is always a pop.
195 assert(MFI->isVRegStackified(MI->getOperand(0).getReg()));
196
197 if (isVerbose()) {
198 OutStreamer->AddComment("fallthrough-return: $pop" +
Benjamin Kramer3a13ed62017-12-28 16:58:54 +0000199 Twine(MFI->getWARegStackId(
Dan Gohmanb7c24002016-05-21 00:21:56 +0000200 MFI->getWAReg(MI->getOperand(0).getReg()))));
201 OutStreamer->AddBlankLine();
202 }
203 break;
204 }
205 case WebAssembly::FALLTHROUGH_RETURN_VOID:
206 // This instruction represents the implicit return at the end of a
207 // function body with no return value.
208 if (isVerbose()) {
209 OutStreamer->AddComment("fallthrough-return");
210 OutStreamer->AddBlankLine();
211 }
212 break;
Dan Gohmane51c0582015-10-06 00:27:55 +0000213 default: {
Dan Gohmancf4748f2015-11-12 17:04:33 +0000214 WebAssemblyMCInstLower MCInstLowering(OutContext, *this);
215 MCInst TmpInst;
216 MCInstLowering.Lower(MI, TmpInst);
217 EmitToStreamer(*OutStreamer, TmpInst);
Dan Gohmane51c0582015-10-06 00:27:55 +0000218 break;
219 }
Dan Gohman4f52e002015-09-09 00:52:47 +0000220 }
JF Bastienb9073fb2015-07-22 21:28:15 +0000221}
222
Dan Gohman26c67652016-01-11 23:38:05 +0000223const MCExpr *WebAssemblyAsmPrinter::lowerConstant(const Constant *CV) {
224 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
Sam Clegg7736855d2017-06-13 01:42:21 +0000225 if (GV->getValueType()->isFunctionTy()) {
Dan Gohman26c67652016-01-11 23:38:05 +0000226 return MCSymbolRefExpr::create(
Sam Clegg9bf73c02017-07-05 20:25:08 +0000227 getSymbol(GV), MCSymbolRefExpr::VK_WebAssembly_FUNCTION, OutContext);
Sam Clegg7736855d2017-06-13 01:42:21 +0000228 }
Dan Gohman26c67652016-01-11 23:38:05 +0000229 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
Dan Gohmanb465aa02017-11-08 19:18:08 +0000279 // The current approach to inline asm is that "r" constraints are expressed
280 // as local indices, rather than values on the operand stack. This simplifies
281 // using "r" as it eliminates the need to push and pop the values in a
282 // particular order, however it also makes it impossible to have an "m"
283 // constraint. So we don't support it.
Dan Gohmanf19ed562015-11-13 01:42:29 +0000284
285 return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
286}
287
JF Bastienb9073fb2015-07-22 21:28:15 +0000288// Force static initialization.
289extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
Mehdi Aminif42454b2016-10-09 23:00:34 +0000290 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(getTheWebAssemblyTarget32());
291 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(getTheWebAssemblyTarget64());
JF Bastienb9073fb2015-07-22 21:28:15 +0000292}