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 | |
| 14 | #include "CodeGenFunction.h" |
| 15 | #include "clang/AST/AST.h" |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 16 | #include "clang/Basic/TargetInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | #include "llvm/Constants.h" |
| 18 | #include "llvm/DerivedTypes.h" |
| 19 | #include "llvm/Function.h" |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 20 | #include "llvm/InlineAsm.h" |
| 21 | #include "llvm/ADT/StringExtras.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | using namespace CodeGen; |
| 24 | |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | // Statement Emission |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
| 29 | void CodeGenFunction::EmitStmt(const Stmt *S) { |
| 30 | assert(S && "Null statement?"); |
| 31 | |
| 32 | switch (S->getStmtClass()) { |
| 33 | default: |
Chris Lattner | 1e4d21e | 2007-08-26 22:58:05 +0000 | [diff] [blame] | 34 | // Must be an expression in a stmt context. Emit the value (to get |
| 35 | // side-effects) and ignore the result. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 36 | if (const Expr *E = dyn_cast<Expr>(S)) { |
Chris Lattner | 1e4d21e | 2007-08-26 22:58:05 +0000 | [diff] [blame] | 37 | if (!hasAggregateLLVMType(E->getType())) |
| 38 | EmitScalarExpr(E); |
| 39 | else if (E->getType()->isComplexType()) |
| 40 | EmitComplexExpr(E); |
| 41 | else |
| 42 | EmitAggExpr(E, 0, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 43 | } else { |
Chris Lattner | dc4d280 | 2007-12-02 01:49:16 +0000 | [diff] [blame] | 44 | WarnUnsupported(S, "statement"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 45 | } |
| 46 | break; |
| 47 | case Stmt::NullStmtClass: break; |
| 48 | case Stmt::CompoundStmtClass: EmitCompoundStmt(cast<CompoundStmt>(*S)); break; |
| 49 | case Stmt::LabelStmtClass: EmitLabelStmt(cast<LabelStmt>(*S)); break; |
| 50 | case Stmt::GotoStmtClass: EmitGotoStmt(cast<GotoStmt>(*S)); break; |
| 51 | |
| 52 | case Stmt::IfStmtClass: EmitIfStmt(cast<IfStmt>(*S)); break; |
| 53 | case Stmt::WhileStmtClass: EmitWhileStmt(cast<WhileStmt>(*S)); break; |
| 54 | case Stmt::DoStmtClass: EmitDoStmt(cast<DoStmt>(*S)); break; |
| 55 | case Stmt::ForStmtClass: EmitForStmt(cast<ForStmt>(*S)); break; |
| 56 | |
| 57 | case Stmt::ReturnStmtClass: EmitReturnStmt(cast<ReturnStmt>(*S)); break; |
| 58 | case Stmt::DeclStmtClass: EmitDeclStmt(cast<DeclStmt>(*S)); break; |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 59 | |
| 60 | case Stmt::BreakStmtClass: EmitBreakStmt(); break; |
| 61 | case Stmt::ContinueStmtClass: EmitContinueStmt(); break; |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 62 | case Stmt::SwitchStmtClass: EmitSwitchStmt(cast<SwitchStmt>(*S)); break; |
| 63 | case Stmt::DefaultStmtClass: EmitDefaultStmt(cast<DefaultStmt>(*S)); break; |
| 64 | case Stmt::CaseStmtClass: EmitCaseStmt(cast<CaseStmt>(*S)); break; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 65 | case Stmt::AsmStmtClass: EmitAsmStmt(cast<AsmStmt>(*S)); break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 69 | /// EmitCompoundStmt - Emit a compound statement {..} node. If GetLast is true, |
| 70 | /// this captures the expression result of the last sub-statement and returns it |
| 71 | /// (for use by the statement expression extension). |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 72 | RValue CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast, |
| 73 | llvm::Value *AggLoc, bool isAggVol) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 74 | // FIXME: handle vla's etc. |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 75 | if (S.body_empty() || !isa<Expr>(S.body_back())) GetLast = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 77 | for (CompoundStmt::const_body_iterator I = S.body_begin(), |
| 78 | E = S.body_end()-GetLast; I != E; ++I) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 79 | EmitStmt(*I); |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 80 | |
| 81 | |
| 82 | if (!GetLast) |
| 83 | return RValue::get(0); |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 84 | |
| 85 | return EmitAnyExpr(cast<Expr>(S.body_back()), AggLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void CodeGenFunction::EmitBlock(llvm::BasicBlock *BB) { |
| 89 | // Emit a branch from this block to the next one if this was a real block. If |
| 90 | // this was just a fall-through block after a terminator, don't emit it. |
| 91 | llvm::BasicBlock *LastBB = Builder.GetInsertBlock(); |
| 92 | |
| 93 | if (LastBB->getTerminator()) { |
| 94 | // If the previous block is already terminated, don't touch it. |
| 95 | } else if (LastBB->empty() && LastBB->getValueName() == 0) { |
| 96 | // If the last block was an empty placeholder, remove it now. |
| 97 | // TODO: cache and reuse these. |
| 98 | Builder.GetInsertBlock()->eraseFromParent(); |
| 99 | } else { |
| 100 | // Otherwise, create a fall-through branch. |
| 101 | Builder.CreateBr(BB); |
| 102 | } |
| 103 | CurFn->getBasicBlockList().push_back(BB); |
| 104 | Builder.SetInsertPoint(BB); |
| 105 | } |
| 106 | |
| 107 | void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) { |
| 108 | llvm::BasicBlock *NextBB = getBasicBlockForLabel(&S); |
| 109 | |
| 110 | EmitBlock(NextBB); |
| 111 | EmitStmt(S.getSubStmt()); |
| 112 | } |
| 113 | |
| 114 | void CodeGenFunction::EmitGotoStmt(const GotoStmt &S) { |
| 115 | Builder.CreateBr(getBasicBlockForLabel(S.getLabel())); |
| 116 | |
| 117 | // Emit a block after the branch so that dead code after a goto has some place |
| 118 | // to go. |
| 119 | Builder.SetInsertPoint(new llvm::BasicBlock("", CurFn)); |
| 120 | } |
| 121 | |
| 122 | void CodeGenFunction::EmitIfStmt(const IfStmt &S) { |
| 123 | // C99 6.8.4.1: The first substatement is executed if the expression compares |
| 124 | // unequal to 0. The condition must be a scalar type. |
| 125 | llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); |
| 126 | |
| 127 | llvm::BasicBlock *ContBlock = new llvm::BasicBlock("ifend"); |
| 128 | llvm::BasicBlock *ThenBlock = new llvm::BasicBlock("ifthen"); |
| 129 | llvm::BasicBlock *ElseBlock = ContBlock; |
| 130 | |
| 131 | if (S.getElse()) |
| 132 | ElseBlock = new llvm::BasicBlock("ifelse"); |
| 133 | |
| 134 | // Insert the conditional branch. |
| 135 | Builder.CreateCondBr(BoolCondVal, ThenBlock, ElseBlock); |
| 136 | |
| 137 | // Emit the 'then' code. |
| 138 | EmitBlock(ThenBlock); |
| 139 | EmitStmt(S.getThen()); |
Devang Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 140 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 141 | if (isDummyBlock(BB)) { |
| 142 | BB->eraseFromParent(); |
| 143 | Builder.SetInsertPoint(ThenBlock); |
| 144 | } |
| 145 | else |
| 146 | Builder.CreateBr(ContBlock); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 147 | |
| 148 | // Emit the 'else' code if present. |
| 149 | if (const Stmt *Else = S.getElse()) { |
| 150 | EmitBlock(ElseBlock); |
| 151 | EmitStmt(Else); |
Devang Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 152 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 153 | if (isDummyBlock(BB)) { |
| 154 | BB->eraseFromParent(); |
| 155 | Builder.SetInsertPoint(ElseBlock); |
| 156 | } |
| 157 | else |
| 158 | Builder.CreateBr(ContBlock); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | // Emit the continuation block for code after the if. |
| 162 | EmitBlock(ContBlock); |
| 163 | } |
| 164 | |
| 165 | void CodeGenFunction::EmitWhileStmt(const WhileStmt &S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 166 | // Emit the header for the loop, insert it, which will create an uncond br to |
| 167 | // it. |
| 168 | llvm::BasicBlock *LoopHeader = new llvm::BasicBlock("whilecond"); |
| 169 | EmitBlock(LoopHeader); |
| 170 | |
| 171 | // Evaluate the conditional in the while header. C99 6.8.5.1: The evaluation |
| 172 | // of the controlling expression takes place before each execution of the loop |
| 173 | // body. |
| 174 | llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); |
Devang Patel | 2c30d8f | 2007-10-09 20:51:27 +0000 | [diff] [blame] | 175 | |
| 176 | // while(1) is common, avoid extra exit blocks. Be sure |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 177 | // to correctly handle break/continue though. |
Devang Patel | 2c30d8f | 2007-10-09 20:51:27 +0000 | [diff] [blame] | 178 | bool EmitBoolCondBranch = true; |
| 179 | if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal)) |
| 180 | if (C->isOne()) |
| 181 | EmitBoolCondBranch = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 182 | |
| 183 | // Create an exit block for when the condition fails, create a block for the |
| 184 | // body of the loop. |
| 185 | llvm::BasicBlock *ExitBlock = new llvm::BasicBlock("whileexit"); |
| 186 | llvm::BasicBlock *LoopBody = new llvm::BasicBlock("whilebody"); |
| 187 | |
| 188 | // As long as the condition is true, go to the loop body. |
Devang Patel | 2c30d8f | 2007-10-09 20:51:27 +0000 | [diff] [blame] | 189 | if (EmitBoolCondBranch) |
| 190 | Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 191 | |
| 192 | // Store the blocks to use for break and continue. |
| 193 | BreakContinueStack.push_back(BreakContinue(ExitBlock, LoopHeader)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 194 | |
| 195 | // Emit the loop body. |
| 196 | EmitBlock(LoopBody); |
| 197 | EmitStmt(S.getBody()); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 198 | |
| 199 | BreakContinueStack.pop_back(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 200 | |
| 201 | // Cycle to the condition. |
| 202 | Builder.CreateBr(LoopHeader); |
| 203 | |
| 204 | // Emit the exit block. |
| 205 | EmitBlock(ExitBlock); |
Devang Patel | 2c30d8f | 2007-10-09 20:51:27 +0000 | [diff] [blame] | 206 | |
| 207 | // If LoopHeader is a simple forwarding block then eliminate it. |
| 208 | if (!EmitBoolCondBranch |
| 209 | && &LoopHeader->front() == LoopHeader->getTerminator()) { |
| 210 | LoopHeader->replaceAllUsesWith(LoopBody); |
| 211 | LoopHeader->getTerminator()->eraseFromParent(); |
| 212 | LoopHeader->eraseFromParent(); |
| 213 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | void CodeGenFunction::EmitDoStmt(const DoStmt &S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 217 | // Emit the body for the loop, insert it, which will create an uncond br to |
| 218 | // it. |
| 219 | llvm::BasicBlock *LoopBody = new llvm::BasicBlock("dobody"); |
| 220 | llvm::BasicBlock *AfterDo = new llvm::BasicBlock("afterdo"); |
| 221 | EmitBlock(LoopBody); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 222 | |
| 223 | llvm::BasicBlock *DoCond = new llvm::BasicBlock("docond"); |
| 224 | |
| 225 | // Store the blocks to use for break and continue. |
| 226 | BreakContinueStack.push_back(BreakContinue(AfterDo, DoCond)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 227 | |
| 228 | // Emit the body of the loop into the block. |
| 229 | EmitStmt(S.getBody()); |
| 230 | |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 231 | BreakContinueStack.pop_back(); |
| 232 | |
| 233 | EmitBlock(DoCond); |
| 234 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 235 | // C99 6.8.5.2: "The evaluation of the controlling expression takes place |
| 236 | // after each execution of the loop body." |
| 237 | |
| 238 | // Evaluate the conditional in the while header. |
| 239 | // C99 6.8.5p2/p4: The first substatement is executed if the expression |
| 240 | // compares unequal to 0. The condition must be a scalar type. |
| 241 | llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); |
Devang Patel | 05f6e6b | 2007-10-09 20:33:39 +0000 | [diff] [blame] | 242 | |
| 243 | // "do {} while (0)" is common in macros, avoid extra blocks. Be sure |
| 244 | // to correctly handle break/continue though. |
| 245 | bool EmitBoolCondBranch = true; |
| 246 | if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal)) |
| 247 | if (C->isZero()) |
| 248 | EmitBoolCondBranch = false; |
| 249 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 250 | // As long as the condition is true, iterate the loop. |
Devang Patel | 05f6e6b | 2007-10-09 20:33:39 +0000 | [diff] [blame] | 251 | if (EmitBoolCondBranch) |
| 252 | Builder.CreateCondBr(BoolCondVal, LoopBody, AfterDo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 253 | |
| 254 | // Emit the exit block. |
| 255 | EmitBlock(AfterDo); |
Devang Patel | 05f6e6b | 2007-10-09 20:33:39 +0000 | [diff] [blame] | 256 | |
| 257 | // If DoCond is a simple forwarding block then eliminate it. |
| 258 | if (!EmitBoolCondBranch && &DoCond->front() == DoCond->getTerminator()) { |
| 259 | DoCond->replaceAllUsesWith(AfterDo); |
| 260 | DoCond->getTerminator()->eraseFromParent(); |
| 261 | DoCond->eraseFromParent(); |
| 262 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | void CodeGenFunction::EmitForStmt(const ForStmt &S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 266 | // FIXME: What do we do if the increment (f.e.) contains a stmt expression, |
| 267 | // which contains a continue/break? |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 268 | // TODO: We could keep track of whether the loop body contains any |
| 269 | // break/continue statements and not create unnecessary blocks (like |
| 270 | // "afterfor" for a condless loop) if it doesn't. |
| 271 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 272 | // Evaluate the first part before the loop. |
| 273 | if (S.getInit()) |
| 274 | EmitStmt(S.getInit()); |
| 275 | |
| 276 | // Start the loop with a block that tests the condition. |
| 277 | llvm::BasicBlock *CondBlock = new llvm::BasicBlock("forcond"); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 278 | llvm::BasicBlock *AfterFor = new llvm::BasicBlock("afterfor"); |
| 279 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 280 | EmitBlock(CondBlock); |
| 281 | |
| 282 | // Evaluate the condition if present. If not, treat it as a non-zero-constant |
| 283 | // according to 6.8.5.3p2, aka, true. |
| 284 | if (S.getCond()) { |
| 285 | // C99 6.8.5p2/p4: The first substatement is executed if the expression |
| 286 | // compares unequal to 0. The condition must be a scalar type. |
| 287 | llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); |
| 288 | |
| 289 | // As long as the condition is true, iterate the loop. |
| 290 | llvm::BasicBlock *ForBody = new llvm::BasicBlock("forbody"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 291 | Builder.CreateCondBr(BoolCondVal, ForBody, AfterFor); |
| 292 | EmitBlock(ForBody); |
| 293 | } else { |
| 294 | // Treat it as a non-zero constant. Don't even create a new block for the |
| 295 | // body, just fall into it. |
| 296 | } |
| 297 | |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 298 | // If the for loop doesn't have an increment we can just use the |
| 299 | // condition as the continue block. |
| 300 | llvm::BasicBlock *ContinueBlock; |
| 301 | if (S.getInc()) |
| 302 | ContinueBlock = new llvm::BasicBlock("forinc"); |
| 303 | else |
| 304 | ContinueBlock = CondBlock; |
| 305 | |
| 306 | // Store the blocks to use for break and continue. |
| 307 | BreakContinueStack.push_back(BreakContinue(AfterFor, ContinueBlock)); |
| 308 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 309 | // If the condition is true, execute the body of the for stmt. |
| 310 | EmitStmt(S.getBody()); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 311 | |
| 312 | BreakContinueStack.pop_back(); |
| 313 | |
| 314 | if (S.getInc()) |
| 315 | EmitBlock(ContinueBlock); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 316 | |
| 317 | // If there is an increment, emit it next. |
| 318 | if (S.getInc()) |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 319 | EmitStmt(S.getInc()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 320 | |
| 321 | // Finally, branch back up to the condition for the next iteration. |
| 322 | Builder.CreateBr(CondBlock); |
| 323 | |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 324 | // Emit the fall-through block. |
| 325 | EmitBlock(AfterFor); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /// EmitReturnStmt - Note that due to GCC extensions, this can have an operand |
| 329 | /// if the function returns void, or may be missing one if the function returns |
| 330 | /// non-void. Fun stuff :). |
| 331 | void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 332 | // Emit the result value, even if unused, to evalute the side effects. |
| 333 | const Expr *RV = S.getRetValue(); |
Chris Lattner | 4b0029d | 2007-08-26 07:14:44 +0000 | [diff] [blame] | 334 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 335 | QualType FnRetTy = CurFuncDecl->getType().getCanonicalType(); |
| 336 | FnRetTy = cast<FunctionType>(FnRetTy)->getResultType(); |
| 337 | |
| 338 | if (FnRetTy->isVoidType()) { |
Chris Lattner | 4b0029d | 2007-08-26 07:14:44 +0000 | [diff] [blame] | 339 | // If the function returns void, emit ret void. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 340 | Builder.CreateRetVoid(); |
| 341 | } else if (RV == 0) { |
Chris Lattner | 4b0029d | 2007-08-26 07:14:44 +0000 | [diff] [blame] | 342 | // Handle "return;" in a function that returns a value. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 343 | const llvm::Type *RetTy = CurFn->getFunctionType()->getReturnType(); |
| 344 | if (RetTy == llvm::Type::VoidTy) |
| 345 | Builder.CreateRetVoid(); // struct return etc. |
| 346 | else |
| 347 | Builder.CreateRet(llvm::UndefValue::get(RetTy)); |
Chris Lattner | 4b0029d | 2007-08-26 07:14:44 +0000 | [diff] [blame] | 348 | } else if (!hasAggregateLLVMType(RV->getType())) { |
| 349 | Builder.CreateRet(EmitScalarExpr(RV)); |
| 350 | } else if (RV->getType()->isComplexType()) { |
| 351 | llvm::Value *SRetPtr = CurFn->arg_begin(); |
Chris Lattner | 190dbe2 | 2007-08-26 16:22:13 +0000 | [diff] [blame] | 352 | EmitComplexExprIntoAddr(RV, SRetPtr, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 353 | } else { |
Chris Lattner | 4b0029d | 2007-08-26 07:14:44 +0000 | [diff] [blame] | 354 | llvm::Value *SRetPtr = CurFn->arg_begin(); |
| 355 | EmitAggExpr(RV, SRetPtr, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | // Emit a block after the branch so that dead code after a return has some |
| 359 | // place to go. |
| 360 | EmitBlock(new llvm::BasicBlock()); |
| 361 | } |
| 362 | |
| 363 | void CodeGenFunction::EmitDeclStmt(const DeclStmt &S) { |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 364 | for (const ScopedDecl *Decl = S.getDecl(); Decl; |
| 365 | Decl = Decl->getNextDeclarator()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 366 | EmitDecl(*Decl); |
Chris Lattner | 6fa5f09 | 2007-07-12 15:43:07 +0000 | [diff] [blame] | 367 | } |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 368 | |
| 369 | void CodeGenFunction::EmitBreakStmt() { |
| 370 | assert(!BreakContinueStack.empty() && "break stmt not in a loop or switch!"); |
| 371 | |
| 372 | llvm::BasicBlock *Block = BreakContinueStack.back().BreakBlock; |
| 373 | Builder.CreateBr(Block); |
| 374 | EmitBlock(new llvm::BasicBlock()); |
| 375 | } |
| 376 | |
| 377 | void CodeGenFunction::EmitContinueStmt() { |
| 378 | assert(!BreakContinueStack.empty() && "continue stmt not in a loop!"); |
| 379 | |
| 380 | llvm::BasicBlock *Block = BreakContinueStack.back().ContinueBlock; |
| 381 | Builder.CreateBr(Block); |
| 382 | EmitBlock(new llvm::BasicBlock()); |
| 383 | } |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 384 | |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 385 | /// EmitCaseStmtRange - If case statement range is not too big then |
| 386 | /// add multiple cases to switch instruction, one for each value within |
| 387 | /// the range. If range is too big then emit "if" condition check. |
| 388 | void CodeGenFunction::EmitCaseStmtRange(const CaseStmt &S) { |
| 389 | assert (S.getRHS() && "Unexpected RHS value in CaseStmt"); |
| 390 | |
| 391 | const Expr *L = S.getLHS(); |
| 392 | const Expr *R = S.getRHS(); |
| 393 | llvm::ConstantInt *LV = cast<llvm::ConstantInt>(EmitScalarExpr(L)); |
| 394 | llvm::ConstantInt *RV = cast<llvm::ConstantInt>(EmitScalarExpr(R)); |
| 395 | llvm::APInt LHS = LV->getValue(); |
Devang Patel | 00ee4e4 | 2007-10-09 17:10:59 +0000 | [diff] [blame] | 396 | const llvm::APInt &RHS = RV->getValue(); |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 397 | |
| 398 | llvm::APInt Range = RHS - LHS; |
| 399 | if (Range.ult(llvm::APInt(Range.getBitWidth(), 64))) { |
| 400 | // Range is small enough to add multiple switch instruction cases. |
| 401 | StartBlock("sw.bb"); |
| 402 | llvm::BasicBlock *CaseDest = Builder.GetInsertBlock(); |
| 403 | SwitchInsn->addCase(LV, CaseDest); |
Devang Patel | 2d79d0f | 2007-10-05 20:54:07 +0000 | [diff] [blame] | 404 | LHS++; |
| 405 | while (LHS != RHS) { |
| 406 | SwitchInsn->addCase(llvm::ConstantInt::get(LHS), CaseDest); |
| 407 | LHS++; |
| 408 | } |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 409 | SwitchInsn->addCase(RV, CaseDest); |
| 410 | EmitStmt(S.getSubStmt()); |
| 411 | return; |
| 412 | } |
| 413 | |
| 414 | // The range is too big. Emit "if" condition. |
| 415 | llvm::BasicBlock *FalseDest = NULL; |
| 416 | llvm::BasicBlock *CaseDest = new llvm::BasicBlock("sw.bb"); |
Devang Patel | 2d79d0f | 2007-10-05 20:54:07 +0000 | [diff] [blame] | 417 | |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 418 | // If we have already seen one case statement range for this switch |
| 419 | // instruction then piggy-back otherwise use default block as false |
| 420 | // destination. |
| 421 | if (CaseRangeBlock) |
| 422 | FalseDest = CaseRangeBlock; |
| 423 | else |
| 424 | FalseDest = SwitchInsn->getDefaultDest(); |
| 425 | |
| 426 | // Start new block to hold case statement range check instructions. |
| 427 | StartBlock("case.range"); |
| 428 | CaseRangeBlock = Builder.GetInsertBlock(); |
| 429 | |
| 430 | // Emit range check. |
| 431 | llvm::Value *Diff = |
| 432 | Builder.CreateSub(SwitchInsn->getCondition(), LV, "tmp"); |
| 433 | llvm::Value *Cond = |
| 434 | Builder.CreateICmpULE(Diff, llvm::ConstantInt::get(Range), "tmp"); |
| 435 | Builder.CreateCondBr(Cond, CaseDest, FalseDest); |
| 436 | |
| 437 | // Now emit case statement body. |
| 438 | EmitBlock(CaseDest); |
| 439 | EmitStmt(S.getSubStmt()); |
| 440 | } |
| 441 | |
| 442 | void CodeGenFunction::EmitCaseStmt(const CaseStmt &S) { |
| 443 | if (S.getRHS()) { |
| 444 | EmitCaseStmtRange(S); |
| 445 | return; |
| 446 | } |
| 447 | |
| 448 | StartBlock("sw.bb"); |
| 449 | llvm::BasicBlock *CaseDest = Builder.GetInsertBlock(); |
Chris Lattner | c69a581 | 2007-11-30 17:44:57 +0000 | [diff] [blame] | 450 | llvm::APSInt CaseVal(32); |
| 451 | S.getLHS()->isIntegerConstantExpr(CaseVal, getContext()); |
| 452 | llvm::ConstantInt *LV = llvm::ConstantInt::get(CaseVal); |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 453 | SwitchInsn->addCase(LV, CaseDest); |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 454 | EmitStmt(S.getSubStmt()); |
| 455 | } |
| 456 | |
| 457 | void CodeGenFunction::EmitDefaultStmt(const DefaultStmt &S) { |
| 458 | StartBlock("sw.default"); |
| 459 | // Current insert block is the default destination. |
| 460 | SwitchInsn->setSuccessor(0, Builder.GetInsertBlock()); |
| 461 | EmitStmt(S.getSubStmt()); |
| 462 | } |
| 463 | |
| 464 | void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) { |
| 465 | llvm::Value *CondV = EmitScalarExpr(S.getCond()); |
| 466 | |
| 467 | // Handle nested switch statements. |
| 468 | llvm::SwitchInst *SavedSwitchInsn = SwitchInsn; |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 469 | llvm::BasicBlock *SavedCRBlock = CaseRangeBlock; |
| 470 | CaseRangeBlock = NULL; |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 471 | |
| 472 | // Create basic block to hold stuff that comes after switch statement. |
| 473 | // Initially use it to hold DefaultStmt. |
Chris Lattner | 1438b49 | 2007-12-01 05:27:33 +0000 | [diff] [blame] | 474 | llvm::BasicBlock *NextBlock = new llvm::BasicBlock("after.sw"); |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 475 | SwitchInsn = Builder.CreateSwitch(CondV, NextBlock); |
| 476 | |
Devang Patel | e9b8c0a | 2007-10-30 20:59:40 +0000 | [diff] [blame] | 477 | // All break statements jump to NextBlock. If BreakContinueStack is non empty |
| 478 | // then reuse last ContinueBlock. |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 479 | llvm::BasicBlock *ContinueBlock = NULL; |
| 480 | if (!BreakContinueStack.empty()) |
| 481 | ContinueBlock = BreakContinueStack.back().ContinueBlock; |
| 482 | BreakContinueStack.push_back(BreakContinue(NextBlock, ContinueBlock)); |
| 483 | |
| 484 | // Emit switch body. |
| 485 | EmitStmt(S.getBody()); |
| 486 | BreakContinueStack.pop_back(); |
| 487 | |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 488 | // If one or more case statement range is seen then use CaseRangeBlock |
| 489 | // as the default block. False edge of CaseRangeBlock will lead to |
| 490 | // original default block. |
| 491 | if (CaseRangeBlock) |
| 492 | SwitchInsn->setSuccessor(0, CaseRangeBlock); |
| 493 | |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 494 | // Prune insert block if it is dummy. |
| 495 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 496 | if (isDummyBlock(BB)) |
| 497 | BB->eraseFromParent(); |
Chris Lattner | 1438b49 | 2007-12-01 05:27:33 +0000 | [diff] [blame] | 498 | else // Otherwise, branch to continuation. |
| 499 | Builder.CreateBr(NextBlock); |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 500 | |
| 501 | // Place NextBlock as the new insert point. |
Chris Lattner | 1438b49 | 2007-12-01 05:27:33 +0000 | [diff] [blame] | 502 | CurFn->getBasicBlockList().push_back(NextBlock); |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 503 | Builder.SetInsertPoint(NextBlock); |
| 504 | SwitchInsn = SavedSwitchInsn; |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 505 | CaseRangeBlock = SavedCRBlock; |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 506 | } |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 507 | |
| 508 | static inline std::string ConvertAsmString(const char *Start, |
Anders Carlsson | 2abd25f | 2008-02-05 23:18:57 +0000 | [diff] [blame] | 509 | unsigned NumOperands, |
| 510 | bool IsSimple) |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 511 | { |
| 512 | static unsigned AsmCounter = 0; |
| 513 | |
| 514 | AsmCounter++; |
| 515 | |
| 516 | std::string Result; |
Anders Carlsson | 2abd25f | 2008-02-05 23:18:57 +0000 | [diff] [blame] | 517 | if (IsSimple) { |
| 518 | while (*Start) { |
| 519 | switch (*Start) { |
| 520 | default: |
| 521 | Result += *Start; |
| 522 | break; |
| 523 | case '$': |
| 524 | Result += "$$"; |
| 525 | break; |
| 526 | } |
| 527 | |
| 528 | Start++; |
| 529 | } |
| 530 | |
| 531 | return Result; |
| 532 | } |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 533 | |
| 534 | while (*Start) { |
| 535 | switch (*Start) { |
| 536 | default: |
| 537 | Result += *Start; |
| 538 | break; |
| 539 | case '$': |
| 540 | Result += "$$"; |
| 541 | break; |
| 542 | case '%': |
| 543 | // Escaped character |
| 544 | Start++; |
| 545 | if (!*Start) { |
| 546 | // FIXME: This should be caught during Sema. |
| 547 | assert(0 && "Trailing '%' in asm string."); |
| 548 | } |
| 549 | |
| 550 | char EscapedChar = *Start; |
| 551 | if (EscapedChar == '%') { |
| 552 | // Escaped percentage sign. |
| 553 | Result += '%'; |
| 554 | } |
| 555 | else if (EscapedChar == '=') { |
| 556 | // Generate an unique ID. |
| 557 | Result += llvm::utostr(AsmCounter); |
| 558 | } else if (isdigit(EscapedChar)) { |
| 559 | // %n - Assembler operand n |
| 560 | char *End; |
| 561 | |
| 562 | unsigned long n = strtoul(Start, &End, 10); |
| 563 | if (Start == End) { |
| 564 | // FIXME: This should be caught during Sema. |
| 565 | assert(0 && "Missing operand!"); |
| 566 | } else if (n >= NumOperands) { |
| 567 | // FIXME: This should be caught during Sema. |
| 568 | assert(0 && "Operand number out of range!"); |
| 569 | } |
| 570 | |
| 571 | Result += '$' + llvm::utostr(n); |
Lauro Ramos Venancio | 7695f70 | 2008-02-26 19:19:58 +0000 | [diff] [blame^] | 572 | Start = End - 1; |
Anders Carlsson | 2abd25f | 2008-02-05 23:18:57 +0000 | [diff] [blame] | 573 | } else if (isalpha(EscapedChar)) { |
| 574 | char *End; |
| 575 | |
| 576 | unsigned long n = strtoul(Start + 1, &End, 10); |
| 577 | if (Start == End) { |
| 578 | // FIXME: This should be caught during Sema. |
| 579 | assert(0 && "Missing operand!"); |
| 580 | } else if (n >= NumOperands) { |
| 581 | // FIXME: This should be caught during Sema. |
| 582 | assert(0 && "Operand number out of range!"); |
| 583 | } |
| 584 | |
| 585 | Result += "${" + llvm::utostr(n) + ':' + EscapedChar + '}'; |
Lauro Ramos Venancio | 7695f70 | 2008-02-26 19:19:58 +0000 | [diff] [blame^] | 586 | Start = End - 1; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 587 | } else { |
| 588 | assert(0 && "Unhandled asm escaped character!"); |
| 589 | } |
| 590 | } |
| 591 | Start++; |
| 592 | } |
| 593 | |
| 594 | return Result; |
| 595 | } |
| 596 | |
Lauro Ramos Venancio | a5694b8 | 2008-02-26 18:33:46 +0000 | [diff] [blame] | 597 | static std::string SimplifyConstraint(const char* Constraint, |
| 598 | TargetInfo &Target) { |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 599 | std::string Result; |
| 600 | |
| 601 | while (*Constraint) { |
| 602 | switch (*Constraint) { |
| 603 | default: |
Lauro Ramos Venancio | a5694b8 | 2008-02-26 18:33:46 +0000 | [diff] [blame] | 604 | Result += Target.convertConstraint(*Constraint); |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 605 | break; |
| 606 | // Ignore these |
| 607 | case '*': |
| 608 | case '?': |
| 609 | case '!': |
| 610 | break; |
| 611 | case 'g': |
| 612 | Result += "imr"; |
| 613 | break; |
| 614 | } |
| 615 | |
| 616 | Constraint++; |
| 617 | } |
| 618 | |
| 619 | return Result; |
| 620 | } |
| 621 | |
| 622 | void CodeGenFunction::EmitAsmStmt(const AsmStmt &S) { |
| 623 | std::string AsmString = |
| 624 | ConvertAsmString(std::string(S.getAsmString()->getStrData(), |
| 625 | S.getAsmString()->getByteLength()).c_str(), |
Anders Carlsson | 2abd25f | 2008-02-05 23:18:57 +0000 | [diff] [blame] | 626 | S.getNumOutputs() + S.getNumInputs(), S.isSimple()); |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 627 | |
| 628 | std::string Constraints; |
| 629 | |
| 630 | llvm::Value *ResultAddr = 0; |
| 631 | const llvm::Type *ResultType = llvm::Type::VoidTy; |
| 632 | |
| 633 | std::vector<const llvm::Type*> ArgTypes; |
| 634 | std::vector<llvm::Value*> Args; |
Anders Carlsson | f39a421 | 2008-02-05 20:01:53 +0000 | [diff] [blame] | 635 | |
| 636 | // Keep track of inout constraints. |
| 637 | std::string InOutConstraints; |
| 638 | std::vector<llvm::Value*> InOutArgs; |
| 639 | std::vector<const llvm::Type*> InOutArgTypes; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 640 | |
| 641 | for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) { |
| 642 | std::string OutputConstraint(S.getOutputConstraint(i)->getStrData(), |
| 643 | S.getOutputConstraint(i)->getByteLength()); |
| 644 | |
| 645 | TargetInfo::ConstraintInfo Info; |
| 646 | bool result = Target.validateOutputConstraint(OutputConstraint.c_str(), |
| 647 | Info); |
| 648 | assert(result && "Failed to parse output constraint"); |
| 649 | |
| 650 | // Simplify the output constraint. |
Lauro Ramos Venancio | a5694b8 | 2008-02-26 18:33:46 +0000 | [diff] [blame] | 651 | OutputConstraint = SimplifyConstraint(OutputConstraint.c_str() + 1, Target); |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 652 | |
| 653 | LValue Dest = EmitLValue(S.getOutputExpr(i)); |
| 654 | const llvm::Type *DestValueType = |
| 655 | cast<llvm::PointerType>(Dest.getAddress()->getType())->getElementType(); |
| 656 | |
| 657 | // If the first output operand is not a memory dest, we'll |
| 658 | // make it the return value. |
| 659 | if (i == 0 && !(Info & TargetInfo::CI_AllowsMemory) && |
| 660 | DestValueType->isFirstClassType()) { |
| 661 | ResultAddr = Dest.getAddress(); |
| 662 | ResultType = DestValueType; |
| 663 | Constraints += "=" + OutputConstraint; |
| 664 | } else { |
| 665 | ArgTypes.push_back(Dest.getAddress()->getType()); |
Anders Carlsson | cad3ab6 | 2008-02-05 16:57:38 +0000 | [diff] [blame] | 666 | Args.push_back(Dest.getAddress()); |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 667 | if (i != 0) |
| 668 | Constraints += ','; |
Anders Carlsson | f39a421 | 2008-02-05 20:01:53 +0000 | [diff] [blame] | 669 | Constraints += "=*"; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 670 | Constraints += OutputConstraint; |
Anders Carlsson | f39a421 | 2008-02-05 20:01:53 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | if (Info & TargetInfo::CI_ReadWrite) { |
| 674 | // FIXME: This code should be shared with the code that handles inputs. |
| 675 | InOutConstraints += ','; |
| 676 | |
| 677 | const Expr *InputExpr = S.getOutputExpr(i); |
| 678 | llvm::Value *Arg; |
| 679 | if ((Info & TargetInfo::CI_AllowsRegister) || |
| 680 | !(Info & TargetInfo::CI_AllowsMemory)) { |
| 681 | if (ConvertType(InputExpr->getType())->isFirstClassType()) { |
| 682 | Arg = EmitScalarExpr(InputExpr); |
| 683 | } else { |
| 684 | assert(0 && "FIXME: Implement passing non first class types as inputs"); |
| 685 | } |
| 686 | } else { |
| 687 | LValue Dest = EmitLValue(InputExpr); |
| 688 | Arg = Dest.getAddress(); |
| 689 | InOutConstraints += '*'; |
| 690 | } |
| 691 | |
| 692 | InOutArgTypes.push_back(Arg->getType()); |
| 693 | InOutArgs.push_back(Arg); |
| 694 | InOutConstraints += OutputConstraint; |
| 695 | } |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | unsigned NumConstraints = S.getNumOutputs() + S.getNumInputs(); |
| 699 | |
| 700 | for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) { |
| 701 | const Expr *InputExpr = S.getInputExpr(i); |
| 702 | |
| 703 | std::string InputConstraint(S.getInputConstraint(i)->getStrData(), |
| 704 | S.getInputConstraint(i)->getByteLength()); |
| 705 | |
| 706 | TargetInfo::ConstraintInfo Info; |
| 707 | bool result = Target.validateInputConstraint(InputConstraint.c_str(), |
| 708 | NumConstraints, |
| 709 | Info); |
| 710 | assert(result && "Failed to parse input constraint"); |
| 711 | |
| 712 | if (i != 0 || S.getNumOutputs() > 0) |
| 713 | Constraints += ','; |
| 714 | |
| 715 | // Simplify the input constraint. |
Lauro Ramos Venancio | a5694b8 | 2008-02-26 18:33:46 +0000 | [diff] [blame] | 716 | InputConstraint = SimplifyConstraint(InputConstraint.c_str(), Target); |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 717 | |
| 718 | llvm::Value *Arg; |
| 719 | |
| 720 | if ((Info & TargetInfo::CI_AllowsRegister) || |
| 721 | !(Info & TargetInfo::CI_AllowsMemory)) { |
| 722 | if (ConvertType(InputExpr->getType())->isFirstClassType()) { |
| 723 | Arg = EmitScalarExpr(InputExpr); |
| 724 | } else { |
| 725 | assert(0 && "FIXME: Implement passing non first class types as inputs"); |
| 726 | } |
| 727 | } else { |
| 728 | LValue Dest = EmitLValue(InputExpr); |
| 729 | Arg = Dest.getAddress(); |
| 730 | Constraints += '*'; |
| 731 | } |
| 732 | |
| 733 | ArgTypes.push_back(Arg->getType()); |
| 734 | Args.push_back(Arg); |
| 735 | Constraints += InputConstraint; |
| 736 | } |
| 737 | |
Anders Carlsson | f39a421 | 2008-02-05 20:01:53 +0000 | [diff] [blame] | 738 | // Append the "input" part of inout constraints last. |
| 739 | for (unsigned i = 0, e = InOutArgs.size(); i != e; i++) { |
| 740 | ArgTypes.push_back(InOutArgTypes[i]); |
| 741 | Args.push_back(InOutArgs[i]); |
| 742 | } |
| 743 | Constraints += InOutConstraints; |
| 744 | |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 745 | // Clobbers |
| 746 | for (unsigned i = 0, e = S.getNumClobbers(); i != e; i++) { |
| 747 | std::string Clobber(S.getClobber(i)->getStrData(), |
| 748 | S.getClobber(i)->getByteLength()); |
| 749 | |
| 750 | Clobber = Target.getNormalizedGCCRegisterName(Clobber.c_str()); |
| 751 | |
Anders Carlsson | ea04175 | 2008-02-06 00:11:32 +0000 | [diff] [blame] | 752 | if (i != 0 || NumConstraints != 0) |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 753 | Constraints += ','; |
Anders Carlsson | ea04175 | 2008-02-06 00:11:32 +0000 | [diff] [blame] | 754 | |
| 755 | Constraints += "~{"; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 756 | Constraints += Clobber; |
Anders Carlsson | ea04175 | 2008-02-06 00:11:32 +0000 | [diff] [blame] | 757 | Constraints += '}'; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | // Add machine specific clobbers |
| 761 | if (const char *C = Target.getClobbers()) { |
| 762 | if (!Constraints.empty()) |
| 763 | Constraints += ','; |
| 764 | Constraints += C; |
| 765 | } |
Anders Carlsson | f39a421 | 2008-02-05 20:01:53 +0000 | [diff] [blame] | 766 | |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 767 | const llvm::FunctionType *FTy = |
| 768 | llvm::FunctionType::get(ResultType, ArgTypes, false); |
| 769 | |
| 770 | llvm::InlineAsm *IA = |
| 771 | llvm::InlineAsm::get(FTy, AsmString, Constraints, |
| 772 | S.isVolatile() || S.getNumOutputs() == 0); |
| 773 | llvm::Value *Result = Builder.CreateCall(IA, Args.begin(), Args.end(), ""); |
| 774 | if (ResultAddr) |
| 775 | Builder.CreateStore(Result, ResultAddr); |
| 776 | } |