implement makeJString(); minor tweaks elsewhere

This commit is contained in:
Joel Dice 2007-06-06 18:30:16 -06:00
parent 48e49f1fa1
commit 94338e15a8
5 changed files with 83 additions and 19 deletions

13
src/class_finder.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef CLASS_FINDER_H
#define CLASS_FINDER_H
#include "common.h"
class ClassFinder {
public:
virtual ~ClassFinder() { }
virtual const uint8_t* find(const char* className) = 0;
virtual void free(const uint8_t* class_) = 0;
};
#endif//CLASS_FINDER_H

View File

@ -2,6 +2,9 @@
#define COMMON_H #define COMMON_H
#include "stdint.h" #include "stdint.h"
#include "stdarg.h"
#include "string.h"
#include "stdio.h"
#define NO_RETURN __attribute__((noreturn)) #define NO_RETURN __attribute__((noreturn))
#define UNLIKELY(v) __builtin_expect(v, 0) #define UNLIKELY(v) __builtin_expect(v, 0)

View File

@ -63,9 +63,7 @@ class System {
virtual bool success(Status) = 0; virtual bool success(Status) = 0;
virtual void* allocate(unsigned size) = 0; virtual void* allocate(unsigned size) = 0;
virtual void zero(void*, unsigned size) = 0;
virtual void free(void*) = 0; virtual void free(void*) = 0;
virtual void copy(void* src, void* dst, unsigned size);
virtual Status start(Thread*) = 0; virtual Status start(Thread*) = 0;
virtual Status make(Monitor**) = 0; virtual Status make(Monitor**) = 0;
virtual Status open(File**, const char* path, int flags, int mode) = 0; virtual Status open(File**, const char* path, int flags, int mode) = 0;

View File

@ -49,8 +49,6 @@
(object spec)) (object spec))
(type string (type string
(uint32_t hash)
(uint32_t id)
(array char value)) (array char value))
(type byte (type byte

View File

@ -2,6 +2,10 @@
#include "system.h" #include "system.h"
#include "heap.h" #include "heap.h"
#define PROTECTED(thread, name, value) \
Protector MAKE_NAME(protector_) (thread, value); \
object& name(thread->stack[thread->sp - 1]);
namespace { namespace {
typedef void* object; typedef void* object;
@ -36,6 +40,7 @@ class Machine {
unsigned activeCount; unsigned activeCount;
unsigned liveCount; unsigned liveCount;
System::Monitor* stateLock; System::Monitor* stateLock;
object jstringClass;
}; };
class Thread { class Thread {
@ -80,7 +85,7 @@ assert(Thread* t, bool v)
void void
init(Machine* m, System* sys, Heap* heap) init(Machine* m, System* sys, Heap* heap)
{ {
sys->zero(m, sizeof(Machine)); memset(m, 0, sizeof(Machine));
m->sys = sys; m->sys = sys;
m->heap = heap; m->heap = heap;
if (not sys->success(sys->make(&(m->stateLock)))) { if (not sys->success(sys->make(&(m->stateLock)))) {
@ -97,7 +102,7 @@ dispose(Machine* m)
void void
init(Thread* t, Machine* m) init(Thread* t, Machine* m)
{ {
m->sys->zero(m, sizeof(Thread)); memset(m, 0, sizeof(Thread));
t->vm = m; t->vm = m;
m->rootThread = t; m->rootThread = t;
t->state = Thread::NoState; t->state = Thread::NoState;
@ -290,6 +295,60 @@ pop(Thread* t)
return t->stack[--(t->sp)]; return t->stack[--(t->sp)];
} }
class Protector {
public:
Protector(Thread* t, object value): t(t) {
if (t->sp >= Thread::StackSize) abort(t);
push(t, value);
}
~Protector() {
pop(t);
}
private:
Thread* t;
};
inline object
make(Thread* t, object class_)
{
unsigned size = classFixedSize(t, class_);
object instance = allocate(t, size);
*static_cast<object*>(instance) = class_;
memset(static_cast<object*>(instance) + sizeof(object), 0,
size - sizeof(object));
return instance;
}
template <class T>
inline T&
cast(object p, unsigned offset)
{
return *reinterpret_cast<T*>(static_cast<uint8_t*>(p) + offset);
}
object
makeJString(Thread* t, const char* format, ...)
{
static const unsigned Size = 256;
char buffer[Size];
va_list a;
va_start(a, format);
vsnprintf(buffer, Size - 1, format, a);
va_end(a);
PROTECTED(t, s, makeString(t, strlen(buffer) + 1));
memcpy(stringValue(t, s), buffer, stringLength(t, s));
object r = make(t, t->vm->jstringClass);
cast<object>(r, sizeof(void*)) = s;
cast<int32_t>(r, sizeof(void*) * 2) = 0;
cast<int32_t>(r, (sizeof(void*) * 2) + sizeof(int32_t)) = stringLength(t, s);
return r;
}
object object
run(Thread* t) run(Thread* t)
{ {
@ -386,7 +445,7 @@ run(Thread* t)
if (t->exception) goto throw_; if (t->exception) goto throw_;
object array = makeObjectArray(t, class_, c); object array = makeObjectArray(t, class_, c);
t->vm->sys->zero(objectArrayBody(t, array), c * 4); memset(objectArrayBody(t, array), 0, c * 4);
push(t, array); push(t, array);
} else { } else {
@ -418,8 +477,7 @@ run(Thread* t)
push(t, makeInt(t, objectArrayLength(t, array))); push(t, makeInt(t, objectArrayLength(t, array)));
} else { } else {
// for all other array types, the length follow the class pointer. // for all other array types, the length follow the class pointer.
push(t, makeInt(t, reinterpret_cast<uint32_t&> push(t, makeInt(t, cast<uint32_t>(array, sizeof(void*))));
(static_cast<uintptr_t*>(array)[1])));
} }
} else { } else {
t->exception = makeNullPointerException(t, 0); t->exception = makeNullPointerException(t, 0);
@ -1379,13 +1437,7 @@ run(Thread* t)
goto invoke; goto invoke;
} }
unsigned size = instanceSize(class_); push(t, make(t, class_));
object instance = allocate(t, size);
*static_cast<object*>(instance) = class_;
t->vm->sys->zero(static_cast<object*>(instance) + sizeof(object),
size - sizeof(object));
push(t, instance);
} goto loop; } goto loop;
case newarray: { case newarray: {
@ -1442,8 +1494,8 @@ run(Thread* t)
default: abort(t); default: abort(t);
} }
t->vm->sys->zero(static_cast<object*>(instance) + (sizeof(object) * 2), memset(static_cast<object*>(instance) + (sizeof(object) * 2), 0
c * factor); c * factor);
push(t, array); push(t, array);
} else { } else {
@ -1637,7 +1689,7 @@ run(Thread* t)
t->sp -= parameterCount; t->sp -= parameterCount;
t->frame = makeFrame(t, t->code, t->frame, 0, t->sp, t->frame = makeFrame(t, t->code, t->frame, 0, t->sp,
codeMaxLocals(t, methodCode(t, t->code))); codeMaxLocals(t, methodCode(t, t->code)));
t->vm->sys->copy(t->stack + t->sp, frameLocals(t, t->frame), parameterCount); memcpy(frameLocals(t, t->frame), t->stack + t->sp, parameterCount);
ip = 0; ip = 0;
goto loop; goto loop;