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