@@ -579,6 +579,63 @@ static LogicalResult printOperation(CppEmitter &emitter,
579
579
return success ();
580
580
}
581
581
582
+ static LogicalResult printOperation (CppEmitter &emitter,
583
+ cf::SwitchOp switchOp) {
584
+ raw_indented_ostream &os = emitter.ostream ();
585
+ auto iteratorCaseValues = (*switchOp.getCaseValues ()).begin ();
586
+ auto iteratorCaseValuesEnd = (*switchOp.getCaseValues ()).end ();
587
+ size_t caseIndex = 0 ;
588
+
589
+ os << " \n switch(" << emitter.getOrCreateName (switchOp.getFlag ()) << " ) {" ;
590
+
591
+ for (const auto caseBlock : switchOp.getCaseDestinations ()) {
592
+ if (iteratorCaseValues == iteratorCaseValuesEnd)
593
+ return switchOp.emitOpError (" case's value is absent for case block" );
594
+
595
+ os << " \n case " << *(iteratorCaseValues++) << " : {\n " ;
596
+ os.indent ();
597
+
598
+ for (auto pair : llvm::zip (switchOp.getCaseOperands (caseIndex++),
599
+ caseBlock->getArguments ())) {
600
+ Value &operand = std::get<0 >(pair);
601
+ BlockArgument &argument = std::get<1 >(pair);
602
+ os << emitter.getOrCreateName (argument) << " = "
603
+ << emitter.getOrCreateName (operand) << " ;\n " ;
604
+ }
605
+
606
+ os << " goto " ;
607
+
608
+ if (!(emitter.hasBlockLabel (*caseBlock)))
609
+ return switchOp.emitOpError (" unable to find label for case block" );
610
+ os << emitter.getOrCreateName (*caseBlock) << " ;\n " ;
611
+
612
+ os.unindent () << " }" ;
613
+ }
614
+
615
+ os << " \n default: {\n " ;
616
+ os.indent ();
617
+
618
+ for (auto pair :
619
+ llvm::zip (switchOp.getDefaultOperands (),
620
+ (switchOp.getDefaultDestination ())->getArguments ())) {
621
+ Value &operand = std::get<0 >(pair);
622
+ BlockArgument &argument = std::get<1 >(pair);
623
+ os << emitter.getOrCreateName (argument) << " = "
624
+ << emitter.getOrCreateName (operand) << " ;\n " ;
625
+ }
626
+
627
+ os << " goto " ;
628
+
629
+ if (!(emitter.hasBlockLabel (*switchOp.getDefaultDestination ())))
630
+ return switchOp.emitOpError (" unable to find label for default block" );
631
+ os << emitter.getOrCreateName (*switchOp.getDefaultDestination ()) << " ;\n " ;
632
+
633
+ os.unindent () << " }\n " ;
634
+ os << " }\n " ;
635
+
636
+ return success ();
637
+ }
638
+
582
639
static LogicalResult printCallOperation (CppEmitter &emitter, Operation *callOp,
583
640
StringRef callee) {
584
641
if (failed (emitter.emitAssignPrefix (*callOp)))
@@ -997,8 +1054,8 @@ static LogicalResult printFunctionBody(CppEmitter &emitter,
997
1054
// When generating code for an emitc.for and emitc.verbatim op, printing a
998
1055
// trailing semicolon is handled within the printOperation function.
999
1056
bool trailingSemicolon =
1000
- !isa<cf::CondBranchOp, emitc::DeclareFuncOp , emitc::ForOp ,
1001
- emitc::IfOp, emitc::VerbatimOp>(op);
1057
+ !isa<cf::CondBranchOp, cf::SwitchOp , emitc::DeclareFuncOp ,
1058
+ emitc::ForOp, emitc:: IfOp, emitc::VerbatimOp>(op);
1002
1059
1003
1060
if (failed (emitter.emitOperation (
1004
1061
op, /* trailingSemicolon=*/ trailingSemicolon)))
@@ -1496,7 +1553,7 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
1496
1553
// Builtin ops.
1497
1554
.Case <ModuleOp>([&](auto op) { return printOperation (*this , op); })
1498
1555
// CF ops.
1499
- .Case <cf::BranchOp, cf::CondBranchOp>(
1556
+ .Case <cf::BranchOp, cf::CondBranchOp, cf::SwitchOp >(
1500
1557
[&](auto op) { return printOperation (*this , op); })
1501
1558
// EmitC ops.
1502
1559
.Case <emitc::AddOp, emitc::ApplyOp, emitc::AssignOp,
0 commit comments