blob: b7b8c4fe03988d0e4338197679c9969747fd2522 [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:
Derek Schuffe4825972018-03-20 20:06:35 +000088 // Have to guard against an empty stack, in case of mismatched pairs
89 // in assembly parsing.
Heejin Ahnf208f632018-09-05 01:27:38 +000090 if (!ControlFlowStack.empty())
91 ControlFlowStack.pop_back();
Dan Gohman1d68e80f2016-01-12 19:14:46 +000092 break;
93 case WebAssembly::END_BLOCK:
Richard Trieu01f99f32018-08-11 04:18:05 +000094 case WebAssembly::END_BLOCK_S:
Heejin Ahnf208f632018-09-05 01:27:38 +000095 if (!ControlFlowStack.empty())
96 printAnnotation(
97 OS, "label" + utostr(ControlFlowStack.pop_back_val().first) + ':');
Dan Gohman1d68e80f2016-01-12 19:14:46 +000098 break;
99 }
100
101 // Annotate any control flow label references.
102 unsigned NumFixedOperands = Desc.NumOperands;
103 SmallSet<uint64_t, 8> Printed;
104 for (unsigned i = 0, e = MI->getNumOperands(); i < e; ++i) {
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000105 if (!(i < NumFixedOperands
Dan Gohman207ed222016-12-22 16:00:55 +0000106 ? (Desc.OpInfo[i].OperandType ==
107 WebAssembly::OPERAND_BASIC_BLOCK)
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000108 : (Desc.TSFlags & WebAssemblyII::VariableOpImmediateIsLabel)))
109 continue;
110 uint64_t Depth = MI->getOperand(i).getImm();
111 if (!Printed.insert(Depth).second)
112 continue;
113 const auto &Pair = ControlFlowStack.rbegin()[Depth];
114 printAnnotation(OS, utostr(Depth) + ": " + (Pair.second ? "up" : "down") +
115 " to label" + utostr(Pair.first));
116 }
117 }
Dan Gohmancf4748f2015-11-12 17:04:33 +0000118}
119
120static std::string toString(const APFloat &FP) {
Dan Gohmanaa742912016-02-16 15:14:23 +0000121 // Print NaNs with custom payloads specially.
Heejin Ahnf208f632018-09-05 01:27:38 +0000122 if (FP.isNaN() && !FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics())) &&
Dan Gohman207ed222016-12-22 16:00:55 +0000123 !FP.bitwiseIsEqual(
124 APFloat::getQNaN(FP.getSemantics(), /*Negative=*/true))) {
Dan Gohmanaa742912016-02-16 15:14:23 +0000125 APInt AI = FP.bitcastToAPInt();
Heejin Ahnf208f632018-09-05 01:27:38 +0000126 return std::string(AI.isNegative() ? "-" : "") + "nan:0x" +
127 utohexstr(AI.getZExtValue() &
128 (AI.getBitWidth() == 32 ? INT64_C(0x007fffff)
129 : INT64_C(0x000fffffffffffff)),
130 /*LowerCase=*/true);
Dan Gohmanaa742912016-02-16 15:14:23 +0000131 }
132
133 // Use C99's hexadecimal floating-point representation.
Dan Gohmancf4748f2015-11-12 17:04:33 +0000134 static const size_t BufBytes = 128;
135 char buf[BufBytes];
Dan Gohmancf4748f2015-11-12 17:04:33 +0000136 auto Written = FP.convertToHexString(
137 buf, /*hexDigits=*/0, /*upperCase=*/false, APFloat::rmNearestTiesToEven);
138 (void)Written;
139 assert(Written != 0);
140 assert(Written < BufBytes);
141 return buf;
Dan Gohman10e730a2015-06-29 23:51:55 +0000142}
JF Bastienaf111db2015-08-24 22:16:48 +0000143
144void WebAssemblyInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
145 raw_ostream &O) {
146 const MCOperand &Op = MI->getOperand(OpNo);
Dan Gohmane9361d52015-11-05 19:28:16 +0000147 if (Op.isReg()) {
Dan Gohman85159ca2016-01-12 01:45:12 +0000148 assert((OpNo < MII.get(MI->getOpcode()).getNumOperands() ||
149 MII.get(MI->getOpcode()).TSFlags == 0) &&
150 "WebAssembly variable_ops register ops don't use TSFlags");
Dan Gohman4ba48162015-11-18 16:12:01 +0000151 unsigned WAReg = Op.getReg();
152 if (int(WAReg) >= 0)
153 printRegName(O, WAReg);
154 else if (OpNo >= MII.get(MI->getOpcode()).getNumDefs())
Dan Gohmanb7c24002016-05-21 00:21:56 +0000155 O << "$pop" << WebAssemblyFunctionInfo::getWARegStackId(WAReg);
Dan Gohman4ba48162015-11-18 16:12:01 +0000156 else if (WAReg != WebAssemblyFunctionInfo::UnusedReg)
Dan Gohmanb7c24002016-05-21 00:21:56 +0000157 O << "$push" << WebAssemblyFunctionInfo::getWARegStackId(WAReg);
Dan Gohman4ba48162015-11-18 16:12:01 +0000158 else
Dan Gohman71008092016-05-17 23:19:03 +0000159 O << "$drop";
Dan Gohman700515f2015-11-23 21:55:57 +0000160 // Add a '=' suffix if this is a def.
161 if (OpNo < MII.get(MI->getOpcode()).getNumDefs())
162 O << '=';
Dan Gohman53828fd2015-11-23 16:50:18 +0000163 } else if (Op.isImm()) {
Dan Gohman4fc4e422016-10-24 19:49:43 +0000164 const MCInstrDesc &Desc = MII.get(MI->getOpcode());
165 assert((OpNo < Desc.getNumOperands() ||
166 (Desc.TSFlags & WebAssemblyII::VariableOpIsImmediate)) &&
Dan Gohman85159ca2016-01-12 01:45:12 +0000167 "WebAssemblyII::VariableOpIsImmediate should be set for "
168 "variable_ops immediate ops");
Benjamin Kramer7df30432016-10-25 09:08:50 +0000169 (void)Desc;
Dan Gohman3acb1872016-10-24 23:27:49 +0000170 // TODO: (MII.get(MI->getOpcode()).TSFlags &
171 // WebAssemblyII::VariableOpImmediateIsLabel)
172 // can tell us whether this is an immediate referencing a label in the
173 // control flow stack, and it may be nice to pretty-print.
174 O << Op.getImm();
Dan Gohman85159ca2016-01-12 01:45:12 +0000175 } else if (Op.isFPImm()) {
Dan Gohmanaa742912016-02-16 15:14:23 +0000176 const MCInstrDesc &Desc = MII.get(MI->getOpcode());
177 assert(OpNo < Desc.getNumOperands() &&
178 "Unexpected floating-point immediate as a non-fixed operand");
179 assert(Desc.TSFlags == 0 &&
Dan Gohman85159ca2016-01-12 01:45:12 +0000180 "WebAssembly variable_ops floating point ops don't use TSFlags");
Dan Gohmanaa742912016-02-16 15:14:23 +0000181 const MCOperandInfo &Info = Desc.OpInfo[OpNo];
Dan Gohman4b8e8be2016-10-03 21:31:31 +0000182 if (Info.OperandType == WebAssembly::OPERAND_F32IMM) {
Dan Gohmanaa742912016-02-16 15:14:23 +0000183 // TODO: MC converts all floating point immediate operands to double.
184 // This is fine for numeric values, but may cause NaNs to change bits.
Benjamin Kramera01e97d2018-02-22 22:29:27 +0000185 O << ::toString(APFloat(float(Op.getFPImm())));
Dan Gohmanaa742912016-02-16 15:14:23 +0000186 } else {
Dan Gohman4b8e8be2016-10-03 21:31:31 +0000187 assert(Info.OperandType == WebAssembly::OPERAND_F64IMM);
Benjamin Kramera01e97d2018-02-22 22:29:27 +0000188 O << ::toString(APFloat(Op.getFPImm()));
Dan Gohmanaa742912016-02-16 15:14:23 +0000189 }
Dan Gohman85159ca2016-01-12 01:45:12 +0000190 } else {
191 assert((OpNo < MII.get(MI->getOpcode()).getNumOperands() ||
192 (MII.get(MI->getOpcode()).TSFlags &
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000193 WebAssemblyII::VariableOpIsImmediate)) &&
Dan Gohman85159ca2016-01-12 01:45:12 +0000194 "WebAssemblyII::VariableOpIsImmediate should be set for "
195 "variable_ops expr ops");
JF Bastienaf111db2015-08-24 22:16:48 +0000196 assert(Op.isExpr() && "unknown operand kind in printOperand");
197 Op.getExpr()->print(O, &MAI);
198 }
199}
Dan Gohman5e0886b2015-12-06 19:42:29 +0000200
Heejin Ahnf208f632018-09-05 01:27:38 +0000201void WebAssemblyInstPrinter::printWebAssemblyP2AlignOperand(const MCInst *MI,
202 unsigned OpNo,
203 raw_ostream &O) {
Dan Gohmanbb372242016-01-26 03:39:31 +0000204 int64_t Imm = MI->getOperand(OpNo).getImm();
205 if (Imm == WebAssembly::GetDefaultP2Align(MI->getOpcode()))
206 return;
207 O << ":p2align=" << Imm;
208}
209
Heejin Ahnf208f632018-09-05 01:27:38 +0000210void WebAssemblyInstPrinter::printWebAssemblySignatureOperand(const MCInst *MI,
211 unsigned OpNo,
212 raw_ostream &O) {
Dan Gohman2726b882016-10-06 22:29:32 +0000213 int64_t Imm = MI->getOperand(OpNo).getImm();
Dan Gohman4fc4e422016-10-24 19:49:43 +0000214 switch (WebAssembly::ExprType(Imm)) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000215 case WebAssembly::ExprType::Void:
216 break;
217 case WebAssembly::ExprType::I32:
218 O << "i32";
219 break;
220 case WebAssembly::ExprType::I64:
221 O << "i64";
222 break;
223 case WebAssembly::ExprType::F32:
224 O << "f32";
225 break;
226 case WebAssembly::ExprType::F64:
227 O << "f64";
228 break;
229 case WebAssembly::ExprType::V128:
230 O << "v128";
231 break;
232 case WebAssembly::ExprType::ExceptRef:
233 O << "except_ref";
234 break;
Dan Gohman2726b882016-10-06 22:29:32 +0000235 }
236}
237
Derek Schuff77a7a382018-10-03 22:22:48 +0000238const char *llvm::WebAssembly::TypeToString(wasm::ValType Ty) {
239 switch (Ty) {
240 case wasm::ValType::I32:
Dan Gohman5e0886b2015-12-06 19:42:29 +0000241 return "i32";
Derek Schuff77a7a382018-10-03 22:22:48 +0000242 case wasm::ValType::I64:
Dan Gohman5e0886b2015-12-06 19:42:29 +0000243 return "i64";
Derek Schuff77a7a382018-10-03 22:22:48 +0000244 case wasm::ValType::F32:
Dan Gohman5e0886b2015-12-06 19:42:29 +0000245 return "f32";
Derek Schuff77a7a382018-10-03 22:22:48 +0000246 case wasm::ValType::F64:
Dan Gohman5e0886b2015-12-06 19:42:29 +0000247 return "f64";
Derek Schuff77a7a382018-10-03 22:22:48 +0000248 case wasm::ValType::V128:
Derek Schuff39bf39f2016-08-02 23:16:09 +0000249 return "v128";
Derek Schuff77a7a382018-10-03 22:22:48 +0000250 case wasm::ValType::EXCEPT_REF:
Heejin Ahn0de58722018-03-08 04:05:37 +0000251 return "except_ref";
Dan Gohman5e0886b2015-12-06 19:42:29 +0000252 }
253}