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