| 1 |
/*========================== begin_copyright_notice ============================ |
| 2 |
|
| 3 |
Copyright (C) 2018-2021 Intel Corporation |
| 4 |
|
| 5 |
SPDX-License-Identifier: MIT |
| 6 |
|
| 7 |
============================= end_copyright_notice ===========================*/ |
| 8 |
|
| 9 |
#ifndef IGA_NATIVE_INSTDECODER_HPP |
| 10 |
#define IGA_NATIVE_INSTDECODER_HPP |
| 11 |
|
| 12 |
#include "../../Frontend/Floats.hpp" |
| 13 |
#include "../../Frontend/IRToString.hpp" |
| 14 |
#include "../../IR/InstBuilder.hpp" |
| 15 |
#include "../../strings.hpp" |
| 16 |
#include "../BitProcessor.hpp" |
| 17 |
#include "Field.hpp" |
| 18 |
#include "Interface.hpp" |
| 19 |
#include "MInst.hpp" |
| 20 |
|
| 21 |
#include <algorithm> |
| 22 |
#include <functional> |
| 23 |
#include <initializer_list> |
| 24 |
#include <iomanip> |
| 25 |
#include <sstream> |
| 26 |
#include <string> |
| 27 |
#include <vector> |
| 28 |
|
| 29 |
namespace iga { |
| 30 |
typedef std::function<void(uint64_t bits, std::stringstream &ss)> |
| 31 |
FormatFunction; |
| 32 |
|
| 33 |
static inline int nextPowerOfTwo(int v) { |
| 34 |
v--; |
| 35 |
v |= v >> 1; |
| 36 |
v |= v >> 2; |
| 37 |
v |= v >> 4; |
| 38 |
v |= v >> 8; |
| 39 |
v |= v >> 16; |
| 40 |
v++; |
| 41 |
return v; |
| 42 |
} |
| 43 |
static inline PredCtrl decodePredCtrl(uint64_t bits) { |
| 44 |
return static_cast<PredCtrl>(bits); |
| 45 |
} |
| 46 |
static inline PredCtrl decodePredCtrlPvc(uint64_t bits) { |
| 47 |
switch (bits) { |
| 48 |
case 0: |
| 49 |
return PredCtrl::NONE; |
| 50 |
case 1: |
| 51 |
return PredCtrl::SEQ; |
| 52 |
case 2: |
| 53 |
return PredCtrl::ANY; |
| 54 |
case 3: |
| 55 |
return PredCtrl::ALL; |
| 56 |
} |
| 57 |
return static_cast<PredCtrl>(bits); |
| 58 |
} |
| 59 |
static inline ExecSize decodeExecSizeBits(uint64_t val) { |
| 60 |
switch (val) { |
| 61 |
case 0: |
| 62 |
return ExecSize::SIMD1; |
| 63 |
case 1: |
| 64 |
return ExecSize::SIMD2; |
| 65 |
case 2: |
| 66 |
return ExecSize::SIMD4; |
| 67 |
case 3: |
| 68 |
return ExecSize::SIMD8; |
| 69 |
case 4: |
| 70 |
return ExecSize::SIMD16; |
| 71 |
case 5: |
| 72 |
return ExecSize::SIMD32; |
| 73 |
default: |
| 74 |
return ExecSize::INVALID; |
| 75 |
} |
| 76 |
} |
| 77 |
static inline ChannelOffset decodeChannelOffsetBits(uint64_t val) { |
| 78 |
return static_cast<ChannelOffset>(val); |
| 79 |
} |
| 80 |
static inline FlagModifier decodeFlagModifierBits(uint64_t val) { |
| 81 |
FlagModifier fm; |
| 82 |
switch (val) { |
| 83 |
case 0: |
| 84 |
fm = FlagModifier::NONE; |
| 85 |
break; |
| 86 |
case 1: |
| 87 |
fm = FlagModifier::EQ; |
| 88 |
break; |
| 89 |
case 2: |
| 90 |
fm = FlagModifier::NE; |
| 91 |
break; |
| 92 |
case 3: |
| 93 |
fm = FlagModifier::GT; |
| 94 |
break; |
| 95 |
case 4: |
| 96 |
fm = FlagModifier::GE; |
| 97 |
break; |
| 98 |
case 5: |
| 99 |
fm = FlagModifier::LT; |
| 100 |
break; |
| 101 |
case 6: |
| 102 |
fm = FlagModifier::LE; |
| 103 |
break; |
| 104 |
// case 7: reserved |
| 105 |
case 8: |
| 106 |
fm = FlagModifier::OV; |
| 107 |
break; |
| 108 |
case 9: |
| 109 |
fm = FlagModifier::UN; |
| 110 |
break; |
| 111 |
default: |
| 112 |
fm = static_cast<FlagModifier>(val); |
| 113 |
break; |
| 114 |
} |
| 115 |
return fm; |
| 116 |
} |
| 117 |
static inline SrcModifier decodeSrcModifierBits(uint64_t bits) { |
| 118 |
switch (bits) { |
| 119 |
case 0: |
| 120 |
return SrcModifier::NONE; |
| 121 |
case 1: |
| 122 |
return SrcModifier::ABS; |
| 123 |
case 2: |
| 124 |
return SrcModifier::NEG; |
| 125 |
case 3: |
| 126 |
return SrcModifier::NEG_ABS; |
| 127 |
default: |
| 128 |
return static_cast<SrcModifier>(bits); |
| 129 |
} |
| 130 |
} |
| 131 |
static inline FlagModifier decodeFlagModifier(uint64_t bits) { |
| 132 |
switch (bits) { |
| 133 |
case 0: |
| 134 |
return FlagModifier::NONE; |
| 135 |
case 1: |
| 136 |
return FlagModifier::EQ; |
| 137 |
case 2: |
| 138 |
return FlagModifier::NE; |
| 139 |
case 3: |
| 140 |
return FlagModifier::GT; |
| 141 |
case 4: |
| 142 |
return FlagModifier::GE; |
| 143 |
case 5: |
| 144 |
return FlagModifier::LT; |
| 145 |
case 6: |
| 146 |
return FlagModifier::LE; |
| 147 |
// |
| 148 |
case 8: |
| 149 |
return FlagModifier::OV; |
| 150 |
case 9: |
| 151 |
return FlagModifier::UN; |
| 152 |
default: |
| 153 |
return static_cast<FlagModifier>(bits); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
// |
| 158 |
// This is a generic template for instruction decoders for various GEN |
| 159 |
// platforms. Users can use this via inheritance or composition equally. |
| 160 |
// This class also decodes field by field for various debugging routines. |
| 161 |
// E.g. iga64 -Xifs ... decodes each field |
| 162 |
// |
| 163 |
// The naming convention is as follows: |
| 164 |
// |
| 165 |
// T decodeXXXX(Field fXXXX) |
| 166 |
// decodes something (an XXXX) using field fXXXX and store this into the |
| 167 |
// field list. This implies that fXXXX is properly part of the format and |
| 168 |
// thus will not overlap with any other field decoded. It also might |
| 169 |
// commit the field to the instruction builder. |
| 170 |
// |
| 171 |
// T peekXXXX(Field fXXXX) |
| 172 |
// decoes something, but quietly (don't add it to the list of |
| 173 |
// fields decoded). E.g. we might speculatively need to look at a field |
| 174 |
// that we might not want to add. |
| 175 |
// |
| 176 |
// TODO: template<typename P> where P is PlatformInfo |
| 177 |
// for GEN-specific stuff use this var; e.g. P.decodeBasicRegType(...) |
| 178 |
struct InstDecoder { |
| 179 |
InstBuilder &builder; |
| 180 |
ErrorHandler &errorHandler; |
| 181 |
const Model &model; |
| 182 |
const OpSpec &os; |
| 183 |
MInst bits; |
| 184 |
FragmentList *fields; |
| 185 |
Loc loc; |
| 186 |
|
| 187 |
// instruction state |
| 188 |
ExecSize execSize; |
| 189 |
InstOptSet instOptSet; |
| 190 |
|
| 191 |
InstDecoder(InstBuilder &_builder, ErrorHandler &_errorHandler, |
| 192 |
const Model &_model, const OpSpec &_os, MInst _bits, |
| 193 |
FragmentList *_fields, Loc _loc) |
| 194 |
: builder(_builder), errorHandler(_errorHandler), model(_model), os(_os), |
| 195 |
bits(_bits), fields(_fields), loc(_loc), execSize(ExecSize::INVALID), |
| 196 |
instOptSet(0) {} |
| 197 |
|
| 198 |
bool isXeHpcPlus() const { return model.platform >= Platform::XE_HPC; } |
| 199 |
|
| 200 |
void reportError(const char *msg) { errorHandler.reportError(loc, msg); } |
| 201 |
void reportError(const std::string &msg) { |
| 202 |
errorHandler.reportError(loc, msg); |
| 203 |
} |
| 204 |
void reportWarning(const std::string &msg) { |
| 205 |
errorHandler.reportWarning(loc, msg); |
| 206 |
} |
| 207 |
void reportFieldError(const Field &f, const char *msg) { |
| 208 |
std::stringstream ss; |
| 209 |
ss << f.name << ": " << msg; |
| 210 |
reportError(ss.str()); |
| 211 |
} |
| 212 |
void reportFieldWarning(const Field &f, const char *msg) { |
| 213 |
std::stringstream ss; |
| 214 |
ss << f.name << ": " << msg; |
| 215 |
reportWarning(ss.str()); |
| 216 |
} |
| 217 |
void reportFieldErrorInvalidValue(const Field &f) { |
| 218 |
std::stringstream ss; |
| 219 |
reportFieldError(f, "invalid value"); |
| 220 |
} |
| 221 |
|
| 222 |
/////////////////////////////////////////// |
| 223 |
// PRIMITIVE FIELD ADDERS |
| 224 |
void addDecodedField(const Field &f, const std::string& meaning) { |
| 225 |
if (fields) { |
| 226 |
int encodedFragments = 0; |
| 227 |
for (const Fragment &fr : f.fragments) { |
| 228 |
if (fr.isInvalid()) |
| 229 |
break; |
| 230 |
else if (fr.isEncoded()) |
| 231 |
encodedFragments++; |
| 232 |
} |
| 233 |
for (const Fragment &fr : f.fragments) { |
| 234 |
if (fr.isInvalid()) |
| 235 |
break; |
| 236 |
else if (fr.isEncoded()) { |
| 237 |
addDecodedFragment(fr, meaning); |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
void addDecodedFragment(const Fragment &fr, const std::string& val) { |
| 243 |
if (fields) |
| 244 |
fields->emplace_back(fr, val); |
| 245 |
} |
| 246 |
uint64_t decodeFragment(const Fragment &f) { |
| 247 |
addDecodedFragment(f, ""); |
| 248 |
return bits.getFragment(f); |
| 249 |
} |
| 250 |
uint64_t decodeFragment(const Fragment &f, FormatFunction fmt) { |
| 251 |
auto val = bits.getFragment(f); |
| 252 |
if (fields) { |
| 253 |
std::stringstream ss; |
| 254 |
fmt(val, ss); |
| 255 |
addDecodedFragment(f, ss.str()); |
| 256 |
} |
| 257 |
return val; |
| 258 |
} |
| 259 |
uint64_t decodeFieldWithFunction(const Fragment &f, FormatFunction fmt) { |
| 260 |
return decodeFragment(f, fmt); |
| 261 |
} |
| 262 |
|
| 263 |
void addReserved(int off, int len, const std::string &errStr = "?") { |
| 264 |
Fragment fRSVD("Reserved", off, len); |
| 265 |
auto b = bits.getFragment(fRSVD); |
| 266 |
addDecodedFragment(fRSVD, b != 0 ? errStr : ""); |
| 267 |
} |
| 268 |
void addReserved(const Fragment &f) { |
| 269 |
std::stringstream ss; |
| 270 |
ss << "? (shadows " << f.name << ")"; |
| 271 |
addReserved(f.offset, f.length, ss.str().c_str()); |
| 272 |
} |
| 273 |
void addReserved(const Field &f) { |
| 274 |
for (const Fragment &fr : f.fragments) { |
| 275 |
if (fr.isEncoded()) { |
| 276 |
addReserved(fr); |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
uint32_t decodeFieldU32(const Field &f) { |
| 282 |
addDecodedField(f, ""); |
| 283 |
return (uint32_t)bits.getField(f); |
| 284 |
} |
| 285 |
|
| 286 |
/////////////////////////////////////////////////////////////////////////// |
| 287 |
// one bit fields |
| 288 |
/////////////////////////////////////////////////////////////////////////// |
| 289 |
template <typename T> |
| 290 |
T decodeField(const Field &f, T val0, const char *str0, T val1, |
| 291 |
const char *str1) { |
| 292 |
IGA_ASSERT(f.length() == 1, "field is >1 bit"); |
| 293 |
T val; |
| 294 |
const char *str = ""; |
| 295 |
auto b = bits.getField(f); |
| 296 |
switch (b) { |
| 297 |
case 0: |
| 298 |
val = val0; |
| 299 |
str = str0; |
| 300 |
break; |
| 301 |
case 1: |
| 302 |
val = val1; |
| 303 |
str = str1; |
| 304 |
break; |
| 305 |
default: |
| 306 |
val = val0; // for compiler warning |
| 307 |
IGA_ASSERT_FALSE("Unreachable"); |
| 308 |
break; // unreachable |
| 309 |
}; |
| 310 |
if (fields) { |
| 311 |
fields->emplace_back(f, str); |
| 312 |
} |
| 313 |
return val; |
| 314 |
} |
| 315 |
bool decodeBoolField(const Field &f, const char *falseMeaning, |
| 316 |
const char *trueMeaning) { |
| 317 |
return decodeField<bool>(f, false, falseMeaning, true, trueMeaning); |
| 318 |
} |
| 319 |
|
| 320 |
/////////////////////////////////////////////////////////////////////// |
| 321 |
// two bit fields |
| 322 |
/////////////////////////////////////////////////////////////////////// |
| 323 |
template <typename T> |
| 324 |
T decodeField(const Field &f, T val0, const char *str0, T val1, |
| 325 |
const char *str1, T val2, const char *str2, T val3, |
| 326 |
const char *str3) { |
| 327 |
// IGA_ASSERT(f.length == 2, "field is not a two bits"); |
| 328 |
// we can have subset fields like Dst.RgnHz[0] in ternary |
| 329 |
IGA_ASSERT(f.length() <= 2, "field is <=2 bits"); |
| 330 |
T val; |
| 331 |
const char *str = ""; |
| 332 |
auto b = bits.getField(f); |
| 333 |
switch (b) { |
| 334 |
case 0: |
| 335 |
val = val0; |
| 336 |
str = str0; |
| 337 |
break; |
| 338 |
case 1: |
| 339 |
val = val1; |
| 340 |
str = str1; |
| 341 |
break; |
| 342 |
case 2: |
| 343 |
val = val2; |
| 344 |
str = str2; |
| 345 |
break; |
| 346 |
case 3: |
| 347 |
val = val3; |
| 348 |
str = str3; |
| 349 |
break; |
| 350 |
default: |
| 351 |
val = val0; // for compiler warning |
| 352 |
IGA_ASSERT_FALSE("Unreachable"); |
| 353 |
break; // unreachable |
| 354 |
}; |
| 355 |
if (fields) { |
| 356 |
fields->emplace_back(f, str); |
| 357 |
} |
| 358 |
return val; |
| 359 |
} |
| 360 |
|
| 361 |
/////////////////////////////////////////////////////////////////////// |
| 362 |
// multi bit fields |
| 363 |
/////////////////////////////////////////////////////////////////////// |
| 364 |
uint64_t decodeField(const Field &f, FormatFunction fmt) { |
| 365 |
auto val = bits.getField(f); |
| 366 |
if (fields) { |
| 367 |
std::stringstream ss; |
| 368 |
fmt(val, ss); |
| 369 |
fields->emplace_back(f, ss.str()); |
| 370 |
} |
| 371 |
return val; |
| 372 |
} |
| 373 |
|
| 374 |
template <typename T> |
| 375 |
T decodeField(const Field &f, T invalid, |
| 376 |
std::initializer_list<std::pair<T, const char *>> vals) { |
| 377 |
IGA_ASSERT(nextPowerOfTwo((int)vals.size()) != f.length(), |
| 378 |
"field is wrong number of bits"); |
| 379 |
int i = 0; |
| 380 |
T retVal = invalid; |
| 381 |
const char *strVal = "?"; |
| 382 |
auto b = bits.getField(f); |
| 383 |
for (const auto &val : vals) { |
| 384 |
if (i == (int)b) { |
| 385 |
retVal = val.first; |
| 386 |
strVal = val.second; |
| 387 |
break; |
| 388 |
} |
| 389 |
i++; |
| 390 |
} |
| 391 |
if (i == (int)vals.size() || retVal == invalid) { // didn't find it |
| 392 |
retVal = invalid; |
| 393 |
strVal = "?"; |
| 394 |
reportFieldErrorInvalidValue(f); |
| 395 |
} |
| 396 |
|
| 397 |
if (fields) { |
| 398 |
fields->emplace_back(f, strVal); |
| 399 |
} |
| 400 |
return retVal; |
| 401 |
} |
| 402 |
|
| 403 |
/////////////////////////////////////////////////////////////////////// |
| 404 |
// generic GEN helpers |
| 405 |
/////////////////////////////////////////////////////////////////////// |
| 406 |
bool isMacro() const { return builder.isMacroOp(); } |
| 407 |
|
| 408 |
void decodeMaskCtrl(const Field &fMASKCTRL) { |
| 409 |
if (decodeBoolField(fMASKCTRL, "", "WrEn")) { |
| 410 |
builder.InstNoMask(loc); |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
void decodePredication(const Field &fPREDINV, const Field &fPREDCTRL, |
| 415 |
const Field &fFLAGREG) { |
| 416 |
PredCtrl predCtrl = isXeHpcPlus() |
| 417 |
? decodePredCtrlPvc(bits.getField(fPREDCTRL)) |
| 418 |
: decodePredCtrl(bits.getField(fPREDCTRL)); |
| 419 |
addDecodedField(fPREDCTRL, ToSyntax(predCtrl)); |
| 420 |
bool predInv = decodeBoolField(fPREDINV, "", "~"); |
| 421 |
if (predInv && predCtrl == PredCtrl::NONE) { |
| 422 |
reportFieldError(fPREDINV, "PredCtrl is not set, but PredInv is set"); |
| 423 |
} |
| 424 |
if (predCtrl != PredCtrl::NONE) { |
| 425 |
RegRef flagReg = peekFlagRegRef(fFLAGREG); |
| 426 |
builder.InstPredication(loc, predInv, flagReg, predCtrl); |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
void decodeBrCtl(const Field &fBRCTL) { |
| 431 |
bool brCtl = decodeBoolField(fBRCTL, "", ".b"); |
| 432 |
builder.InstSubfunction(brCtl ? BranchCntrl::ON : BranchCntrl::OFF); |
| 433 |
} |
| 434 |
|
| 435 |
RegRef peekFlagRegRef(const Field &fFLAGREG) const { |
| 436 |
auto val = (uint32_t)bits.getField(fFLAGREG); |
| 437 |
return RegRef(val >> 1, val & 0x1); |
| 438 |
} |
| 439 |
|
| 440 |
RegRef decodeFlagReg(const Field &fFLAGREG) { |
| 441 |
// flag register |
| 442 |
decodeField(fFLAGREG, [](uint64_t bits, std::stringstream &ss) { |
| 443 |
ss << "f" << (bits >> 1) << "." << (bits & 0x1); |
| 444 |
}); |
| 445 |
return peekFlagRegRef(fFLAGREG); |
| 446 |
} |
| 447 |
|
| 448 |
void decodeFlagModifierField(const Field &fFLAGMOD, const Field &fFLAGREG) { |
| 449 |
RegRef flagReg = peekFlagRegRef(fFLAGREG); |
| 450 |
FlagModifier invalidModifier = static_cast<FlagModifier>(-1); |
| 451 |
FlagModifier zeroValue = FlagModifier::NONE; |
| 452 |
const char *zeroString = ""; |
| 453 |
if (os.is(Op::MATH) && isMacro()) { |
| 454 |
zeroValue = FlagModifier::EO; |
| 455 |
zeroString = "(eo) (early out)"; |
| 456 |
} |
| 457 |
// TODO: use decodeFlagModifier() and ToSyntax |
| 458 |
FlagModifier flagMod = |
| 459 |
decodeField<FlagModifier>(fFLAGMOD, invalidModifier, |
| 460 |
{{zeroValue, zeroString}, |
| 461 |
{FlagModifier::EQ, "(eq)"}, |
| 462 |
{FlagModifier::NE, "(ne)"}, |
| 463 |
{FlagModifier::GT, "(gt)"}, |
| 464 |
{FlagModifier::GE, "(ge)"}, |
| 465 |
{FlagModifier::LT, "(lt)"}, |
| 466 |
{FlagModifier::LE, "(le)"}, |
| 467 |
{invalidModifier, "?"}, |
| 468 |
{FlagModifier::OV, "(ov) (overflow)"}, |
| 469 |
{FlagModifier::UN, "(un) (unordered)"}}); |
| 470 |
builder.InstFlagModifier(flagReg, flagMod); |
| 471 |
} |
| 472 |
|
| 473 |
ImmVal decodeImm32(const Field &fIMM32L, Type t) { |
| 474 |
std::stringstream ss; |
| 475 |
ImmVal immVal; |
| 476 |
uint64_t val = bits.getField(fIMM32L); |
| 477 |
// |
| 478 |
// TODO: determine if ImmVal should store everything as s64 |
| 479 |
// otherwise I need to normalize what the GED parser and decoder |
| 480 |
// a specific exmaple: |
| 481 |
// NATIVE: 0x0000000000008000 |
| 482 |
// GED: 0xFFFFFFFFFFFF8000 |
| 483 |
// |
| 484 |
// NATIVE: src0: PC[0]: |
| 485 |
// mov: 61 00 03 00 a0 45 05 01 00 00 00 00 00 80 00 80 |
| 486 |
// mov (8|M0) r1.0<1>:f -32768:w |
| 487 |
// GED takes a 64b value |
| 488 |
switch (t) { |
| 489 |
case Type::UW: |
| 490 |
immVal = (uint16_t)val; |
| 491 |
ss << std::hex << (uint16_t)val; |
| 492 |
break; |
| 493 |
case Type::UD: |
| 494 |
immVal = (uint32_t)val; |
| 495 |
ss << std::hex << "0x" << (uint32_t)val; |
| 496 |
break; |
| 497 |
case Type::W: |
| 498 |
immVal = (int16_t)val; |
| 499 |
immVal.s64 = immVal.s16; |
| 500 |
ss << std::dec << (int16_t)val; |
| 501 |
break; |
| 502 |
case Type::D: |
| 503 |
immVal = (int32_t)val; |
| 504 |
immVal.s64 = immVal.s32; |
| 505 |
ss << std::dec << (int32_t)val; |
| 506 |
break; |
| 507 |
case Type::HF: |
| 508 |
immVal = (uint16_t)val; |
| 509 |
immVal.kind = ImmVal::Kind::F16; |
| 510 |
FormatFloat(ss, FloatFromBits((uint16_t)val)); |
| 511 |
break; |
| 512 |
case Type::F: |
| 513 |
immVal = FloatFromBits((uint32_t)val); |
| 514 |
FormatFloat(ss, FloatFromBits((uint32_t)val)); |
| 515 |
break; |
| 516 |
case Type::V: |
| 517 |
case Type::UV: |
| 518 |
immVal = (uint32_t)val; |
| 519 |
ss << "("; |
| 520 |
for (int i = 7; i >= 0; i--) { |
| 521 |
if (i < 7) |
| 522 |
ss << ','; |
| 523 |
if (t == Type::V) { |
| 524 |
ss << std::hex << getSignedBits<int64_t>((int64_t)val, i * 4, 4); |
| 525 |
} else { |
| 526 |
ss << std::dec << getBits<uint64_t>(val, i * 4, 4); |
| 527 |
} |
| 528 |
} |
| 529 |
ss << ")"; |
| 530 |
break; |
| 531 |
case Type::VF: |
| 532 |
immVal = (uint32_t)val; |
| 533 |
ss << "("; |
| 534 |
for (int i = 3; i >= 0; i--) { |
| 535 |
if (i < 3) |
| 536 |
ss << ','; |
| 537 |
FormatFloat(ss, (uint8_t)getBits(val, i * 8, 8)); |
| 538 |
} |
| 539 |
ss << ")"; |
| 540 |
break; |
| 541 |
break; |
| 542 |
default: |
| 543 |
immVal = (uint32_t)0; |
| 544 |
reportFieldError(fIMM32L, "invalid type for 32b IMM"); |
| 545 |
ss << "?"; |
| 546 |
} |
| 547 |
|
| 548 |
addDecodedField(fIMM32L, ss.str()); |
| 549 |
|
| 550 |
return immVal; |
| 551 |
} |
| 552 |
|
| 553 |
ImmVal decodeImm64(const Field &fIMM32L, const Field &fIMM32H, Type type) { |
| 554 |
uint64_t lo = bits.getField(fIMM32L); |
| 555 |
uint64_t hi = bits.getField(fIMM32H); |
| 556 |
ImmVal immVal; |
| 557 |
immVal.u64 = ((hi << 32) | lo); |
| 558 |
; |
| 559 |
std::stringstream ss; |
| 560 |
ss << "("; |
| 561 |
switch (type) { |
| 562 |
case Type::DF: |
| 563 |
immVal.kind = ImmVal::Kind::F64; |
| 564 |
FormatFloat(ss, FloatFromBits(immVal.u64)); |
| 565 |
break; |
| 566 |
case Type::UQ: |
| 567 |
immVal.kind = ImmVal::Kind::U64; |
| 568 |
ss << immVal.u64; |
| 569 |
break; |
| 570 |
case Type::Q: |
| 571 |
immVal.kind = ImmVal::Kind::S64; |
| 572 |
ss << immVal.s64; |
| 573 |
break; |
| 574 |
default: |
| 575 |
ss << "ERROR: expected 64b type"; |
| 576 |
reportError("ERROR: expected 64b type"); |
| 577 |
} |
| 578 |
ss << ")"; |
| 579 |
addDecodedField(fIMM32L, "LO32" + ss.str()); |
| 580 |
addDecodedField(fIMM32H, "HI32" + ss.str()); |
| 581 |
return immVal; |
| 582 |
} |
| 583 |
|
| 584 |
void decodeExecOffsetInfo(const Field &fEXECSIZE, const Field &fCHANOFF) { |
| 585 |
execSize = decodeExecSizeBits(bits.getField(fEXECSIZE)); |
| 586 |
if (execSize == ExecSize::INVALID) { |
| 587 |
reportFieldErrorInvalidValue(fEXECSIZE); |
| 588 |
} |
| 589 |
std::stringstream ssEs; |
| 590 |
ssEs << "(" << ToSyntax(execSize) << "|...)"; |
| 591 |
addDecodedField(fEXECSIZE, ssEs.str()); |
| 592 |
|
| 593 |
auto chOffBits = bits.getField(fCHANOFF); |
| 594 |
ChannelOffset chOff = decodeChannelOffsetBits(chOffBits); |
| 595 |
if (chOff < ChannelOffset::M0 || chOff > ChannelOffset::M28) { |
| 596 |
reportFieldErrorInvalidValue(fCHANOFF); |
| 597 |
} |
| 598 |
std::stringstream ssCo; |
| 599 |
ssCo << "(..." << ToSyntax(chOff) << ")"; |
| 600 |
addDecodedField(fCHANOFF, ssCo.str()); |
| 601 |
|
| 602 |
builder.InstExecInfo(loc, execSize, loc, chOff); |
| 603 |
} |
| 604 |
|
| 605 |
DstModifier decodeDstModifier(const Field &fSATURATE) { |
| 606 |
return decodeField(fSATURATE, DstModifier::NONE, "", DstModifier::SAT, |
| 607 |
"(sat)"); |
| 608 |
} |
| 609 |
|
| 610 |
SrcModifier decodeSrcMods(const Field &fSRCMODS) { |
| 611 |
return decodeField<SrcModifier>(fSRCMODS, SrcModifier::NONE, "", |
| 612 |
SrcModifier::ABS, "(abs)", SrcModifier::NEG, |
| 613 |
os.isBitwise() ? "~" : "-", |
| 614 |
SrcModifier::NEG_ABS, "-(abs)"); |
| 615 |
} |
| 616 |
|
| 617 |
MathMacroExt decodeMathMacroReg(const Field &fSPCACC) { |
| 618 |
addReserved(fSPCACC.fragments[0].offset + 4, 1); |
| 619 |
return decodeMathMacroRegField(fSPCACC); |
| 620 |
} |
| 621 |
|
| 622 |
void decodeSubReg(OperandInfo &opInfo, const Field &fSUBREG) { |
| 623 |
decodeSubRegWithType(opInfo, fSUBREG, opInfo.type, ToSyntax(opInfo.type)); |
| 624 |
} |
| 625 |
|
| 626 |
// e.g. for subregisters without proper types |
| 627 |
void decodeSubRegWithImplicitType(OperandInfo &opInfo, const Field &fSUBREG, |
| 628 |
Type t) { |
| 629 |
decodeSubRegWithType(opInfo, fSUBREG, t, ""); |
| 630 |
} |
| 631 |
|
| 632 |
void decodeSubRegWithType(OperandInfo &opInfo, const Field &fSUBREG, |
| 633 |
Type type, const std::string &typeSyntax) { |
| 634 |
auto srb = (int)bits.getField(fSUBREG); |
| 635 |
auto scaled = |
| 636 |
BinaryOffsetToSubReg(srb, opInfo.regOpName, type, model.platform); |
| 637 |
auto unscaled = SubRegToBinaryOffset((int)scaled, opInfo.regOpName, type, |
| 638 |
model.platform); |
| 639 |
if ((int)unscaled != srb) { |
| 640 |
reportFieldError(fSUBREG, |
| 641 |
"subregister offset is misaligned for type size"); |
| 642 |
} |
| 643 |
opInfo.regOpReg.subRegNum = scaled; |
| 644 |
|
| 645 |
std::stringstream ss; |
| 646 |
ss << "." << (int)opInfo.regOpReg.subRegNum << typeSyntax; |
| 647 |
addDecodedField(fSUBREG, ss.str()); |
| 648 |
} |
| 649 |
|
| 650 |
void decodeRegDirectFields(OpIx opIndex, OperandInfo &opInfo, |
| 651 |
const Field &fREGFILE, // direct only |
| 652 |
const Field &fSPCACC, const Field &fSUBREG, |
| 653 |
const Field &fREG) { |
| 654 |
decodeRegFields(opInfo, fREGFILE, fREG); |
| 655 |
if (isMacro()) { |
| 656 |
opInfo.kind = Operand::Kind::MACRO; |
| 657 |
opInfo.regOpMathMacroExtReg = decodeMathMacroRegField(fSPCACC); |
| 658 |
addReserved(fSPCACC.fragments[0].offset + fSPCACC.fragments[0].length, 1); |
| 659 |
if (!IsTernary(opIndex) && (IsDst(opIndex) || ToSrcIndex(opIndex) == 0) && |
| 660 |
fSUBREG.length() > 1 && fSUBREG.fragments[0].length == 1) { |
| 661 |
// for binary macros, the low of subreg is stowed elsewhere |
| 662 |
addReserved(fSUBREG.fragments[0]); |
| 663 |
} |
| 664 |
} else { |
| 665 |
opInfo.kind = Operand::Kind::DIRECT; |
| 666 |
decodeSubReg(opInfo, fSUBREG); |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
void decodeRegFields(OperandInfo &opInfo, const Field &fREGFILE, |
| 671 |
const Field &fREG) { |
| 672 |
bool isGrf = decodeBoolField(fREGFILE, "ARF", "GRF"); |
| 673 |
std::stringstream ss; |
| 674 |
auto regVal = bits.getField(fREG); |
| 675 |
if (isGrf) { |
| 676 |
opInfo.regOpName = RegName::GRF_R; |
| 677 |
opInfo.regOpReg.regNum = (uint16_t)regVal; |
| 678 |
ss << "r" << regVal; |
| 679 |
} else { |
| 680 |
readArfRegisterInfo(opInfo, fREG, ss); |
| 681 |
} |
| 682 |
addDecodedField(fREG, ss.str()); |
| 683 |
} |
| 684 |
|
| 685 |
// factored out so decodeInstSendDstOperand can share it |
| 686 |
void readArfRegisterInfo(OperandInfo &opInfo, const Field &fREG, |
| 687 |
std::stringstream &ssDesc) { |
| 688 |
auto regVal = bits.getField(fREG); |
| 689 |
const RegInfo *regInfo = model.lookupArfRegInfoByRegNum((uint8_t)regVal); |
| 690 |
if (regInfo == nullptr) { |
| 691 |
opInfo.regOpName = RegName::INVALID; |
| 692 |
ssDesc << "invalid ARF"; |
| 693 |
std::stringstream ssErr; |
| 694 |
ssErr << "invalid ARF Reg (" << fmtHex(regVal, 2) << ")"; |
| 695 |
reportFieldError(fREG, ssErr.str().c_str()); |
| 696 |
return; |
| 697 |
} |
| 698 |
// |
| 699 |
opInfo.regOpName = regInfo->regName; |
| 700 |
ssDesc << regInfo->syntax; |
| 701 |
int arfReg = 0; |
| 702 |
if (regVal > 0xFF || !regInfo->decode((uint8_t)regVal, arfReg)) { |
| 703 |
ssDesc << arfReg << "?"; |
| 704 |
std::stringstream ss; |
| 705 |
ss << regInfo->syntax << arfReg << " is out of bounds"; |
| 706 |
reportFieldError(fREG, ss.str().c_str()); |
| 707 |
} else { |
| 708 |
if (regInfo->hasRegNum()) { |
| 709 |
ssDesc << arfReg; |
| 710 |
} |
| 711 |
opInfo.regOpReg.regNum = (uint16_t)arfReg; |
| 712 |
if (regInfo->regNumBase > 0) { |
| 713 |
// if the register covers to another, let's tell |
| 714 |
// them which |
| 715 |
uint8_t coverRegBits = (uint8_t)(regVal & 0xF0); |
| 716 |
const RegInfo *coverRegInfo = |
| 717 |
model.lookupArfRegInfoByRegNum(coverRegBits); |
| 718 |
if (coverRegInfo) { |
| 719 |
// e.g. XE+ acc10 is mme2 |
| 720 |
ssDesc << " (" << coverRegInfo->syntax << (regVal & 0xF) << ")"; |
| 721 |
} |
| 722 |
} |
| 723 |
} |
| 724 |
} |
| 725 |
|
| 726 |
MathMacroExt decodeMathMacroRegField(const Field &fSPCACC) { |
| 727 |
return decodeField<MathMacroExt>(fSPCACC, MathMacroExt::INVALID, |
| 728 |
{{MathMacroExt::MME0, ".mme0"}, |
| 729 |
{MathMacroExt::MME1, ".mme1"}, |
| 730 |
{MathMacroExt::MME2, ".mme2"}, |
| 731 |
{MathMacroExt::MME3, ".mme3"}, |
| 732 |
{MathMacroExt::MME4, ".mme4"}, |
| 733 |
{MathMacroExt::MME5, ".mme5"}, |
| 734 |
{MathMacroExt::MME6, ".mme6"}, |
| 735 |
{MathMacroExt::MME7, ".mme7"}, |
| 736 |
{MathMacroExt::NOMME, ".nomme"}}); |
| 737 |
} |
| 738 |
|
| 739 |
bool decodeInstOpt(const Field &fINSTOPT, InstOpt opt) { |
| 740 |
IGA_ASSERT(fINSTOPT.length() == 1, "inst opt field is >1 bit"); |
| 741 |
|
| 742 |
std::stringstream ss; |
| 743 |
bool z = bits.getField(fINSTOPT) != 0; |
| 744 |
if (z) { |
| 745 |
instOptSet.add(opt); |
| 746 |
ss << "{" << ToSyntax(opt) << "}"; |
| 747 |
} |
| 748 |
addDecodedField(fINSTOPT, ss.str()); |
| 749 |
return z; |
| 750 |
} |
| 751 |
}; // InstDecoder |
| 752 |
} // namespace iga |
| 753 |
|
| 754 |
#endif // IGA_NATIVE_INSTDECODER_HPP |