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

Alkis Evlogimenos alkis at cs.uiuc.edu
Sat Mar 26 06:41:59 PST 2005



Changes in directory llvm-java/lib/Compiler:

Compiler.cpp updated: 1.249 -> 1.250
Class.h updated: 1.6 -> 1.7
Class.cpp updated: 1.6 -> 1.7
---
Log message:

Resolve constant pool inside the Class object and cache the
results. Right now we only resolve constants (class and member
references will follow).


---
Diffs of the changes:  (+77 -67)

 Class.cpp    |   47 +++++++++++++++++++++++++++++
 Class.h      |    4 ++
 Compiler.cpp |   93 +++++++++++++++++------------------------------------------
 3 files changed, 77 insertions(+), 67 deletions(-)


Index: llvm-java/lib/Compiler/Compiler.cpp
diff -u llvm-java/lib/Compiler/Compiler.cpp:1.249 llvm-java/lib/Compiler/Compiler.cpp:1.250
--- llvm-java/lib/Compiler/Compiler.cpp:1.249	Sat Mar 26 00:10:56 2005
+++ llvm-java/lib/Compiler/Compiler.cpp	Sat Mar 26 08:41:48 2005
@@ -156,8 +156,9 @@
     }
 
     template <typename InsertionPointTy>
-    GlobalVariable* createNewString(const std::string& str,
-                                    InsertionPointTy* ip) {
+    void initializeString(Value* globalString,
+                          const std::string& str,
+                          InsertionPointTy* ip) {
       // Create a new byte[] object and initialize it with the
       // contents of this string constant.
       Value* count = ConstantUInt::get(Type::UIntTy, str.size());
@@ -194,14 +195,6 @@
       const Class& ci = resolver_->getClass("java/lang/String");
       const VTableInfo& vi = getVTableInfo(ci.getClassFile());
 
-      // Create a zeroinitialized static java/lang/String object.
-      GlobalVariable* globalString =
-        new GlobalVariable(ci.getStructType(),
-                           false,
-                           GlobalVariable::LinkOnceLinkage,
-                           llvm::Constant::getNullValue(ci.getStructType()),
-                           str + ".java/lang/String",
-                           &module_);
       // Install the vtable pointer.
       Value* objBase =
         new CastInst(globalString, resolver_->getObjectBaseRefType(), TMP, ip);
@@ -219,45 +212,6 @@
       params.push_back(new CastInst(arrayRef, resolver_->getObjectBaseRefType(), TMP, ip));
       params.push_back(ConstantSInt::get(Type::IntTy, 0));
       new CallInst(function, params, "", ip);
-
-      return globalString;
-    }
-
-    GlobalVariable* getConstantString(ConstantString* s) {
-      const std::string& str = s->getValue()->str();
-      StringMap::iterator it = stringMap_.find(str);
-      if (it == stringMap_.end()) {
-        // Get the static initializer function and get its one and
-        // only basic block to add code to.
-        Function* hook = module_.getOrInsertFunction(LLVM_JAVA_STATIC_INIT,
-                                                     Type::VoidTy, 0);
-        Instruction* I = hook->front().getTerminator();
-        assert(I && LLVM_JAVA_STATIC_INIT " should have a terminator!");
-
-        GlobalVariable* stringGlobal = createNewString(str, I);
-
-        // Insert this new string into the map.
-        it = stringMap_.insert(it, std::make_pair(str, stringGlobal));
-      }
-
-      return it->second;
-    }
-
-    /// Given a llvm::Java::Constant returns an llvm::Constant.
-    llvm::Constant* getConstant(Constant* c) {
-      if (ConstantString* s = dynamic_cast<ConstantString*>(c))
-        return getConstantString(s);
-      else if (ConstantInteger* i = dynamic_cast<ConstantInteger*>(c))
-        return ConstantSInt::get(Type::IntTy, i->getValue());
-      else if (ConstantFloat* f = dynamic_cast<ConstantFloat*>(c))
-        return ConstantFP::get(Type::FloatTy, f->getValue());
-      else if (ConstantLong* l = dynamic_cast<ConstantLong*>(c))
-        return ConstantSInt::get(Type::LongTy, l->getValue());
-      else if (ConstantDouble* d = dynamic_cast<ConstantDouble*>(c))
-        return ConstantFP::get(Type::DoubleTy, d->getValue());
-      else
-        assert(0 && "Unknown llvm::Java::Constant!");
-      return 0; // not reached
     }
 
     /// Returns the type of the Java string descriptor for JNI.
@@ -1325,6 +1279,10 @@
           emitStaticInitializers(
             ClassFile::get(classfile->getSuperClass()->getName()->str()));
 
+        const std::string& className =
+          classfile->getThisClass()->getName()->str();
+        const Class& 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) {
@@ -1338,9 +1296,8 @@
             bool isConstant = false;
             llvm::Constant* init = llvm::Constant::getNullValue(globalTy);
             if (field->getConstantValueAttribute()) {
-              Constant* constant =
-                field->getConstantValueAttribute()->getValue();
-              init = ConstantExpr::getCast(getConstant(constant), globalTy);
+              unsigned i = field->getConstantValueAttribute()->getValueIndex();
+              init = ConstantExpr::getCast(clazz.getConstant(i), globalTy);
               isConstant = field->isFinal();
             }
 
@@ -1357,24 +1314,28 @@
           }
         }
 
+        Function* hook = module_.getOrInsertFunction(LLVM_JAVA_STATIC_INIT,
+                                                     Type::VoidTy, 0);
+        Instruction* I = hook->front().getTerminator();
+        assert(I && LLVM_JAVA_STATIC_INIT " should have a terminator!");
+
+        // Create constant strings for this class.
+        for (unsigned i = 0, e = classfile->getNumConstants(); i != e; ++i)
+          if (ConstantString* s = dynamic_cast<ConstantString*>(classfile->getConstant(i)))
+            initializeString(clazz.getConstant(i), s->getValue()->str(), I);
+
         // Call its class initialization method if it exists.
         if (const Method* method = classfile->getMethod("<clinit>()V")) {
-          std::string name = classfile->getThisClass()->getName()->str();
-          name += '/';
-          name += method->getName()->str();
-          name += method->getDescriptor()->str();
-
-          Function* hook = module_.getOrInsertFunction(LLVM_JAVA_STATIC_INIT,
-                                                       Type::VoidTy, 0);
-          Function* init = module_.getOrInsertFunction(name, Type::VoidTy, 0);
+          const std::string& functionName = className + '/' +
+            method->getName()->str() + method->getDescriptor()->str();
+          Function* init =
+            module_.getOrInsertFunction(functionName, Type::VoidTy, 0);
 
           // Insert a call to it right before the terminator of the only
           // basic block in llvm_java_static_init.
           bool inserted =  scheduleFunction(init);
           assert(inserted && "Class initialization method already called!");
-          assert(hook->front().getTerminator() &&
-                 LLVM_JAVA_STATIC_INIT " should have a terminator!");
-          new CallInst(init, "", hook->front().getTerminator());
+          new CallInst(init, "", I);
         }
       }
     }
@@ -1475,9 +1436,9 @@
     }
 
     void do_ldc(unsigned index) {
-      Constant* c = class_->getClassFile()->getConstant(index);
-      assert(getConstant(c) && "Java constant not handled!");
-      push(getConstant(c));
+      llvm::Constant* c = class_->getConstant(index);
+      assert(c && "Java constant not handled!");
+      push(c);
     }
 
     void do_ldc2(unsigned index) {


Index: llvm-java/lib/Compiler/Class.h
diff -u llvm-java/lib/Compiler/Class.h:1.6 llvm-java/lib/Compiler/Class.h:1.7
--- llvm-java/lib/Compiler/Class.h:1.6	Fri Mar 25 23:24:05 2005
+++ llvm-java/lib/Compiler/Class.h	Sat Mar 26 08:41:48 2005
@@ -15,6 +15,7 @@
 #ifndef LLVM_JAVA_CLASS_H
 #define LLVM_JAVA_CLASS_H
 
+#include <llvm/Constant.h>
 #include <llvm/Module.h>
 #include <llvm/Type.h>
 #include <map>
@@ -39,6 +40,7 @@
     Field2IndexMap f2iMap_;
     typedef std::vector<const Type*> ElementTypes;
     ElementTypes elementTypes_;
+    mutable std::vector<void*> resolvedConstantPool_;
 
     void addField(const std::string& name, const Type* type);
     void resolveType();
@@ -73,6 +75,8 @@
     bool isPrimitive() const { return !structType_; }
     unsigned getInterfaceIndex() const { return interfaceIndex_; }
     int getFieldIndex(const std::string& name) const;
+
+    llvm::Constant* getConstant(unsigned index) const;
   };
 
 } } // namespace llvm::Java


Index: llvm-java/lib/Compiler/Class.cpp
diff -u llvm-java/lib/Compiler/Class.cpp:1.6 llvm-java/lib/Compiler/Class.cpp:1.7
--- llvm-java/lib/Compiler/Class.cpp:1.6	Fri Mar 25 23:24:05 2005
+++ llvm-java/lib/Compiler/Class.cpp	Sat Mar 26 08:41:48 2005
@@ -18,6 +18,7 @@
 #include "Class.h"
 #include "Resolver.h"
 #include <llvm/DerivedTypes.h>
+#include <llvm/Constants.h>
 #include <llvm/Java/ClassFile.h>
 
 #define LLVM_JAVA_OBJECT_BASE "struct.llvm_java_object_base"
@@ -32,7 +33,8 @@
     componentClass_(NULL),
     structType_(OpaqueType::get()),
     type_(PointerType::get(structType_)),
-    interfaceIndex_(INVALID_INTERFACE_INDEX)
+    interfaceIndex_(INVALID_INTERFACE_INDEX),
+    resolvedConstantPool_(classFile_->getNumConstants())
 {
 
 }
@@ -127,3 +129,46 @@
 
   assert(!isa<OpaqueType>(getStructType()) &&"Class not initialized properly!");
 }
+
+llvm::Constant* Class::getConstant(unsigned index) const
+{
+  assert(classFile_ && "No constant pool!");
+  assert((dynamic_cast<ConstantString*>(classFile_->getConstant(index)) ||
+          dynamic_cast<ConstantInteger*>(classFile_->getConstant(index)) ||
+          dynamic_cast<ConstantFloat*>(classFile_->getConstant(index)) ||
+          dynamic_cast<ConstantLong*>(classFile_->getConstant(index)) ||
+          dynamic_cast<ConstantDouble*>(classFile_->getConstant(index))) &&
+         "Not an index to a constant!");
+
+  // If we haven't resolved this constant already, do so now.
+  if (!resolvedConstantPool_[index]) {
+    Constant* jc = classFile_->getConstant(index);
+    if (ConstantString* s = dynamic_cast<ConstantString*>(jc)) {
+      const Class& stringClass = resolver_->getClass("java/lang/String");
+      const Type* stringType = stringClass.getStructType();
+      resolvedConstantPool_[index] =
+        new GlobalVariable(stringType,
+                           false,
+                           GlobalVariable::LinkOnceLinkage,
+                           llvm::Constant::getNullValue(stringType),
+                           s->getValue()->str() + ".java/lang/String",
+                           &resolver_->getModule());
+    }
+    else if (ConstantInteger* i = dynamic_cast<ConstantInteger*>(jc))
+      resolvedConstantPool_[index] =
+        ConstantSInt::get(Type::IntTy, i->getValue());
+    else if (ConstantFloat* f = dynamic_cast<ConstantFloat*>(jc))
+      resolvedConstantPool_[index] =
+        ConstantFP::get(Type::FloatTy, f->getValue());
+    else if (ConstantLong* l = dynamic_cast<ConstantLong*>(jc))
+      resolvedConstantPool_[index] =
+        ConstantSInt::get(Type::LongTy, l->getValue());
+    else if (ConstantDouble* d = dynamic_cast<ConstantDouble*>(jc))
+      resolvedConstantPool_[index] =
+        ConstantFP::get(Type::DoubleTy, d->getValue());
+    else
+      assert(0 && "Not a constant!");
+  }
+
+  return static_cast<llvm::Constant*>(resolvedConstantPool_[index]);
+}






More information about the llvm-commits mailing list