[llvm] [LLVM][TableGen][DecoderEmitter] Add wrapper class for `bit_value_t` (PR #146248)
Craig Topper via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 30 10:49:13 PDT 2025
================
@@ -252,84 +246,86 @@ class DecoderEmitter {
StringRef PredicateNamespace;
};
-} // end anonymous namespace
-
// The set (BIT_TRUE, BIT_FALSE, BIT_UNSET) represents a ternary logic system
// for a bit value.
//
// BIT_UNFILTERED is used as the init value for a filter position. It is used
// only for filter processings.
-typedef enum : uint8_t {
- BIT_FALSE, // '0'
- BIT_TRUE, // '1'
- BIT_UNSET, // '?'
- BIT_UNFILTERED // unfiltered
-} bit_value_t;
-
-static bool ValueSet(bit_value_t V) {
- return (V == BIT_TRUE || V == BIT_FALSE);
-}
+class BitValue {
+public:
+ enum bit_value_t : uint8_t {
+ BIT_FALSE, // '0'
+ BIT_TRUE, // '1'
+ BIT_UNSET, // '?', printed as '_'
+ BIT_UNFILTERED // unfiltered, printed as '.'
+ };
+
+ BitValue(bit_value_t V) : V(V) {}
+ BitValue(const Init *Init) {
+ if (const BitInit *Bit = dyn_cast<BitInit>(Init))
+ V = Bit->getValue() ? BIT_TRUE : BIT_FALSE;
+ else
+ V = BIT_UNSET;
+ }
+ BitValue(const BitsInit &Bits, unsigned Idx) : BitValue(Bits.getBit(Idx)) {}
+
+ bool isSet(void) const { return V == BIT_TRUE || V == BIT_FALSE; }
+ bool isUnset(void) const { return V == BIT_UNSET; }
+ std::optional<uint64_t> getValue(void) const {
+ if (isSet())
+ return static_cast<uint64_t>(V);
+ return std::nullopt;
+ }
+ bit_value_t getRawValue() const { return V; }
-static bool ValueNotSet(bit_value_t V) { return (V == BIT_UNSET); }
+ // For printing a bit value.
+ operator StringRef() const { return StringRef("01_.").slice(V, V + 1); }
-static int Value(bit_value_t V) {
- return ValueNotSet(V) ? -1 : (V == BIT_FALSE ? 0 : 1);
-}
+ bool operator==(bit_value_t Other) const { return Other == V; }
+ bool operator!=(bit_value_t Other) const { return Other != V; }
+
+private:
+ bit_value_t V;
+};
-static bit_value_t bitFromBits(const BitsInit &bits, unsigned index) {
- if (const BitInit *bit = dyn_cast<BitInit>(bits.getBit(index)))
- return bit->getValue() ? BIT_TRUE : BIT_FALSE;
+} // end anonymous namespace
- // The bit is uninitialized.
- return BIT_UNSET;
+static raw_ostream &operator<<(raw_ostream &OS, const EncodingAndInst &Value) {
+ if (Value.EncodingDef != Value.Inst->TheDef)
+ OS << Value.EncodingDef->getName() << ":";
+ OS << Value.Inst->TheDef->getName();
+ return OS;
}
// Prints the bit value for each position.
-static void dumpBits(raw_ostream &OS, const BitsInit &bits) {
- for (unsigned index = bits.getNumBits(); index > 0; --index) {
- switch (bitFromBits(bits, index - 1)) {
- case BIT_TRUE:
- OS << "1";
- break;
- case BIT_FALSE:
- OS << "0";
- break;
- case BIT_UNSET:
- OS << "_";
- break;
- default:
- llvm_unreachable("unexpected return value from bitFromBits");
- }
- }
+static void dumpBits(raw_ostream &OS, const BitsInit &Bits) {
+ for (const Init *Bit : reverse(Bits.getBits()))
+ OS << BitValue(Bit);
}
-static const BitsInit &getBitsField(const Record &def, StringRef str) {
- const RecordVal *RV = def.getValue(str);
+static const BitsInit &getBitsField(const Record &Def, StringRef FieldName) {
+ const RecordVal *RV = Def.getValue(FieldName);
if (const BitsInit *Bits = dyn_cast<BitsInit>(RV->getValue()))
return *Bits;
- // variable length instruction
- VarLenInst VLI = VarLenInst(cast<DagInit>(RV->getValue()), RV);
+ // Handle variable length instructions.
+ VarLenInst VLI(cast<DagInit>(RV->getValue()), RV);
SmallVector<const Init *, 16> Bits;
for (const auto &SI : VLI) {
- if (const BitsInit *BI = dyn_cast<BitsInit>(SI.Value)) {
- for (unsigned Idx = 0U; Idx < BI->getNumBits(); ++Idx) {
- Bits.push_back(BI->getBit(Idx));
- }
- } else if (const BitInit *BI = dyn_cast<BitInit>(SI.Value)) {
+ if (const BitsInit *BI = dyn_cast<BitsInit>(SI.Value))
+ llvm::append_range(Bits, BI->getBits());
+ else if (const BitInit *BI = dyn_cast<BitInit>(SI.Value))
Bits.push_back(BI);
- } else {
- for (unsigned Idx = 0U; Idx < SI.BitWidth; ++Idx)
- Bits.push_back(UnsetInit::get(def.getRecords()));
- }
+ else
+ Bits.insert(Bits.end(), SI.BitWidth, UnsetInit::get(Def.getRecords()));
----------------
topperc wrote:
append?
https://github.com/llvm/llvm-project/pull/146248
More information about the llvm-commits
mailing list