CIRCT  19.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  std::get<1>(input).cast<StringAttr>().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(
84  llvm::zip(resultTypes, resultNames), p,
85  [&](std::tuple<Type, Attribute> result) {
86  p.printKeywordOrString(
87  std::get<1>(result).cast<StringAttr>().getValue());
88  p << ": " << std::get<0>(result);
89  });
90  p << ")";
91 }
92 
93 ParseResult circt::parseOptionalParameterList(OpAsmParser &parser,
94  ArrayAttr &parameters) {
95  SmallVector<Attribute> params;
96 
97  auto parseParameter = [&]() {
98  std::string name;
99  Type type;
100  Attribute value;
101 
102  if (parser.parseKeywordOrString(&name) || parser.parseColonType(type))
103  return failure();
104 
105  // Parse the default value if present.
106  if (succeeded(parser.parseOptionalEqual())) {
107  if (parser.parseAttribute(value, type))
108  return failure();
109  }
110 
111  auto &builder = parser.getBuilder();
112  params.push_back(ParamDeclAttr::get(
113  builder.getContext(), builder.getStringAttr(name), type, value));
114  return success();
115  };
116 
117  if (failed(parser.parseCommaSeparatedList(
118  OpAsmParser::Delimiter::OptionalLessGreater, parseParameter)))
119  return failure();
120 
121  parameters = ArrayAttr::get(parser.getContext(), params);
122 
123  return success();
124 }
125 
126 void circt::printOptionalParameterList(OpAsmPrinter &p, Operation *op,
127  ArrayAttr parameters) {
128  if (parameters.empty())
129  return;
130 
131  p << '<';
132  llvm::interleaveComma(parameters, p, [&](Attribute param) {
133  auto paramAttr = param.cast<ParamDeclAttr>();
134  p << paramAttr.getName().getValue() << ": " << paramAttr.getType();
135  if (auto value = paramAttr.getValue()) {
136  p << " = ";
137  p.printAttributeWithoutType(value);
138  }
139  });
140  p << '>';
141 }
static ParseResult parseInputPort(OpAsmParser &parser, module_like_impl::PortParse &result)
llvm::SmallVector< StringAttr > inputs
Builder builder
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 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.