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