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

Alkis Evlogimenos alkis at cs.uiuc.edu
Tue Mar 29 05:24:57 PST 2005



Changes in directory llvm-java/lib/Compiler:

VMField.h updated: 1.1 -> 1.2
VMField.cpp updated: 1.1 -> 1.2
VMClass.h updated: 1.20 -> 1.21
VMClass.cpp updated: 1.23 -> 1.24
Compiler.cpp updated: 1.264 -> 1.265
---
Log message:

Resolve and cache field references in VMClass objects.


---
Diffs of the changes:  (+75 -54)

 Compiler.cpp |   64 ++++++++++++++---------------------------------------------
 VMClass.cpp  |   44 +++++++++++++++++++++++++++++++++++++++-
 VMClass.h    |    2 +
 VMField.cpp  |    7 ++++--
 VMField.h    |   12 ++++++++---
 5 files changed, 75 insertions(+), 54 deletions(-)


Index: llvm-java/lib/Compiler/VMField.h
diff -u llvm-java/lib/Compiler/VMField.h:1.1 llvm-java/lib/Compiler/VMField.h:1.2
--- llvm-java/lib/Compiler/VMField.h:1.1	Mon Mar 28 21:10:46 2005
+++ llvm-java/lib/Compiler/VMField.h	Tue Mar 29 07:24:46 2005
@@ -28,6 +28,7 @@
   class VMClass;
 
   class VMField {
+    const VMClass* parent_;
     const VMClass* clazz_;
     const Field* field_;
     union {
@@ -39,11 +40,15 @@
     // Interface for VMClass.
 
     // Create static field reference.
-    VMField(const VMClass* clazz, const Field* field);
+    VMField(const VMClass* parent, const VMClass* clazz, const Field* field);
 
     // Create member field reference.
-    VMField(const VMClass* clazz, const Field* field, int index)
-      : clazz_(clazz),
+    VMField(const VMClass* parent,
+            const VMClass* clazz,
+            const Field* field,
+            int index)
+      : parent_(parent),
+        clazz_(clazz),
         field_(field) {
       assert(!isStatic() && "This should be a member field!");
       data_.index = index;
@@ -54,6 +59,7 @@
     const std::string& getName() const { return field_->getName()->str(); }
     bool isStatic() const { return field_->isStatic(); }
 
+    const VMClass* getParent() const { return parent_; }
     const VMClass* getClass() const { return clazz_; }
     int getMemberIndex() const {
       assert(!isStatic() && "Field should not be static!");


Index: llvm-java/lib/Compiler/VMField.cpp
diff -u llvm-java/lib/Compiler/VMField.cpp:1.1 llvm-java/lib/Compiler/VMField.cpp:1.2
--- llvm-java/lib/Compiler/VMField.cpp:1.1	Mon Mar 28 21:10:46 2005
+++ llvm-java/lib/Compiler/VMField.cpp	Tue Mar 29 07:24:46 2005
@@ -19,8 +19,11 @@
 using namespace llvm;
 using namespace llvm::Java;
 
-VMField::VMField(const VMClass* clazz, const Field* field)
-  : clazz_(clazz),
+VMField::VMField(const VMClass* parent,
+                 const VMClass* clazz,
+                 const Field* field)
+  : parent_(parent),
+    clazz_(clazz),
     field_(field)
 {
   assert(isStatic() && "This should be a static field!");


Index: llvm-java/lib/Compiler/VMClass.h
diff -u llvm-java/lib/Compiler/VMClass.h:1.20 llvm-java/lib/Compiler/VMClass.h:1.21
--- llvm-java/lib/Compiler/VMClass.h:1.20	Tue Mar 29 06:50:57 2005
+++ llvm-java/lib/Compiler/VMClass.h	Tue Mar 29 07:24:46 2005
@@ -46,6 +46,7 @@
     std::vector<const VMField*> memberFields_;
 
     void computeLayout();
+    const VMField* lookupField(const std::string& name) const;
 
     friend class Resolver;
 
@@ -88,6 +89,7 @@
 
     llvm::Constant* getConstant(unsigned index) const;
     const VMClass* getClass(unsigned index) const;
+    const VMField* getField(unsigned index) const;
   };
 
 } } // namespace llvm::Java


Index: llvm-java/lib/Compiler/VMClass.cpp
diff -u llvm-java/lib/Compiler/VMClass.cpp:1.23 llvm-java/lib/Compiler/VMClass.cpp:1.24
--- llvm-java/lib/Compiler/VMClass.cpp:1.23	Tue Mar 29 06:50:57 2005
+++ llvm-java/lib/Compiler/VMClass.cpp	Tue Mar 29 07:24:46 2005
@@ -70,6 +70,29 @@
 
 }
 
+const VMField* VMClass::lookupField(const std::string& name) const
+{
+  FieldMap::const_iterator it = fieldMap_.find(name);
+  if (it != fieldMap_.end())
+    return &it->second;
+
+  for (unsigned i = 0, e = getNumInterfaces(); i != e; ++i) {
+    const VMClass* interface = getInterface(i);
+    it = interface->fieldMap_.find(name);
+    if (it != interface->fieldMap_.end())
+      return &it->second;
+  }
+
+  for (unsigned i = 0, e = getNumSuperClasses(); i != e; ++i) {
+    const VMClass* superClass = getSuperClass(i);
+    it = superClass->fieldMap_.find(name);
+    if (it != superClass->fieldMap_.end())
+      return &it->second;
+  }
+
+  return NULL;
+}
+
 int VMClass::getFieldIndex(const std::string& name) const {
   FieldMap::const_iterator it = fieldMap_.find(name);
   return it == fieldMap_.end() ? -1 : it->second.getMemberIndex();
@@ -101,10 +124,11 @@
       }
       else {
         const VMClass* fc = getClass(field->getDescriptorIndex());
+        unsigned index = memberFields_.size() + 1;
         FieldMap::iterator i =
           fieldMap_.insert(std::make_pair(
                              field->getName()->str(),
-                             VMField(fc, field, memberFields_.size()+1))).first;
+                             VMField(this, fc, field, index))).first;
         memberFields_.push_back(&i->second);
         layout.push_back(fc->getType());
       }
@@ -245,3 +269,21 @@
 
   return static_cast<const VMClass*>(resolvedConstantPool_[index]);
 }
+
+const VMField* VMClass::getField(unsigned index) const
+{
+  assert(classFile_ && "No constant pool!");
+  assert(dynamic_cast<ConstantFieldRef*>(classFile_->getConstant(index)) &&
+         "Not an index to a field reference!");
+
+  // If we haven't resolved this constant already, do so now.
+  if (!resolvedConstantPool_[index]) {
+    ConstantFieldRef* jc = classFile_->getConstantFieldRef(index);
+    const VMClass* clazz = getClass(jc->getClassIndex());
+    const std::string& name = jc->getNameAndType()->getName()->str();
+    resolvedConstantPool_[index] =
+      const_cast<VMField*>(clazz->lookupField(name));
+  }
+
+  return static_cast<const VMField*>(resolvedConstantPool_[index]);
+}


Index: llvm-java/lib/Compiler/Compiler.cpp
diff -u llvm-java/lib/Compiler/Compiler.cpp:1.264 llvm-java/lib/Compiler/Compiler.cpp:1.265
--- llvm-java/lib/Compiler/Compiler.cpp:1.264	Tue Mar 29 06:50:57 2005
+++ llvm-java/lib/Compiler/Compiler.cpp	Tue Mar 29 07:24:46 2005
@@ -847,39 +847,14 @@
 
     /// Emits the necessary code to get a field from the passed
     /// pointer to an object.
-    Value* getField(unsigned index, Value* ptr) {
-      ConstantFieldRef* fieldRef =
-        class_->getClassFile()->getConstantFieldRef(index);
-      ConstantNameAndType* nameAndType = fieldRef->getNameAndType();
-      return getField(
-        class_->getClass(fieldRef->getClassIndex()),
-        nameAndType->getName()->str(),
-        ptr);
-    }
-
-    /// Emits the necessary code to get a field from the passed
-    /// pointer to an object.
-    Value* getField(const VMClass* clazz,
-                    const std::string& fieldName,
-                    Value* ptr) {
-      // Cast ptr to correct type.
-      ptr = new CastInst(ptr, clazz->getType(), TMP, currentBB_);
-
-      // Deref pointer.
-      std::vector<Value*> indices(1, ConstantUInt::get(Type::UIntTy, 0));
-      while (true) {
-        int slot = clazz->getFieldIndex(fieldName);
-        if (slot == -1) {
-          indices.push_back(ConstantUInt::get(Type::UIntTy, 0));
-          clazz = clazz->getSuperClass();
-        }
-        else {
-          indices.push_back(ConstantUInt::get(Type::UIntTy, slot));
-          break;
-        }
-      }
+    Value* getField(const VMField* field, Value* ptr) {
+      std::vector<Value*> indices(2, ConstantUInt::get(Type::UIntTy, 0));
+      indices[1] = ConstantUInt::get(Type::UIntTy, field->getMemberIndex());
 
-      return new GetElementPtrInst(ptr, indices, fieldName + '*', currentBB_);
+      return new GetElementPtrInst(ptr,
+                                   indices,
+                                   field->getName()+'*',
+                                   currentBB_);
     }
 
     std::string getMangledString(const std::string& str) {
@@ -1625,26 +1600,19 @@
     }
 
     void do_getfield(unsigned index) {
-      ConstantFieldRef* fieldRef =
-        class_->getClassFile()->getConstantFieldRef(index);
-      const std::string& name = fieldRef->getNameAndType()->getName()->str();
-      Value* p = pop(resolver_->getObjectBaseType());
-      Value* v = new LoadInst(getField(index, p), name, currentBB_);
+      const VMField* field = class_->getField(index);
+
+      Value* p = pop(field->getParent()->getType());
+      Value* v = new LoadInst(getField(field, p), field->getName(), currentBB_);
       push(v);
     }
 
     void do_putfield(unsigned index) {
-      ConstantFieldRef* fieldRef =
-        class_->getClassFile()->getConstantFieldRef(index);
-      const VMClass* fieldClass = class_->getClass(
-        fieldRef->getNameAndType()->getDescriptorIndex());
-      const Type* type = fieldClass->getType();
-      Value* v = pop(type);
-      Value* p = pop(resolver_->getObjectBaseType());
-      Value* fp = getField(index, p);
-      const Type* ft = cast<PointerType>(fp->getType())->getElementType();
-      v = new CastInst(v, ft, TMP, currentBB_);
-      new StoreInst(v, getField(index, p), currentBB_);
+      const VMField* field = class_->getField(index);
+
+      Value* v = pop(field->getClass()->getType());
+      Value* p = pop(field->getParent()->getType());
+      new StoreInst(v, getField(field, p), currentBB_);
     }
 
     void makeCall(Value* fun, const std::vector<Value*> params) {






More information about the llvm-commits mailing list