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/ADT/StringExtras.h" |
Anders Carlsson | 17d28a3 | 2008-12-12 05:52:00 +0000 | [diff] [blame] | 20 | #include "llvm/InlineAsm.h" |
| 21 | #include "llvm/Intrinsics.h" |
Anders Carlsson | ebaae2a | 2009-01-12 02:22:13 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetData.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | using namespace CodeGen; |
| 25 | |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | // Statement Emission |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 30 | void CodeGenFunction::EmitStopPoint(const Stmt *S) { |
| 31 | if (CGDebugInfo *DI = CGM.getDebugInfo()) { |
| 32 | DI->setLocation(S->getLocStart()); |
| 33 | DI->EmitStopPoint(CurFn, Builder); |
| 34 | } |
| 35 | } |
| 36 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 37 | void CodeGenFunction::EmitStmt(const Stmt *S) { |
| 38 | assert(S && "Null statement?"); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 39 | |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 40 | // Check if we can handle this without bothering to generate an |
| 41 | // insert point or debug info. |
| 42 | if (EmitSimpleStmt(S)) |
| 43 | return; |
| 44 | |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 45 | // If we happen to be at an unreachable point just create a dummy |
| 46 | // basic block to hold the code. We could change parts of irgen to |
| 47 | // simply not generate this code, but this situation is rare and |
| 48 | // probably not worth the effort. |
| 49 | // FIXME: Verify previous performance/effort claim. |
| 50 | EnsureInsertPoint(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 51 | |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 52 | // Generate a stoppoint if we are emitting debug info. |
| 53 | EmitStopPoint(S); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 54 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 55 | switch (S->getStmtClass()) { |
| 56 | default: |
Chris Lattner | 1e4d21e | 2007-08-26 22:58:05 +0000 | [diff] [blame] | 57 | // Must be an expression in a stmt context. Emit the value (to get |
| 58 | // side-effects) and ignore the result. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 59 | if (const Expr *E = dyn_cast<Expr>(S)) { |
Chris Lattner | 1e4d21e | 2007-08-26 22:58:05 +0000 | [diff] [blame] | 60 | if (!hasAggregateLLVMType(E->getType())) |
| 61 | EmitScalarExpr(E); |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 62 | else if (E->getType()->isAnyComplexType()) |
Chris Lattner | 1e4d21e | 2007-08-26 22:58:05 +0000 | [diff] [blame] | 63 | EmitComplexExpr(E); |
| 64 | else |
| 65 | EmitAggExpr(E, 0, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 66 | } else { |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 67 | ErrorUnsupported(S, "statement"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 68 | } |
| 69 | break; |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 70 | case Stmt::IndirectGotoStmtClass: |
| 71 | EmitIndirectGotoStmt(cast<IndirectGotoStmt>(*S)); break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 72 | |
| 73 | case Stmt::IfStmtClass: EmitIfStmt(cast<IfStmt>(*S)); break; |
| 74 | case Stmt::WhileStmtClass: EmitWhileStmt(cast<WhileStmt>(*S)); break; |
| 75 | case Stmt::DoStmtClass: EmitDoStmt(cast<DoStmt>(*S)); break; |
| 76 | case Stmt::ForStmtClass: EmitForStmt(cast<ForStmt>(*S)); break; |
| 77 | |
| 78 | case Stmt::ReturnStmtClass: EmitReturnStmt(cast<ReturnStmt>(*S)); break; |
| 79 | case Stmt::DeclStmtClass: EmitDeclStmt(cast<DeclStmt>(*S)); break; |
Daniel Dunbar | a4275d1 | 2008-10-02 18:02:06 +0000 | [diff] [blame] | 80 | |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 81 | case Stmt::SwitchStmtClass: EmitSwitchStmt(cast<SwitchStmt>(*S)); break; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 82 | case Stmt::AsmStmtClass: EmitAsmStmt(cast<AsmStmt>(*S)); break; |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 83 | |
| 84 | case Stmt::ObjCAtTryStmtClass: |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 85 | EmitObjCAtTryStmt(cast<ObjCAtTryStmt>(*S)); |
| 86 | break; |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 87 | case Stmt::ObjCAtCatchStmtClass: |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 88 | assert(0 && "@catch statements should be handled by EmitObjCAtTryStmt"); |
| 89 | break; |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 90 | case Stmt::ObjCAtFinallyStmtClass: |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 91 | assert(0 && "@finally statements should be handled by EmitObjCAtTryStmt"); |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 92 | break; |
| 93 | case Stmt::ObjCAtThrowStmtClass: |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 94 | EmitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(*S)); |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 95 | break; |
| 96 | case Stmt::ObjCAtSynchronizedStmtClass: |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 97 | EmitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(*S)); |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 98 | break; |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 99 | case Stmt::ObjCForCollectionStmtClass: |
| 100 | EmitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(*S)); |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 101 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 105 | bool CodeGenFunction::EmitSimpleStmt(const Stmt *S) { |
| 106 | switch (S->getStmtClass()) { |
| 107 | default: return false; |
| 108 | case Stmt::NullStmtClass: break; |
| 109 | case Stmt::CompoundStmtClass: EmitCompoundStmt(cast<CompoundStmt>(*S)); break; |
| 110 | case Stmt::LabelStmtClass: EmitLabelStmt(cast<LabelStmt>(*S)); break; |
| 111 | case Stmt::GotoStmtClass: EmitGotoStmt(cast<GotoStmt>(*S)); break; |
| 112 | case Stmt::BreakStmtClass: EmitBreakStmt(cast<BreakStmt>(*S)); break; |
| 113 | case Stmt::ContinueStmtClass: EmitContinueStmt(cast<ContinueStmt>(*S)); break; |
| 114 | case Stmt::DefaultStmtClass: EmitDefaultStmt(cast<DefaultStmt>(*S)); break; |
| 115 | case Stmt::CaseStmtClass: EmitCaseStmt(cast<CaseStmt>(*S)); break; |
| 116 | } |
| 117 | |
| 118 | return true; |
| 119 | } |
| 120 | |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 121 | /// EmitCompoundStmt - Emit a compound statement {..} node. If GetLast is true, |
| 122 | /// this captures the expression result of the last sub-statement and returns it |
| 123 | /// (for use by the statement expression extension). |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 124 | RValue CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast, |
| 125 | llvm::Value *AggLoc, bool isAggVol) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 126 | // FIXME: handle vla's etc. |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 127 | CGDebugInfo *DI = CGM.getDebugInfo(); |
| 128 | if (DI) { |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 129 | EnsureInsertPoint(); |
Daniel Dunbar | 66031a5 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 130 | DI->setLocation(S.getLBracLoc()); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 131 | DI->EmitRegionStart(CurFn, Builder); |
| 132 | } |
| 133 | |
Anders Carlsson | 17d28a3 | 2008-12-12 05:52:00 +0000 | [diff] [blame] | 134 | // Push a null stack save value. |
| 135 | StackSaveValues.push_back(0); |
| 136 | |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 137 | for (CompoundStmt::const_body_iterator I = S.body_begin(), |
| 138 | E = S.body_end()-GetLast; I != E; ++I) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 139 | EmitStmt(*I); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 140 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 141 | if (DI) { |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 142 | EnsureInsertPoint(); |
Daniel Dunbar | 66031a5 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 143 | DI->setLocation(S.getRBracLoc()); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 144 | DI->EmitRegionEnd(CurFn, Builder); |
| 145 | } |
| 146 | |
Anders Carlsson | 17d28a3 | 2008-12-12 05:52:00 +0000 | [diff] [blame] | 147 | RValue RV; |
| 148 | if (!GetLast) |
| 149 | RV = RValue::get(0); |
| 150 | else { |
| 151 | // We have to special case labels here. They are statements, but when put |
| 152 | // at the end of a statement expression, they yield the value of their |
| 153 | // subexpression. Handle this by walking through all labels we encounter, |
| 154 | // emitting them before we evaluate the subexpr. |
| 155 | const Stmt *LastStmt = S.body_back(); |
| 156 | while (const LabelStmt *LS = dyn_cast<LabelStmt>(LastStmt)) { |
| 157 | EmitLabel(*LS); |
| 158 | LastStmt = LS->getSubStmt(); |
| 159 | } |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 160 | |
Anders Carlsson | 17d28a3 | 2008-12-12 05:52:00 +0000 | [diff] [blame] | 161 | EnsureInsertPoint(); |
| 162 | |
| 163 | RV = EmitAnyExpr(cast<Expr>(LastStmt), AggLoc); |
| 164 | } |
| 165 | |
| 166 | if (llvm::Value *V = StackSaveValues.pop_back_val()) { |
Mike Stump | 36a2ada | 2009-02-07 12:52:26 +0000 | [diff] [blame] | 167 | StackDepth = V; |
Anders Carlsson | 17d28a3 | 2008-12-12 05:52:00 +0000 | [diff] [blame] | 168 | V = Builder.CreateLoad(V, "tmp"); |
| 169 | |
| 170 | llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stackrestore); |
| 171 | Builder.CreateCall(F, V); |
Chris Lattner | 91d723d | 2008-07-26 20:23:23 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Anders Carlsson | 17d28a3 | 2008-12-12 05:52:00 +0000 | [diff] [blame] | 174 | return RV; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Daniel Dunbar | a0c21a8 | 2008-11-13 01:24:05 +0000 | [diff] [blame] | 177 | void CodeGenFunction::EmitBlock(llvm::BasicBlock *BB, bool IsFinished) { |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 178 | // Fall out of the current block (if necessary). |
| 179 | EmitBranch(BB); |
Daniel Dunbar | a0c21a8 | 2008-11-13 01:24:05 +0000 | [diff] [blame] | 180 | |
| 181 | if (IsFinished && BB->use_empty()) { |
| 182 | delete BB; |
| 183 | return; |
| 184 | } |
| 185 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 186 | CurFn->getBasicBlockList().push_back(BB); |
| 187 | Builder.SetInsertPoint(BB); |
| 188 | } |
| 189 | |
Mike Stump | 36a2ada | 2009-02-07 12:52:26 +0000 | [diff] [blame] | 190 | void CodeGenFunction::EmitStackUpdate(const LabelStmt &S) { |
| 191 | if (StackDepthMap.find(&S) == StackDepthMap.end()) { |
| 192 | // If we can't find it, just remember the depth now, |
| 193 | // so we can validate it later. |
| 194 | // FIXME: We need to save a place to insert the adjustment, |
| 195 | // if needed, here, sa that later in EmitLabel, we can |
| 196 | // backpatch the adjustment into that place, instead of |
| 197 | // saying unsupported. |
| 198 | StackDepthMap[&S] = StackDepth; |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | // Find applicable stack depth, if any... |
| 203 | llvm::Value *V = StackDepthMap[&S]; |
| 204 | // V can be 0 here, if it is, be sure to start searching from the |
| 205 | // top of the function, as we want the next save after that point. |
| 206 | for (unsigned int i = 0; i < StackSaveValues.size(); ++i) |
| 207 | if (StackSaveValues[i] == V) { |
| 208 | // The actual depth is actually in the next used slot, if any. |
| 209 | while (++i < StackSaveValues.size() |
| 210 | && (V = StackSaveValues[i]) == 0) ; |
| 211 | // If there were no other depth changes, we don't need any |
| 212 | // adjustments. |
| 213 | if (V) { |
| 214 | V = Builder.CreateLoad(V, "tmp"); |
| 215 | // and restore it. |
| 216 | llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stackrestore); |
| 217 | Builder.CreateCall(F, V); |
| 218 | } |
| 219 | } else |
| 220 | // FIXME: Move to semq and assert here, codegen isn't the right |
| 221 | // time to be checking. |
| 222 | CGM.ErrorUnsupported(&S, "invalid goto to VLA scope that has finished"); |
| 223 | } |
| 224 | |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 225 | void CodeGenFunction::EmitBranch(llvm::BasicBlock *Target) { |
| 226 | // Emit a branch from the current block to the target one if this |
| 227 | // was a real block. If this was just a fall-through block after a |
| 228 | // terminator, don't emit it. |
| 229 | llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); |
| 230 | |
| 231 | if (!CurBB || CurBB->getTerminator()) { |
| 232 | // If there is no insert point or the previous block is already |
| 233 | // terminated, don't touch it. |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 234 | } else { |
| 235 | // Otherwise, create a fall-through branch. |
| 236 | Builder.CreateBr(Target); |
| 237 | } |
Daniel Dunbar | 5e08ad3 | 2008-11-11 22:06:59 +0000 | [diff] [blame] | 238 | |
| 239 | Builder.ClearInsertionPoint(); |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Chris Lattner | 91d723d | 2008-07-26 20:23:23 +0000 | [diff] [blame] | 242 | void CodeGenFunction::EmitLabel(const LabelStmt &S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 243 | llvm::BasicBlock *NextBB = getBasicBlockForLabel(&S); |
Mike Stump | 36a2ada | 2009-02-07 12:52:26 +0000 | [diff] [blame] | 244 | if (StackDepthMap.find(&S) == StackDepthMap.end()) { |
| 245 | // We need to remember the stack depth so that we can readjust the |
| 246 | // stack back to the right depth for this label if we want to |
| 247 | // transfer here from a different depth. |
| 248 | StackDepthMap[&S] = StackDepth; |
| 249 | } else { |
| 250 | if (StackDepthMap[&S] != StackDepth) { |
| 251 | // FIXME: Sema needs to ckeck for jumps that cross decls with |
| 252 | // initializations for C++, and all VLAs, not just the first in |
| 253 | // a block that does a stacksave. |
| 254 | // FIXME: We need to save a place to insert the adjustment |
| 255 | // when we do a EmitStackUpdate on a forward jump, and then |
| 256 | // backpatch the adjustment into that place. |
| 257 | CGM.ErrorUnsupported(&S, "forward goto inside scope with VLA"); |
| 258 | } |
| 259 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 260 | EmitBlock(NextBB); |
Chris Lattner | 91d723d | 2008-07-26 20:23:23 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | |
| 264 | void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) { |
| 265 | EmitLabel(S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 266 | EmitStmt(S.getSubStmt()); |
| 267 | } |
| 268 | |
| 269 | void CodeGenFunction::EmitGotoStmt(const GotoStmt &S) { |
Daniel Dunbar | a4275d1 | 2008-10-02 18:02:06 +0000 | [diff] [blame] | 270 | // FIXME: Implement goto out in @try or @catch blocks. |
| 271 | if (!ObjCEHStack.empty()) { |
| 272 | CGM.ErrorUnsupported(&S, "goto inside an Obj-C exception block"); |
| 273 | return; |
| 274 | } |
| 275 | |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 276 | // If this code is reachable then emit a stop point (if generating |
| 277 | // debug info). We have to do this ourselves because we are on the |
| 278 | // "simple" statement path. |
| 279 | if (HaveInsertPoint()) |
| 280 | EmitStopPoint(&S); |
Mike Stump | 36a2ada | 2009-02-07 12:52:26 +0000 | [diff] [blame] | 281 | |
| 282 | // We need to adjust the stack, if the destination was (will be) at |
| 283 | // a different depth. |
| 284 | EmitStackUpdate(*S.getLabel()); |
| 285 | |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 286 | EmitBranch(getBasicBlockForLabel(S.getLabel())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 289 | void CodeGenFunction::EmitIndirectGotoStmt(const IndirectGotoStmt &S) { |
Daniel Dunbar | a4275d1 | 2008-10-02 18:02:06 +0000 | [diff] [blame] | 290 | // FIXME: Implement indirect goto in @try or @catch blocks. |
| 291 | if (!ObjCEHStack.empty()) { |
| 292 | CGM.ErrorUnsupported(&S, "goto inside an Obj-C exception block"); |
| 293 | return; |
| 294 | } |
| 295 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 296 | // Emit initial switch which will be patched up later by |
| 297 | // EmitIndirectSwitches(). We need a default dest, so we use the |
| 298 | // current BB, but this is overwritten. |
| 299 | llvm::Value *V = Builder.CreatePtrToInt(EmitScalarExpr(S.getTarget()), |
| 300 | llvm::Type::Int32Ty, |
| 301 | "addr"); |
| 302 | llvm::SwitchInst *I = Builder.CreateSwitch(V, Builder.GetInsertBlock()); |
| 303 | IndirectSwitches.push_back(I); |
| 304 | |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 305 | // Clear the insertion point to indicate we are in unreachable code. |
| 306 | Builder.ClearInsertionPoint(); |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Chris Lattner | 62b72f6 | 2008-11-11 07:24:28 +0000 | [diff] [blame] | 309 | void CodeGenFunction::EmitIfStmt(const IfStmt &S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 310 | // C99 6.8.4.1: The first substatement is executed if the expression compares |
| 311 | // unequal to 0. The condition must be a scalar type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 312 | |
Chris Lattner | 9bc47e2 | 2008-11-12 07:46:33 +0000 | [diff] [blame] | 313 | // If the condition constant folds and can be elided, try to avoid emitting |
| 314 | // the condition and the dead arm of the if/else. |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 315 | if (int Cond = ConstantFoldsToSimpleInteger(S.getCond())) { |
Chris Lattner | 62b72f6 | 2008-11-11 07:24:28 +0000 | [diff] [blame] | 316 | // Figure out which block (then or else) is executed. |
| 317 | const Stmt *Executed = S.getThen(), *Skipped = S.getElse(); |
Chris Lattner | 9bc47e2 | 2008-11-12 07:46:33 +0000 | [diff] [blame] | 318 | if (Cond == -1) // Condition false? |
Chris Lattner | 62b72f6 | 2008-11-11 07:24:28 +0000 | [diff] [blame] | 319 | std::swap(Executed, Skipped); |
Chris Lattner | 9bc47e2 | 2008-11-12 07:46:33 +0000 | [diff] [blame] | 320 | |
Chris Lattner | 62b72f6 | 2008-11-11 07:24:28 +0000 | [diff] [blame] | 321 | // If the skipped block has no labels in it, just emit the executed block. |
| 322 | // This avoids emitting dead code and simplifies the CFG substantially. |
Chris Lattner | 9bc47e2 | 2008-11-12 07:46:33 +0000 | [diff] [blame] | 323 | if (!ContainsLabel(Skipped)) { |
Chris Lattner | 62b72f6 | 2008-11-11 07:24:28 +0000 | [diff] [blame] | 324 | if (Executed) |
| 325 | EmitStmt(Executed); |
| 326 | return; |
| 327 | } |
| 328 | } |
Chris Lattner | 9bc47e2 | 2008-11-12 07:46:33 +0000 | [diff] [blame] | 329 | |
| 330 | // Otherwise, the condition did not fold, or we couldn't elide it. Just emit |
| 331 | // the conditional branch. |
Daniel Dunbar | 781d7ca | 2008-11-13 00:47:57 +0000 | [diff] [blame] | 332 | llvm::BasicBlock *ThenBlock = createBasicBlock("if.then"); |
| 333 | llvm::BasicBlock *ContBlock = createBasicBlock("if.end"); |
| 334 | llvm::BasicBlock *ElseBlock = ContBlock; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 335 | if (S.getElse()) |
Daniel Dunbar | 781d7ca | 2008-11-13 00:47:57 +0000 | [diff] [blame] | 336 | ElseBlock = createBasicBlock("if.else"); |
| 337 | EmitBranchOnBoolExpr(S.getCond(), ThenBlock, ElseBlock); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 338 | |
| 339 | // Emit the 'then' code. |
| 340 | EmitBlock(ThenBlock); |
| 341 | EmitStmt(S.getThen()); |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 342 | EmitBranch(ContBlock); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 343 | |
| 344 | // Emit the 'else' code if present. |
| 345 | if (const Stmt *Else = S.getElse()) { |
| 346 | EmitBlock(ElseBlock); |
| 347 | EmitStmt(Else); |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 348 | EmitBranch(ContBlock); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | // Emit the continuation block for code after the if. |
Daniel Dunbar | c22d665 | 2008-11-13 01:54:24 +0000 | [diff] [blame] | 352 | EmitBlock(ContBlock, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | void CodeGenFunction::EmitWhileStmt(const WhileStmt &S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 356 | // Emit the header for the loop, insert it, which will create an uncond br to |
| 357 | // it. |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 358 | llvm::BasicBlock *LoopHeader = createBasicBlock("while.cond"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 359 | EmitBlock(LoopHeader); |
Mike Stump | 72cac2c | 2009-02-07 18:08:12 +0000 | [diff] [blame] | 360 | |
| 361 | // Create an exit block for when the condition fails, create a block for the |
| 362 | // body of the loop. |
| 363 | llvm::BasicBlock *ExitBlock = createBasicBlock("while.end"); |
| 364 | llvm::BasicBlock *LoopBody = createBasicBlock("while.body"); |
| 365 | |
| 366 | // Store the blocks to use for break and continue. |
| 367 | BreakContinuePush(ExitBlock, LoopHeader); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 368 | |
Mike Stump | 16b1620 | 2009-02-07 17:18:33 +0000 | [diff] [blame] | 369 | // Evaluate the conditional in the while header. C99 6.8.5.1: The |
| 370 | // evaluation of the controlling expression takes place before each |
| 371 | // execution of the loop body. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 372 | llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); |
Devang Patel | 2c30d8f | 2007-10-09 20:51:27 +0000 | [diff] [blame] | 373 | |
| 374 | // while(1) is common, avoid extra exit blocks. Be sure |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 375 | // to correctly handle break/continue though. |
Devang Patel | 2c30d8f | 2007-10-09 20:51:27 +0000 | [diff] [blame] | 376 | bool EmitBoolCondBranch = true; |
| 377 | if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal)) |
| 378 | if (C->isOne()) |
| 379 | EmitBoolCondBranch = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 380 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 381 | // As long as the condition is true, go to the loop body. |
Devang Patel | 2c30d8f | 2007-10-09 20:51:27 +0000 | [diff] [blame] | 382 | if (EmitBoolCondBranch) |
| 383 | Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 384 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 385 | // Emit the loop body. |
| 386 | EmitBlock(LoopBody); |
| 387 | EmitStmt(S.getBody()); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 388 | |
Mike Stump | 36a2ada | 2009-02-07 12:52:26 +0000 | [diff] [blame] | 389 | BreakContinuePop(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 390 | |
| 391 | // Cycle to the condition. |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 392 | EmitBranch(LoopHeader); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 393 | |
| 394 | // Emit the exit block. |
Daniel Dunbar | c22d665 | 2008-11-13 01:54:24 +0000 | [diff] [blame] | 395 | EmitBlock(ExitBlock, true); |
Devang Patel | 2c30d8f | 2007-10-09 20:51:27 +0000 | [diff] [blame] | 396 | |
| 397 | // If LoopHeader is a simple forwarding block then eliminate it. |
| 398 | if (!EmitBoolCondBranch |
| 399 | && &LoopHeader->front() == LoopHeader->getTerminator()) { |
| 400 | LoopHeader->replaceAllUsesWith(LoopBody); |
| 401 | LoopHeader->getTerminator()->eraseFromParent(); |
| 402 | LoopHeader->eraseFromParent(); |
| 403 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | void CodeGenFunction::EmitDoStmt(const DoStmt &S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 407 | // Emit the body for the loop, insert it, which will create an uncond br to |
| 408 | // it. |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 409 | llvm::BasicBlock *LoopBody = createBasicBlock("do.body"); |
| 410 | llvm::BasicBlock *AfterDo = createBasicBlock("do.end"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 411 | EmitBlock(LoopBody); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 412 | |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 413 | llvm::BasicBlock *DoCond = createBasicBlock("do.cond"); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 414 | |
| 415 | // Store the blocks to use for break and continue. |
Mike Stump | 36a2ada | 2009-02-07 12:52:26 +0000 | [diff] [blame] | 416 | BreakContinuePush(AfterDo, DoCond); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 417 | |
| 418 | // Emit the body of the loop into the block. |
| 419 | EmitStmt(S.getBody()); |
| 420 | |
Mike Stump | 36a2ada | 2009-02-07 12:52:26 +0000 | [diff] [blame] | 421 | BreakContinuePop(); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 422 | |
| 423 | EmitBlock(DoCond); |
| 424 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 425 | // C99 6.8.5.2: "The evaluation of the controlling expression takes place |
| 426 | // after each execution of the loop body." |
| 427 | |
| 428 | // Evaluate the conditional in the while header. |
| 429 | // C99 6.8.5p2/p4: The first substatement is executed if the expression |
| 430 | // compares unequal to 0. The condition must be a scalar type. |
| 431 | llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); |
Devang Patel | 05f6e6b | 2007-10-09 20:33:39 +0000 | [diff] [blame] | 432 | |
| 433 | // "do {} while (0)" is common in macros, avoid extra blocks. Be sure |
| 434 | // to correctly handle break/continue though. |
| 435 | bool EmitBoolCondBranch = true; |
| 436 | if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal)) |
| 437 | if (C->isZero()) |
| 438 | EmitBoolCondBranch = false; |
| 439 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 440 | // As long as the condition is true, iterate the loop. |
Devang Patel | 05f6e6b | 2007-10-09 20:33:39 +0000 | [diff] [blame] | 441 | if (EmitBoolCondBranch) |
| 442 | Builder.CreateCondBr(BoolCondVal, LoopBody, AfterDo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 443 | |
| 444 | // Emit the exit block. |
Daniel Dunbar | c22d665 | 2008-11-13 01:54:24 +0000 | [diff] [blame] | 445 | EmitBlock(AfterDo, true); |
Devang Patel | 05f6e6b | 2007-10-09 20:33:39 +0000 | [diff] [blame] | 446 | |
| 447 | // If DoCond is a simple forwarding block then eliminate it. |
| 448 | if (!EmitBoolCondBranch && &DoCond->front() == DoCond->getTerminator()) { |
| 449 | DoCond->replaceAllUsesWith(AfterDo); |
| 450 | DoCond->getTerminator()->eraseFromParent(); |
| 451 | DoCond->eraseFromParent(); |
| 452 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | void CodeGenFunction::EmitForStmt(const ForStmt &S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 456 | // FIXME: What do we do if the increment (f.e.) contains a stmt expression, |
| 457 | // which contains a continue/break? |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 458 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 459 | // Evaluate the first part before the loop. |
| 460 | if (S.getInit()) |
| 461 | EmitStmt(S.getInit()); |
| 462 | |
| 463 | // Start the loop with a block that tests the condition. |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 464 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 465 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 466 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 467 | EmitBlock(CondBlock); |
| 468 | |
Mike Stump | 3e9da66 | 2009-02-07 23:02:10 +0000 | [diff] [blame^] | 469 | llvm::Value *saveStackDepth = StackDepth; |
| 470 | |
Mike Stump | 20926c6 | 2009-02-07 20:14:12 +0000 | [diff] [blame] | 471 | // Evaluate the condition if present. If not, treat it as a |
| 472 | // non-zero-constant according to 6.8.5.3p2, aka, true. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 473 | if (S.getCond()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 474 | // As long as the condition is true, iterate the loop. |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 475 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 476 | |
| 477 | // C99 6.8.5p2/p4: The first substatement is executed if the expression |
| 478 | // compares unequal to 0. The condition must be a scalar type. |
| 479 | EmitBranchOnBoolExpr(S.getCond(), ForBody, AfterFor); |
| 480 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 481 | EmitBlock(ForBody); |
| 482 | } else { |
| 483 | // Treat it as a non-zero constant. Don't even create a new block for the |
| 484 | // body, just fall into it. |
| 485 | } |
| 486 | |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 487 | // If the for loop doesn't have an increment we can just use the |
| 488 | // condition as the continue block. |
| 489 | llvm::BasicBlock *ContinueBlock; |
| 490 | if (S.getInc()) |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 491 | ContinueBlock = createBasicBlock("for.inc"); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 492 | else |
| 493 | ContinueBlock = CondBlock; |
| 494 | |
| 495 | // Store the blocks to use for break and continue. |
Mike Stump | 3e9da66 | 2009-02-07 23:02:10 +0000 | [diff] [blame^] | 496 | // Ensure any vlas created between there and here, are undone |
| 497 | BreakContinuePush(AfterFor, ContinueBlock, |
| 498 | saveStackDepth, saveStackDepth); |
| 499 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 500 | // If the condition is true, execute the body of the for stmt. |
| 501 | EmitStmt(S.getBody()); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 502 | |
Mike Stump | 36a2ada | 2009-02-07 12:52:26 +0000 | [diff] [blame] | 503 | BreakContinuePop(); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 504 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 505 | // If there is an increment, emit it next. |
Daniel Dunbar | ad12b6d | 2008-09-28 00:19:22 +0000 | [diff] [blame] | 506 | if (S.getInc()) { |
| 507 | EmitBlock(ContinueBlock); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 508 | EmitStmt(S.getInc()); |
Daniel Dunbar | ad12b6d | 2008-09-28 00:19:22 +0000 | [diff] [blame] | 509 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 510 | |
| 511 | // Finally, branch back up to the condition for the next iteration. |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 512 | EmitBranch(CondBlock); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 513 | |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 514 | // Emit the fall-through block. |
Daniel Dunbar | c22d665 | 2008-11-13 01:54:24 +0000 | [diff] [blame] | 515 | EmitBlock(AfterFor, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Daniel Dunbar | 29e0bcc | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 518 | void CodeGenFunction::EmitReturnOfRValue(RValue RV, QualType Ty) { |
| 519 | if (RV.isScalar()) { |
| 520 | Builder.CreateStore(RV.getScalarVal(), ReturnValue); |
| 521 | } else if (RV.isAggregate()) { |
| 522 | EmitAggregateCopy(ReturnValue, RV.getAggregateAddr(), Ty); |
| 523 | } else { |
| 524 | StoreComplexToAddr(RV.getComplexVal(), ReturnValue, false); |
| 525 | } |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 526 | EmitBranch(ReturnBlock); |
Daniel Dunbar | 29e0bcc | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 529 | /// EmitReturnStmt - Note that due to GCC extensions, this can have an operand |
| 530 | /// if the function returns void, or may be missing one if the function returns |
| 531 | /// non-void. Fun stuff :). |
| 532 | void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) { |
Eli Friedman | 20c802b | 2008-12-20 23:18:29 +0000 | [diff] [blame] | 533 | for (unsigned i = 0; i < StackSaveValues.size(); i++) { |
Anders Carlsson | 7e63b85 | 2008-12-20 21:33:38 +0000 | [diff] [blame] | 534 | if (StackSaveValues[i]) { |
| 535 | CGM.ErrorUnsupported(&S, "return inside scope with VLA"); |
| 536 | return; |
| 537 | } |
Anders Carlsson | eb91f0e | 2008-12-20 19:33:21 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 540 | // Emit the result value, even if unused, to evalute the side effects. |
| 541 | const Expr *RV = S.getRetValue(); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 542 | |
| 543 | // FIXME: Clean this up by using an LValue for ReturnTemp, |
| 544 | // EmitStoreThroughLValue, and EmitAnyExpr. |
| 545 | if (!ReturnValue) { |
| 546 | // Make sure not to return anything, but evaluate the expression |
| 547 | // for side effects. |
| 548 | if (RV) |
Eli Friedman | 144ac61 | 2008-05-22 01:22:33 +0000 | [diff] [blame] | 549 | EmitAnyExpr(RV); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 550 | } else if (RV == 0) { |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 551 | // Do nothing (return value is left uninitialized) |
Chris Lattner | 4b0029d | 2007-08-26 07:14:44 +0000 | [diff] [blame] | 552 | } else if (!hasAggregateLLVMType(RV->getType())) { |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 553 | Builder.CreateStore(EmitScalarExpr(RV), ReturnValue); |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 554 | } else if (RV->getType()->isAnyComplexType()) { |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 555 | EmitComplexExprIntoAddr(RV, ReturnValue, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 556 | } else { |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 557 | EmitAggExpr(RV, ReturnValue, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 558 | } |
Eli Friedman | 144ac61 | 2008-05-22 01:22:33 +0000 | [diff] [blame] | 559 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 560 | if (!ObjCEHStack.empty()) { |
| 561 | for (ObjCEHStackType::reverse_iterator i = ObjCEHStack.rbegin(), |
| 562 | e = ObjCEHStack.rend(); i != e; ++i) { |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 563 | llvm::BasicBlock *ReturnPad = createBasicBlock("return.pad"); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 564 | EmitJumpThroughFinally(*i, ReturnPad); |
| 565 | EmitBlock(ReturnPad); |
| 566 | } |
| 567 | } |
| 568 | |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 569 | EmitBranch(ReturnBlock); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | void CodeGenFunction::EmitDeclStmt(const DeclStmt &S) { |
Ted Kremenek | e4ea1f4 | 2008-10-06 18:42:27 +0000 | [diff] [blame] | 573 | for (DeclStmt::const_decl_iterator I = S.decl_begin(), E = S.decl_end(); |
| 574 | I != E; ++I) |
| 575 | EmitDecl(**I); |
Chris Lattner | 6fa5f09 | 2007-07-12 15:43:07 +0000 | [diff] [blame] | 576 | } |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 577 | |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 578 | void CodeGenFunction::EmitBreakStmt(const BreakStmt &S) { |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 579 | assert(!BreakContinueStack.empty() && "break stmt not in a loop or switch!"); |
| 580 | |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 581 | // FIXME: Implement break in @try or @catch blocks. |
Anders Carlsson | e21269b | 2008-12-13 22:52:24 +0000 | [diff] [blame] | 582 | if (ObjCEHStack.size() != BreakContinueStack.back().EHStackSize) { |
Anders Carlsson | eb91f0e | 2008-12-20 19:33:21 +0000 | [diff] [blame] | 583 | CGM.ErrorUnsupported(&S, "break inside an Obj-C exception block"); |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 584 | return; |
| 585 | } |
| 586 | |
Eli Friedman | 20c802b | 2008-12-20 23:18:29 +0000 | [diff] [blame] | 587 | for (unsigned i = 0; i < StackSaveValues.size(); i++) { |
| 588 | if (StackSaveValues[i]) { |
| 589 | CGM.ErrorUnsupported(&S, "break inside scope with VLA"); |
| 590 | return; |
| 591 | } |
Anders Carlsson | eb91f0e | 2008-12-20 19:33:21 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 594 | // If this code is reachable then emit a stop point (if generating |
| 595 | // debug info). We have to do this ourselves because we are on the |
| 596 | // "simple" statement path. |
| 597 | if (HaveInsertPoint()) |
| 598 | EmitStopPoint(&S); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 599 | llvm::BasicBlock *Block = BreakContinueStack.back().BreakBlock; |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 600 | EmitBranch(Block); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 603 | void CodeGenFunction::EmitContinueStmt(const ContinueStmt &S) { |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 604 | assert(!BreakContinueStack.empty() && "continue stmt not in a loop!"); |
| 605 | |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 606 | // FIXME: Implement continue in @try or @catch blocks. |
Anders Carlsson | e21269b | 2008-12-13 22:52:24 +0000 | [diff] [blame] | 607 | if (ObjCEHStack.size() != BreakContinueStack.back().EHStackSize) { |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 608 | CGM.ErrorUnsupported(&S, "continue inside an Obj-C exception block"); |
| 609 | return; |
| 610 | } |
| 611 | |
Eli Friedman | 20c802b | 2008-12-20 23:18:29 +0000 | [diff] [blame] | 612 | for (unsigned i = 0; i < StackSaveValues.size(); i++) { |
| 613 | if (StackSaveValues[i]) { |
| 614 | CGM.ErrorUnsupported(&S, "continue inside scope with VLA"); |
| 615 | return; |
| 616 | } |
Anders Carlsson | eb91f0e | 2008-12-20 19:33:21 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 619 | // If this code is reachable then emit a stop point (if generating |
| 620 | // debug info). We have to do this ourselves because we are on the |
| 621 | // "simple" statement path. |
| 622 | if (HaveInsertPoint()) |
| 623 | EmitStopPoint(&S); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 624 | llvm::BasicBlock *Block = BreakContinueStack.back().ContinueBlock; |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 625 | EmitBranch(Block); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 626 | } |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 627 | |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 628 | /// EmitCaseStmtRange - If case statement range is not too big then |
| 629 | /// add multiple cases to switch instruction, one for each value within |
| 630 | /// the range. If range is too big then emit "if" condition check. |
| 631 | void CodeGenFunction::EmitCaseStmtRange(const CaseStmt &S) { |
Daniel Dunbar | 4efde8d | 2008-07-24 01:18:41 +0000 | [diff] [blame] | 632 | assert(S.getRHS() && "Expected RHS value in CaseStmt"); |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 633 | |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 634 | llvm::APSInt LHS = S.getLHS()->EvaluateAsInt(getContext()); |
| 635 | llvm::APSInt RHS = S.getRHS()->EvaluateAsInt(getContext()); |
Daniel Dunbar | 4efde8d | 2008-07-24 01:18:41 +0000 | [diff] [blame] | 636 | |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 637 | // Emit the code for this case. We do this first to make sure it is |
| 638 | // properly chained from our predecessor before generating the |
| 639 | // switch machinery to enter this block. |
Daniel Dunbar | f84dcda | 2008-11-11 04:12:31 +0000 | [diff] [blame] | 640 | EmitBlock(createBasicBlock("sw.bb")); |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 641 | llvm::BasicBlock *CaseDest = Builder.GetInsertBlock(); |
| 642 | EmitStmt(S.getSubStmt()); |
| 643 | |
Daniel Dunbar | 4efde8d | 2008-07-24 01:18:41 +0000 | [diff] [blame] | 644 | // If range is empty, do nothing. |
| 645 | if (LHS.isSigned() ? RHS.slt(LHS) : RHS.ult(LHS)) |
| 646 | return; |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 647 | |
| 648 | llvm::APInt Range = RHS - LHS; |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 649 | // FIXME: parameters such as this should not be hardcoded. |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 650 | if (Range.ult(llvm::APInt(Range.getBitWidth(), 64))) { |
| 651 | // Range is small enough to add multiple switch instruction cases. |
Daniel Dunbar | 4efde8d | 2008-07-24 01:18:41 +0000 | [diff] [blame] | 652 | for (unsigned i = 0, e = Range.getZExtValue() + 1; i != e; ++i) { |
Devang Patel | 2d79d0f | 2007-10-05 20:54:07 +0000 | [diff] [blame] | 653 | SwitchInsn->addCase(llvm::ConstantInt::get(LHS), CaseDest); |
| 654 | LHS++; |
| 655 | } |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 656 | return; |
| 657 | } |
| 658 | |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 659 | // The range is too big. Emit "if" condition into a new block, |
| 660 | // making sure to save and restore the current insertion point. |
| 661 | llvm::BasicBlock *RestoreBB = Builder.GetInsertBlock(); |
Devang Patel | 2d79d0f | 2007-10-05 20:54:07 +0000 | [diff] [blame] | 662 | |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 663 | // Push this test onto the chain of range checks (which terminates |
| 664 | // in the default basic block). The switch's default will be changed |
| 665 | // to the top of this chain after switch emission is complete. |
| 666 | llvm::BasicBlock *FalseDest = CaseRangeBlock; |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 667 | CaseRangeBlock = createBasicBlock("sw.caserange"); |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 668 | |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 669 | CurFn->getBasicBlockList().push_back(CaseRangeBlock); |
| 670 | Builder.SetInsertPoint(CaseRangeBlock); |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 671 | |
| 672 | // Emit range check. |
| 673 | llvm::Value *Diff = |
Daniel Dunbar | 4efde8d | 2008-07-24 01:18:41 +0000 | [diff] [blame] | 674 | Builder.CreateSub(SwitchInsn->getCondition(), llvm::ConstantInt::get(LHS), |
| 675 | "tmp"); |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 676 | llvm::Value *Cond = |
| 677 | Builder.CreateICmpULE(Diff, llvm::ConstantInt::get(Range), "tmp"); |
| 678 | Builder.CreateCondBr(Cond, CaseDest, FalseDest); |
| 679 | |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 680 | // Restore the appropriate insertion point. |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 681 | if (RestoreBB) |
| 682 | Builder.SetInsertPoint(RestoreBB); |
| 683 | else |
| 684 | Builder.ClearInsertionPoint(); |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | void CodeGenFunction::EmitCaseStmt(const CaseStmt &S) { |
| 688 | if (S.getRHS()) { |
| 689 | EmitCaseStmtRange(S); |
| 690 | return; |
| 691 | } |
| 692 | |
Daniel Dunbar | f84dcda | 2008-11-11 04:12:31 +0000 | [diff] [blame] | 693 | EmitBlock(createBasicBlock("sw.bb")); |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 694 | llvm::BasicBlock *CaseDest = Builder.GetInsertBlock(); |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 695 | llvm::APSInt CaseVal = S.getLHS()->EvaluateAsInt(getContext()); |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 696 | SwitchInsn->addCase(llvm::ConstantInt::get(CaseVal), CaseDest); |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 697 | EmitStmt(S.getSubStmt()); |
| 698 | } |
| 699 | |
| 700 | void CodeGenFunction::EmitDefaultStmt(const DefaultStmt &S) { |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 701 | llvm::BasicBlock *DefaultBlock = SwitchInsn->getDefaultDest(); |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 702 | assert(DefaultBlock->empty() && |
| 703 | "EmitDefaultStmt: Default block already defined?"); |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 704 | EmitBlock(DefaultBlock); |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 705 | EmitStmt(S.getSubStmt()); |
| 706 | } |
| 707 | |
| 708 | void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) { |
| 709 | llvm::Value *CondV = EmitScalarExpr(S.getCond()); |
| 710 | |
| 711 | // Handle nested switch statements. |
| 712 | llvm::SwitchInst *SavedSwitchInsn = SwitchInsn; |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 713 | llvm::BasicBlock *SavedCRBlock = CaseRangeBlock; |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 714 | |
Mike Stump | 3e9da66 | 2009-02-07 23:02:10 +0000 | [diff] [blame^] | 715 | // Ensure any vlas created inside are destroyed on break. |
| 716 | llvm::Value *saveBreakStackDepth = StackDepth; |
| 717 | |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 718 | // Create basic block to hold stuff that comes after switch |
| 719 | // statement. We also need to create a default block now so that |
| 720 | // explicit case ranges tests can have a place to jump to on |
| 721 | // failure. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 722 | llvm::BasicBlock *NextBlock = createBasicBlock("sw.epilog"); |
| 723 | llvm::BasicBlock *DefaultBlock = createBasicBlock("sw.default"); |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 724 | SwitchInsn = Builder.CreateSwitch(CondV, DefaultBlock); |
| 725 | CaseRangeBlock = DefaultBlock; |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 726 | |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 727 | // Clear the insertion point to indicate we are in unreachable code. |
| 728 | Builder.ClearInsertionPoint(); |
Eli Friedman | d28a80d | 2008-05-12 16:08:04 +0000 | [diff] [blame] | 729 | |
Devang Patel | e9b8c0a | 2007-10-30 20:59:40 +0000 | [diff] [blame] | 730 | // All break statements jump to NextBlock. If BreakContinueStack is non empty |
| 731 | // then reuse last ContinueBlock. |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 732 | llvm::BasicBlock *ContinueBlock = NULL; |
Mike Stump | 3e9da66 | 2009-02-07 23:02:10 +0000 | [diff] [blame^] | 733 | llvm::Value *saveContinueStackDepth = NULL; |
| 734 | if (!BreakContinueStack.empty()) { |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 735 | ContinueBlock = BreakContinueStack.back().ContinueBlock; |
Mike Stump | 3e9da66 | 2009-02-07 23:02:10 +0000 | [diff] [blame^] | 736 | saveContinueStackDepth = BreakContinueStack.back().SaveContinueStackDepth; |
| 737 | } |
| 738 | // Ensure any vlas created between there and here, are undone |
| 739 | BreakContinuePush(NextBlock, ContinueBlock, |
| 740 | saveBreakStackDepth, saveContinueStackDepth); |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 741 | |
| 742 | // Emit switch body. |
| 743 | EmitStmt(S.getBody()); |
Mike Stump | 36a2ada | 2009-02-07 12:52:26 +0000 | [diff] [blame] | 744 | BreakContinuePop(); |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 745 | |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 746 | // Update the default block in case explicit case range tests have |
| 747 | // been chained on top. |
| 748 | SwitchInsn->setSuccessor(0, CaseRangeBlock); |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 749 | |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 750 | // If a default was never emitted then reroute any jumps to it and |
| 751 | // discard. |
| 752 | if (!DefaultBlock->getParent()) { |
| 753 | DefaultBlock->replaceAllUsesWith(NextBlock); |
| 754 | delete DefaultBlock; |
| 755 | } |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 756 | |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 757 | // Emit continuation. |
Daniel Dunbar | c22d665 | 2008-11-13 01:54:24 +0000 | [diff] [blame] | 758 | EmitBlock(NextBlock, true); |
Daniel Dunbar | 16f2357 | 2008-07-25 01:11:38 +0000 | [diff] [blame] | 759 | |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 760 | SwitchInsn = SavedSwitchInsn; |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 761 | CaseRangeBlock = SavedCRBlock; |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 762 | } |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 763 | |
Anders Carlsson | ce179ab | 2008-11-09 18:54:14 +0000 | [diff] [blame] | 764 | static std::string ConvertAsmString(const AsmStmt& S, bool &Failed) |
| 765 | { |
| 766 | // FIXME: No need to create new std::string here, we could just make sure |
| 767 | // that we don't read past the end of the string data. |
| 768 | std::string str(S.getAsmString()->getStrData(), |
| 769 | S.getAsmString()->getByteLength()); |
| 770 | const char *Start = str.c_str(); |
| 771 | |
| 772 | unsigned NumOperands = S.getNumOutputs() + S.getNumInputs(); |
| 773 | bool IsSimple = S.isSimple(); |
Daniel Dunbar | 281f55c | 2008-10-17 20:58:01 +0000 | [diff] [blame] | 774 | Failed = false; |
| 775 | |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 776 | static unsigned AsmCounter = 0; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 777 | AsmCounter++; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 778 | std::string Result; |
Anders Carlsson | 2abd25f | 2008-02-05 23:18:57 +0000 | [diff] [blame] | 779 | if (IsSimple) { |
| 780 | while (*Start) { |
| 781 | switch (*Start) { |
| 782 | default: |
| 783 | Result += *Start; |
| 784 | break; |
| 785 | case '$': |
| 786 | Result += "$$"; |
| 787 | break; |
| 788 | } |
Anders Carlsson | 2abd25f | 2008-02-05 23:18:57 +0000 | [diff] [blame] | 789 | Start++; |
| 790 | } |
| 791 | |
| 792 | return Result; |
| 793 | } |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 794 | |
| 795 | while (*Start) { |
| 796 | switch (*Start) { |
| 797 | default: |
| 798 | Result += *Start; |
| 799 | break; |
| 800 | case '$': |
| 801 | Result += "$$"; |
| 802 | break; |
| 803 | case '%': |
| 804 | // Escaped character |
| 805 | Start++; |
| 806 | if (!*Start) { |
| 807 | // FIXME: This should be caught during Sema. |
| 808 | assert(0 && "Trailing '%' in asm string."); |
| 809 | } |
| 810 | |
| 811 | char EscapedChar = *Start; |
| 812 | if (EscapedChar == '%') { |
| 813 | // Escaped percentage sign. |
| 814 | Result += '%'; |
Chris Lattner | 345f720 | 2008-07-26 20:15:14 +0000 | [diff] [blame] | 815 | } else if (EscapedChar == '=') { |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 816 | // Generate an unique ID. |
| 817 | Result += llvm::utostr(AsmCounter); |
| 818 | } else if (isdigit(EscapedChar)) { |
| 819 | // %n - Assembler operand n |
| 820 | char *End; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 821 | unsigned long n = strtoul(Start, &End, 10); |
| 822 | if (Start == End) { |
| 823 | // FIXME: This should be caught during Sema. |
| 824 | assert(0 && "Missing operand!"); |
| 825 | } else if (n >= NumOperands) { |
| 826 | // FIXME: This should be caught during Sema. |
| 827 | assert(0 && "Operand number out of range!"); |
| 828 | } |
| 829 | |
| 830 | Result += '$' + llvm::utostr(n); |
Lauro Ramos Venancio | 7695f70 | 2008-02-26 19:19:58 +0000 | [diff] [blame] | 831 | Start = End - 1; |
Anders Carlsson | 2abd25f | 2008-02-05 23:18:57 +0000 | [diff] [blame] | 832 | } else if (isalpha(EscapedChar)) { |
| 833 | char *End; |
| 834 | |
| 835 | unsigned long n = strtoul(Start + 1, &End, 10); |
| 836 | if (Start == End) { |
| 837 | // FIXME: This should be caught during Sema. |
| 838 | assert(0 && "Missing operand!"); |
| 839 | } else if (n >= NumOperands) { |
| 840 | // FIXME: This should be caught during Sema. |
| 841 | assert(0 && "Operand number out of range!"); |
| 842 | } |
| 843 | |
| 844 | Result += "${" + llvm::utostr(n) + ':' + EscapedChar + '}'; |
Lauro Ramos Venancio | 7695f70 | 2008-02-26 19:19:58 +0000 | [diff] [blame] | 845 | Start = End - 1; |
Anders Carlsson | ce179ab | 2008-11-09 18:54:14 +0000 | [diff] [blame] | 846 | } else if (EscapedChar == '[') { |
| 847 | std::string SymbolicName; |
| 848 | |
| 849 | Start++; |
| 850 | |
| 851 | while (*Start && *Start != ']') { |
| 852 | SymbolicName += *Start; |
| 853 | |
| 854 | Start++; |
| 855 | } |
| 856 | |
| 857 | if (!Start) { |
| 858 | // FIXME: Should be caught by sema. |
| 859 | assert(0 && "Could not parse symbolic name"); |
| 860 | } |
| 861 | |
| 862 | assert(*Start == ']' && "Error parsing symbolic name"); |
| 863 | |
| 864 | int Index = -1; |
| 865 | |
| 866 | // Check if this is an output operand. |
| 867 | for (unsigned i = 0; i < S.getNumOutputs(); i++) { |
| 868 | if (S.getOutputName(i) == SymbolicName) { |
| 869 | Index = i; |
| 870 | break; |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | if (Index == -1) { |
| 875 | for (unsigned i = 0; i < S.getNumInputs(); i++) { |
| 876 | if (S.getInputName(i) == SymbolicName) { |
| 877 | Index = S.getNumOutputs() + i; |
| 878 | } |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | assert(Index != -1 && "Did not find right operand!"); |
| 883 | |
| 884 | Result += '$' + llvm::utostr(Index); |
| 885 | |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 886 | } else { |
Daniel Dunbar | 281f55c | 2008-10-17 20:58:01 +0000 | [diff] [blame] | 887 | Failed = true; |
| 888 | return ""; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 889 | } |
| 890 | } |
| 891 | Start++; |
| 892 | } |
| 893 | |
| 894 | return Result; |
| 895 | } |
| 896 | |
Lauro Ramos Venancio | a5694b8 | 2008-02-26 18:33:46 +0000 | [diff] [blame] | 897 | static std::string SimplifyConstraint(const char* Constraint, |
Anders Carlsson | 300fb5d | 2009-01-18 02:06:20 +0000 | [diff] [blame] | 898 | TargetInfo &Target, |
| 899 | const std::string *OutputNamesBegin = 0, |
| 900 | const std::string *OutputNamesEnd = 0) |
| 901 | { |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 902 | std::string Result; |
| 903 | |
| 904 | while (*Constraint) { |
| 905 | switch (*Constraint) { |
| 906 | default: |
Lauro Ramos Venancio | a5694b8 | 2008-02-26 18:33:46 +0000 | [diff] [blame] | 907 | Result += Target.convertConstraint(*Constraint); |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 908 | break; |
| 909 | // Ignore these |
| 910 | case '*': |
| 911 | case '?': |
| 912 | case '!': |
| 913 | break; |
| 914 | case 'g': |
| 915 | Result += "imr"; |
| 916 | break; |
Anders Carlsson | 300fb5d | 2009-01-18 02:06:20 +0000 | [diff] [blame] | 917 | case '[': { |
| 918 | assert(OutputNamesBegin && OutputNamesEnd && |
| 919 | "Must pass output names to constraints with a symbolic name"); |
| 920 | unsigned Index; |
| 921 | bool result = Target.resolveSymbolicName(Constraint, |
| 922 | OutputNamesBegin, |
| 923 | OutputNamesEnd, Index); |
Chris Lattner | 5363765 | 2009-01-21 07:35:26 +0000 | [diff] [blame] | 924 | assert(result && "Could not resolve symbolic name"); result=result; |
Anders Carlsson | 300fb5d | 2009-01-18 02:06:20 +0000 | [diff] [blame] | 925 | Result += llvm::utostr(Index); |
| 926 | break; |
| 927 | } |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | Constraint++; |
| 931 | } |
| 932 | |
| 933 | return Result; |
| 934 | } |
| 935 | |
Anders Carlsson | 6347172 | 2009-01-11 19:32:54 +0000 | [diff] [blame] | 936 | llvm::Value* CodeGenFunction::EmitAsmInput(const AsmStmt &S, |
| 937 | TargetInfo::ConstraintInfo Info, |
| 938 | const Expr *InputExpr, |
| 939 | std::string &ConstraintStr) |
| 940 | { |
| 941 | llvm::Value *Arg; |
| 942 | if ((Info & TargetInfo::CI_AllowsRegister) || |
Anders Carlsson | ebaae2a | 2009-01-12 02:22:13 +0000 | [diff] [blame] | 943 | !(Info & TargetInfo::CI_AllowsMemory)) { |
| 944 | const llvm::Type *Ty = ConvertType(InputExpr->getType()); |
| 945 | |
| 946 | if (Ty->isSingleValueType()) { |
Anders Carlsson | 6347172 | 2009-01-11 19:32:54 +0000 | [diff] [blame] | 947 | Arg = EmitScalarExpr(InputExpr); |
| 948 | } else { |
Anders Carlsson | ebaae2a | 2009-01-12 02:22:13 +0000 | [diff] [blame] | 949 | LValue Dest = EmitLValue(InputExpr); |
| 950 | |
| 951 | uint64_t Size = CGM.getTargetData().getTypeSizeInBits(Ty); |
| 952 | if (Size <= 64 && llvm::isPowerOf2_64(Size)) { |
| 953 | Ty = llvm::IntegerType::get(Size); |
| 954 | Ty = llvm::PointerType::getUnqual(Ty); |
| 955 | |
| 956 | Arg = Builder.CreateLoad(Builder.CreateBitCast(Dest.getAddress(), Ty)); |
| 957 | } else { |
| 958 | Arg = Dest.getAddress(); |
| 959 | ConstraintStr += '*'; |
| 960 | } |
Anders Carlsson | 6347172 | 2009-01-11 19:32:54 +0000 | [diff] [blame] | 961 | } |
| 962 | } else { |
| 963 | LValue Dest = EmitLValue(InputExpr); |
| 964 | Arg = Dest.getAddress(); |
| 965 | ConstraintStr += '*'; |
| 966 | } |
| 967 | |
| 968 | return Arg; |
| 969 | } |
| 970 | |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 971 | void CodeGenFunction::EmitAsmStmt(const AsmStmt &S) { |
Daniel Dunbar | 281f55c | 2008-10-17 20:58:01 +0000 | [diff] [blame] | 972 | bool Failed; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 973 | std::string AsmString = |
Anders Carlsson | ce179ab | 2008-11-09 18:54:14 +0000 | [diff] [blame] | 974 | ConvertAsmString(S, Failed); |
Daniel Dunbar | 281f55c | 2008-10-17 20:58:01 +0000 | [diff] [blame] | 975 | |
| 976 | if (Failed) { |
| 977 | ErrorUnsupported(&S, "asm string"); |
| 978 | return; |
| 979 | } |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 980 | |
| 981 | std::string Constraints; |
| 982 | |
| 983 | llvm::Value *ResultAddr = 0; |
| 984 | const llvm::Type *ResultType = llvm::Type::VoidTy; |
| 985 | |
| 986 | std::vector<const llvm::Type*> ArgTypes; |
| 987 | std::vector<llvm::Value*> Args; |
Anders Carlsson | f39a421 | 2008-02-05 20:01:53 +0000 | [diff] [blame] | 988 | |
| 989 | // Keep track of inout constraints. |
| 990 | std::string InOutConstraints; |
| 991 | std::vector<llvm::Value*> InOutArgs; |
| 992 | std::vector<const llvm::Type*> InOutArgTypes; |
Anders Carlsson | 03eb543 | 2009-01-27 20:38:24 +0000 | [diff] [blame] | 993 | |
| 994 | llvm::SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos; |
| 995 | |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 996 | for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) { |
| 997 | std::string OutputConstraint(S.getOutputConstraint(i)->getStrData(), |
| 998 | S.getOutputConstraint(i)->getByteLength()); |
| 999 | |
| 1000 | TargetInfo::ConstraintInfo Info; |
| 1001 | bool result = Target.validateOutputConstraint(OutputConstraint.c_str(), |
| 1002 | Info); |
Chris Lattner | 3304e55 | 2008-10-12 00:31:50 +0000 | [diff] [blame] | 1003 | assert(result && "Failed to parse output constraint"); result=result; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1004 | |
Anders Carlsson | 03eb543 | 2009-01-27 20:38:24 +0000 | [diff] [blame] | 1005 | OutputConstraintInfos.push_back(Info); |
| 1006 | |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1007 | // Simplify the output constraint. |
Lauro Ramos Venancio | a5694b8 | 2008-02-26 18:33:46 +0000 | [diff] [blame] | 1008 | OutputConstraint = SimplifyConstraint(OutputConstraint.c_str() + 1, Target); |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1009 | |
| 1010 | LValue Dest = EmitLValue(S.getOutputExpr(i)); |
| 1011 | const llvm::Type *DestValueType = |
| 1012 | cast<llvm::PointerType>(Dest.getAddress()->getType())->getElementType(); |
| 1013 | |
| 1014 | // If the first output operand is not a memory dest, we'll |
| 1015 | // make it the return value. |
| 1016 | if (i == 0 && !(Info & TargetInfo::CI_AllowsMemory) && |
Dan Gohman | d79a726 | 2008-05-22 22:12:56 +0000 | [diff] [blame] | 1017 | DestValueType->isSingleValueType()) { |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1018 | ResultAddr = Dest.getAddress(); |
| 1019 | ResultType = DestValueType; |
| 1020 | Constraints += "=" + OutputConstraint; |
| 1021 | } else { |
| 1022 | ArgTypes.push_back(Dest.getAddress()->getType()); |
Anders Carlsson | cad3ab6 | 2008-02-05 16:57:38 +0000 | [diff] [blame] | 1023 | Args.push_back(Dest.getAddress()); |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1024 | if (i != 0) |
| 1025 | Constraints += ','; |
Anders Carlsson | f39a421 | 2008-02-05 20:01:53 +0000 | [diff] [blame] | 1026 | Constraints += "=*"; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1027 | Constraints += OutputConstraint; |
Anders Carlsson | f39a421 | 2008-02-05 20:01:53 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | if (Info & TargetInfo::CI_ReadWrite) { |
Anders Carlsson | f39a421 | 2008-02-05 20:01:53 +0000 | [diff] [blame] | 1031 | InOutConstraints += ','; |
Anders Carlsson | 6347172 | 2009-01-11 19:32:54 +0000 | [diff] [blame] | 1032 | |
Anders Carlsson | f39a421 | 2008-02-05 20:01:53 +0000 | [diff] [blame] | 1033 | const Expr *InputExpr = S.getOutputExpr(i); |
Anders Carlsson | 6347172 | 2009-01-11 19:32:54 +0000 | [diff] [blame] | 1034 | llvm::Value *Arg = EmitAsmInput(S, Info, InputExpr, InOutConstraints); |
Anders Carlsson | f39a421 | 2008-02-05 20:01:53 +0000 | [diff] [blame] | 1035 | |
Anders Carlsson | 9f2505b | 2009-01-11 21:23:27 +0000 | [diff] [blame] | 1036 | if (Info & TargetInfo::CI_AllowsRegister) |
| 1037 | InOutConstraints += llvm::utostr(i); |
| 1038 | else |
| 1039 | InOutConstraints += OutputConstraint; |
Anders Carlsson | 2763b3a | 2009-01-11 19:46:50 +0000 | [diff] [blame] | 1040 | |
Anders Carlsson | f39a421 | 2008-02-05 20:01:53 +0000 | [diff] [blame] | 1041 | InOutArgTypes.push_back(Arg->getType()); |
| 1042 | InOutArgs.push_back(Arg); |
Anders Carlsson | f39a421 | 2008-02-05 20:01:53 +0000 | [diff] [blame] | 1043 | } |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | unsigned NumConstraints = S.getNumOutputs() + S.getNumInputs(); |
| 1047 | |
| 1048 | for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) { |
| 1049 | const Expr *InputExpr = S.getInputExpr(i); |
| 1050 | |
| 1051 | std::string InputConstraint(S.getInputConstraint(i)->getStrData(), |
| 1052 | S.getInputConstraint(i)->getByteLength()); |
| 1053 | |
| 1054 | TargetInfo::ConstraintInfo Info; |
| 1055 | bool result = Target.validateInputConstraint(InputConstraint.c_str(), |
Anders Carlsson | 45b050e | 2009-01-17 23:36:15 +0000 | [diff] [blame] | 1056 | S.begin_output_names(), |
| 1057 | S.end_output_names(), |
Anders Carlsson | 03eb543 | 2009-01-27 20:38:24 +0000 | [diff] [blame] | 1058 | &OutputConstraintInfos[0], |
Chris Lattner | 5363765 | 2009-01-21 07:35:26 +0000 | [diff] [blame] | 1059 | Info); result=result; |
Anders Carlsson | 42e1ee0 | 2009-01-18 01:56:57 +0000 | [diff] [blame] | 1060 | assert(result && "Failed to parse input constraint"); |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1061 | |
| 1062 | if (i != 0 || S.getNumOutputs() > 0) |
| 1063 | Constraints += ','; |
| 1064 | |
| 1065 | // Simplify the input constraint. |
Anders Carlsson | 300fb5d | 2009-01-18 02:06:20 +0000 | [diff] [blame] | 1066 | InputConstraint = SimplifyConstraint(InputConstraint.c_str(), Target, |
| 1067 | S.begin_output_names(), |
| 1068 | S.end_output_names()); |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1069 | |
Anders Carlsson | 6347172 | 2009-01-11 19:32:54 +0000 | [diff] [blame] | 1070 | llvm::Value *Arg = EmitAsmInput(S, Info, InputExpr, Constraints); |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1071 | |
| 1072 | ArgTypes.push_back(Arg->getType()); |
| 1073 | Args.push_back(Arg); |
| 1074 | Constraints += InputConstraint; |
| 1075 | } |
| 1076 | |
Anders Carlsson | f39a421 | 2008-02-05 20:01:53 +0000 | [diff] [blame] | 1077 | // Append the "input" part of inout constraints last. |
| 1078 | for (unsigned i = 0, e = InOutArgs.size(); i != e; i++) { |
| 1079 | ArgTypes.push_back(InOutArgTypes[i]); |
| 1080 | Args.push_back(InOutArgs[i]); |
| 1081 | } |
| 1082 | Constraints += InOutConstraints; |
| 1083 | |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1084 | // Clobbers |
| 1085 | for (unsigned i = 0, e = S.getNumClobbers(); i != e; i++) { |
| 1086 | std::string Clobber(S.getClobber(i)->getStrData(), |
| 1087 | S.getClobber(i)->getByteLength()); |
| 1088 | |
| 1089 | Clobber = Target.getNormalizedGCCRegisterName(Clobber.c_str()); |
| 1090 | |
Anders Carlsson | ea04175 | 2008-02-06 00:11:32 +0000 | [diff] [blame] | 1091 | if (i != 0 || NumConstraints != 0) |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1092 | Constraints += ','; |
Anders Carlsson | ea04175 | 2008-02-06 00:11:32 +0000 | [diff] [blame] | 1093 | |
| 1094 | Constraints += "~{"; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1095 | Constraints += Clobber; |
Anders Carlsson | ea04175 | 2008-02-06 00:11:32 +0000 | [diff] [blame] | 1096 | Constraints += '}'; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | // Add machine specific clobbers |
Eli Friedman | ccf614c | 2008-12-21 01:15:32 +0000 | [diff] [blame] | 1100 | std::string MachineClobbers = Target.getClobbers(); |
| 1101 | if (!MachineClobbers.empty()) { |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1102 | if (!Constraints.empty()) |
| 1103 | Constraints += ','; |
Eli Friedman | ccf614c | 2008-12-21 01:15:32 +0000 | [diff] [blame] | 1104 | Constraints += MachineClobbers; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1105 | } |
Anders Carlsson | f39a421 | 2008-02-05 20:01:53 +0000 | [diff] [blame] | 1106 | |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1107 | const llvm::FunctionType *FTy = |
| 1108 | llvm::FunctionType::get(ResultType, ArgTypes, false); |
| 1109 | |
| 1110 | llvm::InlineAsm *IA = |
| 1111 | llvm::InlineAsm::get(FTy, AsmString, Constraints, |
| 1112 | S.isVolatile() || S.getNumOutputs() == 0); |
| 1113 | llvm::Value *Result = Builder.CreateCall(IA, Args.begin(), Args.end(), ""); |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 1114 | if (ResultAddr) // FIXME: volatility |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1115 | Builder.CreateStore(Result, ResultAddr); |
| 1116 | } |