[vmkit-commits] [PATCH] Add support for extra VM-only fields, use this to add fields to j.l.Class in OpenJDK

Will Dietz wdietz2 at illinois.edu
Fri Oct 28 13:49:29 PDT 2011


Inlined below.

Misc comments:
There was a trade-off regarding the dynamic nature of nbExtraFields()
(that in the OpenJDK means a string comparison) versus adding an extra
field to the "Class" type.
Same for extraFields() method, but since it's just doing simple
pointer arithmetic adding a field for it seems unnecessary regardless.

AOT won't work for arbitrarily added fields using this method, but
since these fields are VM-specific it seemed that handling that on a
case-by-case seemed most appropriate (rather than teaching the AOT
compiler to try to auto-emit these things).
But then again AOT needs a few patches to support OpenJDK regardless,
so have work there anyway :).

Thanks, and enjoy your weekend! :)

~Will

>From ca20223a1d73f78311bd48207250f8631f0d73d2 Mon Sep 17 00:00:00 2001
From: Will Dietz <w at wdtz.org>
Date: Fri, 28 Oct 2011 12:23:58 -0500
Subject: [PATCH] Add support for extra 'hidden' fields, add fields to OJ's
 j.l.Class

---
 lib/J3/ClassLib/OpenJDK/ClasspathReflect.h |   13 +++++----
 lib/J3/Compiler/LLVMInfo.cpp               |    7 +++++
 lib/J3/VMCore/JavaClass.cpp                |   24 ++++++++++++++++-
 lib/J3/VMCore/JavaClass.h                  |   11 ++++++++
 lib/J3/VMCore/JavaClassClasspath.inc       |   16 ++++++++++++
 lib/J3/VMCore/JavaClassOpenJDK.inc         |   37 ++++++++++++++++++++++++++++
 6 files changed, 100 insertions(+), 8 deletions(-)
 create mode 100644 lib/J3/VMCore/JavaClassClasspath.inc
 create mode 100644 lib/J3/VMCore/JavaClassOpenJDK.inc

diff --git a/lib/J3/ClassLib/OpenJDK/ClasspathReflect.h
b/lib/J3/ClassLib/OpenJDK/ClasspathReflect.h
index 75d35a7..e931705 100644
--- a/lib/J3/ClassLib/OpenJDK/ClasspathReflect.h
+++ b/lib/J3/ClassLib/OpenJDK/ClasspathReflect.h
@@ -38,30 +38,31 @@ private:
   JavaObject* annotations;
   JavaObject* declaredAnnotations;
   JavaObject* annotationType;
+  // Extra fields added by VM
+  UserCommonClass* internalClass;
+  JavaObject* pd;

 public:

   static UserCommonClass* getClass(JavaObjectClass* cl) {
     llvm_gcroot(cl, 0);
-    UNIMPLEMENTED();
-    return NULL;
+    return cl->internalClass;
   }

   static void setClass(JavaObjectClass* cl, UserCommonClass* vmdata) {
     llvm_gcroot(cl, 0);
-    UNIMPLEMENTED();
+    cl->internalClass = vmdata;
   }

   static void setProtectionDomain(JavaObjectClass* cl, JavaObject* pd) {
     llvm_gcroot(cl, 0);
     llvm_gcroot(pd, 0);
-    UNIMPLEMENTED();
+    cl->pd = pd;
   }

   static JavaObject* getProtectionDomain(JavaObjectClass* cl) {
     llvm_gcroot(cl, 0);
-    UNIMPLEMENTED();
-    return NULL;
+    return cl->pd;
   }

   static void staticTracer(JavaObjectClass* obj, word_t closure) {
diff --git a/lib/J3/Compiler/LLVMInfo.cpp b/lib/J3/Compiler/LLVMInfo.cpp
index aa5a32a..a3836c3 100644
--- a/lib/J3/Compiler/LLVMInfo.cpp
+++ b/lib/J3/Compiler/LLVMInfo.cpp
@@ -54,6 +54,13 @@ Type* LLVMClassInfo::getVirtualType() {
         LLVMAssessorInfo& LAI = Compiler->getTypedefInfo(type);
         fields.push_back(LAI.llvmType);
       }
+
+      for (uint32 i = 0; i < classDef->nbExtraFields(); ++i) {
+        JavaField& field = classDef->extraFields()[i];
+        Typedef* type = field.getSignature();
+        LLVMAssessorInfo& LAI = Compiler->getTypedefInfo(type);
+        fields.push_back(LAI.llvmType);
+      }


       structType = StructType::get(context, fields, false);
diff --git a/lib/J3/VMCore/JavaClass.cpp b/lib/J3/VMCore/JavaClass.cpp
index 7daf125..caead3d 100644
--- a/lib/J3/VMCore/JavaClass.cpp
+++ b/lib/J3/VMCore/JavaClass.cpp
@@ -101,6 +101,12 @@ Class::~Class() {
     cur->~JavaField();
     classLoader->allocator.Deallocate(cur);
   }
+
+  for (uint32 i = 0; i < nbExtraFields(); ++i) {
+    JavaField* cur = &(extraFields()[i]);
+    cur->~JavaField();
+    classLoader->allocator.Deallocate(cur);
+  }

   for (uint32 i = 0; i < nbVirtualMethods; ++i) {
     JavaMethod* cur = &(virtualMethods[i]);
@@ -726,8 +732,9 @@ Attribut* Class::readAttributs(Reader& reader,
uint16& size) {

 void Class::readFields(Reader& reader) {
   uint16 nbFields = reader.readU2();
-  virtualFields = new (classLoader->allocator, "Fields") JavaField[nbFields];
-  staticFields = virtualFields + nbFields;
+  uint16 extraFields = nbExtraFields();
+  virtualFields = new (classLoader->allocator, "Fields")
JavaField[nbFields+extraFields];
+  staticFields = virtualFields + nbFields + extraFields;
   for (int i = 0; i < nbFields; i++) {
     uint16 access = reader.readU2();
     const UTF8* name = ctpInfo->UTF8At(reader.readU2());
@@ -745,6 +752,8 @@ void Class::readFields(Reader& reader) {
     }
     field->attributs = readAttributs(reader, field->nbAttributs);
   }
+
+  initializeExtraVMFields();
 }

 void Class::fillIMT(std::set<JavaMethod*>* meths) {
@@ -1774,3 +1783,14 @@ void JavaField::setStaticObjectField(JavaObject* val) {
   JavaObject** ptr =
(JavaObject**)((uint64)classDef->getStaticInstance() + ptrOffset);
   mvm::Collector::objectReferenceNonHeapWriteBarrier((gc**)ptr, (gc*)val);
 }
+
+JavaField* Class::extraFields() {
+  return virtualFields + nbVirtualFields;
+}
+
+#ifdef USE_OPENJDK
+#include "JavaClassOpenJDK.inc"
+#else
+#include "JavaClassClasspath.inc"
+#endif
+
diff --git a/lib/J3/VMCore/JavaClass.h b/lib/J3/VMCore/JavaClass.h
index 1fa9b77..593a26c 100644
--- a/lib/J3/VMCore/JavaClass.h
+++ b/lib/J3/VMCore/JavaClass.h
@@ -778,6 +778,17 @@ public:
   ///
   void makeVT();

+  /// nbExtraFields -- Number of hidden VM-only 'extra' fields
+  ///
+  uint16 nbExtraFields();
+
+  /// extraFields -- Pointer to array of the extra fields
+  JavaField * extraFields();
+
+private:
+  /// initializeExtraVMFields -- Initializes the extra VM fields (if any)
+  ///
+  void initializeExtraVMFields();
 };

 /// ClassArray - This class represents Java array classes.
diff --git a/lib/J3/VMCore/JavaClassClasspath.inc
b/lib/J3/VMCore/JavaClassClasspath.inc
new file mode 100644
index 0000000..1704150
--- /dev/null
+++ b/lib/J3/VMCore/JavaClassClasspath.inc
@@ -0,0 +1,16 @@
+//===-------- JavaClassClasspath.inc - Java class details for
Classpath ---===//
+//
+//                            The VMKit project
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+uint16 Class::nbExtraFields() {
+  return 0;
+}
+
+void Class::initializeExtraVMFields() {
+  // Nothing
+}
diff --git a/lib/J3/VMCore/JavaClassOpenJDK.inc
b/lib/J3/VMCore/JavaClassOpenJDK.inc
new file mode 100644
index 0000000..b85aa98
--- /dev/null
+++ b/lib/J3/VMCore/JavaClassOpenJDK.inc
@@ -0,0 +1,37 @@
+//===-------- JavaClassOpenJDK.inc - Java class details for OpenJDK
-------===//
+//
+//                            The VMKit project
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+bool isClass(const UTF8* name) {
+  return !strcmp(UTF8Buffer(name).cString(), "java/lang/Class");
+}
+
+uint16 Class::nbExtraFields() {
+  return ::isClass(name) ? 2 : 0;
+}
+
+void Class::initializeExtraVMFields() {
+  if (!::isClass(name)) return;
+
+  JavaField * vmFields = extraFields();
+
+  JavaField & classField = vmFields[0];
+  JavaField & pdField = vmFields[1];
+
+  uint32 access = ACC_PRIVATE | ACC_FINAL | ACC_TRANSIENT;
+
+  const UTF8* objType =
+    classLoader->asciizConstructUTF8("Ljava/lang/Object;");
+  const UTF8* className =
+    classLoader->asciizConstructUTF8("internalClass");
+  const UTF8* pdName =
+    classLoader->asciizConstructUTF8("pd");
+
+  classField.initialise(this, className, objType, access);
+  pdField.initialise(this, pdName, objType, access);
+}
-- 
1.7.5.1



More information about the vmkit-commits mailing list