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