blob: fe28b3aeccdd25c9775434657f6f76c083433334 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
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 Friedman3f2af102008-05-22 01:40:10 +000016#include "CGDebugInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/Basic/TargetInfo.h"
Chris Lattner31a09842008-11-12 08:04:58 +000018#include "clang/AST/APValue.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000019#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000020#include "clang/AST/Decl.h"
Devang Pateld9363c32007-09-28 21:49:18 +000021#include "llvm/Support/CFG.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23using namespace CodeGen;
24
25CodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
Devang Patelc049e4f2007-10-08 20:57:48 +000026 : CGM(cgm), Target(CGM.getContext().Target), SwitchInsn(NULL),
Chris Lattner41110242008-06-17 18:05:57 +000027 CaseRangeBlock(NULL) {
28 LLVMIntTy = ConvertType(getContext().IntTy);
29 LLVMPointerWidth = Target.getPointerWidth(0);
30}
Reid Spencer5f016e22007-07-11 17:01:13 +000031
32ASTContext &CodeGenFunction::getContext() const {
33 return CGM.getContext();
34}
35
36
37llvm::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 Dunbar55e87422008-11-11 02:29:29 +000042 return BB = createBasicBlock(S->getName());
Reid Spencer5f016e22007-07-11 17:01:13 +000043}
44
Lauro Ramos Venancio81373352008-02-26 21:41:45 +000045llvm::Constant *
Steve Naroff248a7532008-04-15 22:42:06 +000046CodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) {
Lauro Ramos Venancio81373352008-02-26 21:41:45 +000047 return cast<llvm::Constant>(LocalDeclMap[BVD]);
48}
Reid Spencer5f016e22007-07-11 17:01:13 +000049
Anders Carlssondde0a942008-09-11 09:15:33 +000050llvm::Value *CodeGenFunction::GetAddrOfLocalVar(const VarDecl *VD)
51{
52 return LocalDeclMap[VD];
53}
54
Reid Spencer5f016e22007-07-11 17:01:13 +000055const llvm::Type *CodeGenFunction::ConvertType(QualType T) {
56 return CGM.getTypes().ConvertType(T);
57}
58
Chris Lattner41110242008-06-17 18:05:57 +000059bool CodeGenFunction::isObjCPointerType(QualType T) {
60 // All Objective-C types are pointers.
61 return T->isObjCInterfaceType() ||
62 T->isObjCQualifiedInterfaceType() || T->isObjCQualifiedIdType();
63}
64
Reid Spencer5f016e22007-07-11 17:01:13 +000065bool CodeGenFunction::hasAggregateLLVMType(QualType T) {
Daniel Dunbara782ca72009-01-09 02:44:18 +000066 // FIXME: Use positive checks instead of negative ones to be more
67 // robust in the face of extension.
Chris Lattner41110242008-06-17 18:05:57 +000068 return !isObjCPointerType(T) &&!T->isRealType() && !T->isPointerLikeType() &&
Daniel Dunbara782ca72009-01-09 02:44:18 +000069 !T->isVoidType() && !T->isVectorType() && !T->isFunctionType() &&
70 !T->isBlockPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +000071}
72
Daniel Dunbar1c1d6072009-01-26 23:27:52 +000073void CodeGenFunction::EmitReturnBlock() {
74 // For cleanliness, we try to avoid emitting the return block for
75 // simple cases.
76 llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
77
78 if (CurBB) {
79 assert(!CurBB->getTerminator() && "Unexpected terminated block.");
80
81 // We have a valid insert point, reuse it if there are no explicit
82 // jumps to the return block.
83 if (ReturnBlock->use_empty())
84 delete ReturnBlock;
85 else
86 EmitBlock(ReturnBlock);
87 return;
88 }
89
90 // Otherwise, if the return block is the target of a single direct
91 // branch then we can just put the code in that block instead. This
92 // cleans up functions which started with a unified return block.
93 if (ReturnBlock->hasOneUse()) {
94 llvm::BranchInst *BI =
95 dyn_cast<llvm::BranchInst>(*ReturnBlock->use_begin());
96 if (BI && BI->isUnconditional() && BI->getSuccessor(0) == ReturnBlock) {
97 // Reset insertion point and delete the branch.
98 Builder.SetInsertPoint(BI->getParent());
99 BI->eraseFromParent();
100 delete ReturnBlock;
101 return;
102 }
103 }
104
105 // FIXME: We are at an unreachable point, there is no reason to emit
106 // the block unless it has uses. However, we still need a place to
107 // put the debug region.end for now.
108
109 EmitBlock(ReturnBlock);
110}
111
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000112void CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000113 // Finish emission of indirect switches.
114 EmitIndirectSwitches();
115
Chris Lattnerda138702007-07-16 21:28:45 +0000116 assert(BreakContinueStack.empty() &&
117 "mismatched push/pop in break/continue stack!");
Daniel Dunbar5ca20842008-09-09 21:00:17 +0000118
Daniel Dunbar1c1d6072009-01-26 23:27:52 +0000119 // Emit function epilog (to return).
120 EmitReturnBlock();
Daniel Dunbarf5bd45c2008-11-11 20:59:54 +0000121
122 // Emit debug descriptor for function end.
123 if (CGDebugInfo *DI = CGM.getDebugInfo()) {
124 DI->setLocation(EndLoc);
125 DI->EmitRegionEnd(CurFn, Builder);
126 }
127
Daniel Dunbar88b53962009-02-02 22:03:45 +0000128 EmitFunctionEpilog(*CurFnInfo, ReturnValue);
Daniel Dunbar5ca20842008-09-09 21:00:17 +0000129
Chris Lattner5a2fa142007-12-02 06:32:24 +0000130 // Remove the AllocaInsertPt instruction, which is just a convenience for us.
131 AllocaInsertPt->eraseFromParent();
132 AllocaInsertPt = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000133}
134
Daniel Dunbar7c086512008-09-09 23:14:03 +0000135void CodeGenFunction::StartFunction(const Decl *D, QualType RetTy,
136 llvm::Function *Fn,
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000137 const FunctionArgList &Args,
138 SourceLocation StartLoc) {
Daniel Dunbar7c086512008-09-09 23:14:03 +0000139 CurFuncDecl = D;
140 FnRetTy = RetTy;
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000141 CurFn = Fn;
Chris Lattner41110242008-06-17 18:05:57 +0000142 assert(CurFn->isDeclaration() && "Function already has body?");
143
Daniel Dunbar55e87422008-11-11 02:29:29 +0000144 llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
Daniel Dunbar5ca20842008-09-09 21:00:17 +0000145
Chris Lattner41110242008-06-17 18:05:57 +0000146 // Create a marker to make it easy to insert allocas into the entryblock
147 // later. Don't create this with the builder, because we don't want it
148 // folded.
149 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
150 AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt",
151 EntryBB);
Daniel Dunbar5ca20842008-09-09 21:00:17 +0000152
Daniel Dunbar55e87422008-11-11 02:29:29 +0000153 ReturnBlock = createBasicBlock("return");
Daniel Dunbar5ca20842008-09-09 21:00:17 +0000154 ReturnValue = 0;
Daniel Dunbar7c086512008-09-09 23:14:03 +0000155 if (!RetTy->isVoidType())
156 ReturnValue = CreateTempAlloca(ConvertType(RetTy), "retval");
Daniel Dunbar5ca20842008-09-09 21:00:17 +0000157
Chris Lattner41110242008-06-17 18:05:57 +0000158 Builder.SetInsertPoint(EntryBB);
159
Sanjiv Guptaaf994172008-07-04 11:04:26 +0000160 // Emit subprogram debug descriptor.
Daniel Dunbar7c086512008-09-09 23:14:03 +0000161 // FIXME: The cast here is a huge hack.
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000162 if (CGDebugInfo *DI = CGM.getDebugInfo()) {
163 DI->setLocation(StartLoc);
164 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Chris Lattner8ec03f52008-11-24 03:54:41 +0000165 DI->EmitFunctionStart(FD->getIdentifier()->getName(),
166 RetTy, CurFn, Builder);
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000167 } else {
168 // Just use LLVM function name.
169 DI->EmitFunctionStart(Fn->getName().c_str(),
170 RetTy, CurFn, Builder);
Sanjiv Guptaaf994172008-07-04 11:04:26 +0000171 }
Sanjiv Guptaaf994172008-07-04 11:04:26 +0000172 }
173
Daniel Dunbar88b53962009-02-02 22:03:45 +0000174 // FIXME: Leaked.
175 CurFnInfo = new CGFunctionInfo(FnRetTy, Args);
176 EmitFunctionProlog(*CurFnInfo, CurFn, Args);
Anders Carlsson751358f2008-12-20 21:28:43 +0000177
178 // If any of the arguments have a variably modified type, make sure to
179 // emit the type size.
180 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
181 i != e; ++i) {
182 QualType Ty = i->second;
183
184 if (Ty->isVariablyModifiedType())
185 EmitVLASize(Ty);
186 }
Daniel Dunbar7c086512008-09-09 23:14:03 +0000187}
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000188
Daniel Dunbar7c086512008-09-09 23:14:03 +0000189void CodeGenFunction::GenerateCode(const FunctionDecl *FD,
190 llvm::Function *Fn) {
191 FunctionArgList Args;
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000192 if (FD->getNumParams()) {
193 const FunctionTypeProto* FProto = FD->getType()->getAsFunctionTypeProto();
194 assert(FProto && "Function def must have prototype!");
Daniel Dunbar7c086512008-09-09 23:14:03 +0000195
196 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i)
197 Args.push_back(std::make_pair(FD->getParamDecl(i),
198 FProto->getArgType(i)));
Chris Lattner41110242008-06-17 18:05:57 +0000199 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000200
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000201 StartFunction(FD, FD->getResultType(), Fn, Args,
202 cast<CompoundStmt>(FD->getBody())->getLBracLoc());
Daniel Dunbar7c086512008-09-09 23:14:03 +0000203
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000204 EmitStmt(FD->getBody());
205
206 const CompoundStmt *S = dyn_cast<CompoundStmt>(FD->getBody());
207 if (S) {
208 FinishFunction(S->getRBracLoc());
209 } else {
210 FinishFunction();
211 }
Chris Lattner41110242008-06-17 18:05:57 +0000212}
213
Chris Lattner0946ccd2008-11-11 07:41:27 +0000214/// ContainsLabel - Return true if the statement contains a label in it. If
215/// this statement is not executed normally, it not containing a label means
216/// that we can just remove the code.
217bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) {
218 // Null statement, not a label!
219 if (S == 0) return false;
220
221 // If this is a label, we have to emit the code, consider something like:
222 // if (0) { ... foo: bar(); } goto foo;
223 if (isa<LabelStmt>(S))
224 return true;
225
226 // If this is a case/default statement, and we haven't seen a switch, we have
227 // to emit the code.
228 if (isa<SwitchCase>(S) && !IgnoreCaseStmts)
229 return true;
230
231 // If this is a switch statement, we want to ignore cases below it.
232 if (isa<SwitchStmt>(S))
233 IgnoreCaseStmts = true;
234
235 // Scan subexpressions for verboten labels.
236 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
237 I != E; ++I)
238 if (ContainsLabel(*I, IgnoreCaseStmts))
239 return true;
240
241 return false;
242}
243
Chris Lattner31a09842008-11-12 08:04:58 +0000244
245/// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to
246/// a constant, or if it does but contains a label, return 0. If it constant
247/// folds to 'true' and does not contain a label, return 1, if it constant folds
248/// to 'false' and does not contain a label, return -1.
249int CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) {
Daniel Dunbar36bc14c2008-11-12 22:37:10 +0000250 // FIXME: Rename and handle conversion of other evaluatable things
251 // to bool.
Anders Carlsson64712f12008-12-01 02:46:24 +0000252 Expr::EvalResult Result;
253 if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() ||
254 Result.HasSideEffects)
Anders Carlssonef5a66d2008-11-22 22:32:07 +0000255 return 0; // Not foldable, not integer or not fully evaluatable.
Chris Lattner31a09842008-11-12 08:04:58 +0000256
257 if (CodeGenFunction::ContainsLabel(Cond))
258 return 0; // Contains a label.
259
Anders Carlsson64712f12008-12-01 02:46:24 +0000260 return Result.Val.getInt().getBoolValue() ? 1 : -1;
Chris Lattner31a09842008-11-12 08:04:58 +0000261}
262
263
264/// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if
265/// statement) to the specified blocks. Based on the condition, this might try
266/// to simplify the codegen of the conditional based on the branch.
267///
268void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
269 llvm::BasicBlock *TrueBlock,
270 llvm::BasicBlock *FalseBlock) {
271 if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond))
272 return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock);
273
274 if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) {
275 // Handle X && Y in a condition.
276 if (CondBOp->getOpcode() == BinaryOperator::LAnd) {
277 // If we have "1 && X", simplify the code. "0 && X" would have constant
278 // folded if the case was simple enough.
279 if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) {
280 // br(1 && X) -> br(X).
281 return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
282 }
283
284 // If we have "X && 1", simplify the code to use an uncond branch.
285 // "X && 0" would have been constant folded to 0.
286 if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) {
287 // br(X && 1) -> br(X).
288 return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
289 }
290
291 // Emit the LHS as a conditional. If the LHS conditional is false, we
292 // want to jump to the FalseBlock.
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000293 llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true");
Chris Lattner31a09842008-11-12 08:04:58 +0000294 EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock);
295 EmitBlock(LHSTrue);
296
297 EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
298 return;
299 } else if (CondBOp->getOpcode() == BinaryOperator::LOr) {
300 // If we have "0 || X", simplify the code. "1 || X" would have constant
301 // folded if the case was simple enough.
302 if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) {
303 // br(0 || X) -> br(X).
304 return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
305 }
306
307 // If we have "X || 0", simplify the code to use an uncond branch.
308 // "X || 1" would have been constant folded to 1.
309 if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) {
310 // br(X || 0) -> br(X).
311 return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
312 }
313
314 // Emit the LHS as a conditional. If the LHS conditional is true, we
315 // want to jump to the TrueBlock.
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000316 llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false");
Chris Lattner31a09842008-11-12 08:04:58 +0000317 EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse);
318 EmitBlock(LHSFalse);
319
320 EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
321 return;
322 }
Chris Lattner552f4c42008-11-12 08:13:36 +0000323 }
324
325 if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) {
326 // br(!x, t, f) -> br(x, f, t)
327 if (CondUOp->getOpcode() == UnaryOperator::LNot)
328 return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock);
Chris Lattner31a09842008-11-12 08:04:58 +0000329 }
330
Daniel Dunbar09b14892008-11-12 10:30:32 +0000331 if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) {
332 // Handle ?: operator.
333
334 // Just ignore GNU ?: extension.
335 if (CondOp->getLHS()) {
336 // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f))
337 llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
338 llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
339 EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock);
340 EmitBlock(LHSBlock);
341 EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock);
342 EmitBlock(RHSBlock);
343 EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock);
344 return;
345 }
346 }
347
Chris Lattner31a09842008-11-12 08:04:58 +0000348 // Emit the code with the fully general case.
349 llvm::Value *CondV = EvaluateExprAsBool(Cond);
350 Builder.CreateCondBr(CondV, TrueBlock, FalseBlock);
351}
352
Devang Patel88a981b2007-11-01 19:11:01 +0000353/// getCGRecordLayout - Return record layout info.
354const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT,
Chris Lattneraf319132008-02-05 06:55:31 +0000355 QualType Ty) {
356 const RecordType *RTy = Ty->getAsRecordType();
357 assert (RTy && "Unexpected type. RecordType expected here.");
Devang Patelb84a06e2007-10-23 02:10:49 +0000358
Chris Lattneraf319132008-02-05 06:55:31 +0000359 return CGT.getCGRecordLayout(RTy->getDecl());
Devang Patelb84a06e2007-10-23 02:10:49 +0000360}
Chris Lattnerdc5e8262007-12-02 01:43:38 +0000361
Daniel Dunbar488e9932008-08-16 00:56:44 +0000362/// ErrorUnsupported - Print out an error that codegen doesn't support the
Chris Lattnerdc5e8262007-12-02 01:43:38 +0000363/// specified stmt yet.
Daniel Dunbar90df4b62008-09-04 03:43:08 +0000364void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type,
365 bool OmitOnError) {
366 CGM.ErrorUnsupported(S, Type, OmitOnError);
Chris Lattnerdc5e8262007-12-02 01:43:38 +0000367}
368
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000369unsigned CodeGenFunction::GetIDForAddrOfLabel(const LabelStmt *L) {
370 // Use LabelIDs.size() as the new ID if one hasn't been assigned.
371 return LabelIDs.insert(std::make_pair(L, LabelIDs.size())).first->second;
372}
373
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000374void CodeGenFunction::EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty)
375{
376 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
377 if (DestPtr->getType() != BP)
378 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
379
380 // Get size and alignment info for this aggregate.
381 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
382
383 // FIXME: Handle variable sized types.
384 const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth);
385
386 Builder.CreateCall4(CGM.getMemSetFn(), DestPtr,
387 llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty),
388 // TypeInfo.first describes size in bits.
389 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
390 llvm::ConstantInt::get(llvm::Type::Int32Ty,
391 TypeInfo.second/8));
392}
393
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000394void CodeGenFunction::EmitIndirectSwitches() {
395 llvm::BasicBlock *Default;
396
Daniel Dunbar76526a52008-08-04 17:24:44 +0000397 if (IndirectSwitches.empty())
398 return;
399
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000400 if (!LabelIDs.empty()) {
401 Default = getBasicBlockForLabel(LabelIDs.begin()->first);
402 } else {
403 // No possible targets for indirect goto, just emit an infinite
404 // loop.
Daniel Dunbar55e87422008-11-11 02:29:29 +0000405 Default = createBasicBlock("indirectgoto.loop", CurFn);
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000406 llvm::BranchInst::Create(Default, Default);
407 }
408
409 for (std::vector<llvm::SwitchInst*>::iterator i = IndirectSwitches.begin(),
410 e = IndirectSwitches.end(); i != e; ++i) {
411 llvm::SwitchInst *I = *i;
412
413 I->setSuccessor(0, Default);
414 for (std::map<const LabelStmt*,unsigned>::iterator LI = LabelIDs.begin(),
415 LE = LabelIDs.end(); LI != LE; ++LI) {
416 I->addCase(llvm::ConstantInt::get(llvm::Type::Int32Ty,
417 LI->second),
418 getBasicBlockForLabel(LI->first));
419 }
420 }
421}
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000422
423llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty)
424{
425 // FIXME: This entire method is hardcoded for 32-bit X86.
426
427 const char *TargetPrefix = getContext().Target.getTargetPrefix();
428
429 if (strcmp(TargetPrefix, "x86") != 0 ||
430 getContext().Target.getPointerWidth(0) != 32)
431 return 0;
432
433 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
434 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
435
436 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
437 "ap");
438 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
439 llvm::Value *AddrTyped =
440 Builder.CreateBitCast(Addr,
441 llvm::PointerType::getUnqual(ConvertType(Ty)));
442
443 uint64_t SizeInBytes = getContext().getTypeSize(Ty) / 8;
444 const unsigned ArgumentSizeInBytes = 4;
445 if (SizeInBytes < ArgumentSizeInBytes)
446 SizeInBytes = ArgumentSizeInBytes;
447
448 llvm::Value *NextAddr =
449 Builder.CreateGEP(Addr,
450 llvm::ConstantInt::get(llvm::Type::Int32Ty, SizeInBytes),
451 "ap.next");
452 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
453
454 return AddrTyped;
455}
456
Anders Carlssonf666b772008-12-20 20:27:15 +0000457
Anders Carlssondcc90d82008-12-12 07:19:02 +0000458llvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT)
459{
460 llvm::Value *&SizeEntry = VLASizeMap[VAT];
Anders Carlssondcc90d82008-12-12 07:19:02 +0000461
Anders Carlssonf666b772008-12-20 20:27:15 +0000462 assert(SizeEntry && "Did not emit size for type");
463 return SizeEntry;
464}
Anders Carlssondcc90d82008-12-12 07:19:02 +0000465
Anders Carlsson60d35412008-12-20 20:46:34 +0000466llvm::Value *CodeGenFunction::EmitVLASize(QualType Ty)
Anders Carlssonf666b772008-12-20 20:27:15 +0000467{
Anders Carlsson60d35412008-12-20 20:46:34 +0000468 assert(Ty->isVariablyModifiedType() &&
469 "Must pass variably modified type to EmitVLASizes!");
Anders Carlssonf666b772008-12-20 20:27:15 +0000470
Anders Carlsson60d35412008-12-20 20:46:34 +0000471 if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) {
472 llvm::Value *&SizeEntry = VLASizeMap[VAT];
473
Anders Carlssonfcdbb932008-12-20 21:51:53 +0000474 if (!SizeEntry) {
475 // Get the element size;
476 llvm::Value *ElemSize;
Anders Carlsson60d35412008-12-20 20:46:34 +0000477
Anders Carlssonfcdbb932008-12-20 21:51:53 +0000478 QualType ElemTy = VAT->getElementType();
Anders Carlsson60d35412008-12-20 20:46:34 +0000479
Anders Carlssonfcdbb932008-12-20 21:51:53 +0000480 if (ElemTy->isVariableArrayType())
481 ElemSize = EmitVLASize(ElemTy);
482 else {
483 // FIXME: We use Int32Ty here because the alloca instruction takes a
484 // 32-bit integer. What should we do about overflow?
485 ElemSize = llvm::ConstantInt::get(llvm::Type::Int32Ty,
486 getContext().getTypeSize(ElemTy) / 8);
487 }
Anders Carlsson60d35412008-12-20 20:46:34 +0000488
Anders Carlssonfcdbb932008-12-20 21:51:53 +0000489 llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr());
490
491 SizeEntry = Builder.CreateMul(ElemSize, NumElements);
Anders Carlsson60d35412008-12-20 20:46:34 +0000492 }
493
Anders Carlsson60d35412008-12-20 20:46:34 +0000494 return SizeEntry;
495 } else if (const PointerType *PT = Ty->getAsPointerType())
496 EmitVLASize(PT->getPointeeType());
Anders Carlssonf666b772008-12-20 20:27:15 +0000497 else {
Anders Carlsson60d35412008-12-20 20:46:34 +0000498 assert(0 && "unknown VM type!");
Anders Carlssondcc90d82008-12-12 07:19:02 +0000499 }
Anders Carlsson60d35412008-12-20 20:46:34 +0000500
501 return 0;
Anders Carlssondcc90d82008-12-12 07:19:02 +0000502}
Eli Friedman4fd0aa52009-01-20 17:46:04 +0000503
504llvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) {
505 if (CGM.getContext().getBuiltinVaListType()->isArrayType()) {
506 return EmitScalarExpr(E);
507 }
508 return EmitLValue(E).getAddress();
509}