Skip to content

Commit 030d2e5

Browse files
authored
[SV] Add preprocessor include op (#8329)
1 parent 9570cdc commit 030d2e5

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

include/circt/Dialect/SV/SVStatements.td

+16
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,22 @@ def MacroDefOp : SVOp<"macro.def",
913913
}];
914914
}
915915

916+
def IncludeStyle
917+
: I32EnumAttr<
918+
"IncludeStyle",
919+
"Double-quoted local include, or angle bracketed system include",
920+
[I32EnumAttrCase<"Local", 0, "local">,
921+
I32EnumAttrCase<"System", 1, "system">]> {
922+
let genSpecializedAttr = 0;
923+
}
924+
925+
def IncludeStyleAttr : EnumAttr<SVDialect, IncludeStyle, "include_style">;
926+
927+
def IncludeOp : SVOp<"include"> {
928+
let summary = "preprocessor `include directive";
929+
let arguments = (ins IncludeStyleAttr:$style, StrAttr:$target);
930+
let assemblyFormat = [{ $style $target attr-dict }];
931+
}
916932

917933
//===----------------------------------------------------------------------===//
918934
// Function/Call

include/circt/Dialect/SV/SVVisitors.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Visitor {
4040
// Other Statements.
4141
AssignOp, BPAssignOp, PAssignOp, ForceOp, ReleaseOp, AliasOp,
4242
FWriteOp, SystemFunctionOp, VerbatimOp, MacroRefOp, FuncCallOp,
43-
FuncCallProceduralOp, ReturnOp,
43+
FuncCallProceduralOp, ReturnOp, IncludeOp,
4444
// Type declarations.
4545
InterfaceOp, InterfaceSignalOp, InterfaceModportOp,
4646
InterfaceInstanceOp, GetModportOp, AssignInterfaceSignalOp,
@@ -137,6 +137,7 @@ class Visitor {
137137
HANDLE(ReturnOp, Unhandled);
138138
HANDLE(VerbatimOp, Unhandled);
139139
HANDLE(MacroRefOp, Unhandled);
140+
HANDLE(IncludeOp, Unhandled);
140141

141142
// Type declarations.
142143
HANDLE(InterfaceOp, Unhandled);

lib/Conversion/ExportVerilog/ExportVerilog.cpp

+17-1
Original file line numberDiff line numberDiff line change
@@ -4062,6 +4062,7 @@ class StmtEmitter : public EmitterBase,
40624062
LogicalResult visitSV(FuncCallProceduralOp op);
40634063
LogicalResult visitSV(FuncCallOp op);
40644064
LogicalResult visitSV(ReturnOp op);
4065+
LogicalResult visitSV(IncludeOp op);
40654066

40664067
public:
40674068
ModuleEmitter &emitter;
@@ -4451,6 +4452,20 @@ LogicalResult StmtEmitter::visitSV(ReturnOp op) {
44514452
return emitOutputLikeOp(op, ports);
44524453
}
44534454

4455+
LogicalResult StmtEmitter::visitSV(IncludeOp op) {
4456+
startStatement();
4457+
ps << "`include" << PP::nbsp;
4458+
4459+
if (op.getStyle() == IncludeStyle::System)
4460+
ps << "<" << op.getTarget() << ">";
4461+
else
4462+
ps << "\"" << op.getTarget() << "\"";
4463+
4464+
emitLocationInfo(op.getLoc());
4465+
setPendingNewline();
4466+
return success();
4467+
}
4468+
44544469
LogicalResult StmtEmitter::visitSV(FuncDPIImportOp importOp) {
44554470
startStatement();
44564471

@@ -6713,7 +6728,7 @@ void SharedEmitterState::gatherFiles(bool separateModules) {
67136728
collectPorts(op);
67146729
// External modules are _not_ emitted.
67156730
})
6716-
.Case<VerbatimOp, IfDefOp, MacroDefOp, FuncDPIImportOp>(
6731+
.Case<VerbatimOp, IfDefOp, MacroDefOp, IncludeOp, FuncDPIImportOp>(
67176732
[&](Operation *op) {
67186733
// Emit into a separate file using the specified file name or
67196734
// replicate the operation in each outputfile.
@@ -6857,6 +6872,7 @@ static void emitOperation(VerilogEmitterState &state, Operation *op) {
68576872
.Case<MacroDefOp, FuncDPIImportOp>(
68586873
[&](auto op) { ModuleEmitter(state).emitStatement(op); })
68596874
.Case<FuncOp>([&](auto op) { ModuleEmitter(state).emitFunc(op); })
6875+
.Case<IncludeOp>([&](auto op) { ModuleEmitter(state).emitStatement(op); })
68606876
.Default([&](auto *op) {
68616877
state.encounteredError = true;
68626878
op->emitError("unknown operation (ExportVerilog::emitOperation)");

test/Conversion/ExportVerilog/verilog-basic.mlir

+6
Original file line numberDiff line numberDiff line change
@@ -801,3 +801,9 @@ sv.bind #hw.innerNameRef<@SiFive_MulDiv::@__ETC_SiFive_MulDiv_assert>
801801
// CHECK-NEXT: ._io_req_ready_output (1'h0)
802802
// CHECK-NEXT: .resetSignalName (reset),
803803
// CHECK-NEXT: .clock (clock)
804+
805+
// CHECK: `include "foo/bar"
806+
sv.include local "foo/bar"
807+
808+
// CHECK: `include <foo/bar>
809+
sv.include system "foo/bar"

0 commit comments

Comments
 (0)