blob: b5d4d369b7265799851a05285eed3852df5dca31 [file] [log] [blame]
Dan Gohman10e730a2015-06-29 23:51:55 +00001//=- WebAssemblyInstPrinter.cpp - WebAssembly assembly instruction printing -=//
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
Dan Gohman10e730a2015-06-29 23:51:55 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// Print MCInst instructions to wasm format.
Dan Gohman10e730a2015-06-29 23:51:55 +000011///
12//===----------------------------------------------------------------------===//
13
David L. Jonesa263aa22019-05-13 03:32:41 +000014#include "MCTargetDesc/WebAssemblyInstPrinter.h"
Dan Gohman7a6b9822015-11-29 22:32:02 +000015#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000016#include "WebAssembly.h"
Dan Gohman058fce52015-11-13 00:21:05 +000017#include "WebAssemblyMachineFunctionInfo.h"
Dan Gohman1d68e80f2016-01-12 19:14:46 +000018#include "llvm/ADT/SmallSet.h"
19#include "llvm/ADT/StringExtras.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000020#include "llvm/CodeGen/TargetRegisterInfo.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000021#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCInst.h"
23#include "llvm/MC/MCInstrInfo.h"
24#include "llvm/MC/MCSubtargetInfo.h"
25#include "llvm/MC/MCSymbol.h"
26#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Support/FormattedStream.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000028using namespace llvm;
29
30#define DEBUG_TYPE "asm-printer"
31
JF Bastienb9073fb2015-07-22 21:28:15 +000032#include "WebAssemblyGenAsmWriter.inc"
33
Dan Gohman10e730a2015-06-29 23:51:55 +000034WebAssemblyInstPrinter::WebAssemblyInstPrinter(const MCAsmInfo &MAI,
35 const MCInstrInfo &MII,
36 const MCRegisterInfo &MRI)
Heejin Ahn3103d3d2018-10-25 23:45:48 +000037 : MCInstPrinter(MAI, MII, MRI) {}
Dan Gohman10e730a2015-06-29 23:51:55 +000038
39void WebAssemblyInstPrinter::printRegName(raw_ostream &OS,
40 unsigned RegNo) const {
Dan Gohman058fce52015-11-13 00:21:05 +000041 assert(RegNo != WebAssemblyFunctionInfo::UnusedReg);
Thomas Lively6a87dda2019-01-08 06:25:55 +000042 // Note that there's an implicit local.get/local.set here!
Dan Gohman4ba48162015-11-18 16:12:01 +000043 OS << "$" << RegNo;
Dan Gohman10e730a2015-06-29 23:51:55 +000044}
45
46void WebAssemblyInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
47 StringRef Annot,
Sam Clegg16c16822018-05-10 22:16:44 +000048 const MCSubtargetInfo &STI) {
Dan Gohmandd20c702015-12-21 16:50:41 +000049 // Print the instruction (this uses the AsmStrings from the .td files).
JF Bastienb9073fb2015-07-22 21:28:15 +000050 printInstruction(MI, OS);
Dan Gohmancf4748f2015-11-12 17:04:33 +000051
Dan Gohmandd20c702015-12-21 16:50:41 +000052 // Print any additional variadic operands.
Dan Gohmancf4748f2015-11-12 17:04:33 +000053 const MCInstrDesc &Desc = MII.get(MI->getOpcode());
54 if (Desc.isVariadic())
Heejin Ahn18c56a02019-02-04 19:13:39 +000055 for (auto I = Desc.getNumOperands(), E = MI->getNumOperands(); I < E; ++I) {
Dan Gohmanf50d9642016-10-25 16:55:52 +000056 // FIXME: For CALL_INDIRECT_VOID, don't print a leading comma, because
57 // we have an extra flags operand which is not currently printed, for
58 // compatiblity reasons.
Heejin Ahn18c56a02019-02-04 19:13:39 +000059 if (I != 0 && ((MI->getOpcode() != WebAssembly::CALL_INDIRECT_VOID &&
Heejin Ahnf208f632018-09-05 01:27:38 +000060 MI->getOpcode() != WebAssembly::CALL_INDIRECT_VOID_S) ||
Heejin Ahn18c56a02019-02-04 19:13:39 +000061 I != Desc.getNumOperands()))
Dan Gohman53828fd2015-11-23 16:50:18 +000062 OS << ", ";
Heejin Ahn18c56a02019-02-04 19:13:39 +000063 printOperand(MI, I, OS);
Dan Gohmancf4748f2015-11-12 17:04:33 +000064 }
65
Dan Gohmandd20c702015-12-21 16:50:41 +000066 // Print any added annotation.
JF Bastienb9073fb2015-07-22 21:28:15 +000067 printAnnotation(OS, Annot);
Dan Gohman1d68e80f2016-01-12 19:14:46 +000068
69 if (CommentStream) {
70 // Observe any effects on the control flow stack, for use in annotating
71 // control flow label references.
Heejin Ahn3103d3d2018-10-25 23:45:48 +000072 unsigned Opc = MI->getOpcode();
73 switch (Opc) {
Dan Gohman1d68e80f2016-01-12 19:14:46 +000074 default:
75 break;
Heejin Ahn3103d3d2018-10-25 23:45:48 +000076
Richard Trieu01f99f32018-08-11 04:18:05 +000077 case WebAssembly::LOOP:
Heejin Ahn3103d3d2018-10-25 23:45:48 +000078 case WebAssembly::LOOP_S:
Dan Gohman3a643e82016-10-06 22:10:23 +000079 printAnnotation(OS, "label" + utostr(ControlFlowCounter) + ':');
80 ControlFlowStack.push_back(std::make_pair(ControlFlowCounter++, true));
Dan Gohman1d68e80f2016-01-12 19:14:46 +000081 break;
Heejin Ahn3103d3d2018-10-25 23:45:48 +000082
Dan Gohman1d68e80f2016-01-12 19:14:46 +000083 case WebAssembly::BLOCK:
Richard Trieu01f99f32018-08-11 04:18:05 +000084 case WebAssembly::BLOCK_S:
Dan Gohman1d68e80f2016-01-12 19:14:46 +000085 ControlFlowStack.push_back(std::make_pair(ControlFlowCounter++, false));
86 break;
Heejin Ahn3103d3d2018-10-25 23:45:48 +000087
88 case WebAssembly::TRY:
89 case WebAssembly::TRY_S:
90 ControlFlowStack.push_back(std::make_pair(ControlFlowCounter++, false));
91 EHPadStack.push_back(EHPadStackCounter++);
92 LastSeenEHInst = TRY;
93 break;
94
Dan Gohman1d68e80f2016-01-12 19:14:46 +000095 case WebAssembly::END_LOOP:
Richard Trieu01f99f32018-08-11 04:18:05 +000096 case WebAssembly::END_LOOP_S:
Wouter van Oortmerssen98432952019-01-03 22:59:59 +000097 if (ControlFlowStack.empty()) {
98 printAnnotation(OS, "End marker mismatch!");
99 } else {
100 ControlFlowStack.pop_back();
101 }
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000102 break;
Heejin Ahn3103d3d2018-10-25 23:45:48 +0000103
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000104 case WebAssembly::END_BLOCK:
Richard Trieu01f99f32018-08-11 04:18:05 +0000105 case WebAssembly::END_BLOCK_S:
Wouter van Oortmerssen98432952019-01-03 22:59:59 +0000106 if (ControlFlowStack.empty()) {
107 printAnnotation(OS, "End marker mismatch!");
108 } else {
109 printAnnotation(
110 OS, "label" + utostr(ControlFlowStack.pop_back_val().first) + ':');
111 }
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000112 break;
Heejin Ahn3103d3d2018-10-25 23:45:48 +0000113
114 case WebAssembly::END_TRY:
115 case WebAssembly::END_TRY_S:
Wouter van Oortmerssen98432952019-01-03 22:59:59 +0000116 if (ControlFlowStack.empty()) {
117 printAnnotation(OS, "End marker mismatch!");
118 } else {
119 printAnnotation(
120 OS, "label" + utostr(ControlFlowStack.pop_back_val().first) + ':');
121 LastSeenEHInst = END_TRY;
122 }
Heejin Ahn3103d3d2018-10-25 23:45:48 +0000123 break;
124
Heejin Ahnd6f48782019-01-30 03:21:57 +0000125 case WebAssembly::CATCH:
126 case WebAssembly::CATCH_S:
127 if (EHPadStack.empty()) {
128 printAnnotation(OS, "try-catch mismatch!");
129 } else {
130 printAnnotation(OS, "catch" + utostr(EHPadStack.pop_back_val()) + ':');
Heejin Ahn3103d3d2018-10-25 23:45:48 +0000131 }
Heejin Ahn3103d3d2018-10-25 23:45:48 +0000132 break;
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000133 }
134
135 // Annotate any control flow label references.
Heejin Ahnd6f48782019-01-30 03:21:57 +0000136
137 // rethrow instruction does not take any depth argument and rethrows to the
138 // nearest enclosing catch scope, if any. If there's no enclosing catch
139 // scope, it throws up to the caller.
140 if (Opc == WebAssembly::RETHROW || Opc == WebAssembly::RETHROW_S) {
141 if (EHPadStack.empty()) {
142 printAnnotation(OS, "to caller");
Wouter van Oortmerssend3c544a2018-12-17 22:04:44 +0000143 } else {
Heejin Ahnd6f48782019-01-30 03:21:57 +0000144 printAnnotation(OS, "down to catch" + utostr(EHPadStack.back()));
Wouter van Oortmerssend3c544a2018-12-17 22:04:44 +0000145 }
Heejin Ahn3103d3d2018-10-25 23:45:48 +0000146
Heejin Ahnd6f48782019-01-30 03:21:57 +0000147 } else {
148 unsigned NumFixedOperands = Desc.NumOperands;
149 SmallSet<uint64_t, 8> Printed;
150 for (unsigned I = 0, E = MI->getNumOperands(); I < E; ++I) {
151 // See if this operand denotes a basic block target.
152 if (I < NumFixedOperands) {
153 // A non-variable_ops operand, check its type.
154 if (Desc.OpInfo[I].OperandType != WebAssembly::OPERAND_BASIC_BLOCK)
155 continue;
Heejin Ahn3103d3d2018-10-25 23:45:48 +0000156 } else {
Heejin Ahnd6f48782019-01-30 03:21:57 +0000157 // A variable_ops operand, which currently can be immediates (used in
158 // br_table) which are basic block targets, or for call instructions
159 // when using -wasm-keep-registers (in which case they are registers,
160 // and should not be processed).
161 if (!MI->getOperand(I).isImm())
162 continue;
Heejin Ahn3103d3d2018-10-25 23:45:48 +0000163 }
Heejin Ahnd6f48782019-01-30 03:21:57 +0000164 uint64_t Depth = MI->getOperand(I).getImm();
165 if (!Printed.insert(Depth).second)
166 continue;
Wouter van Oortmerssen98432952019-01-03 22:59:59 +0000167 if (Depth >= ControlFlowStack.size()) {
168 printAnnotation(OS, "Invalid depth argument!");
169 } else {
170 const auto &Pair = ControlFlowStack.rbegin()[Depth];
171 printAnnotation(OS, utostr(Depth) + ": " +
172 (Pair.second ? "up" : "down") + " to label" +
173 utostr(Pair.first));
174 }
Heejin Ahn3103d3d2018-10-25 23:45:48 +0000175 }
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000176 }
177 }
Dan Gohmancf4748f2015-11-12 17:04:33 +0000178}
179
180static std::string toString(const APFloat &FP) {
Dan Gohmanaa742912016-02-16 15:14:23 +0000181 // Print NaNs with custom payloads specially.
Heejin Ahnf208f632018-09-05 01:27:38 +0000182 if (FP.isNaN() && !FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics())) &&
Dan Gohman207ed222016-12-22 16:00:55 +0000183 !FP.bitwiseIsEqual(
184 APFloat::getQNaN(FP.getSemantics(), /*Negative=*/true))) {
Dan Gohmanaa742912016-02-16 15:14:23 +0000185 APInt AI = FP.bitcastToAPInt();
Heejin Ahnf208f632018-09-05 01:27:38 +0000186 return std::string(AI.isNegative() ? "-" : "") + "nan:0x" +
187 utohexstr(AI.getZExtValue() &
188 (AI.getBitWidth() == 32 ? INT64_C(0x007fffff)
189 : INT64_C(0x000fffffffffffff)),
190 /*LowerCase=*/true);
Dan Gohmanaa742912016-02-16 15:14:23 +0000191 }
192
193 // Use C99's hexadecimal floating-point representation.
Dan Gohmancf4748f2015-11-12 17:04:33 +0000194 static const size_t BufBytes = 128;
Heejin Ahn18c56a02019-02-04 19:13:39 +0000195 char Buf[BufBytes];
Dan Gohmancf4748f2015-11-12 17:04:33 +0000196 auto Written = FP.convertToHexString(
Rui Ueyama49a3ad22019-07-16 04:46:31 +0000197 Buf, /*HexDigits=*/0, /*UpperCase=*/false, APFloat::rmNearestTiesToEven);
Dan Gohmancf4748f2015-11-12 17:04:33 +0000198 (void)Written;
199 assert(Written != 0);
200 assert(Written < BufBytes);
Heejin Ahn18c56a02019-02-04 19:13:39 +0000201 return Buf;
Dan Gohman10e730a2015-06-29 23:51:55 +0000202}
JF Bastienaf111db2015-08-24 22:16:48 +0000203
204void WebAssemblyInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
205 raw_ostream &O) {
206 const MCOperand &Op = MI->getOperand(OpNo);
Dan Gohmane9361d52015-11-05 19:28:16 +0000207 if (Op.isReg()) {
Dan Gohman4ba48162015-11-18 16:12:01 +0000208 unsigned WAReg = Op.getReg();
209 if (int(WAReg) >= 0)
210 printRegName(O, WAReg);
211 else if (OpNo >= MII.get(MI->getOpcode()).getNumDefs())
Dan Gohmanb7c24002016-05-21 00:21:56 +0000212 O << "$pop" << WebAssemblyFunctionInfo::getWARegStackId(WAReg);
Dan Gohman4ba48162015-11-18 16:12:01 +0000213 else if (WAReg != WebAssemblyFunctionInfo::UnusedReg)
Dan Gohmanb7c24002016-05-21 00:21:56 +0000214 O << "$push" << WebAssemblyFunctionInfo::getWARegStackId(WAReg);
Dan Gohman4ba48162015-11-18 16:12:01 +0000215 else
Dan Gohman71008092016-05-17 23:19:03 +0000216 O << "$drop";
Dan Gohman700515f2015-11-23 21:55:57 +0000217 // Add a '=' suffix if this is a def.
218 if (OpNo < MII.get(MI->getOpcode()).getNumDefs())
219 O << '=';
Dan Gohman53828fd2015-11-23 16:50:18 +0000220 } else if (Op.isImm()) {
Dan Gohman3acb1872016-10-24 23:27:49 +0000221 O << Op.getImm();
Dan Gohman85159ca2016-01-12 01:45:12 +0000222 } else if (Op.isFPImm()) {
Dan Gohmanaa742912016-02-16 15:14:23 +0000223 const MCInstrDesc &Desc = MII.get(MI->getOpcode());
Dan Gohmanaa742912016-02-16 15:14:23 +0000224 const MCOperandInfo &Info = Desc.OpInfo[OpNo];
Dan Gohman4b8e8be2016-10-03 21:31:31 +0000225 if (Info.OperandType == WebAssembly::OPERAND_F32IMM) {
Dan Gohmanaa742912016-02-16 15:14:23 +0000226 // TODO: MC converts all floating point immediate operands to double.
227 // This is fine for numeric values, but may cause NaNs to change bits.
Benjamin Kramera01e97d2018-02-22 22:29:27 +0000228 O << ::toString(APFloat(float(Op.getFPImm())));
Dan Gohmanaa742912016-02-16 15:14:23 +0000229 } else {
Dan Gohman4b8e8be2016-10-03 21:31:31 +0000230 assert(Info.OperandType == WebAssembly::OPERAND_F64IMM);
Benjamin Kramera01e97d2018-02-22 22:29:27 +0000231 O << ::toString(APFloat(Op.getFPImm()));
Dan Gohmanaa742912016-02-16 15:14:23 +0000232 }
Dan Gohman85159ca2016-01-12 01:45:12 +0000233 } else {
JF Bastienaf111db2015-08-24 22:16:48 +0000234 assert(Op.isExpr() && "unknown operand kind in printOperand");
235 Op.getExpr()->print(O, &MAI);
236 }
237}
Dan Gohman5e0886b2015-12-06 19:42:29 +0000238
Wouter van Oortmerssend3c544a2018-12-17 22:04:44 +0000239void WebAssemblyInstPrinter::printBrList(const MCInst *MI, unsigned OpNo,
240 raw_ostream &O) {
241 O << "{";
242 for (unsigned I = OpNo, E = MI->getNumOperands(); I != E; ++I) {
243 if (I != OpNo)
244 O << ", ";
245 O << MI->getOperand(I).getImm();
246 }
247 O << "}";
248}
249
Heejin Ahnf208f632018-09-05 01:27:38 +0000250void WebAssemblyInstPrinter::printWebAssemblyP2AlignOperand(const MCInst *MI,
251 unsigned OpNo,
252 raw_ostream &O) {
Dan Gohmanbb372242016-01-26 03:39:31 +0000253 int64_t Imm = MI->getOperand(OpNo).getImm();
254 if (Imm == WebAssembly::GetDefaultP2Align(MI->getOpcode()))
255 return;
256 O << ":p2align=" << Imm;
257}
258
Heejin Ahnf208f632018-09-05 01:27:38 +0000259void WebAssemblyInstPrinter::printWebAssemblySignatureOperand(const MCInst *MI,
260 unsigned OpNo,
261 raw_ostream &O) {
Wouter van Oortmerssen98432952019-01-03 22:59:59 +0000262 auto Imm = static_cast<unsigned>(MI->getOperand(OpNo).getImm());
263 if (Imm != wasm::WASM_TYPE_NORESULT)
264 O << WebAssembly::anyTypeToString(Imm);
265}
266
267// We have various enums representing a subset of these types, use this
268// function to convert any of them to text.
269const char *llvm::WebAssembly::anyTypeToString(unsigned Ty) {
270 switch (Ty) {
271 case wasm::WASM_TYPE_I32:
272 return "i32";
273 case wasm::WASM_TYPE_I64:
274 return "i64";
275 case wasm::WASM_TYPE_F32:
276 return "f32";
277 case wasm::WASM_TYPE_F64:
278 return "f64";
279 case wasm::WASM_TYPE_V128:
280 return "v128";
Thomas Lively6a87dda2019-01-08 06:25:55 +0000281 case wasm::WASM_TYPE_FUNCREF:
282 return "funcref";
Wouter van Oortmerssen98432952019-01-03 22:59:59 +0000283 case wasm::WASM_TYPE_FUNC:
284 return "func";
Heejin Ahn9f96a582019-07-15 22:49:25 +0000285 case wasm::WASM_TYPE_EXNREF:
286 return "exnref";
Wouter van Oortmerssen98432952019-01-03 22:59:59 +0000287 case wasm::WASM_TYPE_NORESULT:
288 return "void";
Wouter van Oortmerssenad72f682019-01-02 23:23:51 +0000289 default:
Wouter van Oortmerssen98432952019-01-03 22:59:59 +0000290 return "invalid_type";
Dan Gohman2726b882016-10-06 22:29:32 +0000291 }
292}
293
Wouter van Oortmerssen98432952019-01-03 22:59:59 +0000294const char *llvm::WebAssembly::typeToString(wasm::ValType Ty) {
295 return anyTypeToString(static_cast<unsigned>(Ty));
Dan Gohman5e0886b2015-12-06 19:42:29 +0000296}