19 #include "mlir/Dialect/EmitC/IR/EmitC.h"
20 #include "mlir/IR/BuiltinOps.h"
21 #include "mlir/Support/FileUtilities.h"
22 #include "mlir/Tools/mlir-translate/Translation.h"
23 #include "llvm/Support/FileSystem.h"
24 #include "llvm/Support/Path.h"
25 #include "llvm/Support/ToolOutputFile.h"
28 using namespace circt;
31 #define DEBUG_TYPE "export-systemc"
38 auto str = std::regex_replace(path.upper(), std::regex(
"[\\\\./]"),
"_");
41 return std::regex_replace(str, std::regex(
"[^a-zA-Z0-9_$]+"),
"");
46 static LogicalResult
emitFile(ArrayRef<Operation *> operations,
47 StringRef filePath, raw_ostream &os) {
48 mlir::raw_indented_ostream ios(os);
50 ios <<
"// " << filePath <<
"\n";
52 ios <<
"#ifndef " << macroname <<
"\n";
53 ios <<
"#define " << macroname <<
"\n\n";
57 if (!operations.empty()) {
65 operations[0]->getLoc());
67 for (
auto *op : operations)
73 ios <<
"\n#endif // " << macroname <<
"\n\n";
75 return failure(failed);
83 llvm::raw_ostream &os) {
84 return emitFile({module},
"stdout.h", os);
88 StringRef directory) {
90 SmallVector<Operation *> includes;
91 module->walk([&](mlir::emitc::IncludeOp op) { includes.push_back(op); });
93 for (Operation &op : module.getRegion().front()) {
94 if (
auto symbolOp = dyn_cast<mlir::SymbolOpInterface>(op)) {
96 if (std::error_code error = llvm::sys::fs::create_directories(directory))
97 return module.emitError(
"cannot create output directory \"")
98 << directory <<
"\": " << error.message();
101 std::string fileName = symbolOp.getName().str() +
".h";
102 SmallString<128> filePath(directory);
104 std::string errorMessage;
105 auto output = mlir::openOutputFile(filePath, &errorMessage);
107 return module.emitError(errorMessage);
110 SmallVector<Operation *> opsInThisFile(includes);
111 opsInThisFile.push_back(symbolOp);
112 if (failed(
emitFile(opsInThisFile, filePath, output->os())))
113 return symbolOp->emitError(
"failed to emit to file \"")
131 "export-dir", llvm::cl::desc(
"Directory path to write the files to."),
132 llvm::cl::init(
"./"));
134 static mlir::TranslateFromMLIRRegistration toSystemC(
135 "export-systemc",
"export SystemC",
136 [](ModuleOp module, raw_ostream &output) {
139 [](mlir::DialectRegistry ®istry) {
140 registry.insert<hw::HWDialect, comb::CombDialect,
141 systemc::SystemCDialect, mlir::emitc::EmitCDialect>();
144 static mlir::TranslateFromMLIRRegistration toSplitSystemC(
145 "export-split-systemc",
"export SystemC (split)",
146 [](ModuleOp module, raw_ostream &output) {
149 [](mlir::DialectRegistry ®istry) {
150 registry.insert<hw::HWDialect, comb::CombDialect,
151 systemc::SystemCDialect, mlir::emitc::EmitCDialect>();
static std::string pathToMacroName(StringRef path)
Helper to convert a file-path to a macro name that can be used to guard a header file.
static LogicalResult emitFile(ArrayRef< Operation * > operations, StringRef filePath, raw_ostream &os)
Emits the given operation to a file represented by the passed ostream and file-path.
static StringAttr append(StringAttr base, const Twine &suffix)
Return a attribute with the specified suffix appended.
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.
void emitOp(Operation *op)
Emit the given operation as a statement to the ostream associated with this printer according to the ...
LogicalResult exitState() const
Returns whether everything was printed successfully or some error occurred (e.g., there was an operat...
void registerExportSystemCTranslation()
LogicalResult exportSplitSystemC(ModuleOp module, StringRef directory)
void registerAllTypeEmitters(TypeEmissionPatternSet &patterns)
Collects the type emission patterns of all supported dialects.
LogicalResult exportSystemC(ModuleOp module, llvm::raw_ostream &os)
void registerAllOpEmitters(OpEmissionPatternSet &patterns, MLIRContext *context)
Collects the operation emission patterns of all supported dialects.
void registerAllAttrEmitters(AttrEmissionPatternSet &patterns)
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.