2007-07-06 23:50:26 +00:00
|
|
|
#include "jnienv.h"
|
|
|
|
#include "builtin.h"
|
|
|
|
#include "machine.h"
|
2007-07-14 17:31:01 +00:00
|
|
|
#include "stream.h"
|
|
|
|
#include "constants.h"
|
2007-07-06 23:50:26 +00:00
|
|
|
|
|
|
|
using namespace vm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2007-07-07 18:09:16 +00:00
|
|
|
bool
|
|
|
|
find(Thread* t, Thread* o)
|
|
|
|
{
|
|
|
|
if (t == o) return true;
|
|
|
|
|
|
|
|
for (Thread* p = t->peer; p; p = p->peer) {
|
|
|
|
if (p == o) return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (t->child) return find(t->child, o);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
join(Thread* t, Thread* o)
|
|
|
|
{
|
|
|
|
if (t != o) {
|
|
|
|
o->systemThread->join();
|
2007-07-18 01:33:00 +00:00
|
|
|
o->state = Thread::JoinedState;
|
2007-07-07 18:09:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dispose(Thread* t, Thread* o, bool remove)
|
|
|
|
{
|
|
|
|
if (remove) {
|
|
|
|
if (o->parent) {
|
|
|
|
if (o->child) {
|
|
|
|
o->parent->child = o->child;
|
|
|
|
if (o->peer) {
|
|
|
|
o->peer->peer = o->child->peer;
|
|
|
|
o->child->peer = o->peer;
|
|
|
|
}
|
|
|
|
} else if (o->peer) {
|
|
|
|
o->parent->child = o->peer;
|
|
|
|
} else {
|
|
|
|
o->parent->child = 0;
|
|
|
|
}
|
|
|
|
} else if (o->child) {
|
|
|
|
t->vm->rootThread = o->child;
|
|
|
|
if (o->peer) {
|
|
|
|
o->peer->peer = o->child->peer;
|
|
|
|
o->child->peer = o->peer;
|
|
|
|
}
|
|
|
|
} else if (o->peer) {
|
|
|
|
t->vm->rootThread = o->peer;
|
|
|
|
} else {
|
|
|
|
abort(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(t, not find(t->vm->rootThread, o));
|
|
|
|
}
|
|
|
|
|
|
|
|
o->dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
joinAll(Thread* m, Thread* o)
|
|
|
|
{
|
|
|
|
for (Thread* p = o->child; p;) {
|
|
|
|
Thread* child = p;
|
|
|
|
p = p->peer;
|
|
|
|
joinAll(m, child);
|
|
|
|
}
|
|
|
|
|
|
|
|
join(m, o);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
disposeAll(Thread* m, Thread* o)
|
|
|
|
{
|
|
|
|
for (Thread* p = o->child; p;) {
|
|
|
|
Thread* child = p;
|
|
|
|
p = p->peer;
|
|
|
|
disposeAll(m, child);
|
|
|
|
}
|
|
|
|
|
|
|
|
dispose(m, o, false);
|
|
|
|
}
|
|
|
|
|
2007-07-06 23:50:26 +00:00
|
|
|
void
|
2007-07-07 18:09:16 +00:00
|
|
|
killZombies(Thread* t, Thread* o)
|
2007-07-06 23:50:26 +00:00
|
|
|
{
|
2007-07-07 18:09:16 +00:00
|
|
|
for (Thread* p = o->child; p;) {
|
|
|
|
Thread* child = p;
|
|
|
|
p = p->peer;
|
|
|
|
killZombies(t, child);
|
|
|
|
}
|
|
|
|
|
2007-07-18 01:33:00 +00:00
|
|
|
switch (o->state) {
|
|
|
|
case Thread::ZombieState:
|
2007-07-07 18:09:16 +00:00
|
|
|
join(t, o);
|
2007-07-18 01:33:00 +00:00
|
|
|
// fall through
|
|
|
|
|
|
|
|
case Thread::JoinedState:
|
2007-07-07 18:09:16 +00:00
|
|
|
dispose(t, o, true);
|
2007-07-18 01:33:00 +00:00
|
|
|
|
|
|
|
default: break;
|
2007-07-07 18:09:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-07 23:47:35 +00:00
|
|
|
void
|
|
|
|
visitRoots(Thread* t, Heap::Visitor* v)
|
|
|
|
{
|
|
|
|
if (t->state != Thread::ZombieState) {
|
|
|
|
t->heapIndex = 0;
|
|
|
|
|
|
|
|
v->visit(&(t->javaThread));
|
|
|
|
v->visit(&(t->code));
|
|
|
|
v->visit(&(t->exception));
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < t->sp; ++i) {
|
|
|
|
if (t->stack[i * 2] == ObjectTag) {
|
|
|
|
v->visit(reinterpret_cast<object*>(t->stack + (i * 2) + 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Thread::Protector* p = t->protector; p; p = p->next) {
|
|
|
|
v->visit(p->p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Thread* c = t->child; c; c = c->peer) {
|
|
|
|
visitRoots(c, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-20 01:07:30 +00:00
|
|
|
void
|
|
|
|
referenceTargetUnreachable(Thread* t, object* p, Heap::Visitor* v)
|
|
|
|
{
|
2007-07-21 17:50:26 +00:00
|
|
|
// fprintf(stderr, "target %p unreachable for reference %p\n",
|
|
|
|
// jreferenceTarget(t, *p), *p);
|
|
|
|
|
2007-07-20 01:07:30 +00:00
|
|
|
v->visit(p);
|
|
|
|
jreferenceTarget(t, *p) = 0;
|
|
|
|
|
2007-07-20 03:18:25 +00:00
|
|
|
if (jreferenceQueue(t, *p)
|
|
|
|
and t->vm->heap->status(jreferenceQueue(t, *p)) != Heap::Unreachable)
|
|
|
|
{
|
2007-07-20 01:07:30 +00:00
|
|
|
// queue is reachable - add the reference
|
|
|
|
|
|
|
|
v->visit(&jreferenceQueue(t, *p));
|
|
|
|
|
|
|
|
object q = jreferenceQueue(t, *p);
|
|
|
|
|
|
|
|
set(t, jreferenceJnext(t, *p), *p);
|
|
|
|
if (referenceQueueFront(t, q)) {
|
|
|
|
set(t, jreferenceJnext(t, referenceQueueRear(t, q)), *p);
|
|
|
|
} else {
|
|
|
|
set(t, referenceQueueFront(t, q), *p);
|
|
|
|
}
|
|
|
|
set(t, referenceQueueRear(t, q), *p);
|
|
|
|
|
|
|
|
jreferenceQueue(t, *p) = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-20 03:18:25 +00:00
|
|
|
void
|
|
|
|
referenceUnreachable(Thread* t, object* p, Heap::Visitor* v)
|
|
|
|
{
|
2007-07-21 17:50:26 +00:00
|
|
|
// fprintf(stderr, "reference %p unreachable\n",
|
|
|
|
// *p);
|
|
|
|
|
2007-07-20 03:18:25 +00:00
|
|
|
if (jreferenceQueue(t, *p)
|
|
|
|
and t->vm->heap->status(jreferenceQueue(t, *p)) != Heap::Unreachable)
|
|
|
|
{
|
|
|
|
// queue is reachable - add the reference
|
|
|
|
referenceTargetUnreachable(t, p, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-20 01:07:30 +00:00
|
|
|
void
|
|
|
|
referenceTargetReachable(Thread* t, object* p, Heap::Visitor* v)
|
|
|
|
{
|
2007-07-21 17:50:26 +00:00
|
|
|
// fprintf(stderr, "target %p reachable for reference %p\n",
|
|
|
|
// jreferenceTarget(t, *p), *p);
|
|
|
|
|
2007-07-20 01:07:30 +00:00
|
|
|
v->visit(p);
|
|
|
|
v->visit(&jreferenceTarget(t, *p));
|
|
|
|
|
|
|
|
if (t->vm->heap->status(jreferenceQueue(t, *p)) == Heap::Unreachable) {
|
|
|
|
jreferenceQueue(t, *p) = 0;
|
|
|
|
} else {
|
|
|
|
v->visit(&jreferenceQueue(t, *p));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-10 01:43:43 +00:00
|
|
|
void
|
|
|
|
postVisit(Thread* t, Heap::Visitor* v)
|
|
|
|
{
|
|
|
|
Machine* m = t->vm;
|
|
|
|
|
|
|
|
object firstNewTenuredFinalizer = 0;
|
|
|
|
object lastNewTenuredFinalizer = 0;
|
|
|
|
|
|
|
|
for (object* p = &(m->finalizers); *p;) {
|
|
|
|
v->visit(p);
|
|
|
|
|
|
|
|
if (m->heap->status(finalizerTarget(t, *p)) == Heap::Unreachable) {
|
|
|
|
// target is unreachable - queue it up for finalization
|
|
|
|
|
|
|
|
v->visit(&finalizerTarget(t, *p));
|
|
|
|
|
|
|
|
object finalizer = *p;
|
|
|
|
*p = finalizerNext(t, finalizer);
|
|
|
|
finalizerNext(t, finalizer) = m->finalizeQueue;
|
|
|
|
m->finalizeQueue = finalizer;
|
|
|
|
} else {
|
|
|
|
// target is reachable
|
|
|
|
|
|
|
|
v->visit(&finalizerTarget(t, *p));
|
|
|
|
|
|
|
|
if (m->heap->status(*p) == Heap::Tenured) {
|
|
|
|
// the finalizer is tenured, so we remove it from
|
|
|
|
// m->finalizers and later add it to m->tenuredFinalizers
|
|
|
|
|
|
|
|
if (lastNewTenuredFinalizer == 0) {
|
|
|
|
lastNewTenuredFinalizer = *p;
|
|
|
|
}
|
|
|
|
|
|
|
|
object finalizer = *p;
|
|
|
|
*p = finalizerNext(t, finalizer);
|
|
|
|
finalizerNext(t, finalizer) = firstNewTenuredFinalizer;
|
|
|
|
firstNewTenuredFinalizer = finalizer;
|
|
|
|
} else {
|
|
|
|
p = &finalizerNext(t, *p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
object firstNewTenuredWeakReference = 0;
|
|
|
|
object lastNewTenuredWeakReference = 0;
|
|
|
|
|
|
|
|
for (object* p = &(m->weakReferences); *p;) {
|
|
|
|
if (m->heap->status(*p) == Heap::Unreachable) {
|
2007-07-20 03:18:25 +00:00
|
|
|
// reference is unreachable
|
2007-07-10 01:43:43 +00:00
|
|
|
|
2007-07-20 03:18:25 +00:00
|
|
|
referenceUnreachable(t, p, v);
|
2007-07-10 01:43:43 +00:00
|
|
|
*p = jreferenceNext(t, *p);
|
|
|
|
} else if (m->heap->status(jreferenceTarget(t, *p)) == Heap::Unreachable) {
|
2007-07-20 03:18:25 +00:00
|
|
|
// target is unreachable
|
2007-07-10 01:43:43 +00:00
|
|
|
|
2007-07-20 01:07:30 +00:00
|
|
|
referenceTargetUnreachable(t, p, v);
|
2007-07-10 01:43:43 +00:00
|
|
|
*p = jreferenceNext(t, *p);
|
|
|
|
} else {
|
|
|
|
// both reference and target are reachable
|
|
|
|
|
2007-07-20 01:07:30 +00:00
|
|
|
referenceTargetReachable(t, p, v);
|
2007-07-10 01:43:43 +00:00
|
|
|
|
|
|
|
if (m->heap->status(*p) == Heap::Tenured) {
|
|
|
|
// the reference is tenured, so we remove it from
|
|
|
|
// m->weakReferences and later add it to
|
|
|
|
// m->tenuredWeakReferences
|
|
|
|
|
|
|
|
if (lastNewTenuredWeakReference == 0) {
|
|
|
|
lastNewTenuredWeakReference = *p;
|
|
|
|
}
|
|
|
|
|
|
|
|
object reference = *p;
|
|
|
|
*p = jreferenceNext(t, reference);
|
|
|
|
jreferenceNext(t, reference) = firstNewTenuredWeakReference;
|
|
|
|
firstNewTenuredWeakReference = reference;
|
|
|
|
} else {
|
|
|
|
p = &jreferenceNext(t, *p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m->heap->collectionType() == Heap::MajorCollection) {
|
|
|
|
for (object* p = &(m->tenuredFinalizers); *p;) {
|
|
|
|
v->visit(p);
|
|
|
|
|
|
|
|
if (m->heap->status(finalizerTarget(t, *p)) == Heap::Unreachable) {
|
|
|
|
// target is unreachable - queue it up for finalization
|
|
|
|
|
|
|
|
v->visit(&finalizerTarget(t, *p));
|
|
|
|
|
|
|
|
object finalizer = *p;
|
|
|
|
*p = finalizerNext(t, finalizer);
|
|
|
|
finalizerNext(t, finalizer) = m->finalizeQueue;
|
|
|
|
m->finalizeQueue = finalizer;
|
|
|
|
} else {
|
|
|
|
// target is reachable
|
|
|
|
|
|
|
|
v->visit(&finalizerTarget(t, *p));
|
|
|
|
|
|
|
|
p = &finalizerNext(t, *p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (object* p = &(m->tenuredWeakReferences); *p;) {
|
|
|
|
if (m->heap->status(*p) == Heap::Unreachable) {
|
2007-07-20 03:18:25 +00:00
|
|
|
// reference is unreachable
|
2007-07-10 01:43:43 +00:00
|
|
|
|
2007-07-20 03:18:25 +00:00
|
|
|
referenceUnreachable(t, p, v);
|
2007-07-10 01:43:43 +00:00
|
|
|
*p = jreferenceNext(t, *p);
|
|
|
|
} else if (m->heap->status(jreferenceTarget(t, *p))
|
|
|
|
== Heap::Unreachable)
|
|
|
|
{
|
2007-07-20 03:18:25 +00:00
|
|
|
// target is unreachable
|
2007-07-10 01:43:43 +00:00
|
|
|
|
2007-07-20 01:07:30 +00:00
|
|
|
referenceTargetUnreachable(t, p, v);
|
2007-07-10 01:43:43 +00:00
|
|
|
*p = jreferenceNext(t, *p);
|
|
|
|
} else {
|
2007-07-20 03:18:25 +00:00
|
|
|
// both reference and target are reachable
|
2007-07-10 01:43:43 +00:00
|
|
|
|
2007-07-20 01:07:30 +00:00
|
|
|
referenceTargetReachable(t, p, v);
|
2007-07-10 01:43:43 +00:00
|
|
|
p = &jreferenceNext(t, *p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lastNewTenuredFinalizer) {
|
|
|
|
finalizerNext(t, lastNewTenuredFinalizer) = m->tenuredFinalizers;
|
2007-07-11 13:35:28 +00:00
|
|
|
m->tenuredFinalizers = firstNewTenuredFinalizer;
|
2007-07-10 01:43:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lastNewTenuredWeakReference) {
|
|
|
|
jreferenceNext(t, lastNewTenuredWeakReference) = m->tenuredWeakReferences;
|
2007-07-11 13:35:28 +00:00
|
|
|
m->tenuredWeakReferences = firstNewTenuredWeakReference;
|
2007-07-10 01:43:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-07 23:47:35 +00:00
|
|
|
void
|
|
|
|
postCollect(Thread* t)
|
|
|
|
{
|
2007-07-16 23:58:37 +00:00
|
|
|
#ifdef VM_STRESS
|
|
|
|
t->vm->system->free(t->heap);
|
|
|
|
t->heap = static_cast<object*>
|
|
|
|
(t->vm->system->allocate(Thread::HeapSizeInBytes));
|
|
|
|
#endif
|
|
|
|
|
2007-07-07 23:47:35 +00:00
|
|
|
if (t->large) {
|
|
|
|
t->vm->system->free(t->large);
|
|
|
|
t->large = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Thread* c = t->child; c; c = c->peer) {
|
|
|
|
postCollect(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-06 23:50:26 +00:00
|
|
|
object
|
|
|
|
makeByteArray(Thread* t, const char* format, va_list a)
|
|
|
|
{
|
|
|
|
static const unsigned Size = 256;
|
|
|
|
char buffer[Size];
|
|
|
|
|
|
|
|
vsnprintf(buffer, Size - 1, format, a);
|
|
|
|
|
|
|
|
object s = makeByteArray(t, strlen(buffer) + 1, false);
|
|
|
|
memcpy(&byteArrayBody(t, s, 0), buffer, byteArrayLength(t, s));
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2007-07-14 17:31:01 +00:00
|
|
|
unsigned
|
|
|
|
mangledSize(int8_t c)
|
|
|
|
{
|
|
|
|
switch (c) {
|
|
|
|
case '_':
|
|
|
|
case ';':
|
|
|
|
case '[':
|
|
|
|
return 2;
|
|
|
|
|
|
|
|
case '$':
|
|
|
|
return 6;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
mangle(int8_t c, int8_t* dst)
|
|
|
|
{
|
|
|
|
switch (c) {
|
|
|
|
case '/':
|
|
|
|
dst[0] = '_';
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case '_':
|
|
|
|
dst[0] = '_';
|
|
|
|
dst[1] = '1';
|
|
|
|
return 2;
|
|
|
|
|
|
|
|
case ';':
|
|
|
|
dst[0] = '_';
|
|
|
|
dst[1] = '2';
|
|
|
|
return 2;
|
|
|
|
|
|
|
|
case '[':
|
|
|
|
dst[0] = '_';
|
|
|
|
dst[1] = '3';
|
|
|
|
return 2;
|
|
|
|
|
|
|
|
case '$':
|
|
|
|
memcpy(dst, "_00024", 6);
|
|
|
|
return 6;
|
|
|
|
|
|
|
|
default:
|
|
|
|
dst[0] = c;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
object
|
|
|
|
makeJNIName(Thread* t, object method, bool decorate)
|
|
|
|
{
|
|
|
|
unsigned size = 5;
|
|
|
|
object className = ::className(t, methodClass(t, method));
|
|
|
|
PROTECT(t, className);
|
|
|
|
for (unsigned i = 0; i < byteArrayLength(t, className) - 1; ++i) {
|
|
|
|
size += mangledSize(byteArrayBody(t, className, i));
|
|
|
|
}
|
|
|
|
|
|
|
|
++ size;
|
|
|
|
|
|
|
|
object methodName = ::methodName(t, method);
|
|
|
|
PROTECT(t, methodName);
|
|
|
|
for (unsigned i = 0; i < byteArrayLength(t, methodName) - 1; ++i) {
|
|
|
|
size += mangledSize(byteArrayBody(t, methodName, i));
|
|
|
|
}
|
|
|
|
|
|
|
|
object methodSpec = ::methodSpec(t, method);
|
|
|
|
PROTECT(t, methodSpec);
|
|
|
|
if (decorate) {
|
|
|
|
size += 2;
|
|
|
|
for (unsigned i = 1; i < byteArrayLength(t, methodSpec) - 1
|
|
|
|
and byteArrayBody(t, methodSpec, i) != ')'; ++i)
|
|
|
|
{
|
|
|
|
size += mangledSize(byteArrayBody(t, methodSpec, i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
object name = makeByteArray(t, size + 1, false);
|
|
|
|
unsigned index = 0;
|
|
|
|
|
|
|
|
memcpy(&byteArrayBody(t, name, index), "Java_", 5);
|
|
|
|
index += 5;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < byteArrayLength(t, className) - 1; ++i) {
|
|
|
|
index += mangle(byteArrayBody(t, className, i),
|
|
|
|
&byteArrayBody(t, name, index));
|
|
|
|
}
|
|
|
|
|
|
|
|
byteArrayBody(t, name, index++) = '_';
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < byteArrayLength(t, methodName) - 1; ++i) {
|
|
|
|
index += mangle(byteArrayBody(t, methodName, i),
|
|
|
|
&byteArrayBody(t, name, index));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (decorate) {
|
|
|
|
byteArrayBody(t, name, index++) = '_';
|
|
|
|
byteArrayBody(t, name, index++) = '_';
|
|
|
|
for (unsigned i = 1; i < byteArrayLength(t, methodSpec) - 1
|
|
|
|
and byteArrayBody(t, methodSpec, i) != ')'; ++i)
|
|
|
|
{
|
2007-07-27 00:06:05 +00:00
|
|
|
index += mangle(byteArrayBody(t, methodSpec, i),
|
2007-07-14 17:31:01 +00:00
|
|
|
&byteArrayBody(t, name, index));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
byteArrayBody(t, name, index++) = 0;
|
|
|
|
|
|
|
|
assert(t, index == size + 1);
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
object
|
|
|
|
parsePool(Thread* t, Stream& s)
|
|
|
|
{
|
|
|
|
unsigned poolCount = s.read2() - 1;
|
|
|
|
object pool = makeArray(t, poolCount, true);
|
|
|
|
|
|
|
|
PROTECT(t, pool);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < poolCount; ++i) {
|
|
|
|
unsigned c = s.read1();
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case CONSTANT_Integer: {
|
|
|
|
object value = makeInt(t, s.read4());
|
|
|
|
set(t, arrayBody(t, pool, i), value);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CONSTANT_Float: {
|
|
|
|
object value = makeFloat(t, s.readFloat());
|
|
|
|
set(t, arrayBody(t, pool, i), value);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CONSTANT_Long: {
|
|
|
|
object value = makeLong(t, s.read8());
|
|
|
|
set(t, arrayBody(t, pool, i), value);
|
|
|
|
++i;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CONSTANT_Double: {
|
|
|
|
object value = makeLong(t, s.readDouble());
|
|
|
|
set(t, arrayBody(t, pool, i), value);
|
|
|
|
++i;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CONSTANT_Utf8: {
|
|
|
|
unsigned length = s.read2();
|
|
|
|
object value = makeByteArray(t, length + 1, false);
|
|
|
|
s.read(reinterpret_cast<uint8_t*>(&byteArrayBody(t, value, 0)), length);
|
|
|
|
byteArrayBody(t, value, length) = 0;
|
|
|
|
set(t, arrayBody(t, pool, i), value);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CONSTANT_Class: {
|
|
|
|
object value = makeIntArray(t, 2, false);
|
|
|
|
intArrayBody(t, value, 0) = c;
|
|
|
|
intArrayBody(t, value, 1) = s.read2();
|
|
|
|
set(t, arrayBody(t, pool, i), value);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CONSTANT_String: {
|
|
|
|
object value = makeIntArray(t, 2, false);
|
|
|
|
intArrayBody(t, value, 0) = c;
|
|
|
|
intArrayBody(t, value, 1) = s.read2();
|
|
|
|
set(t, arrayBody(t, pool, i), value);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CONSTANT_NameAndType: {
|
|
|
|
object value = makeIntArray(t, 3, false);
|
|
|
|
intArrayBody(t, value, 0) = c;
|
|
|
|
intArrayBody(t, value, 1) = s.read2();
|
|
|
|
intArrayBody(t, value, 2) = s.read2();
|
|
|
|
set(t, arrayBody(t, pool, i), value);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CONSTANT_Fieldref:
|
|
|
|
case CONSTANT_Methodref:
|
|
|
|
case CONSTANT_InterfaceMethodref: {
|
|
|
|
object value = makeIntArray(t, 3, false);
|
|
|
|
intArrayBody(t, value, 0) = c;
|
|
|
|
intArrayBody(t, value, 1) = s.read2();
|
|
|
|
intArrayBody(t, value, 2) = s.read2();
|
|
|
|
set(t, arrayBody(t, pool, i), value);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
default: abort(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < poolCount; ++i) {
|
|
|
|
object o = arrayBody(t, pool, i);
|
|
|
|
if (o and objectClass(t, o)
|
|
|
|
== arrayBody(t, t->vm->types, Machine::IntArrayType))
|
|
|
|
{
|
|
|
|
switch (intArrayBody(t, o, 0)) {
|
|
|
|
case CONSTANT_Class: {
|
|
|
|
set(t, arrayBody(t, pool, i),
|
|
|
|
arrayBody(t, pool, intArrayBody(t, o, 1) - 1));
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CONSTANT_String: {
|
|
|
|
object bytes = arrayBody(t, pool, intArrayBody(t, o, 1) - 1);
|
|
|
|
object value = makeString
|
|
|
|
(t, bytes, 0, byteArrayLength(t, bytes) - 1, 0);
|
|
|
|
set(t, arrayBody(t, pool, i), value);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CONSTANT_NameAndType: {
|
|
|
|
object name = arrayBody(t, pool, intArrayBody(t, o, 1) - 1);
|
|
|
|
object type = arrayBody(t, pool, intArrayBody(t, o, 2) - 1);
|
|
|
|
object value = makePair(t, name, type);
|
|
|
|
set(t, arrayBody(t, pool, i), value);
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < poolCount; ++i) {
|
|
|
|
object o = arrayBody(t, pool, i);
|
|
|
|
if (o and objectClass(t, o)
|
|
|
|
== arrayBody(t, t->vm->types, Machine::IntArrayType))
|
|
|
|
{
|
|
|
|
switch (intArrayBody(t, o, 0)) {
|
|
|
|
case CONSTANT_Fieldref:
|
|
|
|
case CONSTANT_Methodref:
|
|
|
|
case CONSTANT_InterfaceMethodref: {
|
|
|
|
object c = arrayBody(t, pool, intArrayBody(t, o, 1) - 1);
|
|
|
|
object nameAndType = arrayBody(t, pool, intArrayBody(t, o, 2) - 1);
|
|
|
|
object value = makeReference
|
|
|
|
(t, c, pairFirst(t, nameAndType), pairSecond(t, nameAndType));
|
|
|
|
set(t, arrayBody(t, pool, i), value);
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pool;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
addInterfaces(Thread* t, object class_, object map)
|
|
|
|
{
|
|
|
|
object table = classInterfaceTable(t, class_);
|
|
|
|
if (table) {
|
|
|
|
unsigned increment = 2;
|
|
|
|
if (classFlags(t, class_) & ACC_INTERFACE) {
|
|
|
|
increment = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
PROTECT(t, map);
|
|
|
|
PROTECT(t, table);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < arrayLength(t, table); i += increment) {
|
|
|
|
object interface = arrayBody(t, table, i);
|
|
|
|
object name = className(t, interface);
|
|
|
|
hashMapInsertMaybe(t, map, name, interface, byteArrayHash,
|
|
|
|
byteArrayEqual);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
parseInterfaceTable(Thread* t, Stream& s, object class_, object pool)
|
|
|
|
{
|
|
|
|
PROTECT(t, class_);
|
|
|
|
PROTECT(t, pool);
|
|
|
|
|
|
|
|
object map = makeHashMap(t, NormalMap, 0, 0);
|
|
|
|
PROTECT(t, map);
|
|
|
|
|
|
|
|
if (classSuper(t, class_)) {
|
|
|
|
addInterfaces(t, classSuper(t, class_), map);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned count = s.read2();
|
|
|
|
for (unsigned i = 0; i < count; ++i) {
|
|
|
|
object name = arrayBody(t, pool, s.read2() - 1);
|
|
|
|
PROTECT(t, name);
|
|
|
|
|
|
|
|
object interface = resolveClass(t, name);
|
|
|
|
PROTECT(t, interface);
|
|
|
|
|
|
|
|
hashMapInsertMaybe(t, map, name, interface, byteArrayHash, byteArrayEqual);
|
|
|
|
|
|
|
|
addInterfaces(t, interface, map);
|
|
|
|
}
|
|
|
|
|
|
|
|
object interfaceTable = 0;
|
|
|
|
if (hashMapSize(t, map)) {
|
|
|
|
unsigned length = hashMapSize(t, map) ;
|
|
|
|
if ((classFlags(t, class_) & ACC_INTERFACE) == 0) {
|
|
|
|
length *= 2;
|
|
|
|
}
|
|
|
|
interfaceTable = makeArray(t, length, true);
|
|
|
|
PROTECT(t, interfaceTable);
|
|
|
|
|
|
|
|
unsigned i = 0;
|
|
|
|
object it = hashMapIterator(t, map);
|
|
|
|
PROTECT(t, it);
|
|
|
|
|
|
|
|
for (; it; it = hashMapIteratorNext(t, it)) {
|
|
|
|
object interface = resolveClass
|
|
|
|
(t, tripleFirst(t, hashMapIteratorNode(t, it)));
|
|
|
|
if (UNLIKELY(t->exception)) return;
|
|
|
|
|
|
|
|
set(t, arrayBody(t, interfaceTable, i++), interface);
|
|
|
|
|
|
|
|
if ((classFlags(t, class_) & ACC_INTERFACE) == 0) {
|
|
|
|
// we'll fill in this table in parseMethodTable():
|
|
|
|
object vtable = makeArray
|
|
|
|
(t, arrayLength(t, classVirtualTable(t, interface)), true);
|
|
|
|
|
|
|
|
set(t, arrayBody(t, interfaceTable, i++), vtable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set(t, classInterfaceTable(t, class_), interfaceTable);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
parseFieldTable(Thread* t, Stream& s, object class_, object pool)
|
|
|
|
{
|
|
|
|
PROTECT(t, class_);
|
|
|
|
PROTECT(t, pool);
|
|
|
|
|
|
|
|
unsigned memberOffset = BytesPerWord;
|
|
|
|
if (classSuper(t, class_)) {
|
|
|
|
memberOffset = classFixedSize(t, classSuper(t, class_));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned count = s.read2();
|
|
|
|
if (count) {
|
|
|
|
unsigned staticOffset = 0;
|
|
|
|
|
|
|
|
object fieldTable = makeArray(t, count, true);
|
|
|
|
PROTECT(t, fieldTable);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < count; ++i) {
|
|
|
|
unsigned flags = s.read2();
|
|
|
|
unsigned name = s.read2();
|
|
|
|
unsigned spec = s.read2();
|
|
|
|
|
|
|
|
unsigned attributeCount = s.read2();
|
|
|
|
for (unsigned j = 0; j < attributeCount; ++j) {
|
|
|
|
s.read2();
|
|
|
|
s.skip(s.read4());
|
|
|
|
}
|
|
|
|
|
|
|
|
object field = makeField
|
|
|
|
(t,
|
2007-07-21 17:50:26 +00:00
|
|
|
0, // vm flags
|
|
|
|
fieldCode(t, byteArrayBody(t, arrayBody(t, pool, spec - 1), 0)),
|
2007-07-14 17:31:01 +00:00
|
|
|
flags,
|
|
|
|
0, // offset
|
|
|
|
arrayBody(t, pool, name - 1),
|
|
|
|
arrayBody(t, pool, spec - 1),
|
|
|
|
class_);
|
|
|
|
|
|
|
|
if (flags & ACC_STATIC) {
|
|
|
|
fieldOffset(t, field) = staticOffset++;
|
|
|
|
} else {
|
|
|
|
unsigned excess = memberOffset % BytesPerWord;
|
|
|
|
if (excess and fieldCode(t, field) == ObjectField) {
|
|
|
|
memberOffset += BytesPerWord - excess;
|
|
|
|
}
|
|
|
|
|
|
|
|
fieldOffset(t, field) = memberOffset;
|
|
|
|
memberOffset += fieldSize(t, field);
|
|
|
|
}
|
|
|
|
|
|
|
|
set(t, arrayBody(t, fieldTable, i), field);
|
|
|
|
}
|
|
|
|
|
|
|
|
set(t, classFieldTable(t, class_), fieldTable);
|
|
|
|
|
|
|
|
if (staticOffset) {
|
|
|
|
object staticTable = makeArray(t, staticOffset, true);
|
|
|
|
|
|
|
|
set(t, classStaticTable(t, class_), staticTable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
classFixedSize(t, class_) = pad(memberOffset);
|
|
|
|
|
|
|
|
if (classSuper(t, class_)
|
|
|
|
and memberOffset == classFixedSize(t, classSuper(t, class_)))
|
|
|
|
{
|
|
|
|
set(t, classObjectMask(t, class_),
|
|
|
|
classObjectMask(t, classSuper(t, class_)));
|
|
|
|
} else {
|
|
|
|
object mask = makeIntArray
|
2007-07-26 00:48:28 +00:00
|
|
|
(t, ceiling(classFixedSize(t, class_), BitsPerWord * BytesPerWord), true);
|
2007-07-14 17:31:01 +00:00
|
|
|
intArrayBody(t, mask, 0) = 1;
|
|
|
|
|
|
|
|
bool sawReferenceField = false;
|
|
|
|
for (object c = class_; c; c = classSuper(t, c)) {
|
|
|
|
object fieldTable = classFieldTable(t, c);
|
|
|
|
if (fieldTable) {
|
|
|
|
for (int i = arrayLength(t, fieldTable) - 1; i >= 0; --i) {
|
|
|
|
object field = arrayBody(t, fieldTable, i);
|
|
|
|
if (fieldCode(t, field) == ObjectField) {
|
|
|
|
unsigned index = fieldOffset(t, field) / BytesPerWord;
|
|
|
|
intArrayBody(t, mask, (index / 32)) |= 1 << (index % 32);
|
|
|
|
sawReferenceField = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sawReferenceField) {
|
|
|
|
set(t, classObjectMask(t, class_), mask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
object
|
|
|
|
parseCode(Thread* t, Stream& s, object pool)
|
|
|
|
{
|
2007-07-19 23:45:44 +00:00
|
|
|
PROTECT(t, pool);
|
|
|
|
|
2007-07-14 17:31:01 +00:00
|
|
|
unsigned maxStack = s.read2();
|
|
|
|
unsigned maxLocals = s.read2();
|
|
|
|
unsigned length = s.read4();
|
|
|
|
|
|
|
|
object code = makeCode(t, pool, 0, 0, maxStack, maxLocals, length, false);
|
|
|
|
s.read(&codeBody(t, code, 0), length);
|
2007-07-16 23:58:37 +00:00
|
|
|
PROTECT(t, code);
|
2007-07-14 17:31:01 +00:00
|
|
|
|
|
|
|
unsigned ehtLength = s.read2();
|
|
|
|
if (ehtLength) {
|
|
|
|
object eht = makeExceptionHandlerTable(t, ehtLength, false);
|
|
|
|
for (unsigned i = 0; i < ehtLength; ++i) {
|
|
|
|
ExceptionHandler* eh = exceptionHandlerTableBody(t, eht, i);
|
|
|
|
exceptionHandlerStart(eh) = s.read2();
|
|
|
|
exceptionHandlerEnd(eh) = s.read2();
|
|
|
|
exceptionHandlerIp(eh) = s.read2();
|
|
|
|
exceptionHandlerCatchType(eh) = s.read2();
|
|
|
|
}
|
|
|
|
|
|
|
|
set(t, codeExceptionHandlerTable(t, code), eht);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned attributeCount = s.read2();
|
|
|
|
for (unsigned j = 0; j < attributeCount; ++j) {
|
|
|
|
object name = arrayBody(t, pool, s.read2() - 1);
|
|
|
|
unsigned length = s.read4();
|
|
|
|
|
|
|
|
if (strcmp(reinterpret_cast<const int8_t*>("LineNumberTable"),
|
|
|
|
&byteArrayBody(t, name, 0)) == 0)
|
|
|
|
{
|
|
|
|
unsigned lntLength = s.read2();
|
|
|
|
object lnt = makeLineNumberTable(t, lntLength, false);
|
|
|
|
for (unsigned i = 0; i < lntLength; ++i) {
|
|
|
|
LineNumber* ln = lineNumberTableBody(t, lnt, i);
|
|
|
|
lineNumberIp(ln) = s.read2();
|
|
|
|
lineNumberLine(ln) = s.read2();
|
|
|
|
}
|
|
|
|
|
|
|
|
set(t, codeLineNumberTable(t, code), lnt);
|
|
|
|
} else {
|
|
|
|
s.skip(length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
parseMethodTable(Thread* t, Stream& s, object class_, object pool)
|
|
|
|
{
|
|
|
|
PROTECT(t, class_);
|
|
|
|
PROTECT(t, pool);
|
|
|
|
|
|
|
|
object virtualMap = makeHashMap(t, NormalMap, 0, 0);
|
|
|
|
PROTECT(t, virtualMap);
|
|
|
|
|
|
|
|
object nativeMap = makeHashMap(t, NormalMap, 0, 0);
|
|
|
|
PROTECT(t, nativeMap);
|
|
|
|
|
|
|
|
unsigned virtualCount = 0;
|
|
|
|
unsigned declaredVirtualCount = 0;
|
|
|
|
|
|
|
|
object superVirtualTable = 0;
|
|
|
|
PROTECT(t, superVirtualTable);
|
|
|
|
|
|
|
|
if (classFlags(t, class_) & ACC_INTERFACE) {
|
|
|
|
object itable = classInterfaceTable(t, class_);
|
|
|
|
if (itable) {
|
|
|
|
for (unsigned i = 0; i < arrayLength(t, itable); ++i) {
|
|
|
|
object vtable = classVirtualTable(t, arrayBody(t, itable, i));
|
|
|
|
for (unsigned j = 0; j < virtualCount; ++j) {
|
|
|
|
object method = arrayBody(t, vtable, j);
|
|
|
|
if (hashMapInsertMaybe(t, virtualMap, method, method, methodHash,
|
|
|
|
methodEqual))
|
|
|
|
{
|
|
|
|
++ virtualCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (classSuper(t, class_)) {
|
|
|
|
superVirtualTable = classVirtualTable(t, classSuper(t, class_));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (superVirtualTable) {
|
|
|
|
virtualCount = arrayLength(t, superVirtualTable);
|
|
|
|
for (unsigned i = 0; i < virtualCount; ++i) {
|
|
|
|
object method = arrayBody(t, superVirtualTable, i);
|
|
|
|
hashMapInsert(t, virtualMap, method, method, methodHash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
object newVirtuals = makeList(t, 0, 0, 0);
|
|
|
|
PROTECT(t, newVirtuals);
|
|
|
|
|
|
|
|
unsigned count = s.read2();
|
|
|
|
if (count) {
|
|
|
|
object methodTable = makeArray(t, count, true);
|
|
|
|
PROTECT(t, methodTable);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < count; ++i) {
|
|
|
|
unsigned flags = s.read2();
|
|
|
|
unsigned name = s.read2();
|
|
|
|
unsigned spec = s.read2();
|
|
|
|
|
|
|
|
object code = 0;
|
|
|
|
unsigned attributeCount = s.read2();
|
|
|
|
for (unsigned j = 0; j < attributeCount; ++j) {
|
|
|
|
object name = arrayBody(t, pool, s.read2() - 1);
|
|
|
|
unsigned length = s.read4();
|
|
|
|
|
|
|
|
if (strcmp(reinterpret_cast<const int8_t*>("Code"),
|
|
|
|
&byteArrayBody(t, name, 0)) == 0)
|
|
|
|
{
|
|
|
|
code = parseCode(t, s, pool);
|
|
|
|
} else {
|
|
|
|
s.skip(length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned parameterCount = ::parameterCount
|
|
|
|
(t, arrayBody(t, pool, spec - 1));
|
|
|
|
|
|
|
|
unsigned parameterFootprint = ::parameterFootprint
|
|
|
|
(t, arrayBody(t, pool, spec - 1));
|
|
|
|
|
|
|
|
if ((flags & ACC_STATIC) == 0) {
|
|
|
|
++ parameterCount;
|
|
|
|
++ parameterFootprint;
|
|
|
|
}
|
|
|
|
|
|
|
|
object method = makeMethod(t,
|
2007-07-21 17:50:26 +00:00
|
|
|
0, // vm flags
|
2007-07-14 17:31:01 +00:00
|
|
|
parameterCount,
|
|
|
|
parameterFootprint,
|
2007-07-21 17:50:26 +00:00
|
|
|
flags,
|
|
|
|
0, // offset
|
2007-07-14 17:31:01 +00:00
|
|
|
arrayBody(t, pool, name - 1),
|
|
|
|
arrayBody(t, pool, spec - 1),
|
|
|
|
class_,
|
|
|
|
code);
|
|
|
|
PROTECT(t, method);
|
|
|
|
|
|
|
|
if (flags & ACC_STATIC) {
|
|
|
|
if (strcmp(reinterpret_cast<const int8_t*>("<clinit>"),
|
|
|
|
&byteArrayBody(t, methodName(t, method), 0)) == 0)
|
|
|
|
{
|
2007-07-28 16:10:13 +00:00
|
|
|
classVmFlags(t, class_) |= NeedInitFlag;
|
2007-07-14 17:31:01 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
++ declaredVirtualCount;
|
|
|
|
|
|
|
|
object p = hashMapFindNode
|
|
|
|
(t, virtualMap, method, methodHash, methodEqual);
|
|
|
|
|
|
|
|
if (p) {
|
|
|
|
methodOffset(t, method) = methodOffset(t, tripleFirst(t, p));
|
|
|
|
|
|
|
|
set(t, tripleSecond(t, p), method);
|
|
|
|
} else {
|
|
|
|
methodOffset(t, method) = virtualCount++;
|
|
|
|
|
|
|
|
listAppend(t, newVirtuals, method);
|
|
|
|
|
|
|
|
hashMapInsert(t, virtualMap, method, method, methodHash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & ACC_NATIVE) {
|
|
|
|
object p = hashMapFindNode
|
2007-07-27 00:06:05 +00:00
|
|
|
(t, nativeMap, methodName(t, method), byteArrayHash, byteArrayEqual);
|
2007-07-14 17:31:01 +00:00
|
|
|
|
|
|
|
if (p) {
|
|
|
|
set(t, tripleSecond(t, p), method);
|
|
|
|
} else {
|
2007-07-27 00:06:05 +00:00
|
|
|
hashMapInsert(t, nativeMap, methodName(t, method), 0, byteArrayHash);
|
2007-07-14 17:31:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set(t, arrayBody(t, methodTable, i), method);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < count; ++i) {
|
|
|
|
object method = arrayBody(t, methodTable, i);
|
|
|
|
|
|
|
|
if (methodFlags(t, method) & ACC_NATIVE) {
|
2007-07-17 13:22:29 +00:00
|
|
|
PROTECT(t, method);
|
|
|
|
|
2007-07-14 17:31:01 +00:00
|
|
|
object overloaded = hashMapFind
|
2007-07-27 00:06:05 +00:00
|
|
|
(t, nativeMap, methodName(t, method), byteArrayHash, byteArrayEqual);
|
2007-07-14 17:31:01 +00:00
|
|
|
|
|
|
|
object jniName = makeJNIName(t, method, overloaded);
|
|
|
|
set(t, methodCode(t, method), jniName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set(t, classMethodTable(t, class_), methodTable);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (declaredVirtualCount == 0) {
|
|
|
|
// inherit interface table and virtual table from superclass
|
|
|
|
|
|
|
|
set(t, classInterfaceTable(t, class_),
|
|
|
|
classInterfaceTable(t, classSuper(t, class_)));
|
|
|
|
|
|
|
|
set(t, classVirtualTable(t, class_), superVirtualTable);
|
|
|
|
} else if (virtualCount) {
|
|
|
|
// generate class vtable
|
|
|
|
|
|
|
|
unsigned i = 0;
|
2007-07-17 13:22:29 +00:00
|
|
|
object vtable = makeArray(t, virtualCount, true);
|
2007-07-14 17:31:01 +00:00
|
|
|
|
|
|
|
if (classFlags(t, class_) & ACC_INTERFACE) {
|
2007-07-16 23:58:37 +00:00
|
|
|
PROTECT(t, vtable);
|
|
|
|
|
2007-07-14 17:31:01 +00:00
|
|
|
object it = hashMapIterator(t, virtualMap);
|
|
|
|
|
|
|
|
for (; it; it = hashMapIteratorNext(t, it)) {
|
|
|
|
object method = tripleFirst(t, hashMapIteratorNode(t, it));
|
|
|
|
set(t, arrayBody(t, vtable, i++), method);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (superVirtualTable) {
|
|
|
|
for (; i < arrayLength(t, superVirtualTable); ++i) {
|
|
|
|
object method = arrayBody(t, superVirtualTable, i);
|
|
|
|
method = hashMapFind(t, virtualMap, method, methodHash, methodEqual);
|
|
|
|
|
|
|
|
set(t, arrayBody(t, vtable, i), method);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (object p = listFront(t, newVirtuals); p; p = pairSecond(t, p)) {
|
|
|
|
set(t, arrayBody(t, vtable, i++), pairFirst(t, p));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set(t, classVirtualTable(t, class_), vtable);
|
|
|
|
|
|
|
|
if ((classFlags(t, class_) & ACC_INTERFACE) == 0) {
|
|
|
|
// generate interface vtables
|
|
|
|
|
|
|
|
object itable = classInterfaceTable(t, class_);
|
|
|
|
if (itable) {
|
|
|
|
PROTECT(t, itable);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < arrayLength(t, itable); i += 2) {
|
|
|
|
object ivtable = classVirtualTable(t, arrayBody(t, itable, i));
|
|
|
|
object vtable = arrayBody(t, itable, i + 1);
|
|
|
|
|
|
|
|
for (unsigned j = 0; j < arrayLength(t, ivtable); ++j) {
|
|
|
|
object method = arrayBody(t, ivtable, j);
|
|
|
|
method = hashMapFind
|
|
|
|
(t, virtualMap, method, methodHash, methodEqual);
|
|
|
|
assert(t, method);
|
|
|
|
|
|
|
|
set(t, arrayBody(t, vtable, j), method);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
object
|
|
|
|
parseClass(Thread* t, const uint8_t* data, unsigned size)
|
|
|
|
{
|
|
|
|
class Client : public Stream::Client {
|
|
|
|
public:
|
|
|
|
Client(Thread* t): t(t) { }
|
|
|
|
|
|
|
|
virtual void NO_RETURN handleEOS() {
|
|
|
|
abort(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Thread* t;
|
|
|
|
} client(t);
|
|
|
|
|
|
|
|
Stream s(&client, data, size);
|
|
|
|
|
|
|
|
uint32_t magic = s.read4();
|
|
|
|
assert(t, magic == 0xCAFEBABE);
|
|
|
|
s.read2(); // minor version
|
|
|
|
s.read2(); // major version
|
|
|
|
|
|
|
|
object pool = parsePool(t, s);
|
2007-07-19 23:45:44 +00:00
|
|
|
PROTECT(t, pool);
|
2007-07-14 17:31:01 +00:00
|
|
|
|
|
|
|
unsigned flags = s.read2();
|
|
|
|
unsigned name = s.read2();
|
|
|
|
|
|
|
|
object class_ = makeClass(t,
|
|
|
|
flags,
|
|
|
|
0, // VM flags
|
|
|
|
0, // array dimensions
|
|
|
|
0, // fixed size
|
|
|
|
0, // array size
|
|
|
|
0, // object mask
|
|
|
|
arrayBody(t, pool, name - 1),
|
|
|
|
0, // super
|
|
|
|
0, // interfaces
|
|
|
|
0, // vtable
|
|
|
|
0, // fields
|
|
|
|
0, // methods
|
2007-07-28 16:10:13 +00:00
|
|
|
0); // static table
|
2007-07-14 17:31:01 +00:00
|
|
|
PROTECT(t, class_);
|
|
|
|
|
|
|
|
unsigned super = s.read2();
|
|
|
|
if (super) {
|
|
|
|
object sc = resolveClass(t, arrayBody(t, pool, super - 1));
|
|
|
|
if (UNLIKELY(t->exception)) return 0;
|
|
|
|
|
|
|
|
set(t, classSuper(t, class_), sc);
|
|
|
|
|
2007-07-28 16:10:13 +00:00
|
|
|
classVmFlags(t, class_)
|
|
|
|
|= (classVmFlags(t, sc) & (ReferenceFlag | WeakReferenceFlag));
|
2007-07-14 17:31:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parseInterfaceTable(t, s, class_, pool);
|
|
|
|
if (UNLIKELY(t->exception)) return 0;
|
|
|
|
|
|
|
|
parseFieldTable(t, s, class_, pool);
|
|
|
|
if (UNLIKELY(t->exception)) return 0;
|
|
|
|
|
|
|
|
parseMethodTable(t, s, class_, pool);
|
|
|
|
if (UNLIKELY(t->exception)) return 0;
|
|
|
|
|
|
|
|
return class_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
updateBootstrapClass(Thread* t, object bootstrapClass, object class_)
|
|
|
|
{
|
|
|
|
expect(t, bootstrapClass != class_);
|
|
|
|
|
|
|
|
// verify that the classes have the same layout
|
|
|
|
expect(t, classSuper(t, bootstrapClass) == classSuper(t, class_));
|
|
|
|
expect(t, classFixedSize(t, bootstrapClass) == classFixedSize(t, class_));
|
2007-07-20 03:18:25 +00:00
|
|
|
expect(t,
|
|
|
|
(classVmFlags(t, bootstrapClass) & ReferenceFlag)
|
|
|
|
or (classObjectMask(t, bootstrapClass) == 0
|
2007-07-14 17:31:01 +00:00
|
|
|
and classObjectMask(t, class_) == 0)
|
|
|
|
or intArrayEqual(t, classObjectMask(t, bootstrapClass),
|
|
|
|
classObjectMask(t, class_)));
|
|
|
|
|
|
|
|
PROTECT(t, bootstrapClass);
|
|
|
|
PROTECT(t, class_);
|
|
|
|
|
|
|
|
ENTER(t, Thread::ExclusiveState);
|
|
|
|
|
2007-07-16 23:58:37 +00:00
|
|
|
classFlags(t, bootstrapClass) = classFlags(t, class_);
|
2007-07-28 16:10:13 +00:00
|
|
|
classVmFlags(t, bootstrapClass) |= classVmFlags(t, class_);
|
2007-07-14 17:31:01 +00:00
|
|
|
|
2007-07-16 23:58:37 +00:00
|
|
|
set(t, classSuper(t, bootstrapClass), classSuper(t, class_));
|
|
|
|
set(t, classInterfaceTable(t, bootstrapClass),
|
|
|
|
classInterfaceTable(t, class_));
|
|
|
|
set(t, classVirtualTable(t, bootstrapClass), classVirtualTable(t, class_));
|
|
|
|
set(t, classFieldTable(t, bootstrapClass), classFieldTable(t, class_));
|
|
|
|
set(t, classMethodTable(t, bootstrapClass), classMethodTable(t, class_));
|
|
|
|
set(t, classStaticTable(t, bootstrapClass), classStaticTable(t, class_));
|
2007-07-14 17:31:01 +00:00
|
|
|
|
|
|
|
object fieldTable = classFieldTable(t, class_);
|
|
|
|
if (fieldTable) {
|
|
|
|
for (unsigned i = 0; i < arrayLength(t, fieldTable); ++i) {
|
|
|
|
set(t, fieldClass(t, arrayBody(t, fieldTable, i)), bootstrapClass);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
object methodTable = classMethodTable(t, class_);
|
|
|
|
if (methodTable) {
|
|
|
|
for (unsigned i = 0; i < arrayLength(t, methodTable); ++i) {
|
|
|
|
set(t, methodClass(t, arrayBody(t, methodTable, i)), bootstrapClass);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
object
|
|
|
|
makeArrayClass(Thread* t, unsigned dimensions, object spec,
|
|
|
|
object elementClass)
|
|
|
|
{
|
|
|
|
return makeClass
|
|
|
|
(t,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
dimensions,
|
|
|
|
2 * BytesPerWord,
|
|
|
|
BytesPerWord,
|
|
|
|
classObjectMask(t, arrayBody(t, t->vm->types, Machine::ArrayType)),
|
|
|
|
spec,
|
|
|
|
arrayBody(t, t->vm->types, Machine::JobjectType),
|
|
|
|
elementClass,
|
|
|
|
classVirtualTable(t, arrayBody(t, t->vm->types, Machine::JobjectType)),
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
|
|
|
|
object
|
|
|
|
makeArrayClass(Thread* t, object spec)
|
|
|
|
{
|
|
|
|
PROTECT(t, spec);
|
|
|
|
|
|
|
|
const char* s = reinterpret_cast<const char*>(&byteArrayBody(t, spec, 0));
|
|
|
|
const char* start = s;
|
|
|
|
unsigned dimensions = 0;
|
|
|
|
for (; *s == '['; ++s) ++ dimensions;
|
|
|
|
|
|
|
|
object elementSpec;
|
|
|
|
switch (*s) {
|
|
|
|
case 'L': {
|
|
|
|
++ s;
|
|
|
|
const char* elementSpecStart = s;
|
|
|
|
while (*s and *s != ';') ++ s;
|
|
|
|
|
|
|
|
elementSpec = makeByteArray(t, s - elementSpecStart + 1, false);
|
|
|
|
memcpy(&byteArrayBody(t, elementSpec, 0),
|
|
|
|
&byteArrayBody(t, spec, elementSpecStart - start),
|
|
|
|
s - elementSpecStart);
|
|
|
|
byteArrayBody(t, elementSpec, s - elementSpecStart) = 0;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (dimensions > 1) {
|
|
|
|
char c = *s;
|
|
|
|
elementSpec = makeByteArray(t, 3, false);
|
|
|
|
byteArrayBody(t, elementSpec, 0) = '[';
|
|
|
|
byteArrayBody(t, elementSpec, 1) = c;
|
|
|
|
byteArrayBody(t, elementSpec, 2) = 0;
|
|
|
|
-- dimensions;
|
|
|
|
} else {
|
|
|
|
abort(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-14 18:37:04 +00:00
|
|
|
object elementClass = hashMapFind
|
|
|
|
(t, t->vm->bootstrapClassMap, elementSpec, byteArrayHash, byteArrayEqual);
|
|
|
|
|
|
|
|
if (elementClass == 0) {
|
|
|
|
elementClass = resolveClass(t, elementSpec);
|
|
|
|
if (UNLIKELY(t->exception)) return 0;
|
|
|
|
}
|
2007-07-14 17:31:01 +00:00
|
|
|
|
|
|
|
return makeArrayClass(t, dimensions, spec, elementClass);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
removeMonitor(Thread* t, object o)
|
|
|
|
{
|
|
|
|
object p = hashMapRemove(t, t->vm->monitorMap, o, objectHash, objectEqual);
|
|
|
|
|
|
|
|
assert(t, p);
|
|
|
|
|
|
|
|
if (DebugMonitors) {
|
|
|
|
fprintf(stderr, "dispose monitor %p for object %x\n",
|
|
|
|
static_cast<System::Monitor*>(pointerValue(t, p)),
|
|
|
|
objectHash(t, o));
|
|
|
|
}
|
|
|
|
|
|
|
|
static_cast<System::Monitor*>(pointerValue(t, p))->dispose();
|
|
|
|
}
|
|
|
|
|
2007-07-06 23:50:26 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace vm {
|
|
|
|
|
|
|
|
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),
|
2007-07-20 03:18:25 +00:00
|
|
|
referenceLock(0),
|
2007-07-06 23:50:26 +00:00
|
|
|
libraries(0),
|
|
|
|
classMap(0),
|
|
|
|
bootstrapClassMap(0),
|
|
|
|
builtinMap(0),
|
|
|
|
monitorMap(0),
|
|
|
|
types(0),
|
|
|
|
finalizers(0),
|
2007-07-10 01:43:43 +00:00
|
|
|
tenuredFinalizers(0),
|
|
|
|
finalizeQueue(0),
|
2007-07-10 03:04:49 +00:00
|
|
|
weakReferences(0),
|
2007-07-10 01:43:43 +00:00
|
|
|
tenuredWeakReferences(0),
|
2007-07-06 23:50:26 +00:00
|
|
|
unsafe(false)
|
|
|
|
{
|
2007-07-27 00:06:05 +00:00
|
|
|
populateJNITable(&jniEnvVTable);
|
2007-07-06 23:50:26 +00:00
|
|
|
|
|
|
|
if (not system->success(system->make(&stateLock)) or
|
|
|
|
not system->success(system->make(&heapLock)) or
|
|
|
|
not system->success(system->make(&classLock)) or
|
2007-07-20 03:18:25 +00:00
|
|
|
not system->success(system->make(&referenceLock)))
|
2007-07-06 23:50:26 +00:00
|
|
|
{
|
|
|
|
system->abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Machine::dispose()
|
|
|
|
{
|
|
|
|
stateLock->dispose();
|
|
|
|
heapLock->dispose();
|
|
|
|
classLock->dispose();
|
2007-07-20 03:18:25 +00:00
|
|
|
referenceLock->dispose();
|
2007-07-06 23:50:26 +00:00
|
|
|
|
|
|
|
if (libraries) {
|
|
|
|
libraries->dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-24 01:44:20 +00:00
|
|
|
Thread::Thread(Machine* m, object javaThread, Thread* parent):
|
2007-07-06 23:50:26 +00:00
|
|
|
vtable(&(m->jniEnvVTable)),
|
|
|
|
vm(m),
|
2007-07-07 18:09:16 +00:00
|
|
|
parent(parent),
|
|
|
|
peer((parent ? parent->child : 0)),
|
2007-07-06 23:50:26 +00:00
|
|
|
child(0),
|
|
|
|
state(NoState),
|
2007-07-07 18:09:16 +00:00
|
|
|
systemThread(0),
|
|
|
|
javaThread(javaThread),
|
2007-07-06 23:50:26 +00:00
|
|
|
code(0),
|
|
|
|
exception(0),
|
2007-07-07 18:09:16 +00:00
|
|
|
large(0),
|
2007-07-06 23:50:26 +00:00
|
|
|
ip(0),
|
|
|
|
sp(0),
|
|
|
|
frame(-1),
|
|
|
|
heapIndex(0),
|
2007-07-07 18:09:16 +00:00
|
|
|
protector(0)
|
2007-07-16 23:58:37 +00:00
|
|
|
#ifdef VM_STRESS
|
|
|
|
, stress(false),
|
|
|
|
heap(static_cast<object*>(m->system->allocate(HeapSizeInBytes)))
|
|
|
|
#endif // VM_STRESS
|
2007-07-06 23:50:26 +00:00
|
|
|
{
|
2007-07-07 18:09:16 +00:00
|
|
|
if (parent == 0) {
|
|
|
|
assert(this, m->rootThread == 0);
|
|
|
|
assert(this, javaThread == 0);
|
|
|
|
|
2007-07-06 23:50:26 +00:00
|
|
|
m->rootThread = this;
|
|
|
|
m->unsafe = true;
|
|
|
|
|
2007-07-07 18:09:16 +00:00
|
|
|
if (not m->system->success(m->system->attach(&systemThread))) {
|
|
|
|
abort(this);
|
|
|
|
}
|
|
|
|
|
2007-07-06 23:50:26 +00:00
|
|
|
Thread* t = this;
|
|
|
|
|
|
|
|
#include "type-initializations.cpp"
|
|
|
|
|
|
|
|
object arrayClass = arrayBody(t, t->vm->types, Machine::ArrayType);
|
|
|
|
set(t, cast<object>(t->vm->types, 0), arrayClass);
|
|
|
|
|
2007-07-12 23:46:08 +00:00
|
|
|
object objectClass = arrayBody(t, m->types, Machine::JobjectType);
|
|
|
|
|
2007-07-06 23:50:26 +00:00
|
|
|
object classClass = arrayBody(t, m->types, Machine::ClassType);
|
|
|
|
set(t, cast<object>(classClass, 0), classClass);
|
2007-07-12 23:46:08 +00:00
|
|
|
set(t, classSuper(t, classClass), objectClass);
|
2007-07-06 23:50:26 +00:00
|
|
|
|
|
|
|
object intArrayClass = arrayBody(t, m->types, Machine::IntArrayType);
|
|
|
|
set(t, cast<object>(intArrayClass, 0), classClass);
|
2007-07-12 23:46:08 +00:00
|
|
|
set(t, classSuper(t, intArrayClass), objectClass);
|
2007-07-06 23:50:26 +00:00
|
|
|
|
|
|
|
m->unsafe = false;
|
|
|
|
|
2007-07-20 03:18:25 +00:00
|
|
|
classVmFlags(t, arrayBody(t, m->types, Machine::JreferenceType))
|
|
|
|
|= ReferenceFlag;
|
|
|
|
classVmFlags(t, arrayBody(t, m->types, Machine::WeakReferenceType))
|
|
|
|
|= ReferenceFlag | WeakReferenceFlag;
|
|
|
|
classVmFlags(t, arrayBody(t, m->types, Machine::PhantomReferenceType))
|
|
|
|
|= ReferenceFlag | WeakReferenceFlag;
|
|
|
|
|
2007-07-14 17:31:01 +00:00
|
|
|
m->bootstrapClassMap = makeHashMap(this, NormalMap, 0, 0);
|
2007-07-06 23:50:26 +00:00
|
|
|
|
|
|
|
#include "type-java-initializations.cpp"
|
|
|
|
|
2007-07-14 17:31:01 +00:00
|
|
|
m->classMap = makeHashMap(this, NormalMap, 0, 0);
|
|
|
|
m->builtinMap = makeHashMap(this, NormalMap, 0, 0);
|
|
|
|
m->monitorMap = makeHashMap(this, WeakMap, 0, 0);
|
2007-07-06 23:50:26 +00:00
|
|
|
|
2007-07-27 00:06:05 +00:00
|
|
|
populateBuiltinMap(t, m->builtinMap);
|
2007-07-07 18:09:16 +00:00
|
|
|
|
2007-07-28 16:55:24 +00:00
|
|
|
t->javaThread = makeThread(t, reinterpret_cast<int64_t>(t), 0, 0, 0, 0);
|
2007-07-07 18:09:16 +00:00
|
|
|
} else {
|
|
|
|
threadPeer(this, javaThread) = reinterpret_cast<jlong>(this);
|
|
|
|
parent->child = this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Thread::exit()
|
|
|
|
{
|
|
|
|
if (state != Thread::ExitState and
|
|
|
|
state != Thread::ZombieState)
|
|
|
|
{
|
|
|
|
enter(this, Thread::ExclusiveState);
|
|
|
|
|
|
|
|
if (vm->liveCount == 1) {
|
|
|
|
vm::exit(this);
|
|
|
|
} else {
|
|
|
|
enter(this, Thread::ZombieState);
|
|
|
|
}
|
2007-07-06 23:50:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Thread::dispose()
|
|
|
|
{
|
2007-07-07 18:09:16 +00:00
|
|
|
if (large) {
|
|
|
|
vm->system->free(large);
|
|
|
|
large = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (systemThread) {
|
|
|
|
systemThread->dispose();
|
|
|
|
systemThread = 0;
|
|
|
|
}
|
|
|
|
|
2007-07-17 00:23:23 +00:00
|
|
|
#ifdef VM_STRESS
|
|
|
|
vm->system->free(heap);
|
|
|
|
heap = 0;
|
|
|
|
#endif // VM_STRESS
|
|
|
|
|
2007-07-24 01:44:20 +00:00
|
|
|
vm->system->free(this);
|
2007-07-06 23:50:26 +00:00
|
|
|
}
|
|
|
|
|
2007-07-07 18:09:16 +00:00
|
|
|
void
|
|
|
|
exit(Thread* t)
|
|
|
|
{
|
|
|
|
enter(t, Thread::ExitState);
|
|
|
|
|
|
|
|
joinAll(t, t->vm->rootThread);
|
|
|
|
|
2007-07-17 01:55:49 +00:00
|
|
|
for (object* p = &(t->vm->finalizers); *p;) {
|
|
|
|
object f = *p;
|
|
|
|
*p = finalizerNext(t, *p);
|
|
|
|
|
2007-07-07 18:09:16 +00:00
|
|
|
reinterpret_cast<void (*)(Thread*, object)>(finalizerFinalize(t, f))
|
|
|
|
(t, finalizerTarget(t, f));
|
|
|
|
}
|
|
|
|
|
2007-07-17 01:55:49 +00:00
|
|
|
for (object* p = &(t->vm->tenuredFinalizers); *p;) {
|
|
|
|
object f = *p;
|
|
|
|
*p = finalizerNext(t, *p);
|
|
|
|
|
2007-07-11 04:19:26 +00:00
|
|
|
reinterpret_cast<void (*)(Thread*, object)>(finalizerFinalize(t, f))
|
|
|
|
(t, finalizerTarget(t, f));
|
|
|
|
}
|
|
|
|
|
2007-07-07 18:09:16 +00:00
|
|
|
disposeAll(t, t->vm->rootThread);
|
|
|
|
}
|
|
|
|
|
2007-07-06 23:50:26 +00:00
|
|
|
void
|
|
|
|
enter(Thread* t, Thread::State s)
|
|
|
|
{
|
2007-07-16 01:03:02 +00:00
|
|
|
stress(t);
|
|
|
|
|
2007-07-06 23:50:26 +00:00
|
|
|
if (s == t->state) return;
|
|
|
|
|
2007-07-18 01:33:00 +00:00
|
|
|
if (t->state == Thread::ExitState) {
|
|
|
|
// once in exit state, we stay that way
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-07-06 23:50:26 +00:00
|
|
|
ACQUIRE_RAW(t, t->vm->stateLock);
|
|
|
|
|
|
|
|
switch (s) {
|
|
|
|
case Thread::ExclusiveState: {
|
2007-07-18 01:33:00 +00:00
|
|
|
assert(t, t->state == Thread::ActiveState);
|
2007-07-06 23:50:26 +00:00
|
|
|
|
|
|
|
while (t->vm->exclusive) {
|
|
|
|
// another thread got here first.
|
2007-07-07 18:09:16 +00:00
|
|
|
ENTER(t, Thread::IdleState);
|
2007-07-06 23:50:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t->state = Thread::ExclusiveState;
|
|
|
|
t->vm->exclusive = t;
|
|
|
|
|
|
|
|
while (t->vm->activeCount > 1) {
|
|
|
|
t->vm->stateLock->wait(t, 0);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Thread::IdleState:
|
|
|
|
case Thread::ZombieState: {
|
|
|
|
switch (t->state) {
|
|
|
|
case Thread::ExclusiveState: {
|
|
|
|
assert(t, t->vm->exclusive == t);
|
|
|
|
t->vm->exclusive = 0;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Thread::ActiveState: break;
|
|
|
|
|
|
|
|
default: abort(t);
|
|
|
|
}
|
|
|
|
|
2007-07-18 01:33:00 +00:00
|
|
|
assert(t, t->vm->activeCount > 0);
|
2007-07-06 23:50:26 +00:00
|
|
|
-- t->vm->activeCount;
|
2007-07-18 01:33:00 +00:00
|
|
|
|
2007-07-06 23:50:26 +00:00
|
|
|
if (s == Thread::ZombieState) {
|
2007-07-18 01:33:00 +00:00
|
|
|
assert(t, t->vm->liveCount > 0);
|
2007-07-06 23:50:26 +00:00
|
|
|
-- t->vm->liveCount;
|
|
|
|
}
|
|
|
|
t->state = s;
|
|
|
|
|
|
|
|
t->vm->stateLock->notifyAll(t);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Thread::ActiveState: {
|
|
|
|
switch (t->state) {
|
|
|
|
case Thread::ExclusiveState: {
|
|
|
|
assert(t, t->vm->exclusive == t);
|
|
|
|
|
|
|
|
t->state = s;
|
|
|
|
t->vm->exclusive = 0;
|
|
|
|
|
|
|
|
t->vm->stateLock->notifyAll(t);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Thread::NoState:
|
|
|
|
case Thread::IdleState: {
|
|
|
|
while (t->vm->exclusive) {
|
|
|
|
t->vm->stateLock->wait(t, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
++ t->vm->activeCount;
|
|
|
|
if (t->state == Thread::NoState) {
|
|
|
|
++ t->vm->liveCount;
|
|
|
|
}
|
|
|
|
t->state = s;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
default: abort(t);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Thread::ExitState: {
|
|
|
|
switch (t->state) {
|
|
|
|
case Thread::ExclusiveState: {
|
|
|
|
assert(t, t->vm->exclusive == t);
|
|
|
|
t->vm->exclusive = 0;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Thread::ActiveState: break;
|
|
|
|
|
|
|
|
default: abort(t);
|
|
|
|
}
|
2007-07-18 01:33:00 +00:00
|
|
|
|
|
|
|
assert(t, t->vm->activeCount > 0);
|
2007-07-06 23:50:26 +00:00
|
|
|
-- t->vm->activeCount;
|
2007-07-18 01:33:00 +00:00
|
|
|
|
2007-07-06 23:50:26 +00:00
|
|
|
t->state = s;
|
|
|
|
|
|
|
|
while (t->vm->liveCount > 1) {
|
|
|
|
t->vm->stateLock->wait(t, 0);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
default: abort(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
object
|
|
|
|
allocate2(Thread* t, unsigned sizeInBytes)
|
|
|
|
{
|
2007-07-07 18:09:16 +00:00
|
|
|
if (sizeInBytes > Thread::HeapSizeInBytes and t->large == 0) {
|
2007-07-06 23:50:26 +00:00
|
|
|
return allocateLarge(t, sizeInBytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
ACQUIRE_RAW(t, t->vm->stateLock);
|
|
|
|
|
2007-07-07 18:09:16 +00:00
|
|
|
while (t->vm->exclusive and t->vm->exclusive != t) {
|
2007-07-06 23:50:26 +00:00
|
|
|
// another thread wants to enter the exclusive state, either for a
|
|
|
|
// collection or some other reason. We give it a chance here.
|
2007-07-07 18:09:16 +00:00
|
|
|
ENTER(t, Thread::IdleState);
|
2007-07-06 23:50:26 +00:00
|
|
|
}
|
|
|
|
|
2007-07-26 00:48:28 +00:00
|
|
|
if (t->heapIndex + ceiling(sizeInBytes, BytesPerWord)
|
2007-07-06 23:50:26 +00:00
|
|
|
>= Thread::HeapSizeInWords)
|
|
|
|
{
|
2007-07-07 18:09:16 +00:00
|
|
|
ENTER(t, Thread::ExclusiveState);
|
|
|
|
collect(t, Heap::MinorCollection);
|
2007-07-06 23:50:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sizeInBytes > Thread::HeapSizeInBytes) {
|
|
|
|
return allocateLarge(t, sizeInBytes);
|
|
|
|
} else {
|
|
|
|
return allocateSmall(t, sizeInBytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-27 00:06:05 +00:00
|
|
|
object
|
|
|
|
make(Thread* t, object class_)
|
|
|
|
{
|
|
|
|
PROTECT(t, class_);
|
|
|
|
unsigned sizeInBytes = pad(classFixedSize(t, class_));
|
|
|
|
object instance = allocate(t, sizeInBytes);
|
|
|
|
*static_cast<object*>(instance) = class_;
|
|
|
|
memset(static_cast<object*>(instance) + 1, 0,
|
|
|
|
sizeInBytes - sizeof(object));
|
|
|
|
|
|
|
|
if (UNLIKELY(classVmFlags(t, class_) & WeakReferenceFlag)) {
|
|
|
|
PROTECT(t, instance);
|
|
|
|
|
|
|
|
ACQUIRE(t, t->vm->referenceLock);
|
|
|
|
|
|
|
|
jreferenceNextUnsafe(t, instance) = t->vm->weakReferences;
|
|
|
|
t->vm->weakReferences = instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2007-07-06 23:50:26 +00:00
|
|
|
object
|
|
|
|
makeByteArray(Thread* t, const char* format, ...)
|
|
|
|
{
|
|
|
|
va_list a;
|
|
|
|
va_start(a, format);
|
|
|
|
object s = ::makeByteArray(t, format, a);
|
|
|
|
va_end(a);
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
object
|
|
|
|
makeString(Thread* t, const char* format, ...)
|
|
|
|
{
|
|
|
|
va_list a;
|
|
|
|
va_start(a, format);
|
|
|
|
object s = ::makeByteArray(t, format, a);
|
|
|
|
va_end(a);
|
|
|
|
|
2007-07-11 01:38:06 +00:00
|
|
|
return makeString(t, s, 0, byteArrayLength(t, s) - 1, 0);
|
2007-07-06 23:50:26 +00:00
|
|
|
}
|
|
|
|
|
2007-07-08 01:06:32 +00:00
|
|
|
void
|
|
|
|
stringChars(Thread* t, object string, char* chars)
|
|
|
|
{
|
|
|
|
object data = stringData(t, string);
|
|
|
|
if (objectClass(t, data)
|
|
|
|
== arrayBody(t, t->vm->types, Machine::ByteArrayType))
|
|
|
|
{
|
|
|
|
memcpy(chars,
|
|
|
|
&byteArrayBody(t, data, stringOffset(t, string)),
|
|
|
|
stringLength(t, string));
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < stringLength(t, string); ++i) {
|
|
|
|
chars[i] = charArrayBody(t, data, stringOffset(t, string) + i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
chars[stringLength(t, string)] = 0;
|
|
|
|
}
|
|
|
|
|
2007-07-24 01:44:20 +00:00
|
|
|
bool
|
|
|
|
isAssignableFrom(Thread* t, object a, object b)
|
|
|
|
{
|
|
|
|
if (classFlags(t, a) & ACC_INTERFACE) {
|
|
|
|
for (; b; b = classSuper(t, b)) {
|
|
|
|
object itable = classInterfaceTable(t, b);
|
|
|
|
for (unsigned i = 0; i < arrayLength(t, itable); i += 2) {
|
|
|
|
if (arrayBody(t, itable, i) == a) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (; b; b = classSuper(t, b)) {
|
|
|
|
if (b == a) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
instanceOf(Thread* t, object class_, object o)
|
|
|
|
{
|
|
|
|
if (o == 0) {
|
|
|
|
return false;
|
2007-07-24 03:16:59 +00:00
|
|
|
} else {
|
|
|
|
return isAssignableFrom(t, class_, objectClass(t, o));
|
2007-07-24 01:44:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-28 16:10:13 +00:00
|
|
|
object
|
|
|
|
classInitializer(Thread* t, object class_)
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < arrayLength(t, classMethodTable(t, class_)); ++i) {
|
|
|
|
object o = arrayBody(t, classMethodTable(t, class_), i);
|
|
|
|
|
|
|
|
if (strcmp(reinterpret_cast<const int8_t*>("<clinit>"),
|
|
|
|
&byteArrayBody(t, methodName(t, o), 0)) == 0)
|
|
|
|
{
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
abort(t);
|
|
|
|
}
|
|
|
|
|
2007-07-16 23:58:37 +00:00
|
|
|
unsigned
|
|
|
|
parameterFootprint(const char* s)
|
|
|
|
{
|
|
|
|
unsigned footprint = 0;
|
|
|
|
++ s; // skip '('
|
|
|
|
while (*s and *s != ')') {
|
|
|
|
switch (*s) {
|
|
|
|
case 'L':
|
|
|
|
while (*s and *s != ';') ++ s;
|
|
|
|
++ s;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '[':
|
|
|
|
while (*s == '[') ++ s;
|
|
|
|
switch (*s) {
|
|
|
|
case 'L':
|
|
|
|
while (*s and *s != ';') ++ s;
|
|
|
|
++ s;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
++ s;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'J':
|
|
|
|
case 'D':
|
|
|
|
++ s;
|
|
|
|
++ footprint;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
++ s;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
++ footprint;
|
|
|
|
}
|
|
|
|
|
|
|
|
return footprint;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
parameterCount(const char* s)
|
|
|
|
{
|
|
|
|
unsigned count = 0;
|
|
|
|
++ s; // skip '('
|
|
|
|
while (*s and *s != ')') {
|
|
|
|
switch (*s) {
|
|
|
|
case 'L':
|
|
|
|
while (*s and *s != ';') ++ s;
|
|
|
|
++ s;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '[':
|
|
|
|
while (*s == '[') ++ s;
|
|
|
|
switch (*s) {
|
|
|
|
case 'L':
|
|
|
|
while (*s and *s != ';') ++ s;
|
|
|
|
++ s;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
++ s;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
++ s;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
++ count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2007-07-06 23:50:26 +00:00
|
|
|
object
|
|
|
|
hashMapFindNode(Thread* t, object map, object key,
|
|
|
|
uint32_t (*hash)(Thread*, object),
|
|
|
|
bool (*equal)(Thread*, object, object))
|
|
|
|
{
|
2007-07-14 17:31:01 +00:00
|
|
|
bool weak = hashMapType(t, map) == WeakMap;
|
2007-07-06 23:50:26 +00:00
|
|
|
object array = hashMapArray(t, map);
|
|
|
|
if (array) {
|
|
|
|
unsigned index = hash(t, key) & (arrayLength(t, array) - 1);
|
2007-07-11 04:19:26 +00:00
|
|
|
for (object n = arrayBody(t, array, index); n; n = tripleThird(t, n)) {
|
2007-07-14 17:31:01 +00:00
|
|
|
object k = tripleFirst(t, n);
|
|
|
|
if (weak) {
|
|
|
|
k = jreferenceTarget(t, k);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (equal(t, key, k)) {
|
2007-07-06 23:50:26 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
hashMapResize(Thread* t, object map, uint32_t (*hash)(Thread*, object),
|
|
|
|
unsigned size)
|
|
|
|
{
|
|
|
|
PROTECT(t, map);
|
|
|
|
|
2007-07-11 04:19:26 +00:00
|
|
|
object newArray = 0;
|
|
|
|
|
|
|
|
if (size) {
|
|
|
|
object oldArray = hashMapArray(t, map);
|
|
|
|
PROTECT(t, oldArray);
|
2007-07-06 23:50:26 +00:00
|
|
|
|
2007-07-11 04:19:26 +00:00
|
|
|
unsigned newLength = nextPowerOfTwo(size);
|
2007-07-21 20:44:39 +00:00
|
|
|
if (oldArray and arrayLength(t, oldArray) == newLength) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-07-11 04:19:26 +00:00
|
|
|
newArray = makeArray(t, newLength, true);
|
2007-07-06 23:50:26 +00:00
|
|
|
|
2007-07-11 04:19:26 +00:00
|
|
|
if (oldArray) {
|
2007-07-14 17:31:01 +00:00
|
|
|
bool weak = hashMapType(t, map) == WeakMap;
|
|
|
|
|
2007-07-11 04:19:26 +00:00
|
|
|
for (unsigned i = 0; i < arrayLength(t, oldArray); ++i) {
|
|
|
|
object next;
|
|
|
|
for (object p = arrayBody(t, oldArray, i); p; p = next) {
|
|
|
|
next = tripleThird(t, p);
|
2007-07-06 23:50:26 +00:00
|
|
|
|
2007-07-14 17:31:01 +00:00
|
|
|
object k = tripleFirst(t, p);
|
|
|
|
if (weak) {
|
|
|
|
k = jreferenceTarget(t, k);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned index = hash(t, k) & (newLength - 1);
|
2007-07-06 23:50:26 +00:00
|
|
|
|
2007-07-11 04:19:26 +00:00
|
|
|
set(t, tripleThird(t, p), arrayBody(t, newArray, index));
|
|
|
|
set(t, arrayBody(t, newArray, index), p);
|
|
|
|
}
|
2007-07-06 23:50:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set(t, hashMapArray(t, map), newArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-07-14 17:31:01 +00:00
|
|
|
hashMapInsert(Thread* t, object map, object key, object value,
|
2007-07-11 04:19:26 +00:00
|
|
|
uint32_t (*hash)(Thread*, object))
|
2007-07-06 23:50:26 +00:00
|
|
|
{
|
2007-07-14 17:31:01 +00:00
|
|
|
bool weak = hashMapType(t, map) == WeakMap;
|
2007-07-06 23:50:26 +00:00
|
|
|
object array = hashMapArray(t, map);
|
|
|
|
PROTECT(t, array);
|
|
|
|
|
|
|
|
++ hashMapSize(t, map);
|
|
|
|
|
|
|
|
if (array == 0 or hashMapSize(t, map) >= arrayLength(t, array) * 2) {
|
|
|
|
PROTECT(t, map);
|
|
|
|
PROTECT(t, key);
|
|
|
|
PROTECT(t, value);
|
|
|
|
|
|
|
|
hashMapResize(t, map, hash, array ? arrayLength(t, array) * 2 : 16);
|
|
|
|
array = hashMapArray(t, map);
|
|
|
|
}
|
|
|
|
|
2007-07-14 17:31:01 +00:00
|
|
|
unsigned index = hash(t, key) & (arrayLength(t, array) - 1);
|
2007-07-06 23:50:26 +00:00
|
|
|
|
2007-07-14 17:31:01 +00:00
|
|
|
if (weak) {
|
2007-07-19 23:45:44 +00:00
|
|
|
PROTECT(t, key);
|
2007-07-14 17:31:01 +00:00
|
|
|
PROTECT(t, value);
|
2007-07-19 23:45:44 +00:00
|
|
|
|
2007-07-20 01:07:30 +00:00
|
|
|
t->vm->weakReferences = makeWeakReference
|
2007-07-21 17:50:26 +00:00
|
|
|
(t, t->vm->weakReferences, 0, 0, 0);
|
2007-07-19 23:45:44 +00:00
|
|
|
jreferenceTarget(t, t->vm->weakReferences) = key;
|
|
|
|
key = t->vm->weakReferences;
|
2007-07-14 17:31:01 +00:00
|
|
|
}
|
|
|
|
|
2007-07-19 23:45:44 +00:00
|
|
|
object n = makeTriple(t, key, value, arrayBody(t, array, index));
|
2007-07-06 23:50:26 +00:00
|
|
|
|
|
|
|
set(t, arrayBody(t, array, index), n);
|
|
|
|
}
|
|
|
|
|
|
|
|
object
|
2007-07-14 17:31:01 +00:00
|
|
|
hashMapRemove(Thread* t, object map, object key,
|
|
|
|
uint32_t (*hash)(Thread*, object),
|
|
|
|
bool (*equal)(Thread*, object, object))
|
2007-07-06 23:50:26 +00:00
|
|
|
{
|
2007-07-14 17:31:01 +00:00
|
|
|
bool weak = hashMapType(t, map) == WeakMap;
|
2007-07-06 23:50:26 +00:00
|
|
|
object array = hashMapArray(t, map);
|
|
|
|
object o = 0;
|
|
|
|
if (array) {
|
2007-07-14 17:31:01 +00:00
|
|
|
unsigned index = hash(t, key) & (arrayLength(t, array) - 1);
|
2007-07-11 04:19:26 +00:00
|
|
|
for (object* n = &arrayBody(t, array, index); *n;) {
|
2007-07-14 17:31:01 +00:00
|
|
|
object k = tripleFirst(t, *n);
|
|
|
|
if (weak) {
|
|
|
|
k = jreferenceTarget(t, k);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (equal(t, key, k)) {
|
2007-07-11 04:19:26 +00:00
|
|
|
o = tripleSecond(t, *n);
|
|
|
|
set(t, *n, tripleThird(t, *n));
|
|
|
|
-- hashMapSize(t, map);
|
2007-07-21 20:44:39 +00:00
|
|
|
break;
|
2007-07-11 04:19:26 +00:00
|
|
|
} else {
|
|
|
|
n = &tripleThird(t, *n);
|
2007-07-06 23:50:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-11 04:19:26 +00:00
|
|
|
if (hashMapSize(t, map) <= arrayLength(t, array) / 3) {
|
2007-07-19 23:45:44 +00:00
|
|
|
PROTECT(t, o);
|
2007-07-11 04:19:26 +00:00
|
|
|
hashMapResize(t, map, hash, arrayLength(t, array) / 2);
|
|
|
|
}
|
2007-07-06 23:50:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
object
|
|
|
|
makeTrace(Thread* t, int frame)
|
|
|
|
{
|
|
|
|
unsigned count = 0;
|
|
|
|
for (int f = frame; f >= 0; f = frameNext(t, f)) {
|
|
|
|
++ count;
|
|
|
|
}
|
|
|
|
|
2007-07-14 17:31:01 +00:00
|
|
|
object trace = makeArray(t, count, true);
|
2007-07-06 23:50:26 +00:00
|
|
|
PROTECT(t, trace);
|
|
|
|
|
|
|
|
unsigned index = 0;
|
|
|
|
for (int f = frame; f >= 0; f = frameNext(t, f)) {
|
2007-07-14 18:37:04 +00:00
|
|
|
object e = makeTraceElement(t, frameMethod(t, f), frameIp(t, f));
|
2007-07-14 17:31:01 +00:00
|
|
|
set(t, arrayBody(t, trace, index++), e);
|
2007-07-06 23:50:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return trace;
|
|
|
|
}
|
|
|
|
|
|
|
|
object
|
|
|
|
hashMapIterator(Thread* t, object map)
|
|
|
|
{
|
|
|
|
object array = hashMapArray(t, map);
|
|
|
|
if (array) {
|
|
|
|
for (unsigned i = 0; i < arrayLength(t, array); ++i) {
|
|
|
|
if (arrayBody(t, array, i)) {
|
|
|
|
return makeHashMapIterator(t, map, arrayBody(t, array, i), i + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
object
|
|
|
|
hashMapIteratorNext(Thread* t, object it)
|
|
|
|
{
|
|
|
|
object map = hashMapIteratorMap(t, it);
|
|
|
|
object node = hashMapIteratorNode(t, it);
|
|
|
|
unsigned index = hashMapIteratorIndex(t, it);
|
|
|
|
|
|
|
|
if (tripleThird(t, node)) {
|
|
|
|
return makeHashMapIterator(t, map, tripleThird(t, node), index + 1);
|
|
|
|
} else {
|
|
|
|
object array = hashMapArray(t, map);
|
|
|
|
for (unsigned i = index; i < arrayLength(t, array); ++i) {
|
|
|
|
if (arrayBody(t, array, i)) {
|
|
|
|
return makeHashMapIterator(t, map, arrayBody(t, array, i), i + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
listAppend(Thread* t, object list, object value)
|
|
|
|
{
|
|
|
|
PROTECT(t, list);
|
|
|
|
|
|
|
|
++ listSize(t, list);
|
|
|
|
|
|
|
|
object p = makePair(t, value, 0);
|
|
|
|
if (listFront(t, list)) {
|
|
|
|
set(t, pairSecond(t, listRear(t, list)), p);
|
|
|
|
} else {
|
|
|
|
set(t, listFront(t, list), p);
|
|
|
|
}
|
|
|
|
set(t, listRear(t, list), p);
|
|
|
|
}
|
|
|
|
|
2007-07-14 17:31:01 +00:00
|
|
|
unsigned
|
|
|
|
fieldCode(Thread* t, unsigned javaCode)
|
2007-07-06 23:50:26 +00:00
|
|
|
{
|
2007-07-14 17:31:01 +00:00
|
|
|
switch (javaCode) {
|
|
|
|
case 'B':
|
|
|
|
return ByteField;
|
|
|
|
case 'C':
|
|
|
|
return CharField;
|
|
|
|
case 'D':
|
|
|
|
return DoubleField;
|
|
|
|
case 'F':
|
|
|
|
return FloatField;
|
|
|
|
case 'I':
|
|
|
|
return IntField;
|
|
|
|
case 'J':
|
|
|
|
return LongField;
|
|
|
|
case 'S':
|
|
|
|
return ShortField;
|
|
|
|
case 'V':
|
|
|
|
return VoidField;
|
|
|
|
case 'Z':
|
|
|
|
return BooleanField;
|
|
|
|
case 'L':
|
|
|
|
case '[':
|
|
|
|
return ObjectField;
|
2007-07-06 23:50:26 +00:00
|
|
|
|
2007-07-14 17:31:01 +00:00
|
|
|
default: abort(t);
|
|
|
|
}
|
|
|
|
}
|
2007-07-06 23:50:26 +00:00
|
|
|
|
2007-07-14 17:31:01 +00:00
|
|
|
unsigned
|
|
|
|
fieldType(Thread* t, unsigned code)
|
|
|
|
{
|
|
|
|
switch (code) {
|
|
|
|
case VoidField:
|
|
|
|
return VOID_TYPE;
|
|
|
|
case ByteField:
|
|
|
|
case BooleanField:
|
|
|
|
return INT8_TYPE;
|
|
|
|
case CharField:
|
|
|
|
case ShortField:
|
|
|
|
return INT16_TYPE;
|
|
|
|
case DoubleField:
|
|
|
|
return DOUBLE_TYPE;
|
|
|
|
case FloatField:
|
|
|
|
return FLOAT_TYPE;
|
|
|
|
case IntField:
|
|
|
|
return INT32_TYPE;
|
|
|
|
case LongField:
|
|
|
|
return INT64_TYPE;
|
|
|
|
case ObjectField:
|
|
|
|
return POINTER_TYPE;
|
|
|
|
|
|
|
|
default: abort(t);
|
|
|
|
}
|
2007-07-06 23:50:26 +00:00
|
|
|
}
|
|
|
|
|
2007-07-14 17:31:01 +00:00
|
|
|
unsigned
|
|
|
|
primitiveSize(Thread* t, unsigned code)
|
2007-07-11 04:19:26 +00:00
|
|
|
{
|
2007-07-14 17:31:01 +00:00
|
|
|
switch (code) {
|
|
|
|
case VoidField:
|
|
|
|
return 0;
|
|
|
|
case ByteField:
|
|
|
|
case BooleanField:
|
|
|
|
return 1;
|
|
|
|
case CharField:
|
|
|
|
case ShortField:
|
|
|
|
return 2;
|
|
|
|
case FloatField:
|
|
|
|
case IntField:
|
|
|
|
return 4;
|
|
|
|
case DoubleField:
|
|
|
|
case LongField:
|
|
|
|
return 8;
|
2007-07-11 04:19:26 +00:00
|
|
|
|
2007-07-14 17:31:01 +00:00
|
|
|
default: abort(t);
|
|
|
|
}
|
|
|
|
}
|
2007-07-11 04:19:26 +00:00
|
|
|
|
2007-07-14 17:31:01 +00:00
|
|
|
object
|
|
|
|
resolveClass(Thread* t, object spec)
|
|
|
|
{
|
|
|
|
PROTECT(t, spec);
|
|
|
|
ACQUIRE(t, t->vm->classLock);
|
|
|
|
|
|
|
|
object class_ = hashMapFind
|
|
|
|
(t, t->vm->classMap, spec, byteArrayHash, byteArrayEqual);
|
|
|
|
if (class_ == 0) {
|
|
|
|
if (byteArrayBody(t, spec, 0) == '[') {
|
|
|
|
class_ = hashMapFind
|
|
|
|
(t, t->vm->bootstrapClassMap, spec, byteArrayHash, byteArrayEqual);
|
|
|
|
|
|
|
|
if (class_ == 0) {
|
|
|
|
class_ = makeArrayClass(t, spec);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ClassFinder::Data* data = t->vm->classFinder->find
|
|
|
|
(reinterpret_cast<const char*>(&byteArrayBody(t, spec, 0)));
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
if (Verbose) {
|
2007-07-24 03:31:28 +00:00
|
|
|
fprintf(stderr, "parsing %s\n", &byteArrayBody(t, spec, 0));
|
2007-07-14 17:31:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// parse class file
|
|
|
|
class_ = parseClass(t, data->start(), data->length());
|
|
|
|
data->dispose();
|
|
|
|
|
2007-07-24 03:31:28 +00:00
|
|
|
if (LIKELY(t->exception == 0)) {
|
|
|
|
if (Verbose) {
|
|
|
|
fprintf(stderr, "done parsing %s\n", &byteArrayBody(t, spec, 0));
|
|
|
|
}
|
2007-07-14 17:31:01 +00:00
|
|
|
|
2007-07-24 03:31:28 +00:00
|
|
|
object bootstrapClass = hashMapFind
|
|
|
|
(t, t->vm->bootstrapClassMap, spec, byteArrayHash, byteArrayEqual);
|
2007-07-14 17:31:01 +00:00
|
|
|
|
2007-07-24 03:31:28 +00:00
|
|
|
if (bootstrapClass) {
|
|
|
|
PROTECT(t, bootstrapClass);
|
2007-07-14 17:31:01 +00:00
|
|
|
|
2007-07-24 03:31:28 +00:00
|
|
|
updateBootstrapClass(t, bootstrapClass, class_);
|
|
|
|
class_ = bootstrapClass;
|
|
|
|
}
|
2007-07-14 17:31:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (class_) {
|
2007-07-16 23:58:37 +00:00
|
|
|
PROTECT(t, class_);
|
|
|
|
|
2007-07-14 17:31:01 +00:00
|
|
|
hashMapInsert(t, t->vm->classMap, spec, class_, byteArrayHash);
|
|
|
|
} else if (t->exception == 0) {
|
|
|
|
object message = makeString(t, "%s", &byteArrayBody(t, spec, 0));
|
|
|
|
t->exception = makeClassNotFoundException(t, message);
|
|
|
|
}
|
2007-07-11 04:19:26 +00:00
|
|
|
}
|
|
|
|
|
2007-07-14 17:31:01 +00:00
|
|
|
return class_;
|
|
|
|
}
|
|
|
|
|
|
|
|
object
|
|
|
|
resolveObjectArrayClass(Thread* t, object elementSpec)
|
|
|
|
{
|
|
|
|
PROTECT(t, elementSpec);
|
|
|
|
|
|
|
|
object spec;
|
|
|
|
if (byteArrayBody(t, elementSpec, 0) == '[') {
|
|
|
|
spec = makeByteArray(t, byteArrayLength(t, elementSpec) + 1, false);
|
|
|
|
byteArrayBody(t, elementSpec, 0) = '[';
|
|
|
|
memcpy(&byteArrayBody(t, spec, 1),
|
|
|
|
&byteArrayBody(t, elementSpec, 0),
|
|
|
|
byteArrayLength(t, elementSpec));
|
|
|
|
} else {
|
|
|
|
spec = makeByteArray(t, byteArrayLength(t, elementSpec) + 3, false);
|
|
|
|
byteArrayBody(t, spec, 0) = '[';
|
|
|
|
byteArrayBody(t, spec, 1) = 'L';
|
|
|
|
memcpy(&byteArrayBody(t, spec, 2),
|
|
|
|
&byteArrayBody(t, elementSpec, 0),
|
|
|
|
byteArrayLength(t, elementSpec) - 1);
|
|
|
|
byteArrayBody(t, spec, byteArrayLength(t, elementSpec) + 1) = ';';
|
|
|
|
byteArrayBody(t, spec, byteArrayLength(t, elementSpec) + 2) = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolveClass(t, spec);
|
|
|
|
}
|
|
|
|
|
|
|
|
object
|
|
|
|
makeObjectArray(Thread* t, object elementClass, unsigned count, bool clear)
|
|
|
|
{
|
|
|
|
object arrayClass = resolveObjectArrayClass(t, className(t, elementClass));
|
|
|
|
PROTECT(t, arrayClass);
|
|
|
|
|
|
|
|
object array = makeArray(t, count, clear);
|
|
|
|
setObjectClass(t, array, arrayClass);
|
|
|
|
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2007-07-14 18:37:04 +00:00
|
|
|
int
|
|
|
|
lineNumber(Thread* t, object method, unsigned ip)
|
|
|
|
{
|
|
|
|
if (methodFlags(t, method) & ACC_NATIVE) {
|
|
|
|
return NativeLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
object table = codeLineNumberTable(t, methodCode(t, method));
|
|
|
|
if (table) {
|
|
|
|
// todo: do a binary search:
|
|
|
|
int last = UnknownLine;
|
|
|
|
for (unsigned i = 0; i < lineNumberTableLength(t, table); ++i) {
|
|
|
|
if (ip <= lineNumberIp(lineNumberTableBody(t, table, i))) {
|
|
|
|
return last;
|
|
|
|
} else {
|
|
|
|
last = lineNumberLine(lineNumberTableBody(t, table, i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return last;
|
|
|
|
} else {
|
|
|
|
return UnknownLine;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-14 17:31:01 +00:00
|
|
|
void
|
|
|
|
addFinalizer(Thread* t, object target, void (*finalize)(Thread*, object))
|
|
|
|
{
|
|
|
|
PROTECT(t, target);
|
|
|
|
|
2007-07-20 03:18:25 +00:00
|
|
|
ACQUIRE(t, t->vm->referenceLock);
|
2007-07-14 17:31:01 +00:00
|
|
|
|
|
|
|
t->vm->finalizers = makeFinalizer
|
2007-07-19 23:45:44 +00:00
|
|
|
(t, 0, reinterpret_cast<void*>(finalize), t->vm->finalizers);
|
|
|
|
finalizerTarget(t, t->vm->finalizers) = target;
|
2007-07-11 04:19:26 +00:00
|
|
|
}
|
|
|
|
|
2007-07-06 23:50:26 +00:00
|
|
|
System::Monitor*
|
|
|
|
objectMonitor(Thread* t, object o)
|
|
|
|
{
|
2007-07-14 17:31:01 +00:00
|
|
|
object p = hashMapFind(t, t->vm->monitorMap, o, objectHash, objectEqual);
|
2007-07-06 23:50:26 +00:00
|
|
|
|
|
|
|
if (p) {
|
2007-07-10 23:34:53 +00:00
|
|
|
if (DebugMonitors) {
|
2007-07-11 04:19:26 +00:00
|
|
|
fprintf(stderr, "found monitor %p for object %x\n",
|
2007-07-10 23:34:53 +00:00
|
|
|
static_cast<System::Monitor*>(pointerValue(t, p)),
|
|
|
|
objectHash(t, o));
|
|
|
|
}
|
2007-07-10 03:04:49 +00:00
|
|
|
|
2007-07-06 23:50:26 +00:00
|
|
|
return static_cast<System::Monitor*>(pointerValue(t, p));
|
|
|
|
} else {
|
|
|
|
PROTECT(t, o);
|
|
|
|
|
|
|
|
ENTER(t, Thread::ExclusiveState);
|
|
|
|
|
|
|
|
System::Monitor* m;
|
|
|
|
System::Status s = t->vm->system->make(&m);
|
|
|
|
expect(t, t->vm->system->success(s));
|
|
|
|
|
2007-07-10 23:34:53 +00:00
|
|
|
if (DebugMonitors) {
|
2007-07-11 04:19:26 +00:00
|
|
|
fprintf(stderr, "made monitor %p for object %x\n",
|
2007-07-10 23:34:53 +00:00
|
|
|
m,
|
|
|
|
objectHash(t, o));
|
|
|
|
}
|
2007-07-06 23:50:26 +00:00
|
|
|
|
2007-07-14 17:31:01 +00:00
|
|
|
p = makePointer(t, m);
|
|
|
|
hashMapInsert(t, t->vm->monitorMap, o, p, objectHash);
|
2007-07-06 23:50:26 +00:00
|
|
|
|
|
|
|
addFinalizer(t, o, removeMonitor);
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-16 01:03:02 +00:00
|
|
|
void
|
|
|
|
collect(Thread* t, Heap::CollectionType type)
|
|
|
|
{
|
|
|
|
Machine* m = t->vm;
|
|
|
|
|
|
|
|
class Client: public Heap::Client {
|
|
|
|
public:
|
|
|
|
Client(Machine* m): m(m) { }
|
|
|
|
|
|
|
|
virtual void visitRoots(Heap::Visitor* v) {
|
|
|
|
v->visit(&(m->classMap));
|
|
|
|
v->visit(&(m->bootstrapClassMap));
|
|
|
|
v->visit(&(m->builtinMap));
|
|
|
|
v->visit(&(m->monitorMap));
|
|
|
|
v->visit(&(m->types));
|
|
|
|
|
|
|
|
for (Thread* t = m->rootThread; t; t = t->peer) {
|
|
|
|
::visitRoots(t, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
postVisit(m->rootThread, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual unsigned sizeInWords(object o) {
|
|
|
|
Thread* t = m->rootThread;
|
|
|
|
|
|
|
|
o = m->heap->follow(mask(o));
|
|
|
|
|
|
|
|
return extendedSize
|
|
|
|
(t, o, baseSize(t, o, m->heap->follow(objectClass(t, o))));
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual unsigned copiedSizeInWords(object o) {
|
|
|
|
Thread* t = m->rootThread;
|
|
|
|
|
|
|
|
o = m->heap->follow(mask(o));
|
|
|
|
|
|
|
|
unsigned n = baseSize(t, o, m->heap->follow(objectClass(t, o)));
|
|
|
|
|
|
|
|
if (objectExtended(t, o) or hashTaken(t, o)) {
|
|
|
|
++ n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void copy(object o, object dst) {
|
|
|
|
Thread* t = m->rootThread;
|
|
|
|
|
|
|
|
o = m->heap->follow(mask(o));
|
|
|
|
object class_ = m->heap->follow(objectClass(t, o));
|
|
|
|
|
|
|
|
unsigned base = baseSize(t, o, class_);
|
|
|
|
unsigned n = extendedSize(t, o, base);
|
|
|
|
|
|
|
|
memcpy(dst, o, n * BytesPerWord);
|
|
|
|
|
|
|
|
if (hashTaken(t, o)) {
|
|
|
|
cast<uintptr_t>(dst, 0) &= PointerMask;
|
|
|
|
cast<uintptr_t>(dst, 0) |= ExtendedMark;
|
|
|
|
extendedWord(t, dst, base) = takeHash(t, o);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void walk(void* p, Heap::Walker* w) {
|
|
|
|
Thread* t = m->rootThread;
|
|
|
|
|
|
|
|
p = m->heap->follow(mask(p));
|
|
|
|
object class_ = m->heap->follow(objectClass(t, p));
|
|
|
|
object objectMask = m->heap->follow(classObjectMask(t, class_));
|
|
|
|
|
|
|
|
if (objectMask) {
|
|
|
|
// fprintf(stderr, "p: %p; class: %p; mask: %p; mask length: %d\n",
|
|
|
|
// p, class_, objectMask, intArrayLength(t, objectMask));
|
|
|
|
|
|
|
|
unsigned fixedSize = classFixedSize(t, class_);
|
|
|
|
unsigned arrayElementSize = classArrayElementSize(t, class_);
|
|
|
|
unsigned arrayLength
|
|
|
|
= (arrayElementSize ?
|
|
|
|
cast<uintptr_t>(p, fixedSize - BytesPerWord) : 0);
|
|
|
|
|
|
|
|
int mask[intArrayLength(t, objectMask)];
|
|
|
|
memcpy(mask, &intArrayBody(t, objectMask, 0),
|
|
|
|
intArrayLength(t, objectMask) * 4);
|
|
|
|
|
|
|
|
// fprintf
|
|
|
|
// (stderr,
|
|
|
|
// "fixed size: %d; array length: %d; element size: %d; mask: %x\n",
|
|
|
|
// fixedSize, arrayLength, arrayElementSize, mask[0]);
|
|
|
|
|
2007-07-26 00:48:28 +00:00
|
|
|
unsigned fixedSizeInWords = ceiling(fixedSize, BytesPerWord);
|
2007-07-16 01:03:02 +00:00
|
|
|
unsigned arrayElementSizeInWords
|
2007-07-26 00:48:28 +00:00
|
|
|
= ceiling(arrayElementSize, BytesPerWord);
|
2007-07-16 01:03:02 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < fixedSizeInWords; ++i) {
|
|
|
|
if (mask[wordOf(i)] & (static_cast<uintptr_t>(1) << bitOf(i))) {
|
|
|
|
if (not w->visit(i)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool arrayObjectElements = false;
|
|
|
|
for (unsigned j = 0; j < arrayElementSizeInWords; ++j) {
|
|
|
|
unsigned k = fixedSizeInWords + j;
|
|
|
|
if (mask[wordOf(k)] & (static_cast<uintptr_t>(1) << bitOf(k))) {
|
|
|
|
arrayObjectElements = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arrayObjectElements) {
|
|
|
|
for (unsigned i = 0; i < arrayLength; ++i) {
|
|
|
|
for (unsigned j = 0; j < arrayElementSizeInWords; ++j) {
|
|
|
|
unsigned k = fixedSizeInWords + j;
|
|
|
|
if (mask[wordOf(k)] & (static_cast<uintptr_t>(1) << bitOf(k))) {
|
|
|
|
if (not w->visit
|
|
|
|
(fixedSizeInWords + (i * arrayElementSizeInWords) + j))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
w->visit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Machine* m;
|
|
|
|
} it(m);
|
|
|
|
|
|
|
|
m->unsafe = true;
|
|
|
|
m->heap->collect(type, &it);
|
|
|
|
m->unsafe = false;
|
|
|
|
|
|
|
|
postCollect(m->rootThread);
|
|
|
|
|
|
|
|
for (object f = m->finalizeQueue; f; f = finalizerNext(t, f)) {
|
|
|
|
reinterpret_cast<void (*)(Thread*, object)>(finalizerFinalize(t, f))
|
|
|
|
(t, finalizerTarget(t, f));
|
|
|
|
}
|
|
|
|
m->finalizeQueue = 0;
|
|
|
|
|
|
|
|
killZombies(t, m->rootThread);
|
|
|
|
}
|
|
|
|
|
2007-07-24 03:16:59 +00:00
|
|
|
void
|
|
|
|
printTrace(Thread* t, object exception)
|
|
|
|
{
|
|
|
|
for (object e = exception; e; e = throwableCauseUnsafe(t, e)) {
|
|
|
|
if (e != exception) {
|
|
|
|
fprintf(stderr, "caused by: ");
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "%s", &byteArrayBody
|
|
|
|
(t, className(t, objectClass(t, e)), 0));
|
|
|
|
|
|
|
|
if (throwableMessageUnsafe(t, e)) {
|
|
|
|
object m = throwableMessageUnsafe(t, e);
|
|
|
|
char message[stringLength(t, m) + 1];
|
|
|
|
stringChars(t, m, message);
|
|
|
|
fprintf(stderr, ": %s\n", message);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
object trace = throwableTraceUnsafe(t, e);
|
|
|
|
for (unsigned i = 0; i < arrayLength(t, trace); ++i) {
|
|
|
|
object e = arrayBody(t, trace, i);
|
|
|
|
const int8_t* class_ = &byteArrayBody
|
|
|
|
(t, className(t, methodClass(t, traceElementMethod(t, e))), 0);
|
|
|
|
const int8_t* method = &byteArrayBody
|
|
|
|
(t, methodName(t, traceElementMethod(t, e)), 0);
|
|
|
|
int line = lineNumber(t, traceElementMethod(t, e), traceElementIp(t, e));
|
|
|
|
|
|
|
|
fprintf(stderr, " at %s.%s ", class_, method);
|
|
|
|
|
|
|
|
switch (line) {
|
|
|
|
case NativeLine:
|
|
|
|
fprintf(stderr, "(native)\n");
|
|
|
|
break;
|
|
|
|
case UnknownLine:
|
|
|
|
fprintf(stderr, "(unknown line)\n");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "(line %d)\n", line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-10 01:43:43 +00:00
|
|
|
void
|
|
|
|
noop()
|
|
|
|
{ }
|
|
|
|
|
2007-07-06 23:50:26 +00:00
|
|
|
#include "type-constructors.cpp"
|
|
|
|
|
|
|
|
} // namespace vm
|