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