blob: 07e18cd6f193e5821d0a65fca690c06265959af7 [file] [log] [blame]
JF Bastienb9073fb2015-07-22 21:28:15 +00001//===-- WebAssemblyAsmPrinter.cpp - WebAssembly LLVM assembly writer ------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
JF Bastienb9073fb2015-07-22 21:28:15 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// This file contains a printer that converts from our internal
JF Bastienb9073fb2015-07-22 21:28:15 +000011/// representation of machine-dependent LLVM code to the WebAssembly assembly
12/// language.
13///
14//===----------------------------------------------------------------------===//
15
Dan Gohmand934cb82017-02-24 23:18:00 +000016#include "WebAssemblyAsmPrinter.h"
David L. Jonesa263aa22019-05-13 03:32:41 +000017#include "MCTargetDesc/WebAssemblyInstPrinter.h"
Dan Gohmancf4748f2015-11-12 17:04:33 +000018#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
Dan Gohman3469ee12016-01-12 20:30:51 +000019#include "MCTargetDesc/WebAssemblyTargetStreamer.h"
Richard Trieuc6c42132019-05-15 01:03:00 +000020#include "TargetInfo/WebAssemblyTargetInfo.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"
Thomas Livelyf6f4f842019-03-20 20:26:45 +000025#include "WebAssemblyTargetMachine.h"
Thomas Livelycbda16e2019-01-17 02:29:55 +000026#include "llvm/ADT/SmallSet.h"
Dan Gohmane51c0582015-10-06 00:27:55 +000027#include "llvm/ADT/StringExtras.h"
Thomas Livelyf6f4f842019-03-20 20:26:45 +000028#include "llvm/BinaryFormat/Wasm.h"
Dan Gohman754cd112015-11-11 01:33:02 +000029#include "llvm/CodeGen/Analysis.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000030#include "llvm/CodeGen/AsmPrinter.h"
JF Bastien54be3b12015-08-25 23:19:49 +000031#include "llvm/CodeGen/MachineConstantPool.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000032#include "llvm/CodeGen/MachineInstr.h"
Dan Gohman82607f52017-02-24 23:46:05 +000033#include "llvm/CodeGen/MachineModuleInfoImpls.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000034#include "llvm/IR/DataLayout.h"
Thomas Livelyc6795e02019-01-18 02:47:48 +000035#include "llvm/IR/DebugInfoMetadata.h"
Dan Gohmand934cb82017-02-24 23:18:00 +000036#include "llvm/IR/GlobalVariable.h"
Thomas Lively3f34e1b82019-03-29 00:14:01 +000037#include "llvm/IR/Metadata.h"
Dan Gohmancf4748f2015-11-12 17:04:33 +000038#include "llvm/MC/MCContext.h"
Sam Cleggcfd44a22018-04-05 17:01:39 +000039#include "llvm/MC/MCSectionWasm.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000040#include "llvm/MC/MCStreamer.h"
JF Bastienb6091df2015-08-25 22:58:05 +000041#include "llvm/MC/MCSymbol.h"
Sam Cleggcfd44a22018-04-05 17:01:39 +000042#include "llvm/MC/MCSymbolWasm.h"
JF Bastienb9073fb2015-07-22 21:28:15 +000043#include "llvm/Support/Debug.h"
44#include "llvm/Support/TargetRegistry.h"
45#include "llvm/Support/raw_ostream.h"
Thomas Lively2e150402019-02-19 22:56:19 +000046
JF Bastienb9073fb2015-07-22 21:28:15 +000047using namespace llvm;
48
49#define DEBUG_TYPE "asm-printer"
50
Heejin Ahnd6f48782019-01-30 03:21:57 +000051extern cl::opt<bool> WasmKeepRegisters;
52
JF Bastienb9073fb2015-07-22 21:28:15 +000053//===----------------------------------------------------------------------===//
JF Bastien45479f62015-08-26 22:09:54 +000054// Helpers.
55//===----------------------------------------------------------------------===//
JF Bastienb9073fb2015-07-22 21:28:15 +000056
Dan Gohman53828fd2015-11-23 16:50:18 +000057MVT WebAssemblyAsmPrinter::getRegType(unsigned RegNo) const {
Krzysztof Parzyszekc8e8e2a2017-04-24 19:51:12 +000058 const TargetRegisterInfo *TRI = Subtarget->getRegisterInfo();
Dan Gohman0cfb5f82016-05-10 04:24:02 +000059 const TargetRegisterClass *TRC = MRI->getRegClass(RegNo);
Derek Schuff39bf39f2016-08-02 23:16:09 +000060 for (MVT T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64, MVT::v16i8, MVT::v8i16,
Derek Schuff51ed1312018-08-07 21:24:01 +000061 MVT::v4i32, MVT::v2i64, MVT::v4f32, MVT::v2f64})
Krzysztof Parzyszekc8e8e2a2017-04-24 19:51:12 +000062 if (TRI->isTypeLegalForClass(*TRC, T))
Dan Gohman53828fd2015-11-23 16:50:18 +000063 return T;
Nicola Zaghend34e60c2018-05-14 12:53:11 +000064 LLVM_DEBUG(errs() << "Unknown type for register number: " << RegNo);
JF Bastien1d20a5e2015-10-16 00:53:49 +000065 llvm_unreachable("Unknown register type");
Dan Gohman53828fd2015-11-23 16:50:18 +000066 return MVT::Other;
JF Bastien1d20a5e2015-10-16 00:53:49 +000067}
68
JF Bastien1d20a5e2015-10-16 00:53:49 +000069std::string WebAssemblyAsmPrinter::regToString(const MachineOperand &MO) {
Daniel Sanders05c145d2019-08-12 22:40:45 +000070 Register RegNo = MO.getReg();
Daniel Sanders2bea69b2019-08-01 23:27:28 +000071 assert(Register::isVirtualRegister(RegNo) &&
Dan Gohmand9625272015-11-20 03:13:31 +000072 "Unlowered physical register encountered during assembly printing");
Dan Gohman4ba48162015-11-18 16:12:01 +000073 assert(!MFI->isVRegStackified(RegNo));
Dan Gohman058fce52015-11-13 00:21:05 +000074 unsigned WAReg = MFI->getWAReg(RegNo);
75 assert(WAReg != WebAssemblyFunctionInfo::UnusedReg);
Dan Gohman4ba48162015-11-18 16:12:01 +000076 return '$' + utostr(WAReg);
Dan Gohmane51c0582015-10-06 00:27:55 +000077}
78
Dan Gohmanec977b02016-01-25 15:12:05 +000079WebAssemblyTargetStreamer *WebAssemblyAsmPrinter::getTargetStreamer() {
Dan Gohman3469ee12016-01-12 20:30:51 +000080 MCTargetStreamer *TS = OutStreamer->getTargetStreamer();
81 return static_cast<WebAssemblyTargetStreamer *>(TS);
JF Bastien73ff6af2015-08-31 22:24:11 +000082}
83
JF Bastien45479f62015-08-26 22:09:54 +000084//===----------------------------------------------------------------------===//
85// WebAssemblyAsmPrinter Implementation.
86//===----------------------------------------------------------------------===//
Derek Schuff46e33162015-11-16 21:12:41 +000087
Fangrui Song0dce4092020-02-13 13:10:49 -080088void WebAssemblyAsmPrinter::emitEndOfAsmFile(Module &M) {
Wouter van Oortmerssen3231e512018-11-02 00:45:00 +000089 for (auto &It : OutContext.getSymbols()) {
Heejin Ahnda419bd2018-11-14 02:46:21 +000090 // Emit a .globaltype and .eventtype declaration.
Wouter van Oortmerssen3231e512018-11-02 00:45:00 +000091 auto Sym = cast<MCSymbolWasm>(It.getValue());
Heejin Ahnda419bd2018-11-14 02:46:21 +000092 if (Sym->getType() == wasm::WASM_SYMBOL_TYPE_GLOBAL)
Wouter van Oortmerssen3231e512018-11-02 00:45:00 +000093 getTargetStreamer()->emitGlobalType(Sym);
Heejin Ahnda419bd2018-11-14 02:46:21 +000094 else if (Sym->getType() == wasm::WASM_SYMBOL_TYPE_EVENT)
95 getTargetStreamer()->emitEventType(Sym);
Wouter van Oortmerssen3231e512018-11-02 00:45:00 +000096 }
97
Derek Schuff5859a9ed2016-06-03 18:34:36 +000098 for (const auto &F : M) {
Sam Clegg881d8772019-11-05 10:15:56 -080099 if (F.isIntrinsic())
100 continue;
101
Derek Schuff5859a9ed2016-06-03 18:34:36 +0000102 // Emit function type info for all undefined functions
Sam Clegg881d8772019-11-05 10:15:56 -0800103 if (F.isDeclarationForLinker()) {
Dan Gohman2726b882016-10-06 22:29:32 +0000104 SmallVector<MVT, 4> Results;
105 SmallVector<MVT, 4> Params;
Heejin Ahn18c56a02019-02-04 19:13:39 +0000106 computeSignatureVTs(F.getFunctionType(), F, TM, Params, Results);
Derek Schuff77a7a382018-10-03 22:22:48 +0000107 auto *Sym = cast<MCSymbolWasm>(getSymbol(&F));
Heejin Ahn21d45a22018-12-11 00:53:59 +0000108 Sym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
Derek Schuff77a7a382018-10-03 22:22:48 +0000109 if (!Sym->getSignature()) {
Heejin Ahn18c56a02019-02-04 19:13:39 +0000110 auto Signature = signatureFromMVTs(Results, Params);
Derek Schuff77a7a382018-10-03 22:22:48 +0000111 Sym->setSignature(Signature.get());
112 addSignature(std::move(Signature));
113 }
114 // FIXME: this was originally intended for post-linking and was only used
115 // for imports that were only called indirectly (i.e. s2wasm could not
116 // infer the type from a call). With object files it applies to all
117 // imports. so fix the names and the tests, or rethink how import
118 // delcarations work in asm files.
Wouter van Oortmerssen49482f82018-11-19 17:10:36 +0000119 getTargetStreamer()->emitFunctionType(Sym);
Dan Gohmandb1916a2018-02-09 23:13:22 +0000120
Jacob Gravelleca358da2018-02-12 21:41:12 +0000121 if (TM.getTargetTriple().isOSBinFormatWasm() &&
122 F.hasFnAttribute("wasm-import-module")) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000123 StringRef Name =
124 F.getFnAttribute("wasm-import-module").getValueAsString();
Dan Gohmanf726e442019-02-01 22:27:34 +0000125 Sym->setImportModule(Name);
Derek Schuff77a7a382018-10-03 22:22:48 +0000126 getTargetStreamer()->emitImportModule(Sym, Name);
Dan Gohmandb1916a2018-02-09 23:13:22 +0000127 }
Dan Gohmanf726e442019-02-01 22:27:34 +0000128 if (TM.getTargetTriple().isOSBinFormatWasm() &&
129 F.hasFnAttribute("wasm-import-name")) {
130 StringRef Name =
131 F.getFnAttribute("wasm-import-name").getValueAsString();
132 Sym->setImportName(Name);
133 getTargetStreamer()->emitImportName(Sym, Name);
134 }
Derek Schuff5859a9ed2016-06-03 18:34:36 +0000135 }
Sam Clegg881d8772019-11-05 10:15:56 -0800136
137 if (F.hasFnAttribute("wasm-export-name")) {
138 auto *Sym = cast<MCSymbolWasm>(getSymbol(&F));
139 StringRef Name = F.getFnAttribute("wasm-export-name").getValueAsString();
140 Sym->setExportName(Name);
141 getTargetStreamer()->emitExportName(Sym, Name);
142 }
Derek Schuff5859a9ed2016-06-03 18:34:36 +0000143 }
Wouter van Oortmerssen3231e512018-11-02 00:45:00 +0000144
Derek Schuff7747d703e2016-12-01 00:11:15 +0000145 for (const auto &G : M.globals()) {
146 if (!G.hasInitializer() && G.hasExternalLinkage()) {
Dan Gohman5cf64732017-12-07 00:14:30 +0000147 if (G.getValueType()->isSized()) {
148 uint16_t Size = M.getDataLayout().getTypeAllocSize(G.getValueType());
Dan Gohman5cf64732017-12-07 00:14:30 +0000149 OutStreamer->emitELFSize(getSymbol(&G),
150 MCConstantExpr::create(Size, OutContext));
151 }
Derek Schuff7747d703e2016-12-01 00:11:15 +0000152 }
153 }
Sam Cleggcfd44a22018-04-05 17:01:39 +0000154
155 if (const NamedMDNode *Named = M.getNamedMetadata("wasm.custom_sections")) {
156 for (const Metadata *MD : Named->operands()) {
Heejin Ahn18c56a02019-02-04 19:13:39 +0000157 const auto *Tuple = dyn_cast<MDTuple>(MD);
Sam Cleggcfd44a22018-04-05 17:01:39 +0000158 if (!Tuple || Tuple->getNumOperands() != 2)
159 continue;
160 const MDString *Name = dyn_cast<MDString>(Tuple->getOperand(0));
161 const MDString *Contents = dyn_cast<MDString>(Tuple->getOperand(1));
162 if (!Name || !Contents)
163 continue;
164
165 OutStreamer->PushSection();
166 std::string SectionName = (".custom_section." + Name->getString()).str();
Heejin Ahn18c56a02019-02-04 19:13:39 +0000167 MCSectionWasm *MySection =
Sam Cleggcfd44a22018-04-05 17:01:39 +0000168 OutContext.getWasmSection(SectionName, SectionKind::getMetadata());
Heejin Ahn18c56a02019-02-04 19:13:39 +0000169 OutStreamer->SwitchSection(MySection);
Fangrui Songa55daa12020-02-14 18:16:24 -0800170 OutStreamer->emitBytes(Contents->getString());
Sam Cleggcfd44a22018-04-05 17:01:39 +0000171 OutStreamer->PopSection();
172 }
173 }
Thomas Livelycbda16e2019-01-17 02:29:55 +0000174
Thomas Livelyc6795e02019-01-18 02:47:48 +0000175 EmitProducerInfo(M);
Thomas Lively3f34e1b82019-03-29 00:14:01 +0000176 EmitTargetFeatures(M);
Thomas Livelyc6795e02019-01-18 02:47:48 +0000177}
178
179void WebAssemblyAsmPrinter::EmitProducerInfo(Module &M) {
180 llvm::SmallVector<std::pair<std::string, std::string>, 4> Languages;
181 if (const NamedMDNode *Debug = M.getNamedMetadata("llvm.dbg.cu")) {
Heejin Ahn18c56a02019-02-04 19:13:39 +0000182 llvm::SmallSet<StringRef, 4> SeenLanguages;
183 for (size_t I = 0, E = Debug->getNumOperands(); I < E; ++I) {
184 const auto *CU = cast<DICompileUnit>(Debug->getOperand(I));
Thomas Livelyc6795e02019-01-18 02:47:48 +0000185 StringRef Language = dwarf::LanguageString(CU->getSourceLanguage());
186 Language.consume_front("DW_LANG_");
187 if (SeenLanguages.insert(Language).second)
188 Languages.emplace_back(Language.str(), "");
189 }
190 }
191
192 llvm::SmallVector<std::pair<std::string, std::string>, 4> Tools;
Thomas Livelycbda16e2019-01-17 02:29:55 +0000193 if (const NamedMDNode *Ident = M.getNamedMetadata("llvm.ident")) {
194 llvm::SmallSet<StringRef, 4> SeenTools;
Heejin Ahn18c56a02019-02-04 19:13:39 +0000195 for (size_t I = 0, E = Ident->getNumOperands(); I < E; ++I) {
196 const auto *S = cast<MDString>(Ident->getOperand(I)->getOperand(0));
Thomas Livelycbda16e2019-01-17 02:29:55 +0000197 std::pair<StringRef, StringRef> Field = S->getString().split("version");
198 StringRef Name = Field.first.trim();
199 StringRef Version = Field.second.trim();
Thomas Livelyc6795e02019-01-18 02:47:48 +0000200 if (SeenTools.insert(Name).second)
201 Tools.emplace_back(Name.str(), Version.str());
Thomas Livelycbda16e2019-01-17 02:29:55 +0000202 }
Thomas Livelyc6795e02019-01-18 02:47:48 +0000203 }
204
205 int FieldCount = int(!Languages.empty()) + int(!Tools.empty());
206 if (FieldCount != 0) {
Thomas Livelycbda16e2019-01-17 02:29:55 +0000207 MCSectionWasm *Producers = OutContext.getWasmSection(
208 ".custom_section.producers", SectionKind::getMetadata());
209 OutStreamer->PushSection();
210 OutStreamer->SwitchSection(Producers);
Fangrui Song0bc77a02020-02-13 13:26:21 -0800211 OutStreamer->emitULEB128IntValue(FieldCount);
Thomas Livelyc6795e02019-01-18 02:47:48 +0000212 for (auto &Producers : {std::make_pair("language", &Languages),
213 std::make_pair("processed-by", &Tools)}) {
214 if (Producers.second->empty())
215 continue;
Fangrui Song0bc77a02020-02-13 13:26:21 -0800216 OutStreamer->emitULEB128IntValue(strlen(Producers.first));
Fangrui Songa55daa12020-02-14 18:16:24 -0800217 OutStreamer->emitBytes(Producers.first);
Fangrui Song0bc77a02020-02-13 13:26:21 -0800218 OutStreamer->emitULEB128IntValue(Producers.second->size());
Thomas Livelyc6795e02019-01-18 02:47:48 +0000219 for (auto &Producer : *Producers.second) {
Fangrui Song0bc77a02020-02-13 13:26:21 -0800220 OutStreamer->emitULEB128IntValue(Producer.first.size());
Fangrui Songa55daa12020-02-14 18:16:24 -0800221 OutStreamer->emitBytes(Producer.first);
Fangrui Song0bc77a02020-02-13 13:26:21 -0800222 OutStreamer->emitULEB128IntValue(Producer.second.size());
Fangrui Songa55daa12020-02-14 18:16:24 -0800223 OutStreamer->emitBytes(Producer.second);
Thomas Livelyc6795e02019-01-18 02:47:48 +0000224 }
Thomas Livelycbda16e2019-01-17 02:29:55 +0000225 }
226 OutStreamer->PopSection();
227 }
Derek Schuff5859a9ed2016-06-03 18:34:36 +0000228}
229
Thomas Lively3f34e1b82019-03-29 00:14:01 +0000230void WebAssemblyAsmPrinter::EmitTargetFeatures(Module &M) {
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000231 struct FeatureEntry {
232 uint8_t Prefix;
233 StringRef Name;
234 };
235
Thomas Lively3f34e1b82019-03-29 00:14:01 +0000236 // Read target features and linkage policies from module metadata
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000237 SmallVector<FeatureEntry, 4> EmittedFeatures;
Thomas Lively3f34e1b82019-03-29 00:14:01 +0000238 for (const SubtargetFeatureKV &KV : WebAssemblyFeatureKV) {
239 std::string MDKey = (StringRef("wasm-feature-") + KV.Key).str();
240 Metadata *Policy = M.getModuleFlag(MDKey);
241 if (Policy == nullptr)
242 continue;
243
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000244 FeatureEntry Entry;
Thomas Lively3f34e1b82019-03-29 00:14:01 +0000245 Entry.Prefix = 0;
246 Entry.Name = KV.Key;
247
248 if (auto *MD = cast<ConstantAsMetadata>(Policy))
249 if (auto *I = cast<ConstantInt>(MD->getValue()))
250 Entry.Prefix = I->getZExtValue();
251
252 // Silently ignore invalid metadata
253 if (Entry.Prefix != wasm::WASM_FEATURE_PREFIX_USED &&
254 Entry.Prefix != wasm::WASM_FEATURE_PREFIX_REQUIRED &&
255 Entry.Prefix != wasm::WASM_FEATURE_PREFIX_DISALLOWED)
256 continue;
257
258 EmittedFeatures.push_back(Entry);
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000259 }
260
Thomas Lively3f34e1b82019-03-29 00:14:01 +0000261 if (EmittedFeatures.size() == 0)
262 return;
263
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000264 // Emit features and linkage policies into the "target_features" section
265 MCSectionWasm *FeaturesSection = OutContext.getWasmSection(
266 ".custom_section.target_features", SectionKind::getMetadata());
267 OutStreamer->PushSection();
268 OutStreamer->SwitchSection(FeaturesSection);
269
Fangrui Song0bc77a02020-02-13 13:26:21 -0800270 OutStreamer->emitULEB128IntValue(EmittedFeatures.size());
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000271 for (auto &F : EmittedFeatures) {
272 OutStreamer->EmitIntValue(F.Prefix, 1);
Fangrui Song0bc77a02020-02-13 13:26:21 -0800273 OutStreamer->emitULEB128IntValue(F.Name.size());
Fangrui Songa55daa12020-02-14 18:16:24 -0800274 OutStreamer->emitBytes(F.Name);
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000275 }
276
277 OutStreamer->PopSection();
278}
279
Fangrui Song1d49eb02020-02-13 16:36:27 -0800280void WebAssemblyAsmPrinter::emitConstantPool() {
Derek Schuff5859a9ed2016-06-03 18:34:36 +0000281 assert(MF->getConstantPool()->getConstants().empty() &&
282 "WebAssembly disables constant pools");
283}
284
Fangrui Song1d49eb02020-02-13 16:36:27 -0800285void WebAssemblyAsmPrinter::emitJumpTableInfo() {
Derek Schuff5859a9ed2016-06-03 18:34:36 +0000286 // Nothing to do; jump tables are incorporated into the instruction stream.
287}
288
Fangrui Song0dce4092020-02-13 13:10:49 -0800289void WebAssemblyAsmPrinter::emitFunctionBodyStart() {
David Blaikie21109242017-12-15 23:52:06 +0000290 const Function &F = MF->getFunction();
Derek Schuff77a7a382018-10-03 22:22:48 +0000291 SmallVector<MVT, 1> ResultVTs;
292 SmallVector<MVT, 4> ParamVTs;
Heejin Ahn18c56a02019-02-04 19:13:39 +0000293 computeSignatureVTs(F.getFunctionType(), F, TM, ParamVTs, ResultVTs);
294 auto Signature = signatureFromMVTs(ResultVTs, ParamVTs);
Derek Schuff77a7a382018-10-03 22:22:48 +0000295 auto *WasmSym = cast<MCSymbolWasm>(CurrentFnSym);
296 WasmSym->setSignature(Signature.get());
297 addSignature(std::move(Signature));
Heejin Ahn21d45a22018-12-11 00:53:59 +0000298 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
Derek Schuff77a7a382018-10-03 22:22:48 +0000299
300 // FIXME: clean up how params and results are emitted (use signatures)
Wouter van Oortmerssen49482f82018-11-19 17:10:36 +0000301 getTargetStreamer()->emitFunctionType(WasmSym);
Derek Schuffc64d7652016-08-01 22:25:02 +0000302
303 // Emit the function index.
304 if (MDNode *Idx = F.getMetadata("wasm.index")) {
305 assert(Idx->getNumOperands() == 1);
306
307 getTargetStreamer()->emitIndIdx(AsmPrinter::lowerConstant(
308 cast<ConstantAsMetadata>(Idx->getOperand(0))->getValue()));
309 }
310
Wouter van Oortmerssen49482f82018-11-19 17:10:36 +0000311 SmallVector<wasm::ValType, 16> Locals;
Heejin Ahn18c56a02019-02-04 19:13:39 +0000312 valTypesFromMVTs(MFI->getLocals(), Locals);
Wouter van Oortmerssen49482f82018-11-19 17:10:36 +0000313 getTargetStreamer()->emitLocal(Locals);
JF Bastien1d20a5e2015-10-16 00:53:49 +0000314
Fangrui Song0dce4092020-02-13 13:10:49 -0800315 AsmPrinter::emitFunctionBodyStart();
JF Bastienb6091df2015-08-25 22:58:05 +0000316}
317
Fangrui Songbcd24b22020-02-13 21:58:16 -0800318void WebAssemblyAsmPrinter::emitInstruction(const MachineInstr *MI) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000319 LLVM_DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
JF Bastienb9073fb2015-07-22 21:28:15 +0000320
Dan Gohmane51c0582015-10-06 00:27:55 +0000321 switch (MI->getOpcode()) {
Thomas Lively0ff82ac2018-10-13 07:09:10 +0000322 case WebAssembly::ARGUMENT_i32:
323 case WebAssembly::ARGUMENT_i32_S:
324 case WebAssembly::ARGUMENT_i64:
325 case WebAssembly::ARGUMENT_i64_S:
326 case WebAssembly::ARGUMENT_f32:
327 case WebAssembly::ARGUMENT_f32_S:
328 case WebAssembly::ARGUMENT_f64:
329 case WebAssembly::ARGUMENT_f64_S:
Derek Schuff39bf39f2016-08-02 23:16:09 +0000330 case WebAssembly::ARGUMENT_v16i8:
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +0000331 case WebAssembly::ARGUMENT_v16i8_S:
Derek Schuff39bf39f2016-08-02 23:16:09 +0000332 case WebAssembly::ARGUMENT_v8i16:
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +0000333 case WebAssembly::ARGUMENT_v8i16_S:
Derek Schuff39bf39f2016-08-02 23:16:09 +0000334 case WebAssembly::ARGUMENT_v4i32:
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +0000335 case WebAssembly::ARGUMENT_v4i32_S:
Derek Schuff51ed1312018-08-07 21:24:01 +0000336 case WebAssembly::ARGUMENT_v2i64:
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +0000337 case WebAssembly::ARGUMENT_v2i64_S:
Derek Schuff39bf39f2016-08-02 23:16:09 +0000338 case WebAssembly::ARGUMENT_v4f32:
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +0000339 case WebAssembly::ARGUMENT_v4f32_S:
Derek Schuff51ed1312018-08-07 21:24:01 +0000340 case WebAssembly::ARGUMENT_v2f64:
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +0000341 case WebAssembly::ARGUMENT_v2f64_S:
Dan Gohmancf4748f2015-11-12 17:04:33 +0000342 // These represent values which are live into the function entry, so there's
343 // no instruction to emit.
Dan Gohmane51c0582015-10-06 00:27:55 +0000344 break;
Thomas Lively00f9e5a2019-10-09 21:42:08 +0000345 case WebAssembly::FALLTHROUGH_RETURN: {
Dan Gohmanb7c24002016-05-21 00:21:56 +0000346 // These instructions represent the implicit return at the end of a
Thomas Lively00f9e5a2019-10-09 21:42:08 +0000347 // function body.
Dan Gohmanb7c24002016-05-21 00:21:56 +0000348 if (isVerbose()) {
Thomas Lively00f9e5a2019-10-09 21:42:08 +0000349 OutStreamer->AddComment("fallthrough-return");
Dan Gohmanb7c24002016-05-21 00:21:56 +0000350 OutStreamer->AddBlankLine();
351 }
352 break;
353 }
Heejin Ahn55146582019-05-28 22:09:12 +0000354 case WebAssembly::COMPILER_FENCE:
355 // This is a compiler barrier that prevents instruction reordering during
356 // backend compilation, and should not be emitted.
357 break;
Heejin Ahnd6f48782019-01-30 03:21:57 +0000358 case WebAssembly::EXTRACT_EXCEPTION_I32:
359 case WebAssembly::EXTRACT_EXCEPTION_I32_S:
360 // These are pseudo instructions that simulates popping values from stack.
361 // We print these only when we have -wasm-keep-registers on for assembly
362 // readability.
363 if (!WasmKeepRegisters)
364 break;
365 LLVM_FALLTHROUGH;
Dan Gohmane51c0582015-10-06 00:27:55 +0000366 default: {
Dan Gohmancf4748f2015-11-12 17:04:33 +0000367 WebAssemblyMCInstLower MCInstLowering(OutContext, *this);
368 MCInst TmpInst;
Heejin Ahn18c56a02019-02-04 19:13:39 +0000369 MCInstLowering.lower(MI, TmpInst);
Dan Gohmancf4748f2015-11-12 17:04:33 +0000370 EmitToStreamer(*OutStreamer, TmpInst);
Dan Gohmane51c0582015-10-06 00:27:55 +0000371 break;
372 }
Dan Gohman4f52e002015-09-09 00:52:47 +0000373 }
JF Bastienb9073fb2015-07-22 21:28:15 +0000374}
375
Dan Gohmanf19ed562015-11-13 01:42:29 +0000376bool WebAssemblyAsmPrinter::PrintAsmOperand(const MachineInstr *MI,
Nick Desaulniers5277b3f2019-04-10 16:38:43 +0000377 unsigned OpNo,
Dan Gohmanf19ed562015-11-13 01:42:29 +0000378 const char *ExtraCode,
379 raw_ostream &OS) {
Dan Gohman30a42bf2015-12-16 17:15:17 +0000380 // First try the generic code, which knows about modifiers like 'c' and 'n'.
Nick Desaulniers5277b3f2019-04-10 16:38:43 +0000381 if (!AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, OS))
Dan Gohman30a42bf2015-12-16 17:15:17 +0000382 return false;
383
Dan Gohmanf19ed562015-11-13 01:42:29 +0000384 if (!ExtraCode) {
385 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohman30a42bf2015-12-16 17:15:17 +0000386 switch (MO.getType()) {
387 case MachineOperand::MO_Immediate:
Dan Gohmanf19ed562015-11-13 01:42:29 +0000388 OS << MO.getImm();
Dan Gohman30a42bf2015-12-16 17:15:17 +0000389 return false;
390 case MachineOperand::MO_Register:
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +0000391 // FIXME: only opcode that still contains registers, as required by
392 // MachineInstr::getDebugVariable().
393 assert(MI->getOpcode() == WebAssembly::INLINEASM);
Dan Gohmanf19ed562015-11-13 01:42:29 +0000394 OS << regToString(MO);
Dan Gohman30a42bf2015-12-16 17:15:17 +0000395 return false;
396 case MachineOperand::MO_GlobalAddress:
Nick Desaulniers7ab164c2019-04-26 18:45:04 +0000397 PrintSymbolOperand(MO, OS);
Dan Gohman30a42bf2015-12-16 17:15:17 +0000398 return false;
399 case MachineOperand::MO_ExternalSymbol:
400 GetExternalSymbolSymbol(MO.getSymbolName())->print(OS, MAI);
401 printOffset(MO.getOffset(), OS);
402 return false;
403 case MachineOperand::MO_MachineBasicBlock:
404 MO.getMBB()->getSymbol()->print(OS, MAI);
405 return false;
406 default:
407 break;
408 }
Dan Gohmanf19ed562015-11-13 01:42:29 +0000409 }
410
Dan Gohman30a42bf2015-12-16 17:15:17 +0000411 return true;
Dan Gohmanf19ed562015-11-13 01:42:29 +0000412}
413
414bool WebAssemblyAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
415 unsigned OpNo,
Dan Gohmanf19ed562015-11-13 01:42:29 +0000416 const char *ExtraCode,
417 raw_ostream &OS) {
Dan Gohmanb465aa02017-11-08 19:18:08 +0000418 // The current approach to inline asm is that "r" constraints are expressed
419 // as local indices, rather than values on the operand stack. This simplifies
420 // using "r" as it eliminates the need to push and pop the values in a
421 // particular order, however it also makes it impossible to have an "m"
422 // constraint. So we don't support it.
Dan Gohmanf19ed562015-11-13 01:42:29 +0000423
Nick Desaulniers5277b3f2019-04-10 16:38:43 +0000424 return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, ExtraCode, OS);
Dan Gohmanf19ed562015-11-13 01:42:29 +0000425}
426
JF Bastienb9073fb2015-07-22 21:28:15 +0000427// Force static initialization.
Tom Stellard0dbcb362020-01-14 19:15:07 -0800428extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeWebAssemblyAsmPrinter() {
Mehdi Aminif42454b2016-10-09 23:00:34 +0000429 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(getTheWebAssemblyTarget32());
430 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(getTheWebAssemblyTarget64());
JF Bastienb9073fb2015-07-22 21:28:15 +0000431}