blob: 2fd8d638d65760e2cfbfc721b0e9a8bc4356273c [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 Dunbaraf05bb92008-08-26 08:29:31 +000073void CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
Daniel Dunbar0ffb1252008-08-04 16:51:22 +000074 // Finish emission of indirect switches.
75 EmitIndirectSwitches();
76
Chris Lattnerda138702007-07-16 21:28:45 +000077 assert(BreakContinueStack.empty() &&
78 "mismatched push/pop in break/continue stack!");
Daniel Dunbar5ca20842008-09-09 21:00:17 +000079
Daniel Dunbarb01d1912008-09-27 07:15:59 +000080 // Emit function epilog (to return). This has the nice side effect
81 // of also automatically handling code that falls off the end.
82 EmitBlock(ReturnBlock);
Daniel Dunbarf5bd45c2008-11-11 20:59:54 +000083
84 // Emit debug descriptor for function end.
85 if (CGDebugInfo *DI = CGM.getDebugInfo()) {
86 DI->setLocation(EndLoc);
87 DI->EmitRegionEnd(CurFn, Builder);
88 }
89
Daniel Dunbar17b708d2008-09-09 23:27:19 +000090 EmitFunctionEpilog(FnRetTy, ReturnValue);
Daniel Dunbar5ca20842008-09-09 21:00:17 +000091
Chris Lattner5a2fa142007-12-02 06:32:24 +000092 // Remove the AllocaInsertPt instruction, which is just a convenience for us.
93 AllocaInsertPt->eraseFromParent();
94 AllocaInsertPt = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000095}
96
Daniel Dunbar7c086512008-09-09 23:14:03 +000097void CodeGenFunction::StartFunction(const Decl *D, QualType RetTy,
98 llvm::Function *Fn,
Daniel Dunbar2284ac92008-10-18 18:22:23 +000099 const FunctionArgList &Args,
100 SourceLocation StartLoc) {
Daniel Dunbar7c086512008-09-09 23:14:03 +0000101 CurFuncDecl = D;
102 FnRetTy = RetTy;
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000103 CurFn = Fn;
Chris Lattner41110242008-06-17 18:05:57 +0000104 assert(CurFn->isDeclaration() && "Function already has body?");
105
Daniel Dunbar55e87422008-11-11 02:29:29 +0000106 llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
Daniel Dunbar5ca20842008-09-09 21:00:17 +0000107
Chris Lattner41110242008-06-17 18:05:57 +0000108 // Create a marker to make it easy to insert allocas into the entryblock
109 // later. Don't create this with the builder, because we don't want it
110 // folded.
111 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
112 AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt",
113 EntryBB);
Daniel Dunbar5ca20842008-09-09 21:00:17 +0000114
Daniel Dunbar55e87422008-11-11 02:29:29 +0000115 ReturnBlock = createBasicBlock("return");
Daniel Dunbar5ca20842008-09-09 21:00:17 +0000116 ReturnValue = 0;
Daniel Dunbar7c086512008-09-09 23:14:03 +0000117 if (!RetTy->isVoidType())
118 ReturnValue = CreateTempAlloca(ConvertType(RetTy), "retval");
Daniel Dunbar5ca20842008-09-09 21:00:17 +0000119
Chris Lattner41110242008-06-17 18:05:57 +0000120 Builder.SetInsertPoint(EntryBB);
121
Sanjiv Guptaaf994172008-07-04 11:04:26 +0000122 // Emit subprogram debug descriptor.
Daniel Dunbar7c086512008-09-09 23:14:03 +0000123 // FIXME: The cast here is a huge hack.
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000124 if (CGDebugInfo *DI = CGM.getDebugInfo()) {
125 DI->setLocation(StartLoc);
126 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Chris Lattner8ec03f52008-11-24 03:54:41 +0000127 DI->EmitFunctionStart(FD->getIdentifier()->getName(),
128 RetTy, CurFn, Builder);
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000129 } else {
130 // Just use LLVM function name.
131 DI->EmitFunctionStart(Fn->getName().c_str(),
132 RetTy, CurFn, Builder);
Sanjiv Guptaaf994172008-07-04 11:04:26 +0000133 }
Sanjiv Guptaaf994172008-07-04 11:04:26 +0000134 }
135
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000136 EmitFunctionProlog(CurFn, FnRetTy, Args);
Anders Carlsson751358f2008-12-20 21:28:43 +0000137
138 // If any of the arguments have a variably modified type, make sure to
139 // emit the type size.
140 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
141 i != e; ++i) {
142 QualType Ty = i->second;
143
144 if (Ty->isVariablyModifiedType())
145 EmitVLASize(Ty);
146 }
Daniel Dunbar7c086512008-09-09 23:14:03 +0000147}
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000148
Daniel Dunbar7c086512008-09-09 23:14:03 +0000149void CodeGenFunction::GenerateCode(const FunctionDecl *FD,
150 llvm::Function *Fn) {
151 FunctionArgList Args;
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000152 if (FD->getNumParams()) {
153 const FunctionTypeProto* FProto = FD->getType()->getAsFunctionTypeProto();
154 assert(FProto && "Function def must have prototype!");
Daniel Dunbar7c086512008-09-09 23:14:03 +0000155
156 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i)
157 Args.push_back(std::make_pair(FD->getParamDecl(i),
158 FProto->getArgType(i)));
Chris Lattner41110242008-06-17 18:05:57 +0000159 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000160
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000161 StartFunction(FD, FD->getResultType(), Fn, Args,
162 cast<CompoundStmt>(FD->getBody())->getLBracLoc());
Daniel Dunbar7c086512008-09-09 23:14:03 +0000163
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000164 EmitStmt(FD->getBody());
165
166 const CompoundStmt *S = dyn_cast<CompoundStmt>(FD->getBody());
167 if (S) {
168 FinishFunction(S->getRBracLoc());
169 } else {
170 FinishFunction();
171 }
Chris Lattner41110242008-06-17 18:05:57 +0000172}
173
Chris Lattner0946ccd2008-11-11 07:41:27 +0000174/// ContainsLabel - Return true if the statement contains a label in it. If
175/// this statement is not executed normally, it not containing a label means
176/// that we can just remove the code.
177bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) {
178 // Null statement, not a label!
179 if (S == 0) return false;
180
181 // If this is a label, we have to emit the code, consider something like:
182 // if (0) { ... foo: bar(); } goto foo;
183 if (isa<LabelStmt>(S))
184 return true;
185
186 // If this is a case/default statement, and we haven't seen a switch, we have
187 // to emit the code.
188 if (isa<SwitchCase>(S) && !IgnoreCaseStmts)
189 return true;
190
191 // If this is a switch statement, we want to ignore cases below it.
192 if (isa<SwitchStmt>(S))
193 IgnoreCaseStmts = true;
194
195 // Scan subexpressions for verboten labels.
196 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
197 I != E; ++I)
198 if (ContainsLabel(*I, IgnoreCaseStmts))
199 return true;
200
201 return false;
202}
203
Chris Lattner31a09842008-11-12 08:04:58 +0000204
205/// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to
206/// a constant, or if it does but contains a label, return 0. If it constant
207/// folds to 'true' and does not contain a label, return 1, if it constant folds
208/// to 'false' and does not contain a label, return -1.
209int CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) {
Daniel Dunbar36bc14c2008-11-12 22:37:10 +0000210 // FIXME: Rename and handle conversion of other evaluatable things
211 // to bool.
Anders Carlsson64712f12008-12-01 02:46:24 +0000212 Expr::EvalResult Result;
213 if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() ||
214 Result.HasSideEffects)
Anders Carlssonef5a66d2008-11-22 22:32:07 +0000215 return 0; // Not foldable, not integer or not fully evaluatable.
Chris Lattner31a09842008-11-12 08:04:58 +0000216
217 if (CodeGenFunction::ContainsLabel(Cond))
218 return 0; // Contains a label.
219
Anders Carlsson64712f12008-12-01 02:46:24 +0000220 return Result.Val.getInt().getBoolValue() ? 1 : -1;
Chris Lattner31a09842008-11-12 08:04:58 +0000221}
222
223
224/// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if
225/// statement) to the specified blocks. Based on the condition, this might try
226/// to simplify the codegen of the conditional based on the branch.
227///
228void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
229 llvm::BasicBlock *TrueBlock,
230 llvm::BasicBlock *FalseBlock) {
231 if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond))
232 return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock);
233
234 if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) {
235 // Handle X && Y in a condition.
236 if (CondBOp->getOpcode() == BinaryOperator::LAnd) {
237 // If we have "1 && X", simplify the code. "0 && X" would have constant
238 // folded if the case was simple enough.
239 if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) {
240 // br(1 && X) -> br(X).
241 return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
242 }
243
244 // If we have "X && 1", simplify the code to use an uncond branch.
245 // "X && 0" would have been constant folded to 0.
246 if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) {
247 // br(X && 1) -> br(X).
248 return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
249 }
250
251 // Emit the LHS as a conditional. If the LHS conditional is false, we
252 // want to jump to the FalseBlock.
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000253 llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true");
Chris Lattner31a09842008-11-12 08:04:58 +0000254 EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock);
255 EmitBlock(LHSTrue);
256
257 EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
258 return;
259 } else if (CondBOp->getOpcode() == BinaryOperator::LOr) {
260 // If we have "0 || X", simplify the code. "1 || X" would have constant
261 // folded if the case was simple enough.
262 if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) {
263 // br(0 || X) -> br(X).
264 return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
265 }
266
267 // If we have "X || 0", simplify the code to use an uncond branch.
268 // "X || 1" would have been constant folded to 1.
269 if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) {
270 // br(X || 0) -> br(X).
271 return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
272 }
273
274 // Emit the LHS as a conditional. If the LHS conditional is true, we
275 // want to jump to the TrueBlock.
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000276 llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false");
Chris Lattner31a09842008-11-12 08:04:58 +0000277 EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse);
278 EmitBlock(LHSFalse);
279
280 EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
281 return;
282 }
Chris Lattner552f4c42008-11-12 08:13:36 +0000283 }
284
285 if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) {
286 // br(!x, t, f) -> br(x, f, t)
287 if (CondUOp->getOpcode() == UnaryOperator::LNot)
288 return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock);
Chris Lattner31a09842008-11-12 08:04:58 +0000289 }
290
Daniel Dunbar09b14892008-11-12 10:30:32 +0000291 if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) {
292 // Handle ?: operator.
293
294 // Just ignore GNU ?: extension.
295 if (CondOp->getLHS()) {
296 // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f))
297 llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
298 llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
299 EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock);
300 EmitBlock(LHSBlock);
301 EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock);
302 EmitBlock(RHSBlock);
303 EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock);
304 return;
305 }
306 }
307
Chris Lattner31a09842008-11-12 08:04:58 +0000308 // Emit the code with the fully general case.
309 llvm::Value *CondV = EvaluateExprAsBool(Cond);
310 Builder.CreateCondBr(CondV, TrueBlock, FalseBlock);
311}
312
Devang Patel88a981b2007-11-01 19:11:01 +0000313/// getCGRecordLayout - Return record layout info.
314const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT,
Chris Lattneraf319132008-02-05 06:55:31 +0000315 QualType Ty) {
316 const RecordType *RTy = Ty->getAsRecordType();
317 assert (RTy && "Unexpected type. RecordType expected here.");
Devang Patelb84a06e2007-10-23 02:10:49 +0000318
Chris Lattneraf319132008-02-05 06:55:31 +0000319 return CGT.getCGRecordLayout(RTy->getDecl());
Devang Patelb84a06e2007-10-23 02:10:49 +0000320}
Chris Lattnerdc5e8262007-12-02 01:43:38 +0000321
Daniel Dunbar488e9932008-08-16 00:56:44 +0000322/// ErrorUnsupported - Print out an error that codegen doesn't support the
Chris Lattnerdc5e8262007-12-02 01:43:38 +0000323/// specified stmt yet.
Daniel Dunbar90df4b62008-09-04 03:43:08 +0000324void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type,
325 bool OmitOnError) {
326 CGM.ErrorUnsupported(S, Type, OmitOnError);
Chris Lattnerdc5e8262007-12-02 01:43:38 +0000327}
328
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000329unsigned CodeGenFunction::GetIDForAddrOfLabel(const LabelStmt *L) {
330 // Use LabelIDs.size() as the new ID if one hasn't been assigned.
331 return LabelIDs.insert(std::make_pair(L, LabelIDs.size())).first->second;
332}
333
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000334void CodeGenFunction::EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty)
335{
336 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
337 if (DestPtr->getType() != BP)
338 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
339
340 // Get size and alignment info for this aggregate.
341 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
342
343 // FIXME: Handle variable sized types.
344 const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth);
345
346 Builder.CreateCall4(CGM.getMemSetFn(), DestPtr,
347 llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty),
348 // TypeInfo.first describes size in bits.
349 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
350 llvm::ConstantInt::get(llvm::Type::Int32Ty,
351 TypeInfo.second/8));
352}
353
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000354void CodeGenFunction::EmitIndirectSwitches() {
355 llvm::BasicBlock *Default;
356
Daniel Dunbar76526a52008-08-04 17:24:44 +0000357 if (IndirectSwitches.empty())
358 return;
359
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000360 if (!LabelIDs.empty()) {
361 Default = getBasicBlockForLabel(LabelIDs.begin()->first);
362 } else {
363 // No possible targets for indirect goto, just emit an infinite
364 // loop.
Daniel Dunbar55e87422008-11-11 02:29:29 +0000365 Default = createBasicBlock("indirectgoto.loop", CurFn);
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000366 llvm::BranchInst::Create(Default, Default);
367 }
368
369 for (std::vector<llvm::SwitchInst*>::iterator i = IndirectSwitches.begin(),
370 e = IndirectSwitches.end(); i != e; ++i) {
371 llvm::SwitchInst *I = *i;
372
373 I->setSuccessor(0, Default);
374 for (std::map<const LabelStmt*,unsigned>::iterator LI = LabelIDs.begin(),
375 LE = LabelIDs.end(); LI != LE; ++LI) {
376 I->addCase(llvm::ConstantInt::get(llvm::Type::Int32Ty,
377 LI->second),
378 getBasicBlockForLabel(LI->first));
379 }
380 }
381}
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000382
383llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty)
384{
385 // FIXME: This entire method is hardcoded for 32-bit X86.
386
387 const char *TargetPrefix = getContext().Target.getTargetPrefix();
388
389 if (strcmp(TargetPrefix, "x86") != 0 ||
390 getContext().Target.getPointerWidth(0) != 32)
391 return 0;
392
393 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
394 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
395
396 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
397 "ap");
398 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
399 llvm::Value *AddrTyped =
400 Builder.CreateBitCast(Addr,
401 llvm::PointerType::getUnqual(ConvertType(Ty)));
402
403 uint64_t SizeInBytes = getContext().getTypeSize(Ty) / 8;
404 const unsigned ArgumentSizeInBytes = 4;
405 if (SizeInBytes < ArgumentSizeInBytes)
406 SizeInBytes = ArgumentSizeInBytes;
407
408 llvm::Value *NextAddr =
409 Builder.CreateGEP(Addr,
410 llvm::ConstantInt::get(llvm::Type::Int32Ty, SizeInBytes),
411 "ap.next");
412 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
413
414 return AddrTyped;
415}
416
Anders Carlssonf666b772008-12-20 20:27:15 +0000417
Anders Carlssondcc90d82008-12-12 07:19:02 +0000418llvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT)
419{
420 llvm::Value *&SizeEntry = VLASizeMap[VAT];
Anders Carlssondcc90d82008-12-12 07:19:02 +0000421
Anders Carlssonf666b772008-12-20 20:27:15 +0000422 assert(SizeEntry && "Did not emit size for type");
423 return SizeEntry;
424}
Anders Carlssondcc90d82008-12-12 07:19:02 +0000425
Anders Carlsson60d35412008-12-20 20:46:34 +0000426llvm::Value *CodeGenFunction::EmitVLASize(QualType Ty)
Anders Carlssonf666b772008-12-20 20:27:15 +0000427{
Anders Carlsson60d35412008-12-20 20:46:34 +0000428 assert(Ty->isVariablyModifiedType() &&
429 "Must pass variably modified type to EmitVLASizes!");
Anders Carlssonf666b772008-12-20 20:27:15 +0000430
Anders Carlsson60d35412008-12-20 20:46:34 +0000431 if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) {
432 llvm::Value *&SizeEntry = VLASizeMap[VAT];
433
Anders Carlssonfcdbb932008-12-20 21:51:53 +0000434 if (!SizeEntry) {
435 // Get the element size;
436 llvm::Value *ElemSize;
Anders Carlsson60d35412008-12-20 20:46:34 +0000437
Anders Carlssonfcdbb932008-12-20 21:51:53 +0000438 QualType ElemTy = VAT->getElementType();
Anders Carlsson60d35412008-12-20 20:46:34 +0000439
Anders Carlssonfcdbb932008-12-20 21:51:53 +0000440 if (ElemTy->isVariableArrayType())
441 ElemSize = EmitVLASize(ElemTy);
442 else {
443 // FIXME: We use Int32Ty here because the alloca instruction takes a
444 // 32-bit integer. What should we do about overflow?
445 ElemSize = llvm::ConstantInt::get(llvm::Type::Int32Ty,
446 getContext().getTypeSize(ElemTy) / 8);
447 }
Anders Carlsson60d35412008-12-20 20:46:34 +0000448
Anders Carlssonfcdbb932008-12-20 21:51:53 +0000449 llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr());
450
451 SizeEntry = Builder.CreateMul(ElemSize, NumElements);
Anders Carlsson60d35412008-12-20 20:46:34 +0000452 }
453
Anders Carlsson60d35412008-12-20 20:46:34 +0000454 return SizeEntry;
455 } else if (const PointerType *PT = Ty->getAsPointerType())
456 EmitVLASize(PT->getPointeeType());
Anders Carlssonf666b772008-12-20 20:27:15 +0000457 else {
Anders Carlsson60d35412008-12-20 20:46:34 +0000458 assert(0 && "unknown VM type!");
Anders Carlssondcc90d82008-12-12 07:19:02 +0000459 }
Anders Carlsson60d35412008-12-20 20:46:34 +0000460
461 return 0;
Anders Carlssondcc90d82008-12-12 07:19:02 +0000462}
Eli Friedman4fd0aa52009-01-20 17:46:04 +0000463
464llvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) {
465 if (CGM.getContext().getBuiltinVaListType()->isArrayType()) {
466 return EmitScalarExpr(E);
467 }
468 return EmitLValue(E).getAddress();
469}