Support case statement ranges.

llvm-svn: 42648
This commit is contained in:
Devang Patel 2007-10-05 20:54:07 +00:00
parent fa2c828687
commit 64a9ca7c58
2 changed files with 30 additions and 3 deletions

View File

@ -356,9 +356,21 @@ void CodeGenFunction::EmitContinueStmt() {
void CodeGenFunction::EmitCaseStmt(const CaseStmt &S) {
StartBlock("sw.bb");
llvm::Value *V = EmitScalarExpr(S.getLHS());
SwitchInsn->addCase(cast<llvm::ConstantInt>(V), Builder.GetInsertBlock());
assert (!S.getRHS() && "Case statement range is not yet supported");
llvm::BasicBlock *CaseDest = Builder.GetInsertBlock();
llvm::ConstantInt *LV = cast<llvm::ConstantInt>(EmitScalarExpr(S.getLHS()));
SwitchInsn->addCase(LV, CaseDest);
if (const Expr *R = S.getRHS()) {
llvm::ConstantInt *RV = cast<llvm::ConstantInt>(EmitScalarExpr(R));
llvm::APInt LHS = LV->getValue();
llvm::APInt RHS = RV->getValue();
LHS++;
while (LHS != RHS) {
SwitchInsn->addCase(llvm::ConstantInt::get(LHS), CaseDest);
LHS++;
}
SwitchInsn->addCase(llvm::ConstantInt::get(LHS), CaseDest);
}
EmitStmt(S.getSubStmt());
}

View File

@ -15,3 +15,18 @@ int foo(int i) {
}
int foo2(int i) {
int j = 0;
switch (i) {
case 1 :
j = 2; break;
case 2 ... 10:
j = 3; break;
default:
j = 42; break;
}
j = j + 1;
return j;
}