CIRCT  20.0.0git
DebugOps.cpp
Go to the documentation of this file.
1 //===- DebugOps.cpp - Debug dialect operations ----------------------------===//
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 "mlir/IR/OpImplementation.h"
11 
12 using namespace circt;
13 using namespace debug;
14 using namespace mlir;
15 
16 //===----------------------------------------------------------------------===//
17 // StructOp
18 //===----------------------------------------------------------------------===//
19 
20 ParseResult StructOp::parse(OpAsmParser &parser, OperationState &result) {
21  // Parse the struct fields.
22  SmallVector<Attribute> names;
23  SmallVector<OpAsmParser::UnresolvedOperand, 16> operands;
24  std::string nameBuffer;
25  auto parseField = [&]() {
26  nameBuffer.clear();
27  if (parser.parseString(&nameBuffer) || parser.parseColon() ||
28  parser.parseOperand(operands.emplace_back()))
29  return failure();
30  names.push_back(StringAttr::get(parser.getContext(), nameBuffer));
31  return success();
32  };
33  if (parser.parseCommaSeparatedList(AsmParser::Delimiter::Braces, parseField))
34  return failure();
35 
36  // Parse the attribute dictionary.
37  if (parser.parseOptionalAttrDict(result.attributes))
38  return failure();
39 
40  // Parse the field types, if there are any fields.
41  SmallVector<Type> types;
42  if (!operands.empty()) {
43  if (parser.parseColon())
44  return failure();
45  auto typesLoc = parser.getCurrentLocation();
46  if (parser.parseTypeList(types))
47  return failure();
48  if (types.size() != operands.size())
49  return parser.emitError(typesLoc,
50  "number of fields and types must match");
51  }
52 
53  // Resolve the operands.
54  for (auto [operand, type] : llvm::zip(operands, types))
55  if (parser.resolveOperand(operand, type, result.operands))
56  return failure();
57 
58  // Finalize the op.
59  result.addAttribute("names", ArrayAttr::get(parser.getContext(), names));
60  result.addTypes(StructType::get(parser.getContext()));
61  return success();
62 }
63 
64 void StructOp::print(OpAsmPrinter &printer) {
65  printer << " {";
66  llvm::interleaveComma(llvm::zip(getFields(), getNames()), printer.getStream(),
67  [&](auto pair) {
68  auto [field, name] = pair;
69  printer.printAttribute(name);
70  printer << ": ";
71  printer.printOperand(field);
72  });
73  printer << '}';
74  printer.printOptionalAttrDict(getOperation()->getAttrs(), {"names"});
75  if (!getFields().empty()) {
76  printer << " : ";
77  printer << getFields().getTypes();
78  }
79 }
80 
81 //===----------------------------------------------------------------------===//
82 // ArrayOp
83 //===----------------------------------------------------------------------===//
84 
85 ParseResult ArrayOp::parse(OpAsmParser &parser, OperationState &result) {
86  // Parse the elements, attributes and types.
87  SmallVector<OpAsmParser::UnresolvedOperand, 16> operands;
88  if (parser.parseOperandList(operands, AsmParser::Delimiter::Square) ||
89  parser.parseOptionalAttrDict(result.attributes))
90  return failure();
91 
92  // Resolve the operands.
93  if (!operands.empty()) {
94  Type type;
95  if (parser.parseColon() || parser.parseType(type))
96  return failure();
97  for (auto operand : operands)
98  if (parser.resolveOperand(operand, type, result.operands))
99  return failure();
100  }
101 
102  // Finalize the op.
103  result.addTypes(ArrayType::get(parser.getContext()));
104  return success();
105 }
106 
107 void ArrayOp::print(OpAsmPrinter &printer) {
108  printer << " [";
109  printer << getElements();
110  printer << ']';
111  printer.printOptionalAttrDict(getOperation()->getAttrs());
112  if (!getElements().empty()) {
113  printer << " : ";
114  printer << getElements()[0].getType();
115  }
116 }
117 
118 // Operation implementations generated from `Debug.td`
119 #define GET_OP_CLASSES
120 #include "circt/Dialect/Debug/Debug.cpp.inc"
121 
122 void DebugDialect::registerOps() {
123  addOperations<
124 #define GET_OP_LIST
125 #include "circt/Dialect/Debug/Debug.cpp.inc"
126  >();
127 }
static InstancePath empty
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
Definition: debug.py:1