@@ -774,6 +774,41 @@ static LogicalResult printOperation(CppEmitter &emitter,
774
774
return printBinaryOperation (emitter, operation, " ||" );
775
775
}
776
776
777
+ static LogicalResult printOperation (CppEmitter &emitter,
778
+ cf::SwitchOp switchOp) {
779
+ raw_indented_ostream &os = emitter.ostream ();
780
+ auto iteratorCaseValues = (*switchOp.getCaseValues ()).begin ();
781
+ auto iteratorCaseValuesEnd = (*switchOp.getCaseValues ()).end ();
782
+
783
+ os << " \n switch(" << emitter.getOrCreateName (switchOp.getFlag ()) << " ) {" ;
784
+
785
+ for (const auto caseBlock : switchOp.getCaseDestinations ()) {
786
+ if (iteratorCaseValues == iteratorCaseValuesEnd)
787
+ return switchOp.emitOpError (" case's value is absent for case block" );
788
+
789
+ os << " \n case " << *(iteratorCaseValues++) << " : {\n " ;
790
+ os.indent () << " goto " ;
791
+
792
+ if (!(emitter.hasBlockLabel (*caseBlock)))
793
+ return switchOp.emitOpError (" unable to find label for case block" );
794
+ os << emitter.getOrCreateName (*caseBlock) << " ;\n " ;
795
+
796
+ os.unindent () << " }" ;
797
+ }
798
+
799
+ os << " \n default: {\n " ;
800
+ os.indent () << " goto " ;
801
+
802
+ if (!(emitter.hasBlockLabel (*switchOp.getDefaultDestination ())))
803
+ return switchOp.emitOpError (" unable to find label for default block" );
804
+ os << emitter.getOrCreateName (*switchOp.getDefaultDestination ()) << " ;\n " ;
805
+
806
+ os.unindent () << " }\n " ;
807
+ os << " }\n " ;
808
+
809
+ return success ();
810
+ }
811
+
777
812
static LogicalResult printOperation (CppEmitter &emitter, emitc::ForOp forOp) {
778
813
779
814
raw_indented_ostream &os = emitter.ostream ();
@@ -998,7 +1033,7 @@ static LogicalResult printFunctionBody(CppEmitter &emitter,
998
1033
// trailing semicolon is handled within the printOperation function.
999
1034
bool trailingSemicolon =
1000
1035
!isa<cf::CondBranchOp, emitc::DeclareFuncOp, emitc::ForOp,
1001
- emitc::IfOp, emitc::VerbatimOp>(op);
1036
+ cf::SwitchOp, emitc::IfOp, emitc::VerbatimOp>(op);
1002
1037
1003
1038
if (failed (emitter.emitOperation (
1004
1039
op, /* trailingSemicolon=*/ trailingSemicolon)))
@@ -1496,7 +1531,7 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
1496
1531
// Builtin ops.
1497
1532
.Case <ModuleOp>([&](auto op) { return printOperation (*this , op); })
1498
1533
// CF ops.
1499
- .Case <cf::BranchOp, cf::CondBranchOp>(
1534
+ .Case <cf::BranchOp, cf::CondBranchOp, cf::SwitchOp >(
1500
1535
[&](auto op) { return printOperation (*this , op); })
1501
1536
// EmitC ops.
1502
1537
.Case <emitc::AddOp, emitc::ApplyOp, emitc::AssignOp,
0 commit comments