CIRCT  20.0.0git
MooreAttributes.cpp
Go to the documentation of this file.
1 //===- MooreAttributes.cpp - Implement the Moore attributes ---------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the Moore dialect attributes.
10 //
11 //===----------------------------------------------------------------------===//
12 
16 #include "mlir/IR/DialectImplementation.h"
17 #include "llvm/ADT/TypeSwitch.h"
18 
19 using namespace circt;
20 using namespace circt::moore;
21 using mlir::AsmParser;
22 using mlir::AsmPrinter;
23 
24 //===----------------------------------------------------------------------===//
25 // FVIntegerAttr
26 //===----------------------------------------------------------------------===//
27 
28 Attribute FVIntegerAttr::parse(AsmParser &p, Type) {
29  // Parse the value and width specifier.
30  FVInt value;
31  unsigned width;
32  llvm::SMLoc widthLoc;
33  if (p.parseLess() || parseFVInt(p, value) || p.parseColon() ||
34  p.getCurrentLocation(&widthLoc) || p.parseInteger(width) ||
35  p.parseGreater())
36  return {};
37 
38  // Make sure the integer fits into the requested number of bits.
39  unsigned neededBits =
40  value.isNegative() ? value.getSignificantBits() : value.getActiveBits();
41  if (width < neededBits) {
42  p.emitError(widthLoc) << "integer literal requires at least " << neededBits
43  << " bits, but attribute specifies only " << width;
44  return {};
45  }
46 
47  return FVIntegerAttr::get(p.getContext(), value.sextOrTrunc(width));
48 }
49 
50 void FVIntegerAttr::print(AsmPrinter &p) const {
51  p << "<";
52  printFVInt(p, getValue());
53  p << " : " << getValue().getBitWidth() << ">";
54 }
55 
56 //===----------------------------------------------------------------------===//
57 // Generated logic
58 //===----------------------------------------------------------------------===//
59 
60 #define GET_ATTRDEF_CLASSES
61 #include "circt/Dialect/Moore/MooreAttributes.cpp.inc"
62 
63 void MooreDialect::registerAttributes() {
64  addAttributes<
65 #define GET_ATTRDEF_LIST
66 #include "circt/Dialect/Moore/MooreAttributes.cpp.inc"
67  >();
68 }
Four-valued arbitrary precision integers.
Definition: FVInt.h:37
FVInt sextOrTrunc(unsigned bitWidth) const
Truncate or sign-extend to a target bit width.
Definition: FVInt.h:155
bool isNegative() const
Determine whether the integer interpreted as a signed number would be negative.
Definition: FVInt.h:181
unsigned getSignificantBits() const
Compute the minimum bit width necessary to accurately represent this integer's value and sign.
Definition: FVInt.h:98
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition: FVInt.h:88
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
Definition: CalyxOps.cpp:55
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21
void printFVInt(AsmPrinter &p, const FVInt &value)
Print a four-valued integer usign an AsmPrinter.
Definition: FVInt.cpp:147
ParseResult parseFVInt(AsmParser &p, FVInt &result)
Parse a four-valued integer using an AsmParser.
Definition: FVInt.cpp:162