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"
|
|
|
|
#include "clang/AST/AST.h"
|
2008-03-08 04:04:22 +08:00
|
|
|
#include "llvm/CallingConv.h"
|
2007-05-31 01:57:17 +08:00
|
|
|
#include "llvm/Constants.h"
|
2007-05-30 07:17:50 +08:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2007-05-30 07:50:05 +08:00
|
|
|
#include "llvm/Function.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-06-18 02:05:57 +08:00
|
|
|
void CodeGenFunction::GenerateFunction(const Stmt *Body) {
|
2008-03-31 07:03:07 +08:00
|
|
|
// Emit the function body.
|
2008-06-18 02:05:57 +08:00
|
|
|
EmitStmt(Body);
|
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
|
|
|
}
|
|
|
|
|
2007-05-30 07:50:05 +08:00
|
|
|
void CodeGenFunction::GenerateCode(const FunctionDecl *FD) {
|
2007-06-02 11:19:07 +08:00
|
|
|
CurFuncDecl = FD;
|
2008-06-18 02:05:57 +08:00
|
|
|
FnRetTy = FD->getResultType();
|
2007-12-02 15:09:19 +08:00
|
|
|
CurFn = cast<llvm::Function>(CGM.GetAddrOfFunctionDecl(FD, true));
|
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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-06-14 04:44:40 +08:00
|
|
|
for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i, ++AI) {
|
|
|
|
assert(AI != CurFn->arg_end() && "Argument mismatch!");
|
|
|
|
EmitParmDecl(*FD->getParamDecl(i), AI);
|
|
|
|
}
|
2008-06-18 02:05:57 +08:00
|
|
|
GenerateFunction(FD->getBody());
|
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) {
|
|
|
|
if (BB->empty() && pred_begin(BB) == pred_end(BB))
|
|
|
|
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
|
|
|
|
|
|
|
/// WarnUnsupported - Print out a warning that codegen doesn't support the
|
|
|
|
/// specified stmt yet.
|
2007-12-02 09:49:16 +08:00
|
|
|
void CodeGenFunction::WarnUnsupported(const Stmt *S, const char *Type) {
|
2007-12-02 15:19:18 +08:00
|
|
|
CGM.WarnUnsupported(S, Type);
|
2007-12-02 09:43:38 +08:00
|
|
|
}
|
|
|
|
|