forked from OSchip/llvm-project
parent
f5009110bf
commit
da5d6bbc40
|
@ -57,6 +57,9 @@ void CodeGenFunction::EmitStmt(const Stmt *S) {
|
|||
|
||||
case Stmt::BreakStmtClass: EmitBreakStmt(); break;
|
||||
case Stmt::ContinueStmtClass: EmitContinueStmt(); break;
|
||||
case Stmt::SwitchStmtClass: EmitSwitchStmt(cast<SwitchStmt>(*S)); break;
|
||||
case Stmt::DefaultStmtClass: EmitDefaultStmt(cast<DefaultStmt>(*S)); break;
|
||||
case Stmt::CaseStmtClass: EmitCaseStmt(cast<CaseStmt>(*S)); break;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -350,3 +353,50 @@ void CodeGenFunction::EmitContinueStmt() {
|
|||
Builder.CreateBr(Block);
|
||||
EmitBlock(new llvm::BasicBlock());
|
||||
}
|
||||
|
||||
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");
|
||||
EmitStmt(S.getSubStmt());
|
||||
}
|
||||
|
||||
void CodeGenFunction::EmitDefaultStmt(const DefaultStmt &S) {
|
||||
StartBlock("sw.default");
|
||||
// Current insert block is the default destination.
|
||||
SwitchInsn->setSuccessor(0, Builder.GetInsertBlock());
|
||||
EmitStmt(S.getSubStmt());
|
||||
}
|
||||
|
||||
void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) {
|
||||
llvm::Value *CondV = EmitScalarExpr(S.getCond());
|
||||
|
||||
// Handle nested switch statements.
|
||||
llvm::SwitchInst *SavedSwitchInsn = SwitchInsn;
|
||||
|
||||
// Create basic block to hold stuff that comes after switch statement.
|
||||
// Initially use it to hold DefaultStmt.
|
||||
llvm::BasicBlock *NextBlock = new llvm::BasicBlock("after.sw", CurFn);
|
||||
SwitchInsn = Builder.CreateSwitch(CondV, NextBlock);
|
||||
|
||||
// All break statements jump to NextBlock. If BreakContinueStack is non empty then
|
||||
// reuse last ContinueBlock.
|
||||
llvm::BasicBlock *ContinueBlock = NULL;
|
||||
if (!BreakContinueStack.empty())
|
||||
ContinueBlock = BreakContinueStack.back().ContinueBlock;
|
||||
BreakContinueStack.push_back(BreakContinue(NextBlock, ContinueBlock));
|
||||
|
||||
// Emit switch body.
|
||||
EmitStmt(S.getBody());
|
||||
BreakContinueStack.pop_back();
|
||||
|
||||
// Prune insert block if it is dummy.
|
||||
llvm::BasicBlock *BB = Builder.GetInsertBlock();
|
||||
if (isDummyBlock(BB))
|
||||
BB->eraseFromParent();
|
||||
|
||||
// Place NextBlock as the new insert point.
|
||||
Builder.SetInsertPoint(NextBlock);
|
||||
SwitchInsn = SavedSwitchInsn;
|
||||
}
|
||||
|
|
|
@ -115,3 +115,13 @@ bool CodeGenFunction::isDummyBlock(const llvm::BasicBlock *BB) {
|
|||
return false;
|
||||
}
|
||||
|
||||
/// StartBlock - Start new block named N. If insert block is a dummy block
|
||||
/// then reuse it.
|
||||
void CodeGenFunction::StartBlock(const char *N) {
|
||||
llvm::BasicBlock *BB = Builder.GetInsertBlock();
|
||||
if (!isDummyBlock(BB))
|
||||
EmitBlock(new llvm::BasicBlock(N));
|
||||
else
|
||||
BB->setName(N);
|
||||
}
|
||||
|
||||
|
|
|
@ -41,7 +41,10 @@ namespace clang {
|
|||
class ForStmt;
|
||||
class ReturnStmt;
|
||||
class DeclStmt;
|
||||
|
||||
class CaseStmt;
|
||||
class DefaultStmt;
|
||||
class SwitchStmt;
|
||||
|
||||
class Expr;
|
||||
class DeclRefExpr;
|
||||
class StringLiteral;
|
||||
|
@ -237,6 +240,10 @@ private:
|
|||
};
|
||||
llvm::SmallVector<BreakContinue, 8> BreakContinueStack;
|
||||
|
||||
// SwitchInsn - This is used by EmitCaseStmt() and EmitDefaultStmt() to
|
||||
// populate switch instruction
|
||||
llvm::SwitchInst *SwitchInsn;
|
||||
|
||||
public:
|
||||
CodeGenFunction(CodeGenModule &cgm);
|
||||
|
||||
|
@ -281,6 +288,10 @@ public:
|
|||
/// with no predecessors.
|
||||
static bool isDummyBlock(const llvm::BasicBlock *BB);
|
||||
|
||||
/// StartBlock - Start new block named N. If insert block is a dummy block
|
||||
/// then reuse it.
|
||||
void StartBlock(const char *N);
|
||||
|
||||
//===--------------------------------------------------------------------===//
|
||||
// Declaration Emission
|
||||
//===--------------------------------------------------------------------===//
|
||||
|
@ -308,7 +319,10 @@ public:
|
|||
void EmitDeclStmt(const DeclStmt &S);
|
||||
void EmitBreakStmt();
|
||||
void EmitContinueStmt();
|
||||
|
||||
void EmitSwitchStmt(const SwitchStmt &S);
|
||||
void EmitDefaultStmt(const DefaultStmt &S);
|
||||
void EmitCaseStmt(const CaseStmt &S);
|
||||
|
||||
//===--------------------------------------------------------------------===//
|
||||
// LValue Expression Emission
|
||||
//===--------------------------------------------------------------------===//
|
||||
|
|
|
@ -271,6 +271,13 @@ public:
|
|||
Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
|
||||
Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }
|
||||
virtual Stmt* v_getSubStmt() { return getSubStmt(); }
|
||||
const Expr *getLHS() const {
|
||||
return reinterpret_cast<const Expr*>(SubExprs[LHS]);
|
||||
}
|
||||
const Expr *getRHS() const {
|
||||
return reinterpret_cast<const Expr*>(SubExprs[RHS]);
|
||||
}
|
||||
const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; }
|
||||
|
||||
virtual SourceRange getSourceRange() const {
|
||||
return SourceRange(CaseLoc, SubExprs[SUBSTMT]->getLocEnd());
|
||||
|
@ -294,6 +301,7 @@ public:
|
|||
|
||||
Stmt *getSubStmt() { return SubStmt; }
|
||||
virtual Stmt* v_getSubStmt() { return getSubStmt(); }
|
||||
const Stmt *getSubStmt() const { return SubStmt; }
|
||||
|
||||
SourceLocation getDefaultLoc() const { return DefaultLoc; }
|
||||
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
// RUN: clang %s -emit-llvm | llvm-as | opt -std-compile-opts -disable-output
|
||||
|
||||
int foo(int i) {
|
||||
int j = 0;
|
||||
switch (i) {
|
||||
case 1 :
|
||||
j = 2; break;
|
||||
case 2:
|
||||
j = 3; break;
|
||||
default:
|
||||
j = 42; break;
|
||||
}
|
||||
j = j + 1;
|
||||
return j;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue