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