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