| 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_BACKEND_NATIVE_MINST_HPP |
| 10 |
#define IGA_BACKEND_NATIVE_MINST_HPP |
| 11 |
|
| 12 |
#include "../../bits.hpp" |
| 13 |
#include "Field.hpp" |
| 14 |
|
| 15 |
#include <cstdint> |
| 16 |
|
| 17 |
namespace iga { |
| 18 |
// machine instruction |
| 19 |
struct MInst { |
| 20 |
union { |
| 21 |
struct { |
| 22 |
uint32_t dw0, dw1, dw2, dw3; |
| 23 |
}; |
| 24 |
struct { |
| 25 |
uint32_t dws[4]; |
| 26 |
}; |
| 27 |
struct { |
| 28 |
uint64_t qw0, qw1; |
| 29 |
}; |
| 30 |
struct { |
| 31 |
uint64_t qws[2]; |
| 32 |
}; |
| 33 |
}; |
| 34 |
|
| 35 |
MInst() { qw0 = qw1 = 0; } |
| 36 |
void reset() { qw0 = qw1 = 0; } |
| 37 |
|
| 38 |
bool operator==(const MInst &rhs) const { |
| 39 |
return qw0 == rhs.qw0 && qw1 == rhs.qw1; |
| 40 |
} |
| 41 |
bool operator!=(const MInst &rhs) const { return !(*this == rhs); } |
| 42 |
bool operator<(const MInst &rhs) const { |
| 43 |
return (qw1 < rhs.qw1) || (qw1 == rhs.qw1 && qw0 < rhs.qw0); |
| 44 |
} |
| 45 |
|
| 46 |
bool testBit(int off) const { return getBits(off, 1) != 0; } |
| 47 |
|
| 48 |
uint64_t getBits(int off, int len) const { |
| 49 |
return iga::getBits<uint64_t, 2>(qws, off, len); |
| 50 |
} |
| 51 |
|
| 52 |
uint64_t getFragment(const Fragment &f) const { |
| 53 |
switch (f.kind) { |
| 54 |
case Fragment::Kind::ZERO_FILL: |
| 55 |
case Fragment::Kind::ZERO_WIRES: |
| 56 |
return 0; |
| 57 |
case Fragment::Kind::ENCODED: |
| 58 |
return getBits(f.offset, f.length); |
| 59 |
default: |
| 60 |
IGA_ASSERT_FALSE("unreachable"); |
| 61 |
return (uint64_t)-1; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
// gets a fragmented field |
| 66 |
uint64_t getBits(const Fragment &fLo, const Fragment &fHi) const { |
| 67 |
uint64_t lo = getBits(fLo.offset, fLo.length); |
| 68 |
uint64_t hi = getBits(fHi.offset, fHi.length); |
| 69 |
return ((hi << fLo.length) | lo); |
| 70 |
} |
| 71 |
|
| 72 |
uint64_t getField(const Field &f) const { |
| 73 |
uint64_t bits = 0; |
| 74 |
// |
| 75 |
int off = 0; |
| 76 |
for (const Fragment &fr : f.fragments) { |
| 77 |
if (fr.kind == Fragment::Kind::INVALID) |
| 78 |
break; |
| 79 |
auto frag = fr.isZeroFill() ? 0 : getBits(fr.offset, fr.length); |
| 80 |
bits |= frag << off; |
| 81 |
off += fr.length; |
| 82 |
} |
| 83 |
// |
| 84 |
return bits; |
| 85 |
} |
| 86 |
int64_t getSignedField(const Field &f) const { |
| 87 |
auto bits = getField(f); |
| 88 |
return getSignedBits(bits, 0, f.length()); |
| 89 |
} |
| 90 |
|
| 91 |
// gets a fragmented field from an array of fields |
| 92 |
// the fields are ordered low bit to high bit |
| 93 |
// we stop when we find a field with length 0 |
| 94 |
template <int N> uint64_t getBits(const Fragment ff[N]) const { |
| 95 |
uint64_t bits = 0; |
| 96 |
|
| 97 |
int off = 0; |
| 98 |
for (const Fragment &fr : ff) { |
| 99 |
if (fr.isInvalid()) { |
| 100 |
break; |
| 101 |
} |
| 102 |
auto frag = fr.isZeroFill() |
| 103 |
? 0 |
| 104 |
: iga::getBits<uint64_t, N>(qws, fr.offset, fr.length); |
| 105 |
bits |= frag << off; |
| 106 |
off += fr.length; |
| 107 |
} |
| 108 |
|
| 109 |
return bits; |
| 110 |
} |
| 111 |
|
| 112 |
// returns false if field is too small to hold the value |
| 113 |
bool setField(const Field &f, uint64_t val) { |
| 114 |
for (const auto &fr : f.fragments) { |
| 115 |
if (fr.kind == Fragment::Kind::INVALID) |
| 116 |
break; |
| 117 |
const auto fragValue = val & fr.getMask(); |
| 118 |
switch (fr.kind) { |
| 119 |
case Fragment::Kind::ENCODED: |
| 120 |
if (!setBits(fr.offset, fr.length, fragValue)) { |
| 121 |
// this overflows the fragment |
| 122 |
return false; |
| 123 |
} |
| 124 |
break; |
| 125 |
case Fragment::Kind::ZERO_WIRES: |
| 126 |
case Fragment::Kind::ZERO_FILL: |
| 127 |
if (fragValue != 0) { |
| 128 |
// an intrinsic fragment has non-zero bits |
| 129 |
// e.g. ternary Dst.Subreg[3:0] is non-zero |
| 130 |
return false; |
| 131 |
} |
| 132 |
break; |
| 133 |
default: |
| 134 |
IGA_ASSERT_FALSE("unreachable"); |
| 135 |
} |
| 136 |
val >>= fr.length; |
| 137 |
} |
| 138 |
return val == 0; // ensure no high bits left over |
| 139 |
} |
| 140 |
|
| 141 |
bool setFragment(const Fragment &fr, uint64_t val) { |
| 142 |
if (fr.isEncoded()) |
| 143 |
return iga::setBits<uint64_t, 2>(qws, fr.offset, fr.length, val); |
| 144 |
else |
| 145 |
return (fr.isZeroFill() && val == 0); |
| 146 |
} |
| 147 |
|
| 148 |
bool setBits(int off, int len, uint64_t val) { |
| 149 |
return iga::setBits<uint64_t, 2>(qws, off, len, val); |
| 150 |
} |
| 151 |
|
| 152 |
// checks if a machine instruction is compacted |
| 153 |
// if compacted, the high 64b are undefined |
| 154 |
bool isCompact() const { return testBit(29); } |
| 155 |
}; |
| 156 |
static_assert(sizeof(MInst) == 16, "MInst must be 16 bytes"); |
| 157 |
} // namespace iga |
| 158 |
|
| 159 |
#endif // IGA_BACKEND_NATIVE_MINST_HPP |