Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===// |
| 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 coordinates the per-function state used while generating code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
| 15 | #include "CodeGenModule.h" |
Eli Friedman | 9bc7c8d | 2008-05-22 01:40:10 +0000 | [diff] [blame] | 16 | #include "CGDebugInfo.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 3d6606b | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 18 | #include "clang/AST/APValue.h" |
Daniel Dunbar | eee5cd1 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 64789f8 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 20 | #include "clang/AST/Decl.h" |
Devang Patel | 9729936 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CFG.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | using namespace CodeGen; |
| 24 | |
| 25 | CodeGenFunction::CodeGenFunction(CodeGenModule &cgm) |
Devang Patel | 347ca32 | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 26 | : CGM(cgm), Target(CGM.getContext().Target), SwitchInsn(NULL), |
Chris Lattner | 8c7c6a1 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 27 | CaseRangeBlock(NULL) { |
| 28 | LLVMIntTy = ConvertType(getContext().IntTy); |
| 29 | LLVMPointerWidth = Target.getPointerWidth(0); |
| 30 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 31 | |
| 32 | ASTContext &CodeGenFunction::getContext() const { |
| 33 | return CGM.getContext(); |
| 34 | } |
| 35 | |
| 36 | |
| 37 | llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) { |
| 38 | llvm::BasicBlock *&BB = LabelMap[S]; |
| 39 | if (BB) return BB; |
| 40 | |
| 41 | // Create, but don't insert, the new block. |
Daniel Dunbar | 72f9655 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 42 | return BB = createBasicBlock(S->getName()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Lauro Ramos Venancio | 934fb02 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 45 | llvm::Constant * |
Steve Naroff | 72a6ebc | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 46 | CodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) { |
Lauro Ramos Venancio | 934fb02 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 47 | return cast<llvm::Constant>(LocalDeclMap[BVD]); |
| 48 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 49 | |
Anders Carlsson | 75d8673 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 50 | llvm::Value *CodeGenFunction::GetAddrOfLocalVar(const VarDecl *VD) |
| 51 | { |
| 52 | return LocalDeclMap[VD]; |
| 53 | } |
| 54 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 55 | const llvm::Type *CodeGenFunction::ConvertType(QualType T) { |
| 56 | return CGM.getTypes().ConvertType(T); |
| 57 | } |
| 58 | |
Chris Lattner | 8c7c6a1 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 59 | bool CodeGenFunction::isObjCPointerType(QualType T) { |
| 60 | // All Objective-C types are pointers. |
| 61 | return T->isObjCInterfaceType() || |
| 62 | T->isObjCQualifiedInterfaceType() || T->isObjCQualifiedIdType(); |
| 63 | } |
| 64 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 65 | bool CodeGenFunction::hasAggregateLLVMType(QualType T) { |
Chris Lattner | 8c7c6a1 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 66 | return !isObjCPointerType(T) &&!T->isRealType() && !T->isPointerLikeType() && |
| 67 | !T->isVoidType() && !T->isVectorType() && !T->isFunctionType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Daniel Dunbar | 6b57d43 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 70 | void CodeGenFunction::FinishFunction(SourceLocation EndLoc) { |
Daniel Dunbar | 879788d | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 71 | // Finish emission of indirect switches. |
| 72 | EmitIndirectSwitches(); |
| 73 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 74 | assert(BreakContinueStack.empty() && |
| 75 | "mismatched push/pop in break/continue stack!"); |
Daniel Dunbar | 9fb751f | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 76 | |
Daniel Dunbar | 7a57b8d | 2008-09-27 07:15:59 +0000 | [diff] [blame] | 77 | // Emit function epilog (to return). This has the nice side effect |
| 78 | // of also automatically handling code that falls off the end. |
| 79 | EmitBlock(ReturnBlock); |
Daniel Dunbar | 03f7ae1 | 2008-11-11 20:59:54 +0000 | [diff] [blame] | 80 | |
| 81 | // Emit debug descriptor for function end. |
| 82 | if (CGDebugInfo *DI = CGM.getDebugInfo()) { |
| 83 | DI->setLocation(EndLoc); |
| 84 | DI->EmitRegionEnd(CurFn, Builder); |
| 85 | } |
| 86 | |
Daniel Dunbar | fc1a9c4 | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 87 | EmitFunctionEpilog(FnRetTy, ReturnValue); |
Daniel Dunbar | 9fb751f | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 88 | |
Chris Lattner | ca92981 | 2007-12-02 06:32:24 +0000 | [diff] [blame] | 89 | // Remove the AllocaInsertPt instruction, which is just a convenience for us. |
| 90 | AllocaInsertPt->eraseFromParent(); |
| 91 | AllocaInsertPt = 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Daniel Dunbar | 9681683 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 94 | void CodeGenFunction::StartFunction(const Decl *D, QualType RetTy, |
| 95 | llvm::Function *Fn, |
Daniel Dunbar | 54968bf | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 96 | const FunctionArgList &Args, |
| 97 | SourceLocation StartLoc) { |
Daniel Dunbar | 9681683 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 98 | CurFuncDecl = D; |
| 99 | FnRetTy = RetTy; |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 100 | CurFn = Fn; |
Chris Lattner | 8c7c6a1 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 101 | assert(CurFn->isDeclaration() && "Function already has body?"); |
| 102 | |
Daniel Dunbar | 72f9655 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 103 | llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn); |
Daniel Dunbar | 9fb751f | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 104 | |
Chris Lattner | 8c7c6a1 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 105 | // Create a marker to make it easy to insert allocas into the entryblock |
| 106 | // later. Don't create this with the builder, because we don't want it |
| 107 | // folded. |
| 108 | llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty); |
| 109 | AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt", |
| 110 | EntryBB); |
Daniel Dunbar | 9fb751f | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 111 | |
Daniel Dunbar | 72f9655 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 112 | ReturnBlock = createBasicBlock("return"); |
Daniel Dunbar | 9fb751f | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 113 | ReturnValue = 0; |
Daniel Dunbar | 9681683 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 114 | if (!RetTy->isVoidType()) |
| 115 | ReturnValue = CreateTempAlloca(ConvertType(RetTy), "retval"); |
Daniel Dunbar | 9fb751f | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 116 | |
Chris Lattner | 8c7c6a1 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 117 | Builder.SetInsertPoint(EntryBB); |
| 118 | |
Sanjiv Gupta | 1d340eb | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 119 | // Emit subprogram debug descriptor. |
Daniel Dunbar | 9681683 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 120 | // FIXME: The cast here is a huge hack. |
Daniel Dunbar | 54968bf | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 121 | if (CGDebugInfo *DI = CGM.getDebugInfo()) { |
| 122 | DI->setLocation(StartLoc); |
| 123 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame^] | 124 | DI->EmitFunctionStart(FD->getIdentifier()->getName(), |
| 125 | RetTy, CurFn, Builder); |
Daniel Dunbar | 54968bf | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 126 | } else { |
| 127 | // Just use LLVM function name. |
| 128 | DI->EmitFunctionStart(Fn->getName().c_str(), |
| 129 | RetTy, CurFn, Builder); |
Sanjiv Gupta | 1d340eb | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 130 | } |
Sanjiv Gupta | 1d340eb | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Daniel Dunbar | fc1a9c4 | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 133 | EmitFunctionProlog(CurFn, FnRetTy, Args); |
Daniel Dunbar | 9681683 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 134 | } |
Eli Friedman | 769e730 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 135 | |
Daniel Dunbar | 9681683 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 136 | void CodeGenFunction::GenerateCode(const FunctionDecl *FD, |
| 137 | llvm::Function *Fn) { |
| 138 | FunctionArgList Args; |
Eli Friedman | 769e730 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 139 | if (FD->getNumParams()) { |
| 140 | const FunctionTypeProto* FProto = FD->getType()->getAsFunctionTypeProto(); |
| 141 | assert(FProto && "Function def must have prototype!"); |
Daniel Dunbar | 9681683 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 142 | |
| 143 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) |
| 144 | Args.push_back(std::make_pair(FD->getParamDecl(i), |
| 145 | FProto->getArgType(i))); |
Chris Lattner | 8c7c6a1 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 146 | } |
Daniel Dunbar | 6b57d43 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 147 | |
Daniel Dunbar | 54968bf | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 148 | StartFunction(FD, FD->getResultType(), Fn, Args, |
| 149 | cast<CompoundStmt>(FD->getBody())->getLBracLoc()); |
Daniel Dunbar | 9681683 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 150 | |
Daniel Dunbar | 6b57d43 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 151 | EmitStmt(FD->getBody()); |
| 152 | |
| 153 | const CompoundStmt *S = dyn_cast<CompoundStmt>(FD->getBody()); |
| 154 | if (S) { |
| 155 | FinishFunction(S->getRBracLoc()); |
| 156 | } else { |
| 157 | FinishFunction(); |
| 158 | } |
Chris Lattner | 8c7c6a1 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Chris Lattner | 3f73d0d | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 161 | /// ContainsLabel - Return true if the statement contains a label in it. If |
| 162 | /// this statement is not executed normally, it not containing a label means |
| 163 | /// that we can just remove the code. |
| 164 | bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) { |
| 165 | // Null statement, not a label! |
| 166 | if (S == 0) return false; |
| 167 | |
| 168 | // If this is a label, we have to emit the code, consider something like: |
| 169 | // if (0) { ... foo: bar(); } goto foo; |
| 170 | if (isa<LabelStmt>(S)) |
| 171 | return true; |
| 172 | |
| 173 | // If this is a case/default statement, and we haven't seen a switch, we have |
| 174 | // to emit the code. |
| 175 | if (isa<SwitchCase>(S) && !IgnoreCaseStmts) |
| 176 | return true; |
| 177 | |
| 178 | // If this is a switch statement, we want to ignore cases below it. |
| 179 | if (isa<SwitchStmt>(S)) |
| 180 | IgnoreCaseStmts = true; |
| 181 | |
| 182 | // Scan subexpressions for verboten labels. |
| 183 | for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); |
| 184 | I != E; ++I) |
| 185 | if (ContainsLabel(*I, IgnoreCaseStmts)) |
| 186 | return true; |
| 187 | |
| 188 | return false; |
| 189 | } |
| 190 | |
Chris Lattner | 3d6606b | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 191 | |
| 192 | /// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to |
| 193 | /// a constant, or if it does but contains a label, return 0. If it constant |
| 194 | /// folds to 'true' and does not contain a label, return 1, if it constant folds |
| 195 | /// to 'false' and does not contain a label, return -1. |
| 196 | int CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) { |
| 197 | APValue V; |
Daniel Dunbar | 1493702 | 2008-11-12 22:37:10 +0000 | [diff] [blame] | 198 | |
| 199 | // FIXME: Rename and handle conversion of other evaluatable things |
| 200 | // to bool. |
Anders Carlsson | ea3c6ab | 2008-11-22 22:32:07 +0000 | [diff] [blame] | 201 | bool isEvaluated; |
| 202 | if (!Cond->Evaluate(V, getContext(), &isEvaluated) || !V.isInt() || |
| 203 | !isEvaluated) |
| 204 | return 0; // Not foldable, not integer or not fully evaluatable. |
Chris Lattner | 3d6606b | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 205 | |
| 206 | if (CodeGenFunction::ContainsLabel(Cond)) |
| 207 | return 0; // Contains a label. |
| 208 | |
| 209 | return V.getInt().getBoolValue() ? 1 : -1; |
| 210 | } |
| 211 | |
| 212 | |
| 213 | /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if |
| 214 | /// statement) to the specified blocks. Based on the condition, this might try |
| 215 | /// to simplify the codegen of the conditional based on the branch. |
| 216 | /// |
| 217 | void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond, |
| 218 | llvm::BasicBlock *TrueBlock, |
| 219 | llvm::BasicBlock *FalseBlock) { |
| 220 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond)) |
| 221 | return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock); |
| 222 | |
| 223 | if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) { |
| 224 | // Handle X && Y in a condition. |
| 225 | if (CondBOp->getOpcode() == BinaryOperator::LAnd) { |
| 226 | // If we have "1 && X", simplify the code. "0 && X" would have constant |
| 227 | // folded if the case was simple enough. |
| 228 | if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) { |
| 229 | // br(1 && X) -> br(X). |
| 230 | return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 231 | } |
| 232 | |
| 233 | // If we have "X && 1", simplify the code to use an uncond branch. |
| 234 | // "X && 0" would have been constant folded to 0. |
| 235 | if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) { |
| 236 | // br(X && 1) -> br(X). |
| 237 | return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); |
| 238 | } |
| 239 | |
| 240 | // Emit the LHS as a conditional. If the LHS conditional is false, we |
| 241 | // want to jump to the FalseBlock. |
Daniel Dunbar | 6e3a10c | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 242 | llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true"); |
Chris Lattner | 3d6606b | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 243 | EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock); |
| 244 | EmitBlock(LHSTrue); |
| 245 | |
| 246 | EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 247 | return; |
| 248 | } else if (CondBOp->getOpcode() == BinaryOperator::LOr) { |
| 249 | // If we have "0 || X", simplify the code. "1 || X" would have constant |
| 250 | // folded if the case was simple enough. |
| 251 | if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) { |
| 252 | // br(0 || X) -> br(X). |
| 253 | return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 254 | } |
| 255 | |
| 256 | // If we have "X || 0", simplify the code to use an uncond branch. |
| 257 | // "X || 1" would have been constant folded to 1. |
| 258 | if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) { |
| 259 | // br(X || 0) -> br(X). |
| 260 | return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); |
| 261 | } |
| 262 | |
| 263 | // Emit the LHS as a conditional. If the LHS conditional is true, we |
| 264 | // want to jump to the TrueBlock. |
Daniel Dunbar | 6e3a10c | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 265 | llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false"); |
Chris Lattner | 3d6606b | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 266 | EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse); |
| 267 | EmitBlock(LHSFalse); |
| 268 | |
| 269 | EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 270 | return; |
| 271 | } |
Chris Lattner | cfbfe91 | 2008-11-12 08:13:36 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) { |
| 275 | // br(!x, t, f) -> br(x, f, t) |
| 276 | if (CondUOp->getOpcode() == UnaryOperator::LNot) |
| 277 | return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock); |
Chris Lattner | 3d6606b | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Daniel Dunbar | ab81c63 | 2008-11-12 10:30:32 +0000 | [diff] [blame] | 280 | if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) { |
| 281 | // Handle ?: operator. |
| 282 | |
| 283 | // Just ignore GNU ?: extension. |
| 284 | if (CondOp->getLHS()) { |
| 285 | // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f)) |
| 286 | llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true"); |
| 287 | llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false"); |
| 288 | EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock); |
| 289 | EmitBlock(LHSBlock); |
| 290 | EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock); |
| 291 | EmitBlock(RHSBlock); |
| 292 | EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock); |
| 293 | return; |
| 294 | } |
| 295 | } |
| 296 | |
Chris Lattner | 3d6606b | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 297 | // Emit the code with the fully general case. |
| 298 | llvm::Value *CondV = EvaluateExprAsBool(Cond); |
| 299 | Builder.CreateCondBr(CondV, TrueBlock, FalseBlock); |
| 300 | } |
| 301 | |
Devang Patel | 7a78e43 | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 302 | /// getCGRecordLayout - Return record layout info. |
| 303 | const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT, |
Chris Lattner | 7b2543e | 2008-02-05 06:55:31 +0000 | [diff] [blame] | 304 | QualType Ty) { |
| 305 | const RecordType *RTy = Ty->getAsRecordType(); |
| 306 | assert (RTy && "Unexpected type. RecordType expected here."); |
Devang Patel | aebd83f | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 307 | |
Chris Lattner | 7b2543e | 2008-02-05 06:55:31 +0000 | [diff] [blame] | 308 | return CGT.getCGRecordLayout(RTy->getDecl()); |
Devang Patel | aebd83f | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 309 | } |
Chris Lattner | 9d4e620 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 310 | |
Daniel Dunbar | 9503b78 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 311 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | 9d4e620 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 312 | /// specified stmt yet. |
Daniel Dunbar | 49bddf7 | 2008-09-04 03:43:08 +0000 | [diff] [blame] | 313 | void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type, |
| 314 | bool OmitOnError) { |
| 315 | CGM.ErrorUnsupported(S, Type, OmitOnError); |
Chris Lattner | 9d4e620 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Daniel Dunbar | 879788d | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 318 | unsigned CodeGenFunction::GetIDForAddrOfLabel(const LabelStmt *L) { |
| 319 | // Use LabelIDs.size() as the new ID if one hasn't been assigned. |
| 320 | return LabelIDs.insert(std::make_pair(L, LabelIDs.size())).first->second; |
| 321 | } |
| 322 | |
Anders Carlsson | 82b0d0c | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 323 | void CodeGenFunction::EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty) |
| 324 | { |
| 325 | const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 326 | if (DestPtr->getType() != BP) |
| 327 | DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp"); |
| 328 | |
| 329 | // Get size and alignment info for this aggregate. |
| 330 | std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty); |
| 331 | |
| 332 | // FIXME: Handle variable sized types. |
| 333 | const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth); |
| 334 | |
| 335 | Builder.CreateCall4(CGM.getMemSetFn(), DestPtr, |
| 336 | llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty), |
| 337 | // TypeInfo.first describes size in bits. |
| 338 | llvm::ConstantInt::get(IntPtr, TypeInfo.first/8), |
| 339 | llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 340 | TypeInfo.second/8)); |
| 341 | } |
| 342 | |
Daniel Dunbar | 879788d | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 343 | void CodeGenFunction::EmitIndirectSwitches() { |
| 344 | llvm::BasicBlock *Default; |
| 345 | |
Daniel Dunbar | 8ccfa80 | 2008-08-04 17:24:44 +0000 | [diff] [blame] | 346 | if (IndirectSwitches.empty()) |
| 347 | return; |
| 348 | |
Daniel Dunbar | 879788d | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 349 | if (!LabelIDs.empty()) { |
| 350 | Default = getBasicBlockForLabel(LabelIDs.begin()->first); |
| 351 | } else { |
| 352 | // No possible targets for indirect goto, just emit an infinite |
| 353 | // loop. |
Daniel Dunbar | 72f9655 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 354 | Default = createBasicBlock("indirectgoto.loop", CurFn); |
Daniel Dunbar | 879788d | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 355 | llvm::BranchInst::Create(Default, Default); |
| 356 | } |
| 357 | |
| 358 | for (std::vector<llvm::SwitchInst*>::iterator i = IndirectSwitches.begin(), |
| 359 | e = IndirectSwitches.end(); i != e; ++i) { |
| 360 | llvm::SwitchInst *I = *i; |
| 361 | |
| 362 | I->setSuccessor(0, Default); |
| 363 | for (std::map<const LabelStmt*,unsigned>::iterator LI = LabelIDs.begin(), |
| 364 | LE = LabelIDs.end(); LI != LE; ++LI) { |
| 365 | I->addCase(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 366 | LI->second), |
| 367 | getBasicBlockForLabel(LI->first)); |
| 368 | } |
| 369 | } |
| 370 | } |
Anders Carlsson | 285611e | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 371 | |
| 372 | llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) |
| 373 | { |
| 374 | // FIXME: This entire method is hardcoded for 32-bit X86. |
| 375 | |
| 376 | const char *TargetPrefix = getContext().Target.getTargetPrefix(); |
| 377 | |
| 378 | if (strcmp(TargetPrefix, "x86") != 0 || |
| 379 | getContext().Target.getPointerWidth(0) != 32) |
| 380 | return 0; |
| 381 | |
| 382 | const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 383 | const llvm::Type *BPP = llvm::PointerType::getUnqual(BP); |
| 384 | |
| 385 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 386 | "ap"); |
| 387 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 388 | llvm::Value *AddrTyped = |
| 389 | Builder.CreateBitCast(Addr, |
| 390 | llvm::PointerType::getUnqual(ConvertType(Ty))); |
| 391 | |
| 392 | uint64_t SizeInBytes = getContext().getTypeSize(Ty) / 8; |
| 393 | const unsigned ArgumentSizeInBytes = 4; |
| 394 | if (SizeInBytes < ArgumentSizeInBytes) |
| 395 | SizeInBytes = ArgumentSizeInBytes; |
| 396 | |
| 397 | llvm::Value *NextAddr = |
| 398 | Builder.CreateGEP(Addr, |
| 399 | llvm::ConstantInt::get(llvm::Type::Int32Ty, SizeInBytes), |
| 400 | "ap.next"); |
| 401 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 402 | |
| 403 | return AddrTyped; |
| 404 | } |
| 405 | |