blob: 5d34f65fc14dce36a4cf051cfca30b1294646cf9 [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 Dunbar17b708d2008-09-09 23:27:19 +0000128 EmitFunctionEpilog(FnRetTy, 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 Dunbar17b708d2008-09-09 23:27:19 +0000174 EmitFunctionProlog(CurFn, FnRetTy, Args);
Anders Carlsson751358f2008-12-20 21:28:43 +0000175
176 // If any of the arguments have a variably modified type, make sure to
177 // emit the type size.
178 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
179 i != e; ++i) {
180 QualType Ty = i->second;
181
182 if (Ty->isVariablyModifiedType())
183 EmitVLASize(Ty);
184 }
Daniel Dunbar7c086512008-09-09 23:14:03 +0000185}
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000186
Daniel Dunbar7c086512008-09-09 23:14:03 +0000187void CodeGenFunction::GenerateCode(const FunctionDecl *FD,
188 llvm::Function *Fn) {
189 FunctionArgList Args;
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000190 if (FD->getNumParams()) {
191 const FunctionTypeProto* FProto = FD->getType()->getAsFunctionTypeProto();
192 assert(FProto && "Function def must have prototype!");
Daniel Dunbar7c086512008-09-09 23:14:03 +0000193
194 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i)
195 Args.push_back(std::make_pair(FD->getParamDecl(i),
196 FProto->getArgType(i)));
Chris Lattner41110242008-06-17 18:05:57 +0000197 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000198
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000199 StartFunction(FD, FD->getResultType(), Fn, Args,
200 cast<CompoundStmt>(FD->getBody())->getLBracLoc());
Daniel Dunbar7c086512008-09-09 23:14:03 +0000201
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000202 EmitStmt(FD->getBody());
203
204 const CompoundStmt *S = dyn_cast<CompoundStmt>(FD->getBody());
205 if (S) {
206 FinishFunction(S->getRBracLoc());
207 } else {
208 FinishFunction();
209 }
Chris Lattner41110242008-06-17 18:05:57 +0000210}
211
Chris Lattner0946ccd2008-11-11 07:41:27 +0000212/// ContainsLabel - Return true if the statement contains a label in it. If
213/// this statement is not executed normally, it not containing a label means
214/// that we can just remove the code.
215bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) {
216 // Null statement, not a label!
217 if (S == 0) return false;
218
219 // If this is a label, we have to emit the code, consider something like:
220 // if (0) { ... foo: bar(); } goto foo;
221 if (isa<LabelStmt>(S))
222 return true;
223
224 // If this is a case/default statement, and we haven't seen a switch, we have
225 // to emit the code.
226 if (isa<SwitchCase>(S) && !IgnoreCaseStmts)
227 return true;
228
229 // If this is a switch statement, we want to ignore cases below it.
230 if (isa<SwitchStmt>(S))
231 IgnoreCaseStmts = true;
232
233 // Scan subexpressions for verboten labels.
234 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
235 I != E; ++I)
236 if (ContainsLabel(*I, IgnoreCaseStmts))
237 return true;
238
239 return false;
240}
241
Chris Lattner31a09842008-11-12 08:04:58 +0000242
243/// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to
244/// a constant, or if it does but contains a label, return 0. If it constant
245/// folds to 'true' and does not contain a label, return 1, if it constant folds
246/// to 'false' and does not contain a label, return -1.
247int CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) {
Daniel Dunbar36bc14c2008-11-12 22:37:10 +0000248 // FIXME: Rename and handle conversion of other evaluatable things
249 // to bool.
Anders Carlsson64712f12008-12-01 02:46:24 +0000250 Expr::EvalResult Result;
251 if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() ||
252 Result.HasSideEffects)
Anders Carlssonef5a66d2008-11-22 22:32:07 +0000253 return 0; // Not foldable, not integer or not fully evaluatable.
Chris Lattner31a09842008-11-12 08:04:58 +0000254
255 if (CodeGenFunction::ContainsLabel(Cond))
256 return 0; // Contains a label.
257
Anders Carlsson64712f12008-12-01 02:46:24 +0000258 return Result.Val.getInt().getBoolValue() ? 1 : -1;
Chris Lattner31a09842008-11-12 08:04:58 +0000259}
260
261
262/// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if
263/// statement) to the specified blocks. Based on the condition, this might try
264/// to simplify the codegen of the conditional based on the branch.
265///
266void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
267 llvm::BasicBlock *TrueBlock,
268 llvm::BasicBlock *FalseBlock) {
269 if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond))
270 return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock);
271
272 if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) {
273 // Handle X && Y in a condition.
274 if (CondBOp->getOpcode() == BinaryOperator::LAnd) {
275 // If we have "1 && X", simplify the code. "0 && X" would have constant
276 // folded if the case was simple enough.
277 if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) {
278 // br(1 && X) -> br(X).
279 return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
280 }
281
282 // If we have "X && 1", simplify the code to use an uncond branch.
283 // "X && 0" would have been constant folded to 0.
284 if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) {
285 // br(X && 1) -> br(X).
286 return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
287 }
288
289 // Emit the LHS as a conditional. If the LHS conditional is false, we
290 // want to jump to the FalseBlock.
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000291 llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true");
Chris Lattner31a09842008-11-12 08:04:58 +0000292 EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock);
293 EmitBlock(LHSTrue);
294
295 EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
296 return;
297 } else if (CondBOp->getOpcode() == BinaryOperator::LOr) {
298 // If we have "0 || X", simplify the code. "1 || X" would have constant
299 // folded if the case was simple enough.
300 if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) {
301 // br(0 || X) -> br(X).
302 return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
303 }
304
305 // If we have "X || 0", simplify the code to use an uncond branch.
306 // "X || 1" would have been constant folded to 1.
307 if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) {
308 // br(X || 0) -> br(X).
309 return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
310 }
311
312 // Emit the LHS as a conditional. If the LHS conditional is true, we
313 // want to jump to the TrueBlock.
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000314 llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false");
Chris Lattner31a09842008-11-12 08:04:58 +0000315 EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse);
316 EmitBlock(LHSFalse);
317
318 EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
319 return;
320 }
Chris Lattner552f4c42008-11-12 08:13:36 +0000321 }
322
323 if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) {
324 // br(!x, t, f) -> br(x, f, t)
325 if (CondUOp->getOpcode() == UnaryOperator::LNot)
326 return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock);
Chris Lattner31a09842008-11-12 08:04:58 +0000327 }
328
Daniel Dunbar09b14892008-11-12 10:30:32 +0000329 if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) {
330 // Handle ?: operator.
331
332 // Just ignore GNU ?: extension.
333 if (CondOp->getLHS()) {
334 // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f))
335 llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
336 llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
337 EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock);
338 EmitBlock(LHSBlock);
339 EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock);
340 EmitBlock(RHSBlock);
341 EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock);
342 return;
343 }
344 }
345
Chris Lattner31a09842008-11-12 08:04:58 +0000346 // Emit the code with the fully general case.
347 llvm::Value *CondV = EvaluateExprAsBool(Cond);
348 Builder.CreateCondBr(CondV, TrueBlock, FalseBlock);
349}
350
Devang Patel88a981b2007-11-01 19:11:01 +0000351/// getCGRecordLayout - Return record layout info.
352const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT,
Chris Lattneraf319132008-02-05 06:55:31 +0000353 QualType Ty) {
354 const RecordType *RTy = Ty->getAsRecordType();
355 assert (RTy && "Unexpected type. RecordType expected here.");
Devang Patelb84a06e2007-10-23 02:10:49 +0000356
Chris Lattneraf319132008-02-05 06:55:31 +0000357 return CGT.getCGRecordLayout(RTy->getDecl());
Devang Patelb84a06e2007-10-23 02:10:49 +0000358}
Chris Lattnerdc5e8262007-12-02 01:43:38 +0000359
Daniel Dunbar488e9932008-08-16 00:56:44 +0000360/// ErrorUnsupported - Print out an error that codegen doesn't support the
Chris Lattnerdc5e8262007-12-02 01:43:38 +0000361/// specified stmt yet.
Daniel Dunbar90df4b62008-09-04 03:43:08 +0000362void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type,
363 bool OmitOnError) {
364 CGM.ErrorUnsupported(S, Type, OmitOnError);
Chris Lattnerdc5e8262007-12-02 01:43:38 +0000365}
366
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000367unsigned CodeGenFunction::GetIDForAddrOfLabel(const LabelStmt *L) {
368 // Use LabelIDs.size() as the new ID if one hasn't been assigned.
369 return LabelIDs.insert(std::make_pair(L, LabelIDs.size())).first->second;
370}
371
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000372void CodeGenFunction::EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty)
373{
374 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
375 if (DestPtr->getType() != BP)
376 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
377
378 // Get size and alignment info for this aggregate.
379 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
380
381 // FIXME: Handle variable sized types.
382 const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth);
383
384 Builder.CreateCall4(CGM.getMemSetFn(), DestPtr,
385 llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty),
386 // TypeInfo.first describes size in bits.
387 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
388 llvm::ConstantInt::get(llvm::Type::Int32Ty,
389 TypeInfo.second/8));
390}
391
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000392void CodeGenFunction::EmitIndirectSwitches() {
393 llvm::BasicBlock *Default;
394
Daniel Dunbar76526a52008-08-04 17:24:44 +0000395 if (IndirectSwitches.empty())
396 return;
397
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000398 if (!LabelIDs.empty()) {
399 Default = getBasicBlockForLabel(LabelIDs.begin()->first);
400 } else {
401 // No possible targets for indirect goto, just emit an infinite
402 // loop.
Daniel Dunbar55e87422008-11-11 02:29:29 +0000403 Default = createBasicBlock("indirectgoto.loop", CurFn);
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000404 llvm::BranchInst::Create(Default, Default);
405 }
406
407 for (std::vector<llvm::SwitchInst*>::iterator i = IndirectSwitches.begin(),
408 e = IndirectSwitches.end(); i != e; ++i) {
409 llvm::SwitchInst *I = *i;
410
411 I->setSuccessor(0, Default);
412 for (std::map<const LabelStmt*,unsigned>::iterator LI = LabelIDs.begin(),
413 LE = LabelIDs.end(); LI != LE; ++LI) {
414 I->addCase(llvm::ConstantInt::get(llvm::Type::Int32Ty,
415 LI->second),
416 getBasicBlockForLabel(LI->first));
417 }
418 }
419}
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000420
421llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty)
422{
423 // FIXME: This entire method is hardcoded for 32-bit X86.
424
425 const char *TargetPrefix = getContext().Target.getTargetPrefix();
426
427 if (strcmp(TargetPrefix, "x86") != 0 ||
428 getContext().Target.getPointerWidth(0) != 32)
429 return 0;
430
431 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
432 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
433
434 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
435 "ap");
436 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
437 llvm::Value *AddrTyped =
438 Builder.CreateBitCast(Addr,
439 llvm::PointerType::getUnqual(ConvertType(Ty)));
440
441 uint64_t SizeInBytes = getContext().getTypeSize(Ty) / 8;
442 const unsigned ArgumentSizeInBytes = 4;
443 if (SizeInBytes < ArgumentSizeInBytes)
444 SizeInBytes = ArgumentSizeInBytes;
445
446 llvm::Value *NextAddr =
447 Builder.CreateGEP(Addr,
448 llvm::ConstantInt::get(llvm::Type::Int32Ty, SizeInBytes),
449 "ap.next");
450 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
451
452 return AddrTyped;
453}
454
Anders Carlssonf666b772008-12-20 20:27:15 +0000455
Anders Carlssondcc90d82008-12-12 07:19:02 +0000456llvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT)
457{
458 llvm::Value *&SizeEntry = VLASizeMap[VAT];
Anders Carlssondcc90d82008-12-12 07:19:02 +0000459
Anders Carlssonf666b772008-12-20 20:27:15 +0000460 assert(SizeEntry && "Did not emit size for type");
461 return SizeEntry;
462}
Anders Carlssondcc90d82008-12-12 07:19:02 +0000463
Anders Carlsson60d35412008-12-20 20:46:34 +0000464llvm::Value *CodeGenFunction::EmitVLASize(QualType Ty)
Anders Carlssonf666b772008-12-20 20:27:15 +0000465{
Anders Carlsson60d35412008-12-20 20:46:34 +0000466 assert(Ty->isVariablyModifiedType() &&
467 "Must pass variably modified type to EmitVLASizes!");
Anders Carlssonf666b772008-12-20 20:27:15 +0000468
Anders Carlsson60d35412008-12-20 20:46:34 +0000469 if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) {
470 llvm::Value *&SizeEntry = VLASizeMap[VAT];
471
Anders Carlssonfcdbb932008-12-20 21:51:53 +0000472 if (!SizeEntry) {
473 // Get the element size;
474 llvm::Value *ElemSize;
Anders Carlsson60d35412008-12-20 20:46:34 +0000475
Anders Carlssonfcdbb932008-12-20 21:51:53 +0000476 QualType ElemTy = VAT->getElementType();
Anders Carlsson60d35412008-12-20 20:46:34 +0000477
Anders Carlssonfcdbb932008-12-20 21:51:53 +0000478 if (ElemTy->isVariableArrayType())
479 ElemSize = EmitVLASize(ElemTy);
480 else {
481 // FIXME: We use Int32Ty here because the alloca instruction takes a
482 // 32-bit integer. What should we do about overflow?
483 ElemSize = llvm::ConstantInt::get(llvm::Type::Int32Ty,
484 getContext().getTypeSize(ElemTy) / 8);
485 }
Anders Carlsson60d35412008-12-20 20:46:34 +0000486
Anders Carlssonfcdbb932008-12-20 21:51:53 +0000487 llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr());
488
489 SizeEntry = Builder.CreateMul(ElemSize, NumElements);
Anders Carlsson60d35412008-12-20 20:46:34 +0000490 }
491
Anders Carlsson60d35412008-12-20 20:46:34 +0000492 return SizeEntry;
493 } else if (const PointerType *PT = Ty->getAsPointerType())
494 EmitVLASize(PT->getPointeeType());
Anders Carlssonf666b772008-12-20 20:27:15 +0000495 else {
Anders Carlsson60d35412008-12-20 20:46:34 +0000496 assert(0 && "unknown VM type!");
Anders Carlssondcc90d82008-12-12 07:19:02 +0000497 }
Anders Carlsson60d35412008-12-20 20:46:34 +0000498
499 return 0;
Anders Carlssondcc90d82008-12-12 07:19:02 +0000500}
Eli Friedman4fd0aa52009-01-20 17:46:04 +0000501
502llvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) {
503 if (CGM.getContext().getBuiltinVaListType()->isArrayType()) {
504 return EmitScalarExpr(E);
505 }
506 return EmitLValue(E).getAddress();
507}