CIRCT  18.0.0git
CustomDirectiveImpl.cpp
Go to the documentation of this file.
1 //===- CustomDirectiveImpl.cpp - Custom TableGen directives ---------------===//
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 
10 #include "llvm/ADT/SmallString.h"
11 
12 using namespace circt;
13 
14 ParseResult circt::parseImplicitSSAName(OpAsmParser &parser, StringAttr &attr) {
15  // Use the explicit name if one is provided as `name "xyz"`.
16  if (!parser.parseOptionalKeyword("name")) {
17  std::string str;
18  if (parser.parseString(&str))
19  return failure();
20  attr = parser.getBuilder().getStringAttr(str);
21  return success();
22  }
23 
24  // Infer the name from the SSA name of the operation's first result.
25  auto resultName = parser.getResultName(0).first;
26  if (!resultName.empty() && isdigit(resultName[0]))
27  resultName = "";
28  attr = parser.getBuilder().getStringAttr(resultName);
29  return success();
30 }
31 
32 ParseResult circt::parseImplicitSSAName(OpAsmParser &parser,
33  NamedAttrList &attrs) {
34  if (parser.parseOptionalAttrDict(attrs))
35  return failure();
36  inferImplicitSSAName(parser, attrs);
37  return success();
38 }
39 
40 bool circt::inferImplicitSSAName(OpAsmParser &parser, NamedAttrList &attrs) {
41  // Don't do anything if a `name` attribute is explicitly provided.
42  if (attrs.get("name"))
43  return false;
44 
45  // Infer the name from the SSA name of the operation's first result.
46  auto resultName = parser.getResultName(0).first;
47  if (!resultName.empty() && isdigit(resultName[0]))
48  resultName = "";
49  auto nameAttr = parser.getBuilder().getStringAttr(resultName);
50  auto *context = parser.getBuilder().getContext();
51  attrs.push_back({StringAttr::get(context, "name"), nameAttr});
52  return true;
53 }
54 
55 void circt::printImplicitSSAName(OpAsmPrinter &printer, Operation *op,
56  StringAttr attr) {
57  SmallString<32> resultNameStr;
58  llvm::raw_svector_ostream tmpStream(resultNameStr);
59  printer.printOperand(op->getResult(0), tmpStream);
60  auto actualName = tmpStream.str().drop_front();
61  auto expectedName = attr.getValue();
62  // Anonymous names are printed as digits, which is fine.
63  if (actualName == expectedName ||
64  (expectedName.empty() && isdigit(actualName[0])))
65  return;
66 
67  printer << " name " << attr;
68 }
69 
70 void circt::printImplicitSSAName(OpAsmPrinter &printer, Operation *op,
71  DictionaryAttr attrs,
72  ArrayRef<StringRef> extraElides) {
73  SmallVector<StringRef, 2> elides(extraElides.begin(), extraElides.end());
74  elideImplicitSSAName(printer, op, attrs, elides);
75  printer.printOptionalAttrDict(attrs.getValue(), elides);
76 }
77 
78 void circt::elideImplicitSSAName(OpAsmPrinter &printer, Operation *op,
79  DictionaryAttr attrs,
80  SmallVectorImpl<StringRef> &elides) {
81  SmallString<32> resultNameStr;
82  llvm::raw_svector_ostream tmpStream(resultNameStr);
83  printer.printOperand(op->getResult(0), tmpStream);
84  auto actualName = tmpStream.str().drop_front();
85  auto expectedName = attrs.getAs<StringAttr>("name").getValue();
86  // Anonymous names are printed as digits, which is fine.
87  if (actualName == expectedName ||
88  (expectedName.empty() && isdigit(actualName[0])))
89  elides.push_back("name");
90 }
91 
92 ParseResult circt::parseOptionalBinaryOpTypes(OpAsmParser &parser, Type &lhs,
93  Type &rhs) {
94  if (parser.parseType(lhs))
95  return failure();
96 
97  // Parse an optional rhs type.
98  if (parser.parseOptionalComma()) {
99  rhs = lhs;
100  } else {
101  if (parser.parseType(rhs))
102  return failure();
103  }
104  return success();
105 }
106 
107 void circt::printOptionalBinaryOpTypes(OpAsmPrinter &p, Operation *op, Type lhs,
108  Type rhs) {
109  p << lhs;
110  // If operand types are not same, print a rhs type.
111  if (lhs != rhs)
112  p << ", " << rhs;
113 }
114 
115 ParseResult circt::parseKeywordBool(OpAsmParser &parser, BoolAttr &attr,
116  StringRef trueKeyword,
117  StringRef falseKeyword) {
118  if (succeeded(parser.parseOptionalKeyword(trueKeyword))) {
119  attr = BoolAttr::get(parser.getContext(), true);
120  } else if (succeeded(parser.parseOptionalKeyword(falseKeyword))) {
121  attr = BoolAttr::get(parser.getContext(), false);
122  } else {
123  return parser.emitError(parser.getCurrentLocation())
124  << "expected keyword \"" << trueKeyword << "\" or \"" << falseKeyword
125  << "\"";
126  }
127  return success();
128 }
129 
130 void circt::printKeywordBool(OpAsmPrinter &printer, Operation *op,
131  BoolAttr attr, StringRef trueKeyword,
132  StringRef falseKeyword) {
133  if (attr.getValue())
134  printer << trueKeyword;
135  else
136  printer << falseKeyword;
137 }
#define isdigit(x)
Definition: FIRLexer.cpp:26
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
Definition: CalyxOps.cpp:53
This file defines an intermediate representation for circuits acting as an abstraction for constraint...
ParseResult parseKeywordBool(OpAsmParser &parser, BoolAttr &attr, StringRef trueKeyword, StringRef falseKeyword)
Parse a boolean as one of two keywords.
void printOptionalBinaryOpTypes(OpAsmPrinter &p, Operation *op, Type lhs, Type rhs)
Print/parse binary operands type only when types are different.
void elideImplicitSSAName(OpAsmPrinter &printer, Operation *op, DictionaryAttr attrs, SmallVectorImpl< StringRef > &elides)
Check if the name attribute in attrs matches the SSA name of the operation's first result.
ParseResult parseImplicitSSAName(OpAsmParser &parser, StringAttr &attr)
Parse an implicit SSA name string attribute.
void printKeywordBool(OpAsmPrinter &printer, Operation *op, BoolAttr attr, StringRef trueKeyword, StringRef falseKeyword)
Print a boolean as one of two keywords.
bool inferImplicitSSAName(OpAsmParser &parser, NamedAttrList &attrs)
Ensure that attrs contains a name attribute by inferring its value from the SSA name of the operation...
void printImplicitSSAName(OpAsmPrinter &p, Operation *op, StringAttr attr)
Print an implicit SSA name string attribute.
ParseResult parseOptionalBinaryOpTypes(OpAsmParser &parser, Type &lhs, Type &rhs)