From cd2c1a28365e321b842e007c7a257186570af676 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Sun, 24 Jun 2007 20:02:24 -0600 Subject: [PATCH] JNI up the wazoo --- makefile | 2 +- src/{class_finder.h => class-finder.h} | 0 src/constants.h | 2 +- src/{jni.h => jni-vm.h} | 26 +++++- src/main.cpp | 1 + src/vm.cpp | 117 +++++++++++++------------ src/vm.h | 2 +- 7 files changed, 86 insertions(+), 64 deletions(-) rename src/{class_finder.h => class-finder.h} (100%) rename src/{jni.h => jni-vm.h} (98%) diff --git a/makefile b/makefile index 3378690e2e..41261378fe 100644 --- a/makefile +++ b/makefile @@ -50,7 +50,7 @@ interpreter-depends = \ $(src)/common.h \ $(src)/system.h \ $(src)/heap.h \ - $(src)/class_finder.h \ + $(src)/class-finder.h \ $(src)/stream.h \ $(src)/constants.h \ $(src)/vm.h diff --git a/src/class_finder.h b/src/class-finder.h similarity index 100% rename from src/class_finder.h rename to src/class-finder.h diff --git a/src/constants.h b/src/constants.h index e2b5eb48a0..91ab9855d1 100644 --- a/src/constants.h +++ b/src/constants.h @@ -196,7 +196,7 @@ enum OpCode { new_ = 0xbb, newarray = 0xbc, nop = 0x00, - pop = 0x57, + pop_ = 0x57, pop2 = 0x58, putfield = 0xb5, putstatic = 0xb3, diff --git a/src/jni.h b/src/jni-vm.h similarity index 98% rename from src/jni.h rename to src/jni-vm.h index 8e2a59351b..7a71f9ddae 100644 --- a/src/jni.h +++ b/src/jni-vm.h @@ -1,5 +1,5 @@ -#ifndef JNI_H -#define JNI_H +#ifndef JNI_VM_H +#define JNI_VM_H #include "stdint.h" #include "stdarg.h" @@ -8,6 +8,8 @@ #define JNIIMPORT #define JNICALL +namespace vm { + typedef uint8_t jboolean; typedef int8_t jbyte; typedef uint16_t jchar; @@ -58,7 +60,15 @@ struct JNINativeMethod { void* function; }; +struct JavaVMVTable; + struct JavaVM { + JavaVM(JavaVMVTable* vtable): vtable(vtable) { } + + JavaVMVTable* vtable; +}; + +struct JavaVMVTable { void* reserved0; void* reserved1; void* reserved2; @@ -84,7 +94,15 @@ struct JavaVM { (JavaVM*, void**, void*); }; +struct JNIEnvVTable; + struct JNIEnv { + JNIEnv(JNIEnvVTable* vtable): vtable(vtable) { } + + JNIEnvVTable* vtable; +}; + +struct JNIEnvVTable { void* reserved0; void* reserved1; void* reserved2; @@ -1007,4 +1025,6 @@ struct JNIEnv { (JNIEnv*, jobject); }; -#endif//JNI_H +} // namespace vm + +#endif//JNI_VM_H diff --git a/src/main.cpp b/src/main.cpp index 609edbc41f..ffb14b3bd4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,6 +6,7 @@ #include "common.h" #include "system.h" #include "heap.h" +#include "class-finder.h" #include "vm.h" using namespace vm; diff --git a/src/vm.cpp b/src/vm.cpp index 8b9f63f25c..cdd464bcae 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -1,10 +1,10 @@ #include "common.h" #include "system.h" #include "heap.h" -#include "class_finder.h" +#include "class-finder.h" #include "stream.h" #include "constants.h" -#include "jni.h" +#include "jni-vm.h" #include "vm.h" #define PROTECT(thread, name) \ @@ -81,6 +81,7 @@ class Machine { object builtinMap; object types; bool unsafe; + JNIEnvVTable jniEnvVTable; }; class Chain { @@ -105,7 +106,7 @@ class Chain { Chain* next; }; -class Thread { +class Thread : public JNIEnv { public: enum State { NoState, @@ -156,7 +157,6 @@ class Thread { Chain* chain; object stack[StackSizeInWords]; object heap[HeapSizeInWords]; - JNIEnv jniEnv; }; #include "type-declarations.cpp" @@ -212,45 +212,6 @@ expect(Thread* t, bool v) expect(t->vm->system, v); } -Machine::Machine(System* system, Heap* heap, ClassFinder* classFinder): - system(system), - heap(heap), - classFinder(classFinder), - rootThread(0), - exclusive(0), - activeCount(0), - liveCount(0), - stateLock(0), - heapLock(0), - classLock(0), - libraries(0), - classMap(0), - bootstrapClassMap(0), - builtinMap(0), - types(0), - unsafe(false) -{ - if (not system->success(system->make(&stateLock)) or - not system->success(system->make(&heapLock)) or - not system->success(system->make(&classLock))) - { - system->abort(); - } -} - -void -Machine::dispose() -{ - stateLock->dispose(); - heapLock->dispose(); - classLock->dispose(); - libraries->dispose(); - - if (rootThread) { - rootThread->dispose(); - } -} - uint32_t hash(const int8_t* s, unsigned length) { @@ -2063,8 +2024,7 @@ invokeNative(Thread* t, object method) unsigned offset = 0; sizes[0] = BytesPerWord; - JNIEnv* e = &(t->jniEnv); - memcpy(args + offset, &e, BytesPerWord); + memcpy(args + offset, &t, BytesPerWord); offset += BytesPerWord / 4; for (unsigned i = 0; i < parameterCount; ++i) { @@ -2125,7 +2085,7 @@ invokeNative(Thread* t, object method) void builtinLoadLibrary(JNIEnv* e, jstring nameString) { - Thread* t = static_cast(e->reserved0); + Thread* t = static_cast(e); if (LIKELY(nameString)) { object n = *nameString; @@ -2152,7 +2112,7 @@ builtinLoadLibrary(JNIEnv* e, jstring nameString) jstring builtinToString(JNIEnv* e, jobject this_) { - Thread* t = static_cast(e->reserved0); + Thread* t = static_cast(e); object s = makeString (t, "%s@%p", @@ -2166,7 +2126,8 @@ builtinToString(JNIEnv* e, jobject this_) jsize GetStringUTFLength(JNIEnv* e, jstring s) { - Thread* t = static_cast(e->reserved0); + Thread* t = static_cast(e); + enter(t, Thread::ActiveState); jsize length = 0; @@ -2184,7 +2145,8 @@ GetStringUTFLength(JNIEnv* e, jstring s) const char* GetStringUTFChars(JNIEnv* e, jstring s, jboolean* isCopy) { - Thread* t = static_cast(e->reserved0); + Thread* t = static_cast(e); + enter(t, Thread::ActiveState); char* chars = 0; @@ -2209,11 +2171,56 @@ GetStringUTFChars(JNIEnv* e, jstring s, jboolean* isCopy) void ReleaseStringUTFChars(JNIEnv* e, jstring, const char* chars) { - Thread* t = static_cast(e->reserved0); - t->vm->system->free(chars); + static_cast(e)->vm->system->free(chars); +} + +Machine::Machine(System* system, Heap* heap, ClassFinder* classFinder): + system(system), + heap(heap), + classFinder(classFinder), + rootThread(0), + exclusive(0), + activeCount(0), + liveCount(0), + stateLock(0), + heapLock(0), + classLock(0), + libraries(0), + classMap(0), + bootstrapClassMap(0), + builtinMap(0), + types(0), + unsafe(false) +{ + memset(&jniEnvVTable, 0, sizeof(JNIEnvVTable)); + + jniEnvVTable.GetStringUTFLength = GetStringUTFLength; + jniEnvVTable.GetStringUTFChars = GetStringUTFChars; + jniEnvVTable.ReleaseStringUTFChars = ReleaseStringUTFChars; + + if (not system->success(system->make(&stateLock)) or + not system->success(system->make(&heapLock)) or + not system->success(system->make(&classLock))) + { + system->abort(); + } +} + +void +Machine::dispose() +{ + stateLock->dispose(); + heapLock->dispose(); + classLock->dispose(); + libraries->dispose(); + + if (rootThread) { + rootThread->dispose(); + } } Thread::Thread(Machine* m): + JNIEnv(&(m->jniEnvVTable)), vm(m), next(0), child(0), @@ -2228,12 +2235,6 @@ Thread::Thread(Machine* m): protector(0), chain(0) { - memset(&jniEnv, 0, sizeof(JNIEnv)); - jniEnv.reserved0 = this; - jniEnv.GetStringUTFLength = GetStringUTFLength; - jniEnv.GetStringUTFChars = GetStringUTFChars; - jniEnv.ReleaseStringUTFChars = ReleaseStringUTFChars; - if (m->rootThread == 0) { m->rootThread = this; m->unsafe = true; @@ -3461,7 +3462,7 @@ run(Thread* t) case nop: goto loop; - case vm::pop: { + case pop_: { -- sp; } goto loop; diff --git a/src/vm.h b/src/vm.h index 5c7afcee9d..ecb81bf02c 100644 --- a/src/vm.h +++ b/src/vm.h @@ -3,7 +3,7 @@ #include "system.h" #include "heap.h" -#include "class_finder.h" +#include "class-finder.h" namespace vm {