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-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-05-31 06:55:31 +08:00
|
|
|
#include "llvm/Analysis/Verifier.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),
|
2008-06-18 02:05:57 +08:00
|
|
|
CaseRangeBlock(NULL) {
|
|
|
|
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-04-07 04:42:52 +08:00
|
|
|
return BB = llvm::BasicBlock::Create(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
|
|
|
|
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) {
|
|
|
|
return !isObjCPointerType(T) &&!T->isRealType() && !T->isPointerLikeType() &&
|
|
|
|
!T->isVoidType() && !T->isVectorType() && !T->isFunctionType();
|
|
|
|
}
|
2008-03-31 07:03:07 +08:00
|
|
|
|
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-07-04 19:04:26 +08:00
|
|
|
// Emit debug descriptor for function end.
|
|
|
|
CGDebugInfo *DI = CGM.getDebugInfo();
|
|
|
|
if (DI) {
|
2008-08-26 16:29:31 +08:00
|
|
|
if (EndLoc.isValid()) {
|
|
|
|
DI->setLocation(EndLoc);
|
2008-07-04 19:04:26 +08:00
|
|
|
}
|
|
|
|
DI->EmitRegionEnd(CurFn, Builder);
|
|
|
|
}
|
|
|
|
|
2008-03-31 07:03:07 +08: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();
|
|
|
|
if (isDummyBlock(BB))
|
|
|
|
BB->eraseFromParent();
|
|
|
|
else {
|
2008-06-18 02:05:57 +08:00
|
|
|
// FIXME: if this is C++ main, this should return 0.
|
2008-03-31 07:03:07 +08:00
|
|
|
if (CurFn->getReturnType() == llvm::Type::VoidTy)
|
|
|
|
Builder.CreateRetVoid();
|
|
|
|
else
|
|
|
|
Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType()));
|
|
|
|
}
|
|
|
|
assert(BreakContinueStack.empty() &&
|
|
|
|
"mismatched push/pop in break/continue stack!");
|
|
|
|
|
|
|
|
// Remove the AllocaInsertPt instruction, which is just a convenience for us.
|
|
|
|
AllocaInsertPt->eraseFromParent();
|
|
|
|
AllocaInsertPt = 0;
|
2008-06-18 02:05:57 +08:00
|
|
|
|
2008-03-31 07:03:07 +08:00
|
|
|
// Verify that the function is well formed.
|
2008-06-18 02:05:57 +08:00
|
|
|
assert(!verifyFunction(*CurFn) && "Generated function is not well formed.");
|
2008-04-04 12:07:35 +08:00
|
|
|
}
|
|
|
|
|
2008-07-30 07:18:29 +08:00
|
|
|
void CodeGenFunction::GenerateCode(const FunctionDecl *FD,
|
|
|
|
llvm::Function *Fn) {
|
2007-06-02 11:19:07 +08:00
|
|
|
CurFuncDecl = FD;
|
2008-06-18 02:05:57 +08:00
|
|
|
FnRetTy = FD->getResultType();
|
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-04-07 04:42:52 +08:00
|
|
|
llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn);
|
2007-05-30 07:50:05 +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);
|
|
|
|
|
|
|
|
Builder.SetInsertPoint(EntryBB);
|
2008-06-18 02:05:57 +08:00
|
|
|
|
2008-07-04 19:04:26 +08: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-23 06:02:34 +08:00
|
|
|
// Emit allocs for param decls. Give the LLVM Argument nodes names.
|
2007-06-14 04:44:40 +08:00
|
|
|
llvm::Function::arg_iterator AI = CurFn->arg_begin();
|
2007-06-23 06:02:34 +08:00
|
|
|
|
|
|
|
// Name the struct return argument.
|
|
|
|
if (hasAggregateLLVMType(FD->getResultType())) {
|
|
|
|
AI->setName("agg.result");
|
|
|
|
++AI;
|
|
|
|
}
|
2008-08-26 05:31:01 +08: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-14 04:44:40 +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
|
|
|
}
|
|
|
|
|
2007-09-29 05:49:18 +08:00
|
|
|
/// isDummyBlock - Return true if BB is an empty basic block
|
|
|
|
/// with no predecessors.
|
|
|
|
bool CodeGenFunction::isDummyBlock(const llvm::BasicBlock *BB) {
|
2008-07-26 07:40:10 +08:00
|
|
|
if (BB->empty() && pred_begin(BB) == pred_end(BB) && !BB->hasName())
|
2007-09-29 05:49:18 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-10-05 07:45:31 +08: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-07 04:42:52 +08:00
|
|
|
EmitBlock(llvm::BasicBlock::Create(N));
|
2007-10-05 07:45:31 +08:00
|
|
|
else
|
|
|
|
BB->setName(N);
|
|
|
|
}
|
|
|
|
|
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-08-16 08:56:44 +08:00
|
|
|
void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type) {
|
|
|
|
CGM.ErrorUnsupported(S, Type);
|
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.
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|