blob: 9a95150cb5571c6d3cdc7d17d1c84ffaa408c21a [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 Gohman53828fd2015-11-23 16:50:18 +000057 if (i != 0)
58 OS << ", ";
Dan Gohmancf4748f2015-11-12 17:04:33 +000059 printOperand(MI, i, OS);
60 }
61
Dan Gohmandd20c702015-12-21 16:50:41 +000062 // Print any added annotation.
JF Bastienb9073fb2015-07-22 21:28:15 +000063 printAnnotation(OS, Annot);
Dan Gohman1d68e80f2016-01-12 19:14:46 +000064
65 if (CommentStream) {
66 // Observe any effects on the control flow stack, for use in annotating
67 // control flow label references.
68 switch (MI->getOpcode()) {
69 default:
70 break;
71 case WebAssembly::LOOP: {
72 // Grab the TopLabel value first so that labels print in numeric order.
73 uint64_t TopLabel = ControlFlowCounter++;
74 ControlFlowStack.push_back(std::make_pair(ControlFlowCounter++, false));
75 printAnnotation(OS, "label" + utostr(TopLabel) + ':');
76 ControlFlowStack.push_back(std::make_pair(TopLabel, true));
77 break;
78 }
79 case WebAssembly::BLOCK:
80 ControlFlowStack.push_back(std::make_pair(ControlFlowCounter++, false));
81 break;
82 case WebAssembly::END_LOOP:
83 ControlFlowStack.pop_back();
84 printAnnotation(
85 OS, "label" + utostr(ControlFlowStack.pop_back_val().first) + ':');
86 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) {
113 static const size_t BufBytes = 128;
114 char buf[BufBytes];
115 if (FP.isNaN())
116 assert((FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics())) ||
117 FP.bitwiseIsEqual(
118 APFloat::getQNaN(FP.getSemantics(), /*Negative=*/true))) &&
119 "convertToHexString handles neither SNaN nor NaN payloads");
120 // Use C99's hexadecimal floating-point representation.
121 auto Written = FP.convertToHexString(
122 buf, /*hexDigits=*/0, /*upperCase=*/false, APFloat::rmNearestTiesToEven);
123 (void)Written;
124 assert(Written != 0);
125 assert(Written < BufBytes);
126 return buf;
Dan Gohman10e730a2015-06-29 23:51:55 +0000127}
JF Bastienaf111db2015-08-24 22:16:48 +0000128
129void WebAssemblyInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
130 raw_ostream &O) {
131 const MCOperand &Op = MI->getOperand(OpNo);
Dan Gohmane9361d52015-11-05 19:28:16 +0000132 if (Op.isReg()) {
Dan Gohman85159ca2016-01-12 01:45:12 +0000133 assert((OpNo < MII.get(MI->getOpcode()).getNumOperands() ||
134 MII.get(MI->getOpcode()).TSFlags == 0) &&
135 "WebAssembly variable_ops register ops don't use TSFlags");
Dan Gohman4ba48162015-11-18 16:12:01 +0000136 unsigned WAReg = Op.getReg();
137 if (int(WAReg) >= 0)
138 printRegName(O, WAReg);
139 else if (OpNo >= MII.get(MI->getOpcode()).getNumDefs())
140 O << "$pop" << (WAReg & INT32_MAX);
141 else if (WAReg != WebAssemblyFunctionInfo::UnusedReg)
142 O << "$push" << (WAReg & INT32_MAX);
143 else
144 O << "$discard";
Dan Gohman700515f2015-11-23 21:55:57 +0000145 // Add a '=' suffix if this is a def.
146 if (OpNo < MII.get(MI->getOpcode()).getNumDefs())
147 O << '=';
Dan Gohman53828fd2015-11-23 16:50:18 +0000148 } else if (Op.isImm()) {
Dan Gohman85159ca2016-01-12 01:45:12 +0000149 assert((OpNo < MII.get(MI->getOpcode()).getNumOperands() ||
150 (MII.get(MI->getOpcode()).TSFlags &
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000151 WebAssemblyII::VariableOpIsImmediate)) &&
Dan Gohman85159ca2016-01-12 01:45:12 +0000152 "WebAssemblyII::VariableOpIsImmediate should be set for "
153 "variable_ops immediate ops");
Dan Gohman3469ee12016-01-12 20:30:51 +0000154 // TODO: (MII.get(MI->getOpcode()).TSFlags &
155 // WebAssemblyII::VariableOpImmediateIsLabel)
156 // can tell us whether this is an immediate referencing a label in the
157 // control flow stack, and it may be nice to pretty-print.
158 O << Op.getImm();
Dan Gohman85159ca2016-01-12 01:45:12 +0000159 } else if (Op.isFPImm()) {
160 assert((OpNo < MII.get(MI->getOpcode()).getNumOperands() ||
161 MII.get(MI->getOpcode()).TSFlags == 0) &&
162 "WebAssembly variable_ops floating point ops don't use TSFlags");
Dan Gohmancf4748f2015-11-12 17:04:33 +0000163 O << toString(APFloat(Op.getFPImm()));
Dan Gohman85159ca2016-01-12 01:45:12 +0000164 } else {
165 assert((OpNo < MII.get(MI->getOpcode()).getNumOperands() ||
166 (MII.get(MI->getOpcode()).TSFlags &
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000167 WebAssemblyII::VariableOpIsImmediate)) &&
Dan Gohman85159ca2016-01-12 01:45:12 +0000168 "WebAssemblyII::VariableOpIsImmediate should be set for "
169 "variable_ops expr ops");
JF Bastienaf111db2015-08-24 22:16:48 +0000170 assert(Op.isExpr() && "unknown operand kind in printOperand");
171 Op.getExpr()->print(O, &MAI);
172 }
173}
Dan Gohman5e0886b2015-12-06 19:42:29 +0000174
175const char *llvm::WebAssembly::TypeToString(MVT Ty) {
176 switch (Ty.SimpleTy) {
177 case MVT::i32:
178 return "i32";
179 case MVT::i64:
180 return "i64";
181 case MVT::f32:
182 return "f32";
183 case MVT::f64:
184 return "f64";
185 default:
186 llvm_unreachable("unsupported type");
187 }
188}