blob: bc63382aa8f9b0aa6bf2776774180f43a98e729f [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"
Wouter van Oortmerssen87af0b12019-08-01 18:08:26 +000018#include "WebAssemblyUtilities.h"
Dan Gohman1d68e80f2016-01-12 19:14:46 +000019#include "llvm/ADT/SmallSet.h"
20#include "llvm/ADT/StringExtras.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000021#include "llvm/CodeGen/TargetRegisterInfo.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000022#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCInst.h"
24#include "llvm/MC/MCInstrInfo.h"
25#include "llvm/MC/MCSubtargetInfo.h"
26#include "llvm/MC/MCSymbol.h"
27#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/FormattedStream.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000029using namespace llvm;
30
31#define DEBUG_TYPE "asm-printer"
32
JF Bastienb9073fb2015-07-22 21:28:15 +000033#include "WebAssemblyGenAsmWriter.inc"
34
Dan Gohman10e730a2015-06-29 23:51:55 +000035WebAssemblyInstPrinter::WebAssemblyInstPrinter(const MCAsmInfo &MAI,
36 const MCInstrInfo &MII,
37 const MCRegisterInfo &MRI)
Heejin Ahn3103d3d2018-10-25 23:45:48 +000038 : MCInstPrinter(MAI, MII, MRI) {}
Dan Gohman10e730a2015-06-29 23:51:55 +000039
40void WebAssemblyInstPrinter::printRegName(raw_ostream &OS,
41 unsigned RegNo) const {
Dan Gohman058fce52015-11-13 00:21:05 +000042 assert(RegNo != WebAssemblyFunctionInfo::UnusedReg);
Thomas Lively6a87dda2019-01-08 06:25:55 +000043 // Note that there's an implicit local.get/local.set here!
Dan Gohman4ba48162015-11-18 16:12:01 +000044 OS << "$" << RegNo;
Dan Gohman10e730a2015-06-29 23:51:55 +000045}
46
47void WebAssemblyInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
48 StringRef Annot,
Sam Clegg16c16822018-05-10 22:16:44 +000049 const MCSubtargetInfo &STI) {
Dan Gohmandd20c702015-12-21 16:50:41 +000050 // Print the instruction (this uses the AsmStrings from the .td files).
JF Bastienb9073fb2015-07-22 21:28:15 +000051 printInstruction(MI, OS);
Dan Gohmancf4748f2015-11-12 17:04:33 +000052
Dan Gohmandd20c702015-12-21 16:50:41 +000053 // Print any additional variadic operands.
Dan Gohmancf4748f2015-11-12 17:04:33 +000054 const MCInstrDesc &Desc = MII.get(MI->getOpcode());
55 if (Desc.isVariadic())
Heejin Ahn18c56a02019-02-04 19:13:39 +000056 for (auto I = Desc.getNumOperands(), E = MI->getNumOperands(); I < E; ++I) {
Dan Gohmanf50d9642016-10-25 16:55:52 +000057 // FIXME: For CALL_INDIRECT_VOID, don't print a leading comma, because
58 // we have an extra flags operand which is not currently printed, for
59 // compatiblity reasons.
Heejin Ahn18c56a02019-02-04 19:13:39 +000060 if (I != 0 && ((MI->getOpcode() != WebAssembly::CALL_INDIRECT_VOID &&
Heejin Ahnf208f632018-09-05 01:27:38 +000061 MI->getOpcode() != WebAssembly::CALL_INDIRECT_VOID_S) ||
Heejin Ahn18c56a02019-02-04 19:13:39 +000062 I != Desc.getNumOperands()))
Dan Gohman53828fd2015-11-23 16:50:18 +000063 OS << ", ";
Heejin Ahn18c56a02019-02-04 19:13:39 +000064 printOperand(MI, I, OS);
Dan Gohmancf4748f2015-11-12 17:04:33 +000065 }
66
Dan Gohmandd20c702015-12-21 16:50:41 +000067 // Print any added annotation.
JF Bastienb9073fb2015-07-22 21:28:15 +000068 printAnnotation(OS, Annot);
Dan Gohman1d68e80f2016-01-12 19:14:46 +000069
70 if (CommentStream) {
71 // Observe any effects on the control flow stack, for use in annotating
72 // control flow label references.
Heejin Ahn3103d3d2018-10-25 23:45:48 +000073 unsigned Opc = MI->getOpcode();
74 switch (Opc) {
Dan Gohman1d68e80f2016-01-12 19:14:46 +000075 default:
76 break;
Heejin Ahn3103d3d2018-10-25 23:45:48 +000077
Richard Trieu01f99f32018-08-11 04:18:05 +000078 case WebAssembly::LOOP:
Heejin Ahn3103d3d2018-10-25 23:45:48 +000079 case WebAssembly::LOOP_S:
Dan Gohman3a643e82016-10-06 22:10:23 +000080 printAnnotation(OS, "label" + utostr(ControlFlowCounter) + ':');
81 ControlFlowStack.push_back(std::make_pair(ControlFlowCounter++, true));
Dan Gohman1d68e80f2016-01-12 19:14:46 +000082 break;
Heejin Ahn3103d3d2018-10-25 23:45:48 +000083
Dan Gohman1d68e80f2016-01-12 19:14:46 +000084 case WebAssembly::BLOCK:
Richard Trieu01f99f32018-08-11 04:18:05 +000085 case WebAssembly::BLOCK_S:
Dan Gohman1d68e80f2016-01-12 19:14:46 +000086 ControlFlowStack.push_back(std::make_pair(ControlFlowCounter++, false));
87 break;
Heejin Ahn3103d3d2018-10-25 23:45:48 +000088
89 case WebAssembly::TRY:
90 case WebAssembly::TRY_S:
91 ControlFlowStack.push_back(std::make_pair(ControlFlowCounter++, false));
92 EHPadStack.push_back(EHPadStackCounter++);
93 LastSeenEHInst = TRY;
94 break;
95
Dan Gohman1d68e80f2016-01-12 19:14:46 +000096 case WebAssembly::END_LOOP:
Richard Trieu01f99f32018-08-11 04:18:05 +000097 case WebAssembly::END_LOOP_S:
Wouter van Oortmerssen98432952019-01-03 22:59:59 +000098 if (ControlFlowStack.empty()) {
99 printAnnotation(OS, "End marker mismatch!");
100 } else {
101 ControlFlowStack.pop_back();
102 }
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000103 break;
Heejin Ahn3103d3d2018-10-25 23:45:48 +0000104
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000105 case WebAssembly::END_BLOCK:
Richard Trieu01f99f32018-08-11 04:18:05 +0000106 case WebAssembly::END_BLOCK_S:
Wouter van Oortmerssen98432952019-01-03 22:59:59 +0000107 if (ControlFlowStack.empty()) {
108 printAnnotation(OS, "End marker mismatch!");
109 } else {
110 printAnnotation(
111 OS, "label" + utostr(ControlFlowStack.pop_back_val().first) + ':');
112 }
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000113 break;
Heejin Ahn3103d3d2018-10-25 23:45:48 +0000114
115 case WebAssembly::END_TRY:
116 case WebAssembly::END_TRY_S:
Wouter van Oortmerssen98432952019-01-03 22:59:59 +0000117 if (ControlFlowStack.empty()) {
118 printAnnotation(OS, "End marker mismatch!");
119 } else {
120 printAnnotation(
121 OS, "label" + utostr(ControlFlowStack.pop_back_val().first) + ':');
122 LastSeenEHInst = END_TRY;
123 }
Heejin Ahn3103d3d2018-10-25 23:45:48 +0000124 break;
125
Heejin Ahnd6f48782019-01-30 03:21:57 +0000126 case WebAssembly::CATCH:
127 case WebAssembly::CATCH_S:
128 if (EHPadStack.empty()) {
129 printAnnotation(OS, "try-catch mismatch!");
130 } else {
131 printAnnotation(OS, "catch" + utostr(EHPadStack.pop_back_val()) + ':');
Heejin Ahn3103d3d2018-10-25 23:45:48 +0000132 }
Heejin Ahn3103d3d2018-10-25 23:45:48 +0000133 break;
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000134 }
135
136 // Annotate any control flow label references.
Heejin Ahnd6f48782019-01-30 03:21:57 +0000137
138 // rethrow instruction does not take any depth argument and rethrows to the
139 // nearest enclosing catch scope, if any. If there's no enclosing catch
140 // scope, it throws up to the caller.
141 if (Opc == WebAssembly::RETHROW || Opc == WebAssembly::RETHROW_S) {
142 if (EHPadStack.empty()) {
143 printAnnotation(OS, "to caller");
Wouter van Oortmerssend3c544a2018-12-17 22:04:44 +0000144 } else {
Heejin Ahnd6f48782019-01-30 03:21:57 +0000145 printAnnotation(OS, "down to catch" + utostr(EHPadStack.back()));
Wouter van Oortmerssend3c544a2018-12-17 22:04:44 +0000146 }
Heejin Ahn3103d3d2018-10-25 23:45:48 +0000147
Heejin Ahnd6f48782019-01-30 03:21:57 +0000148 } else {
149 unsigned NumFixedOperands = Desc.NumOperands;
150 SmallSet<uint64_t, 8> Printed;
151 for (unsigned I = 0, E = MI->getNumOperands(); I < E; ++I) {
152 // See if this operand denotes a basic block target.
153 if (I < NumFixedOperands) {
154 // A non-variable_ops operand, check its type.
155 if (Desc.OpInfo[I].OperandType != WebAssembly::OPERAND_BASIC_BLOCK)
156 continue;
Heejin Ahn3103d3d2018-10-25 23:45:48 +0000157 } else {
Heejin Ahnd6f48782019-01-30 03:21:57 +0000158 // A variable_ops operand, which currently can be immediates (used in
159 // br_table) which are basic block targets, or for call instructions
160 // when using -wasm-keep-registers (in which case they are registers,
161 // and should not be processed).
162 if (!MI->getOperand(I).isImm())
163 continue;
Heejin Ahn3103d3d2018-10-25 23:45:48 +0000164 }
Heejin Ahnd6f48782019-01-30 03:21:57 +0000165 uint64_t Depth = MI->getOperand(I).getImm();
166 if (!Printed.insert(Depth).second)
167 continue;
Wouter van Oortmerssen98432952019-01-03 22:59:59 +0000168 if (Depth >= ControlFlowStack.size()) {
169 printAnnotation(OS, "Invalid depth argument!");
170 } else {
171 const auto &Pair = ControlFlowStack.rbegin()[Depth];
172 printAnnotation(OS, utostr(Depth) + ": " +
173 (Pair.second ? "up" : "down") + " to label" +
174 utostr(Pair.first));
175 }
Heejin Ahn3103d3d2018-10-25 23:45:48 +0000176 }
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000177 }
178 }
Dan Gohmancf4748f2015-11-12 17:04:33 +0000179}
180
181static std::string toString(const APFloat &FP) {
Dan Gohmanaa742912016-02-16 15:14:23 +0000182 // Print NaNs with custom payloads specially.
Heejin Ahnf208f632018-09-05 01:27:38 +0000183 if (FP.isNaN() && !FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics())) &&
Dan Gohman207ed222016-12-22 16:00:55 +0000184 !FP.bitwiseIsEqual(
185 APFloat::getQNaN(FP.getSemantics(), /*Negative=*/true))) {
Dan Gohmanaa742912016-02-16 15:14:23 +0000186 APInt AI = FP.bitcastToAPInt();
Heejin Ahnf208f632018-09-05 01:27:38 +0000187 return std::string(AI.isNegative() ? "-" : "") + "nan:0x" +
188 utohexstr(AI.getZExtValue() &
189 (AI.getBitWidth() == 32 ? INT64_C(0x007fffff)
190 : INT64_C(0x000fffffffffffff)),
191 /*LowerCase=*/true);
Dan Gohmanaa742912016-02-16 15:14:23 +0000192 }
193
194 // Use C99's hexadecimal floating-point representation.
Dan Gohmancf4748f2015-11-12 17:04:33 +0000195 static const size_t BufBytes = 128;
Heejin Ahn18c56a02019-02-04 19:13:39 +0000196 char Buf[BufBytes];
Dan Gohmancf4748f2015-11-12 17:04:33 +0000197 auto Written = FP.convertToHexString(
Rui Ueyama49a3ad22019-07-16 04:46:31 +0000198 Buf, /*HexDigits=*/0, /*UpperCase=*/false, APFloat::rmNearestTiesToEven);
Dan Gohmancf4748f2015-11-12 17:04:33 +0000199 (void)Written;
200 assert(Written != 0);
201 assert(Written < BufBytes);
Heejin Ahn18c56a02019-02-04 19:13:39 +0000202 return Buf;
Dan Gohman10e730a2015-06-29 23:51:55 +0000203}
JF Bastienaf111db2015-08-24 22:16:48 +0000204
205void WebAssemblyInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
206 raw_ostream &O) {
207 const MCOperand &Op = MI->getOperand(OpNo);
Dan Gohmane9361d52015-11-05 19:28:16 +0000208 if (Op.isReg()) {
Dan Gohman4ba48162015-11-18 16:12:01 +0000209 unsigned WAReg = Op.getReg();
210 if (int(WAReg) >= 0)
211 printRegName(O, WAReg);
212 else if (OpNo >= MII.get(MI->getOpcode()).getNumDefs())
Dan Gohmanb7c24002016-05-21 00:21:56 +0000213 O << "$pop" << WebAssemblyFunctionInfo::getWARegStackId(WAReg);
Dan Gohman4ba48162015-11-18 16:12:01 +0000214 else if (WAReg != WebAssemblyFunctionInfo::UnusedReg)
Dan Gohmanb7c24002016-05-21 00:21:56 +0000215 O << "$push" << WebAssemblyFunctionInfo::getWARegStackId(WAReg);
Dan Gohman4ba48162015-11-18 16:12:01 +0000216 else
Dan Gohman71008092016-05-17 23:19:03 +0000217 O << "$drop";
Dan Gohman700515f2015-11-23 21:55:57 +0000218 // Add a '=' suffix if this is a def.
219 if (OpNo < MII.get(MI->getOpcode()).getNumDefs())
220 O << '=';
Dan Gohman53828fd2015-11-23 16:50:18 +0000221 } else if (Op.isImm()) {
Dan Gohman3acb1872016-10-24 23:27:49 +0000222 O << Op.getImm();
Dan Gohman85159ca2016-01-12 01:45:12 +0000223 } else if (Op.isFPImm()) {
Dan Gohmanaa742912016-02-16 15:14:23 +0000224 const MCInstrDesc &Desc = MII.get(MI->getOpcode());
Dan Gohmanaa742912016-02-16 15:14:23 +0000225 const MCOperandInfo &Info = Desc.OpInfo[OpNo];
Dan Gohman4b8e8be2016-10-03 21:31:31 +0000226 if (Info.OperandType == WebAssembly::OPERAND_F32IMM) {
Dan Gohmanaa742912016-02-16 15:14:23 +0000227 // TODO: MC converts all floating point immediate operands to double.
228 // This is fine for numeric values, but may cause NaNs to change bits.
Benjamin Kramera01e97d2018-02-22 22:29:27 +0000229 O << ::toString(APFloat(float(Op.getFPImm())));
Dan Gohmanaa742912016-02-16 15:14:23 +0000230 } else {
Dan Gohman4b8e8be2016-10-03 21:31:31 +0000231 assert(Info.OperandType == WebAssembly::OPERAND_F64IMM);
Benjamin Kramera01e97d2018-02-22 22:29:27 +0000232 O << ::toString(APFloat(Op.getFPImm()));
Dan Gohmanaa742912016-02-16 15:14:23 +0000233 }
Dan Gohman85159ca2016-01-12 01:45:12 +0000234 } else {
JF Bastienaf111db2015-08-24 22:16:48 +0000235 assert(Op.isExpr() && "unknown operand kind in printOperand");
Wouter van Oortmerssen87af0b12019-08-01 18:08:26 +0000236 // call_indirect instructions have a TYPEINDEX operand that we print
237 // as a signature here, such that the assembler can recover this
238 // information.
239 auto SRE = static_cast<const MCSymbolRefExpr *>(Op.getExpr());
240 if (SRE->getKind() == MCSymbolRefExpr::VK_WASM_TYPEINDEX) {
241 auto &Sym = static_cast<const MCSymbolWasm &>(SRE->getSymbol());
242 O << WebAssembly::signatureToString(Sym.getSignature());
243 } else {
244 Op.getExpr()->print(O, &MAI);
245 }
JF Bastienaf111db2015-08-24 22:16:48 +0000246 }
247}
Dan Gohman5e0886b2015-12-06 19:42:29 +0000248
Wouter van Oortmerssend3c544a2018-12-17 22:04:44 +0000249void WebAssemblyInstPrinter::printBrList(const MCInst *MI, unsigned OpNo,
250 raw_ostream &O) {
251 O << "{";
252 for (unsigned I = OpNo, E = MI->getNumOperands(); I != E; ++I) {
253 if (I != OpNo)
254 O << ", ";
255 O << MI->getOperand(I).getImm();
256 }
257 O << "}";
258}
259
Heejin Ahnf208f632018-09-05 01:27:38 +0000260void WebAssemblyInstPrinter::printWebAssemblyP2AlignOperand(const MCInst *MI,
261 unsigned OpNo,
262 raw_ostream &O) {
Dan Gohmanbb372242016-01-26 03:39:31 +0000263 int64_t Imm = MI->getOperand(OpNo).getImm();
264 if (Imm == WebAssembly::GetDefaultP2Align(MI->getOpcode()))
265 return;
266 O << ":p2align=" << Imm;
267}
268
Heejin Ahnf208f632018-09-05 01:27:38 +0000269void WebAssemblyInstPrinter::printWebAssemblySignatureOperand(const MCInst *MI,
270 unsigned OpNo,
271 raw_ostream &O) {
Wouter van Oortmerssen98432952019-01-03 22:59:59 +0000272 auto Imm = static_cast<unsigned>(MI->getOperand(OpNo).getImm());
273 if (Imm != wasm::WASM_TYPE_NORESULT)
274 O << WebAssembly::anyTypeToString(Imm);
275}
276
277// We have various enums representing a subset of these types, use this
278// function to convert any of them to text.
Wouter van Oortmerssen87af0b12019-08-01 18:08:26 +0000279const char *WebAssembly::anyTypeToString(unsigned Ty) {
Wouter van Oortmerssen98432952019-01-03 22:59:59 +0000280 switch (Ty) {
281 case wasm::WASM_TYPE_I32:
282 return "i32";
283 case wasm::WASM_TYPE_I64:
284 return "i64";
285 case wasm::WASM_TYPE_F32:
286 return "f32";
287 case wasm::WASM_TYPE_F64:
288 return "f64";
289 case wasm::WASM_TYPE_V128:
290 return "v128";
Thomas Lively6a87dda2019-01-08 06:25:55 +0000291 case wasm::WASM_TYPE_FUNCREF:
292 return "funcref";
Wouter van Oortmerssen98432952019-01-03 22:59:59 +0000293 case wasm::WASM_TYPE_FUNC:
294 return "func";
Heejin Ahn9f96a582019-07-15 22:49:25 +0000295 case wasm::WASM_TYPE_EXNREF:
296 return "exnref";
Wouter van Oortmerssen98432952019-01-03 22:59:59 +0000297 case wasm::WASM_TYPE_NORESULT:
298 return "void";
Wouter van Oortmerssenad72f682019-01-02 23:23:51 +0000299 default:
Wouter van Oortmerssen98432952019-01-03 22:59:59 +0000300 return "invalid_type";
Dan Gohman2726b882016-10-06 22:29:32 +0000301 }
302}
303
Wouter van Oortmerssen87af0b12019-08-01 18:08:26 +0000304const char *WebAssembly::typeToString(wasm::ValType Ty) {
Wouter van Oortmerssen98432952019-01-03 22:59:59 +0000305 return anyTypeToString(static_cast<unsigned>(Ty));
Dan Gohman5e0886b2015-12-06 19:42:29 +0000306}
Wouter van Oortmerssen87af0b12019-08-01 18:08:26 +0000307
308std::string WebAssembly::typeListToString(ArrayRef<wasm::ValType> List) {
309 std::string S;
310 for (auto &Ty : List) {
311 if (&Ty != &List[0]) S += ", ";
312 S += WebAssembly::typeToString(Ty);
313 }
314 return S;
315}
316
317std::string WebAssembly::signatureToString(const wasm::WasmSignature *Sig) {
318 std::string S("(");
319 S += typeListToString(Sig->Params);
320 S += ") -> (";
321 S += typeListToString(Sig->Returns);
322 S += ")";
323 return S;
324}