CIRCT  20.0.0git
CustomDirectiveImpl.cpp
Go to the documentation of this file.
1 //===- CustomDirectiveImpl.cpp - Custom directive definitions -------------===//
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 
11 
12 using namespace circt;
13 using namespace circt::hw;
14 
16  OpAsmParser &parser,
17  SmallVectorImpl<OpAsmParser::UnresolvedOperand> &inputs,
18  SmallVectorImpl<Type> &inputTypes, ArrayAttr &inputNames) {
19 
20  SmallVector<Attribute> argNames;
21  auto parseInputPort = [&]() -> ParseResult {
22  std::string portName;
23  if (parser.parseKeywordOrString(&portName))
24  return failure();
25  argNames.push_back(StringAttr::get(parser.getContext(), portName));
26  inputs.push_back({});
27  inputTypes.push_back({});
28  return failure(parser.parseColon() || parser.parseOperand(inputs.back()) ||
29  parser.parseColon() || parser.parseType(inputTypes.back()));
30  };
31  llvm::SMLoc inputsOperandsLoc;
32  if (parser.getCurrentLocation(&inputsOperandsLoc) ||
33  parser.parseCommaSeparatedList(OpAsmParser::Delimiter::Paren,
35  return failure();
36 
37  inputNames = ArrayAttr::get(parser.getContext(), argNames);
38 
39  return success();
40 }
41 
42 void circt::printInputPortList(OpAsmPrinter &p, Operation *op,
43  OperandRange inputs, TypeRange inputTypes,
44  ArrayAttr inputNames) {
45  p << "(";
46  llvm::interleaveComma(llvm::zip(inputs, inputNames), p,
47  [&](std::tuple<Value, Attribute> input) {
48  Value val = std::get<0>(input);
49  p.printKeywordOrString(
50  cast<StringAttr>(std::get<1>(input)).getValue());
51  p << ": " << val << ": " << val.getType();
52  });
53  p << ")";
54 }
55 
56 ParseResult circt::parseOutputPortList(OpAsmParser &parser,
57  SmallVectorImpl<Type> &resultTypes,
58  ArrayAttr &resultNames) {
59 
60  SmallVector<Attribute> names;
61  auto parseResultPort = [&]() -> ParseResult {
62  std::string portName;
63  if (parser.parseKeywordOrString(&portName))
64  return failure();
65  names.push_back(StringAttr::get(parser.getContext(), portName));
66  resultTypes.push_back({});
67  return parser.parseColonType(resultTypes.back());
68  };
69  llvm::SMLoc inputsOperandsLoc;
70  if (parser.getCurrentLocation(&inputsOperandsLoc) ||
71  parser.parseCommaSeparatedList(OpAsmParser::Delimiter::Paren,
72  parseResultPort))
73  return failure();
74 
75  resultNames = ArrayAttr::get(parser.getContext(), names);
76 
77  return success();
78 }
79 
80 void circt::printOutputPortList(OpAsmPrinter &p, Operation *op,
81  TypeRange resultTypes, ArrayAttr resultNames) {
82  p << "(";
83  llvm::interleaveComma(llvm::zip(resultTypes, resultNames), p,
84  [&](std::tuple<Type, Attribute> result) {
85  p.printKeywordOrString(
86  cast<StringAttr>(std::get<1>(result)).getValue());
87  p << ": " << std::get<0>(result);
88  });
89  p << ")";
90 }
91 
92 ParseResult circt::parseOptionalParameterList(OpAsmParser &parser,
93  ArrayAttr &parameters) {
94  SmallVector<Attribute> params;
95 
96  auto parseParameter = [&]() {
97  std::string name;
98  Type type;
99  Attribute value;
100 
101  if (parser.parseKeywordOrString(&name) || parser.parseColonType(type))
102  return failure();
103 
104  // Parse the default value if present.
105  if (succeeded(parser.parseOptionalEqual())) {
106  if (parser.parseAttribute(value, type))
107  return failure();
108  }
109 
110  auto &builder = parser.getBuilder();
111  params.push_back(ParamDeclAttr::get(
112  builder.getContext(), builder.getStringAttr(name), type, value));
113  return success();
114  };
115 
116  if (failed(parser.parseCommaSeparatedList(
117  OpAsmParser::Delimiter::OptionalLessGreater, parseParameter)))
118  return failure();
119 
120  parameters = ArrayAttr::get(parser.getContext(), params);
121 
122  return success();
123 }
124 
125 void circt::printOptionalParameterList(OpAsmPrinter &p, Operation *op,
126  ArrayAttr parameters) {
127  if (parameters.empty())
128  return;
129 
130  p << '<';
131  llvm::interleaveComma(parameters, p, [&](Attribute param) {
132  auto paramAttr = cast<ParamDeclAttr>(param);
133  p << paramAttr.getName().getValue() << ": " << paramAttr.getType();
134  if (auto value = paramAttr.getValue()) {
135  p << " = ";
136  p.printAttributeWithoutType(value);
137  }
138  });
139  p << '>';
140 }
static ParseResult parseInputPort(OpAsmParser &parser, module_like_impl::PortParse &result)
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
ParseResult parseInputPortList(OpAsmParser &parser, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &inputs, SmallVectorImpl< Type > &inputTypes, ArrayAttr &inputNames)
Parse a list of instance input ports.
void printOutputPortList(OpAsmPrinter &p, Operation *op, TypeRange resultTypes, ArrayAttr resultNames)
Print a list of instance output ports.
ParseResult parseOptionalParameterList(OpAsmParser &parser, ArrayAttr &parameters)
Parse an parameter list if present.
void printOptionalParameterList(OpAsmPrinter &p, Operation *op, ArrayAttr parameters)
Print a parameter list for a module or instance.
void printInputPortList(OpAsmPrinter &p, Operation *op, OperandRange inputs, TypeRange inputTypes, ArrayAttr inputNames)
Print a list of instance input ports.
ParseResult parseOutputPortList(OpAsmParser &parser, SmallVectorImpl< Type > &resultTypes, ArrayAttr &resultNames)
Parse a list of instance output ports.