[cfe-commits] r39663 - in /cfe/cfe/trunk/CodeGen: CGExpr.cpp CodeGenFunction.cpp CodeGenModule.cpp CodeGenModule.h

clattner at cs.uiuc.edu clattner at cs.uiuc.edu
Wed Jul 11 09:46:53 PDT 2007


Author: clattner
Date: Wed Jul 11 11:46:52 2007
New Revision: 39663

URL: http://llvm.org/viewvc/llvm-project?rev=39663&view=rev
Log:
Hook up global function and variable handling.  We can now compile:

int X, bar(int,int,int);
short Y;

double foo() {
  return bar(X, Y, 3);
}

into:

@X = external global i32                ; <i32*> [#uses=1]
@Y = external global i16                ; <i16*> [#uses=1]

define double @foo() {
entry:
        %tmp = load i32* @X             ; <i32> [#uses=1]
        %tmp1 = load i16* @Y            ; <i16> [#uses=1]
        %promote = sext i16 %tmp1 to i32                ; <i32> [#uses=1]
        %call = tail call i32 @bar( i32 %tmp, i32 %promote, i32 3 )             ; <i32> [#uses=1]
        %conv = sitofp i32 %call to double              ; <double> [#uses=1]
        ret double %conv
}

declare i32 @bar(i32, i32, i32)

Modified:
    cfe/cfe/trunk/CodeGen/CGExpr.cpp
    cfe/cfe/trunk/CodeGen/CodeGenFunction.cpp
    cfe/cfe/trunk/CodeGen/CodeGenModule.cpp
    cfe/cfe/trunk/CodeGen/CodeGenModule.h

Modified: cfe/cfe/trunk/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/CodeGen/CGExpr.cpp?rev=39663&r1=39662&r2=39663&view=diff

==============================================================================
--- cfe/cfe/trunk/CodeGen/CGExpr.cpp (original)
+++ cfe/cfe/trunk/CodeGen/CGExpr.cpp Wed Jul 11 11:46:52 2007
@@ -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 @@
     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");
 }

Modified: cfe/cfe/trunk/CodeGen/CodeGenFunction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/CodeGen/CodeGenFunction.cpp?rev=39663&r1=39662&r2=39663&view=diff

==============================================================================
--- cfe/cfe/trunk/CodeGen/CodeGenFunction.cpp (original)
+++ cfe/cfe/trunk/CodeGen/CodeGenFunction.cpp Wed Jul 11 11:46:52 2007
@@ -47,14 +47,11 @@
   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 @@
   
   // 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));

Modified: cfe/cfe/trunk/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/CodeGen/CodeGenModule.cpp?rev=39663&r1=39662&r2=39663&view=diff

==============================================================================
--- cfe/cfe/trunk/CodeGen/CodeGenModule.cpp (original)
+++ cfe/cfe/trunk/CodeGen/CodeGenModule.cpp Wed Jul 11 11:46:52 2007
@@ -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 @@
 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())

Modified: cfe/cfe/trunk/CodeGen/CodeGenModule.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/CodeGen/CodeGenModule.h?rev=39663&r1=39662&r2=39663&view=diff

==============================================================================
--- cfe/cfe/trunk/CodeGen/CodeGenModule.h (original)
+++ cfe/cfe/trunk/CodeGen/CodeGenModule.h Wed Jul 11 11:46:52 2007
@@ -15,6 +15,7 @@
 #define CODEGEN_CODEGENMODULE_H
 
 #include "CodeGenTypes.h"
+#include "llvm/ADT/DenseMap.h"
 
 namespace llvm {
   class Module;
@@ -35,7 +36,7 @@
   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 @@
   llvm::Module &getModule() const { return TheModule; }
   CodeGenTypes &getTypes() { return Types; }
   
+  llvm::Constant *GetAddrOfGlobalDecl(const Decl *D);
+  
   void EmitFunction(FunctionDecl *FD);
   
   void PrintStats() {}





More information about the cfe-commits mailing list