blob: 00afb88931e9b42138add79201bdce08412bcdf1 [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
11/// \brief Print MCInst instructions to wasm format.
12///
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"
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 Gohmane9361d52015-11-05 19:28:16 +000028#include "llvm/Target/TargetRegisterInfo.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,
Dan Gohman7a6b9822015-11-29 22:32:02 +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.
60 if (i != 0 &&
61 (MI->getOpcode() != WebAssembly::CALL_INDIRECT_VOID ||
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;
76 case WebAssembly::LOOP: {
Dan Gohman3a643e82016-10-06 22:10:23 +000077 printAnnotation(OS, "label" + utostr(ControlFlowCounter) + ':');
78 ControlFlowStack.push_back(std::make_pair(ControlFlowCounter++, true));
Dan Gohman1d68e80f2016-01-12 19:14:46 +000079 break;
80 }
81 case WebAssembly::BLOCK:
82 ControlFlowStack.push_back(std::make_pair(ControlFlowCounter++, false));
83 break;
84 case WebAssembly::END_LOOP:
85 ControlFlowStack.pop_back();
Dan Gohman1d68e80f2016-01-12 19:14:46 +000086 break;
87 case WebAssembly::END_BLOCK:
88 printAnnotation(
89 OS, "label" + utostr(ControlFlowStack.pop_back_val().first) + ':');
90 break;
91 }
92
93 // Annotate any control flow label references.
94 unsigned NumFixedOperands = Desc.NumOperands;
95 SmallSet<uint64_t, 8> Printed;
96 for (unsigned i = 0, e = MI->getNumOperands(); i < e; ++i) {
97 const MCOperandInfo &Info = Desc.OpInfo[i];
98 if (!(i < NumFixedOperands
99 ? (Info.OperandType == WebAssembly::OPERAND_BASIC_BLOCK)
100 : (Desc.TSFlags & WebAssemblyII::VariableOpImmediateIsLabel)))
101 continue;
102 uint64_t Depth = MI->getOperand(i).getImm();
103 if (!Printed.insert(Depth).second)
104 continue;
105 const auto &Pair = ControlFlowStack.rbegin()[Depth];
106 printAnnotation(OS, utostr(Depth) + ": " + (Pair.second ? "up" : "down") +
107 " to label" + utostr(Pair.first));
108 }
109 }
Dan Gohmancf4748f2015-11-12 17:04:33 +0000110}
111
112static std::string toString(const APFloat &FP) {
Dan Gohmanaa742912016-02-16 15:14:23 +0000113 // Print NaNs with custom payloads specially.
114 if (FP.isNaN() &&
115 !FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics())) &&
116 !FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics(), /*Negative=*/true))) {
117 APInt AI = FP.bitcastToAPInt();
118 return
119 std::string(AI.isNegative() ? "-" : "") + "nan:0x" +
120 utohexstr(AI.getZExtValue() &
121 (AI.getBitWidth() == 32 ? INT64_C(0x007fffff) :
122 INT64_C(0x000fffffffffffff)),
123 /*LowerCase=*/true);
124 }
125
126 // Use C99's hexadecimal floating-point representation.
Dan Gohmancf4748f2015-11-12 17:04:33 +0000127 static const size_t BufBytes = 128;
128 char buf[BufBytes];
Dan Gohmancf4748f2015-11-12 17:04:33 +0000129 auto Written = FP.convertToHexString(
130 buf, /*hexDigits=*/0, /*upperCase=*/false, APFloat::rmNearestTiesToEven);
131 (void)Written;
132 assert(Written != 0);
133 assert(Written < BufBytes);
134 return buf;
Dan Gohman10e730a2015-06-29 23:51:55 +0000135}
JF Bastienaf111db2015-08-24 22:16:48 +0000136
137void WebAssemblyInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
138 raw_ostream &O) {
139 const MCOperand &Op = MI->getOperand(OpNo);
Dan Gohmane9361d52015-11-05 19:28:16 +0000140 if (Op.isReg()) {
Dan Gohman85159ca2016-01-12 01:45:12 +0000141 assert((OpNo < MII.get(MI->getOpcode()).getNumOperands() ||
142 MII.get(MI->getOpcode()).TSFlags == 0) &&
143 "WebAssembly variable_ops register ops don't use TSFlags");
Dan Gohman4ba48162015-11-18 16:12:01 +0000144 unsigned WAReg = Op.getReg();
145 if (int(WAReg) >= 0)
146 printRegName(O, WAReg);
147 else if (OpNo >= MII.get(MI->getOpcode()).getNumDefs())
Dan Gohmanb7c24002016-05-21 00:21:56 +0000148 O << "$pop" << WebAssemblyFunctionInfo::getWARegStackId(WAReg);
Dan Gohman4ba48162015-11-18 16:12:01 +0000149 else if (WAReg != WebAssemblyFunctionInfo::UnusedReg)
Dan Gohmanb7c24002016-05-21 00:21:56 +0000150 O << "$push" << WebAssemblyFunctionInfo::getWARegStackId(WAReg);
Dan Gohman4ba48162015-11-18 16:12:01 +0000151 else
Dan Gohman71008092016-05-17 23:19:03 +0000152 O << "$drop";
Dan Gohman700515f2015-11-23 21:55:57 +0000153 // Add a '=' suffix if this is a def.
154 if (OpNo < MII.get(MI->getOpcode()).getNumDefs())
155 O << '=';
Dan Gohman53828fd2015-11-23 16:50:18 +0000156 } else if (Op.isImm()) {
Dan Gohman4fc4e422016-10-24 19:49:43 +0000157 const MCInstrDesc &Desc = MII.get(MI->getOpcode());
158 assert((OpNo < Desc.getNumOperands() ||
159 (Desc.TSFlags & WebAssemblyII::VariableOpIsImmediate)) &&
Dan Gohman85159ca2016-01-12 01:45:12 +0000160 "WebAssemblyII::VariableOpIsImmediate should be set for "
161 "variable_ops immediate ops");
Benjamin Kramer7df30432016-10-25 09:08:50 +0000162 (void)Desc;
Dan Gohman3acb1872016-10-24 23:27:49 +0000163 // TODO: (MII.get(MI->getOpcode()).TSFlags &
164 // WebAssemblyII::VariableOpImmediateIsLabel)
165 // can tell us whether this is an immediate referencing a label in the
166 // control flow stack, and it may be nice to pretty-print.
167 O << Op.getImm();
Dan Gohman85159ca2016-01-12 01:45:12 +0000168 } else if (Op.isFPImm()) {
Dan Gohmanaa742912016-02-16 15:14:23 +0000169 const MCInstrDesc &Desc = MII.get(MI->getOpcode());
170 assert(OpNo < Desc.getNumOperands() &&
171 "Unexpected floating-point immediate as a non-fixed operand");
172 assert(Desc.TSFlags == 0 &&
Dan Gohman85159ca2016-01-12 01:45:12 +0000173 "WebAssembly variable_ops floating point ops don't use TSFlags");
Dan Gohmanaa742912016-02-16 15:14:23 +0000174 const MCOperandInfo &Info = Desc.OpInfo[OpNo];
Dan Gohman4b8e8be2016-10-03 21:31:31 +0000175 if (Info.OperandType == WebAssembly::OPERAND_F32IMM) {
Dan Gohmanaa742912016-02-16 15:14:23 +0000176 // TODO: MC converts all floating point immediate operands to double.
177 // This is fine for numeric values, but may cause NaNs to change bits.
178 O << toString(APFloat(float(Op.getFPImm())));
179 } else {
Dan Gohman4b8e8be2016-10-03 21:31:31 +0000180 assert(Info.OperandType == WebAssembly::OPERAND_F64IMM);
Dan Gohmanaa742912016-02-16 15:14:23 +0000181 O << toString(APFloat(Op.getFPImm()));
182 }
Dan Gohman85159ca2016-01-12 01:45:12 +0000183 } else {
184 assert((OpNo < MII.get(MI->getOpcode()).getNumOperands() ||
185 (MII.get(MI->getOpcode()).TSFlags &
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000186 WebAssemblyII::VariableOpIsImmediate)) &&
Dan Gohman85159ca2016-01-12 01:45:12 +0000187 "WebAssemblyII::VariableOpIsImmediate should be set for "
188 "variable_ops expr ops");
JF Bastienaf111db2015-08-24 22:16:48 +0000189 assert(Op.isExpr() && "unknown operand kind in printOperand");
190 Op.getExpr()->print(O, &MAI);
191 }
192}
Dan Gohman5e0886b2015-12-06 19:42:29 +0000193
Dan Gohmanbb372242016-01-26 03:39:31 +0000194void
195WebAssemblyInstPrinter::printWebAssemblyP2AlignOperand(const MCInst *MI,
196 unsigned OpNo,
197 raw_ostream &O) {
198 int64_t Imm = MI->getOperand(OpNo).getImm();
199 if (Imm == WebAssembly::GetDefaultP2Align(MI->getOpcode()))
200 return;
201 O << ":p2align=" << Imm;
202}
203
Dan Gohman2726b882016-10-06 22:29:32 +0000204void
205WebAssemblyInstPrinter::printWebAssemblySignatureOperand(const MCInst *MI,
206 unsigned OpNo,
207 raw_ostream &O) {
208 int64_t Imm = MI->getOperand(OpNo).getImm();
Dan Gohman4fc4e422016-10-24 19:49:43 +0000209 switch (WebAssembly::ExprType(Imm)) {
210 case WebAssembly::ExprType::Void: break;
211 case WebAssembly::ExprType::I32: O << "i32"; break;
212 case WebAssembly::ExprType::I64: O << "i64"; break;
213 case WebAssembly::ExprType::F32: O << "f32"; break;
214 case WebAssembly::ExprType::F64: O << "f64"; break;
215 case WebAssembly::ExprType::I8x16: O << "i8x16"; break;
216 case WebAssembly::ExprType::I16x8: O << "i16x8"; break;
217 case WebAssembly::ExprType::I32x4: O << "i32x4"; break;
Dan Gohman4fc4e422016-10-24 19:49:43 +0000218 case WebAssembly::ExprType::F32x4: O << "f32x4"; break;
Dan Gohman3acb1872016-10-24 23:27:49 +0000219 case WebAssembly::ExprType::B8x16: O << "b8x16"; break;
220 case WebAssembly::ExprType::B16x8: O << "b16x8"; break;
221 case WebAssembly::ExprType::B32x4: O << "b32x4"; break;
Dan Gohman2726b882016-10-06 22:29:32 +0000222 }
223}
224
Dan Gohman5e0886b2015-12-06 19:42:29 +0000225const char *llvm::WebAssembly::TypeToString(MVT Ty) {
226 switch (Ty.SimpleTy) {
227 case MVT::i32:
228 return "i32";
229 case MVT::i64:
230 return "i64";
231 case MVT::f32:
232 return "f32";
233 case MVT::f64:
234 return "f64";
Derek Schuff39bf39f2016-08-02 23:16:09 +0000235 case MVT::v16i8:
236 case MVT::v8i16:
237 case MVT::v4i32:
238 case MVT::v4f32:
239 return "v128";
Dan Gohman5e0886b2015-12-06 19:42:29 +0000240 default:
241 llvm_unreachable("unsupported type");
242 }
243}