| 1 |
/*========================== begin_copyright_notice ============================ |
| 2 |
|
| 3 |
Copyright (C) 2017-2021 Intel Corporation |
| 4 |
|
| 5 |
SPDX-License-Identifier: MIT |
| 6 |
|
| 7 |
============================= end_copyright_notice ===========================*/ |
| 8 |
|
| 9 |
#ifndef _IGA_FORMATTER |
| 10 |
#define _IGA_FORMATTER |
| 11 |
|
| 12 |
#include "../ErrorHandler.hpp" |
| 13 |
#include "../IR/DUAnalysis.hpp" |
| 14 |
#include "../IR/Kernel.hpp" |
| 15 |
#include "../strings.hpp" |
| 16 |
#include "Floats.hpp" |
| 17 |
|
| 18 |
#include <iomanip> |
| 19 |
#include <ostream> |
| 20 |
#include <sstream> |
| 21 |
#include <string> |
| 22 |
|
| 23 |
namespace iga { |
| 24 |
////////////////////////////////////////////////////////////////////////// |
| 25 |
// EXTERNAL INTERFACES |
| 26 |
////////////////////////////////////////////////////////////////////////// |
| 27 |
|
| 28 |
typedef const char *(*LabelerFunction)(int32_t, void *); |
| 29 |
|
| 30 |
// Formatter options |
| 31 |
struct FormatOpts { |
| 32 |
const Model &model; |
| 33 |
LabelerFunction labeler = nullptr; |
| 34 |
void *labelerContext = nullptr; |
| 35 |
bool numericLabels = false; |
| 36 |
bool hexFloats = true; |
| 37 |
bool printInstPc = false; |
| 38 |
bool syntaxExtensions = false; |
| 39 |
bool syntaxBfnSymbolicFunctions = true; |
| 40 |
// swsb encoding mode, if not specified, the encoding mode will be |
| 41 |
// derived from platform by SWSB::getEncodeMode |
| 42 |
SWSB_ENCODE_MODE swsbEncodingMode = SWSB_ENCODE_MODE::SWSBInvalidMode; |
| 43 |
bool printInstDeps = false; |
| 44 |
bool printInstDefs = false; |
| 45 |
bool printInstBits = true; |
| 46 |
bool printLdSt = false; |
| 47 |
bool printAnsi = false; |
| 48 |
bool printJson = false; |
| 49 |
int printJsonVersion = 1; |
| 50 |
DepAnalysis *liveAnalysis = nullptr; |
| 51 |
uint32_t basePCOffset = 0; |
| 52 |
|
| 53 |
// format with default labels |
| 54 |
FormatOpts(const Model &m) : model(m) {} |
| 55 |
|
| 56 |
// format with all parameters settable |
| 57 |
// requires at least a context |
| 58 |
FormatOpts(const Model &m, LabelerFunction _labeler, |
| 59 |
void *_labelerCtx = nullptr, bool _numericLabels = false, |
| 60 |
bool _printInstPc = false, bool _hexFloats = true, |
| 61 |
bool _syntaxExtensions = false) |
| 62 |
: model(m), labeler(_labeler), labelerContext(_labelerCtx), |
| 63 |
numericLabels(_numericLabels), hexFloats(_hexFloats), |
| 64 |
printInstPc(_printInstPc), syntaxExtensions(_syntaxExtensions) {} |
| 65 |
|
| 66 |
void setSWSBEncodingMode(SWSB_ENCODE_MODE mode) { swsbEncodingMode = mode; } |
| 67 |
|
| 68 |
// in iga.cpp |
| 69 |
// pcOffset is added to pc string in comments during disassembly |
| 70 |
void addApiOpts(uint32_t fmtOpts, uint32_t pcOffset = 0); |
| 71 |
}; |
| 72 |
|
| 73 |
void FormatKernel(ErrorHandler &e, std::ostream &o, const FormatOpts &opts, |
| 74 |
const Kernel &k, const void *bits = nullptr); |
| 75 |
|
| 76 |
void FormatInstruction(ErrorHandler &e, std::ostream &o, const FormatOpts &opts, |
| 77 |
const Instruction &i, const void *bits = nullptr); |
| 78 |
|
| 79 |
#ifndef IGA_DISABLE_ENCODER_EXCEPTIONS |
| 80 |
// this uses the decoder, which uses exceptions |
| 81 |
// but only the IGA tester needs this; so we can ifdef it out |
| 82 |
void FormatInstruction(ErrorHandler &e, std::ostream &o, const FormatOpts &opts, |
| 83 |
size_t startPc, const void *bits, |
| 84 |
bool useNativeDecoder = false); |
| 85 |
void FormatInstruction(ErrorHandler &e, std::ostream &o, const FormatOpts &opts, |
| 86 |
const void *bits); |
| 87 |
#endif // IGA_DISABLE_ENCODER_EXCEPTIONS |
| 88 |
|
| 89 |
void GetDefaultLabelName(std::ostream &o, int32_t pc); |
| 90 |
|
| 91 |
///////////////////////////////////////////////////////// |
| 92 |
// Stuff mainly for testing and debugging. |
| 93 |
|
| 94 |
// for debugging, shows the opcode and bytes |
| 95 |
std::string FormatOpBits(const iga::Model &m, const void *bits); |
| 96 |
// decodes the op for this platform |
| 97 |
std::string FormatOpName(const iga::Model &m, const void *bits); |
| 98 |
|
| 99 |
////////////////////////////////////////////////////////////////////////// |
| 100 |
// Used by other modules internal to this directory |
| 101 |
////////////////////////////////////////////////////////////////////////// |
| 102 |
|
| 103 |
// This wraps a constant ansi escape sequence in a unique type so |
| 104 |
// template functions can handle them different from (const char *)'s. |
| 105 |
// We define emitT(ansi_esc) to not count the characters as part of the |
| 106 |
// current column (for column alignment). |
| 107 |
struct ansi_esc { |
| 108 |
const char *esc; |
| 109 |
constexpr ansi_esc() : ansi_esc(nullptr) {} |
| 110 |
constexpr ansi_esc(const char *_seq) : esc(_seq) {} |
| 111 |
}; |
| 112 |
|
| 113 |
// This wraps a value of some sort to be emitted with an ANSI escape |
| 114 |
// sequence. We emit this and an ANSI_RESET afterwards. |
| 115 |
// E.g. emit(ansi_span<int>("\033[1;32m",0x12)) |
| 116 |
template <typename T> struct ansi_span { |
| 117 |
ansi_esc esc; |
| 118 |
const T &val; |
| 119 |
constexpr ansi_span(const T &_val) : ansi_span(nullptr, _val) {} |
| 120 |
constexpr ansi_span(const char *_seq, const T &_val) : esc(_seq), val(_val) {} |
| 121 |
}; |
| 122 |
|
| 123 |
// The abstract implementation of a formatter that has a notion of |
| 124 |
// column alignment and some other basic, language-agnostic constructs. |
| 125 |
class BasicFormatter { |
| 126 |
size_t currColCapacity; // preferred size of current column |
| 127 |
size_t currColSize; // current col's size |
| 128 |
size_t currLineDebt; // sum total of the column overflow |
| 129 |
// (later cols can pay for overflow |
| 130 |
// to realign future columns) |
| 131 |
bool printAnsi; |
| 132 |
|
| 133 |
protected: |
| 134 |
ansi_esc ANSI_RESET; |
| 135 |
// subclasses to this class may define other ansi sequences |
| 136 |
|
| 137 |
std::ostream &o; |
| 138 |
|
| 139 |
BasicFormatter(bool _printAnsi, std::ostream &out) |
| 140 |
: currColCapacity((size_t)-1), currColSize(0), |
| 141 |
currLineDebt(0), printAnsi(_printAnsi), o(out) { |
| 142 |
// TODO: could make these mappable via environment variable |
| 143 |
// export IGA_FormatAnsiRegisterArf="\033[38;2;138;43;211m" |
| 144 |
// export IGA_FormatAnsiMnemonic=... |
| 145 |
// export IGA_FormatAnsiComment=... |
| 146 |
if (printAnsi) { |
| 147 |
ANSI_RESET = "\033[0m"; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
public: |
| 152 |
// start or finish a padded column |
| 153 |
void startColumn(int len) { |
| 154 |
IGA_ASSERT(len >= 0, "negative column size"); |
| 155 |
IGA_ASSERT(currColCapacity == (size_t)-1, |
| 156 |
"startColumn() called from with-in column " |
| 157 |
"(missing finishColumn())"); |
| 158 |
currColCapacity = (size_t)len; |
| 159 |
currColSize = 0; |
| 160 |
} |
| 161 |
void finishColumn() { |
| 162 |
IGA_ASSERT(currColCapacity != (size_t)-1, |
| 163 |
"finishColumn() called with no active column " |
| 164 |
"(missing startColumn(...))"); |
| 165 |
// |
| 166 |
if (currColSize <= currColCapacity) { |
| 167 |
// we underflowed; we have space left; |
| 168 |
// if there is debt, we can cover some of it |
| 169 |
size_t padding = currColCapacity - currColSize; |
| 170 |
if (currLineDebt > 0) { |
| 171 |
size_t correction = std::min<size_t>(currLineDebt, padding); |
| 172 |
padding -= correction; |
| 173 |
currLineDebt -= correction; |
| 174 |
} |
| 175 |
emitSpaces(padding); |
| 176 |
} else if (currColSize > currColCapacity) { |
| 177 |
// we overflowed accummulate debt |
| 178 |
currLineDebt += currColSize - currColCapacity; |
| 179 |
} |
| 180 |
// |
| 181 |
currColCapacity = (size_t)-1; |
| 182 |
currColSize = 0; |
| 183 |
} |
| 184 |
|
| 185 |
void newline() { |
| 186 |
IGA_ASSERT(currColCapacity == (size_t)-1, "column open at newline"); |
| 187 |
emit('\n'); |
| 188 |
currLineDebt = 0; |
| 189 |
} |
| 190 |
|
| 191 |
template <typename T> void emitT(ansi_span<T> t) { |
| 192 |
emitT(t.esc); |
| 193 |
emitT(t.value); |
| 194 |
emitT(ANSI_RESET); |
| 195 |
} |
| 196 |
void emitT(ansi_esc e) { |
| 197 |
// specialize this instance to not count the ANSI escapes as |
| 198 |
// output characters (size of column) |
| 199 |
if (e.esc) |
| 200 |
o << e.esc; |
| 201 |
} |
| 202 |
template <typename T> void emitT(const T &t) { |
| 203 |
size_t n = (size_t)o.tellp(); |
| 204 |
o << t; |
| 205 |
currColSize += (size_t)o.tellp() - n; |
| 206 |
} |
| 207 |
|
| 208 |
// enables you emit a list of things |
| 209 |
// |
| 210 |
// emit(foo, bar, baz, ...); |
| 211 |
template <typename T, typename... Ts> void emit(const T& t) { emitT(t); } |
| 212 |
template <typename T, typename... Ts> void emit(const T& t, const Ts&... ts) { |
| 213 |
emitT(t); |
| 214 |
emit(ts...); |
| 215 |
} |
| 216 |
|
| 217 |
// emits an sequence conditionally surrounded by an ANSI color |
| 218 |
template <typename T, typename... Ts> |
| 219 |
void emitAnsi(bool z, ansi_esc esc, T t0, Ts... ts) { |
| 220 |
if (z) |
| 221 |
emitT(esc); |
| 222 |
|
| 223 |
emit(std::move(t0), std::move(ts)...); |
| 224 |
|
| 225 |
if (z) |
| 226 |
emitT(ANSI_RESET); |
| 227 |
} |
| 228 |
template <typename T, typename... Ts> |
| 229 |
void emitAnsi(ansi_esc esc, T t, Ts... ts) { |
| 230 |
emitAnsi(true, esc, std::move(t), std::move(ts)...); |
| 231 |
} |
| 232 |
|
| 233 |
void emitSpaces(size_t n) { |
| 234 |
for (size_t i = 0; i < n; i++) |
| 235 |
o << ' '; |
| 236 |
currColSize += n; |
| 237 |
} |
| 238 |
|
| 239 |
template <typename T> void emitDecimal(const T &t) { o << std::dec << t; } |
| 240 |
|
| 241 |
template <typename T> void emitHex(const T &t, int cw = 0) { |
| 242 |
fmtHex(o, (uint64_t)t, cw); |
| 243 |
o << std::dec; |
| 244 |
} |
| 245 |
|
| 246 |
template <typename T> void emitHexDigits(const T &t, int cw = 0) { |
| 247 |
fmtHexDigits(o, (uint64_t)t, cw); |
| 248 |
o << std::dec; |
| 249 |
} |
| 250 |
|
| 251 |
template <typename T> void emitBinary(const T &t) { |
| 252 |
emit("0b"); |
| 253 |
std::stringstream ss; |
| 254 |
T bits = t; |
| 255 |
while (bits != 0) { |
| 256 |
emit((bits & 0x1) ? '1' : '0'); |
| 257 |
bits >>= 1; |
| 258 |
} |
| 259 |
std::string str = ss.str(); |
| 260 |
for (int i = (int)str.length() - 1; i >= 0; i--) { |
| 261 |
emit(str[i]); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
template <typename T> void emitFloat(const T &f) { FormatFloat(o, f); } |
| 266 |
}; |
| 267 |
|
| 268 |
struct ColumnPreferences { |
| 269 |
int predication; |
| 270 |
int opCodeExecInfo; |
| 271 |
int flagMod; |
| 272 |
int dstOp; |
| 273 |
int srcOp; |
| 274 |
int sendDstOp; // we give send ops less space than normal |
| 275 |
int sendSrcOp; // we give send ops less space than norma |
| 276 |
int sendDesc; |
| 277 |
ColumnPreferences() |
| 278 |
// enough space for (~f0.0) or (W), (W&~f0.0) overflows |
| 279 |
: predication(7) |
| 280 |
// sends (16|M0) |
| 281 |
// madm (16|M16) |
| 282 |
, |
| 283 |
opCodeExecInfo(13) |
| 284 |
// (le)f0.0 |
| 285 |
, |
| 286 |
flagMod(9) |
| 287 |
// r120.11<1>:hf |
| 288 |
, |
| 289 |
dstOp(12), srcOp(16), sendDstOp(7) // null:ud |
| 290 |
// r120.4<4;4,1>:ub |
| 291 |
, |
| 292 |
sendSrcOp(6) // r120#1 |
| 293 |
, |
| 294 |
sendDesc(10) {} |
| 295 |
}; |
| 296 |
} // namespace iga |
| 297 |
|
| 298 |
#endif |