[cfe-commits] r39795 - in /cfe/trunk: CodeGen/CGDecl.cpp CodeGen/CodeGenModule.cpp CodeGen/CodeGenModule.h CodeGen/ModuleBuilder.cpp Driver/LLVMCodegen.cpp include/clang/CodeGen/ModuleBuilder.h

Chris Lattner sabre at nondot.org
Thu Jul 12 22:13:45 PDT 2007


Author: lattner
Date: Fri Jul 13 00:13:43 2007
New Revision: 39795

URL: http://llvm.org/viewvc/llvm-project?rev=39795&view=rev
Log:
implement support for basic codegen of global variables with no initializers.

Modified:
    cfe/trunk/CodeGen/CGDecl.cpp
    cfe/trunk/CodeGen/CodeGenModule.cpp
    cfe/trunk/CodeGen/CodeGenModule.h
    cfe/trunk/CodeGen/ModuleBuilder.cpp
    cfe/trunk/Driver/LLVMCodegen.cpp
    cfe/trunk/include/clang/CodeGen/ModuleBuilder.h

Modified: cfe/trunk/CodeGen/CGDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CGDecl.cpp?rev=39795&r1=39794&r2=39795&view=diff

==============================================================================
--- cfe/trunk/CodeGen/CGDecl.cpp (original)
+++ cfe/trunk/CodeGen/CGDecl.cpp Fri Jul 13 00:13:43 2007
@@ -19,7 +19,6 @@
 
 
 void CodeGenFunction::EmitDecl(const Decl &D) {
-
   switch (D.getKind()) {
   default: assert(0 && "Unknown decl kind!");
   case Decl::FileVariable:

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

==============================================================================
--- cfe/trunk/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/CodeGen/CodeGenModule.cpp Fri Jul 13 00:13:43 2007
@@ -48,12 +48,45 @@
                                           0, D->getName(), &getModule());
 }
 
-void CodeGenModule::EmitFunction(FunctionDecl *FD) {
+void CodeGenModule::EmitFunction(const FunctionDecl *FD) {
   // If this is not a prototype, emit the body.
   if (FD->getBody())
     CodeGenFunction(*this).GenerateCode(FD);
 }
 
+void CodeGenModule::EmitGlobalVar(const FileVarDecl *D) {
+  llvm::GlobalVariable *GV = cast<llvm::GlobalVariable>(GetAddrOfGlobalDecl(D));
+  
+  // If the storage class is external and there is no initializer, just leave it
+  // as a declaration.
+  if (D->getStorageClass() == VarDecl::Extern && D->getInit() == 0)
+    return;
+
+  // Otherwise, convert the initializer, or use zero if appropriate.
+  llvm::Constant *Init;
+  if (D->getInit() == 0)
+    Init = llvm::Constant::getNullValue(GV->getType()->getElementType());
+  else
+    assert(D->getInit() == 0 && "FIXME: Global variable initializers unimp!");
+    
+  GV->setInitializer(Init);
+  
+  // Set the llvm linkage type as appropriate.
+  // FIXME: This isn't right.  This should handle common linkage and other
+  // stuff.
+  switch (D->getStorageClass()) {
+  case VarDecl::Auto:
+  case VarDecl::Register:
+    assert(0 && "Can't have auto or register globals");
+  case VarDecl::None:
+  case VarDecl::Extern:
+    // todo: common
+    break;
+  case VarDecl::Static:
+    GV->setLinkage(llvm::GlobalVariable::InternalLinkage);
+    break;
+  }
+}
 
 
 llvm::Function *CodeGenModule::getMemCpyFn() {

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

==============================================================================
--- cfe/trunk/CodeGen/CodeGenModule.h (original)
+++ cfe/trunk/CodeGen/CodeGenModule.h Fri Jul 13 00:13:43 2007
@@ -27,6 +27,7 @@
   class ASTContext;
   class FunctionDecl;
   class Decl;
+  class FileVarDecl;
     
 namespace CodeGen {
 
@@ -50,7 +51,8 @@
   
   llvm::Function *getMemCpyFn();
   
-  void EmitFunction(FunctionDecl *FD);
+  void EmitFunction(const FunctionDecl *FD);
+  void EmitGlobalVar(const FileVarDecl *D);
   
   void PrintStats() {}
 };

Modified: cfe/trunk/CodeGen/ModuleBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/ModuleBuilder.cpp?rev=39795&r1=39794&r2=39795&view=diff

==============================================================================
--- cfe/trunk/CodeGen/ModuleBuilder.cpp (original)
+++ cfe/trunk/CodeGen/ModuleBuilder.cpp Fri Jul 13 00:13:43 2007
@@ -32,6 +32,12 @@
   static_cast<CodeGenModule*>(B)->EmitFunction(D);
 }
 
+/// CodeGenGlobalVar - Emit the specified global variable to LLVM.
+void clang::CodeGen::CodeGenGlobalVar(BuilderTy *Builder, FileVarDecl *D) {
+  static_cast<CodeGenModule*>(Builder)->EmitGlobalVar(D);
+}
+
+
 /// PrintStats - Emit statistic information to stderr.
 ///
 void clang::CodeGen::PrintStats(BuilderTy *B) {

Modified: cfe/trunk/Driver/LLVMCodegen.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/LLVMCodegen.cpp?rev=39795&r1=39794&r2=39795&view=diff

==============================================================================
--- cfe/trunk/Driver/LLVMCodegen.cpp (original)
+++ cfe/trunk/Driver/LLVMCodegen.cpp Fri Jul 13 00:13:43 2007
@@ -45,10 +45,12 @@
     
     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
       CodeGen::CodeGenFunction(Builder, FD);
-    } else if (isa<TypedefDecl>(D)) {
-      std::cerr << "Read top-level typedef decl: '" << D->getName() << "'\n";
+    } else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(D)) {
+      CodeGen::CodeGenGlobalVar(Builder, FVD);
     } else {
-      std::cerr << "Read top-level variable decl: '" << D->getName() << "'\n";
+      assert(isa<TypedefDecl>(D) && "Only expected typedefs here");
+      // don't codegen for now, eventually pass down for debug info.
+      //std::cerr << "Read top-level typedef decl: '" << D->getName() << "'\n";
     }
   }
   

Modified: cfe/trunk/include/clang/CodeGen/ModuleBuilder.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/CodeGen/ModuleBuilder.h?rev=39795&r1=39794&r2=39795&view=diff

==============================================================================
--- cfe/trunk/include/clang/CodeGen/ModuleBuilder.h (original)
+++ cfe/trunk/include/clang/CodeGen/ModuleBuilder.h Fri Jul 13 00:13:43 2007
@@ -21,6 +21,7 @@
 namespace clang {
   class ASTContext;
   class FunctionDecl;
+  class FileVarDecl;
   
 namespace CodeGen {
   /// BuilderTy - This is an opaque type used to reference ModuleBuilder
@@ -34,6 +35,9 @@
   ///
   void CodeGenFunction(BuilderTy *Builder, FunctionDecl *D);
   
+  /// CodeGenGlobalVar - Emit the specified global variable to LLVM.
+  void CodeGenGlobalVar(BuilderTy *Builder, FileVarDecl *D);
+  
   /// PrintStats - Emit statistic information to stderr.
   ///
   void PrintStats(BuilderTy *Builder);





More information about the cfe-commits mailing list