CIRCT  20.0.0git
EmissionPrinter.h
Go to the documentation of this file.
1 //===- EmissionPrinter.h - Provides printing utilites to ExportSystemC ----===//
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 //
9 // This declares the EmissionPrinter class to provide printing utilities to
10 // ExportSystemC.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 // NOLINTNEXTLINE(llvm-header-guard)
15 #ifndef CIRCT_TARGET_EXPORTSYSTEMC_EMISSIONPRINTER_H
16 #define CIRCT_TARGET_EXPORTSYSTEMC_EMISSIONPRINTER_H
17 
18 #include "EmissionPattern.h"
19 #include "mlir/Support/IndentedOstream.h"
20 
21 namespace circt {
22 namespace ExportSystemC {
23 // Forward declarations.
24 class InlineEmitter;
25 
26 /// This is intended to be the driving class for all pattern-based IR emission.
28 public:
29  EmissionPrinter(mlir::raw_indented_ostream &os,
33  Location loc)
36  currentLoc(loc) {}
37 
38  EmissionPrinter(mlir::raw_indented_ostream &os,
41  AttrEmissionPatternSet &attrPatterns, Location loc)
42  : opPatterns(std::move(opPatterns)),
43  typePatterns(std::move(typePatterns)),
44  attrPatterns(std::move(attrPatterns)), os(os), emissionFailed(false),
45  currentLoc(loc) {}
46 
47  /// Emit the given operation as a statement to the ostream associated with
48  /// this printer according to the emission patterns registered. An operation
49  /// might also emit multiple statements, or nothing in case it can only be
50  /// emitted as an expression. If multiple emission patterns match, the first
51  /// one in the first one in the pattern set is chosen. If no pattern matches,
52  /// a remark is left in the output and an error is added to stderr.
53  /// Additionally, the exit-code to be obtained by the 'exitCode()'
54  /// member-function is set to 'failure'.
55  void emitOp(Operation *op);
56 
57  /// Emit the expression represented by the given value to the ostream
58  /// associated with this printer according to the emission patterns
59  /// registered. This will emit exactly one expression and does not emit any
60  /// statements. If multiple emission patterns match, the first one in the
61  /// first one in the pattern set is chosen. If no pattern matches, a remark is
62  /// left in the output and an error is added to stderr. Additionally, the
63  /// exit-code to be obtained by the 'exitCode()' member-function is set to
64  /// 'failure'.
65  InlineEmitter getInlinable(Value value);
66 
67  /// Emit the given type to the ostream associated with this printer according
68  /// to the emission patterns registered. If multiple emission patterns match,
69  /// the first one in the pattern set is chosen. If no pattern matches, a
70  /// remark is left in the output and an error is added to stderr.
71  /// Additionally, the exit-code to be obtained by the 'exitCode()'
72  /// member-function is set to 'failure'.
73  void emitType(Type type);
74 
75  /// Emit the given attribute to the ostream associated with this printer
76  /// according to the emission patterns registered. If multiple emission
77  /// patterns match, the first one in the pattern set is chosen. If no pattern
78  /// matches, a remark is left in the output and an error is added to stderr.
79  /// Additionally, the exit-code to be obtained by the 'exitCode()'
80  /// member-function is set to 'failure'.
81  void emitAttr(Attribute attr);
82 
83  /// Emit the given region to the ostream associated with this printer. Only
84  /// regions with a single basic block are allowed. Prints the operations
85  /// inside according to 'emitOp()' indented one level deeper and encloses the
86  /// region in curly-braces.
87  void emitRegion(Region &region);
88 
89  /// Emit the given region to the ostream associated with this printer. Only
90  /// regions with a single basic block are allowed. Prints the operations
91  /// inside according to 'emitOp()'. The enclosing delimiters and level of
92  /// indentation is determined by the passed scope.
93  void emitRegion(Region &region,
94  mlir::raw_indented_ostream::DelimitedScope &scope);
95 
96  /// Emit an error on the operation and fail emission. Preferred for
97  /// operations, especially the ones that will be inlined, because it places
98  /// the error more precisely.
99  InFlightDiagnostic emitError(Operation *op, const Twine &message);
100  /// Emit an error at the current location of the printer (the newest operation
101  /// to be emitted as a statement). This is primarily intended for types and
102  /// attributes for which no location is available directly.
103  InFlightDiagnostic emitError(const Twine &message);
104 
105  EmissionPrinter &operator<<(StringRef str);
106  EmissionPrinter &operator<<(int64_t num);
107 
108  mlir::raw_indented_ostream &getOstream() const { return os; }
109 
110  /// Returns whether everything was printed successfully or some error occurred
111  /// (e.g., there was an operation or type for which no emission pattern was
112  /// valid).
113  LogicalResult exitState() const { return failure(emissionFailed); }
114 
115 private:
119  mlir::raw_indented_ostream &os;
121  Location currentLoc;
122 };
123 
124 /// This class is returned to a pattern that requested inlined emission of a
125 /// value. It allows the pattern to emit additional characters before the
126 /// requested expression depending on the precedence.
128 public:
129  InlineEmitter(std::function<void()> emitter, Precedence precedence,
131  : precedence(precedence), emitter(std::move(emitter)), printer(printer) {}
132 
133  Precedence getPrecedence() const { return precedence; }
134  void emit() const { emitter(); }
135  void emitWithParensOnLowerPrecedence(Precedence prec, StringRef lParen = "(",
136  StringRef rParen = ")") const;
137 
138 private:
140  std::function<void()> emitter;
142 };
143 
144 } // namespace ExportSystemC
145 } // namespace circt
146 
147 #endif // CIRCT_TARGET_EXPORTSYSTEMC_EMISSIONPRINTER_H
This class collects a set of emission patterns with base type 'PatternTy'.
This is intended to be the driving class for all pattern-based IR emission.
EmissionPrinter(mlir::raw_indented_ostream &os, OpEmissionPatternSet &opPatterns, TypeEmissionPatternSet &typePatterns, AttrEmissionPatternSet &attrPatterns, Location loc)
void emitRegion(Region &region)
Emit the given region to the ostream associated with this printer.
void emitOp(Operation *op)
Emit the given operation as a statement to the ostream associated with this printer according to the ...
EmissionPrinter(mlir::raw_indented_ostream &os, const FrozenOpEmissionPatternSet &opPatterns, const FrozenTypeEmissionPatternSet &typePatterns, const FrozenAttrEmissionPatternSet &attrPatterns, Location loc)
FrozenAttrEmissionPatternSet attrPatterns
InlineEmitter getInlinable(Value value)
Emit the expression represented by the given value to the ostream associated with this printer accord...
void emitAttr(Attribute attr)
Emit the given attribute to the ostream associated with this printer according to the emission patter...
LogicalResult exitState() const
Returns whether everything was printed successfully or some error occurred (e.g., there was an operat...
EmissionPrinter & operator<<(StringRef str)
FrozenTypeEmissionPatternSet typePatterns
void emitType(Type type)
Emit the given type to the ostream associated with this printer according to the emission patterns re...
mlir::raw_indented_ostream & getOstream() const
InFlightDiagnostic emitError(Operation *op, const Twine &message)
Emit an error on the operation and fail emission.
mlir::raw_indented_ostream & os
FrozenOpEmissionPatternSet opPatterns
This class is returned to a pattern that requested inlined emission of a value.
InlineEmitter(std::function< void()> emitter, Precedence precedence, EmissionPrinter &printer)
void emitWithParensOnLowerPrecedence(Precedence prec, StringRef lParen="(", StringRef rParen=")") const
Precedence
This enum encodes the precedence of C++ expressions.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21