CIRCT  19.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  // Optional names are printed as digits.
62  if (!attr)
63  return;
64  auto expectedName = attr.getValue();
65  // Anonymous names are printed as digits, which is fine.
66  if (actualName == expectedName ||
67  (expectedName.empty() && isdigit(actualName[0])))
68  return;
69 
70  printer << " name " << attr;
71 }
72 
73 void circt::printImplicitSSAName(OpAsmPrinter &printer, Operation *op,
74  DictionaryAttr attrs,
75  ArrayRef<StringRef> extraElides) {
76  SmallVector<StringRef, 2> elides(extraElides.begin(), extraElides.end());
77  elideImplicitSSAName(printer, op, attrs, elides);
78  printer.printOptionalAttrDict(attrs.getValue(), elides);
79 }
80 
81 void circt::elideImplicitSSAName(OpAsmPrinter &printer, Operation *op,
82  DictionaryAttr attrs,
83  SmallVectorImpl<StringRef> &elides) {
84  SmallString<32> resultNameStr;
85  llvm::raw_svector_ostream tmpStream(resultNameStr);
86  printer.printOperand(op->getResult(0), tmpStream);
87  auto actualName = tmpStream.str().drop_front();
88  auto expectedName = attrs.getAs<StringAttr>("name").getValue();
89  // Anonymous names are printed as digits, which is fine.
90  if (actualName == expectedName ||
91  (expectedName.empty() && isdigit(actualName[0])))
92  elides.push_back("name");
93 }
94 
95 ParseResult circt::parseOptionalBinaryOpTypes(OpAsmParser &parser, Type &lhs,
96  Type &rhs) {
97  if (parser.parseType(lhs))
98  return failure();
99 
100  // Parse an optional rhs type.
101  if (parser.parseOptionalComma()) {
102  rhs = lhs;
103  } else {
104  if (parser.parseType(rhs))
105  return failure();
106  }
107  return success();
108 }
109 
110 void circt::printOptionalBinaryOpTypes(OpAsmPrinter &p, Operation *op, Type lhs,
111  Type rhs) {
112  p << lhs;
113  // If operand types are not same, print a rhs type.
114  if (lhs != rhs)
115  p << ", " << rhs;
116 }
117 
118 ParseResult circt::parseKeywordBool(OpAsmParser &parser, BoolAttr &attr,
119  StringRef trueKeyword,
120  StringRef falseKeyword) {
121  if (succeeded(parser.parseOptionalKeyword(trueKeyword))) {
122  attr = BoolAttr::get(parser.getContext(), true);
123  } else if (succeeded(parser.parseOptionalKeyword(falseKeyword))) {
124  attr = BoolAttr::get(parser.getContext(), false);
125  } else {
126  return parser.emitError(parser.getCurrentLocation())
127  << "expected keyword \"" << trueKeyword << "\" or \"" << falseKeyword
128  << "\"";
129  }
130  return success();
131 }
132 
133 void circt::printKeywordBool(OpAsmPrinter &printer, Operation *op,
134  BoolAttr attr, StringRef trueKeyword,
135  StringRef falseKeyword) {
136  if (attr.getValue())
137  printer << trueKeyword;
138  else
139  printer << falseKeyword;
140 }
#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...
Definition: DebugAnalysis.h:21
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)