blob: 33b224adc6e22ce9d9541de57bd3e2a9dcc99106 [file] [log] [blame]
Dan Gohman10e730a2015-06-29 23:51:55 +00001//=- WebAssemblyInstPrinter.cpp - WebAssembly assembly instruction printing -=//
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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000011/// Print MCInst instructions to wasm format.
Dan Gohman10e730a2015-06-29 23:51:55 +000012///
13//===----------------------------------------------------------------------===//
14
15#include "InstPrinter/WebAssemblyInstPrinter.h"
Dan Gohman7a6b9822015-11-29 22:32:02 +000016#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000017#include "WebAssembly.h"
Dan Gohman058fce52015-11-13 00:21:05 +000018#include "WebAssemblyMachineFunctionInfo.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)
Dan Gohman1d68e80f2016-01-12 19:14:46 +000038 : MCInstPrinter(MAI, MII, MRI), ControlFlowCounter(0) {}
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);
Dan Gohman4ba48162015-11-18 16:12:01 +000043 // Note that there's an implicit get_local/set_local here!
44 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())
Dan Gohmandd20c702015-12-21 16:50:41 +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 Ahnf208f632018-09-05 01:27:38 +000060 if (i != 0 && ((MI->getOpcode() != WebAssembly::CALL_INDIRECT_VOID &&
61 MI->getOpcode() != WebAssembly::CALL_INDIRECT_VOID_S) ||
62 i != Desc.getNumOperands()))
Dan Gohman53828fd2015-11-23 16:50:18 +000063 OS << ", ";
Dan Gohmancf4748f2015-11-12 17:04:33 +000064 printOperand(MI, i, OS);
65 }
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.
73 switch (MI->getOpcode()) {
74 default:
75 break;
Richard Trieu01f99f32018-08-11 04:18:05 +000076 case WebAssembly::LOOP:
77 case WebAssembly::LOOP_S: {
Dan Gohman3a643e82016-10-06 22:10:23 +000078 printAnnotation(OS, "label" + utostr(ControlFlowCounter) + ':');
79 ControlFlowStack.push_back(std::make_pair(ControlFlowCounter++, true));
Dan Gohman1d68e80f2016-01-12 19:14:46 +000080 break;
81 }
82 case WebAssembly::BLOCK:
Richard Trieu01f99f32018-08-11 04:18:05 +000083 case WebAssembly::BLOCK_S:
Dan Gohman1d68e80f2016-01-12 19:14:46 +000084 ControlFlowStack.push_back(std::make_pair(ControlFlowCounter++, false));
85 break;
86 case WebAssembly::END_LOOP:
Richard Trieu01f99f32018-08-11 04:18:05 +000087 case WebAssembly::END_LOOP_S:
Heejin Ahn1d13e6b2018-10-25 23:35:14 +000088 assert(!ControlFlowStack.empty() && "End marker mismatch!");
Heejin Ahn1147d912018-10-25 23:35:13 +000089 ControlFlowStack.pop_back();
Dan Gohman1d68e80f2016-01-12 19:14:46 +000090 break;
91 case WebAssembly::END_BLOCK:
Richard Trieu01f99f32018-08-11 04:18:05 +000092 case WebAssembly::END_BLOCK_S:
Heejin Ahn1d13e6b2018-10-25 23:35:14 +000093 assert(!ControlFlowStack.empty() && "End marker mismatch!");
Heejin Ahn1147d912018-10-25 23:35:13 +000094 printAnnotation(
95 OS, "label" + utostr(ControlFlowStack.pop_back_val().first) + ':');
Dan Gohman1d68e80f2016-01-12 19:14:46 +000096 break;
97 }
98
99 // Annotate any control flow label references.
100 unsigned NumFixedOperands = Desc.NumOperands;
101 SmallSet<uint64_t, 8> Printed;
102 for (unsigned i = 0, e = MI->getNumOperands(); i < e; ++i) {
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000103 if (!(i < NumFixedOperands
Dan Gohman207ed222016-12-22 16:00:55 +0000104 ? (Desc.OpInfo[i].OperandType ==
105 WebAssembly::OPERAND_BASIC_BLOCK)
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000106 : (Desc.TSFlags & WebAssemblyII::VariableOpImmediateIsLabel)))
107 continue;
108 uint64_t Depth = MI->getOperand(i).getImm();
109 if (!Printed.insert(Depth).second)
110 continue;
111 const auto &Pair = ControlFlowStack.rbegin()[Depth];
112 printAnnotation(OS, utostr(Depth) + ": " + (Pair.second ? "up" : "down") +
113 " to label" + utostr(Pair.first));
114 }
115 }
Dan Gohmancf4748f2015-11-12 17:04:33 +0000116}
117
118static std::string toString(const APFloat &FP) {
Dan Gohmanaa742912016-02-16 15:14:23 +0000119 // Print NaNs with custom payloads specially.
Heejin Ahnf208f632018-09-05 01:27:38 +0000120 if (FP.isNaN() && !FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics())) &&
Dan Gohman207ed222016-12-22 16:00:55 +0000121 !FP.bitwiseIsEqual(
122 APFloat::getQNaN(FP.getSemantics(), /*Negative=*/true))) {
Dan Gohmanaa742912016-02-16 15:14:23 +0000123 APInt AI = FP.bitcastToAPInt();
Heejin Ahnf208f632018-09-05 01:27:38 +0000124 return std::string(AI.isNegative() ? "-" : "") + "nan:0x" +
125 utohexstr(AI.getZExtValue() &
126 (AI.getBitWidth() == 32 ? INT64_C(0x007fffff)
127 : INT64_C(0x000fffffffffffff)),
128 /*LowerCase=*/true);
Dan Gohmanaa742912016-02-16 15:14:23 +0000129 }
130
131 // Use C99's hexadecimal floating-point representation.
Dan Gohmancf4748f2015-11-12 17:04:33 +0000132 static const size_t BufBytes = 128;
133 char buf[BufBytes];
Dan Gohmancf4748f2015-11-12 17:04:33 +0000134 auto Written = FP.convertToHexString(
135 buf, /*hexDigits=*/0, /*upperCase=*/false, APFloat::rmNearestTiesToEven);
136 (void)Written;
137 assert(Written != 0);
138 assert(Written < BufBytes);
139 return buf;
Dan Gohman10e730a2015-06-29 23:51:55 +0000140}
JF Bastienaf111db2015-08-24 22:16:48 +0000141
142void WebAssemblyInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
143 raw_ostream &O) {
144 const MCOperand &Op = MI->getOperand(OpNo);
Dan Gohmane9361d52015-11-05 19:28:16 +0000145 if (Op.isReg()) {
Dan Gohman85159ca2016-01-12 01:45:12 +0000146 assert((OpNo < MII.get(MI->getOpcode()).getNumOperands() ||
147 MII.get(MI->getOpcode()).TSFlags == 0) &&
148 "WebAssembly variable_ops register ops don't use TSFlags");
Dan Gohman4ba48162015-11-18 16:12:01 +0000149 unsigned WAReg = Op.getReg();
150 if (int(WAReg) >= 0)
151 printRegName(O, WAReg);
152 else if (OpNo >= MII.get(MI->getOpcode()).getNumDefs())
Dan Gohmanb7c24002016-05-21 00:21:56 +0000153 O << "$pop" << WebAssemblyFunctionInfo::getWARegStackId(WAReg);
Dan Gohman4ba48162015-11-18 16:12:01 +0000154 else if (WAReg != WebAssemblyFunctionInfo::UnusedReg)
Dan Gohmanb7c24002016-05-21 00:21:56 +0000155 O << "$push" << WebAssemblyFunctionInfo::getWARegStackId(WAReg);
Dan Gohman4ba48162015-11-18 16:12:01 +0000156 else
Dan Gohman71008092016-05-17 23:19:03 +0000157 O << "$drop";
Dan Gohman700515f2015-11-23 21:55:57 +0000158 // Add a '=' suffix if this is a def.
159 if (OpNo < MII.get(MI->getOpcode()).getNumDefs())
160 O << '=';
Dan Gohman53828fd2015-11-23 16:50:18 +0000161 } else if (Op.isImm()) {
Dan Gohman4fc4e422016-10-24 19:49:43 +0000162 const MCInstrDesc &Desc = MII.get(MI->getOpcode());
163 assert((OpNo < Desc.getNumOperands() ||
164 (Desc.TSFlags & WebAssemblyII::VariableOpIsImmediate)) &&
Dan Gohman85159ca2016-01-12 01:45:12 +0000165 "WebAssemblyII::VariableOpIsImmediate should be set for "
166 "variable_ops immediate ops");
Benjamin Kramer7df30432016-10-25 09:08:50 +0000167 (void)Desc;
Dan Gohman3acb1872016-10-24 23:27:49 +0000168 // TODO: (MII.get(MI->getOpcode()).TSFlags &
169 // WebAssemblyII::VariableOpImmediateIsLabel)
170 // can tell us whether this is an immediate referencing a label in the
171 // control flow stack, and it may be nice to pretty-print.
172 O << Op.getImm();
Dan Gohman85159ca2016-01-12 01:45:12 +0000173 } else if (Op.isFPImm()) {
Dan Gohmanaa742912016-02-16 15:14:23 +0000174 const MCInstrDesc &Desc = MII.get(MI->getOpcode());
175 assert(OpNo < Desc.getNumOperands() &&
176 "Unexpected floating-point immediate as a non-fixed operand");
177 assert(Desc.TSFlags == 0 &&
Dan Gohman85159ca2016-01-12 01:45:12 +0000178 "WebAssembly variable_ops floating point ops don't use TSFlags");
Dan Gohmanaa742912016-02-16 15:14:23 +0000179 const MCOperandInfo &Info = Desc.OpInfo[OpNo];
Dan Gohman4b8e8be2016-10-03 21:31:31 +0000180 if (Info.OperandType == WebAssembly::OPERAND_F32IMM) {
Dan Gohmanaa742912016-02-16 15:14:23 +0000181 // TODO: MC converts all floating point immediate operands to double.
182 // This is fine for numeric values, but may cause NaNs to change bits.
Benjamin Kramera01e97d2018-02-22 22:29:27 +0000183 O << ::toString(APFloat(float(Op.getFPImm())));
Dan Gohmanaa742912016-02-16 15:14:23 +0000184 } else {
Dan Gohman4b8e8be2016-10-03 21:31:31 +0000185 assert(Info.OperandType == WebAssembly::OPERAND_F64IMM);
Benjamin Kramera01e97d2018-02-22 22:29:27 +0000186 O << ::toString(APFloat(Op.getFPImm()));
Dan Gohmanaa742912016-02-16 15:14:23 +0000187 }
Dan Gohman85159ca2016-01-12 01:45:12 +0000188 } else {
189 assert((OpNo < MII.get(MI->getOpcode()).getNumOperands() ||
190 (MII.get(MI->getOpcode()).TSFlags &
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000191 WebAssemblyII::VariableOpIsImmediate)) &&
Dan Gohman85159ca2016-01-12 01:45:12 +0000192 "WebAssemblyII::VariableOpIsImmediate should be set for "
193 "variable_ops expr ops");
JF Bastienaf111db2015-08-24 22:16:48 +0000194 assert(Op.isExpr() && "unknown operand kind in printOperand");
195 Op.getExpr()->print(O, &MAI);
196 }
197}
Dan Gohman5e0886b2015-12-06 19:42:29 +0000198
Heejin Ahnf208f632018-09-05 01:27:38 +0000199void WebAssemblyInstPrinter::printWebAssemblyP2AlignOperand(const MCInst *MI,
200 unsigned OpNo,
201 raw_ostream &O) {
Dan Gohmanbb372242016-01-26 03:39:31 +0000202 int64_t Imm = MI->getOperand(OpNo).getImm();
203 if (Imm == WebAssembly::GetDefaultP2Align(MI->getOpcode()))
204 return;
205 O << ":p2align=" << Imm;
206}
207
Heejin Ahnf208f632018-09-05 01:27:38 +0000208void WebAssemblyInstPrinter::printWebAssemblySignatureOperand(const MCInst *MI,
209 unsigned OpNo,
210 raw_ostream &O) {
Dan Gohman2726b882016-10-06 22:29:32 +0000211 int64_t Imm = MI->getOperand(OpNo).getImm();
Dan Gohman4fc4e422016-10-24 19:49:43 +0000212 switch (WebAssembly::ExprType(Imm)) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000213 case WebAssembly::ExprType::Void:
214 break;
215 case WebAssembly::ExprType::I32:
216 O << "i32";
217 break;
218 case WebAssembly::ExprType::I64:
219 O << "i64";
220 break;
221 case WebAssembly::ExprType::F32:
222 O << "f32";
223 break;
224 case WebAssembly::ExprType::F64:
225 O << "f64";
226 break;
227 case WebAssembly::ExprType::V128:
228 O << "v128";
229 break;
230 case WebAssembly::ExprType::ExceptRef:
231 O << "except_ref";
232 break;
Dan Gohman2726b882016-10-06 22:29:32 +0000233 }
234}
235
Derek Schuff77a7a382018-10-03 22:22:48 +0000236const char *llvm::WebAssembly::TypeToString(wasm::ValType Ty) {
237 switch (Ty) {
238 case wasm::ValType::I32:
Dan Gohman5e0886b2015-12-06 19:42:29 +0000239 return "i32";
Derek Schuff77a7a382018-10-03 22:22:48 +0000240 case wasm::ValType::I64:
Dan Gohman5e0886b2015-12-06 19:42:29 +0000241 return "i64";
Derek Schuff77a7a382018-10-03 22:22:48 +0000242 case wasm::ValType::F32:
Dan Gohman5e0886b2015-12-06 19:42:29 +0000243 return "f32";
Derek Schuff77a7a382018-10-03 22:22:48 +0000244 case wasm::ValType::F64:
Dan Gohman5e0886b2015-12-06 19:42:29 +0000245 return "f64";
Derek Schuff77a7a382018-10-03 22:22:48 +0000246 case wasm::ValType::V128:
Derek Schuff39bf39f2016-08-02 23:16:09 +0000247 return "v128";
Derek Schuff77a7a382018-10-03 22:22:48 +0000248 case wasm::ValType::EXCEPT_REF:
Heejin Ahn0de58722018-03-08 04:05:37 +0000249 return "except_ref";
Dan Gohman5e0886b2015-12-06 19:42:29 +0000250 }
Simon Pilgrim991b0d22018-10-04 10:25:52 +0000251 llvm_unreachable("Unknown wasm::ValType");
Dan Gohman5e0886b2015-12-06 19:42:29 +0000252}