Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- 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" |
| 19 | using namespace clang; |
| 20 | using namespace CodeGen; |
| 21 | |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | // Statement Emission |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
| 26 | void 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 Lattner | 23b1cdb | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 34 | EmitAnyExpr(E, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 35 | } 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 Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 52 | |
| 53 | case Stmt::BreakStmtClass: EmitBreakStmt(); break; |
| 54 | case Stmt::ContinueStmtClass: EmitContinueStmt(); break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
| 58 | void 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 | |
| 66 | void 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 | |
| 85 | void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) { |
| 86 | llvm::BasicBlock *NextBB = getBasicBlockForLabel(&S); |
| 87 | |
| 88 | EmitBlock(NextBB); |
| 89 | EmitStmt(S.getSubStmt()); |
| 90 | } |
| 91 | |
| 92 | void 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 | |
| 100 | void 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 | |
| 131 | void CodeGenFunction::EmitWhileStmt(const WhileStmt &S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 132 | // 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 Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 152 | |
| 153 | // Store the blocks to use for break and continue. |
| 154 | BreakContinueStack.push_back(BreakContinue(ExitBlock, LoopHeader)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 155 | |
| 156 | // Emit the loop body. |
| 157 | EmitBlock(LoopBody); |
| 158 | EmitStmt(S.getBody()); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 159 | |
| 160 | BreakContinueStack.pop_back(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 161 | |
| 162 | // Cycle to the condition. |
| 163 | Builder.CreateBr(LoopHeader); |
| 164 | |
| 165 | // Emit the exit block. |
| 166 | EmitBlock(ExitBlock); |
| 167 | } |
| 168 | |
| 169 | void CodeGenFunction::EmitDoStmt(const DoStmt &S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 170 | // 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 Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 178 | |
| 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 Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 183 | |
| 184 | // Emit the body of the loop into the block. |
| 185 | EmitStmt(S.getBody()); |
| 186 | |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 187 | BreakContinueStack.pop_back(); |
| 188 | |
| 189 | EmitBlock(DoCond); |
| 190 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 191 | // 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 | |
| 206 | void CodeGenFunction::EmitForStmt(const ForStmt &S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 207 | // FIXME: What do we do if the increment (f.e.) contains a stmt expression, |
| 208 | // which contains a continue/break? |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 209 | // 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 Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 213 | // 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 Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 219 | llvm::BasicBlock *AfterFor = new llvm::BasicBlock("afterfor"); |
| 220 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 221 | 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 Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 232 | 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 Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 239 | // 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 Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 250 | // If the condition is true, execute the body of the for stmt. |
| 251 | EmitStmt(S.getBody()); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 252 | |
| 253 | BreakContinueStack.pop_back(); |
| 254 | |
| 255 | if (S.getInc()) |
| 256 | EmitBlock(ContinueBlock); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 257 | |
| 258 | // If there is an increment, emit it next. |
| 259 | if (S.getInc()) |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 260 | EmitStmt(S.getInc()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 261 | |
| 262 | // Finally, branch back up to the condition for the next iteration. |
| 263 | Builder.CreateBr(CondBlock); |
| 264 | |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 265 | // Emit the fall-through block. |
| 266 | EmitBlock(AfterFor); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 267 | } |
| 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 :). |
| 272 | void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 273 | // Emit the result value, even if unused, to evalute the side effects. |
| 274 | const Expr *RV = S.getRetValue(); |
Chris Lattner | 4b0029d | 2007-08-26 07:14:44 +0000 | [diff] [blame] | 275 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 276 | QualType FnRetTy = CurFuncDecl->getType().getCanonicalType(); |
| 277 | FnRetTy = cast<FunctionType>(FnRetTy)->getResultType(); |
| 278 | |
| 279 | if (FnRetTy->isVoidType()) { |
Chris Lattner | 4b0029d | 2007-08-26 07:14:44 +0000 | [diff] [blame] | 280 | // If the function returns void, emit ret void. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 281 | Builder.CreateRetVoid(); |
| 282 | } else if (RV == 0) { |
Chris Lattner | 4b0029d | 2007-08-26 07:14:44 +0000 | [diff] [blame] | 283 | // Handle "return;" in a function that returns a value. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 284 | 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 Lattner | 4b0029d | 2007-08-26 07:14:44 +0000 | [diff] [blame] | 289 | } else if (!hasAggregateLLVMType(RV->getType())) { |
| 290 | Builder.CreateRet(EmitScalarExpr(RV)); |
| 291 | } else if (RV->getType()->isComplexType()) { |
| 292 | llvm::Value *SRetPtr = CurFn->arg_begin(); |
Chris Lattner | 190dbe2 | 2007-08-26 16:22:13 +0000 | [diff] [blame^] | 293 | EmitComplexExprIntoAddr(RV, SRetPtr, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 294 | } else { |
Chris Lattner | 4b0029d | 2007-08-26 07:14:44 +0000 | [diff] [blame] | 295 | llvm::Value *SRetPtr = CurFn->arg_begin(); |
| 296 | EmitAggExpr(RV, SRetPtr, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 297 | } |
| 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 | |
| 304 | void CodeGenFunction::EmitDeclStmt(const DeclStmt &S) { |
| 305 | for (const Decl *Decl = S.getDecl(); Decl; Decl = Decl->getNextDeclarator()) |
| 306 | EmitDecl(*Decl); |
Chris Lattner | 6fa5f09 | 2007-07-12 15:43:07 +0000 | [diff] [blame] | 307 | } |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 308 | |
| 309 | void 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 | |
| 317 | void 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 | } |