blob: 32d5f47feca154e148fb9e723c307f72002ca940 [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:
31 // Must be an expression in a stmt context. Emit the value and ignore the
32 // result.
33 if (const Expr *E = dyn_cast<Expr>(S)) {
Chris Lattner23b1cdb2007-08-23 23:43:33 +000034 EmitAnyExpr(E, false);
Reid Spencer5f016e22007-07-11 17:01:13 +000035 } else {
36 printf("Unimplemented stmt!\n");
37 S->dump();
38 }
39 break;
40 case Stmt::NullStmtClass: break;
41 case Stmt::CompoundStmtClass: EmitCompoundStmt(cast<CompoundStmt>(*S)); break;
42 case Stmt::LabelStmtClass: EmitLabelStmt(cast<LabelStmt>(*S)); break;
43 case Stmt::GotoStmtClass: EmitGotoStmt(cast<GotoStmt>(*S)); break;
44
45 case Stmt::IfStmtClass: EmitIfStmt(cast<IfStmt>(*S)); break;
46 case Stmt::WhileStmtClass: EmitWhileStmt(cast<WhileStmt>(*S)); break;
47 case Stmt::DoStmtClass: EmitDoStmt(cast<DoStmt>(*S)); break;
48 case Stmt::ForStmtClass: EmitForStmt(cast<ForStmt>(*S)); break;
49
50 case Stmt::ReturnStmtClass: EmitReturnStmt(cast<ReturnStmt>(*S)); break;
51 case Stmt::DeclStmtClass: EmitDeclStmt(cast<DeclStmt>(*S)); break;
Chris Lattnerda138702007-07-16 21:28:45 +000052
53 case Stmt::BreakStmtClass: EmitBreakStmt(); break;
54 case Stmt::ContinueStmtClass: EmitContinueStmt(); break;
Reid Spencer5f016e22007-07-11 17:01:13 +000055 }
56}
57
58void CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S) {
59 // FIXME: handle vla's etc.
60
61 for (CompoundStmt::const_body_iterator I = S.body_begin(), E = S.body_end();
62 I != E; ++I)
63 EmitStmt(*I);
64}
65
66void CodeGenFunction::EmitBlock(llvm::BasicBlock *BB) {
67 // Emit a branch from this block to the next one if this was a real block. If
68 // this was just a fall-through block after a terminator, don't emit it.
69 llvm::BasicBlock *LastBB = Builder.GetInsertBlock();
70
71 if (LastBB->getTerminator()) {
72 // If the previous block is already terminated, don't touch it.
73 } else if (LastBB->empty() && LastBB->getValueName() == 0) {
74 // If the last block was an empty placeholder, remove it now.
75 // TODO: cache and reuse these.
76 Builder.GetInsertBlock()->eraseFromParent();
77 } else {
78 // Otherwise, create a fall-through branch.
79 Builder.CreateBr(BB);
80 }
81 CurFn->getBasicBlockList().push_back(BB);
82 Builder.SetInsertPoint(BB);
83}
84
85void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) {
86 llvm::BasicBlock *NextBB = getBasicBlockForLabel(&S);
87
88 EmitBlock(NextBB);
89 EmitStmt(S.getSubStmt());
90}
91
92void CodeGenFunction::EmitGotoStmt(const GotoStmt &S) {
93 Builder.CreateBr(getBasicBlockForLabel(S.getLabel()));
94
95 // Emit a block after the branch so that dead code after a goto has some place
96 // to go.
97 Builder.SetInsertPoint(new llvm::BasicBlock("", CurFn));
98}
99
100void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
101 // C99 6.8.4.1: The first substatement is executed if the expression compares
102 // unequal to 0. The condition must be a scalar type.
103 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
104
105 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("ifend");
106 llvm::BasicBlock *ThenBlock = new llvm::BasicBlock("ifthen");
107 llvm::BasicBlock *ElseBlock = ContBlock;
108
109 if (S.getElse())
110 ElseBlock = new llvm::BasicBlock("ifelse");
111
112 // Insert the conditional branch.
113 Builder.CreateCondBr(BoolCondVal, ThenBlock, ElseBlock);
114
115 // Emit the 'then' code.
116 EmitBlock(ThenBlock);
117 EmitStmt(S.getThen());
118 Builder.CreateBr(ContBlock);
119
120 // Emit the 'else' code if present.
121 if (const Stmt *Else = S.getElse()) {
122 EmitBlock(ElseBlock);
123 EmitStmt(Else);
124 Builder.CreateBr(ContBlock);
125 }
126
127 // Emit the continuation block for code after the if.
128 EmitBlock(ContBlock);
129}
130
131void CodeGenFunction::EmitWhileStmt(const WhileStmt &S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 // Emit the header for the loop, insert it, which will create an uncond br to
133 // it.
134 llvm::BasicBlock *LoopHeader = new llvm::BasicBlock("whilecond");
135 EmitBlock(LoopHeader);
136
137 // Evaluate the conditional in the while header. C99 6.8.5.1: The evaluation
138 // of the controlling expression takes place before each execution of the loop
139 // body.
140 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
141
142 // TODO: while(1) is common, avoid extra exit blocks, etc. Be sure
143 // to correctly handle break/continue though.
144
145 // Create an exit block for when the condition fails, create a block for the
146 // body of the loop.
147 llvm::BasicBlock *ExitBlock = new llvm::BasicBlock("whileexit");
148 llvm::BasicBlock *LoopBody = new llvm::BasicBlock("whilebody");
149
150 // As long as the condition is true, go to the loop body.
151 Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock);
Chris Lattnerda138702007-07-16 21:28:45 +0000152
153 // Store the blocks to use for break and continue.
154 BreakContinueStack.push_back(BreakContinue(ExitBlock, LoopHeader));
Reid Spencer5f016e22007-07-11 17:01:13 +0000155
156 // Emit the loop body.
157 EmitBlock(LoopBody);
158 EmitStmt(S.getBody());
Chris Lattnerda138702007-07-16 21:28:45 +0000159
160 BreakContinueStack.pop_back();
Reid Spencer5f016e22007-07-11 17:01:13 +0000161
162 // Cycle to the condition.
163 Builder.CreateBr(LoopHeader);
164
165 // Emit the exit block.
166 EmitBlock(ExitBlock);
167}
168
169void CodeGenFunction::EmitDoStmt(const DoStmt &S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 // TODO: "do {} while (0)" is common in macros, avoid extra blocks. Be sure
171 // to correctly handle break/continue though.
172
173 // Emit the body for the loop, insert it, which will create an uncond br to
174 // it.
175 llvm::BasicBlock *LoopBody = new llvm::BasicBlock("dobody");
176 llvm::BasicBlock *AfterDo = new llvm::BasicBlock("afterdo");
177 EmitBlock(LoopBody);
Chris Lattnerda138702007-07-16 21:28:45 +0000178
179 llvm::BasicBlock *DoCond = new llvm::BasicBlock("docond");
180
181 // Store the blocks to use for break and continue.
182 BreakContinueStack.push_back(BreakContinue(AfterDo, DoCond));
Reid Spencer5f016e22007-07-11 17:01:13 +0000183
184 // Emit the body of the loop into the block.
185 EmitStmt(S.getBody());
186
Chris Lattnerda138702007-07-16 21:28:45 +0000187 BreakContinueStack.pop_back();
188
189 EmitBlock(DoCond);
190
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 // C99 6.8.5.2: "The evaluation of the controlling expression takes place
192 // after each execution of the loop body."
193
194 // Evaluate the conditional in the while header.
195 // C99 6.8.5p2/p4: The first substatement is executed if the expression
196 // compares unequal to 0. The condition must be a scalar type.
197 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
198
199 // As long as the condition is true, iterate the loop.
200 Builder.CreateCondBr(BoolCondVal, LoopBody, AfterDo);
201
202 // Emit the exit block.
203 EmitBlock(AfterDo);
204}
205
206void CodeGenFunction::EmitForStmt(const ForStmt &S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000207 // FIXME: What do we do if the increment (f.e.) contains a stmt expression,
208 // which contains a continue/break?
Chris Lattnerda138702007-07-16 21:28:45 +0000209 // TODO: We could keep track of whether the loop body contains any
210 // break/continue statements and not create unnecessary blocks (like
211 // "afterfor" for a condless loop) if it doesn't.
212
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 // Evaluate the first part before the loop.
214 if (S.getInit())
215 EmitStmt(S.getInit());
216
217 // Start the loop with a block that tests the condition.
218 llvm::BasicBlock *CondBlock = new llvm::BasicBlock("forcond");
Chris Lattnerda138702007-07-16 21:28:45 +0000219 llvm::BasicBlock *AfterFor = new llvm::BasicBlock("afterfor");
220
Reid Spencer5f016e22007-07-11 17:01:13 +0000221 EmitBlock(CondBlock);
222
223 // Evaluate the condition if present. If not, treat it as a non-zero-constant
224 // according to 6.8.5.3p2, aka, true.
225 if (S.getCond()) {
226 // C99 6.8.5p2/p4: The first substatement is executed if the expression
227 // compares unequal to 0. The condition must be a scalar type.
228 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
229
230 // As long as the condition is true, iterate the loop.
231 llvm::BasicBlock *ForBody = new llvm::BasicBlock("forbody");
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 Builder.CreateCondBr(BoolCondVal, ForBody, AfterFor);
233 EmitBlock(ForBody);
234 } else {
235 // Treat it as a non-zero constant. Don't even create a new block for the
236 // body, just fall into it.
237 }
238
Chris Lattnerda138702007-07-16 21:28:45 +0000239 // If the for loop doesn't have an increment we can just use the
240 // condition as the continue block.
241 llvm::BasicBlock *ContinueBlock;
242 if (S.getInc())
243 ContinueBlock = new llvm::BasicBlock("forinc");
244 else
245 ContinueBlock = CondBlock;
246
247 // Store the blocks to use for break and continue.
248 BreakContinueStack.push_back(BreakContinue(AfterFor, ContinueBlock));
249
Reid Spencer5f016e22007-07-11 17:01:13 +0000250 // If the condition is true, execute the body of the for stmt.
251 EmitStmt(S.getBody());
Chris Lattnerda138702007-07-16 21:28:45 +0000252
253 BreakContinueStack.pop_back();
254
255 if (S.getInc())
256 EmitBlock(ContinueBlock);
Reid Spencer5f016e22007-07-11 17:01:13 +0000257
258 // If there is an increment, emit it next.
259 if (S.getInc())
Chris Lattner883f6a72007-08-11 00:04:45 +0000260 EmitStmt(S.getInc());
Reid Spencer5f016e22007-07-11 17:01:13 +0000261
262 // Finally, branch back up to the condition for the next iteration.
263 Builder.CreateBr(CondBlock);
264
Chris Lattnerda138702007-07-16 21:28:45 +0000265 // Emit the fall-through block.
266 EmitBlock(AfterFor);
Reid Spencer5f016e22007-07-11 17:01:13 +0000267}
268
269/// EmitReturnStmt - Note that due to GCC extensions, this can have an operand
270/// if the function returns void, or may be missing one if the function returns
271/// non-void. Fun stuff :).
272void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000273 // Emit the result value, even if unused, to evalute the side effects.
274 const Expr *RV = S.getRetValue();
Chris Lattner4b0029d2007-08-26 07:14:44 +0000275
Reid Spencer5f016e22007-07-11 17:01:13 +0000276 QualType FnRetTy = CurFuncDecl->getType().getCanonicalType();
277 FnRetTy = cast<FunctionType>(FnRetTy)->getResultType();
278
279 if (FnRetTy->isVoidType()) {
Chris Lattner4b0029d2007-08-26 07:14:44 +0000280 // If the function returns void, emit ret void.
Reid Spencer5f016e22007-07-11 17:01:13 +0000281 Builder.CreateRetVoid();
282 } else if (RV == 0) {
Chris Lattner4b0029d2007-08-26 07:14:44 +0000283 // Handle "return;" in a function that returns a value.
Reid Spencer5f016e22007-07-11 17:01:13 +0000284 const llvm::Type *RetTy = CurFn->getFunctionType()->getReturnType();
285 if (RetTy == llvm::Type::VoidTy)
286 Builder.CreateRetVoid(); // struct return etc.
287 else
288 Builder.CreateRet(llvm::UndefValue::get(RetTy));
Chris Lattner4b0029d2007-08-26 07:14:44 +0000289 } else if (!hasAggregateLLVMType(RV->getType())) {
290 Builder.CreateRet(EmitScalarExpr(RV));
291 } else if (RV->getType()->isComplexType()) {
292 llvm::Value *SRetPtr = CurFn->arg_begin();
Chris Lattner190dbe22007-08-26 16:22:13 +0000293 EmitComplexExprIntoAddr(RV, SRetPtr, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000294 } else {
Chris Lattner4b0029d2007-08-26 07:14:44 +0000295 llvm::Value *SRetPtr = CurFn->arg_begin();
296 EmitAggExpr(RV, SRetPtr, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000297 }
298
299 // Emit a block after the branch so that dead code after a return has some
300 // place to go.
301 EmitBlock(new llvm::BasicBlock());
302}
303
304void CodeGenFunction::EmitDeclStmt(const DeclStmt &S) {
305 for (const Decl *Decl = S.getDecl(); Decl; Decl = Decl->getNextDeclarator())
306 EmitDecl(*Decl);
Chris Lattner6fa5f092007-07-12 15:43:07 +0000307}
Chris Lattnerda138702007-07-16 21:28:45 +0000308
309void CodeGenFunction::EmitBreakStmt() {
310 assert(!BreakContinueStack.empty() && "break stmt not in a loop or switch!");
311
312 llvm::BasicBlock *Block = BreakContinueStack.back().BreakBlock;
313 Builder.CreateBr(Block);
314 EmitBlock(new llvm::BasicBlock());
315}
316
317void CodeGenFunction::EmitContinueStmt() {
318 assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
319
320 llvm::BasicBlock *Block = BreakContinueStack.back().ContinueBlock;
321 Builder.CreateBr(Block);
322 EmitBlock(new llvm::BasicBlock());
323}