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