diff options
Diffstat (limited to 'clang/CodeGen')
-rw-r--r-- | clang/CodeGen/CGExpr.cpp | 3 | ||||
-rw-r--r-- | clang/CodeGen/CodeGenFunction.cpp | 15 | ||||
-rw-r--r-- | clang/CodeGen/CodeGenModule.cpp | 24 | ||||
-rw-r--r-- | clang/CodeGen/CodeGenModule.h | 5 |
4 files changed, 37 insertions, 10 deletions
diff --git a/clang/CodeGen/CGExpr.cpp b/clang/CodeGen/CGExpr.cpp index 63879ad66be..e6aaff15d55 100644 --- a/clang/CodeGen/CGExpr.cpp +++ b/clang/CodeGen/CGExpr.cpp @@ -12,6 +12,7 @@ //===----------------------------------------------------------------------===// #include "CodeGenFunction.h" +#include "CodeGenModule.h" #include "clang/AST/AST.h" #include "llvm/Constants.h" #include "llvm/DerivedTypes.h" @@ -261,6 +262,8 @@ LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) { llvm::Value *V = LocalDeclMap[D]; assert(V && "BlockVarDecl not entered in LocalDeclMap?"); return LValue::getAddr(V); + } else if (isa<FunctionDecl>(D) || isa<FileVarDecl>(D)) { + return LValue::getAddr(CGM.GetAddrOfGlobalDecl(D)); } assert(0 && "Unimp declref"); } diff --git a/clang/CodeGen/CodeGenFunction.cpp b/clang/CodeGen/CodeGenFunction.cpp index d3abbc97e2b..3c4a68baeac 100644 --- a/clang/CodeGen/CodeGenFunction.cpp +++ b/clang/CodeGen/CodeGenFunction.cpp @@ -47,14 +47,11 @@ void CodeGenFunction::GenerateCode(const FunctionDecl *FD) { LLVMIntTy = ConvertType(getContext().IntTy, FD->getLocation()); LLVMPointerWidth = Target.getPointerWidth(FD->getLocation()); - const llvm::FunctionType *Ty = - cast<llvm::FunctionType>(ConvertType(FD->getType(), FD->getLocation())); - - // FIXME: param attributes for sext/zext etc. - + CurFn = cast<llvm::Function>(CGM.GetAddrOfGlobalDecl(FD)); CurFuncDecl = FD; - CurFn = new llvm::Function(Ty, llvm::Function::ExternalLinkage, - FD->getName(), &CGM.getModule()); + + // TODO: Set up linkage and many other things. + assert(CurFn->isDeclaration() && "Function already has body?"); llvm::BasicBlock *EntryBB = new llvm::BasicBlock("entry", CurFn); @@ -77,10 +74,10 @@ void CodeGenFunction::GenerateCode(const FunctionDecl *FD) { // Emit a return for code that falls off the end. // FIXME: if this is C++ main, this should return 0. - if (Ty->getReturnType() == llvm::Type::VoidTy) + if (CurFn->getReturnType() == llvm::Type::VoidTy) Builder.CreateRetVoid(); else - Builder.CreateRet(llvm::UndefValue::get(Ty->getReturnType())); + Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType())); // Verify that the function is well formed. assert(!verifyFunction(*CurFn)); diff --git a/clang/CodeGen/CodeGenModule.cpp b/clang/CodeGen/CodeGenModule.cpp index 6ec9b796bd9..d4068c67532 100644 --- a/clang/CodeGen/CodeGenModule.cpp +++ b/clang/CodeGen/CodeGenModule.cpp @@ -15,6 +15,9 @@ #include "CodeGenFunction.h" #include "clang/AST/ASTContext.h" #include "clang/AST/Decl.h" +#include "llvm/DerivedTypes.h" +#include "llvm/Function.h" +#include "llvm/GlobalVariable.h" using namespace clang; using namespace CodeGen; @@ -22,6 +25,27 @@ using namespace CodeGen; CodeGenModule::CodeGenModule(ASTContext &C, llvm::Module &M) : Context(C), TheModule(M), Types(C.Target) {} +llvm::Constant *CodeGenModule::GetAddrOfGlobalDecl(const Decl *D) { + // See if it is already in the map. + llvm::Constant *&Entry = GlobalDeclMap[D]; + if (Entry) return Entry; + + QualType ASTTy = cast<ValueDecl>(D)->getType(); + const llvm::Type *Ty = getTypes().ConvertType(ASTTy, D->getLocation()); + if (isa<FunctionDecl>(D)) { + const llvm::FunctionType *FTy = cast<llvm::FunctionType>(Ty); + // FIXME: param attributes for sext/zext etc. + return Entry = new llvm::Function(FTy, llvm::Function::ExternalLinkage, + D->getName(), &getModule()); + } + + assert(isa<FileVarDecl>(D) && "Unknown global decl!"); + + return Entry = new llvm::GlobalVariable(Ty, false, + llvm::GlobalValue::ExternalLinkage, + 0, D->getName(), &getModule()); +} + void CodeGenModule::EmitFunction(FunctionDecl *FD) { // If this is not a prototype, emit the body. if (FD->getBody()) diff --git a/clang/CodeGen/CodeGenModule.h b/clang/CodeGen/CodeGenModule.h index d72b84549c8..8e40483bd57 100644 --- a/clang/CodeGen/CodeGenModule.h +++ b/clang/CodeGen/CodeGenModule.h @@ -15,6 +15,7 @@ #define CODEGEN_CODEGENMODULE_H #include "CodeGenTypes.h" +#include "llvm/ADT/DenseMap.h" namespace llvm { class Module; @@ -35,7 +36,7 @@ class CodeGenModule { llvm::Module &TheModule; CodeGenTypes Types; - //llvm::DenseMap<const Decl*, llvm::Constant*> GlobalDeclMap; + llvm::DenseMap<const Decl*, llvm::Constant*> GlobalDeclMap; public: CodeGenModule(ASTContext &C, llvm::Module &M); @@ -43,6 +44,8 @@ public: llvm::Module &getModule() const { return TheModule; } CodeGenTypes &getTypes() { return Types; } + llvm::Constant *GetAddrOfGlobalDecl(const Decl *D); + void EmitFunction(FunctionDecl *FD); void PrintStats() {} |