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