forked from llvm/circt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SV Dialect] Implement Visitor and EmitVerilog support for SV.
- Loading branch information
Showing
5 changed files
with
248 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
//===- SV/Visitors.h - SV Dialect Visitors ----------------------*- C++ -*-===// | ||
// | ||
// This file defines visitors that make it easier to work with SV IR. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef CIRCT_DIALECT_SV_VISITORS_H | ||
#define CIRCT_DIALECT_SV_VISITORS_H | ||
|
||
#include "circt/Dialect/SV/Ops.h" | ||
#include "llvm/ADT/TypeSwitch.h" | ||
|
||
namespace circt { | ||
namespace sv { | ||
|
||
template <typename ConcreteType, typename ResultType = void, | ||
typename... ExtraArgs> | ||
class Visitor { | ||
public: | ||
ResultType dispatchSVVisitor(Operation *op, ExtraArgs... args) { | ||
auto *thisCast = static_cast<ConcreteType *>(this); | ||
return TypeSwitch<Operation *, ResultType>(op) | ||
.template Case<TextualValueOp, | ||
// Control flow. | ||
IfDefOp, IfOp, AlwaysAtPosEdgeOp, | ||
// Other Statements. | ||
YieldOp, FWriteOp>([&](auto expr) -> ResultType { | ||
return thisCast->visitSV(expr, args...); | ||
}) | ||
.Default([&](auto expr) -> ResultType { | ||
return thisCast->visitInvalidSV(op, args...); | ||
}); | ||
} | ||
|
||
/// This callback is invoked on any invalid operations. | ||
ResultType visitInvalidSV(Operation *op, ExtraArgs... args) { | ||
op->emitOpError("unknown SV node"); | ||
abort(); | ||
} | ||
|
||
/// This callback is invoked on any SV operations that are not handled by the | ||
/// concrete visitor. | ||
ResultType visitUnhandledSV(Operation *op, ExtraArgs... args) { | ||
return ResultType(); | ||
} | ||
|
||
#define HANDLE(OPTYPE, OPKIND) \ | ||
ResultType visitSV(OPTYPE op, ExtraArgs... args) { \ | ||
return static_cast<ConcreteType *>(this)->visit##OPKIND##SV(op, args...); \ | ||
} | ||
|
||
// Expressions | ||
HANDLE(TextualValueOp, Unhandled) | ||
|
||
// Control flow. | ||
HANDLE(IfDefOp, Unhandled); | ||
HANDLE(IfOp, Unhandled); | ||
HANDLE(AlwaysAtPosEdgeOp, Unhandled); | ||
|
||
// Other Statements. | ||
HANDLE(YieldOp, Unhandled); | ||
HANDLE(FWriteOp, Unhandled); | ||
#undef HANDLE | ||
}; | ||
|
||
} // namespace sv | ||
} // namespace circt | ||
|
||
#endif // CIRCT_DIALECT_SV_VISITORS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,3 +76,5 @@ firrtl.circuit "Circuit" { | |
// CHECK-NEXT:endmodule | ||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// RUN: circt-translate %s -emit-verilog -verify-diagnostics | FileCheck %s --strict-whitespace | ||
|
||
firrtl.circuit "Circuit" { | ||
// CHECK-LABEL: module M1( | ||
firrtl.module @M1(%clock : i1, %cond : i1) { | ||
|
||
sv.alwaysat_posedge %clock { | ||
sv.ifdef "!SYNTHESIS" { | ||
%tmp = sv.textual_value "PRINTF_COND_" : i1 | ||
%tmp2 = rtl.and %tmp, %cond : i1 | ||
sv.if %tmp2 { | ||
sv.fwrite "Hi\n" | ||
} | ||
} | ||
} | ||
|
||
// CHECK: always @(posedge clock) begin | ||
// CHECK-NEXT: #ifndef SYNTHESIS | ||
// CHECK-NEXT: if (PRINTF_COND_ & cond) | ||
// CHECK-NEXT: $fwrite(32'h80000002, "Hi\n"); | ||
// CHECK-NEXT: #endif | ||
// CHECK-NEXT: end // always @(posedge) | ||
sv.if %cond { | ||
sv.fwrite "Hi\n" | ||
sv.fwrite "Bye\n" | ||
} | ||
|
||
// CHECK-NEXT: if (cond) begin | ||
// CHECK-NEXT: $fwrite(32'h80000002, "Hi\n"); | ||
// CHECK-NEXT: $fwrite(32'h80000002, "Bye\n"); | ||
// CHECK-NEXT: end | ||
} | ||
} | ||
|
||
|