2007-05-28 01:07:47 +00:00
|
|
|
//===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:25 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-05-28 01:07:47 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This coordinates the per-function state used while generating code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenFunction.h"
|
|
|
|
#include "CodeGenModule.h"
|
2008-05-22 01:40:10 +00:00
|
|
|
#include "CGDebugInfo.h"
|
2007-05-29 23:17:50 +00:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2008-08-11 05:00:27 +00:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2008-08-11 05:35:13 +00:00
|
|
|
#include "clang/AST/Decl.h"
|
2007-05-30 22:55:31 +00:00
|
|
|
#include "llvm/Analysis/Verifier.h"
|
2007-09-28 21:49:18 +00:00
|
|
|
#include "llvm/Support/CFG.h"
|
2007-05-28 01:07:47 +00:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2007-05-29 23:17:50 +00:00
|
|
|
CodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
|
2007-10-08 20:57:48 +00:00
|
|
|
: CGM(cgm), Target(CGM.getContext().Target), SwitchInsn(NULL),
|
2008-06-17 18:05:57 +00:00
|
|
|
CaseRangeBlock(NULL) {
|
|
|
|
LLVMIntTy = ConvertType(getContext().IntTy);
|
|
|
|
LLVMPointerWidth = Target.getPointerWidth(0);
|
|
|
|
}
|
2007-05-29 23:17:50 +00:00
|
|
|
|
2007-06-02 22:49:07 +00:00
|
|
|
ASTContext &CodeGenFunction::getContext() const {
|
|
|
|
return CGM.getContext();
|
|
|
|
}
|
|
|
|
|
2007-05-29 23:17:50 +00:00
|
|
|
|
2007-05-30 00:13:02 +00:00
|
|
|
llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) {
|
2007-06-15 23:05:46 +00:00
|
|
|
llvm::BasicBlock *&BB = LabelMap[S];
|
2007-05-30 00:13:02 +00:00
|
|
|
if (BB) return BB;
|
|
|
|
|
|
|
|
// Create, but don't insert, the new block.
|
2008-04-06 20:42:52 +00:00
|
|
|
return BB = llvm::BasicBlock::Create(S->getName());
|
2007-05-30 00:13:02 +00:00
|
|
|
}
|
|
|
|
|
2008-02-26 21:41:45 +00:00
|
|
|
llvm::Constant *
|
2008-04-15 22:42:06 +00:00
|
|
|
CodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) {
|
2008-02-26 21:41:45 +00:00
|
|
|
return cast<llvm::Constant>(LocalDeclMap[BVD]);
|
|
|
|
}
|
2007-05-30 00:13:02 +00:00
|
|
|
|
2007-06-22 19:05:19 +00:00
|
|
|
const llvm::Type *CodeGenFunction::ConvertType(QualType T) {
|
|
|
|
return CGM.getTypes().ConvertType(T);
|
2007-06-09 02:28:57 +00:00
|
|
|
}
|
2007-05-29 23:17:50 +00:00
|
|
|
|
2008-06-17 18:05:57 +00:00
|
|
|
bool CodeGenFunction::isObjCPointerType(QualType T) {
|
|
|
|
// All Objective-C types are pointers.
|
|
|
|
return T->isObjCInterfaceType() ||
|
|
|
|
T->isObjCQualifiedInterfaceType() || T->isObjCQualifiedIdType();
|
2007-06-22 22:02:34 +00:00
|
|
|
}
|
|
|
|
|
2008-06-17 18:05:57 +00:00
|
|
|
bool CodeGenFunction::hasAggregateLLVMType(QualType T) {
|
|
|
|
return !isObjCPointerType(T) &&!T->isRealType() && !T->isPointerLikeType() &&
|
|
|
|
!T->isVoidType() && !T->isVectorType() && !T->isFunctionType();
|
|
|
|
}
|
2008-03-30 23:03:07 +00:00
|
|
|
|
2008-08-26 08:29:31 +00:00
|
|
|
void CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
|
2008-08-04 16:51:22 +00:00
|
|
|
// Finish emission of indirect switches.
|
|
|
|
EmitIndirectSwitches();
|
|
|
|
|
2008-07-04 11:04:26 +00:00
|
|
|
// Emit debug descriptor for function end.
|
2008-09-09 21:00:17 +00:00
|
|
|
if (CGDebugInfo *DI = CGM.getDebugInfo()) {
|
2008-08-26 08:29:31 +00:00
|
|
|
if (EndLoc.isValid()) {
|
|
|
|
DI->setLocation(EndLoc);
|
2008-07-04 11:04:26 +00:00
|
|
|
}
|
|
|
|
DI->EmitRegionEnd(CurFn, Builder);
|
|
|
|
}
|
|
|
|
|
2008-03-30 23:03:07 +00:00
|
|
|
// Emit a return for code that falls off the end. If insert point
|
|
|
|
// is a dummy block with no predecessors then remove the block itself.
|
|
|
|
llvm::BasicBlock *BB = Builder.GetInsertBlock();
|
2008-09-09 21:00:17 +00:00
|
|
|
if (isDummyBlock(BB)) {
|
2008-03-30 23:03:07 +00:00
|
|
|
BB->eraseFromParent();
|
2008-09-09 21:00:17 +00:00
|
|
|
} else {
|
|
|
|
// Just transfer to return
|
|
|
|
Builder.CreateBr(ReturnBlock);
|
2008-03-30 23:03:07 +00:00
|
|
|
}
|
|
|
|
assert(BreakContinueStack.empty() &&
|
|
|
|
"mismatched push/pop in break/continue stack!");
|
2008-09-09 21:00:17 +00:00
|
|
|
|
|
|
|
// Emit code to actually return.
|
|
|
|
Builder.SetInsertPoint(ReturnBlock);
|
|
|
|
if (!ReturnValue) {
|
|
|
|
Builder.CreateRetVoid();
|
|
|
|
} else {
|
|
|
|
if (!hasAggregateLLVMType(FnRetTy)) {
|
|
|
|
Builder.CreateRet(Builder.CreateLoad(ReturnValue));
|
|
|
|
} else if (FnRetTy->isAnyComplexType()) {
|
|
|
|
EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, FnRetTy);
|
|
|
|
Builder.CreateRetVoid();
|
|
|
|
} else {
|
|
|
|
EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, FnRetTy);
|
|
|
|
Builder.CreateRetVoid();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-30 23:03:07 +00:00
|
|
|
// Remove the AllocaInsertPt instruction, which is just a convenience for us.
|
|
|
|
AllocaInsertPt->eraseFromParent();
|
|
|
|
AllocaInsertPt = 0;
|
2008-06-17 18:05:57 +00:00
|
|
|
|
2008-03-30 23:03:07 +00:00
|
|
|
// Verify that the function is well formed.
|
2008-06-17 18:05:57 +00:00
|
|
|
assert(!verifyFunction(*CurFn) && "Generated function is not well formed.");
|
2008-04-04 04:07:35 +00:00
|
|
|
}
|
|
|
|
|
2008-09-09 21:00:17 +00:00
|
|
|
// FIXME: There is parallel code in StartObjCMethod.
|
2008-07-29 23:18:29 +00:00
|
|
|
void CodeGenFunction::GenerateCode(const FunctionDecl *FD,
|
|
|
|
llvm::Function *Fn) {
|
2007-06-02 03:19:07 +00:00
|
|
|
CurFuncDecl = FD;
|
2008-06-17 18:05:57 +00:00
|
|
|
FnRetTy = FD->getResultType();
|
2008-07-29 23:18:29 +00:00
|
|
|
CurFn = Fn;
|
2007-06-20 04:44:43 +00:00
|
|
|
assert(CurFn->isDeclaration() && "Function already has body?");
|
2008-03-03 03:28:21 +00:00
|
|
|
|
2008-04-06 20:42:52 +00:00
|
|
|
llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn);
|
2008-09-09 21:00:17 +00:00
|
|
|
|
2007-06-02 04:53:11 +00:00
|
|
|
// Create a marker to make it easy to insert allocas into the entryblock
|
2007-12-17 20:50:59 +00:00
|
|
|
// later. Don't create this with the builder, because we don't want it
|
|
|
|
// folded.
|
2007-06-15 23:05:46 +00:00
|
|
|
llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
|
2007-12-17 20:50:59 +00:00
|
|
|
AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt",
|
|
|
|
EntryBB);
|
2008-09-09 21:00:17 +00:00
|
|
|
|
|
|
|
ReturnBlock = llvm::BasicBlock::Create("return", CurFn);
|
|
|
|
ReturnValue = 0;
|
|
|
|
if (!FnRetTy->isVoidType())
|
|
|
|
ReturnValue = CreateTempAlloca(ConvertType(FnRetTy), "retval");
|
|
|
|
|
2007-12-17 20:50:59 +00:00
|
|
|
Builder.SetInsertPoint(EntryBB);
|
2008-06-17 18:05:57 +00:00
|
|
|
|
2008-07-04 11:04:26 +00:00
|
|
|
// Emit subprogram debug descriptor.
|
|
|
|
CGDebugInfo *DI = CGM.getDebugInfo();
|
|
|
|
if (DI) {
|
|
|
|
CompoundStmt* body = dyn_cast<CompoundStmt>(FD->getBody());
|
|
|
|
if (body && body->getLBracLoc().isValid()) {
|
|
|
|
DI->setLocation(body->getLBracLoc());
|
|
|
|
}
|
|
|
|
DI->EmitFunctionStart(FD, CurFn, Builder);
|
|
|
|
}
|
|
|
|
|
2007-06-22 22:02:34 +00:00
|
|
|
// Emit allocs for param decls. Give the LLVM Argument nodes names.
|
2007-06-13 20:44:40 +00:00
|
|
|
llvm::Function::arg_iterator AI = CurFn->arg_begin();
|
2007-06-22 22:02:34 +00:00
|
|
|
|
|
|
|
// Name the struct return argument.
|
|
|
|
if (hasAggregateLLVMType(FD->getResultType())) {
|
|
|
|
AI->setName("agg.result");
|
|
|
|
++AI;
|
|
|
|
}
|
2008-08-25 21:31:01 +00:00
|
|
|
|
|
|
|
if (FD->getNumParams()) {
|
|
|
|
const FunctionTypeProto* FProto = FD->getType()->getAsFunctionTypeProto();
|
|
|
|
assert(FProto && "Function def must have prototype!");
|
|
|
|
for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i, ++AI) {
|
|
|
|
assert(AI != CurFn->arg_end() && "Argument mismatch!");
|
|
|
|
const ParmVarDecl* CurParam = FD->getParamDecl(i);
|
|
|
|
llvm::Value* V = AI;
|
|
|
|
if (!getContext().typesAreCompatible(FProto->getArgType(i),
|
|
|
|
CurParam->getType())) {
|
|
|
|
// This must be a promotion, for something like
|
|
|
|
// "void a(x) short x; {..."
|
|
|
|
V = EmitScalarConversion(V, FProto->getArgType(i),
|
|
|
|
CurParam->getType());
|
|
|
|
}
|
|
|
|
EmitParmDecl(*CurParam, V);
|
|
|
|
}
|
2007-06-13 20:44:40 +00:00
|
|
|
}
|
2008-08-26 08:29:31 +00:00
|
|
|
|
|
|
|
EmitStmt(FD->getBody());
|
|
|
|
|
|
|
|
const CompoundStmt *S = dyn_cast<CompoundStmt>(FD->getBody());
|
|
|
|
if (S) {
|
|
|
|
FinishFunction(S->getRBracLoc());
|
|
|
|
} else {
|
|
|
|
FinishFunction();
|
|
|
|
}
|
2007-05-29 23:50:05 +00:00
|
|
|
}
|
|
|
|
|
2007-09-28 21:49:18 +00:00
|
|
|
/// isDummyBlock - Return true if BB is an empty basic block
|
|
|
|
/// with no predecessors.
|
|
|
|
bool CodeGenFunction::isDummyBlock(const llvm::BasicBlock *BB) {
|
2008-07-25 23:40:10 +00:00
|
|
|
if (BB->empty() && pred_begin(BB) == pred_end(BB) && !BB->hasName())
|
2007-09-28 21:49:18 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-10-04 23:45:31 +00:00
|
|
|
/// StartBlock - Start new block named N. If insert block is a dummy block
|
|
|
|
/// then reuse it.
|
|
|
|
void CodeGenFunction::StartBlock(const char *N) {
|
|
|
|
llvm::BasicBlock *BB = Builder.GetInsertBlock();
|
|
|
|
if (!isDummyBlock(BB))
|
2008-04-06 20:42:52 +00:00
|
|
|
EmitBlock(llvm::BasicBlock::Create(N));
|
2007-10-04 23:45:31 +00:00
|
|
|
else
|
|
|
|
BB->setName(N);
|
|
|
|
}
|
|
|
|
|
2007-11-01 19:11:01 +00:00
|
|
|
/// getCGRecordLayout - Return record layout info.
|
|
|
|
const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT,
|
2008-02-05 06:55:31 +00:00
|
|
|
QualType Ty) {
|
|
|
|
const RecordType *RTy = Ty->getAsRecordType();
|
|
|
|
assert (RTy && "Unexpected type. RecordType expected here.");
|
2007-10-23 02:10:49 +00:00
|
|
|
|
2008-02-05 06:55:31 +00:00
|
|
|
return CGT.getCGRecordLayout(RTy->getDecl());
|
2007-10-23 02:10:49 +00:00
|
|
|
}
|
2007-12-02 01:43:38 +00:00
|
|
|
|
2008-08-16 00:56:44 +00:00
|
|
|
/// ErrorUnsupported - Print out an error that codegen doesn't support the
|
2007-12-02 01:43:38 +00:00
|
|
|
/// specified stmt yet.
|
2008-09-04 03:43:08 +00:00
|
|
|
void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type,
|
|
|
|
bool OmitOnError) {
|
|
|
|
CGM.ErrorUnsupported(S, Type, OmitOnError);
|
2007-12-02 01:43:38 +00:00
|
|
|
}
|
|
|
|
|
2008-08-04 16:51:22 +00: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-30 19:51:14 +00: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-04 16:51:22 +00:00
|
|
|
void CodeGenFunction::EmitIndirectSwitches() {
|
|
|
|
llvm::BasicBlock *Default;
|
|
|
|
|
2008-08-04 17:24:44 +00:00
|
|
|
if (IndirectSwitches.empty())
|
|
|
|
return;
|
|
|
|
|
2008-08-04 16:51:22 +00:00
|
|
|
if (!LabelIDs.empty()) {
|
|
|
|
Default = getBasicBlockForLabel(LabelIDs.begin()->first);
|
|
|
|
} else {
|
|
|
|
// No possible targets for indirect goto, just emit an infinite
|
|
|
|
// loop.
|
|
|
|
Default = llvm::BasicBlock::Create("indirectgoto.loop", CurFn);
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|