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 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 14 | #include "CGDebugInfo.h" |
| 15 | #include "CodeGenModule.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | #include "CodeGenFunction.h" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 17 | #include "clang/AST/StmtVisitor.h" |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 18 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 19 | #include "llvm/InlineAsm.h" |
| 20 | #include "llvm/ADT/StringExtras.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | using namespace CodeGen; |
| 23 | |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | // Statement Emission |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
| 28 | void CodeGenFunction::EmitStmt(const Stmt *S) { |
| 29 | assert(S && "Null statement?"); |
| 30 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 31 | // Generate stoppoints if we are emitting debug info. |
| 32 | // Beginning of a Compound Statement (e.g. an opening '{') does not produce |
| 33 | // executable code. So do not generate a stoppoint for that. |
| 34 | CGDebugInfo *DI = CGM.getDebugInfo(); |
| 35 | if (DI && S->getStmtClass() != Stmt::CompoundStmtClass) { |
Daniel Dunbar | 66031a5 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 36 | DI->setLocation(S->getLocStart()); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 37 | DI->EmitStopPoint(CurFn, Builder); |
| 38 | } |
| 39 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 40 | switch (S->getStmtClass()) { |
| 41 | default: |
Chris Lattner | 1e4d21e | 2007-08-26 22:58:05 +0000 | [diff] [blame] | 42 | // Must be an expression in a stmt context. Emit the value (to get |
| 43 | // side-effects) and ignore the result. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 44 | if (const Expr *E = dyn_cast<Expr>(S)) { |
Chris Lattner | 1e4d21e | 2007-08-26 22:58:05 +0000 | [diff] [blame] | 45 | if (!hasAggregateLLVMType(E->getType())) |
| 46 | EmitScalarExpr(E); |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 47 | else if (E->getType()->isAnyComplexType()) |
Chris Lattner | 1e4d21e | 2007-08-26 22:58:05 +0000 | [diff] [blame] | 48 | EmitComplexExpr(E); |
| 49 | else |
| 50 | EmitAggExpr(E, 0, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 51 | } else { |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 52 | ErrorUnsupported(S, "statement"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 53 | } |
| 54 | break; |
| 55 | case Stmt::NullStmtClass: break; |
| 56 | case Stmt::CompoundStmtClass: EmitCompoundStmt(cast<CompoundStmt>(*S)); break; |
| 57 | case Stmt::LabelStmtClass: EmitLabelStmt(cast<LabelStmt>(*S)); break; |
| 58 | case Stmt::GotoStmtClass: EmitGotoStmt(cast<GotoStmt>(*S)); break; |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 59 | case Stmt::IndirectGotoStmtClass: |
| 60 | EmitIndirectGotoStmt(cast<IndirectGotoStmt>(*S)); break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 61 | |
| 62 | case Stmt::IfStmtClass: EmitIfStmt(cast<IfStmt>(*S)); break; |
| 63 | case Stmt::WhileStmtClass: EmitWhileStmt(cast<WhileStmt>(*S)); break; |
| 64 | case Stmt::DoStmtClass: EmitDoStmt(cast<DoStmt>(*S)); break; |
| 65 | case Stmt::ForStmtClass: EmitForStmt(cast<ForStmt>(*S)); break; |
| 66 | |
| 67 | case Stmt::ReturnStmtClass: EmitReturnStmt(cast<ReturnStmt>(*S)); break; |
| 68 | case Stmt::DeclStmtClass: EmitDeclStmt(cast<DeclStmt>(*S)); break; |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 69 | |
Daniel Dunbar | a4275d1 | 2008-10-02 18:02:06 +0000 | [diff] [blame] | 70 | case Stmt::BreakStmtClass: |
| 71 | // FIXME: Implement break in @try or @catch blocks. |
| 72 | if (!ObjCEHStack.empty()) { |
| 73 | CGM.ErrorUnsupported(S, "continue inside an Obj-C exception block"); |
| 74 | return; |
| 75 | } |
| 76 | EmitBreakStmt(); |
| 77 | break; |
| 78 | |
| 79 | case Stmt::ContinueStmtClass: |
| 80 | // FIXME: Implement continue in @try or @catch blocks. |
| 81 | if (!ObjCEHStack.empty()) { |
| 82 | CGM.ErrorUnsupported(S, "continue inside an Obj-C exception block"); |
| 83 | return; |
| 84 | } |
| 85 | EmitContinueStmt(); |
| 86 | break; |
| 87 | |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 88 | case Stmt::SwitchStmtClass: EmitSwitchStmt(cast<SwitchStmt>(*S)); break; |
| 89 | case Stmt::DefaultStmtClass: EmitDefaultStmt(cast<DefaultStmt>(*S)); break; |
| 90 | case Stmt::CaseStmtClass: EmitCaseStmt(cast<CaseStmt>(*S)); break; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 91 | case Stmt::AsmStmtClass: EmitAsmStmt(cast<AsmStmt>(*S)); break; |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 92 | |
| 93 | case Stmt::ObjCAtTryStmtClass: |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 94 | EmitObjCAtTryStmt(cast<ObjCAtTryStmt>(*S)); |
| 95 | break; |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 96 | case Stmt::ObjCAtCatchStmtClass: |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 97 | assert(0 && "@catch statements should be handled by EmitObjCAtTryStmt"); |
| 98 | break; |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 99 | case Stmt::ObjCAtFinallyStmtClass: |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 100 | assert(0 && "@finally statements should be handled by EmitObjCAtTryStmt"); |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 101 | break; |
| 102 | case Stmt::ObjCAtThrowStmtClass: |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 103 | EmitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(*S)); |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 104 | break; |
| 105 | case Stmt::ObjCAtSynchronizedStmtClass: |
| 106 | ErrorUnsupported(S, "@synchronized statement"); |
| 107 | break; |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 108 | case Stmt::ObjCForCollectionStmtClass: |
| 109 | EmitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(*S)); |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 110 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 114 | /// EmitCompoundStmt - Emit a compound statement {..} node. If GetLast is true, |
| 115 | /// this captures the expression result of the last sub-statement and returns it |
| 116 | /// (for use by the statement expression extension). |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 117 | RValue CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast, |
| 118 | llvm::Value *AggLoc, bool isAggVol) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 119 | // FIXME: handle vla's etc. |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 120 | CGDebugInfo *DI = CGM.getDebugInfo(); |
| 121 | if (DI) { |
Daniel Dunbar | 66031a5 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 122 | DI->setLocation(S.getLBracLoc()); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 123 | DI->EmitRegionStart(CurFn, Builder); |
| 124 | } |
| 125 | |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 126 | for (CompoundStmt::const_body_iterator I = S.body_begin(), |
| 127 | E = S.body_end()-GetLast; I != E; ++I) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 128 | EmitStmt(*I); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 129 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 130 | if (DI) { |
Daniel Dunbar | 66031a5 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 131 | DI->setLocation(S.getRBracLoc()); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 132 | DI->EmitRegionEnd(CurFn, Builder); |
| 133 | } |
| 134 | |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 135 | if (!GetLast) |
| 136 | return RValue::get(0); |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 137 | |
Chris Lattner | 91d723d | 2008-07-26 20:23:23 +0000 | [diff] [blame] | 138 | // We have to special case labels here. They are statements, but when put at |
| 139 | // the end of a statement expression, they yield the value of their |
| 140 | // subexpression. Handle this by walking through all labels we encounter, |
| 141 | // emitting them before we evaluate the subexpr. |
| 142 | const Stmt *LastStmt = S.body_back(); |
| 143 | while (const LabelStmt *LS = dyn_cast<LabelStmt>(LastStmt)) { |
| 144 | EmitLabel(*LS); |
| 145 | LastStmt = LS->getSubStmt(); |
| 146 | } |
| 147 | |
| 148 | return EmitAnyExpr(cast<Expr>(LastStmt), AggLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void CodeGenFunction::EmitBlock(llvm::BasicBlock *BB) { |
| 152 | // Emit a branch from this block to the next one if this was a real block. If |
| 153 | // this was just a fall-through block after a terminator, don't emit it. |
| 154 | llvm::BasicBlock *LastBB = Builder.GetInsertBlock(); |
| 155 | |
| 156 | if (LastBB->getTerminator()) { |
| 157 | // If the previous block is already terminated, don't touch it. |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 158 | } else if (LastBB->empty() && isDummyBlock(LastBB)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 159 | // If the last block was an empty placeholder, remove it now. |
| 160 | // TODO: cache and reuse these. |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 161 | LastBB->eraseFromParent(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 162 | } else { |
| 163 | // Otherwise, create a fall-through branch. |
| 164 | Builder.CreateBr(BB); |
| 165 | } |
| 166 | CurFn->getBasicBlockList().push_back(BB); |
| 167 | Builder.SetInsertPoint(BB); |
| 168 | } |
| 169 | |
Chris Lattner | 91d723d | 2008-07-26 20:23:23 +0000 | [diff] [blame] | 170 | void CodeGenFunction::EmitLabel(const LabelStmt &S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 171 | llvm::BasicBlock *NextBB = getBasicBlockForLabel(&S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 172 | EmitBlock(NextBB); |
Chris Lattner | 91d723d | 2008-07-26 20:23:23 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | |
| 176 | void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) { |
| 177 | EmitLabel(S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 178 | EmitStmt(S.getSubStmt()); |
| 179 | } |
| 180 | |
| 181 | void CodeGenFunction::EmitGotoStmt(const GotoStmt &S) { |
Daniel Dunbar | a4275d1 | 2008-10-02 18:02:06 +0000 | [diff] [blame] | 182 | // FIXME: Implement goto out in @try or @catch blocks. |
| 183 | if (!ObjCEHStack.empty()) { |
| 184 | CGM.ErrorUnsupported(&S, "goto inside an Obj-C exception block"); |
| 185 | return; |
| 186 | } |
| 187 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 188 | Builder.CreateBr(getBasicBlockForLabel(S.getLabel())); |
| 189 | |
| 190 | // Emit a block after the branch so that dead code after a goto has some place |
| 191 | // to go. |
Gabor Greif | 984d0b4 | 2008-04-06 20:42:52 +0000 | [diff] [blame] | 192 | Builder.SetInsertPoint(llvm::BasicBlock::Create("", CurFn)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 195 | void CodeGenFunction::EmitIndirectGotoStmt(const IndirectGotoStmt &S) { |
Daniel Dunbar | a4275d1 | 2008-10-02 18:02:06 +0000 | [diff] [blame] | 196 | // FIXME: Implement indirect goto in @try or @catch blocks. |
| 197 | if (!ObjCEHStack.empty()) { |
| 198 | CGM.ErrorUnsupported(&S, "goto inside an Obj-C exception block"); |
| 199 | return; |
| 200 | } |
| 201 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 202 | // Emit initial switch which will be patched up later by |
| 203 | // EmitIndirectSwitches(). We need a default dest, so we use the |
| 204 | // current BB, but this is overwritten. |
| 205 | llvm::Value *V = Builder.CreatePtrToInt(EmitScalarExpr(S.getTarget()), |
| 206 | llvm::Type::Int32Ty, |
| 207 | "addr"); |
| 208 | llvm::SwitchInst *I = Builder.CreateSwitch(V, Builder.GetInsertBlock()); |
| 209 | IndirectSwitches.push_back(I); |
| 210 | |
| 211 | // Emit a block after the branch so that dead code after a goto has some place |
| 212 | // to go. |
| 213 | Builder.SetInsertPoint(llvm::BasicBlock::Create("", CurFn)); |
| 214 | } |
| 215 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 216 | void CodeGenFunction::EmitIfStmt(const IfStmt &S) { |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 217 | // FIXME: It would probably be nice for us to skip emission of if |
| 218 | // (0) code here. |
| 219 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 220 | // C99 6.8.4.1: The first substatement is executed if the expression compares |
| 221 | // unequal to 0. The condition must be a scalar type. |
| 222 | llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); |
| 223 | |
Gabor Greif | 984d0b4 | 2008-04-06 20:42:52 +0000 | [diff] [blame] | 224 | llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("ifend"); |
| 225 | llvm::BasicBlock *ThenBlock = llvm::BasicBlock::Create("ifthen"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 226 | llvm::BasicBlock *ElseBlock = ContBlock; |
| 227 | |
| 228 | if (S.getElse()) |
Gabor Greif | 984d0b4 | 2008-04-06 20:42:52 +0000 | [diff] [blame] | 229 | ElseBlock = llvm::BasicBlock::Create("ifelse"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 230 | |
| 231 | // Insert the conditional branch. |
| 232 | Builder.CreateCondBr(BoolCondVal, ThenBlock, ElseBlock); |
| 233 | |
| 234 | // Emit the 'then' code. |
| 235 | EmitBlock(ThenBlock); |
| 236 | EmitStmt(S.getThen()); |
Devang Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 237 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 238 | if (isDummyBlock(BB)) { |
| 239 | BB->eraseFromParent(); |
| 240 | Builder.SetInsertPoint(ThenBlock); |
Chris Lattner | 345f720 | 2008-07-26 20:15:14 +0000 | [diff] [blame] | 241 | } else { |
Devang Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 242 | Builder.CreateBr(ContBlock); |
Chris Lattner | 345f720 | 2008-07-26 20:15:14 +0000 | [diff] [blame] | 243 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 244 | |
| 245 | // Emit the 'else' code if present. |
| 246 | if (const Stmt *Else = S.getElse()) { |
| 247 | EmitBlock(ElseBlock); |
| 248 | EmitStmt(Else); |
Devang Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 249 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 250 | if (isDummyBlock(BB)) { |
| 251 | BB->eraseFromParent(); |
| 252 | Builder.SetInsertPoint(ElseBlock); |
Chris Lattner | 345f720 | 2008-07-26 20:15:14 +0000 | [diff] [blame] | 253 | } else { |
Devang Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 254 | Builder.CreateBr(ContBlock); |
Chris Lattner | 345f720 | 2008-07-26 20:15:14 +0000 | [diff] [blame] | 255 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | // Emit the continuation block for code after the if. |
| 259 | EmitBlock(ContBlock); |
| 260 | } |
| 261 | |
| 262 | void CodeGenFunction::EmitWhileStmt(const WhileStmt &S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 263 | // Emit the header for the loop, insert it, which will create an uncond br to |
| 264 | // it. |
Gabor Greif | 984d0b4 | 2008-04-06 20:42:52 +0000 | [diff] [blame] | 265 | llvm::BasicBlock *LoopHeader = llvm::BasicBlock::Create("whilecond"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 266 | EmitBlock(LoopHeader); |
| 267 | |
| 268 | // Evaluate the conditional in the while header. C99 6.8.5.1: The evaluation |
| 269 | // of the controlling expression takes place before each execution of the loop |
| 270 | // body. |
| 271 | llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); |
Devang Patel | 2c30d8f | 2007-10-09 20:51:27 +0000 | [diff] [blame] | 272 | |
| 273 | // while(1) is common, avoid extra exit blocks. Be sure |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 274 | // to correctly handle break/continue though. |
Devang Patel | 2c30d8f | 2007-10-09 20:51:27 +0000 | [diff] [blame] | 275 | bool EmitBoolCondBranch = true; |
| 276 | if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal)) |
| 277 | if (C->isOne()) |
| 278 | EmitBoolCondBranch = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 279 | |
| 280 | // Create an exit block for when the condition fails, create a block for the |
| 281 | // body of the loop. |
Gabor Greif | 984d0b4 | 2008-04-06 20:42:52 +0000 | [diff] [blame] | 282 | llvm::BasicBlock *ExitBlock = llvm::BasicBlock::Create("whileexit"); |
| 283 | llvm::BasicBlock *LoopBody = llvm::BasicBlock::Create("whilebody"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 284 | |
| 285 | // As long as the condition is true, go to the loop body. |
Devang Patel | 2c30d8f | 2007-10-09 20:51:27 +0000 | [diff] [blame] | 286 | if (EmitBoolCondBranch) |
| 287 | Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 288 | |
| 289 | // Store the blocks to use for break and continue. |
| 290 | BreakContinueStack.push_back(BreakContinue(ExitBlock, LoopHeader)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 291 | |
| 292 | // Emit the loop body. |
| 293 | EmitBlock(LoopBody); |
| 294 | EmitStmt(S.getBody()); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 295 | |
| 296 | BreakContinueStack.pop_back(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 297 | |
| 298 | // Cycle to the condition. |
| 299 | Builder.CreateBr(LoopHeader); |
| 300 | |
| 301 | // Emit the exit block. |
| 302 | EmitBlock(ExitBlock); |
Devang Patel | 2c30d8f | 2007-10-09 20:51:27 +0000 | [diff] [blame] | 303 | |
| 304 | // If LoopHeader is a simple forwarding block then eliminate it. |
| 305 | if (!EmitBoolCondBranch |
| 306 | && &LoopHeader->front() == LoopHeader->getTerminator()) { |
| 307 | LoopHeader->replaceAllUsesWith(LoopBody); |
| 308 | LoopHeader->getTerminator()->eraseFromParent(); |
| 309 | LoopHeader->eraseFromParent(); |
| 310 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | void CodeGenFunction::EmitDoStmt(const DoStmt &S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 314 | // Emit the body for the loop, insert it, which will create an uncond br to |
| 315 | // it. |
Gabor Greif | 984d0b4 | 2008-04-06 20:42:52 +0000 | [diff] [blame] | 316 | llvm::BasicBlock *LoopBody = llvm::BasicBlock::Create("dobody"); |
| 317 | llvm::BasicBlock *AfterDo = llvm::BasicBlock::Create("afterdo"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 318 | EmitBlock(LoopBody); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 319 | |
Gabor Greif | 984d0b4 | 2008-04-06 20:42:52 +0000 | [diff] [blame] | 320 | llvm::BasicBlock *DoCond = llvm::BasicBlock::Create("docond"); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 321 | |
| 322 | // Store the blocks to use for break and continue. |
| 323 | BreakContinueStack.push_back(BreakContinue(AfterDo, DoCond)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 324 | |
| 325 | // Emit the body of the loop into the block. |
| 326 | EmitStmt(S.getBody()); |
| 327 | |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 328 | BreakContinueStack.pop_back(); |
| 329 | |
| 330 | EmitBlock(DoCond); |
| 331 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 332 | // C99 6.8.5.2: "The evaluation of the controlling expression takes place |
| 333 | // after each execution of the loop body." |
| 334 | |
| 335 | // Evaluate the conditional in the while header. |
| 336 | // C99 6.8.5p2/p4: The first substatement is executed if the expression |
| 337 | // compares unequal to 0. The condition must be a scalar type. |
| 338 | llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); |
Devang Patel | 05f6e6b | 2007-10-09 20:33:39 +0000 | [diff] [blame] | 339 | |
| 340 | // "do {} while (0)" is common in macros, avoid extra blocks. Be sure |
| 341 | // to correctly handle break/continue though. |
| 342 | bool EmitBoolCondBranch = true; |
| 343 | if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal)) |
| 344 | if (C->isZero()) |
| 345 | EmitBoolCondBranch = false; |
| 346 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 347 | // As long as the condition is true, iterate the loop. |
Devang Patel | 05f6e6b | 2007-10-09 20:33:39 +0000 | [diff] [blame] | 348 | if (EmitBoolCondBranch) |
| 349 | Builder.CreateCondBr(BoolCondVal, LoopBody, AfterDo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 350 | |
| 351 | // Emit the exit block. |
| 352 | EmitBlock(AfterDo); |
Devang Patel | 05f6e6b | 2007-10-09 20:33:39 +0000 | [diff] [blame] | 353 | |
| 354 | // If DoCond is a simple forwarding block then eliminate it. |
| 355 | if (!EmitBoolCondBranch && &DoCond->front() == DoCond->getTerminator()) { |
| 356 | DoCond->replaceAllUsesWith(AfterDo); |
| 357 | DoCond->getTerminator()->eraseFromParent(); |
| 358 | DoCond->eraseFromParent(); |
| 359 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | void CodeGenFunction::EmitForStmt(const ForStmt &S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 363 | // FIXME: What do we do if the increment (f.e.) contains a stmt expression, |
| 364 | // which contains a continue/break? |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 365 | // TODO: We could keep track of whether the loop body contains any |
| 366 | // break/continue statements and not create unnecessary blocks (like |
| 367 | // "afterfor" for a condless loop) if it doesn't. |
| 368 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 369 | // Evaluate the first part before the loop. |
| 370 | if (S.getInit()) |
| 371 | EmitStmt(S.getInit()); |
| 372 | |
| 373 | // Start the loop with a block that tests the condition. |
Gabor Greif | 984d0b4 | 2008-04-06 20:42:52 +0000 | [diff] [blame] | 374 | llvm::BasicBlock *CondBlock = llvm::BasicBlock::Create("forcond"); |
| 375 | llvm::BasicBlock *AfterFor = llvm::BasicBlock::Create("afterfor"); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 376 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 377 | EmitBlock(CondBlock); |
| 378 | |
| 379 | // Evaluate the condition if present. If not, treat it as a non-zero-constant |
| 380 | // according to 6.8.5.3p2, aka, true. |
| 381 | if (S.getCond()) { |
| 382 | // C99 6.8.5p2/p4: The first substatement is executed if the expression |
| 383 | // compares unequal to 0. The condition must be a scalar type. |
| 384 | llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); |
| 385 | |
| 386 | // As long as the condition is true, iterate the loop. |
Gabor Greif | 984d0b4 | 2008-04-06 20:42:52 +0000 | [diff] [blame] | 387 | llvm::BasicBlock *ForBody = llvm::BasicBlock::Create("forbody"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 388 | Builder.CreateCondBr(BoolCondVal, ForBody, AfterFor); |
| 389 | EmitBlock(ForBody); |
| 390 | } else { |
| 391 | // Treat it as a non-zero constant. Don't even create a new block for the |
| 392 | // body, just fall into it. |
| 393 | } |
| 394 | |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 395 | // If the for loop doesn't have an increment we can just use the |
| 396 | // condition as the continue block. |
| 397 | llvm::BasicBlock *ContinueBlock; |
| 398 | if (S.getInc()) |
Gabor Greif | 984d0b4 | 2008-04-06 20:42:52 +0000 | [diff] [blame] | 399 | ContinueBlock = llvm::BasicBlock::Create("forinc"); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 400 | else |
| 401 | ContinueBlock = CondBlock; |
| 402 | |
| 403 | // Store the blocks to use for break and continue. |
| 404 | BreakContinueStack.push_back(BreakContinue(AfterFor, ContinueBlock)); |
| 405 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 406 | // If the condition is true, execute the body of the for stmt. |
| 407 | EmitStmt(S.getBody()); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 408 | |
| 409 | BreakContinueStack.pop_back(); |
| 410 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 411 | // If there is an increment, emit it next. |
Daniel Dunbar | ad12b6d | 2008-09-28 00:19:22 +0000 | [diff] [blame] | 412 | if (S.getInc()) { |
| 413 | EmitBlock(ContinueBlock); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 414 | EmitStmt(S.getInc()); |
Daniel Dunbar | ad12b6d | 2008-09-28 00:19:22 +0000 | [diff] [blame] | 415 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 416 | |
| 417 | // Finally, branch back up to the condition for the next iteration. |
| 418 | Builder.CreateBr(CondBlock); |
| 419 | |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 420 | // Emit the fall-through block. |
| 421 | EmitBlock(AfterFor); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Daniel Dunbar | 29e0bcc | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 424 | void CodeGenFunction::EmitReturnOfRValue(RValue RV, QualType Ty) { |
| 425 | if (RV.isScalar()) { |
| 426 | Builder.CreateStore(RV.getScalarVal(), ReturnValue); |
| 427 | } else if (RV.isAggregate()) { |
| 428 | EmitAggregateCopy(ReturnValue, RV.getAggregateAddr(), Ty); |
| 429 | } else { |
| 430 | StoreComplexToAddr(RV.getComplexVal(), ReturnValue, false); |
| 431 | } |
| 432 | Builder.CreateBr(ReturnBlock); |
| 433 | |
| 434 | // Emit a block after the branch so that dead code after a return has some |
| 435 | // place to go. |
| 436 | EmitBlock(llvm::BasicBlock::Create()); |
| 437 | } |
| 438 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 439 | /// EmitReturnStmt - Note that due to GCC extensions, this can have an operand |
| 440 | /// if the function returns void, or may be missing one if the function returns |
| 441 | /// non-void. Fun stuff :). |
| 442 | void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 443 | // Emit the result value, even if unused, to evalute the side effects. |
| 444 | const Expr *RV = S.getRetValue(); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 445 | |
| 446 | // FIXME: Clean this up by using an LValue for ReturnTemp, |
| 447 | // EmitStoreThroughLValue, and EmitAnyExpr. |
| 448 | if (!ReturnValue) { |
| 449 | // Make sure not to return anything, but evaluate the expression |
| 450 | // for side effects. |
| 451 | if (RV) |
Eli Friedman | 144ac61 | 2008-05-22 01:22:33 +0000 | [diff] [blame] | 452 | EmitAnyExpr(RV); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 453 | } else if (RV == 0) { |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 454 | // Do nothing (return value is left uninitialized) |
Chris Lattner | 4b0029d | 2007-08-26 07:14:44 +0000 | [diff] [blame] | 455 | } else if (!hasAggregateLLVMType(RV->getType())) { |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 456 | Builder.CreateStore(EmitScalarExpr(RV), ReturnValue); |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 457 | } else if (RV->getType()->isAnyComplexType()) { |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 458 | EmitComplexExprIntoAddr(RV, ReturnValue, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 459 | } else { |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 460 | EmitAggExpr(RV, ReturnValue, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 461 | } |
Eli Friedman | 144ac61 | 2008-05-22 01:22:33 +0000 | [diff] [blame] | 462 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 463 | if (!ObjCEHStack.empty()) { |
| 464 | for (ObjCEHStackType::reverse_iterator i = ObjCEHStack.rbegin(), |
| 465 | e = ObjCEHStack.rend(); i != e; ++i) { |
| 466 | llvm::BasicBlock *ReturnPad = llvm::BasicBlock::Create("return.pad"); |
| 467 | EmitJumpThroughFinally(*i, ReturnPad); |
| 468 | EmitBlock(ReturnPad); |
| 469 | } |
| 470 | } |
| 471 | |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 472 | Builder.CreateBr(ReturnBlock); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 473 | |
| 474 | // Emit a block after the branch so that dead code after a return has some |
| 475 | // place to go. |
Gabor Greif | 984d0b4 | 2008-04-06 20:42:52 +0000 | [diff] [blame] | 476 | EmitBlock(llvm::BasicBlock::Create()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | void CodeGenFunction::EmitDeclStmt(const DeclStmt &S) { |
Ted Kremenek | e4ea1f4 | 2008-10-06 18:42:27 +0000 | [diff] [blame] | 480 | for (DeclStmt::const_decl_iterator I = S.decl_begin(), E = S.decl_end(); |
| 481 | I != E; ++I) |
| 482 | EmitDecl(**I); |
Chris Lattner | 6fa5f09 | 2007-07-12 15:43:07 +0000 | [diff] [blame] | 483 | } |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 484 | |
| 485 | void CodeGenFunction::EmitBreakStmt() { |
| 486 | assert(!BreakContinueStack.empty() && "break stmt not in a loop or switch!"); |
| 487 | |
| 488 | llvm::BasicBlock *Block = BreakContinueStack.back().BreakBlock; |
| 489 | Builder.CreateBr(Block); |
Gabor Greif | 984d0b4 | 2008-04-06 20:42:52 +0000 | [diff] [blame] | 490 | EmitBlock(llvm::BasicBlock::Create()); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | void CodeGenFunction::EmitContinueStmt() { |
| 494 | assert(!BreakContinueStack.empty() && "continue stmt not in a loop!"); |
| 495 | |
| 496 | llvm::BasicBlock *Block = BreakContinueStack.back().ContinueBlock; |
| 497 | Builder.CreateBr(Block); |
Gabor Greif | 984d0b4 | 2008-04-06 20:42:52 +0000 | [diff] [blame] | 498 | EmitBlock(llvm::BasicBlock::Create()); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 499 | } |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 500 | |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 501 | /// EmitCaseStmtRange - If case statement range is not too big then |
| 502 | /// add multiple cases to switch instruction, one for each value within |
| 503 | /// the range. If range is too big then emit "if" condition check. |
| 504 | void CodeGenFunction::EmitCaseStmtRange(const CaseStmt &S) { |
Daniel Dunbar | 4efde8d | 2008-07-24 01:18:41 +0000 | [diff] [blame] | 505 | assert(S.getRHS() && "Expected RHS value in CaseStmt"); |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 506 | |
Daniel Dunbar | 4efde8d | 2008-07-24 01:18:41 +0000 | [diff] [blame] | 507 | llvm::APSInt LHS = S.getLHS()->getIntegerConstantExprValue(getContext()); |
| 508 | llvm::APSInt RHS = S.getRHS()->getIntegerConstantExprValue(getContext()); |
| 509 | |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 510 | // Emit the code for this case. We do this first to make sure it is |
| 511 | // properly chained from our predecessor before generating the |
| 512 | // switch machinery to enter this block. |
| 513 | StartBlock("sw.bb"); |
| 514 | llvm::BasicBlock *CaseDest = Builder.GetInsertBlock(); |
| 515 | EmitStmt(S.getSubStmt()); |
| 516 | |
Daniel Dunbar | 4efde8d | 2008-07-24 01:18:41 +0000 | [diff] [blame] | 517 | // If range is empty, do nothing. |
| 518 | if (LHS.isSigned() ? RHS.slt(LHS) : RHS.ult(LHS)) |
| 519 | return; |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 520 | |
| 521 | llvm::APInt Range = RHS - LHS; |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 522 | // FIXME: parameters such as this should not be hardcoded. |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 523 | if (Range.ult(llvm::APInt(Range.getBitWidth(), 64))) { |
| 524 | // Range is small enough to add multiple switch instruction cases. |
Daniel Dunbar | 4efde8d | 2008-07-24 01:18:41 +0000 | [diff] [blame] | 525 | for (unsigned i = 0, e = Range.getZExtValue() + 1; i != e; ++i) { |
Devang Patel | 2d79d0f | 2007-10-05 20:54:07 +0000 | [diff] [blame] | 526 | SwitchInsn->addCase(llvm::ConstantInt::get(LHS), CaseDest); |
| 527 | LHS++; |
| 528 | } |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 529 | return; |
| 530 | } |
| 531 | |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 532 | // The range is too big. Emit "if" condition into a new block, |
| 533 | // making sure to save and restore the current insertion point. |
| 534 | llvm::BasicBlock *RestoreBB = Builder.GetInsertBlock(); |
Devang Patel | 2d79d0f | 2007-10-05 20:54:07 +0000 | [diff] [blame] | 535 | |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 536 | // Push this test onto the chain of range checks (which terminates |
| 537 | // in the default basic block). The switch's default will be changed |
| 538 | // to the top of this chain after switch emission is complete. |
| 539 | llvm::BasicBlock *FalseDest = CaseRangeBlock; |
| 540 | CaseRangeBlock = llvm::BasicBlock::Create("sw.caserange"); |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 541 | |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 542 | CurFn->getBasicBlockList().push_back(CaseRangeBlock); |
| 543 | Builder.SetInsertPoint(CaseRangeBlock); |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 544 | |
| 545 | // Emit range check. |
| 546 | llvm::Value *Diff = |
Daniel Dunbar | 4efde8d | 2008-07-24 01:18:41 +0000 | [diff] [blame] | 547 | Builder.CreateSub(SwitchInsn->getCondition(), llvm::ConstantInt::get(LHS), |
| 548 | "tmp"); |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 549 | llvm::Value *Cond = |
| 550 | Builder.CreateICmpULE(Diff, llvm::ConstantInt::get(Range), "tmp"); |
| 551 | Builder.CreateCondBr(Cond, CaseDest, FalseDest); |
| 552 | |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 553 | // Restore the appropriate insertion point. |
| 554 | Builder.SetInsertPoint(RestoreBB); |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | void CodeGenFunction::EmitCaseStmt(const CaseStmt &S) { |
| 558 | if (S.getRHS()) { |
| 559 | EmitCaseStmtRange(S); |
| 560 | return; |
| 561 | } |
| 562 | |
| 563 | StartBlock("sw.bb"); |
| 564 | llvm::BasicBlock *CaseDest = Builder.GetInsertBlock(); |
Daniel Dunbar | 4efde8d | 2008-07-24 01:18:41 +0000 | [diff] [blame] | 565 | llvm::APSInt CaseVal = S.getLHS()->getIntegerConstantExprValue(getContext()); |
| 566 | SwitchInsn->addCase(llvm::ConstantInt::get(CaseVal), |
| 567 | CaseDest); |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 568 | EmitStmt(S.getSubStmt()); |
| 569 | } |
| 570 | |
| 571 | void CodeGenFunction::EmitDefaultStmt(const DefaultStmt &S) { |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 572 | llvm::BasicBlock *DefaultBlock = SwitchInsn->getDefaultDest(); |
| 573 | assert(DefaultBlock->empty() && "EmitDefaultStmt: Default block already defined?"); |
| 574 | EmitBlock(DefaultBlock); |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 575 | EmitStmt(S.getSubStmt()); |
| 576 | } |
| 577 | |
| 578 | void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) { |
| 579 | llvm::Value *CondV = EmitScalarExpr(S.getCond()); |
| 580 | |
| 581 | // Handle nested switch statements. |
| 582 | llvm::SwitchInst *SavedSwitchInsn = SwitchInsn; |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 583 | llvm::BasicBlock *SavedCRBlock = CaseRangeBlock; |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 584 | |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 585 | // Create basic block to hold stuff that comes after switch |
| 586 | // statement. We also need to create a default block now so that |
| 587 | // explicit case ranges tests can have a place to jump to on |
| 588 | // failure. |
| 589 | llvm::BasicBlock *NextBlock = llvm::BasicBlock::Create("sw.epilog"); |
| 590 | llvm::BasicBlock *DefaultBlock = llvm::BasicBlock::Create("sw.default"); |
| 591 | SwitchInsn = Builder.CreateSwitch(CondV, DefaultBlock); |
| 592 | CaseRangeBlock = DefaultBlock; |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 593 | |
Eli Friedman | d28a80d | 2008-05-12 16:08:04 +0000 | [diff] [blame] | 594 | // Create basic block for body of switch |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 595 | StartBlock("sw.body"); |
Eli Friedman | d28a80d | 2008-05-12 16:08:04 +0000 | [diff] [blame] | 596 | |
Devang Patel | e9b8c0a | 2007-10-30 20:59:40 +0000 | [diff] [blame] | 597 | // All break statements jump to NextBlock. If BreakContinueStack is non empty |
| 598 | // then reuse last ContinueBlock. |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 599 | llvm::BasicBlock *ContinueBlock = NULL; |
| 600 | if (!BreakContinueStack.empty()) |
| 601 | ContinueBlock = BreakContinueStack.back().ContinueBlock; |
| 602 | BreakContinueStack.push_back(BreakContinue(NextBlock, ContinueBlock)); |
| 603 | |
| 604 | // Emit switch body. |
| 605 | EmitStmt(S.getBody()); |
| 606 | BreakContinueStack.pop_back(); |
| 607 | |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 608 | // Update the default block in case explicit case range tests have |
| 609 | // been chained on top. |
| 610 | SwitchInsn->setSuccessor(0, CaseRangeBlock); |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 611 | |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 612 | // If a default was never emitted then reroute any jumps to it and |
| 613 | // discard. |
| 614 | if (!DefaultBlock->getParent()) { |
| 615 | DefaultBlock->replaceAllUsesWith(NextBlock); |
| 616 | delete DefaultBlock; |
| 617 | } |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 618 | |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 619 | // Emit continuation. |
| 620 | EmitBlock(NextBlock); |
| 621 | |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 622 | SwitchInsn = SavedSwitchInsn; |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 623 | CaseRangeBlock = SavedCRBlock; |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 624 | } |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 625 | |
Chris Lattner | 345f720 | 2008-07-26 20:15:14 +0000 | [diff] [blame] | 626 | static std::string ConvertAsmString(const char *Start, unsigned NumOperands, |
| 627 | bool IsSimple) { |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 628 | static unsigned AsmCounter = 0; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 629 | AsmCounter++; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 630 | std::string Result; |
Anders Carlsson | 2abd25f | 2008-02-05 23:18:57 +0000 | [diff] [blame] | 631 | if (IsSimple) { |
| 632 | while (*Start) { |
| 633 | switch (*Start) { |
| 634 | default: |
| 635 | Result += *Start; |
| 636 | break; |
| 637 | case '$': |
| 638 | Result += "$$"; |
| 639 | break; |
| 640 | } |
Anders Carlsson | 2abd25f | 2008-02-05 23:18:57 +0000 | [diff] [blame] | 641 | Start++; |
| 642 | } |
| 643 | |
| 644 | return Result; |
| 645 | } |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 646 | |
| 647 | while (*Start) { |
| 648 | switch (*Start) { |
| 649 | default: |
| 650 | Result += *Start; |
| 651 | break; |
| 652 | case '$': |
| 653 | Result += "$$"; |
| 654 | break; |
| 655 | case '%': |
| 656 | // Escaped character |
| 657 | Start++; |
| 658 | if (!*Start) { |
| 659 | // FIXME: This should be caught during Sema. |
| 660 | assert(0 && "Trailing '%' in asm string."); |
| 661 | } |
| 662 | |
| 663 | char EscapedChar = *Start; |
| 664 | if (EscapedChar == '%') { |
| 665 | // Escaped percentage sign. |
| 666 | Result += '%'; |
Chris Lattner | 345f720 | 2008-07-26 20:15:14 +0000 | [diff] [blame] | 667 | } else if (EscapedChar == '=') { |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 668 | // Generate an unique ID. |
| 669 | Result += llvm::utostr(AsmCounter); |
| 670 | } else if (isdigit(EscapedChar)) { |
| 671 | // %n - Assembler operand n |
| 672 | char *End; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 673 | unsigned long n = strtoul(Start, &End, 10); |
| 674 | if (Start == End) { |
| 675 | // FIXME: This should be caught during Sema. |
| 676 | assert(0 && "Missing operand!"); |
| 677 | } else if (n >= NumOperands) { |
| 678 | // FIXME: This should be caught during Sema. |
| 679 | assert(0 && "Operand number out of range!"); |
| 680 | } |
| 681 | |
| 682 | Result += '$' + llvm::utostr(n); |
Lauro Ramos Venancio | 7695f70 | 2008-02-26 19:19:58 +0000 | [diff] [blame] | 683 | Start = End - 1; |
Anders Carlsson | 2abd25f | 2008-02-05 23:18:57 +0000 | [diff] [blame] | 684 | } else if (isalpha(EscapedChar)) { |
| 685 | char *End; |
| 686 | |
| 687 | unsigned long n = strtoul(Start + 1, &End, 10); |
| 688 | if (Start == End) { |
| 689 | // FIXME: This should be caught during Sema. |
| 690 | assert(0 && "Missing operand!"); |
| 691 | } else if (n >= NumOperands) { |
| 692 | // FIXME: This should be caught during Sema. |
| 693 | assert(0 && "Operand number out of range!"); |
| 694 | } |
| 695 | |
| 696 | Result += "${" + llvm::utostr(n) + ':' + EscapedChar + '}'; |
Lauro Ramos Venancio | 7695f70 | 2008-02-26 19:19:58 +0000 | [diff] [blame] | 697 | Start = End - 1; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 698 | } else { |
| 699 | assert(0 && "Unhandled asm escaped character!"); |
| 700 | } |
| 701 | } |
| 702 | Start++; |
| 703 | } |
| 704 | |
| 705 | return Result; |
| 706 | } |
| 707 | |
Lauro Ramos Venancio | a5694b8 | 2008-02-26 18:33:46 +0000 | [diff] [blame] | 708 | static std::string SimplifyConstraint(const char* Constraint, |
| 709 | TargetInfo &Target) { |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 710 | std::string Result; |
| 711 | |
| 712 | while (*Constraint) { |
| 713 | switch (*Constraint) { |
| 714 | default: |
Lauro Ramos Venancio | a5694b8 | 2008-02-26 18:33:46 +0000 | [diff] [blame] | 715 | Result += Target.convertConstraint(*Constraint); |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 716 | break; |
| 717 | // Ignore these |
| 718 | case '*': |
| 719 | case '?': |
| 720 | case '!': |
| 721 | break; |
| 722 | case 'g': |
| 723 | Result += "imr"; |
| 724 | break; |
| 725 | } |
| 726 | |
| 727 | Constraint++; |
| 728 | } |
| 729 | |
| 730 | return Result; |
| 731 | } |
| 732 | |
| 733 | void CodeGenFunction::EmitAsmStmt(const AsmStmt &S) { |
| 734 | std::string AsmString = |
| 735 | ConvertAsmString(std::string(S.getAsmString()->getStrData(), |
| 736 | S.getAsmString()->getByteLength()).c_str(), |
Anders Carlsson | 2abd25f | 2008-02-05 23:18:57 +0000 | [diff] [blame] | 737 | S.getNumOutputs() + S.getNumInputs(), S.isSimple()); |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 738 | |
| 739 | std::string Constraints; |
| 740 | |
| 741 | llvm::Value *ResultAddr = 0; |
| 742 | const llvm::Type *ResultType = llvm::Type::VoidTy; |
| 743 | |
| 744 | std::vector<const llvm::Type*> ArgTypes; |
| 745 | std::vector<llvm::Value*> Args; |
Anders Carlsson | f39a421 | 2008-02-05 20:01:53 +0000 | [diff] [blame] | 746 | |
| 747 | // Keep track of inout constraints. |
| 748 | std::string InOutConstraints; |
| 749 | std::vector<llvm::Value*> InOutArgs; |
| 750 | std::vector<const llvm::Type*> InOutArgTypes; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 751 | |
| 752 | for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) { |
| 753 | std::string OutputConstraint(S.getOutputConstraint(i)->getStrData(), |
| 754 | S.getOutputConstraint(i)->getByteLength()); |
| 755 | |
| 756 | TargetInfo::ConstraintInfo Info; |
| 757 | bool result = Target.validateOutputConstraint(OutputConstraint.c_str(), |
| 758 | Info); |
Chris Lattner | 3304e55 | 2008-10-12 00:31:50 +0000 | [diff] [blame] | 759 | assert(result && "Failed to parse output constraint"); result=result; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 760 | |
| 761 | // Simplify the output constraint. |
Lauro Ramos Venancio | a5694b8 | 2008-02-26 18:33:46 +0000 | [diff] [blame] | 762 | OutputConstraint = SimplifyConstraint(OutputConstraint.c_str() + 1, Target); |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 763 | |
| 764 | LValue Dest = EmitLValue(S.getOutputExpr(i)); |
| 765 | const llvm::Type *DestValueType = |
| 766 | cast<llvm::PointerType>(Dest.getAddress()->getType())->getElementType(); |
| 767 | |
| 768 | // If the first output operand is not a memory dest, we'll |
| 769 | // make it the return value. |
| 770 | if (i == 0 && !(Info & TargetInfo::CI_AllowsMemory) && |
Dan Gohman | d79a726 | 2008-05-22 22:12:56 +0000 | [diff] [blame] | 771 | DestValueType->isSingleValueType()) { |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 772 | ResultAddr = Dest.getAddress(); |
| 773 | ResultType = DestValueType; |
| 774 | Constraints += "=" + OutputConstraint; |
| 775 | } else { |
| 776 | ArgTypes.push_back(Dest.getAddress()->getType()); |
Anders Carlsson | cad3ab6 | 2008-02-05 16:57:38 +0000 | [diff] [blame] | 777 | Args.push_back(Dest.getAddress()); |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 778 | if (i != 0) |
| 779 | Constraints += ','; |
Anders Carlsson | f39a421 | 2008-02-05 20:01:53 +0000 | [diff] [blame] | 780 | Constraints += "=*"; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 781 | Constraints += OutputConstraint; |
Anders Carlsson | f39a421 | 2008-02-05 20:01:53 +0000 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | if (Info & TargetInfo::CI_ReadWrite) { |
| 785 | // FIXME: This code should be shared with the code that handles inputs. |
| 786 | InOutConstraints += ','; |
| 787 | |
| 788 | const Expr *InputExpr = S.getOutputExpr(i); |
| 789 | llvm::Value *Arg; |
| 790 | if ((Info & TargetInfo::CI_AllowsRegister) || |
| 791 | !(Info & TargetInfo::CI_AllowsMemory)) { |
Dan Gohman | d79a726 | 2008-05-22 22:12:56 +0000 | [diff] [blame] | 792 | if (ConvertType(InputExpr->getType())->isSingleValueType()) { |
Anders Carlsson | f39a421 | 2008-02-05 20:01:53 +0000 | [diff] [blame] | 793 | Arg = EmitScalarExpr(InputExpr); |
| 794 | } else { |
Daniel Dunbar | 662174c8 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 795 | ErrorUnsupported(&S, "asm statement passing multiple-value types as inputs"); |
Anders Carlsson | f39a421 | 2008-02-05 20:01:53 +0000 | [diff] [blame] | 796 | } |
| 797 | } else { |
| 798 | LValue Dest = EmitLValue(InputExpr); |
| 799 | Arg = Dest.getAddress(); |
| 800 | InOutConstraints += '*'; |
| 801 | } |
| 802 | |
| 803 | InOutArgTypes.push_back(Arg->getType()); |
| 804 | InOutArgs.push_back(Arg); |
| 805 | InOutConstraints += OutputConstraint; |
| 806 | } |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | unsigned NumConstraints = S.getNumOutputs() + S.getNumInputs(); |
| 810 | |
| 811 | for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) { |
| 812 | const Expr *InputExpr = S.getInputExpr(i); |
| 813 | |
| 814 | std::string InputConstraint(S.getInputConstraint(i)->getStrData(), |
| 815 | S.getInputConstraint(i)->getByteLength()); |
| 816 | |
| 817 | TargetInfo::ConstraintInfo Info; |
| 818 | bool result = Target.validateInputConstraint(InputConstraint.c_str(), |
Chris Lattner | 3304e55 | 2008-10-12 00:31:50 +0000 | [diff] [blame] | 819 | NumConstraints, Info); |
| 820 | assert(result && "Failed to parse input constraint"); result=result; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 821 | |
| 822 | if (i != 0 || S.getNumOutputs() > 0) |
| 823 | Constraints += ','; |
| 824 | |
| 825 | // Simplify the input constraint. |
Lauro Ramos Venancio | a5694b8 | 2008-02-26 18:33:46 +0000 | [diff] [blame] | 826 | InputConstraint = SimplifyConstraint(InputConstraint.c_str(), Target); |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 827 | |
| 828 | llvm::Value *Arg; |
| 829 | |
| 830 | if ((Info & TargetInfo::CI_AllowsRegister) || |
| 831 | !(Info & TargetInfo::CI_AllowsMemory)) { |
Dan Gohman | d79a726 | 2008-05-22 22:12:56 +0000 | [diff] [blame] | 832 | if (ConvertType(InputExpr->getType())->isSingleValueType()) { |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 833 | Arg = EmitScalarExpr(InputExpr); |
| 834 | } else { |
Daniel Dunbar | 662174c8 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 835 | ErrorUnsupported(&S, "asm statement passing multiple-value types as inputs"); |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 836 | } |
| 837 | } else { |
| 838 | LValue Dest = EmitLValue(InputExpr); |
| 839 | Arg = Dest.getAddress(); |
| 840 | Constraints += '*'; |
| 841 | } |
| 842 | |
| 843 | ArgTypes.push_back(Arg->getType()); |
| 844 | Args.push_back(Arg); |
| 845 | Constraints += InputConstraint; |
| 846 | } |
| 847 | |
Anders Carlsson | f39a421 | 2008-02-05 20:01:53 +0000 | [diff] [blame] | 848 | // Append the "input" part of inout constraints last. |
| 849 | for (unsigned i = 0, e = InOutArgs.size(); i != e; i++) { |
| 850 | ArgTypes.push_back(InOutArgTypes[i]); |
| 851 | Args.push_back(InOutArgs[i]); |
| 852 | } |
| 853 | Constraints += InOutConstraints; |
| 854 | |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 855 | // Clobbers |
| 856 | for (unsigned i = 0, e = S.getNumClobbers(); i != e; i++) { |
| 857 | std::string Clobber(S.getClobber(i)->getStrData(), |
| 858 | S.getClobber(i)->getByteLength()); |
| 859 | |
| 860 | Clobber = Target.getNormalizedGCCRegisterName(Clobber.c_str()); |
| 861 | |
Anders Carlsson | ea04175 | 2008-02-06 00:11:32 +0000 | [diff] [blame] | 862 | if (i != 0 || NumConstraints != 0) |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 863 | Constraints += ','; |
Anders Carlsson | ea04175 | 2008-02-06 00:11:32 +0000 | [diff] [blame] | 864 | |
| 865 | Constraints += "~{"; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 866 | Constraints += Clobber; |
Anders Carlsson | ea04175 | 2008-02-06 00:11:32 +0000 | [diff] [blame] | 867 | Constraints += '}'; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | // Add machine specific clobbers |
| 871 | if (const char *C = Target.getClobbers()) { |
| 872 | if (!Constraints.empty()) |
| 873 | Constraints += ','; |
| 874 | Constraints += C; |
| 875 | } |
Anders Carlsson | f39a421 | 2008-02-05 20:01:53 +0000 | [diff] [blame] | 876 | |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 877 | const llvm::FunctionType *FTy = |
| 878 | llvm::FunctionType::get(ResultType, ArgTypes, false); |
| 879 | |
| 880 | llvm::InlineAsm *IA = |
| 881 | llvm::InlineAsm::get(FTy, AsmString, Constraints, |
| 882 | S.isVolatile() || S.getNumOutputs() == 0); |
| 883 | llvm::Value *Result = Builder.CreateCall(IA, Args.begin(), Args.end(), ""); |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 884 | if (ResultAddr) // FIXME: volatility |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 885 | Builder.CreateStore(Result, ResultAddr); |
| 886 | } |