blob: 6a2e3aa54302b86e15ba747c1d8172c5dfedba8e [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- CGStmt.cpp - Emit LLVM Code from Statements ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Stmt nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "clang/AST/AST.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19using namespace clang;
20using namespace CodeGen;
21
22//===----------------------------------------------------------------------===//
23// Statement Emission
24//===----------------------------------------------------------------------===//
25
26void CodeGenFunction::EmitStmt(const Stmt *S) {
27 assert(S && "Null statement?");
28
29 switch (S->getStmtClass()) {
30 default:
Chris Lattner1e4d21e2007-08-26 22:58:05 +000031 // Must be an expression in a stmt context. Emit the value (to get
32 // side-effects) and ignore the result.
Reid Spencer5f016e22007-07-11 17:01:13 +000033 if (const Expr *E = dyn_cast<Expr>(S)) {
Chris Lattner1e4d21e2007-08-26 22:58:05 +000034 if (!hasAggregateLLVMType(E->getType()))
35 EmitScalarExpr(E);
36 else if (E->getType()->isComplexType())
37 EmitComplexExpr(E);
38 else
39 EmitAggExpr(E, 0, false);
Reid Spencer5f016e22007-07-11 17:01:13 +000040 } else {
41 printf("Unimplemented stmt!\n");
Chris Lattner419ea7e2007-09-13 01:17:29 +000042 S->dump(getContext().SourceMgr);
Reid Spencer5f016e22007-07-11 17:01:13 +000043 }
44 break;
45 case Stmt::NullStmtClass: break;
46 case Stmt::CompoundStmtClass: EmitCompoundStmt(cast<CompoundStmt>(*S)); break;
47 case Stmt::LabelStmtClass: EmitLabelStmt(cast<LabelStmt>(*S)); break;
48 case Stmt::GotoStmtClass: EmitGotoStmt(cast<GotoStmt>(*S)); break;
49
50 case Stmt::IfStmtClass: EmitIfStmt(cast<IfStmt>(*S)); break;
51 case Stmt::WhileStmtClass: EmitWhileStmt(cast<WhileStmt>(*S)); break;
52 case Stmt::DoStmtClass: EmitDoStmt(cast<DoStmt>(*S)); break;
53 case Stmt::ForStmtClass: EmitForStmt(cast<ForStmt>(*S)); break;
54
55 case Stmt::ReturnStmtClass: EmitReturnStmt(cast<ReturnStmt>(*S)); break;
56 case Stmt::DeclStmtClass: EmitDeclStmt(cast<DeclStmt>(*S)); break;
Chris Lattnerda138702007-07-16 21:28:45 +000057
58 case Stmt::BreakStmtClass: EmitBreakStmt(); break;
59 case Stmt::ContinueStmtClass: EmitContinueStmt(); break;
Devang Patel51b09f22007-10-04 23:45:31 +000060 case Stmt::SwitchStmtClass: EmitSwitchStmt(cast<SwitchStmt>(*S)); break;
61 case Stmt::DefaultStmtClass: EmitDefaultStmt(cast<DefaultStmt>(*S)); break;
62 case Stmt::CaseStmtClass: EmitCaseStmt(cast<CaseStmt>(*S)); break;
Reid Spencer5f016e22007-07-11 17:01:13 +000063 }
64}
65
Chris Lattner33793202007-08-31 22:09:40 +000066/// EmitCompoundStmt - Emit a compound statement {..} node. If GetLast is true,
67/// this captures the expression result of the last sub-statement and returns it
68/// (for use by the statement expression extension).
Chris Lattner9b655512007-08-31 22:49:20 +000069RValue CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast,
70 llvm::Value *AggLoc, bool isAggVol) {
Reid Spencer5f016e22007-07-11 17:01:13 +000071 // FIXME: handle vla's etc.
Chris Lattner33793202007-08-31 22:09:40 +000072 if (S.body_empty() || !isa<Expr>(S.body_back())) GetLast = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000073
Chris Lattner33793202007-08-31 22:09:40 +000074 for (CompoundStmt::const_body_iterator I = S.body_begin(),
75 E = S.body_end()-GetLast; I != E; ++I)
Reid Spencer5f016e22007-07-11 17:01:13 +000076 EmitStmt(*I);
Chris Lattner33793202007-08-31 22:09:40 +000077
78
79 if (!GetLast)
80 return RValue::get(0);
Chris Lattner9b655512007-08-31 22:49:20 +000081
82 return EmitAnyExpr(cast<Expr>(S.body_back()), AggLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000083}
84
85void CodeGenFunction::EmitBlock(llvm::BasicBlock *BB) {
86 // Emit a branch from this block to the next one if this was a real block. If
87 // this was just a fall-through block after a terminator, don't emit it.
88 llvm::BasicBlock *LastBB = Builder.GetInsertBlock();
89
90 if (LastBB->getTerminator()) {
91 // If the previous block is already terminated, don't touch it.
92 } else if (LastBB->empty() && LastBB->getValueName() == 0) {
93 // If the last block was an empty placeholder, remove it now.
94 // TODO: cache and reuse these.
95 Builder.GetInsertBlock()->eraseFromParent();
96 } else {
97 // Otherwise, create a fall-through branch.
98 Builder.CreateBr(BB);
99 }
100 CurFn->getBasicBlockList().push_back(BB);
101 Builder.SetInsertPoint(BB);
102}
103
104void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) {
105 llvm::BasicBlock *NextBB = getBasicBlockForLabel(&S);
106
107 EmitBlock(NextBB);
108 EmitStmt(S.getSubStmt());
109}
110
111void CodeGenFunction::EmitGotoStmt(const GotoStmt &S) {
112 Builder.CreateBr(getBasicBlockForLabel(S.getLabel()));
113
114 // Emit a block after the branch so that dead code after a goto has some place
115 // to go.
116 Builder.SetInsertPoint(new llvm::BasicBlock("", CurFn));
117}
118
119void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
120 // C99 6.8.4.1: The first substatement is executed if the expression compares
121 // unequal to 0. The condition must be a scalar type.
122 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
123
124 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("ifend");
125 llvm::BasicBlock *ThenBlock = new llvm::BasicBlock("ifthen");
126 llvm::BasicBlock *ElseBlock = ContBlock;
127
128 if (S.getElse())
129 ElseBlock = new llvm::BasicBlock("ifelse");
130
131 // Insert the conditional branch.
132 Builder.CreateCondBr(BoolCondVal, ThenBlock, ElseBlock);
133
134 // Emit the 'then' code.
135 EmitBlock(ThenBlock);
136 EmitStmt(S.getThen());
Devang Pateld9363c32007-09-28 21:49:18 +0000137 llvm::BasicBlock *BB = Builder.GetInsertBlock();
138 if (isDummyBlock(BB)) {
139 BB->eraseFromParent();
140 Builder.SetInsertPoint(ThenBlock);
141 }
142 else
143 Builder.CreateBr(ContBlock);
Reid Spencer5f016e22007-07-11 17:01:13 +0000144
145 // Emit the 'else' code if present.
146 if (const Stmt *Else = S.getElse()) {
147 EmitBlock(ElseBlock);
148 EmitStmt(Else);
Devang Pateld9363c32007-09-28 21:49:18 +0000149 llvm::BasicBlock *BB = Builder.GetInsertBlock();
150 if (isDummyBlock(BB)) {
151 BB->eraseFromParent();
152 Builder.SetInsertPoint(ElseBlock);
153 }
154 else
155 Builder.CreateBr(ContBlock);
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 }
157
158 // Emit the continuation block for code after the if.
159 EmitBlock(ContBlock);
160}
161
162void CodeGenFunction::EmitWhileStmt(const WhileStmt &S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 // Emit the header for the loop, insert it, which will create an uncond br to
164 // it.
165 llvm::BasicBlock *LoopHeader = new llvm::BasicBlock("whilecond");
166 EmitBlock(LoopHeader);
167
168 // Evaluate the conditional in the while header. C99 6.8.5.1: The evaluation
169 // of the controlling expression takes place before each execution of the loop
170 // body.
171 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
Devang Patel2c30d8f2007-10-09 20:51:27 +0000172
173 // while(1) is common, avoid extra exit blocks. Be sure
Reid Spencer5f016e22007-07-11 17:01:13 +0000174 // to correctly handle break/continue though.
Devang Patel2c30d8f2007-10-09 20:51:27 +0000175 bool EmitBoolCondBranch = true;
176 if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal))
177 if (C->isOne())
178 EmitBoolCondBranch = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000179
180 // Create an exit block for when the condition fails, create a block for the
181 // body of the loop.
182 llvm::BasicBlock *ExitBlock = new llvm::BasicBlock("whileexit");
183 llvm::BasicBlock *LoopBody = new llvm::BasicBlock("whilebody");
184
185 // As long as the condition is true, go to the loop body.
Devang Patel2c30d8f2007-10-09 20:51:27 +0000186 if (EmitBoolCondBranch)
187 Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock);
Chris Lattnerda138702007-07-16 21:28:45 +0000188
189 // Store the blocks to use for break and continue.
190 BreakContinueStack.push_back(BreakContinue(ExitBlock, LoopHeader));
Reid Spencer5f016e22007-07-11 17:01:13 +0000191
192 // Emit the loop body.
193 EmitBlock(LoopBody);
194 EmitStmt(S.getBody());
Chris Lattnerda138702007-07-16 21:28:45 +0000195
196 BreakContinueStack.pop_back();
Reid Spencer5f016e22007-07-11 17:01:13 +0000197
198 // Cycle to the condition.
199 Builder.CreateBr(LoopHeader);
200
201 // Emit the exit block.
202 EmitBlock(ExitBlock);
Devang Patel2c30d8f2007-10-09 20:51:27 +0000203
204 // If LoopHeader is a simple forwarding block then eliminate it.
205 if (!EmitBoolCondBranch
206 && &LoopHeader->front() == LoopHeader->getTerminator()) {
207 LoopHeader->replaceAllUsesWith(LoopBody);
208 LoopHeader->getTerminator()->eraseFromParent();
209 LoopHeader->eraseFromParent();
210 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000211}
212
213void CodeGenFunction::EmitDoStmt(const DoStmt &S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000214 // Emit the body for the loop, insert it, which will create an uncond br to
215 // it.
216 llvm::BasicBlock *LoopBody = new llvm::BasicBlock("dobody");
217 llvm::BasicBlock *AfterDo = new llvm::BasicBlock("afterdo");
218 EmitBlock(LoopBody);
Chris Lattnerda138702007-07-16 21:28:45 +0000219
220 llvm::BasicBlock *DoCond = new llvm::BasicBlock("docond");
221
222 // Store the blocks to use for break and continue.
223 BreakContinueStack.push_back(BreakContinue(AfterDo, DoCond));
Reid Spencer5f016e22007-07-11 17:01:13 +0000224
225 // Emit the body of the loop into the block.
226 EmitStmt(S.getBody());
227
Chris Lattnerda138702007-07-16 21:28:45 +0000228 BreakContinueStack.pop_back();
229
230 EmitBlock(DoCond);
231
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 // C99 6.8.5.2: "The evaluation of the controlling expression takes place
233 // after each execution of the loop body."
234
235 // Evaluate the conditional in the while header.
236 // C99 6.8.5p2/p4: The first substatement is executed if the expression
237 // compares unequal to 0. The condition must be a scalar type.
238 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
Devang Patel05f6e6b2007-10-09 20:33:39 +0000239
240 // "do {} while (0)" is common in macros, avoid extra blocks. Be sure
241 // to correctly handle break/continue though.
242 bool EmitBoolCondBranch = true;
243 if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal))
244 if (C->isZero())
245 EmitBoolCondBranch = false;
246
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 // As long as the condition is true, iterate the loop.
Devang Patel05f6e6b2007-10-09 20:33:39 +0000248 if (EmitBoolCondBranch)
249 Builder.CreateCondBr(BoolCondVal, LoopBody, AfterDo);
Reid Spencer5f016e22007-07-11 17:01:13 +0000250
251 // Emit the exit block.
252 EmitBlock(AfterDo);
Devang Patel05f6e6b2007-10-09 20:33:39 +0000253
254 // If DoCond is a simple forwarding block then eliminate it.
255 if (!EmitBoolCondBranch && &DoCond->front() == DoCond->getTerminator()) {
256 DoCond->replaceAllUsesWith(AfterDo);
257 DoCond->getTerminator()->eraseFromParent();
258 DoCond->eraseFromParent();
259 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000260}
261
262void CodeGenFunction::EmitForStmt(const ForStmt &S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000263 // FIXME: What do we do if the increment (f.e.) contains a stmt expression,
264 // which contains a continue/break?
Chris Lattnerda138702007-07-16 21:28:45 +0000265 // TODO: We could keep track of whether the loop body contains any
266 // break/continue statements and not create unnecessary blocks (like
267 // "afterfor" for a condless loop) if it doesn't.
268
Reid Spencer5f016e22007-07-11 17:01:13 +0000269 // Evaluate the first part before the loop.
270 if (S.getInit())
271 EmitStmt(S.getInit());
272
273 // Start the loop with a block that tests the condition.
274 llvm::BasicBlock *CondBlock = new llvm::BasicBlock("forcond");
Chris Lattnerda138702007-07-16 21:28:45 +0000275 llvm::BasicBlock *AfterFor = new llvm::BasicBlock("afterfor");
276
Reid Spencer5f016e22007-07-11 17:01:13 +0000277 EmitBlock(CondBlock);
278
279 // Evaluate the condition if present. If not, treat it as a non-zero-constant
280 // according to 6.8.5.3p2, aka, true.
281 if (S.getCond()) {
282 // C99 6.8.5p2/p4: The first substatement is executed if the expression
283 // compares unequal to 0. The condition must be a scalar type.
284 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
285
286 // As long as the condition is true, iterate the loop.
287 llvm::BasicBlock *ForBody = new llvm::BasicBlock("forbody");
Reid Spencer5f016e22007-07-11 17:01:13 +0000288 Builder.CreateCondBr(BoolCondVal, ForBody, AfterFor);
289 EmitBlock(ForBody);
290 } else {
291 // Treat it as a non-zero constant. Don't even create a new block for the
292 // body, just fall into it.
293 }
294
Chris Lattnerda138702007-07-16 21:28:45 +0000295 // If the for loop doesn't have an increment we can just use the
296 // condition as the continue block.
297 llvm::BasicBlock *ContinueBlock;
298 if (S.getInc())
299 ContinueBlock = new llvm::BasicBlock("forinc");
300 else
301 ContinueBlock = CondBlock;
302
303 // Store the blocks to use for break and continue.
304 BreakContinueStack.push_back(BreakContinue(AfterFor, ContinueBlock));
305
Reid Spencer5f016e22007-07-11 17:01:13 +0000306 // If the condition is true, execute the body of the for stmt.
307 EmitStmt(S.getBody());
Chris Lattnerda138702007-07-16 21:28:45 +0000308
309 BreakContinueStack.pop_back();
310
311 if (S.getInc())
312 EmitBlock(ContinueBlock);
Reid Spencer5f016e22007-07-11 17:01:13 +0000313
314 // If there is an increment, emit it next.
315 if (S.getInc())
Chris Lattner883f6a72007-08-11 00:04:45 +0000316 EmitStmt(S.getInc());
Reid Spencer5f016e22007-07-11 17:01:13 +0000317
318 // Finally, branch back up to the condition for the next iteration.
319 Builder.CreateBr(CondBlock);
320
Chris Lattnerda138702007-07-16 21:28:45 +0000321 // Emit the fall-through block.
322 EmitBlock(AfterFor);
Reid Spencer5f016e22007-07-11 17:01:13 +0000323}
324
325/// EmitReturnStmt - Note that due to GCC extensions, this can have an operand
326/// if the function returns void, or may be missing one if the function returns
327/// non-void. Fun stuff :).
328void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 // Emit the result value, even if unused, to evalute the side effects.
330 const Expr *RV = S.getRetValue();
Chris Lattner4b0029d2007-08-26 07:14:44 +0000331
Reid Spencer5f016e22007-07-11 17:01:13 +0000332 QualType FnRetTy = CurFuncDecl->getType().getCanonicalType();
333 FnRetTy = cast<FunctionType>(FnRetTy)->getResultType();
334
335 if (FnRetTy->isVoidType()) {
Chris Lattner4b0029d2007-08-26 07:14:44 +0000336 // If the function returns void, emit ret void.
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 Builder.CreateRetVoid();
338 } else if (RV == 0) {
Chris Lattner4b0029d2007-08-26 07:14:44 +0000339 // Handle "return;" in a function that returns a value.
Reid Spencer5f016e22007-07-11 17:01:13 +0000340 const llvm::Type *RetTy = CurFn->getFunctionType()->getReturnType();
341 if (RetTy == llvm::Type::VoidTy)
342 Builder.CreateRetVoid(); // struct return etc.
343 else
344 Builder.CreateRet(llvm::UndefValue::get(RetTy));
Chris Lattner4b0029d2007-08-26 07:14:44 +0000345 } else if (!hasAggregateLLVMType(RV->getType())) {
346 Builder.CreateRet(EmitScalarExpr(RV));
347 } else if (RV->getType()->isComplexType()) {
348 llvm::Value *SRetPtr = CurFn->arg_begin();
Chris Lattner190dbe22007-08-26 16:22:13 +0000349 EmitComplexExprIntoAddr(RV, SRetPtr, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000350 } else {
Chris Lattner4b0029d2007-08-26 07:14:44 +0000351 llvm::Value *SRetPtr = CurFn->arg_begin();
352 EmitAggExpr(RV, SRetPtr, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 }
354
355 // Emit a block after the branch so that dead code after a return has some
356 // place to go.
357 EmitBlock(new llvm::BasicBlock());
358}
359
360void CodeGenFunction::EmitDeclStmt(const DeclStmt &S) {
Steve Naroff94745042007-09-13 23:52:58 +0000361 for (const ScopedDecl *Decl = S.getDecl(); Decl;
362 Decl = Decl->getNextDeclarator())
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 EmitDecl(*Decl);
Chris Lattner6fa5f092007-07-12 15:43:07 +0000364}
Chris Lattnerda138702007-07-16 21:28:45 +0000365
366void CodeGenFunction::EmitBreakStmt() {
367 assert(!BreakContinueStack.empty() && "break stmt not in a loop or switch!");
368
369 llvm::BasicBlock *Block = BreakContinueStack.back().BreakBlock;
370 Builder.CreateBr(Block);
371 EmitBlock(new llvm::BasicBlock());
372}
373
374void CodeGenFunction::EmitContinueStmt() {
375 assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
376
377 llvm::BasicBlock *Block = BreakContinueStack.back().ContinueBlock;
378 Builder.CreateBr(Block);
379 EmitBlock(new llvm::BasicBlock());
380}
Devang Patel51b09f22007-10-04 23:45:31 +0000381
Devang Patelc049e4f2007-10-08 20:57:48 +0000382/// EmitCaseStmtRange - If case statement range is not too big then
383/// add multiple cases to switch instruction, one for each value within
384/// the range. If range is too big then emit "if" condition check.
385void CodeGenFunction::EmitCaseStmtRange(const CaseStmt &S) {
386 assert (S.getRHS() && "Unexpected RHS value in CaseStmt");
387
388 const Expr *L = S.getLHS();
389 const Expr *R = S.getRHS();
390 llvm::ConstantInt *LV = cast<llvm::ConstantInt>(EmitScalarExpr(L));
391 llvm::ConstantInt *RV = cast<llvm::ConstantInt>(EmitScalarExpr(R));
392 llvm::APInt LHS = LV->getValue();
Devang Patel00ee4e42007-10-09 17:10:59 +0000393 const llvm::APInt &RHS = RV->getValue();
Devang Patelc049e4f2007-10-08 20:57:48 +0000394
395 llvm::APInt Range = RHS - LHS;
396 if (Range.ult(llvm::APInt(Range.getBitWidth(), 64))) {
397 // Range is small enough to add multiple switch instruction cases.
398 StartBlock("sw.bb");
399 llvm::BasicBlock *CaseDest = Builder.GetInsertBlock();
400 SwitchInsn->addCase(LV, CaseDest);
Devang Patel2d79d0f2007-10-05 20:54:07 +0000401 LHS++;
402 while (LHS != RHS) {
403 SwitchInsn->addCase(llvm::ConstantInt::get(LHS), CaseDest);
404 LHS++;
405 }
Devang Patelc049e4f2007-10-08 20:57:48 +0000406 SwitchInsn->addCase(RV, CaseDest);
407 EmitStmt(S.getSubStmt());
408 return;
409 }
410
411 // The range is too big. Emit "if" condition.
412 llvm::BasicBlock *FalseDest = NULL;
413 llvm::BasicBlock *CaseDest = new llvm::BasicBlock("sw.bb");
Devang Patel2d79d0f2007-10-05 20:54:07 +0000414
Devang Patelc049e4f2007-10-08 20:57:48 +0000415 // If we have already seen one case statement range for this switch
416 // instruction then piggy-back otherwise use default block as false
417 // destination.
418 if (CaseRangeBlock)
419 FalseDest = CaseRangeBlock;
420 else
421 FalseDest = SwitchInsn->getDefaultDest();
422
423 // Start new block to hold case statement range check instructions.
424 StartBlock("case.range");
425 CaseRangeBlock = Builder.GetInsertBlock();
426
427 // Emit range check.
428 llvm::Value *Diff =
429 Builder.CreateSub(SwitchInsn->getCondition(), LV, "tmp");
430 llvm::Value *Cond =
431 Builder.CreateICmpULE(Diff, llvm::ConstantInt::get(Range), "tmp");
432 Builder.CreateCondBr(Cond, CaseDest, FalseDest);
433
434 // Now emit case statement body.
435 EmitBlock(CaseDest);
436 EmitStmt(S.getSubStmt());
437}
438
439void CodeGenFunction::EmitCaseStmt(const CaseStmt &S) {
440 if (S.getRHS()) {
441 EmitCaseStmtRange(S);
442 return;
443 }
444
445 StartBlock("sw.bb");
446 llvm::BasicBlock *CaseDest = Builder.GetInsertBlock();
447 llvm::ConstantInt *LV = cast<llvm::ConstantInt>(EmitScalarExpr(S.getLHS()));
448 SwitchInsn->addCase(LV, CaseDest);
Devang Patel51b09f22007-10-04 23:45:31 +0000449 EmitStmt(S.getSubStmt());
450}
451
452void CodeGenFunction::EmitDefaultStmt(const DefaultStmt &S) {
453 StartBlock("sw.default");
454 // Current insert block is the default destination.
455 SwitchInsn->setSuccessor(0, Builder.GetInsertBlock());
456 EmitStmt(S.getSubStmt());
457}
458
459void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) {
460 llvm::Value *CondV = EmitScalarExpr(S.getCond());
461
462 // Handle nested switch statements.
463 llvm::SwitchInst *SavedSwitchInsn = SwitchInsn;
Devang Patelc049e4f2007-10-08 20:57:48 +0000464 llvm::BasicBlock *SavedCRBlock = CaseRangeBlock;
465 CaseRangeBlock = NULL;
Devang Patel51b09f22007-10-04 23:45:31 +0000466
467 // Create basic block to hold stuff that comes after switch statement.
468 // Initially use it to hold DefaultStmt.
469 llvm::BasicBlock *NextBlock = new llvm::BasicBlock("after.sw", CurFn);
470 SwitchInsn = Builder.CreateSwitch(CondV, NextBlock);
471
Devang Patele9b8c0a2007-10-30 20:59:40 +0000472 // All break statements jump to NextBlock. If BreakContinueStack is non empty
473 // then reuse last ContinueBlock.
Devang Patel51b09f22007-10-04 23:45:31 +0000474 llvm::BasicBlock *ContinueBlock = NULL;
475 if (!BreakContinueStack.empty())
476 ContinueBlock = BreakContinueStack.back().ContinueBlock;
477 BreakContinueStack.push_back(BreakContinue(NextBlock, ContinueBlock));
478
479 // Emit switch body.
480 EmitStmt(S.getBody());
481 BreakContinueStack.pop_back();
482
Devang Patelc049e4f2007-10-08 20:57:48 +0000483 // If one or more case statement range is seen then use CaseRangeBlock
484 // as the default block. False edge of CaseRangeBlock will lead to
485 // original default block.
486 if (CaseRangeBlock)
487 SwitchInsn->setSuccessor(0, CaseRangeBlock);
488
Devang Patel51b09f22007-10-04 23:45:31 +0000489 // Prune insert block if it is dummy.
490 llvm::BasicBlock *BB = Builder.GetInsertBlock();
491 if (isDummyBlock(BB))
492 BB->eraseFromParent();
493
494 // Place NextBlock as the new insert point.
495 Builder.SetInsertPoint(NextBlock);
496 SwitchInsn = SavedSwitchInsn;
Devang Patelc049e4f2007-10-08 20:57:48 +0000497 CaseRangeBlock = SavedCRBlock;
Devang Patel51b09f22007-10-04 23:45:31 +0000498}