14#include "llvm/ADT/TypeSwitch.h"
23LogicalResult BitVectorAttr::verify(
26 if (value.getBitWidth() < 1)
27 return emitError() <<
"bit-width must be at least 1, but got "
28 << value.getBitWidth();
32std::string BitVectorAttr::getValueAsString(
bool prefix)
const {
33 unsigned width = getValue().getBitWidth();
35 StringRef pref = prefix ?
"#" :
"";
37 getValue().toString(toPrint, 16,
false,
false,
false);
41 return (pref +
"x" + Twine(leadingZeros) + toPrint).str();
44 getValue().toString(toPrint, 2,
false,
false,
false);
47 return (pref +
"b" + Twine(leadingZeros) + toPrint).str();
51static FailureOr<APInt>
58 return emitError() <<
"expected at least one digit";
61 return APInt(value.size() - 2, std::string(value.begin() + 2, value.end()),
65 return APInt((value.size() - 2) * 4,
66 std::string(value.begin() + 2, value.end()), 16);
68 return emitError() <<
"expected either 'b' or 'x'";
71BitVectorAttr BitVectorAttr::get(
MLIRContext *context, StringRef value) {
74 assert(succeeded(maybeValue) &&
"string must have SMT-LIB format");
75 return Base::get(context, *maybeValue);
85 return Base::getChecked(
emitError, context, *maybeValue);
88BitVectorAttr BitVectorAttr::get(
MLIRContext *context, uint64_t value,
90 return Base::get(context, APInt(width, value));
97 if (width < 64 && value >= (UINT64_C(1) << width)) {
98 emitError() <<
"value does not fit in a bit-vector of desired width";
101 return Base::getChecked(
emitError, context, APInt(width, value));
113 if (!odsType || !llvm::isa<BitVectorType>(odsType)) {
114 odsParser.
emitError(loc) <<
"explicit bit-vector type required";
118 unsigned width = llvm::cast<BitVectorType>(odsType).getWidth();
120 if (width > val.getBitWidth()) {
124 val = val.sext(width);
125 }
else if (width < val.getBitWidth()) {
128 unsigned neededBits =
129 val.isNegative() ? val.getSignificantBits() : val.getActiveBits();
130 if (width < neededBits) {
132 <<
"integer value out of range for given bit-vector type " << odsType;
135 val = val.trunc(width);
138 return BitVectorAttr::get(odsParser.
getContext(), val);
141void BitVectorAttr::print(
AsmPrinter &odsPrinter)
const {
145 odsPrinter <<
"<" << getValue() <<
">";
148Type BitVectorAttr::getType()
const {
156#define GET_ATTRDEF_CLASSES
157#include "mlir/Dialect/SMT/IR/SMTAttributes.cpp.inc"
159void SMTDialect::registerAttributes() {
161#define GET_ATTRDEF_LIST
162#include "mlir/Dialect/SMT/IR/SMTAttributes.cpp.inc"
static unsigned getBitWidth(Type type)
static FailureOr< APInt > parseBitVectorString(function_ref< InFlightDiagnostic()> emitError, StringRef value)
Parse an SMT-LIB formatted bit-vector string.
This base class exposes generic asm parser hooks, usable across the various derived parsers.
MLIRContext * getContext() const
virtual InFlightDiagnostic emitError(SMLoc loc, const Twine &message={})=0
Emit a diagnostic at the specified location and return failure.
ParseResult parseInteger(IntT &result)
Parse an integer value from the stream.
virtual ParseResult parseLess()=0
Parse a '<' token.
virtual SMLoc getCurrentLocation()=0
Get the location of the next token and store it into the argument.
virtual ParseResult parseGreater()=0
Parse a '>' token.
This base class exposes generic asm printer hooks, usable across the various derived printers.
Attributes are known-constant values of operations.
This class represents a diagnostic that is inflight and set to be reported.
MLIRContext is the top-level object for a collection of MLIR operations.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
llvm::function_ref< Fn > function_ref