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