[llvm-commits] CVS: llvm-java/lib/Compiler/VMField.cpp VMClass.h VMClass.cpp Compiler.cpp

Alkis Evlogimenos alkis at cs.uiuc.edu
Tue Mar 29 09:37:19 PST 2005



Changes in directory llvm-java/lib/Compiler:

VMField.cpp updated: 1.3 -> 1.4
VMClass.h updated: 1.21 -> 1.22
VMClass.cpp updated: 1.24 -> 1.25
Compiler.cpp updated: 1.266 -> 1.267
---
Log message:

Allocate space for static members and initialize the constant static
members as part of class linking (as per the JVM specification).

Use the VMField abstraction to refer to static members as well. This
remove a bunch of code from the Compiler.


---
Diffs of the changes:  (+33 -99)

 Compiler.cpp |  102 ++++-------------------------------------------------------
 VMClass.cpp  |    5 +-
 VMClass.h    |    1 
 VMField.cpp  |   24 ++++++++++++-
 4 files changed, 33 insertions(+), 99 deletions(-)


Index: llvm-java/lib/Compiler/VMField.cpp
diff -u llvm-java/lib/Compiler/VMField.cpp:1.3 llvm-java/lib/Compiler/VMField.cpp:1.4
--- llvm-java/lib/Compiler/VMField.cpp:1.3	Tue Mar 29 11:11:39 2005
+++ llvm-java/lib/Compiler/VMField.cpp	Tue Mar 29 11:37:08 2005
@@ -13,8 +13,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "VMField.h"
+#include "Resolver.h"
 #include "VMClass.h"
-#include <iostream>
+#include <llvm/Constants.h>
 
 using namespace llvm;
 using namespace llvm::Java;
@@ -27,5 +28,24 @@
     field_(field)
 {
   assert(isStatic() && "This should be a static field!");
-  // FIXME: add Global to module.
+
+  // A java static field is constant if it is marked final and has a
+  // static initializer.
+  bool isConstant = field_->isFinal() && field->getConstantValueAttribute();
+
+  llvm::Constant* init = NULL;
+  if (ConstantValueAttribute* attr = field_->getConstantValueAttribute()) {
+    init = parent_->getConstant(attr->getValueIndex());
+    if (init->getType() != class_->getType())
+      init = ConstantExpr::getCast(init, class_->getType());
+  }
+  else
+    init = llvm::Constant::getNullValue(class_->getType());
+
+  data_.global = new GlobalVariable(class_->getType(),
+                                    isConstant,
+                                    GlobalVariable::ExternalLinkage,
+                                    init,
+                                    parent_->getName() + '/' + getName(),
+                                    parent_->getResolver()->getModule());
 }


Index: llvm-java/lib/Compiler/VMClass.h
diff -u llvm-java/lib/Compiler/VMClass.h:1.21 llvm-java/lib/Compiler/VMClass.h:1.22
--- llvm-java/lib/Compiler/VMClass.h:1.21	Tue Mar 29 07:24:46 2005
+++ llvm-java/lib/Compiler/VMClass.h	Tue Mar 29 11:37:08 2005
@@ -70,6 +70,7 @@
 
   public:
     const std::string& getName() const { return name_; }
+    Resolver* getResolver() const { return resolver_; }
     const Type* getLayoutType() const { return layoutType_; }
     const Type* getType() const { return type_; }
     const ClassFile* getClassFile() const { return classFile_; }


Index: llvm-java/lib/Compiler/VMClass.cpp
diff -u llvm-java/lib/Compiler/VMClass.cpp:1.24 llvm-java/lib/Compiler/VMClass.cpp:1.25
--- llvm-java/lib/Compiler/VMClass.cpp:1.24	Tue Mar 29 07:24:46 2005
+++ llvm-java/lib/Compiler/VMClass.cpp	Tue Mar 29 11:37:08 2005
@@ -119,11 +119,12 @@
     const Fields& fields = classFile_->getFields();
     for (unsigned i = 0, e = fields.size(); i != e; ++i) {
       Field* field = fields[i];
+      const VMClass* fc = getClass(field->getDescriptorIndex());
       if (field->isStatic()) {
-        // FIXME: Initialize static VMFields as well.
+        fieldMap_.insert(std::make_pair(field->getName()->str(),
+                                        VMField(this, fc, field)));
       }
       else {
-        const VMClass* fc = getClass(field->getDescriptorIndex());
         unsigned index = memberFields_.size() + 1;
         FieldMap::iterator i =
           fieldMap_.insert(std::make_pair(


Index: llvm-java/lib/Compiler/Compiler.cpp
diff -u llvm-java/lib/Compiler/Compiler.cpp:1.266 llvm-java/lib/Compiler/Compiler.cpp:1.267
--- llvm-java/lib/Compiler/Compiler.cpp:1.266	Tue Mar 29 11:29:56 2005
+++ llvm-java/lib/Compiler/Compiler.cpp	Tue Mar 29 11:37:08 2005
@@ -788,63 +788,6 @@
       return arrayInfo;
     }
 
-    /// Emits the necessary code to get a pointer to a static field of
-    /// an object.
-    GlobalVariable* getStaticField(unsigned index) {
-      ConstantFieldRef* fieldRef =
-        class_->getClassFile()->getConstantFieldRef(index);
-      ConstantNameAndType* nameAndType = fieldRef->getNameAndType();
-
-      const std::string& className = fieldRef->getClass()->getName()->str();
-      GlobalVariable* global = getStaticField(
-        class_->getClass(fieldRef->getClassIndex()),
-        nameAndType->getName()->str(),
-        class_->getClass(nameAndType->getDescriptorIndex()));
-
-      assert(global && "Cannot find global for static field!");
-
-      return global;
-    }
-
-    /// Finds a static field in the specified class, any of its
-    /// super clases, or any of the interfaces it implements.
-    GlobalVariable* getStaticField(const VMClass* clazz,
-                                   const std::string& name,
-                                   const VMClass* fieldClass) {
-      emitStaticInitializers(clazz->getClassFile());
-
-      std::string globalName =
-        clazz->getClassFile()->getThisClass()->getName()->str() + '/' + name;
-      DEBUG(std::cerr << "Looking up global: " << globalName << '\n');
-      const Type* type = fieldClass->getType();
-      if (GlobalVariable* g = module_->getGlobalVariable(globalName, type))
-        return g;
-
-      for (unsigned i = 0, e = clazz->getNumInterfaces(); i != e; ++i) {
-        const VMClass* interface = clazz->getInterface(i);
-        emitStaticInitializers(interface->getClassFile());
-        globalName =
-          interface->getClassFile()->getThisClass()->getName()->str() +
-          '/' + name;
-        DEBUG(std::cerr << "Looking up global: " << globalName << '\n');
-        if (GlobalVariable* g = module_->getGlobalVariable(globalName, type))
-          return g;
-      }
-
-      for (unsigned i = 0, e = clazz->getNumSuperClasses(); i != e; ++i) {
-        const VMClass* superClass = clazz->getSuperClass(i);
-        emitStaticInitializers(superClass->getClassFile());
-        globalName =
-          superClass->getClassFile()->getThisClass()->getName()->str() +
-          '/' + name;
-        DEBUG(std::cerr << "Looking up global: " << globalName << '\n');
-        if (GlobalVariable* g = module_->getGlobalVariable(globalName, type))
-          return g;
-      }
-
-      return NULL;
-    }
-
     std::string getMangledString(const std::string& str) {
       std::string mangledStr;
 
@@ -1059,39 +1002,6 @@
           classfile->getThisClass()->getName()->str();
         const VMClass* clazz = resolver_->getClass(className);
 
-        // Create the global variables of this class.
-        const Fields& fields = classfile->getFields();
-        for (unsigned i = 0, e = fields.size(); i != e; ++i) {
-          Field* field = fields[i];
-          if (field->isStatic()) {
-            const VMClass* fieldClass =
-              clazz->getClass(field->getDescriptorIndex());
-            const Type* globalTy = fieldClass->getType();
-            // A java field can be final/constant even if it has a
-            // dynamic initializer. Because LLVM does not currently
-            // support these semantics, we consider constants only
-            // final fields with static initializers.
-            bool isConstant = false;
-            llvm::Constant* init = llvm::Constant::getNullValue(globalTy);
-            if (field->getConstantValueAttribute()) {
-              unsigned i = field->getConstantValueAttribute()->getValueIndex();
-              init = ConstantExpr::getCast(clazz->getConstant(i), globalTy);
-              isConstant = field->isFinal();
-            }
-
-            std::string globalName =
-              classfile->getThisClass()->getName()->str() + '/' +
-              field->getName()->str();
-            DEBUG(std::cerr << "Adding global: " << globalName << '\n');
-            new GlobalVariable(globalTy,
-                               isConstant,
-                               GlobalVariable::ExternalLinkage,
-                               init,
-                               globalName,
-                               module_);
-          }
-        }
-
         Function* hook = module_->getOrInsertFunction(LLVM_JAVA_STATIC_INIT,
                                                       Type::VoidTy, 0);
         Instruction* I = hook->front().getTerminator();
@@ -1576,15 +1486,17 @@
     }
 
     void do_getstatic(unsigned index) {
-      Value* v = new LoadInst(getStaticField(index), TMP, currentBB_);
+      const VMField* field = class_->getField(index);
+
+      Value* v = new LoadInst(field->getGlobal(), TMP, currentBB_);
       push(v);
     }
 
     void do_putstatic(unsigned index) {
-      Value* ptr = getStaticField(index);
-      const Type* fieldTy = cast<PointerType>(ptr->getType())->getElementType();
-      Value* v = pop(fieldTy);
-      new StoreInst(v, ptr, currentBB_);
+      const VMField* field = class_->getField(index);
+
+      Value* v = pop(field->getClass()->getType());
+      new StoreInst(v, field->getGlobal(), currentBB_);
     }
 
     void do_getfield(unsigned index) {






More information about the llvm-commits mailing list