Chris Lattner | e47e440 | 2007-06-01 18:02:12 +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 llvm; |
| 20 | using namespace clang; |
| 21 | using namespace CodeGen; |
| 22 | |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | // Statement Emission |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
| 27 | void 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 Lattner | 946aa31 | 2007-06-05 03:59:43 +0000 | [diff] [blame] | 45 | |
Chris Lattner | e47e440 | 2007-06-01 18:02:12 +0000 | [diff] [blame] | 46 | case Stmt::IfStmtClass: EmitIfStmt(cast<IfStmt>(*S)); break; |
Chris Lattner | 946aa31 | 2007-06-05 03:59:43 +0000 | [diff] [blame] | 47 | case Stmt::WhileStmtClass: EmitWhileStmt(cast<WhileStmt>(*S)); break; |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 48 | case Stmt::DoStmtClass: EmitDoStmt(cast<DoStmt>(*S)); break; |
| 49 | case Stmt::ForStmtClass: EmitForStmt(cast<ForStmt>(*S)); break; |
Chris Lattner | 946aa31 | 2007-06-05 03:59:43 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 3f3dbee | 2007-06-02 03:19:07 +0000 | [diff] [blame] | 51 | case Stmt::ReturnStmtClass: EmitReturnStmt(cast<ReturnStmt>(*S)); break; |
Chris Lattner | 84915fa | 2007-06-02 04:16:21 +0000 | [diff] [blame] | 52 | case Stmt::DeclStmtClass: EmitDeclStmt(cast<DeclStmt>(*S)); break; |
Chris Lattner | e47e440 | 2007-06-01 18:02:12 +0000 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
| 56 | void 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 | |
| 64 | void 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 | |
| 83 | void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) { |
| 84 | llvm::BasicBlock *NextBB = getBasicBlockForLabel(&S); |
| 85 | |
| 86 | EmitBlock(NextBB); |
| 87 | EmitStmt(S.getSubStmt()); |
| 88 | } |
| 89 | |
| 90 | void 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 | |
| 98 | void CodeGenFunction::EmitIfStmt(const IfStmt &S) { |
Chris Lattner | e47e440 | 2007-06-01 18:02:12 +0000 | [diff] [blame] | 99 | // 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 Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 101 | llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); |
Chris Lattner | e47e440 | 2007-06-01 18:02:12 +0000 | [diff] [blame] | 102 | |
| 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 Lattner | 946aa31 | 2007-06-05 03:59:43 +0000 | [diff] [blame] | 129 | void 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 Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 140 | llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); |
Chris Lattner | 946aa31 | 2007-06-05 03:59:43 +0000 | [diff] [blame] | 141 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 142 | // TODO: while(1) is common, avoid extra exit blocks, etc. Be sure |
| 143 | // to correctly handle break/continue though. |
Chris Lattner | 946aa31 | 2007-06-05 03:59:43 +0000 | [diff] [blame] | 144 | |
| 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 Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 164 | void 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 | |
| 193 | void 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 Lattner | 946aa31 | 2007-06-05 03:59:43 +0000 | [diff] [blame] | 240 | |
Chris Lattner | 3f3dbee | 2007-06-02 03:19:07 +0000 | [diff] [blame] | 241 | /// 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 :). |
| 244 | void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) { |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 245 | RValue RetVal; |
Chris Lattner | 3f3dbee | 2007-06-02 03:19:07 +0000 | [diff] [blame] | 246 | |
| 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 Lattner | cf98efa | 2007-06-13 20:50:31 +0000 | [diff] [blame^] | 252 | QualType FnRetTy = CurFuncDecl->getType().getCanonicalType(); |
| 253 | FnRetTy = cast<FunctionType>(FnRetTy)->getResultType(); |
| 254 | |
| 255 | if (FnRetTy->isVoidType()) { |
Chris Lattner | 3f3dbee | 2007-06-02 03:19:07 +0000 | [diff] [blame] | 256 | // 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 Lattner | 3f3dbee | 2007-06-02 03:19:07 +0000 | [diff] [blame] | 265 | } else { |
Chris Lattner | cf98efa | 2007-06-13 20:50:31 +0000 | [diff] [blame^] | 266 | // 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 Lattner | 3f3dbee | 2007-06-02 03:19:07 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 277 | // Emit a block after the branch so that dead code after a return has some |
| 278 | // place to go. |
| 279 | EmitBlock(new BasicBlock()); |
Chris Lattner | 3f3dbee | 2007-06-02 03:19:07 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Chris Lattner | 1ad38f8 | 2007-06-09 01:20:56 +0000 | [diff] [blame] | 282 | void CodeGenFunction::EmitDeclStmt(const DeclStmt &S) { |
| 283 | for (const Decl *Decl = S.getDecl(); Decl; Decl = Decl->getNextDeclarator()) |
| 284 | EmitDecl(*Decl); |
| 285 | } |