| 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 |
#include "InstEncoder.hpp" |
| 10 |
#include "../BitProcessor.hpp" |
| 11 |
|
| 12 |
#include <sstream> |
| 13 |
|
| 14 |
// dummy symbol to avoid LNK4221 warning |
| 15 |
|
| 16 |
using namespace iga; |
| 17 |
|
| 18 |
#ifdef IGA_VALIDATE_BITS |
| 19 |
void InstEncoder::reportFieldOverlap(const Fragment &fr) { |
| 20 |
std::stringstream ss; |
| 21 |
ss << "INTERNAL ERROR: "; |
| 22 |
ss << "instruction #" << state.instIndex << " [PC 0x" << std::hex |
| 23 |
<< state.inst->getPC() << "]: " << fr.name << " overlaps with "; |
| 24 |
// iterate the old fragments and find one that overlapped |
| 25 |
for (const Fragment *of : state.fragmentsSet) { |
| 26 |
if (of->overlaps(fr)) { |
| 27 |
ss << of->name; |
| 28 |
auto str = ss.str(); |
| 29 |
// std::cerr << "checkFieldOverlaps: " << str << "\n"; |
| 30 |
// IGA_ASSERT_FALSE(str.c_str()); |
| 31 |
errorT(str); |
| 32 |
return; |
| 33 |
} |
| 34 |
} |
| 35 |
ss << "another field (?)"; |
| 36 |
auto str = ss.str(); |
| 37 |
errorT(str); |
| 38 |
// IGA_ASSERT_FALSE(str.c_str()); |
| 39 |
} |
| 40 |
#endif |
| 41 |
|
| 42 |
void InstEncoder::encodeFieldBits(const Field &f, uint64_t val0) { |
| 43 |
#ifndef IGA_VALIDATE_BITS |
| 44 |
// bit-validation can't cut this corner |
| 45 |
if (val0 == 0) { |
| 46 |
// short circuit since we start with zeros |
| 47 |
return; |
| 48 |
} |
| 49 |
#endif |
| 50 |
uint64_t val = val0; |
| 51 |
for (const Fragment &fr : f.fragments) { |
| 52 |
if (fr.kind == Fragment::Kind::INVALID) |
| 53 |
break; |
| 54 |
const auto MASK = fr.getMask(); |
| 55 |
const auto fragValue = val & MASK; |
| 56 |
switch (fr.kind) { |
| 57 |
case Fragment::Kind::ENCODED: |
| 58 |
bits->setBits(fr.offset, fr.length, fragValue); |
| 59 |
#ifdef IGA_VALIDATE_BITS |
| 60 |
if (state.dirty.getBits(fr.offset, fr.length)) { |
| 61 |
// some of these bits have already been set by some |
| 62 |
// other field; this demonstrates an internal error |
| 63 |
// in our encoding logic |
| 64 |
reportFieldOverlap(fr); |
| 65 |
} |
| 66 |
state.fragmentsSet.push_back(&fr); |
| 67 |
state.dirty.setBits(fr.offset, fr.length, MASK); |
| 68 |
#endif |
| 69 |
break; |
| 70 |
case Fragment::Kind::ZERO_WIRES: |
| 71 |
case Fragment::Kind::ZERO_FILL: |
| 72 |
if (fragValue != 0) { |
| 73 |
errorAtT(state.inst->getLoc(), fr.name, ": ", fmtHex(val0), |
| 74 |
": field fragment must be zero"); |
| 75 |
} |
| 76 |
break; |
| 77 |
default: |
| 78 |
IGA_ASSERT_FALSE("unreachable"); |
| 79 |
} |
| 80 |
val >>= fr.length; |
| 81 |
} |
| 82 |
|
| 83 |
if (val != 0) { |
| 84 |
// value overflows the virtual encoding |
| 85 |
errorAtT(state.inst->getLoc(), f.name, ": ", fmtHex(val0), |
| 86 |
": value is too large for field"); |
| 87 |
} |
| 88 |
} |