2007-05-28 09:07:47 +08:00
|
|
|
//===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-05-28 09:07:47 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This coordinates the per-function state used while generating code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenFunction.h"
|
|
|
|
#include "CodeGenModule.h"
|
2008-05-22 09:40:10 +08:00
|
|
|
#include "CGDebugInfo.h"
|
2007-05-30 07:17:50 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2008-11-12 16:04:58 +08:00
|
|
|
#include "clang/AST/APValue.h"
|
2008-08-11 13:00:27 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2008-08-11 13:35:13 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
2007-09-29 05:49:18 +08:00
|
|
|
#include "llvm/Support/CFG.h"
|
2007-05-28 09:07:47 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2007-05-30 07:17:50 +08:00
|
|
|
CodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
|
2007-10-09 04:57:48 +08:00
|
|
|
: CGM(cgm), Target(CGM.getContext().Target), SwitchInsn(NULL),
|
2009-02-07 20:52:26 +08:00
|
|
|
CaseRangeBlock(NULL), StackDepth(0) {
|
2008-06-18 02:05:57 +08:00
|
|
|
LLVMIntTy = ConvertType(getContext().IntTy);
|
|
|
|
LLVMPointerWidth = Target.getPointerWidth(0);
|
|
|
|
}
|
2007-05-30 07:17:50 +08:00
|
|
|
|
2007-06-03 06:49:07 +08:00
|
|
|
ASTContext &CodeGenFunction::getContext() const {
|
|
|
|
return CGM.getContext();
|
|
|
|
}
|
|
|
|
|
2007-05-30 07:17:50 +08:00
|
|
|
|
2007-05-30 08:13:02 +08:00
|
|
|
llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) {
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::BasicBlock *&BB = LabelMap[S];
|
2007-05-30 08:13:02 +08:00
|
|
|
if (BB) return BB;
|
|
|
|
|
|
|
|
// Create, but don't insert, the new block.
|
2008-11-11 10:29:29 +08:00
|
|
|
return BB = createBasicBlock(S->getName());
|
2007-05-30 08:13:02 +08:00
|
|
|
}
|
|
|
|
|
2008-02-27 05:41:45 +08:00
|
|
|
llvm::Constant *
|
2008-04-16 06:42:06 +08:00
|
|
|
CodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) {
|
2008-02-27 05:41:45 +08:00
|
|
|
return cast<llvm::Constant>(LocalDeclMap[BVD]);
|
|
|
|
}
|
2007-05-30 08:13:02 +08:00
|
|
|
|
2008-09-11 17:15:33 +08:00
|
|
|
llvm::Value *CodeGenFunction::GetAddrOfLocalVar(const VarDecl *VD)
|
|
|
|
{
|
|
|
|
return LocalDeclMap[VD];
|
|
|
|
}
|
|
|
|
|
2009-02-04 07:03:55 +08:00
|
|
|
const llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) {
|
|
|
|
return CGM.getTypes().ConvertTypeForMem(T);
|
|
|
|
}
|
|
|
|
|
2007-06-23 03:05:19 +08:00
|
|
|
const llvm::Type *CodeGenFunction::ConvertType(QualType T) {
|
|
|
|
return CGM.getTypes().ConvertType(T);
|
2007-06-09 10:28:57 +08:00
|
|
|
}
|
2007-05-30 07:17:50 +08:00
|
|
|
|
2008-06-18 02:05:57 +08:00
|
|
|
bool CodeGenFunction::isObjCPointerType(QualType T) {
|
|
|
|
// All Objective-C types are pointers.
|
|
|
|
return T->isObjCInterfaceType() ||
|
|
|
|
T->isObjCQualifiedInterfaceType() || T->isObjCQualifiedIdType();
|
2007-06-23 06:02:34 +08:00
|
|
|
}
|
|
|
|
|
2008-06-18 02:05:57 +08:00
|
|
|
bool CodeGenFunction::hasAggregateLLVMType(QualType T) {
|
2009-01-09 10:44:18 +08:00
|
|
|
// FIXME: Use positive checks instead of negative ones to be more
|
|
|
|
// robust in the face of extension.
|
2008-06-18 02:05:57 +08:00
|
|
|
return !isObjCPointerType(T) &&!T->isRealType() && !T->isPointerLikeType() &&
|
2009-01-09 10:44:18 +08:00
|
|
|
!T->isVoidType() && !T->isVectorType() && !T->isFunctionType() &&
|
|
|
|
!T->isBlockPointerType();
|
2008-06-18 02:05:57 +08:00
|
|
|
}
|
2008-03-31 07:03:07 +08:00
|
|
|
|
2009-01-27 07:27:52 +08:00
|
|
|
void CodeGenFunction::EmitReturnBlock() {
|
|
|
|
// For cleanliness, we try to avoid emitting the return block for
|
|
|
|
// simple cases.
|
|
|
|
llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
|
|
|
|
|
|
|
|
if (CurBB) {
|
|
|
|
assert(!CurBB->getTerminator() && "Unexpected terminated block.");
|
|
|
|
|
|
|
|
// We have a valid insert point, reuse it if there are no explicit
|
|
|
|
// jumps to the return block.
|
|
|
|
if (ReturnBlock->use_empty())
|
|
|
|
delete ReturnBlock;
|
|
|
|
else
|
|
|
|
EmitBlock(ReturnBlock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, if the return block is the target of a single direct
|
|
|
|
// branch then we can just put the code in that block instead. This
|
|
|
|
// cleans up functions which started with a unified return block.
|
|
|
|
if (ReturnBlock->hasOneUse()) {
|
|
|
|
llvm::BranchInst *BI =
|
|
|
|
dyn_cast<llvm::BranchInst>(*ReturnBlock->use_begin());
|
|
|
|
if (BI && BI->isUnconditional() && BI->getSuccessor(0) == ReturnBlock) {
|
|
|
|
// Reset insertion point and delete the branch.
|
|
|
|
Builder.SetInsertPoint(BI->getParent());
|
|
|
|
BI->eraseFromParent();
|
|
|
|
delete ReturnBlock;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: We are at an unreachable point, there is no reason to emit
|
|
|
|
// the block unless it has uses. However, we still need a place to
|
|
|
|
// put the debug region.end for now.
|
|
|
|
|
|
|
|
EmitBlock(ReturnBlock);
|
|
|
|
}
|
|
|
|
|
2008-08-26 16:29:31 +08:00
|
|
|
void CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
|
2008-08-05 00:51:22 +08:00
|
|
|
// Finish emission of indirect switches.
|
|
|
|
EmitIndirectSwitches();
|
|
|
|
|
2008-03-31 07:03:07 +08:00
|
|
|
assert(BreakContinueStack.empty() &&
|
|
|
|
"mismatched push/pop in break/continue stack!");
|
2009-02-08 08:16:35 +08:00
|
|
|
assert(BlockScopes.empty() &&
|
|
|
|
"did not remove all blocks from block scope map!");
|
|
|
|
assert(CleanupEntries.empty() &&
|
|
|
|
"mismatched push/pop in cleanup stack!");
|
|
|
|
|
2009-01-27 07:27:52 +08:00
|
|
|
// Emit function epilog (to return).
|
|
|
|
EmitReturnBlock();
|
2008-11-12 04:59:54 +08:00
|
|
|
|
|
|
|
// Emit debug descriptor for function end.
|
|
|
|
if (CGDebugInfo *DI = CGM.getDebugInfo()) {
|
|
|
|
DI->setLocation(EndLoc);
|
|
|
|
DI->EmitRegionEnd(CurFn, Builder);
|
|
|
|
}
|
|
|
|
|
2009-02-03 06:03:45 +08:00
|
|
|
EmitFunctionEpilog(*CurFnInfo, ReturnValue);
|
2008-09-10 05:00:17 +08:00
|
|
|
|
2008-03-31 07:03:07 +08:00
|
|
|
// Remove the AllocaInsertPt instruction, which is just a convenience for us.
|
|
|
|
AllocaInsertPt->eraseFromParent();
|
|
|
|
AllocaInsertPt = 0;
|
2008-04-04 12:07:35 +08:00
|
|
|
}
|
|
|
|
|
2008-09-10 07:14:03 +08:00
|
|
|
void CodeGenFunction::StartFunction(const Decl *D, QualType RetTy,
|
|
|
|
llvm::Function *Fn,
|
2008-10-19 02:22:23 +08:00
|
|
|
const FunctionArgList &Args,
|
|
|
|
SourceLocation StartLoc) {
|
2008-09-10 07:14:03 +08:00
|
|
|
CurFuncDecl = D;
|
|
|
|
FnRetTy = RetTy;
|
2008-07-30 07:18:29 +08:00
|
|
|
CurFn = Fn;
|
2007-06-20 12:44:43 +08:00
|
|
|
assert(CurFn->isDeclaration() && "Function already has body?");
|
2008-03-03 11:28:21 +08:00
|
|
|
|
2008-11-11 10:29:29 +08:00
|
|
|
llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
|
2008-09-10 05:00:17 +08:00
|
|
|
|
2007-06-02 12:53:11 +08:00
|
|
|
// Create a marker to make it easy to insert allocas into the entryblock
|
2007-12-18 04:50:59 +08:00
|
|
|
// later. Don't create this with the builder, because we don't want it
|
|
|
|
// folded.
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
|
2007-12-18 04:50:59 +08:00
|
|
|
AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt",
|
|
|
|
EntryBB);
|
2008-09-10 05:00:17 +08:00
|
|
|
|
2008-11-11 10:29:29 +08:00
|
|
|
ReturnBlock = createBasicBlock("return");
|
2008-09-10 05:00:17 +08:00
|
|
|
ReturnValue = 0;
|
2008-09-10 07:14:03 +08:00
|
|
|
if (!RetTy->isVoidType())
|
|
|
|
ReturnValue = CreateTempAlloca(ConvertType(RetTy), "retval");
|
2008-09-10 05:00:17 +08:00
|
|
|
|
2007-12-18 04:50:59 +08:00
|
|
|
Builder.SetInsertPoint(EntryBB);
|
2008-06-18 02:05:57 +08:00
|
|
|
|
2008-07-04 19:04:26 +08:00
|
|
|
// Emit subprogram debug descriptor.
|
2008-09-10 07:14:03 +08:00
|
|
|
// FIXME: The cast here is a huge hack.
|
2008-10-19 02:22:23 +08:00
|
|
|
if (CGDebugInfo *DI = CGM.getDebugInfo()) {
|
|
|
|
DI->setLocation(StartLoc);
|
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
2008-11-24 11:54:41 +08:00
|
|
|
DI->EmitFunctionStart(FD->getIdentifier()->getName(),
|
|
|
|
RetTy, CurFn, Builder);
|
2008-10-19 02:22:23 +08:00
|
|
|
} else {
|
|
|
|
// Just use LLVM function name.
|
|
|
|
DI->EmitFunctionStart(Fn->getName().c_str(),
|
|
|
|
RetTy, CurFn, Builder);
|
2008-07-04 19:04:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-03 06:03:45 +08:00
|
|
|
// FIXME: Leaked.
|
2009-02-03 07:23:47 +08:00
|
|
|
CurFnInfo = &CGM.getTypes().getFunctionInfo(FnRetTy, Args);
|
2009-02-03 06:03:45 +08:00
|
|
|
EmitFunctionProlog(*CurFnInfo, CurFn, Args);
|
2008-12-21 05:28:43 +08:00
|
|
|
|
|
|
|
// If any of the arguments have a variably modified type, make sure to
|
|
|
|
// emit the type size.
|
|
|
|
for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
|
|
|
|
i != e; ++i) {
|
|
|
|
QualType Ty = i->second;
|
|
|
|
|
|
|
|
if (Ty->isVariablyModifiedType())
|
|
|
|
EmitVLASize(Ty);
|
|
|
|
}
|
2008-09-10 07:14:03 +08:00
|
|
|
}
|
2008-08-26 05:31:01 +08:00
|
|
|
|
2008-09-10 07:14:03 +08:00
|
|
|
void CodeGenFunction::GenerateCode(const FunctionDecl *FD,
|
|
|
|
llvm::Function *Fn) {
|
|
|
|
FunctionArgList Args;
|
2008-08-26 05:31:01 +08:00
|
|
|
if (FD->getNumParams()) {
|
|
|
|
const FunctionTypeProto* FProto = FD->getType()->getAsFunctionTypeProto();
|
|
|
|
assert(FProto && "Function def must have prototype!");
|
2008-09-10 07:14:03 +08:00
|
|
|
|
|
|
|
for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i)
|
|
|
|
Args.push_back(std::make_pair(FD->getParamDecl(i),
|
|
|
|
FProto->getArgType(i)));
|
2007-06-14 04:44:40 +08:00
|
|
|
}
|
2008-08-26 16:29:31 +08:00
|
|
|
|
2008-10-19 02:22:23 +08:00
|
|
|
StartFunction(FD, FD->getResultType(), Fn, Args,
|
|
|
|
cast<CompoundStmt>(FD->getBody())->getLBracLoc());
|
2008-09-10 07:14:03 +08:00
|
|
|
|
2008-08-26 16:29:31 +08:00
|
|
|
EmitStmt(FD->getBody());
|
|
|
|
|
|
|
|
const CompoundStmt *S = dyn_cast<CompoundStmt>(FD->getBody());
|
|
|
|
if (S) {
|
|
|
|
FinishFunction(S->getRBracLoc());
|
|
|
|
} else {
|
|
|
|
FinishFunction();
|
|
|
|
}
|
2007-05-30 07:50:05 +08:00
|
|
|
}
|
|
|
|
|
2008-11-11 15:41:27 +08:00
|
|
|
/// ContainsLabel - Return true if the statement contains a label in it. If
|
|
|
|
/// this statement is not executed normally, it not containing a label means
|
|
|
|
/// that we can just remove the code.
|
|
|
|
bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) {
|
|
|
|
// Null statement, not a label!
|
|
|
|
if (S == 0) return false;
|
|
|
|
|
|
|
|
// If this is a label, we have to emit the code, consider something like:
|
|
|
|
// if (0) { ... foo: bar(); } goto foo;
|
|
|
|
if (isa<LabelStmt>(S))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// If this is a case/default statement, and we haven't seen a switch, we have
|
|
|
|
// to emit the code.
|
|
|
|
if (isa<SwitchCase>(S) && !IgnoreCaseStmts)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// If this is a switch statement, we want to ignore cases below it.
|
|
|
|
if (isa<SwitchStmt>(S))
|
|
|
|
IgnoreCaseStmts = true;
|
|
|
|
|
|
|
|
// Scan subexpressions for verboten labels.
|
|
|
|
for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
|
|
|
|
I != E; ++I)
|
|
|
|
if (ContainsLabel(*I, IgnoreCaseStmts))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-11-12 16:04:58 +08:00
|
|
|
|
|
|
|
/// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to
|
|
|
|
/// a constant, or if it does but contains a label, return 0. If it constant
|
|
|
|
/// folds to 'true' and does not contain a label, return 1, if it constant folds
|
|
|
|
/// to 'false' and does not contain a label, return -1.
|
|
|
|
int CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) {
|
2008-11-13 06:37:10 +08:00
|
|
|
// FIXME: Rename and handle conversion of other evaluatable things
|
|
|
|
// to bool.
|
2008-12-01 10:46:24 +08:00
|
|
|
Expr::EvalResult Result;
|
|
|
|
if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() ||
|
|
|
|
Result.HasSideEffects)
|
2008-11-23 06:32:07 +08:00
|
|
|
return 0; // Not foldable, not integer or not fully evaluatable.
|
2008-11-12 16:04:58 +08:00
|
|
|
|
|
|
|
if (CodeGenFunction::ContainsLabel(Cond))
|
|
|
|
return 0; // Contains a label.
|
|
|
|
|
2008-12-01 10:46:24 +08:00
|
|
|
return Result.Val.getInt().getBoolValue() ? 1 : -1;
|
2008-11-12 16:04:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if
|
|
|
|
/// statement) to the specified blocks. Based on the condition, this might try
|
|
|
|
/// to simplify the codegen of the conditional based on the branch.
|
|
|
|
///
|
|
|
|
void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
|
|
|
|
llvm::BasicBlock *TrueBlock,
|
|
|
|
llvm::BasicBlock *FalseBlock) {
|
|
|
|
if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond))
|
|
|
|
return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock);
|
|
|
|
|
|
|
|
if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) {
|
|
|
|
// Handle X && Y in a condition.
|
|
|
|
if (CondBOp->getOpcode() == BinaryOperator::LAnd) {
|
|
|
|
// If we have "1 && X", simplify the code. "0 && X" would have constant
|
|
|
|
// folded if the case was simple enough.
|
|
|
|
if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) {
|
|
|
|
// br(1 && X) -> br(X).
|
|
|
|
return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have "X && 1", simplify the code to use an uncond branch.
|
|
|
|
// "X && 0" would have been constant folded to 0.
|
|
|
|
if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) {
|
|
|
|
// br(X && 1) -> br(X).
|
|
|
|
return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the LHS as a conditional. If the LHS conditional is false, we
|
|
|
|
// want to jump to the FalseBlock.
|
2008-11-13 09:38:36 +08:00
|
|
|
llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true");
|
2008-11-12 16:04:58 +08:00
|
|
|
EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock);
|
|
|
|
EmitBlock(LHSTrue);
|
|
|
|
|
|
|
|
EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
|
|
|
|
return;
|
|
|
|
} else if (CondBOp->getOpcode() == BinaryOperator::LOr) {
|
|
|
|
// If we have "0 || X", simplify the code. "1 || X" would have constant
|
|
|
|
// folded if the case was simple enough.
|
|
|
|
if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) {
|
|
|
|
// br(0 || X) -> br(X).
|
|
|
|
return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have "X || 0", simplify the code to use an uncond branch.
|
|
|
|
// "X || 1" would have been constant folded to 1.
|
|
|
|
if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) {
|
|
|
|
// br(X || 0) -> br(X).
|
|
|
|
return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the LHS as a conditional. If the LHS conditional is true, we
|
|
|
|
// want to jump to the TrueBlock.
|
2008-11-13 09:38:36 +08:00
|
|
|
llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false");
|
2008-11-12 16:04:58 +08:00
|
|
|
EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse);
|
|
|
|
EmitBlock(LHSFalse);
|
|
|
|
|
|
|
|
EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
|
|
|
|
return;
|
|
|
|
}
|
2008-11-12 16:13:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) {
|
|
|
|
// br(!x, t, f) -> br(x, f, t)
|
|
|
|
if (CondUOp->getOpcode() == UnaryOperator::LNot)
|
|
|
|
return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock);
|
2008-11-12 16:04:58 +08:00
|
|
|
}
|
|
|
|
|
2008-11-12 18:30:32 +08:00
|
|
|
if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) {
|
|
|
|
// Handle ?: operator.
|
|
|
|
|
|
|
|
// Just ignore GNU ?: extension.
|
|
|
|
if (CondOp->getLHS()) {
|
|
|
|
// br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f))
|
|
|
|
llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
|
|
|
|
llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
|
|
|
|
EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock);
|
|
|
|
EmitBlock(LHSBlock);
|
|
|
|
EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock);
|
|
|
|
EmitBlock(RHSBlock);
|
|
|
|
EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-12 16:04:58 +08:00
|
|
|
// Emit the code with the fully general case.
|
|
|
|
llvm::Value *CondV = EvaluateExprAsBool(Cond);
|
|
|
|
Builder.CreateCondBr(CondV, TrueBlock, FalseBlock);
|
|
|
|
}
|
|
|
|
|
2007-11-02 03:11:01 +08:00
|
|
|
/// getCGRecordLayout - Return record layout info.
|
|
|
|
const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT,
|
2008-02-05 14:55:31 +08:00
|
|
|
QualType Ty) {
|
|
|
|
const RecordType *RTy = Ty->getAsRecordType();
|
|
|
|
assert (RTy && "Unexpected type. RecordType expected here.");
|
2007-10-23 10:10:49 +08:00
|
|
|
|
2008-02-05 14:55:31 +08:00
|
|
|
return CGT.getCGRecordLayout(RTy->getDecl());
|
2007-10-23 10:10:49 +08:00
|
|
|
}
|
2007-12-02 09:43:38 +08:00
|
|
|
|
2008-08-16 08:56:44 +08:00
|
|
|
/// ErrorUnsupported - Print out an error that codegen doesn't support the
|
2007-12-02 09:43:38 +08:00
|
|
|
/// specified stmt yet.
|
2008-09-04 11:43:08 +08:00
|
|
|
void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type,
|
|
|
|
bool OmitOnError) {
|
|
|
|
CGM.ErrorUnsupported(S, Type, OmitOnError);
|
2007-12-02 09:43:38 +08:00
|
|
|
}
|
|
|
|
|
2008-08-05 00:51:22 +08:00
|
|
|
unsigned CodeGenFunction::GetIDForAddrOfLabel(const LabelStmt *L) {
|
|
|
|
// Use LabelIDs.size() as the new ID if one hasn't been assigned.
|
|
|
|
return LabelIDs.insert(std::make_pair(L, LabelIDs.size())).first->second;
|
|
|
|
}
|
|
|
|
|
2008-08-31 03:51:14 +08:00
|
|
|
void CodeGenFunction::EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty)
|
|
|
|
{
|
|
|
|
const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
|
|
|
|
if (DestPtr->getType() != BP)
|
|
|
|
DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
|
|
|
|
|
|
|
|
// Get size and alignment info for this aggregate.
|
|
|
|
std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
|
|
|
|
|
|
|
|
// FIXME: Handle variable sized types.
|
|
|
|
const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth);
|
|
|
|
|
|
|
|
Builder.CreateCall4(CGM.getMemSetFn(), DestPtr,
|
|
|
|
llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty),
|
|
|
|
// TypeInfo.first describes size in bits.
|
|
|
|
llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
|
|
|
|
llvm::ConstantInt::get(llvm::Type::Int32Ty,
|
|
|
|
TypeInfo.second/8));
|
|
|
|
}
|
|
|
|
|
2008-08-05 00:51:22 +08:00
|
|
|
void CodeGenFunction::EmitIndirectSwitches() {
|
|
|
|
llvm::BasicBlock *Default;
|
|
|
|
|
2008-08-05 01:24:44 +08:00
|
|
|
if (IndirectSwitches.empty())
|
|
|
|
return;
|
|
|
|
|
2008-08-05 00:51:22 +08:00
|
|
|
if (!LabelIDs.empty()) {
|
|
|
|
Default = getBasicBlockForLabel(LabelIDs.begin()->first);
|
|
|
|
} else {
|
|
|
|
// No possible targets for indirect goto, just emit an infinite
|
|
|
|
// loop.
|
2008-11-11 10:29:29 +08:00
|
|
|
Default = createBasicBlock("indirectgoto.loop", CurFn);
|
2008-08-05 00:51:22 +08:00
|
|
|
llvm::BranchInst::Create(Default, Default);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::vector<llvm::SwitchInst*>::iterator i = IndirectSwitches.begin(),
|
|
|
|
e = IndirectSwitches.end(); i != e; ++i) {
|
|
|
|
llvm::SwitchInst *I = *i;
|
|
|
|
|
|
|
|
I->setSuccessor(0, Default);
|
|
|
|
for (std::map<const LabelStmt*,unsigned>::iterator LI = LabelIDs.begin(),
|
|
|
|
LE = LabelIDs.end(); LI != LE; ++LI) {
|
|
|
|
I->addCase(llvm::ConstantInt::get(llvm::Type::Int32Ty,
|
|
|
|
LI->second),
|
|
|
|
getBasicBlockForLabel(LI->first));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-11-04 13:30:00 +08:00
|
|
|
|
|
|
|
llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty)
|
|
|
|
{
|
|
|
|
// FIXME: This entire method is hardcoded for 32-bit X86.
|
|
|
|
|
|
|
|
const char *TargetPrefix = getContext().Target.getTargetPrefix();
|
|
|
|
|
|
|
|
if (strcmp(TargetPrefix, "x86") != 0 ||
|
|
|
|
getContext().Target.getPointerWidth(0) != 32)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
|
|
|
|
const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
|
|
|
|
|
|
|
|
llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
|
|
|
|
"ap");
|
|
|
|
llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
|
|
|
|
llvm::Value *AddrTyped =
|
|
|
|
Builder.CreateBitCast(Addr,
|
|
|
|
llvm::PointerType::getUnqual(ConvertType(Ty)));
|
|
|
|
|
|
|
|
uint64_t SizeInBytes = getContext().getTypeSize(Ty) / 8;
|
|
|
|
const unsigned ArgumentSizeInBytes = 4;
|
|
|
|
if (SizeInBytes < ArgumentSizeInBytes)
|
|
|
|
SizeInBytes = ArgumentSizeInBytes;
|
|
|
|
|
|
|
|
llvm::Value *NextAddr =
|
|
|
|
Builder.CreateGEP(Addr,
|
|
|
|
llvm::ConstantInt::get(llvm::Type::Int32Ty, SizeInBytes),
|
|
|
|
"ap.next");
|
|
|
|
Builder.CreateStore(NextAddr, VAListAddrAsBPP);
|
|
|
|
|
|
|
|
return AddrTyped;
|
|
|
|
}
|
|
|
|
|
2008-12-21 04:27:15 +08:00
|
|
|
|
2008-12-12 15:19:02 +08:00
|
|
|
llvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT)
|
|
|
|
{
|
|
|
|
llvm::Value *&SizeEntry = VLASizeMap[VAT];
|
|
|
|
|
2008-12-21 04:27:15 +08:00
|
|
|
assert(SizeEntry && "Did not emit size for type");
|
|
|
|
return SizeEntry;
|
|
|
|
}
|
2008-12-12 15:19:02 +08:00
|
|
|
|
2008-12-21 04:46:34 +08:00
|
|
|
llvm::Value *CodeGenFunction::EmitVLASize(QualType Ty)
|
2008-12-21 04:27:15 +08:00
|
|
|
{
|
2008-12-21 04:46:34 +08:00
|
|
|
assert(Ty->isVariablyModifiedType() &&
|
|
|
|
"Must pass variably modified type to EmitVLASizes!");
|
2008-12-21 04:27:15 +08:00
|
|
|
|
2008-12-21 04:46:34 +08:00
|
|
|
if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) {
|
|
|
|
llvm::Value *&SizeEntry = VLASizeMap[VAT];
|
|
|
|
|
2008-12-21 05:51:53 +08:00
|
|
|
if (!SizeEntry) {
|
|
|
|
// Get the element size;
|
|
|
|
llvm::Value *ElemSize;
|
2008-12-21 04:46:34 +08:00
|
|
|
|
2008-12-21 05:51:53 +08:00
|
|
|
QualType ElemTy = VAT->getElementType();
|
2009-02-06 03:43:10 +08:00
|
|
|
|
|
|
|
const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
|
|
|
|
|
2008-12-21 05:51:53 +08:00
|
|
|
if (ElemTy->isVariableArrayType())
|
|
|
|
ElemSize = EmitVLASize(ElemTy);
|
|
|
|
else {
|
2009-02-06 03:43:10 +08:00
|
|
|
ElemSize = llvm::ConstantInt::get(SizeTy,
|
2008-12-21 05:51:53 +08:00
|
|
|
getContext().getTypeSize(ElemTy) / 8);
|
|
|
|
}
|
2008-12-21 04:46:34 +08:00
|
|
|
|
2008-12-21 05:51:53 +08:00
|
|
|
llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr());
|
2009-02-06 03:43:10 +08:00
|
|
|
NumElements = Builder.CreateIntCast(NumElements, SizeTy, false, "tmp");
|
|
|
|
|
2008-12-21 05:51:53 +08:00
|
|
|
SizeEntry = Builder.CreateMul(ElemSize, NumElements);
|
|
|
|
}
|
2008-12-21 04:46:34 +08:00
|
|
|
|
|
|
|
return SizeEntry;
|
|
|
|
} else if (const PointerType *PT = Ty->getAsPointerType())
|
|
|
|
EmitVLASize(PT->getPointeeType());
|
2008-12-21 04:27:15 +08:00
|
|
|
else {
|
2008-12-21 04:46:34 +08:00
|
|
|
assert(0 && "unknown VM type!");
|
2008-12-12 15:19:02 +08:00
|
|
|
}
|
2008-12-21 04:46:34 +08:00
|
|
|
|
|
|
|
return 0;
|
2008-12-12 15:19:02 +08:00
|
|
|
}
|
2009-01-21 01:46:04 +08:00
|
|
|
|
|
|
|
llvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) {
|
|
|
|
if (CGM.getContext().getBuiltinVaListType()->isArrayType()) {
|
|
|
|
return EmitScalarExpr(E);
|
|
|
|
}
|
|
|
|
return EmitLValue(E).getAddress();
|
|
|
|
}
|
2009-02-08 06:53:43 +08:00
|
|
|
|
|
|
|
llvm::BasicBlock *CodeGenFunction::CreateCleanupBlock()
|
|
|
|
{
|
|
|
|
llvm::BasicBlock *CleanupBlock = createBasicBlock("cleanup");
|
|
|
|
|
|
|
|
CleanupEntries.push_back(CleanupEntry(CleanupBlock));
|
|
|
|
|
|
|
|
return CleanupBlock;
|
|
|
|
}
|
2009-02-08 07:50:39 +08:00
|
|
|
|
|
|
|
void CodeGenFunction::EmitCleanupBlocks(size_t OldCleanupStackSize)
|
|
|
|
{
|
|
|
|
assert(CleanupEntries.size() >= OldCleanupStackSize &&
|
|
|
|
"Cleanup stack mismatch!");
|
|
|
|
|
|
|
|
while (CleanupEntries.size() > OldCleanupStackSize)
|
|
|
|
EmitCleanupBlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::EmitCleanupBlock()
|
|
|
|
{
|
|
|
|
CleanupEntry &CE = CleanupEntries.back();
|
|
|
|
|
|
|
|
llvm::BasicBlock *CleanupBlock = CE.CleanupBlock;
|
|
|
|
|
2009-02-08 08:16:35 +08:00
|
|
|
std::vector<llvm::BasicBlock *> Blocks;
|
|
|
|
std::swap(Blocks, CE.Blocks);
|
|
|
|
|
|
|
|
std::vector<llvm::BranchInst *> BranchFixups;
|
|
|
|
std::swap(BranchFixups, CE.BranchFixups);
|
|
|
|
|
2009-02-08 07:50:39 +08:00
|
|
|
CleanupEntries.pop_back();
|
|
|
|
|
|
|
|
EmitBlock(CleanupBlock);
|
2009-02-08 08:16:35 +08:00
|
|
|
|
|
|
|
// Remove all blocks from the block scope map.
|
|
|
|
for (size_t i = 0, e = Blocks.size(); i != e; ++i) {
|
|
|
|
assert(BlockScopes.count(Blocks[i]) &&
|
|
|
|
"Did not find block in scope map!");
|
|
|
|
|
|
|
|
BlockScopes.erase(Blocks[i]);
|
|
|
|
}
|
2009-02-08 07:50:39 +08:00
|
|
|
}
|
|
|
|
|
2009-02-08 08:50:42 +08:00
|
|
|
void CodeGenFunction::AddBranchFixup(llvm::BranchInst *BI)
|
|
|
|
{
|
|
|
|
assert(!CleanupEntries.empty() &&
|
|
|
|
"Trying to add branch fixup without cleanup block!");
|
|
|
|
|
|
|
|
// FIXME: We could be more clever here and check if there's already a
|
|
|
|
// branch fixup for this destination and recycle it.
|
|
|
|
CleanupEntries.back().BranchFixups.push_back(BI);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::EmitBranchThroughCleanup(llvm::BasicBlock *Dest)
|
|
|
|
{
|
|
|
|
llvm::BranchInst* BI = Builder.CreateBr(Dest);
|
|
|
|
|
|
|
|
// The stack is empty, no need to do any cleanup.
|
|
|
|
if (CleanupEntries.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!Dest->getParent()) {
|
|
|
|
// We are trying to branch to a block that hasn't been inserted yet.
|
|
|
|
AddBranchFixup(BI);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockScopeMap::iterator I = BlockScopes.find(Dest);
|
|
|
|
if (I == BlockScopes.end()) {
|
|
|
|
// We are trying to jump to a block that is outside of any cleanup scope.
|
|
|
|
AddBranchFixup(BI);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(I->second < CleanupEntries.size() &&
|
|
|
|
"Trying to branch into cleanup region");
|
|
|
|
|
|
|
|
if (I->second == CleanupEntries.size() - 1) {
|
|
|
|
// We have a branch to a block in the same scope.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AddBranchFixup(BI);
|
|
|
|
}
|