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