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 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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" |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame^] | 16 | #include "clang/Basic/TargetInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | #include "llvm/Constants.h" |
| 18 | #include "llvm/DerivedTypes.h" |
| 19 | #include "llvm/Function.h" |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame^] | 20 | #include "llvm/InlineAsm.h" |
| 21 | #include "llvm/ADT/StringExtras.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | using namespace CodeGen; |
| 24 | |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | // Statement Emission |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
| 29 | void CodeGenFunction::EmitStmt(const Stmt *S) { |
| 30 | assert(S && "Null statement?"); |
| 31 | |
| 32 | switch (S->getStmtClass()) { |
| 33 | default: |
Chris Lattner | 1e4d21e | 2007-08-26 22:58:05 +0000 | [diff] [blame] | 34 | // Must be an expression in a stmt context. Emit the value (to get |
| 35 | // side-effects) and ignore the result. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 36 | if (const Expr *E = dyn_cast<Expr>(S)) { |
Chris Lattner | 1e4d21e | 2007-08-26 22:58:05 +0000 | [diff] [blame] | 37 | if (!hasAggregateLLVMType(E->getType())) |
| 38 | EmitScalarExpr(E); |
| 39 | else if (E->getType()->isComplexType()) |
| 40 | EmitComplexExpr(E); |
| 41 | else |
| 42 | EmitAggExpr(E, 0, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 43 | } else { |
Chris Lattner | dc4d280 | 2007-12-02 01:49:16 +0000 | [diff] [blame] | 44 | WarnUnsupported(S, "statement"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 45 | } |
| 46 | break; |
| 47 | case Stmt::NullStmtClass: break; |
| 48 | case Stmt::CompoundStmtClass: EmitCompoundStmt(cast<CompoundStmt>(*S)); break; |
| 49 | case Stmt::LabelStmtClass: EmitLabelStmt(cast<LabelStmt>(*S)); break; |
| 50 | case Stmt::GotoStmtClass: EmitGotoStmt(cast<GotoStmt>(*S)); break; |
| 51 | |
| 52 | case Stmt::IfStmtClass: EmitIfStmt(cast<IfStmt>(*S)); break; |
| 53 | case Stmt::WhileStmtClass: EmitWhileStmt(cast<WhileStmt>(*S)); break; |
| 54 | case Stmt::DoStmtClass: EmitDoStmt(cast<DoStmt>(*S)); break; |
| 55 | case Stmt::ForStmtClass: EmitForStmt(cast<ForStmt>(*S)); break; |
| 56 | |
| 57 | case Stmt::ReturnStmtClass: EmitReturnStmt(cast<ReturnStmt>(*S)); break; |
| 58 | case Stmt::DeclStmtClass: EmitDeclStmt(cast<DeclStmt>(*S)); break; |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 59 | |
| 60 | case Stmt::BreakStmtClass: EmitBreakStmt(); break; |
| 61 | case Stmt::ContinueStmtClass: EmitContinueStmt(); break; |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 62 | case Stmt::SwitchStmtClass: EmitSwitchStmt(cast<SwitchStmt>(*S)); break; |
| 63 | case Stmt::DefaultStmtClass: EmitDefaultStmt(cast<DefaultStmt>(*S)); break; |
| 64 | case Stmt::CaseStmtClass: EmitCaseStmt(cast<CaseStmt>(*S)); break; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame^] | 65 | case Stmt::AsmStmtClass: EmitAsmStmt(cast<AsmStmt>(*S)); break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 69 | /// EmitCompoundStmt - Emit a compound statement {..} node. If GetLast is true, |
| 70 | /// this captures the expression result of the last sub-statement and returns it |
| 71 | /// (for use by the statement expression extension). |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 72 | RValue CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast, |
| 73 | llvm::Value *AggLoc, bool isAggVol) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 74 | // FIXME: handle vla's etc. |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 75 | if (S.body_empty() || !isa<Expr>(S.body_back())) GetLast = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 77 | for (CompoundStmt::const_body_iterator I = S.body_begin(), |
| 78 | E = S.body_end()-GetLast; I != E; ++I) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 79 | EmitStmt(*I); |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 80 | |
| 81 | |
| 82 | if (!GetLast) |
| 83 | return RValue::get(0); |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 84 | |
| 85 | return EmitAnyExpr(cast<Expr>(S.body_back()), AggLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void CodeGenFunction::EmitBlock(llvm::BasicBlock *BB) { |
| 89 | // Emit a branch from this block to the next one if this was a real block. If |
| 90 | // this was just a fall-through block after a terminator, don't emit it. |
| 91 | llvm::BasicBlock *LastBB = Builder.GetInsertBlock(); |
| 92 | |
| 93 | if (LastBB->getTerminator()) { |
| 94 | // If the previous block is already terminated, don't touch it. |
| 95 | } else if (LastBB->empty() && LastBB->getValueName() == 0) { |
| 96 | // If the last block was an empty placeholder, remove it now. |
| 97 | // TODO: cache and reuse these. |
| 98 | Builder.GetInsertBlock()->eraseFromParent(); |
| 99 | } else { |
| 100 | // Otherwise, create a fall-through branch. |
| 101 | Builder.CreateBr(BB); |
| 102 | } |
| 103 | CurFn->getBasicBlockList().push_back(BB); |
| 104 | Builder.SetInsertPoint(BB); |
| 105 | } |
| 106 | |
| 107 | void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) { |
| 108 | llvm::BasicBlock *NextBB = getBasicBlockForLabel(&S); |
| 109 | |
| 110 | EmitBlock(NextBB); |
| 111 | EmitStmt(S.getSubStmt()); |
| 112 | } |
| 113 | |
| 114 | void CodeGenFunction::EmitGotoStmt(const GotoStmt &S) { |
| 115 | Builder.CreateBr(getBasicBlockForLabel(S.getLabel())); |
| 116 | |
| 117 | // Emit a block after the branch so that dead code after a goto has some place |
| 118 | // to go. |
| 119 | Builder.SetInsertPoint(new llvm::BasicBlock("", CurFn)); |
| 120 | } |
| 121 | |
| 122 | void CodeGenFunction::EmitIfStmt(const IfStmt &S) { |
| 123 | // C99 6.8.4.1: The first substatement is executed if the expression compares |
| 124 | // unequal to 0. The condition must be a scalar type. |
| 125 | llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); |
| 126 | |
| 127 | llvm::BasicBlock *ContBlock = new llvm::BasicBlock("ifend"); |
| 128 | llvm::BasicBlock *ThenBlock = new llvm::BasicBlock("ifthen"); |
| 129 | llvm::BasicBlock *ElseBlock = ContBlock; |
| 130 | |
| 131 | if (S.getElse()) |
| 132 | ElseBlock = new llvm::BasicBlock("ifelse"); |
| 133 | |
| 134 | // Insert the conditional branch. |
| 135 | Builder.CreateCondBr(BoolCondVal, ThenBlock, ElseBlock); |
| 136 | |
| 137 | // Emit the 'then' code. |
| 138 | EmitBlock(ThenBlock); |
| 139 | EmitStmt(S.getThen()); |
Devang Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 140 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 141 | if (isDummyBlock(BB)) { |
| 142 | BB->eraseFromParent(); |
| 143 | Builder.SetInsertPoint(ThenBlock); |
| 144 | } |
| 145 | else |
| 146 | Builder.CreateBr(ContBlock); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 147 | |
| 148 | // Emit the 'else' code if present. |
| 149 | if (const Stmt *Else = S.getElse()) { |
| 150 | EmitBlock(ElseBlock); |
| 151 | EmitStmt(Else); |
Devang Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 152 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 153 | if (isDummyBlock(BB)) { |
| 154 | BB->eraseFromParent(); |
| 155 | Builder.SetInsertPoint(ElseBlock); |
| 156 | } |
| 157 | else |
| 158 | Builder.CreateBr(ContBlock); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | // Emit the continuation block for code after the if. |
| 162 | EmitBlock(ContBlock); |
| 163 | } |
| 164 | |
| 165 | void CodeGenFunction::EmitWhileStmt(const WhileStmt &S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 166 | // Emit the header for the loop, insert it, which will create an uncond br to |
| 167 | // it. |
| 168 | llvm::BasicBlock *LoopHeader = new llvm::BasicBlock("whilecond"); |
| 169 | EmitBlock(LoopHeader); |
| 170 | |
| 171 | // Evaluate the conditional in the while header. C99 6.8.5.1: The evaluation |
| 172 | // of the controlling expression takes place before each execution of the loop |
| 173 | // body. |
| 174 | llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); |
Devang Patel | 2c30d8f | 2007-10-09 20:51:27 +0000 | [diff] [blame] | 175 | |
| 176 | // while(1) is common, avoid extra exit blocks. Be sure |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 177 | // to correctly handle break/continue though. |
Devang Patel | 2c30d8f | 2007-10-09 20:51:27 +0000 | [diff] [blame] | 178 | bool EmitBoolCondBranch = true; |
| 179 | if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal)) |
| 180 | if (C->isOne()) |
| 181 | EmitBoolCondBranch = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 182 | |
| 183 | // Create an exit block for when the condition fails, create a block for the |
| 184 | // body of the loop. |
| 185 | llvm::BasicBlock *ExitBlock = new llvm::BasicBlock("whileexit"); |
| 186 | llvm::BasicBlock *LoopBody = new llvm::BasicBlock("whilebody"); |
| 187 | |
| 188 | // As long as the condition is true, go to the loop body. |
Devang Patel | 2c30d8f | 2007-10-09 20:51:27 +0000 | [diff] [blame] | 189 | if (EmitBoolCondBranch) |
| 190 | Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 191 | |
| 192 | // Store the blocks to use for break and continue. |
| 193 | BreakContinueStack.push_back(BreakContinue(ExitBlock, LoopHeader)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 194 | |
| 195 | // Emit the loop body. |
| 196 | EmitBlock(LoopBody); |
| 197 | EmitStmt(S.getBody()); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 198 | |
| 199 | BreakContinueStack.pop_back(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 200 | |
| 201 | // Cycle to the condition. |
| 202 | Builder.CreateBr(LoopHeader); |
| 203 | |
| 204 | // Emit the exit block. |
| 205 | EmitBlock(ExitBlock); |
Devang Patel | 2c30d8f | 2007-10-09 20:51:27 +0000 | [diff] [blame] | 206 | |
| 207 | // If LoopHeader is a simple forwarding block then eliminate it. |
| 208 | if (!EmitBoolCondBranch |
| 209 | && &LoopHeader->front() == LoopHeader->getTerminator()) { |
| 210 | LoopHeader->replaceAllUsesWith(LoopBody); |
| 211 | LoopHeader->getTerminator()->eraseFromParent(); |
| 212 | LoopHeader->eraseFromParent(); |
| 213 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | void CodeGenFunction::EmitDoStmt(const DoStmt &S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 217 | // Emit the body for the loop, insert it, which will create an uncond br to |
| 218 | // it. |
| 219 | llvm::BasicBlock *LoopBody = new llvm::BasicBlock("dobody"); |
| 220 | llvm::BasicBlock *AfterDo = new llvm::BasicBlock("afterdo"); |
| 221 | EmitBlock(LoopBody); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 222 | |
| 223 | llvm::BasicBlock *DoCond = new llvm::BasicBlock("docond"); |
| 224 | |
| 225 | // Store the blocks to use for break and continue. |
| 226 | BreakContinueStack.push_back(BreakContinue(AfterDo, DoCond)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 227 | |
| 228 | // Emit the body of the loop into the block. |
| 229 | EmitStmt(S.getBody()); |
| 230 | |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 231 | BreakContinueStack.pop_back(); |
| 232 | |
| 233 | EmitBlock(DoCond); |
| 234 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 235 | // C99 6.8.5.2: "The evaluation of the controlling expression takes place |
| 236 | // after each execution of the loop body." |
| 237 | |
| 238 | // Evaluate the conditional in the while header. |
| 239 | // C99 6.8.5p2/p4: The first substatement is executed if the expression |
| 240 | // compares unequal to 0. The condition must be a scalar type. |
| 241 | llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); |
Devang Patel | 05f6e6b | 2007-10-09 20:33:39 +0000 | [diff] [blame] | 242 | |
| 243 | // "do {} while (0)" is common in macros, avoid extra blocks. Be sure |
| 244 | // to correctly handle break/continue though. |
| 245 | bool EmitBoolCondBranch = true; |
| 246 | if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal)) |
| 247 | if (C->isZero()) |
| 248 | EmitBoolCondBranch = false; |
| 249 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 250 | // As long as the condition is true, iterate the loop. |
Devang Patel | 05f6e6b | 2007-10-09 20:33:39 +0000 | [diff] [blame] | 251 | if (EmitBoolCondBranch) |
| 252 | Builder.CreateCondBr(BoolCondVal, LoopBody, AfterDo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 253 | |
| 254 | // Emit the exit block. |
| 255 | EmitBlock(AfterDo); |
Devang Patel | 05f6e6b | 2007-10-09 20:33:39 +0000 | [diff] [blame] | 256 | |
| 257 | // If DoCond is a simple forwarding block then eliminate it. |
| 258 | if (!EmitBoolCondBranch && &DoCond->front() == DoCond->getTerminator()) { |
| 259 | DoCond->replaceAllUsesWith(AfterDo); |
| 260 | DoCond->getTerminator()->eraseFromParent(); |
| 261 | DoCond->eraseFromParent(); |
| 262 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | void CodeGenFunction::EmitForStmt(const ForStmt &S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 266 | // FIXME: What do we do if the increment (f.e.) contains a stmt expression, |
| 267 | // which contains a continue/break? |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 268 | // TODO: We could keep track of whether the loop body contains any |
| 269 | // break/continue statements and not create unnecessary blocks (like |
| 270 | // "afterfor" for a condless loop) if it doesn't. |
| 271 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 272 | // Evaluate the first part before the loop. |
| 273 | if (S.getInit()) |
| 274 | EmitStmt(S.getInit()); |
| 275 | |
| 276 | // Start the loop with a block that tests the condition. |
| 277 | llvm::BasicBlock *CondBlock = new llvm::BasicBlock("forcond"); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 278 | llvm::BasicBlock *AfterFor = new llvm::BasicBlock("afterfor"); |
| 279 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 280 | EmitBlock(CondBlock); |
| 281 | |
| 282 | // Evaluate the condition if present. If not, treat it as a non-zero-constant |
| 283 | // according to 6.8.5.3p2, aka, true. |
| 284 | if (S.getCond()) { |
| 285 | // C99 6.8.5p2/p4: The first substatement is executed if the expression |
| 286 | // compares unequal to 0. The condition must be a scalar type. |
| 287 | llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); |
| 288 | |
| 289 | // As long as the condition is true, iterate the loop. |
| 290 | llvm::BasicBlock *ForBody = new llvm::BasicBlock("forbody"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 291 | Builder.CreateCondBr(BoolCondVal, ForBody, AfterFor); |
| 292 | EmitBlock(ForBody); |
| 293 | } else { |
| 294 | // Treat it as a non-zero constant. Don't even create a new block for the |
| 295 | // body, just fall into it. |
| 296 | } |
| 297 | |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 298 | // If the for loop doesn't have an increment we can just use the |
| 299 | // condition as the continue block. |
| 300 | llvm::BasicBlock *ContinueBlock; |
| 301 | if (S.getInc()) |
| 302 | ContinueBlock = new llvm::BasicBlock("forinc"); |
| 303 | else |
| 304 | ContinueBlock = CondBlock; |
| 305 | |
| 306 | // Store the blocks to use for break and continue. |
| 307 | BreakContinueStack.push_back(BreakContinue(AfterFor, ContinueBlock)); |
| 308 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 309 | // If the condition is true, execute the body of the for stmt. |
| 310 | EmitStmt(S.getBody()); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 311 | |
| 312 | BreakContinueStack.pop_back(); |
| 313 | |
| 314 | if (S.getInc()) |
| 315 | EmitBlock(ContinueBlock); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 316 | |
| 317 | // If there is an increment, emit it next. |
| 318 | if (S.getInc()) |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 319 | EmitStmt(S.getInc()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 320 | |
| 321 | // Finally, branch back up to the condition for the next iteration. |
| 322 | Builder.CreateBr(CondBlock); |
| 323 | |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 324 | // Emit the fall-through block. |
| 325 | EmitBlock(AfterFor); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /// EmitReturnStmt - Note that due to GCC extensions, this can have an operand |
| 329 | /// if the function returns void, or may be missing one if the function returns |
| 330 | /// non-void. Fun stuff :). |
| 331 | void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 332 | // Emit the result value, even if unused, to evalute the side effects. |
| 333 | const Expr *RV = S.getRetValue(); |
Chris Lattner | 4b0029d | 2007-08-26 07:14:44 +0000 | [diff] [blame] | 334 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 335 | QualType FnRetTy = CurFuncDecl->getType().getCanonicalType(); |
| 336 | FnRetTy = cast<FunctionType>(FnRetTy)->getResultType(); |
| 337 | |
| 338 | if (FnRetTy->isVoidType()) { |
Chris Lattner | 4b0029d | 2007-08-26 07:14:44 +0000 | [diff] [blame] | 339 | // If the function returns void, emit ret void. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 340 | Builder.CreateRetVoid(); |
| 341 | } else if (RV == 0) { |
Chris Lattner | 4b0029d | 2007-08-26 07:14:44 +0000 | [diff] [blame] | 342 | // Handle "return;" in a function that returns a value. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 343 | const llvm::Type *RetTy = CurFn->getFunctionType()->getReturnType(); |
| 344 | if (RetTy == llvm::Type::VoidTy) |
| 345 | Builder.CreateRetVoid(); // struct return etc. |
| 346 | else |
| 347 | Builder.CreateRet(llvm::UndefValue::get(RetTy)); |
Chris Lattner | 4b0029d | 2007-08-26 07:14:44 +0000 | [diff] [blame] | 348 | } else if (!hasAggregateLLVMType(RV->getType())) { |
| 349 | Builder.CreateRet(EmitScalarExpr(RV)); |
| 350 | } else if (RV->getType()->isComplexType()) { |
| 351 | llvm::Value *SRetPtr = CurFn->arg_begin(); |
Chris Lattner | 190dbe2 | 2007-08-26 16:22:13 +0000 | [diff] [blame] | 352 | EmitComplexExprIntoAddr(RV, SRetPtr, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 353 | } else { |
Chris Lattner | 4b0029d | 2007-08-26 07:14:44 +0000 | [diff] [blame] | 354 | llvm::Value *SRetPtr = CurFn->arg_begin(); |
| 355 | EmitAggExpr(RV, SRetPtr, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | // Emit a block after the branch so that dead code after a return has some |
| 359 | // place to go. |
| 360 | EmitBlock(new llvm::BasicBlock()); |
| 361 | } |
| 362 | |
| 363 | void CodeGenFunction::EmitDeclStmt(const DeclStmt &S) { |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 364 | for (const ScopedDecl *Decl = S.getDecl(); Decl; |
| 365 | Decl = Decl->getNextDeclarator()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 366 | EmitDecl(*Decl); |
Chris Lattner | 6fa5f09 | 2007-07-12 15:43:07 +0000 | [diff] [blame] | 367 | } |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 368 | |
| 369 | void CodeGenFunction::EmitBreakStmt() { |
| 370 | assert(!BreakContinueStack.empty() && "break stmt not in a loop or switch!"); |
| 371 | |
| 372 | llvm::BasicBlock *Block = BreakContinueStack.back().BreakBlock; |
| 373 | Builder.CreateBr(Block); |
| 374 | EmitBlock(new llvm::BasicBlock()); |
| 375 | } |
| 376 | |
| 377 | void CodeGenFunction::EmitContinueStmt() { |
| 378 | assert(!BreakContinueStack.empty() && "continue stmt not in a loop!"); |
| 379 | |
| 380 | llvm::BasicBlock *Block = BreakContinueStack.back().ContinueBlock; |
| 381 | Builder.CreateBr(Block); |
| 382 | EmitBlock(new llvm::BasicBlock()); |
| 383 | } |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 384 | |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 385 | /// EmitCaseStmtRange - If case statement range is not too big then |
| 386 | /// add multiple cases to switch instruction, one for each value within |
| 387 | /// the range. If range is too big then emit "if" condition check. |
| 388 | void CodeGenFunction::EmitCaseStmtRange(const CaseStmt &S) { |
| 389 | assert (S.getRHS() && "Unexpected RHS value in CaseStmt"); |
| 390 | |
| 391 | const Expr *L = S.getLHS(); |
| 392 | const Expr *R = S.getRHS(); |
| 393 | llvm::ConstantInt *LV = cast<llvm::ConstantInt>(EmitScalarExpr(L)); |
| 394 | llvm::ConstantInt *RV = cast<llvm::ConstantInt>(EmitScalarExpr(R)); |
| 395 | llvm::APInt LHS = LV->getValue(); |
Devang Patel | 00ee4e4 | 2007-10-09 17:10:59 +0000 | [diff] [blame] | 396 | const llvm::APInt &RHS = RV->getValue(); |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 397 | |
| 398 | llvm::APInt Range = RHS - LHS; |
| 399 | if (Range.ult(llvm::APInt(Range.getBitWidth(), 64))) { |
| 400 | // Range is small enough to add multiple switch instruction cases. |
| 401 | StartBlock("sw.bb"); |
| 402 | llvm::BasicBlock *CaseDest = Builder.GetInsertBlock(); |
| 403 | SwitchInsn->addCase(LV, CaseDest); |
Devang Patel | 2d79d0f | 2007-10-05 20:54:07 +0000 | [diff] [blame] | 404 | LHS++; |
| 405 | while (LHS != RHS) { |
| 406 | SwitchInsn->addCase(llvm::ConstantInt::get(LHS), CaseDest); |
| 407 | LHS++; |
| 408 | } |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 409 | SwitchInsn->addCase(RV, CaseDest); |
| 410 | EmitStmt(S.getSubStmt()); |
| 411 | return; |
| 412 | } |
| 413 | |
| 414 | // The range is too big. Emit "if" condition. |
| 415 | llvm::BasicBlock *FalseDest = NULL; |
| 416 | llvm::BasicBlock *CaseDest = new llvm::BasicBlock("sw.bb"); |
Devang Patel | 2d79d0f | 2007-10-05 20:54:07 +0000 | [diff] [blame] | 417 | |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 418 | // If we have already seen one case statement range for this switch |
| 419 | // instruction then piggy-back otherwise use default block as false |
| 420 | // destination. |
| 421 | if (CaseRangeBlock) |
| 422 | FalseDest = CaseRangeBlock; |
| 423 | else |
| 424 | FalseDest = SwitchInsn->getDefaultDest(); |
| 425 | |
| 426 | // Start new block to hold case statement range check instructions. |
| 427 | StartBlock("case.range"); |
| 428 | CaseRangeBlock = Builder.GetInsertBlock(); |
| 429 | |
| 430 | // Emit range check. |
| 431 | llvm::Value *Diff = |
| 432 | Builder.CreateSub(SwitchInsn->getCondition(), LV, "tmp"); |
| 433 | llvm::Value *Cond = |
| 434 | Builder.CreateICmpULE(Diff, llvm::ConstantInt::get(Range), "tmp"); |
| 435 | Builder.CreateCondBr(Cond, CaseDest, FalseDest); |
| 436 | |
| 437 | // Now emit case statement body. |
| 438 | EmitBlock(CaseDest); |
| 439 | EmitStmt(S.getSubStmt()); |
| 440 | } |
| 441 | |
| 442 | void CodeGenFunction::EmitCaseStmt(const CaseStmt &S) { |
| 443 | if (S.getRHS()) { |
| 444 | EmitCaseStmtRange(S); |
| 445 | return; |
| 446 | } |
| 447 | |
| 448 | StartBlock("sw.bb"); |
| 449 | llvm::BasicBlock *CaseDest = Builder.GetInsertBlock(); |
Chris Lattner | c69a581 | 2007-11-30 17:44:57 +0000 | [diff] [blame] | 450 | llvm::APSInt CaseVal(32); |
| 451 | S.getLHS()->isIntegerConstantExpr(CaseVal, getContext()); |
| 452 | llvm::ConstantInt *LV = llvm::ConstantInt::get(CaseVal); |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 453 | SwitchInsn->addCase(LV, CaseDest); |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 454 | EmitStmt(S.getSubStmt()); |
| 455 | } |
| 456 | |
| 457 | void CodeGenFunction::EmitDefaultStmt(const DefaultStmt &S) { |
| 458 | StartBlock("sw.default"); |
| 459 | // Current insert block is the default destination. |
| 460 | SwitchInsn->setSuccessor(0, Builder.GetInsertBlock()); |
| 461 | EmitStmt(S.getSubStmt()); |
| 462 | } |
| 463 | |
| 464 | void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) { |
| 465 | llvm::Value *CondV = EmitScalarExpr(S.getCond()); |
| 466 | |
| 467 | // Handle nested switch statements. |
| 468 | llvm::SwitchInst *SavedSwitchInsn = SwitchInsn; |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 469 | llvm::BasicBlock *SavedCRBlock = CaseRangeBlock; |
| 470 | CaseRangeBlock = NULL; |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 471 | |
| 472 | // Create basic block to hold stuff that comes after switch statement. |
| 473 | // Initially use it to hold DefaultStmt. |
Chris Lattner | 1438b49 | 2007-12-01 05:27:33 +0000 | [diff] [blame] | 474 | llvm::BasicBlock *NextBlock = new llvm::BasicBlock("after.sw"); |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 475 | SwitchInsn = Builder.CreateSwitch(CondV, NextBlock); |
| 476 | |
Devang Patel | e9b8c0a | 2007-10-30 20:59:40 +0000 | [diff] [blame] | 477 | // All break statements jump to NextBlock. If BreakContinueStack is non empty |
| 478 | // then reuse last ContinueBlock. |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 479 | llvm::BasicBlock *ContinueBlock = NULL; |
| 480 | if (!BreakContinueStack.empty()) |
| 481 | ContinueBlock = BreakContinueStack.back().ContinueBlock; |
| 482 | BreakContinueStack.push_back(BreakContinue(NextBlock, ContinueBlock)); |
| 483 | |
| 484 | // Emit switch body. |
| 485 | EmitStmt(S.getBody()); |
| 486 | BreakContinueStack.pop_back(); |
| 487 | |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 488 | // If one or more case statement range is seen then use CaseRangeBlock |
| 489 | // as the default block. False edge of CaseRangeBlock will lead to |
| 490 | // original default block. |
| 491 | if (CaseRangeBlock) |
| 492 | SwitchInsn->setSuccessor(0, CaseRangeBlock); |
| 493 | |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 494 | // Prune insert block if it is dummy. |
| 495 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 496 | if (isDummyBlock(BB)) |
| 497 | BB->eraseFromParent(); |
Chris Lattner | 1438b49 | 2007-12-01 05:27:33 +0000 | [diff] [blame] | 498 | else // Otherwise, branch to continuation. |
| 499 | Builder.CreateBr(NextBlock); |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 500 | |
| 501 | // Place NextBlock as the new insert point. |
Chris Lattner | 1438b49 | 2007-12-01 05:27:33 +0000 | [diff] [blame] | 502 | CurFn->getBasicBlockList().push_back(NextBlock); |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 503 | Builder.SetInsertPoint(NextBlock); |
| 504 | SwitchInsn = SavedSwitchInsn; |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 505 | CaseRangeBlock = SavedCRBlock; |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 506 | } |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame^] | 507 | |
| 508 | static inline std::string ConvertAsmString(const char *Start, |
| 509 | unsigned NumOperands) |
| 510 | { |
| 511 | static unsigned AsmCounter = 0; |
| 512 | |
| 513 | AsmCounter++; |
| 514 | |
| 515 | std::string Result; |
| 516 | |
| 517 | while (*Start) { |
| 518 | switch (*Start) { |
| 519 | default: |
| 520 | Result += *Start; |
| 521 | break; |
| 522 | case '$': |
| 523 | Result += "$$"; |
| 524 | break; |
| 525 | case '%': |
| 526 | // Escaped character |
| 527 | Start++; |
| 528 | if (!*Start) { |
| 529 | // FIXME: This should be caught during Sema. |
| 530 | assert(0 && "Trailing '%' in asm string."); |
| 531 | } |
| 532 | |
| 533 | char EscapedChar = *Start; |
| 534 | if (EscapedChar == '%') { |
| 535 | // Escaped percentage sign. |
| 536 | Result += '%'; |
| 537 | } |
| 538 | else if (EscapedChar == '=') { |
| 539 | // Generate an unique ID. |
| 540 | Result += llvm::utostr(AsmCounter); |
| 541 | } else if (isdigit(EscapedChar)) { |
| 542 | // %n - Assembler operand n |
| 543 | char *End; |
| 544 | |
| 545 | unsigned long n = strtoul(Start, &End, 10); |
| 546 | if (Start == End) { |
| 547 | // FIXME: This should be caught during Sema. |
| 548 | assert(0 && "Missing operand!"); |
| 549 | } else if (n >= NumOperands) { |
| 550 | // FIXME: This should be caught during Sema. |
| 551 | assert(0 && "Operand number out of range!"); |
| 552 | } |
| 553 | |
| 554 | Result += '$' + llvm::utostr(n); |
| 555 | } else { |
| 556 | assert(0 && "Unhandled asm escaped character!"); |
| 557 | } |
| 558 | } |
| 559 | Start++; |
| 560 | } |
| 561 | |
| 562 | return Result; |
| 563 | } |
| 564 | |
| 565 | static std::string SimplifyConstraint(const char* Constraint) |
| 566 | { |
| 567 | std::string Result; |
| 568 | |
| 569 | while (*Constraint) { |
| 570 | switch (*Constraint) { |
| 571 | default: |
| 572 | Result += *Constraint; |
| 573 | break; |
| 574 | // Ignore these |
| 575 | case '*': |
| 576 | case '?': |
| 577 | case '!': |
| 578 | break; |
| 579 | case 'g': |
| 580 | Result += "imr"; |
| 581 | break; |
| 582 | } |
| 583 | |
| 584 | Constraint++; |
| 585 | } |
| 586 | |
| 587 | return Result; |
| 588 | } |
| 589 | |
| 590 | void CodeGenFunction::EmitAsmStmt(const AsmStmt &S) { |
| 591 | std::string AsmString = |
| 592 | ConvertAsmString(std::string(S.getAsmString()->getStrData(), |
| 593 | S.getAsmString()->getByteLength()).c_str(), |
| 594 | S.getNumOutputs() + S.getNumInputs()); |
| 595 | |
| 596 | std::string Constraints; |
| 597 | |
| 598 | llvm::Value *ResultAddr = 0; |
| 599 | const llvm::Type *ResultType = llvm::Type::VoidTy; |
| 600 | |
| 601 | std::vector<const llvm::Type*> ArgTypes; |
| 602 | std::vector<llvm::Value*> Args; |
| 603 | |
| 604 | for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) { |
| 605 | std::string OutputConstraint(S.getOutputConstraint(i)->getStrData(), |
| 606 | S.getOutputConstraint(i)->getByteLength()); |
| 607 | |
| 608 | TargetInfo::ConstraintInfo Info; |
| 609 | bool result = Target.validateOutputConstraint(OutputConstraint.c_str(), |
| 610 | Info); |
| 611 | assert(result && "Failed to parse output constraint"); |
| 612 | |
| 613 | // Simplify the output constraint. |
| 614 | OutputConstraint = SimplifyConstraint(OutputConstraint.c_str() + 1); |
| 615 | |
| 616 | LValue Dest = EmitLValue(S.getOutputExpr(i)); |
| 617 | const llvm::Type *DestValueType = |
| 618 | cast<llvm::PointerType>(Dest.getAddress()->getType())->getElementType(); |
| 619 | |
| 620 | // If the first output operand is not a memory dest, we'll |
| 621 | // make it the return value. |
| 622 | if (i == 0 && !(Info & TargetInfo::CI_AllowsMemory) && |
| 623 | DestValueType->isFirstClassType()) { |
| 624 | ResultAddr = Dest.getAddress(); |
| 625 | ResultType = DestValueType; |
| 626 | Constraints += "=" + OutputConstraint; |
| 627 | } else { |
| 628 | ArgTypes.push_back(Dest.getAddress()->getType()); |
| 629 | if (i != 0) |
| 630 | Constraints += ','; |
| 631 | Constraints += '*'; |
| 632 | Constraints += OutputConstraint; |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | unsigned NumConstraints = S.getNumOutputs() + S.getNumInputs(); |
| 637 | |
| 638 | for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) { |
| 639 | const Expr *InputExpr = S.getInputExpr(i); |
| 640 | |
| 641 | std::string InputConstraint(S.getInputConstraint(i)->getStrData(), |
| 642 | S.getInputConstraint(i)->getByteLength()); |
| 643 | |
| 644 | TargetInfo::ConstraintInfo Info; |
| 645 | bool result = Target.validateInputConstraint(InputConstraint.c_str(), |
| 646 | NumConstraints, |
| 647 | Info); |
| 648 | assert(result && "Failed to parse input constraint"); |
| 649 | |
| 650 | if (i != 0 || S.getNumOutputs() > 0) |
| 651 | Constraints += ','; |
| 652 | |
| 653 | // Simplify the input constraint. |
| 654 | InputConstraint = SimplifyConstraint(InputConstraint.c_str()); |
| 655 | |
| 656 | llvm::Value *Arg; |
| 657 | |
| 658 | if ((Info & TargetInfo::CI_AllowsRegister) || |
| 659 | !(Info & TargetInfo::CI_AllowsMemory)) { |
| 660 | if (ConvertType(InputExpr->getType())->isFirstClassType()) { |
| 661 | Arg = EmitScalarExpr(InputExpr); |
| 662 | } else { |
| 663 | assert(0 && "FIXME: Implement passing non first class types as inputs"); |
| 664 | } |
| 665 | } else { |
| 666 | LValue Dest = EmitLValue(InputExpr); |
| 667 | Arg = Dest.getAddress(); |
| 668 | Constraints += '*'; |
| 669 | } |
| 670 | |
| 671 | ArgTypes.push_back(Arg->getType()); |
| 672 | Args.push_back(Arg); |
| 673 | Constraints += InputConstraint; |
| 674 | } |
| 675 | |
| 676 | // Clobbers |
| 677 | for (unsigned i = 0, e = S.getNumClobbers(); i != e; i++) { |
| 678 | std::string Clobber(S.getClobber(i)->getStrData(), |
| 679 | S.getClobber(i)->getByteLength()); |
| 680 | |
| 681 | Clobber = Target.getNormalizedGCCRegisterName(Clobber.c_str()); |
| 682 | |
| 683 | if (i != 0) |
| 684 | Constraints += ','; |
| 685 | Constraints += Clobber; |
| 686 | } |
| 687 | |
| 688 | // Add machine specific clobbers |
| 689 | if (const char *C = Target.getClobbers()) { |
| 690 | if (!Constraints.empty()) |
| 691 | Constraints += ','; |
| 692 | Constraints += C; |
| 693 | } |
| 694 | |
| 695 | const llvm::FunctionType *FTy = |
| 696 | llvm::FunctionType::get(ResultType, ArgTypes, false); |
| 697 | |
| 698 | llvm::InlineAsm *IA = |
| 699 | llvm::InlineAsm::get(FTy, AsmString, Constraints, |
| 700 | S.isVolatile() || S.getNumOutputs() == 0); |
| 701 | llvm::Value *Result = Builder.CreateCall(IA, Args.begin(), Args.end(), ""); |
| 702 | if (ResultAddr) |
| 703 | Builder.CreateStore(Result, ResultAddr); |
| 704 | } |