JNI up the wazoo

This commit is contained in:
Joel Dice 2007-06-24 20:02:24 -06:00
parent 400b3633d7
commit cd2c1a2836
7 changed files with 86 additions and 64 deletions

View File

@ -50,7 +50,7 @@ interpreter-depends = \
$(src)/common.h \ $(src)/common.h \
$(src)/system.h \ $(src)/system.h \
$(src)/heap.h \ $(src)/heap.h \
$(src)/class_finder.h \ $(src)/class-finder.h \
$(src)/stream.h \ $(src)/stream.h \
$(src)/constants.h \ $(src)/constants.h \
$(src)/vm.h $(src)/vm.h

View File

@ -196,7 +196,7 @@ enum OpCode {
new_ = 0xbb, new_ = 0xbb,
newarray = 0xbc, newarray = 0xbc,
nop = 0x00, nop = 0x00,
pop = 0x57, pop_ = 0x57,
pop2 = 0x58, pop2 = 0x58,
putfield = 0xb5, putfield = 0xb5,
putstatic = 0xb3, putstatic = 0xb3,

View File

@ -1,5 +1,5 @@
#ifndef JNI_H #ifndef JNI_VM_H
#define JNI_H #define JNI_VM_H
#include "stdint.h" #include "stdint.h"
#include "stdarg.h" #include "stdarg.h"
@ -8,6 +8,8 @@
#define JNIIMPORT #define JNIIMPORT
#define JNICALL #define JNICALL
namespace vm {
typedef uint8_t jboolean; typedef uint8_t jboolean;
typedef int8_t jbyte; typedef int8_t jbyte;
typedef uint16_t jchar; typedef uint16_t jchar;
@ -58,7 +60,15 @@ struct JNINativeMethod {
void* function; void* function;
}; };
struct JavaVMVTable;
struct JavaVM { struct JavaVM {
JavaVM(JavaVMVTable* vtable): vtable(vtable) { }
JavaVMVTable* vtable;
};
struct JavaVMVTable {
void* reserved0; void* reserved0;
void* reserved1; void* reserved1;
void* reserved2; void* reserved2;
@ -84,7 +94,15 @@ struct JavaVM {
(JavaVM*, void**, void*); (JavaVM*, void**, void*);
}; };
struct JNIEnvVTable;
struct JNIEnv { struct JNIEnv {
JNIEnv(JNIEnvVTable* vtable): vtable(vtable) { }
JNIEnvVTable* vtable;
};
struct JNIEnvVTable {
void* reserved0; void* reserved0;
void* reserved1; void* reserved1;
void* reserved2; void* reserved2;
@ -1007,4 +1025,6 @@ struct JNIEnv {
(JNIEnv*, jobject); (JNIEnv*, jobject);
}; };
#endif//JNI_H } // namespace vm
#endif//JNI_VM_H

View File

@ -6,6 +6,7 @@
#include "common.h" #include "common.h"
#include "system.h" #include "system.h"
#include "heap.h" #include "heap.h"
#include "class-finder.h"
#include "vm.h" #include "vm.h"
using namespace vm; using namespace vm;

View File

@ -1,10 +1,10 @@
#include "common.h" #include "common.h"
#include "system.h" #include "system.h"
#include "heap.h" #include "heap.h"
#include "class_finder.h" #include "class-finder.h"
#include "stream.h" #include "stream.h"
#include "constants.h" #include "constants.h"
#include "jni.h" #include "jni-vm.h"
#include "vm.h" #include "vm.h"
#define PROTECT(thread, name) \ #define PROTECT(thread, name) \
@ -81,6 +81,7 @@ class Machine {
object builtinMap; object builtinMap;
object types; object types;
bool unsafe; bool unsafe;
JNIEnvVTable jniEnvVTable;
}; };
class Chain { class Chain {
@ -105,7 +106,7 @@ class Chain {
Chain* next; Chain* next;
}; };
class Thread { class Thread : public JNIEnv {
public: public:
enum State { enum State {
NoState, NoState,
@ -156,7 +157,6 @@ class Thread {
Chain* chain; Chain* chain;
object stack[StackSizeInWords]; object stack[StackSizeInWords];
object heap[HeapSizeInWords]; object heap[HeapSizeInWords];
JNIEnv jniEnv;
}; };
#include "type-declarations.cpp" #include "type-declarations.cpp"
@ -212,45 +212,6 @@ expect(Thread* t, bool v)
expect(t->vm->system, 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 uint32_t
hash(const int8_t* s, unsigned length) hash(const int8_t* s, unsigned length)
{ {
@ -2063,8 +2024,7 @@ invokeNative(Thread* t, object method)
unsigned offset = 0; unsigned offset = 0;
sizes[0] = BytesPerWord; sizes[0] = BytesPerWord;
JNIEnv* e = &(t->jniEnv); memcpy(args + offset, &t, BytesPerWord);
memcpy(args + offset, &e, BytesPerWord);
offset += BytesPerWord / 4; offset += BytesPerWord / 4;
for (unsigned i = 0; i < parameterCount; ++i) { for (unsigned i = 0; i < parameterCount; ++i) {
@ -2125,7 +2085,7 @@ invokeNative(Thread* t, object method)
void void
builtinLoadLibrary(JNIEnv* e, jstring nameString) builtinLoadLibrary(JNIEnv* e, jstring nameString)
{ {
Thread* t = static_cast<Thread*>(e->reserved0); Thread* t = static_cast<Thread*>(e);
if (LIKELY(nameString)) { if (LIKELY(nameString)) {
object n = *nameString; object n = *nameString;
@ -2152,7 +2112,7 @@ builtinLoadLibrary(JNIEnv* e, jstring nameString)
jstring jstring
builtinToString(JNIEnv* e, jobject this_) builtinToString(JNIEnv* e, jobject this_)
{ {
Thread* t = static_cast<Thread*>(e->reserved0); Thread* t = static_cast<Thread*>(e);
object s = makeString object s = makeString
(t, "%s@%p", (t, "%s@%p",
@ -2166,7 +2126,8 @@ builtinToString(JNIEnv* e, jobject this_)
jsize jsize
GetStringUTFLength(JNIEnv* e, jstring s) GetStringUTFLength(JNIEnv* e, jstring s)
{ {
Thread* t = static_cast<Thread*>(e->reserved0); Thread* t = static_cast<Thread*>(e);
enter(t, Thread::ActiveState); enter(t, Thread::ActiveState);
jsize length = 0; jsize length = 0;
@ -2184,7 +2145,8 @@ GetStringUTFLength(JNIEnv* e, jstring s)
const char* const char*
GetStringUTFChars(JNIEnv* e, jstring s, jboolean* isCopy) GetStringUTFChars(JNIEnv* e, jstring s, jboolean* isCopy)
{ {
Thread* t = static_cast<Thread*>(e->reserved0); Thread* t = static_cast<Thread*>(e);
enter(t, Thread::ActiveState); enter(t, Thread::ActiveState);
char* chars = 0; char* chars = 0;
@ -2209,11 +2171,56 @@ GetStringUTFChars(JNIEnv* e, jstring s, jboolean* isCopy)
void void
ReleaseStringUTFChars(JNIEnv* e, jstring, const char* chars) ReleaseStringUTFChars(JNIEnv* e, jstring, const char* chars)
{ {
Thread* t = static_cast<Thread*>(e->reserved0); static_cast<Thread*>(e)->vm->system->free(chars);
t->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): Thread::Thread(Machine* m):
JNIEnv(&(m->jniEnvVTable)),
vm(m), vm(m),
next(0), next(0),
child(0), child(0),
@ -2228,12 +2235,6 @@ Thread::Thread(Machine* m):
protector(0), protector(0),
chain(0) chain(0)
{ {
memset(&jniEnv, 0, sizeof(JNIEnv));
jniEnv.reserved0 = this;
jniEnv.GetStringUTFLength = GetStringUTFLength;
jniEnv.GetStringUTFChars = GetStringUTFChars;
jniEnv.ReleaseStringUTFChars = ReleaseStringUTFChars;
if (m->rootThread == 0) { if (m->rootThread == 0) {
m->rootThread = this; m->rootThread = this;
m->unsafe = true; m->unsafe = true;
@ -3461,7 +3462,7 @@ run(Thread* t)
case nop: goto loop; case nop: goto loop;
case vm::pop: { case pop_: {
-- sp; -- sp;
} goto loop; } goto loop;

View File

@ -3,7 +3,7 @@
#include "system.h" #include "system.h"
#include "heap.h" #include "heap.h"
#include "class_finder.h" #include "class-finder.h"
namespace vm { namespace vm {