blob: 4f7c68d4882b84bbbf0b59ff39af11681af5a0f9 [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);
Jacob Gravelleca358da2018-02-12 21:41:12 +000087 MCSymbol *Sym = getSymbol(&F);
Dan Gohmandb1916a2018-02-09 23:13:22 +000088 getTargetStreamer()->emitIndirectFunctionType(Sym, Params, Results);
89
Jacob Gravelleca358da2018-02-12 21:41:12 +000090 if (TM.getTargetTriple().isOSBinFormatWasm() &&
91 F.hasFnAttribute("wasm-import-module")) {
92 MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Sym);
Dan Gohmandb1916a2018-02-09 23:13:22 +000093 StringRef Name = F.getFnAttribute("wasm-import-module")
94 .getValueAsString();
Jacob Gravelleca358da2018-02-12 21:41:12 +000095 getTargetStreamer()->emitImportModule(WasmSym, Name);
Dan Gohmandb1916a2018-02-09 23:13:22 +000096 }
Derek Schuff5859a9ed2016-06-03 18:34:36 +000097 }
98 }
Derek Schuff7747d703e2016-12-01 00:11:15 +000099 for (const auto &G : M.globals()) {
100 if (!G.hasInitializer() && G.hasExternalLinkage()) {
Dan Gohman5cf64732017-12-07 00:14:30 +0000101 if (G.getValueType()->isSized()) {
102 uint16_t Size = M.getDataLayout().getTypeAllocSize(G.getValueType());
103 if (TM.getTargetTriple().isOSBinFormatELF())
104 getTargetStreamer()->emitGlobalImport(G.getGlobalIdentifier());
105 OutStreamer->emitELFSize(getSymbol(&G),
106 MCConstantExpr::create(Size, OutContext));
107 }
Derek Schuff7747d703e2016-12-01 00:11:15 +0000108 }
109 }
Derek Schuff5859a9ed2016-06-03 18:34:36 +0000110}
111
112void WebAssemblyAsmPrinter::EmitConstantPool() {
113 assert(MF->getConstantPool()->getConstants().empty() &&
114 "WebAssembly disables constant pools");
115}
116
117void WebAssemblyAsmPrinter::EmitJumpTableInfo() {
118 // Nothing to do; jump tables are incorporated into the instruction stream.
119}
120
JF Bastienb6091df2015-08-25 22:58:05 +0000121void WebAssemblyAsmPrinter::EmitFunctionBodyStart() {
Dan Gohmand934cb82017-02-24 23:18:00 +0000122 getTargetStreamer()->emitParam(CurrentFnSym, MFI->getParams());
Dan Gohmane51c0582015-10-06 00:27:55 +0000123
Derek Schuff46e33162015-11-16 21:12:41 +0000124 SmallVector<MVT, 4> ResultVTs;
David Blaikie21109242017-12-15 23:52:06 +0000125 const Function &F = MF->getFunction();
Derek Schuffc64d7652016-08-01 22:25:02 +0000126
127 // Emit the function index.
128 if (MDNode *Idx = F.getMetadata("wasm.index")) {
129 assert(Idx->getNumOperands() == 1);
130
131 getTargetStreamer()->emitIndIdx(AsmPrinter::lowerConstant(
132 cast<ConstantAsMetadata>(Idx->getOperand(0))->getValue()));
133 }
134
Derek Schuff46e33162015-11-16 21:12:41 +0000135 ComputeLegalValueVTs(F, TM, F.getReturnType(), ResultVTs);
Dan Gohman3469ee12016-01-12 20:30:51 +0000136
Derek Schuff46e33162015-11-16 21:12:41 +0000137 // If the return type needs to be legalized it will get converted into
138 // passing a pointer.
Dan Gohman3469ee12016-01-12 20:30:51 +0000139 if (ResultVTs.size() == 1)
Dan Gohmand934cb82017-02-24 23:18:00 +0000140 getTargetStreamer()->emitResult(CurrentFnSym, ResultVTs);
141 else
142 getTargetStreamer()->emitResult(CurrentFnSym, ArrayRef<MVT>());
JF Bastienb6091df2015-08-25 22:58:05 +0000143
Dan Gohmand934cb82017-02-24 23:18:00 +0000144 if (TM.getTargetTriple().isOSBinFormatELF()) {
145 assert(MFI->getLocals().empty());
146 for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) {
147 unsigned VReg = TargetRegisterInfo::index2VirtReg(Idx);
148 unsigned WAReg = MFI->getWAReg(VReg);
149 // Don't declare unused registers.
150 if (WAReg == WebAssemblyFunctionInfo::UnusedReg)
151 continue;
152 // Don't redeclare parameters.
153 if (WAReg < MFI->getParams().size())
154 continue;
155 // Don't declare stackified registers.
156 if (int(WAReg) < 0)
157 continue;
158 MFI->addLocal(getRegType(VReg));
159 }
JF Bastien1d20a5e2015-10-16 00:53:49 +0000160 }
Dan Gohman3acb1872016-10-24 23:27:49 +0000161
162 getTargetStreamer()->emitLocal(MFI->getLocals());
JF Bastien1d20a5e2015-10-16 00:53:49 +0000163
Dan Gohmane51c0582015-10-06 00:27:55 +0000164 AsmPrinter::EmitFunctionBodyStart();
JF Bastienb6091df2015-08-25 22:58:05 +0000165}
166
Dan Gohman3469ee12016-01-12 20:30:51 +0000167void WebAssemblyAsmPrinter::EmitFunctionBodyEnd() {
Dan Gohmand934cb82017-02-24 23:18:00 +0000168 if (TM.getTargetTriple().isOSBinFormatELF())
169 getTargetStreamer()->emitEndFunc();
Dan Gohman3469ee12016-01-12 20:30:51 +0000170}
171
JF Bastienb9073fb2015-07-22 21:28:15 +0000172void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
JF Bastienaf111db2015-08-24 22:16:48 +0000173 DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
JF Bastienb9073fb2015-07-22 21:28:15 +0000174
Dan Gohmane51c0582015-10-06 00:27:55 +0000175 switch (MI->getOpcode()) {
Dan Gohmane51c0582015-10-06 00:27:55 +0000176 case WebAssembly::ARGUMENT_I32:
177 case WebAssembly::ARGUMENT_I64:
178 case WebAssembly::ARGUMENT_F32:
179 case WebAssembly::ARGUMENT_F64:
Derek Schuff39bf39f2016-08-02 23:16:09 +0000180 case WebAssembly::ARGUMENT_v16i8:
181 case WebAssembly::ARGUMENT_v8i16:
182 case WebAssembly::ARGUMENT_v4i32:
183 case WebAssembly::ARGUMENT_v4f32:
Dan Gohmancf4748f2015-11-12 17:04:33 +0000184 // These represent values which are live into the function entry, so there's
185 // no instruction to emit.
Dan Gohmane51c0582015-10-06 00:27:55 +0000186 break;
Dan Gohmanb7c24002016-05-21 00:21:56 +0000187 case WebAssembly::FALLTHROUGH_RETURN_I32:
188 case WebAssembly::FALLTHROUGH_RETURN_I64:
189 case WebAssembly::FALLTHROUGH_RETURN_F32:
Derek Schuff39bf39f2016-08-02 23:16:09 +0000190 case WebAssembly::FALLTHROUGH_RETURN_F64:
191 case WebAssembly::FALLTHROUGH_RETURN_v16i8:
192 case WebAssembly::FALLTHROUGH_RETURN_v8i16:
193 case WebAssembly::FALLTHROUGH_RETURN_v4i32:
194 case WebAssembly::FALLTHROUGH_RETURN_v4f32: {
Dan Gohmanb7c24002016-05-21 00:21:56 +0000195 // These instructions represent the implicit return at the end of a
196 // function body. The operand is always a pop.
197 assert(MFI->isVRegStackified(MI->getOperand(0).getReg()));
198
199 if (isVerbose()) {
200 OutStreamer->AddComment("fallthrough-return: $pop" +
Benjamin Kramer3a13ed62017-12-28 16:58:54 +0000201 Twine(MFI->getWARegStackId(
Dan Gohmanb7c24002016-05-21 00:21:56 +0000202 MFI->getWAReg(MI->getOperand(0).getReg()))));
203 OutStreamer->AddBlankLine();
204 }
205 break;
206 }
207 case WebAssembly::FALLTHROUGH_RETURN_VOID:
208 // This instruction represents the implicit return at the end of a
209 // function body with no return value.
210 if (isVerbose()) {
211 OutStreamer->AddComment("fallthrough-return");
212 OutStreamer->AddBlankLine();
213 }
214 break;
Dan Gohmane51c0582015-10-06 00:27:55 +0000215 default: {
Dan Gohmancf4748f2015-11-12 17:04:33 +0000216 WebAssemblyMCInstLower MCInstLowering(OutContext, *this);
217 MCInst TmpInst;
218 MCInstLowering.Lower(MI, TmpInst);
219 EmitToStreamer(*OutStreamer, TmpInst);
Dan Gohmane51c0582015-10-06 00:27:55 +0000220 break;
221 }
Dan Gohman4f52e002015-09-09 00:52:47 +0000222 }
JF Bastienb9073fb2015-07-22 21:28:15 +0000223}
224
Dan Gohman26c67652016-01-11 23:38:05 +0000225const MCExpr *WebAssemblyAsmPrinter::lowerConstant(const Constant *CV) {
226 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
Sam Clegg7736855d2017-06-13 01:42:21 +0000227 if (GV->getValueType()->isFunctionTy()) {
Dan Gohman26c67652016-01-11 23:38:05 +0000228 return MCSymbolRefExpr::create(
Sam Clegg9bf73c02017-07-05 20:25:08 +0000229 getSymbol(GV), MCSymbolRefExpr::VK_WebAssembly_FUNCTION, OutContext);
Sam Clegg7736855d2017-06-13 01:42:21 +0000230 }
Dan Gohman26c67652016-01-11 23:38:05 +0000231 return AsmPrinter::lowerConstant(CV);
232}
233
Dan Gohmanf19ed562015-11-13 01:42:29 +0000234bool WebAssemblyAsmPrinter::PrintAsmOperand(const MachineInstr *MI,
235 unsigned OpNo, unsigned AsmVariant,
236 const char *ExtraCode,
237 raw_ostream &OS) {
238 if (AsmVariant != 0)
239 report_fatal_error("There are no defined alternate asm variants");
240
Dan Gohman30a42bf2015-12-16 17:15:17 +0000241 // First try the generic code, which knows about modifiers like 'c' and 'n'.
242 if (!AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS))
243 return false;
244
Dan Gohmanf19ed562015-11-13 01:42:29 +0000245 if (!ExtraCode) {
246 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohman30a42bf2015-12-16 17:15:17 +0000247 switch (MO.getType()) {
248 case MachineOperand::MO_Immediate:
Dan Gohmanf19ed562015-11-13 01:42:29 +0000249 OS << MO.getImm();
Dan Gohman30a42bf2015-12-16 17:15:17 +0000250 return false;
251 case MachineOperand::MO_Register:
Dan Gohmanf19ed562015-11-13 01:42:29 +0000252 OS << regToString(MO);
Dan Gohman30a42bf2015-12-16 17:15:17 +0000253 return false;
254 case MachineOperand::MO_GlobalAddress:
255 getSymbol(MO.getGlobal())->print(OS, MAI);
256 printOffset(MO.getOffset(), OS);
257 return false;
258 case MachineOperand::MO_ExternalSymbol:
259 GetExternalSymbolSymbol(MO.getSymbolName())->print(OS, MAI);
260 printOffset(MO.getOffset(), OS);
261 return false;
262 case MachineOperand::MO_MachineBasicBlock:
263 MO.getMBB()->getSymbol()->print(OS, MAI);
264 return false;
265 default:
266 break;
267 }
Dan Gohmanf19ed562015-11-13 01:42:29 +0000268 }
269
Dan Gohman30a42bf2015-12-16 17:15:17 +0000270 return true;
Dan Gohmanf19ed562015-11-13 01:42:29 +0000271}
272
273bool WebAssemblyAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
274 unsigned OpNo,
275 unsigned AsmVariant,
276 const char *ExtraCode,
277 raw_ostream &OS) {
278 if (AsmVariant != 0)
279 report_fatal_error("There are no defined alternate asm variants");
280
Dan Gohmanb465aa02017-11-08 19:18:08 +0000281 // The current approach to inline asm is that "r" constraints are expressed
282 // as local indices, rather than values on the operand stack. This simplifies
283 // using "r" as it eliminates the need to push and pop the values in a
284 // particular order, however it also makes it impossible to have an "m"
285 // constraint. So we don't support it.
Dan Gohmanf19ed562015-11-13 01:42:29 +0000286
287 return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
288}
289
JF Bastienb9073fb2015-07-22 21:28:15 +0000290// Force static initialization.
291extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
Mehdi Aminif42454b2016-10-09 23:00:34 +0000292 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(getTheWebAssemblyTarget32());
293 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(getTheWebAssemblyTarget64());
JF Bastienb9073fb2015-07-22 21:28:15 +0000294}