blob: 8672a622c9882bafdf7804170eacf0327ce6ef5c [file] [log] [blame]
Chris Lattnere47e4402007-06-01 18:02:12 +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 llvm;
20using namespace clang;
21using namespace CodeGen;
22
23//===----------------------------------------------------------------------===//
24// Statement Emission
25//===----------------------------------------------------------------------===//
26
27void CodeGenFunction::EmitStmt(const Stmt *S) {
28 assert(S && "Null statement?");
29
30 switch (S->getStmtClass()) {
31 default:
32 // Must be an expression in a stmt context. Emit the value and ignore the
33 // result.
34 if (const Expr *E = dyn_cast<Expr>(S)) {
35 EmitExpr(E);
36 } else {
37 printf("Unimplemented stmt!\n");
38 S->dump();
39 }
40 break;
41 case Stmt::NullStmtClass: break;
42 case Stmt::CompoundStmtClass: EmitCompoundStmt(cast<CompoundStmt>(*S)); break;
43 case Stmt::LabelStmtClass: EmitLabelStmt(cast<LabelStmt>(*S)); break;
44 case Stmt::GotoStmtClass: EmitGotoStmt(cast<GotoStmt>(*S)); break;
Chris Lattner946aa312007-06-05 03:59:43 +000045
Chris Lattnere47e4402007-06-01 18:02:12 +000046 case Stmt::IfStmtClass: EmitIfStmt(cast<IfStmt>(*S)); break;
Chris Lattner946aa312007-06-05 03:59:43 +000047 case Stmt::WhileStmtClass: EmitWhileStmt(cast<WhileStmt>(*S)); break;
Chris Lattner8394d792007-06-05 20:53:16 +000048 case Stmt::DoStmtClass: EmitDoStmt(cast<DoStmt>(*S)); break;
49 case Stmt::ForStmtClass: EmitForStmt(cast<ForStmt>(*S)); break;
Chris Lattner946aa312007-06-05 03:59:43 +000050
Chris Lattner3f3dbee2007-06-02 03:19:07 +000051 case Stmt::ReturnStmtClass: EmitReturnStmt(cast<ReturnStmt>(*S)); break;
Chris Lattner84915fa2007-06-02 04:16:21 +000052 case Stmt::DeclStmtClass: EmitDeclStmt(cast<DeclStmt>(*S)); break;
Chris Lattnere47e4402007-06-01 18:02:12 +000053 }
54}
55
56void CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S) {
57 // FIXME: handle vla's etc.
58
59 for (CompoundStmt::const_body_iterator I = S.body_begin(), E = S.body_end();
60 I != E; ++I)
61 EmitStmt(*I);
62}
63
64void CodeGenFunction::EmitBlock(BasicBlock *BB) {
65 // Emit a branch from this block to the next one if this was a real block. If
66 // this was just a fall-through block after a terminator, don't emit it.
67 BasicBlock *LastBB = Builder.GetInsertBlock();
68
69 if (LastBB->getTerminator()) {
70 // If the previous block is already terminated, don't touch it.
71 } else if (LastBB->empty() && LastBB->getValueName() == 0) {
72 // If the last block was an empty placeholder, remove it now.
73 // TODO: cache and reuse these.
74 Builder.GetInsertBlock()->eraseFromParent();
75 } else {
76 // Otherwise, create a fall-through branch.
77 Builder.CreateBr(BB);
78 }
79 CurFn->getBasicBlockList().push_back(BB);
80 Builder.SetInsertPoint(BB);
81}
82
83void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) {
84 llvm::BasicBlock *NextBB = getBasicBlockForLabel(&S);
85
86 EmitBlock(NextBB);
87 EmitStmt(S.getSubStmt());
88}
89
90void CodeGenFunction::EmitGotoStmt(const GotoStmt &S) {
91 Builder.CreateBr(getBasicBlockForLabel(S.getLabel()));
92
93 // Emit a block after the branch so that dead code after a goto has some place
94 // to go.
95 Builder.SetInsertPoint(new BasicBlock("", CurFn));
96}
97
98void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
Chris Lattnere47e4402007-06-01 18:02:12 +000099 // C99 6.8.4.1: The first substatement is executed if the expression compares
100 // unequal to 0. The condition must be a scalar type.
Chris Lattner8394d792007-06-05 20:53:16 +0000101 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
Chris Lattnere47e4402007-06-01 18:02:12 +0000102
103 BasicBlock *ContBlock = new BasicBlock("ifend");
104 BasicBlock *ThenBlock = new BasicBlock("ifthen");
105 BasicBlock *ElseBlock = ContBlock;
106
107 if (S.getElse())
108 ElseBlock = new BasicBlock("ifelse");
109
110 // Insert the conditional branch.
111 Builder.CreateCondBr(BoolCondVal, ThenBlock, ElseBlock);
112
113 // Emit the 'then' code.
114 EmitBlock(ThenBlock);
115 EmitStmt(S.getThen());
116 Builder.CreateBr(ContBlock);
117
118 // Emit the 'else' code if present.
119 if (const Stmt *Else = S.getElse()) {
120 EmitBlock(ElseBlock);
121 EmitStmt(Else);
122 Builder.CreateBr(ContBlock);
123 }
124
125 // Emit the continuation block for code after the if.
126 EmitBlock(ContBlock);
127}
128
Chris Lattner946aa312007-06-05 03:59:43 +0000129void CodeGenFunction::EmitWhileStmt(const WhileStmt &S) {
130 // FIXME: Handle continue/break.
131
132 // Emit the header for the loop, insert it, which will create an uncond br to
133 // it.
134 BasicBlock *LoopHeader = new 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.
Chris Lattner8394d792007-06-05 20:53:16 +0000140 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
Chris Lattner946aa312007-06-05 03:59:43 +0000141
Chris Lattner8394d792007-06-05 20:53:16 +0000142 // TODO: while(1) is common, avoid extra exit blocks, etc. Be sure
143 // to correctly handle break/continue though.
Chris Lattner946aa312007-06-05 03:59:43 +0000144
145 // Create an exit block for when the condition fails, create a block for the
146 // body of the loop.
147 BasicBlock *ExitBlock = new BasicBlock("whileexit");
148 BasicBlock *LoopBody = new BasicBlock("whilebody");
149
150 // As long as the condition is true, go to the loop body.
151 Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock);
152
153 // Emit the loop body.
154 EmitBlock(LoopBody);
155 EmitStmt(S.getBody());
156
157 // Cycle to the condition.
158 Builder.CreateBr(LoopHeader);
159
160 // Emit the exit block.
161 EmitBlock(ExitBlock);
162}
163
Chris Lattner8394d792007-06-05 20:53:16 +0000164void CodeGenFunction::EmitDoStmt(const DoStmt &S) {
165 // FIXME: Handle continue/break.
166 // TODO: "do {} while (0)" is common in macros, avoid extra blocks. Be sure
167 // to correctly handle break/continue though.
168
169 // Emit the body for the loop, insert it, which will create an uncond br to
170 // it.
171 BasicBlock *LoopBody = new BasicBlock("dobody");
172 BasicBlock *AfterDo = new BasicBlock("afterdo");
173 EmitBlock(LoopBody);
174
175 // Emit the body of the loop into the block.
176 EmitStmt(S.getBody());
177
178 // C99 6.8.5.2: "The evaluation of the controlling expression takes place
179 // after each execution of the loop body."
180
181 // Evaluate the conditional in the while header.
182 // C99 6.8.5p2/p4: The first substatement is executed if the expression
183 // compares unequal to 0. The condition must be a scalar type.
184 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
185
186 // As long as the condition is true, iterate the loop.
187 Builder.CreateCondBr(BoolCondVal, LoopBody, AfterDo);
188
189 // Emit the exit block.
190 EmitBlock(AfterDo);
191}
192
193void CodeGenFunction::EmitForStmt(const ForStmt &S) {
194 // FIXME: Handle continue/break.
195 // FIXME: What do we do if the increment (f.e.) contains a stmt expression,
196 // which contains a continue/break?
197
198 // Evaluate the first part before the loop.
199 if (S.getInit())
200 EmitStmt(S.getInit());
201
202 // Start the loop with a block that tests the condition.
203 BasicBlock *CondBlock = new BasicBlock("forcond");
204 BasicBlock *AfterFor = 0;
205 EmitBlock(CondBlock);
206
207 // Evaluate the condition if present. If not, treat it as a non-zero-constant
208 // according to 6.8.5.3p2, aka, true.
209 if (S.getCond()) {
210 // C99 6.8.5p2/p4: The first substatement is executed if the expression
211 // compares unequal to 0. The condition must be a scalar type.
212 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
213
214 // As long as the condition is true, iterate the loop.
215 BasicBlock *ForBody = new BasicBlock("forbody");
216 AfterFor = new BasicBlock("afterfor");
217 Builder.CreateCondBr(BoolCondVal, ForBody, AfterFor);
218 EmitBlock(ForBody);
219 } else {
220 // Treat it as a non-zero constant. Don't even create a new block for the
221 // body, just fall into it.
222 }
223
224 // If the condition is true, execute the body of the for stmt.
225 EmitStmt(S.getBody());
226
227 // If there is an increment, emit it next.
228 if (S.getInc())
229 EmitExpr(S.getInc());
230
231 // Finally, branch back up to the condition for the next iteration.
232 Builder.CreateBr(CondBlock);
233
234 // Emit the fall-through block if there is any.
235 if (AfterFor)
236 EmitBlock(AfterFor);
237 else
238 EmitBlock(new BasicBlock());
239}
Chris Lattner946aa312007-06-05 03:59:43 +0000240
Chris Lattner3f3dbee2007-06-02 03:19:07 +0000241/// EmitReturnStmt - Note that due to GCC extensions, this can have an operand
242/// if the function returns void, or may be missing one if the function returns
243/// non-void. Fun stuff :).
244void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) {
Chris Lattner8394d792007-06-05 20:53:16 +0000245 RValue RetVal;
Chris Lattner3f3dbee2007-06-02 03:19:07 +0000246
247 // Emit the result value, even if unused, to evalute the side effects.
248 const Expr *RV = S.getRetValue();
249 if (RV)
250 RetVal = EmitExpr(RV);
251
Chris Lattnercf98efa2007-06-13 20:50:31 +0000252 QualType FnRetTy = CurFuncDecl->getType().getCanonicalType();
253 FnRetTy = cast<FunctionType>(FnRetTy)->getResultType();
254
255 if (FnRetTy->isVoidType()) {
Chris Lattner3f3dbee2007-06-02 03:19:07 +0000256 // If the function returns void, emit ret void, and ignore the retval.
257 Builder.CreateRetVoid();
258 } else if (RV == 0) {
259 // "return;" in a function that returns a value.
260 const llvm::Type *RetTy = CurFn->getFunctionType()->getReturnType();
261 if (RetTy == llvm::Type::VoidTy)
262 Builder.CreateRetVoid(); // struct return etc.
263 else
264 Builder.CreateRet(llvm::UndefValue::get(RetTy));
Chris Lattner3f3dbee2007-06-02 03:19:07 +0000265 } else {
Chris Lattnercf98efa2007-06-13 20:50:31 +0000266 // Do implicit conversions to the returned type.
267 RetVal = EmitConversion(RetVal, RV->getType(), FnRetTy, SourceLocation());
268
269 if (RetVal.isScalar()) {
270 // FIXME: Pass return loc in!
271 Builder.CreateRet(RetVal.getVal());
272 } else {
273 assert(0 && "FIXME: aggregate return unimp");
274 }
Chris Lattner3f3dbee2007-06-02 03:19:07 +0000275 }
276
Chris Lattner8394d792007-06-05 20:53:16 +0000277 // Emit a block after the branch so that dead code after a return has some
278 // place to go.
279 EmitBlock(new BasicBlock());
Chris Lattner3f3dbee2007-06-02 03:19:07 +0000280}
281
Chris Lattner1ad38f82007-06-09 01:20:56 +0000282void CodeGenFunction::EmitDeclStmt(const DeclStmt &S) {
283 for (const Decl *Decl = S.getDecl(); Decl; Decl = Decl->getNextDeclarator())
284 EmitDecl(*Decl);
285}