From 94338e15a89f3ee91ddd21a3e57040b3706ccf12 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Wed, 6 Jun 2007 18:30:16 -0600 Subject: [PATCH] implement makeJString(); minor tweaks elsewhere --- src/class_finder.h | 13 ++++++++ src/common.h | 3 ++ src/system.h | 2 -- src/types.def | 2 -- src/vm.cpp | 82 +++++++++++++++++++++++++++++++++++++--------- 5 files changed, 83 insertions(+), 19 deletions(-) create mode 100644 src/class_finder.h diff --git a/src/class_finder.h b/src/class_finder.h new file mode 100644 index 0000000000..28d366e27e --- /dev/null +++ b/src/class_finder.h @@ -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 diff --git a/src/common.h b/src/common.h index 21c0ba0ff1..0148902304 100644 --- a/src/common.h +++ b/src/common.h @@ -2,6 +2,9 @@ #define COMMON_H #include "stdint.h" +#include "stdarg.h" +#include "string.h" +#include "stdio.h" #define NO_RETURN __attribute__((noreturn)) #define UNLIKELY(v) __builtin_expect(v, 0) diff --git a/src/system.h b/src/system.h index 42c44c7984..b459dd4705 100644 --- a/src/system.h +++ b/src/system.h @@ -63,9 +63,7 @@ class System { virtual bool success(Status) = 0; virtual void* allocate(unsigned size) = 0; - virtual void zero(void*, unsigned size) = 0; virtual void free(void*) = 0; - virtual void copy(void* src, void* dst, unsigned size); virtual Status start(Thread*) = 0; virtual Status make(Monitor**) = 0; virtual Status open(File**, const char* path, int flags, int mode) = 0; diff --git a/src/types.def b/src/types.def index 23eea6eafa..40cb9bd6ac 100644 --- a/src/types.def +++ b/src/types.def @@ -49,8 +49,6 @@ (object spec)) (type string - (uint32_t hash) - (uint32_t id) (array char value)) (type byte diff --git a/src/vm.cpp b/src/vm.cpp index 14bd03c6b6..10e4b9550a 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -2,6 +2,10 @@ #include "system.h" #include "heap.h" +#define PROTECTED(thread, name, value) \ + Protector MAKE_NAME(protector_) (thread, value); \ + object& name(thread->stack[thread->sp - 1]); + namespace { typedef void* object; @@ -36,6 +40,7 @@ class Machine { unsigned activeCount; unsigned liveCount; System::Monitor* stateLock; + object jstringClass; }; class Thread { @@ -80,7 +85,7 @@ assert(Thread* t, bool v) void init(Machine* m, System* sys, Heap* heap) { - sys->zero(m, sizeof(Machine)); + memset(m, 0, sizeof(Machine)); m->sys = sys; m->heap = heap; if (not sys->success(sys->make(&(m->stateLock)))) { @@ -97,7 +102,7 @@ dispose(Machine* m) void init(Thread* t, Machine* m) { - m->sys->zero(m, sizeof(Thread)); + memset(m, 0, sizeof(Thread)); t->vm = m; m->rootThread = t; t->state = Thread::NoState; @@ -290,6 +295,60 @@ pop(Thread* t) 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(instance) = class_; + memset(static_cast(instance) + sizeof(object), 0, + size - sizeof(object)); + return instance; +} + +template +inline T& +cast(object p, unsigned offset) +{ + return *reinterpret_cast(static_cast(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(r, sizeof(void*)) = s; + cast(r, sizeof(void*) * 2) = 0; + cast(r, (sizeof(void*) * 2) + sizeof(int32_t)) = stringLength(t, s); + return r; +} + object run(Thread* t) { @@ -386,7 +445,7 @@ run(Thread* t) if (t->exception) goto throw_; 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); } else { @@ -418,8 +477,7 @@ run(Thread* t) push(t, makeInt(t, objectArrayLength(t, array))); } else { // for all other array types, the length follow the class pointer. - push(t, makeInt(t, reinterpret_cast - (static_cast(array)[1]))); + push(t, makeInt(t, cast(array, sizeof(void*)))); } } else { t->exception = makeNullPointerException(t, 0); @@ -1379,13 +1437,7 @@ run(Thread* t) goto invoke; } - unsigned size = instanceSize(class_); - object instance = allocate(t, size); - *static_cast(instance) = class_; - t->vm->sys->zero(static_cast(instance) + sizeof(object), - size - sizeof(object)); - - push(t, instance); + push(t, make(t, class_)); } goto loop; case newarray: { @@ -1442,8 +1494,8 @@ run(Thread* t) default: abort(t); } - t->vm->sys->zero(static_cast(instance) + (sizeof(object) * 2), - c * factor); + memset(static_cast(instance) + (sizeof(object) * 2), 0 + c * factor); push(t, array); } else { @@ -1637,7 +1689,7 @@ run(Thread* t) t->sp -= parameterCount; t->frame = makeFrame(t, t->code, t->frame, 0, t->sp, 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; goto loop;