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