blob: b509843daa654ade9fe9064f36d49b649d625d42 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +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 Friedman9bc7c8d2008-05-22 01:40:10 +000016#include "CGDebugInfo.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "clang/Basic/TargetInfo.h"
Chris Lattner3d6606b2008-11-12 08:04:58 +000018#include "clang/AST/APValue.h"
Daniel Dunbareee5cd12008-08-11 05:00:27 +000019#include "clang/AST/ASTContext.h"
Daniel Dunbar64789f82008-08-11 05:35:13 +000020#include "clang/AST/Decl.h"
Devang Patel97299362007-09-28 21:49:18 +000021#include "llvm/Support/CFG.h"
Chris Lattner4b009652007-07-25 00:24:17 +000022using namespace clang;
23using namespace CodeGen;
24
25CodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
Devang Patel347ca322007-10-08 20:57:48 +000026 : CGM(cgm), Target(CGM.getContext().Target), SwitchInsn(NULL),
Chris Lattner8c7c6a12008-06-17 18:05:57 +000027 CaseRangeBlock(NULL) {
28 LLVMIntTy = ConvertType(getContext().IntTy);
29 LLVMPointerWidth = Target.getPointerWidth(0);
30}
Chris Lattner4b009652007-07-25 00:24:17 +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 Dunbar72f96552008-11-11 02:29:29 +000042 return BB = createBasicBlock(S->getName());
Chris Lattner4b009652007-07-25 00:24:17 +000043}
44
Lauro Ramos Venancio934fb022008-02-26 21:41:45 +000045llvm::Constant *
Steve Naroff72a6ebc2008-04-15 22:42:06 +000046CodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) {
Lauro Ramos Venancio934fb022008-02-26 21:41:45 +000047 return cast<llvm::Constant>(LocalDeclMap[BVD]);
48}
Chris Lattner4b009652007-07-25 00:24:17 +000049
Anders Carlsson75d86732008-09-11 09:15:33 +000050llvm::Value *CodeGenFunction::GetAddrOfLocalVar(const VarDecl *VD)
51{
52 return LocalDeclMap[VD];
53}
54
Chris Lattner4b009652007-07-25 00:24:17 +000055const llvm::Type *CodeGenFunction::ConvertType(QualType T) {
56 return CGM.getTypes().ConvertType(T);
57}
58
Chris Lattner8c7c6a12008-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
Chris Lattner4b009652007-07-25 00:24:17 +000065bool CodeGenFunction::hasAggregateLLVMType(QualType T) {
Daniel Dunbar96891242009-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 Lattner8c7c6a12008-06-17 18:05:57 +000068 return !isObjCPointerType(T) &&!T->isRealType() && !T->isPointerLikeType() &&
Daniel Dunbar96891242009-01-09 02:44:18 +000069 !T->isVoidType() && !T->isVectorType() && !T->isFunctionType() &&
70 !T->isBlockPointerType();
Chris Lattner4b009652007-07-25 00:24:17 +000071}
72
Daniel Dunbar6b57d432008-08-26 08:29:31 +000073void CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
Daniel Dunbar879788d2008-08-04 16:51:22 +000074 // Finish emission of indirect switches.
75 EmitIndirectSwitches();
76
Chris Lattner4b009652007-07-25 00:24:17 +000077 assert(BreakContinueStack.empty() &&
78 "mismatched push/pop in break/continue stack!");
Daniel Dunbar9fb751f2008-09-09 21:00:17 +000079
Daniel Dunbarf5874e52009-01-26 21:25:20 +000080 // Emit function epilog (to return). For cleanliness, skip emission
81 // if we know it is safe (when it is unused and the current block is
82 // unterminated).
83 if (!ReturnBlock->use_empty() ||
84 !Builder.GetInsertBlock() ||
85 Builder.GetInsertBlock()->getTerminator())
86 EmitBlock(ReturnBlock);
Daniel Dunbar03f7ae12008-11-11 20:59:54 +000087
88 // Emit debug descriptor for function end.
89 if (CGDebugInfo *DI = CGM.getDebugInfo()) {
90 DI->setLocation(EndLoc);
91 DI->EmitRegionEnd(CurFn, Builder);
92 }
93
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +000094 EmitFunctionEpilog(FnRetTy, ReturnValue);
Daniel Dunbar9fb751f2008-09-09 21:00:17 +000095
Chris Lattnerca929812007-12-02 06:32:24 +000096 // Remove the AllocaInsertPt instruction, which is just a convenience for us.
97 AllocaInsertPt->eraseFromParent();
98 AllocaInsertPt = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000099}
100
Daniel Dunbar96816832008-09-09 23:14:03 +0000101void CodeGenFunction::StartFunction(const Decl *D, QualType RetTy,
102 llvm::Function *Fn,
Daniel Dunbar54968bf2008-10-18 18:22:23 +0000103 const FunctionArgList &Args,
104 SourceLocation StartLoc) {
Daniel Dunbar96816832008-09-09 23:14:03 +0000105 CurFuncDecl = D;
106 FnRetTy = RetTy;
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000107 CurFn = Fn;
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000108 assert(CurFn->isDeclaration() && "Function already has body?");
109
Daniel Dunbar72f96552008-11-11 02:29:29 +0000110 llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
Daniel Dunbar9fb751f2008-09-09 21:00:17 +0000111
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000112 // Create a marker to make it easy to insert allocas into the entryblock
113 // later. Don't create this with the builder, because we don't want it
114 // folded.
115 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
116 AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt",
117 EntryBB);
Daniel Dunbar9fb751f2008-09-09 21:00:17 +0000118
Daniel Dunbar72f96552008-11-11 02:29:29 +0000119 ReturnBlock = createBasicBlock("return");
Daniel Dunbar9fb751f2008-09-09 21:00:17 +0000120 ReturnValue = 0;
Daniel Dunbar96816832008-09-09 23:14:03 +0000121 if (!RetTy->isVoidType())
122 ReturnValue = CreateTempAlloca(ConvertType(RetTy), "retval");
Daniel Dunbar9fb751f2008-09-09 21:00:17 +0000123
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000124 Builder.SetInsertPoint(EntryBB);
125
Sanjiv Gupta1d340eb2008-07-04 11:04:26 +0000126 // Emit subprogram debug descriptor.
Daniel Dunbar96816832008-09-09 23:14:03 +0000127 // FIXME: The cast here is a huge hack.
Daniel Dunbar54968bf2008-10-18 18:22:23 +0000128 if (CGDebugInfo *DI = CGM.getDebugInfo()) {
129 DI->setLocation(StartLoc);
130 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000131 DI->EmitFunctionStart(FD->getIdentifier()->getName(),
132 RetTy, CurFn, Builder);
Daniel Dunbar54968bf2008-10-18 18:22:23 +0000133 } else {
134 // Just use LLVM function name.
135 DI->EmitFunctionStart(Fn->getName().c_str(),
136 RetTy, CurFn, Builder);
Sanjiv Gupta1d340eb2008-07-04 11:04:26 +0000137 }
Sanjiv Gupta1d340eb2008-07-04 11:04:26 +0000138 }
139
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000140 EmitFunctionProlog(CurFn, FnRetTy, Args);
Anders Carlsson21cecd42008-12-20 21:28:43 +0000141
142 // If any of the arguments have a variably modified type, make sure to
143 // emit the type size.
144 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
145 i != e; ++i) {
146 QualType Ty = i->second;
147
148 if (Ty->isVariablyModifiedType())
149 EmitVLASize(Ty);
150 }
Daniel Dunbar96816832008-09-09 23:14:03 +0000151}
Eli Friedman769e7302008-08-25 21:31:01 +0000152
Daniel Dunbar96816832008-09-09 23:14:03 +0000153void CodeGenFunction::GenerateCode(const FunctionDecl *FD,
154 llvm::Function *Fn) {
155 FunctionArgList Args;
Eli Friedman769e7302008-08-25 21:31:01 +0000156 if (FD->getNumParams()) {
157 const FunctionTypeProto* FProto = FD->getType()->getAsFunctionTypeProto();
158 assert(FProto && "Function def must have prototype!");
Daniel Dunbar96816832008-09-09 23:14:03 +0000159
160 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i)
161 Args.push_back(std::make_pair(FD->getParamDecl(i),
162 FProto->getArgType(i)));
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000163 }
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000164
Daniel Dunbar54968bf2008-10-18 18:22:23 +0000165 StartFunction(FD, FD->getResultType(), Fn, Args,
166 cast<CompoundStmt>(FD->getBody())->getLBracLoc());
Daniel Dunbar96816832008-09-09 23:14:03 +0000167
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000168 EmitStmt(FD->getBody());
169
170 const CompoundStmt *S = dyn_cast<CompoundStmt>(FD->getBody());
171 if (S) {
172 FinishFunction(S->getRBracLoc());
173 } else {
174 FinishFunction();
175 }
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000176}
177
Chris Lattner3f73d0d2008-11-11 07:41:27 +0000178/// ContainsLabel - Return true if the statement contains a label in it. If
179/// this statement is not executed normally, it not containing a label means
180/// that we can just remove the code.
181bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) {
182 // Null statement, not a label!
183 if (S == 0) return false;
184
185 // If this is a label, we have to emit the code, consider something like:
186 // if (0) { ... foo: bar(); } goto foo;
187 if (isa<LabelStmt>(S))
188 return true;
189
190 // If this is a case/default statement, and we haven't seen a switch, we have
191 // to emit the code.
192 if (isa<SwitchCase>(S) && !IgnoreCaseStmts)
193 return true;
194
195 // If this is a switch statement, we want to ignore cases below it.
196 if (isa<SwitchStmt>(S))
197 IgnoreCaseStmts = true;
198
199 // Scan subexpressions for verboten labels.
200 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
201 I != E; ++I)
202 if (ContainsLabel(*I, IgnoreCaseStmts))
203 return true;
204
205 return false;
206}
207
Chris Lattner3d6606b2008-11-12 08:04:58 +0000208
209/// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to
210/// a constant, or if it does but contains a label, return 0. If it constant
211/// folds to 'true' and does not contain a label, return 1, if it constant folds
212/// to 'false' and does not contain a label, return -1.
213int CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) {
Daniel Dunbar14937022008-11-12 22:37:10 +0000214 // FIXME: Rename and handle conversion of other evaluatable things
215 // to bool.
Anders Carlsson90d85f32008-12-01 02:46:24 +0000216 Expr::EvalResult Result;
217 if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() ||
218 Result.HasSideEffects)
Anders Carlssonea3c6ab2008-11-22 22:32:07 +0000219 return 0; // Not foldable, not integer or not fully evaluatable.
Chris Lattner3d6606b2008-11-12 08:04:58 +0000220
221 if (CodeGenFunction::ContainsLabel(Cond))
222 return 0; // Contains a label.
223
Anders Carlsson90d85f32008-12-01 02:46:24 +0000224 return Result.Val.getInt().getBoolValue() ? 1 : -1;
Chris Lattner3d6606b2008-11-12 08:04:58 +0000225}
226
227
228/// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if
229/// statement) to the specified blocks. Based on the condition, this might try
230/// to simplify the codegen of the conditional based on the branch.
231///
232void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
233 llvm::BasicBlock *TrueBlock,
234 llvm::BasicBlock *FalseBlock) {
235 if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond))
236 return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock);
237
238 if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) {
239 // Handle X && Y in a condition.
240 if (CondBOp->getOpcode() == BinaryOperator::LAnd) {
241 // If we have "1 && X", simplify the code. "0 && X" would have constant
242 // folded if the case was simple enough.
243 if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) {
244 // br(1 && X) -> br(X).
245 return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
246 }
247
248 // If we have "X && 1", simplify the code to use an uncond branch.
249 // "X && 0" would have been constant folded to 0.
250 if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) {
251 // br(X && 1) -> br(X).
252 return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
253 }
254
255 // Emit the LHS as a conditional. If the LHS conditional is false, we
256 // want to jump to the FalseBlock.
Daniel Dunbar6e3a10c2008-11-13 01:38:36 +0000257 llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true");
Chris Lattner3d6606b2008-11-12 08:04:58 +0000258 EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock);
259 EmitBlock(LHSTrue);
260
261 EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
262 return;
263 } else if (CondBOp->getOpcode() == BinaryOperator::LOr) {
264 // If we have "0 || X", simplify the code. "1 || X" would have constant
265 // folded if the case was simple enough.
266 if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) {
267 // br(0 || X) -> br(X).
268 return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
269 }
270
271 // If we have "X || 0", simplify the code to use an uncond branch.
272 // "X || 1" would have been constant folded to 1.
273 if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) {
274 // br(X || 0) -> br(X).
275 return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
276 }
277
278 // Emit the LHS as a conditional. If the LHS conditional is true, we
279 // want to jump to the TrueBlock.
Daniel Dunbar6e3a10c2008-11-13 01:38:36 +0000280 llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false");
Chris Lattner3d6606b2008-11-12 08:04:58 +0000281 EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse);
282 EmitBlock(LHSFalse);
283
284 EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
285 return;
286 }
Chris Lattnercfbfe912008-11-12 08:13:36 +0000287 }
288
289 if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) {
290 // br(!x, t, f) -> br(x, f, t)
291 if (CondUOp->getOpcode() == UnaryOperator::LNot)
292 return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock);
Chris Lattner3d6606b2008-11-12 08:04:58 +0000293 }
294
Daniel Dunbarab81c632008-11-12 10:30:32 +0000295 if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) {
296 // Handle ?: operator.
297
298 // Just ignore GNU ?: extension.
299 if (CondOp->getLHS()) {
300 // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f))
301 llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
302 llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
303 EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock);
304 EmitBlock(LHSBlock);
305 EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock);
306 EmitBlock(RHSBlock);
307 EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock);
308 return;
309 }
310 }
311
Chris Lattner3d6606b2008-11-12 08:04:58 +0000312 // Emit the code with the fully general case.
313 llvm::Value *CondV = EvaluateExprAsBool(Cond);
314 Builder.CreateCondBr(CondV, TrueBlock, FalseBlock);
315}
316
Devang Patel7a78e432007-11-01 19:11:01 +0000317/// getCGRecordLayout - Return record layout info.
318const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT,
Chris Lattner7b2543e2008-02-05 06:55:31 +0000319 QualType Ty) {
320 const RecordType *RTy = Ty->getAsRecordType();
321 assert (RTy && "Unexpected type. RecordType expected here.");
Devang Patelaebd83f2007-10-23 02:10:49 +0000322
Chris Lattner7b2543e2008-02-05 06:55:31 +0000323 return CGT.getCGRecordLayout(RTy->getDecl());
Devang Patelaebd83f2007-10-23 02:10:49 +0000324}
Chris Lattner9d4e6202007-12-02 01:43:38 +0000325
Daniel Dunbar9503b782008-08-16 00:56:44 +0000326/// ErrorUnsupported - Print out an error that codegen doesn't support the
Chris Lattner9d4e6202007-12-02 01:43:38 +0000327/// specified stmt yet.
Daniel Dunbar49bddf72008-09-04 03:43:08 +0000328void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type,
329 bool OmitOnError) {
330 CGM.ErrorUnsupported(S, Type, OmitOnError);
Chris Lattner9d4e6202007-12-02 01:43:38 +0000331}
332
Daniel Dunbar879788d2008-08-04 16:51:22 +0000333unsigned CodeGenFunction::GetIDForAddrOfLabel(const LabelStmt *L) {
334 // Use LabelIDs.size() as the new ID if one hasn't been assigned.
335 return LabelIDs.insert(std::make_pair(L, LabelIDs.size())).first->second;
336}
337
Anders Carlsson82b0d0c2008-08-30 19:51:14 +0000338void CodeGenFunction::EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty)
339{
340 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
341 if (DestPtr->getType() != BP)
342 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
343
344 // Get size and alignment info for this aggregate.
345 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
346
347 // FIXME: Handle variable sized types.
348 const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth);
349
350 Builder.CreateCall4(CGM.getMemSetFn(), DestPtr,
351 llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty),
352 // TypeInfo.first describes size in bits.
353 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
354 llvm::ConstantInt::get(llvm::Type::Int32Ty,
355 TypeInfo.second/8));
356}
357
Daniel Dunbar879788d2008-08-04 16:51:22 +0000358void CodeGenFunction::EmitIndirectSwitches() {
359 llvm::BasicBlock *Default;
360
Daniel Dunbar8ccfa802008-08-04 17:24:44 +0000361 if (IndirectSwitches.empty())
362 return;
363
Daniel Dunbar879788d2008-08-04 16:51:22 +0000364 if (!LabelIDs.empty()) {
365 Default = getBasicBlockForLabel(LabelIDs.begin()->first);
366 } else {
367 // No possible targets for indirect goto, just emit an infinite
368 // loop.
Daniel Dunbar72f96552008-11-11 02:29:29 +0000369 Default = createBasicBlock("indirectgoto.loop", CurFn);
Daniel Dunbar879788d2008-08-04 16:51:22 +0000370 llvm::BranchInst::Create(Default, Default);
371 }
372
373 for (std::vector<llvm::SwitchInst*>::iterator i = IndirectSwitches.begin(),
374 e = IndirectSwitches.end(); i != e; ++i) {
375 llvm::SwitchInst *I = *i;
376
377 I->setSuccessor(0, Default);
378 for (std::map<const LabelStmt*,unsigned>::iterator LI = LabelIDs.begin(),
379 LE = LabelIDs.end(); LI != LE; ++LI) {
380 I->addCase(llvm::ConstantInt::get(llvm::Type::Int32Ty,
381 LI->second),
382 getBasicBlockForLabel(LI->first));
383 }
384 }
385}
Anders Carlsson285611e2008-11-04 05:30:00 +0000386
387llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty)
388{
389 // FIXME: This entire method is hardcoded for 32-bit X86.
390
391 const char *TargetPrefix = getContext().Target.getTargetPrefix();
392
393 if (strcmp(TargetPrefix, "x86") != 0 ||
394 getContext().Target.getPointerWidth(0) != 32)
395 return 0;
396
397 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
398 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
399
400 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
401 "ap");
402 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
403 llvm::Value *AddrTyped =
404 Builder.CreateBitCast(Addr,
405 llvm::PointerType::getUnqual(ConvertType(Ty)));
406
407 uint64_t SizeInBytes = getContext().getTypeSize(Ty) / 8;
408 const unsigned ArgumentSizeInBytes = 4;
409 if (SizeInBytes < ArgumentSizeInBytes)
410 SizeInBytes = ArgumentSizeInBytes;
411
412 llvm::Value *NextAddr =
413 Builder.CreateGEP(Addr,
414 llvm::ConstantInt::get(llvm::Type::Int32Ty, SizeInBytes),
415 "ap.next");
416 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
417
418 return AddrTyped;
419}
420
Anders Carlssonf860e022008-12-20 20:27:15 +0000421
Anders Carlsson32aa0c22008-12-12 07:19:02 +0000422llvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT)
423{
424 llvm::Value *&SizeEntry = VLASizeMap[VAT];
Anders Carlsson32aa0c22008-12-12 07:19:02 +0000425
Anders Carlssonf860e022008-12-20 20:27:15 +0000426 assert(SizeEntry && "Did not emit size for type");
427 return SizeEntry;
428}
Anders Carlsson32aa0c22008-12-12 07:19:02 +0000429
Anders Carlssond9767612008-12-20 20:46:34 +0000430llvm::Value *CodeGenFunction::EmitVLASize(QualType Ty)
Anders Carlssonf860e022008-12-20 20:27:15 +0000431{
Anders Carlssond9767612008-12-20 20:46:34 +0000432 assert(Ty->isVariablyModifiedType() &&
433 "Must pass variably modified type to EmitVLASizes!");
Anders Carlssonf860e022008-12-20 20:27:15 +0000434
Anders Carlssond9767612008-12-20 20:46:34 +0000435 if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) {
436 llvm::Value *&SizeEntry = VLASizeMap[VAT];
437
Anders Carlssonef2f7df2008-12-20 21:51:53 +0000438 if (!SizeEntry) {
439 // Get the element size;
440 llvm::Value *ElemSize;
Anders Carlssond9767612008-12-20 20:46:34 +0000441
Anders Carlssonef2f7df2008-12-20 21:51:53 +0000442 QualType ElemTy = VAT->getElementType();
Anders Carlssond9767612008-12-20 20:46:34 +0000443
Anders Carlssonef2f7df2008-12-20 21:51:53 +0000444 if (ElemTy->isVariableArrayType())
445 ElemSize = EmitVLASize(ElemTy);
446 else {
447 // FIXME: We use Int32Ty here because the alloca instruction takes a
448 // 32-bit integer. What should we do about overflow?
449 ElemSize = llvm::ConstantInt::get(llvm::Type::Int32Ty,
450 getContext().getTypeSize(ElemTy) / 8);
451 }
Anders Carlssond9767612008-12-20 20:46:34 +0000452
Anders Carlssonef2f7df2008-12-20 21:51:53 +0000453 llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr());
454
455 SizeEntry = Builder.CreateMul(ElemSize, NumElements);
Anders Carlssond9767612008-12-20 20:46:34 +0000456 }
457
Anders Carlssond9767612008-12-20 20:46:34 +0000458 return SizeEntry;
459 } else if (const PointerType *PT = Ty->getAsPointerType())
460 EmitVLASize(PT->getPointeeType());
Anders Carlssonf860e022008-12-20 20:27:15 +0000461 else {
Anders Carlssond9767612008-12-20 20:46:34 +0000462 assert(0 && "unknown VM type!");
Anders Carlsson32aa0c22008-12-12 07:19:02 +0000463 }
Anders Carlssond9767612008-12-20 20:46:34 +0000464
465 return 0;
Anders Carlsson32aa0c22008-12-12 07:19:02 +0000466}
Eli Friedman8f5e8782009-01-20 17:46:04 +0000467
468llvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) {
469 if (CGM.getContext().getBuiltinVaListType()->isArrayType()) {
470 return EmitScalarExpr(E);
471 }
472 return EmitLValue(E).getAddress();
473}