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

Alkis Evlogimenos alkis at cs.uiuc.edu
Fri Apr 22 17:12:25 PDT 2005



Changes in directory llvm-java/lib/Compiler:

VMMethod.h updated: 1.3 -> 1.4
VMMethod.cpp updated: 1.4 -> 1.5
VMClass.h updated: 1.34 -> 1.35
VMClass.cpp updated: 1.45 -> 1.46
Resolver.cpp updated: 1.21 -> 1.22
---
Log message:

Implement calls to java methods (both static and dynamic) from JNI
code.


---
Diffs of the changes:  (+177 -1)

 Resolver.cpp |    8 ++++
 VMClass.cpp  |  102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 VMClass.h    |    4 ++
 VMMethod.cpp |   59 +++++++++++++++++++++++++++++++++-
 VMMethod.h   |    5 ++
 5 files changed, 177 insertions(+), 1 deletion(-)


Index: llvm-java/lib/Compiler/VMMethod.h
diff -u llvm-java/lib/Compiler/VMMethod.h:1.3 llvm-java/lib/Compiler/VMMethod.h:1.4
--- llvm-java/lib/Compiler/VMMethod.h:1.3	Fri Apr  1 12:24:48 2005
+++ llvm-java/lib/Compiler/VMMethod.h	Fri Apr 22 19:12:14 2005
@@ -19,6 +19,7 @@
 
 namespace llvm {
 
+  class Constant;
   class Function;
   class FunctionType;
 
@@ -32,6 +33,8 @@
     const VMClass* parent_;
     const Method* method_;
     Function* function_;
+    Function* bridgeFunction_;
+
     int index_;
 
     void init();
@@ -69,6 +72,8 @@
       return getName() + getDescriptor();
     }
 
+    llvm::Constant* buildMethodDescriptor() const;
+    llvm::Constant* getBridgeFunction() const;
   };
 
 } } // namespace llvm::Java


Index: llvm-java/lib/Compiler/VMMethod.cpp
diff -u llvm-java/lib/Compiler/VMMethod.cpp:1.4 llvm-java/lib/Compiler/VMMethod.cpp:1.5
--- llvm-java/lib/Compiler/VMMethod.cpp:1.4	Tue Apr 19 02:26:47 2005
+++ llvm-java/lib/Compiler/VMMethod.cpp	Fri Apr 22 19:12:14 2005
@@ -15,8 +15,11 @@
 #include "VMMethod.h"
 #include "Resolver.h"
 #include "VMClass.h"
-#include <llvm/Function.h>
+#include <llvm/Constants.h>
 #include <llvm/DerivedTypes.h>
+#include <llvm/Function.h>
+#include <llvm/Instructions.h>
+#include <llvm/ADT/STLExtras.h>
 
 using namespace llvm;
 using namespace llvm::Java;
@@ -38,6 +41,41 @@
     resolver->getType(methodDescriptor, !method_->isStatic()));
   Module* module = resolver->getModule();
   function_ = module->getOrInsertFunction(functionName, functionType);
+
+  std::vector<const Type*> argTypes;
+  argTypes.reserve(2);
+  argTypes.push_back(resolver->getObjectBaseType());
+  argTypes.push_back(PointerType::get(Type::SByteTy));
+  const FunctionType* bridgeFunctionType =
+    FunctionType::get(functionType->getReturnType(), argTypes, false);
+  bridgeFunction_ = module->getOrInsertFunction("bridge_to_" + functionName,
+                                                bridgeFunctionType);
+  BasicBlock* bb = new BasicBlock("entry", bridgeFunction_);
+  std::vector<Value*> params;
+  params.reserve(functionType->getNumParams());
+  Value* objectArg = bridgeFunction_->arg_begin();
+  Value* vaList = next(bridgeFunction_->arg_begin());
+
+  if (!method_->isStatic())
+    params.push_back(objectArg);
+  for (unsigned i = !method_->isStatic(), e = functionType->getNumParams();
+       i != e; ++i) {
+    const Type* paramType = functionType->getParamType(i);
+    const Type* argType = paramType->getVAArgsPromotedType();
+    Value* arg = new VAArgInst(vaList, argType, "tmp", bb);
+    vaList = new VANextInst(vaList, argType, "", bb);
+    if (paramType != argType)
+      arg = new CastInst(arg, paramType, "tmp", bb);
+    params.push_back(arg);
+  }
+  if (functionType->getReturnType() == Type::VoidTy) {
+    new CallInst(function_, params, "", bb);
+    new ReturnInst(bb);
+  }
+  else {
+    Value* result = new CallInst(function_, params, "result", bb);
+    new ReturnInst(result, bb);
+  }
 }
 
 VMMethod::VMMethod(const VMClass* parent, const Method* method)
@@ -57,3 +95,22 @@
   assert(isDynamicallyBound() && "This should be a dynamically bound method!");
   init();
 }
+
+llvm::Constant* VMMethod::buildMethodDescriptor() const
+{
+  llvm::Constant* fd = ConstantArray::get(getName() + getDescriptor());
+
+  return ConstantExpr::getPtrPtrFromArrayPtr(
+    new GlobalVariable(
+      fd->getType(),
+      true,
+      GlobalVariable::ExternalLinkage,
+      fd,
+      getName() + getDescriptor(),
+      parent_->getResolver()->getModule()));
+}
+
+llvm::Constant* VMMethod::getBridgeFunction() const
+{
+  return bridgeFunction_;
+}


Index: llvm-java/lib/Compiler/VMClass.h
diff -u llvm-java/lib/Compiler/VMClass.h:1.34 llvm-java/lib/Compiler/VMClass.h:1.35
--- llvm-java/lib/Compiler/VMClass.h:1.34	Tue Apr 19 02:11:07 2005
+++ llvm-java/lib/Compiler/VMClass.h	Fri Apr 22 19:12:14 2005
@@ -66,6 +66,10 @@
     llvm::Constant* buildFieldOffsets() const;
     llvm::Constant* buildStaticFieldDescriptors() const;
     llvm::Constant* buildStaticFieldPointers() const;
+    llvm::Constant* buildMethodDescriptors() const;
+    llvm::Constant* buildMethodPointers() const;
+    llvm::Constant* buildStaticMethodDescriptors() const;
+    llvm::Constant* buildStaticMethodPointers() const;
     llvm::Constant* buildClassTypeInfo() const;
 
     const VMField* lookupField(const std::string& name) const;


Index: llvm-java/lib/Compiler/VMClass.cpp
diff -u llvm-java/lib/Compiler/VMClass.cpp:1.45 llvm-java/lib/Compiler/VMClass.cpp:1.46
--- llvm-java/lib/Compiler/VMClass.cpp:1.45	Tue Apr 19 02:17:40 2005
+++ llvm-java/lib/Compiler/VMClass.cpp	Fri Apr 22 19:12:14 2005
@@ -393,6 +393,104 @@
       resolver_->getModule()));
 }
 
+llvm::Constant* VMClass::buildMethodDescriptors() const
+{
+  std::vector<llvm::Constant*> init;
+  init.reserve(getNumStaticMethods() + 1);
+
+  for (unsigned i = 0, e = getNumDynamicMethods(); i != e; ++i) {
+    const VMMethod* method = getDynamicMethod(i);
+    init.push_back(method->buildMethodDescriptor());
+  }
+  // Null terminate.
+  init.push_back(llvm::Constant::getNullValue(PointerType::get(Type::SByteTy)));
+
+  const ArrayType* arrayType =
+    ArrayType::get(init.back()->getType(), init.size());
+
+  return ConstantExpr::getPtrPtrFromArrayPtr(
+    new GlobalVariable(
+      arrayType,
+      true,
+      GlobalVariable::ExternalLinkage,
+      ConstantArray::get(arrayType, init),
+      getName() + "<method_descriptors>",
+      resolver_->getModule()));
+}
+
+llvm::Constant* VMClass::buildMethodPointers() const
+{
+  std::vector<llvm::Constant*> init;
+  init.reserve(getNumStaticMethods());
+
+  const Type* pointerType = PointerType::get(Type::SByteTy);
+  for (unsigned i = 0, e = getNumDynamicMethods(); i != e; ++i) {
+    const VMMethod* method = getDynamicMethod(i);
+    init.push_back(ConstantExpr::getCast(method->getBridgeFunction(),
+                                         pointerType));
+  }
+
+  const ArrayType* arrayType = ArrayType::get(pointerType, init.size());
+
+  return ConstantExpr::getPtrPtrFromArrayPtr(
+    new GlobalVariable(
+      arrayType,
+      true,
+      GlobalVariable::ExternalLinkage,
+      ConstantArray::get(arrayType, init),
+      getName() + "<method_pointers>",
+      resolver_->getModule()));
+}
+
+llvm::Constant* VMClass::buildStaticMethodDescriptors() const
+{
+  std::vector<llvm::Constant*> init;
+  init.reserve(getNumStaticMethods() + 1);
+
+  for (unsigned i = 0, e = getNumStaticMethods(); i != e; ++i) {
+    const VMMethod* method = getStaticMethod(i);
+    init.push_back(method->buildMethodDescriptor());
+  }
+  // Null terminate.
+  init.push_back(llvm::Constant::getNullValue(PointerType::get(Type::SByteTy)));
+
+  const ArrayType* arrayType =
+    ArrayType::get(init.back()->getType(), init.size());
+
+  return ConstantExpr::getPtrPtrFromArrayPtr(
+    new GlobalVariable(
+      arrayType,
+      true,
+      GlobalVariable::ExternalLinkage,
+      ConstantArray::get(arrayType, init),
+      getName() + "<static_method_descriptors>",
+      resolver_->getModule()));
+}
+
+llvm::Constant* VMClass::buildStaticMethodPointers() const
+{
+  std::vector<llvm::Constant*> init;
+  init.reserve(getNumStaticMethods());
+
+  const Type* pointerType = PointerType::get(Type::SByteTy);
+  for (unsigned i = 0, e = getNumStaticMethods(); i != e; ++i) {
+    const VMMethod* method = getStaticMethod(i);
+    init.push_back(ConstantExpr::getCast(method->getBridgeFunction(),
+                                         pointerType));
+  }
+
+  const ArrayType* arrayType = ArrayType::get(pointerType, init.size());
+
+  return ConstantExpr::getPtrPtrFromArrayPtr(
+    new GlobalVariable(
+      arrayType,
+      true,
+      GlobalVariable::ExternalLinkage,
+      ConstantArray::get(arrayType, init),
+      getName() + "<static_method_pointers>",
+      resolver_->getModule()));
+}
+
 llvm::Constant* VMClass::buildClassTypeInfo() const
 {
   std::vector<llvm::Constant*> init;
@@ -424,6 +522,10 @@
   init.push_back(buildFieldOffsets());
   init.push_back(buildStaticFieldDescriptors());
   init.push_back(buildStaticFieldPointers());
+  init.push_back(buildMethodDescriptors());
+  init.push_back(buildMethodPointers());
+  init.push_back(buildStaticMethodDescriptors());
+  init.push_back(buildStaticMethodPointers());
 
   return ConstantStruct::get(init);
 }


Index: llvm-java/lib/Compiler/Resolver.cpp
diff -u llvm-java/lib/Compiler/Resolver.cpp:1.21 llvm-java/lib/Compiler/Resolver.cpp:1.22
--- llvm-java/lib/Compiler/Resolver.cpp:1.21	Sat Apr  2 20:49:44 2005
+++ llvm-java/lib/Compiler/Resolver.cpp	Fri Apr 22 19:12:14 2005
@@ -51,6 +51,10 @@
   //   unsigned* fieldOffsets;
   //   char** staticFieldDescriptors;
   //   void** staticFields;
+  //   char** MethodDescriptors;
+  //   void** Methods;
+  //   char** staticMethodDescriptors;
+  //   void** staticMethods;
   // };
 
   // Compute the type_info type.
@@ -66,6 +70,10 @@
   elements.push_back(PointerType::get(Type::UIntTy));
   elements.push_back(PointerType::get(PointerType::get(Type::SByteTy)));
   elements.push_back(PointerType::get(PointerType::get(Type::SByteTy)));
+  elements.push_back(PointerType::get(PointerType::get(Type::SByteTy)));
+  elements.push_back(PointerType::get(PointerType::get(Type::SByteTy)));
+  elements.push_back(PointerType::get(PointerType::get(Type::SByteTy)));
+  elements.push_back(PointerType::get(PointerType::get(Type::SByteTy)));
   typeInfoType_ = StructType::get(elements);
 
   module_->addTypeName("struct.llvm_java_typeinfo", getTypeInfoType());






More information about the llvm-commits mailing list