This commit is contained in:
Joel Dice 2007-10-02 19:54:21 -06:00
parent f7058f8fd6
commit ba03aa88df
2 changed files with 78 additions and 54 deletions

View File

@ -124,27 +124,44 @@ class Buffer {
class Code { class Code {
public: public:
Code(Buffer* code, Buffer* lineNumbers, Buffer* exceptionHandlers): Code(Buffer* code, Buffer* lineNumbers, Buffer* exceptionHandlers):
codeLength(code->length()), codeLength_(code->length()),
lineNumberTableLength(lineNumbers->length()), lineNumberTableLength_(lineNumbers->length()),
exceptionHandlerTableLength(exceptionHandlers->length()) exceptionHandlerTableLength_(exceptionHandlers->length())
{ {
code->copyTo(body); code->copyTo(body);
lineNumbers->copyTo(lineNumberTable()); lineNumbers->copyTo(lineNumber(0));
exceptionHandlers->copyTo(exceptionHandlerTable()); exceptionHandlers->copyTo(exceptionHandler(0));
} }
uint32_t* lineNumberTable() { uint8_t* code() {
return reinterpret_cast<uint32_t*>(body + pad(codeLength)); return body;
} }
uint32_t* exceptionHandlerTable() { unsigned codeLength() {
return reinterpret_cast<uint32_t*> return codeLength_;
(body + pad(codeLength) + pad(lineNumberTableLength));
} }
uint32_t codeLength; NativeLineNumber* lineNumber(unsigned index) {
uint32_t lineNumberTableLength; return reinterpret_cast<NativeLineNumber*>
uint32_t exceptionHandlerTableLength; (body + pad(codeLength_)) + index;
}
unsigned lineNumberTableLength() {
return lineNumberTableLength_ / sizeof(NativeLineNumber);
}
NativeExceptionHandler* exceptionHandler(unsigned index) {
return reinterpret_cast<NativeExceptionHandler*>
(body + pad(codeLength_) + pad(lineNumberTableLength_)) + index;
}
unsigned exceptionHandlerTableLength() {
return exceptionHandlerTableLength_ / sizeof(NativeExceptionHandler);
}
uint32_t codeLength_;
uint32_t lineNumberTableLength_;
uint32_t exceptionHandlerTableLength_;
uint8_t body[0]; uint8_t body[0];
}; };
@ -202,33 +219,27 @@ inline unsigned
addressOffset(Thread* t, object method, void* address) addressOffset(Thread* t, object method, void* address)
{ {
Code* code = static_cast<Code*>(methodCompiled(t, method)); Code* code = static_cast<Code*>(methodCompiled(t, method));
return static_cast<uint8_t*>(address) - code->body; return static_cast<uint8_t*>(address) - code->code();
} }
const unsigned EHStart = 0; NativeExceptionHandler*
const unsigned EHEnd = 1;
const unsigned EHIp = 2;
const unsigned EHCatchType = 3;
const unsigned EHFootprint = 4;
uint32_t*
findExceptionHandler(Thread* t, void* frame) findExceptionHandler(Thread* t, void* frame)
{ {
object method = frameMethod(frame); object method = frameMethod(frame);
Code* code = static_cast<Code*>(methodCompiled(t, method)); Code* code = static_cast<Code*>(methodCompiled(t, method));
uint32_t* table = code->exceptionHandlerTable();
for (unsigned i = 0; i < code->exceptionHandlerTableLength / 4; ++i) { for (unsigned i = 0; i < code->exceptionHandlerTableLength(); ++i) {
uint32_t* handler = table + (i * EHFootprint); NativeExceptionHandler* handler = code->exceptionHandler(i);
unsigned offset = addressOffset(t, method, frameAddress(frame)); unsigned offset = addressOffset(t, method, frameAddress(frame));
if (offset - 1 >= handler[EHStart] if (offset - 1 >= nativeExceptionHandlerStart(handler)
and offset - 1 < handler[EHEnd]) and offset - 1 < nativeExceptionHandlerEnd(handler))
{ {
object catchType; object catchType;
if (handler[EHCatchType]) { if (nativeExceptionHandlerCatchType(handler)) {
catchType = arrayBody catchType = arrayBody
(t, methodCode(t, method), handler[EHCatchType] - 1); (t, methodCode(t, method),
nativeExceptionHandlerCatchType(handler) - 1);
} else { } else {
catchType = 0; catchType = 0;
} }
@ -253,11 +264,11 @@ unwind(MyThread* t)
t->frame = next; t->frame = next;
vmJump(frameReturnAddress(frame)); vmJump(frameReturnAddress(frame));
} else if ((methodFlags(t, frameMethod(frame)) & ACC_NATIVE) == 0) { } else if ((methodFlags(t, frameMethod(frame)) & ACC_NATIVE) == 0) {
uint32_t* eh = findExceptionHandler(t, frame); NativeExceptionHandler* eh = findExceptionHandler(t, frame);
if (eh) { if (eh) {
Code* code = static_cast<Code*>(methodCompiled(t, frameMethod(frame))); Code* code = static_cast<Code*>(methodCompiled(t, frameMethod(frame)));
t->frame = frame; t->frame = frame;
vmJump(code->body + eh[EHIp]); vmJump(code->code() + nativeExceptionHandlerIp(eh));
} }
} }
} }
@ -938,8 +949,12 @@ parameterOffset(unsigned index)
class Compiler: public Assembler { class Compiler: public Assembler {
public: public:
Compiler(System* s): Compiler(Thread* t):
Assembler(s), Assembler(s),
t(t),
threadFrameOffset(reinterpret_cast<uintptr_t>(&(t->frame))
- reinterpret_cast<uintptr_t>(t))
poolRegisterClobbered(true),
javaIPs(s, 1024), javaIPs(s, 1024),
machineIPs(s, 1024), machineIPs(s, 1024),
lineNumbers(s, 256), lineNumbers(s, 256),
@ -983,7 +998,7 @@ class Compiler: public Assembler {
push(poolRegister(), poolReference(target)); push(poolRegister(), poolReference(target));
push(rbp, FrameThread); push(rbp, FrameThread);
callAlignedAddress(code->body); callAlignedAddress(code->code());
add(footprint, rsp); // pop arguments add(footprint, rsp); // pop arguments
@ -991,9 +1006,6 @@ class Compiler: public Assembler {
} }
void compileCall(void* function, object arg1) { void compileCall(void* function, object arg1) {
mov(rbp, FrameThread, rax);
mov(rbp, rax, frameOffset); // set thread frame to current
if (BytesPerWord == 4) { if (BytesPerWord == 4) {
push(poolRegister(), poolReference(arg1)); push(poolRegister(), poolReference(arg1));
push(rbp, FrameThread); push(rbp, FrameThread);
@ -1002,6 +1014,9 @@ class Compiler: public Assembler {
mov(rbp, FrameThread, rdi); mov(rbp, FrameThread, rdi);
} }
mov(rbp, FrameThread, rax);
mov(rbp, rax, threadFrameOffset); // set thread frame to current
callAddress(function); callAddress(function);
if (BytesPerWord == 4) { if (BytesPerWord == 4) {
@ -1010,9 +1025,6 @@ class Compiler: public Assembler {
} }
void compileCall(void* function, Register arg1) { void compileCall(void* function, Register arg1) {
mov(rbp, FrameThread, rax);
mov(rbp, rax, frameOffset); // set thread frame to current
if (BytesPerWord == 4) { if (BytesPerWord == 4) {
push(arg1); push(arg1);
push(rbp, FrameThread); push(rbp, FrameThread);
@ -1021,6 +1033,9 @@ class Compiler: public Assembler {
mov(rbp, FrameThread, rdi); mov(rbp, FrameThread, rdi);
} }
mov(rbp, FrameThread, rax);
mov(rbp, rax, threadFrameOffset); // set thread frame to current
callAddress(function); callAddress(function);
if (BytesPerWord == 4) { if (BytesPerWord == 4) {
@ -1029,9 +1044,6 @@ class Compiler: public Assembler {
} }
void compileCall(void* function, object arg1, Register arg2) { void compileCall(void* function, object arg1, Register arg2) {
mov(rbp, FrameThread, rax);
mov(rbp, rax, frameOffset); // set thread frame to current
if (BytesPerWord == 4) { if (BytesPerWord == 4) {
push(arg2); push(arg2);
push(poolRegister(), poolReference(arg1)); push(poolRegister(), poolReference(arg1));
@ -1042,6 +1054,9 @@ class Compiler: public Assembler {
mov(rbp, FrameThread, rdi); mov(rbp, FrameThread, rdi);
} }
mov(rbp, FrameThread, rax);
mov(rbp, rax, threadFrameOffset); // set thread frame to current
callAddress(function); callAddress(function);
if (BytesPerWord == 4) { if (BytesPerWord == 4) {
@ -1063,6 +1078,9 @@ class Compiler: public Assembler {
mov(rbp, FrameThread, rdi); mov(rbp, FrameThread, rdi);
} }
mov(rbp, FrameThread, rax);
mov(rbp, rax, threadFrameOffset); // set thread frame to current
callAddress(function); callAddress(function);
if (BytesPerWord == 4) { if (BytesPerWord == 4) {
@ -1071,9 +1089,6 @@ class Compiler: public Assembler {
} }
void compileCall(void* function, Register arg1, Register arg2) { void compileCall(void* function, Register arg1, Register arg2) {
mov(rbp, FrameThread, rax);
mov(rbp, rax, frameOffset); // set thread frame to current
if (BytesPerWord == 4) { if (BytesPerWord == 4) {
push(arg2); push(arg2);
push(arg1); push(arg1);
@ -1084,6 +1099,9 @@ class Compiler: public Assembler {
mov(rbp, FrameThread, rdi); mov(rbp, FrameThread, rdi);
} }
mov(rbp, FrameThread, rax);
mov(rbp, rax, threadFrameOffset); // set thread frame to current
callAddress(function); callAddress(function);
if (BytesPerWord == 4) { if (BytesPerWord == 4) {
@ -2086,14 +2104,11 @@ class Compiler: public Assembler {
} }
Code* compileNativeInvoker() { Code* compileNativeInvoker() {
unsigned frameOffset = reinterpret_cast<uintptr_t>(&(t->frame))
- reinterpret_cast<uintptr_t>(t);
push(rbp); push(rbp);
mov(rsp, rbp); mov(rsp, rbp);
mov(rbp, FrameThread, rax); mov(rbp, FrameThread, rax);
mov(rbp, rax, frameOffset); // set thread frame to current mov(rbp, rax, threadFrameOffset); // set thread frame to current
if (BytesPerWord == 4) { if (BytesPerWord == 4) {
push(rbp, FrameMethod); push(rbp, FrameMethod);
@ -2118,14 +2133,11 @@ class Compiler: public Assembler {
} }
Code* compileStub() { Code* compileStub() {
unsigned frameOffset = reinterpret_cast<uintptr_t>(&(t->frame))
- reinterpret_cast<uintptr_t>(t);
push(rbp); push(rbp);
mov(rsp, rbp); mov(rsp, rbp);
mov(rbp, FrameThread, rax); mov(rbp, FrameThread, rax);
mov(rbp, rax, frameOffset); // set thread frame to current mov(rbp, rax, threadFrameOffset); // set thread frame to current
if (BytesPerWord == 4) { if (BytesPerWord == 4) {
push(rbp, FrameMethod); push(rbp, FrameMethod);
@ -2181,6 +2193,7 @@ class Compiler: public Assembler {
if (poolRegisterClobbered) { if (poolRegisterClobbered) {
mov(rbp, FrameMethod, rdi); mov(rbp, FrameMethod, rdi);
mov(rdi, MethodCode, rdi); mov(rdi, MethodCode, rdi);
poolRegisterClobbered = false;
} }
pool.appendAddress(o); pool.appendAddress(o);
return pool.length() + BytesPerWord; return pool.length() + BytesPerWord;
@ -2199,6 +2212,7 @@ class Compiler: public Assembler {
} }
MyThread* t; MyThread* t;
unsigned threadFrameOffset;
bool poolRegisterClobbered; bool poolRegisterClobbered;
Buffer javaIPs; Buffer javaIPs;
Buffer machineIPs; Buffer machineIPs;
@ -2229,8 +2243,8 @@ compileMethod2(MyThread* t, object method)
fprintf(stderr, "compiled %s.%s from %p to %p\n", fprintf(stderr, "compiled %s.%s from %p to %p\n",
&byteArrayBody(t, className(t, methodClass(t, method)), 0), &byteArrayBody(t, className(t, methodClass(t, method)), 0),
&byteArrayBody(t, methodName(t, method), 0), &byteArrayBody(t, methodName(t, method), 0),
code->body, code->code(),
code->body + code->codeLength); code->code() + code->codeLength());
} }
set(t, methodCompiled(t, method), compiled); set(t, methodCompiled(t, method), compiled);

View File

@ -90,6 +90,16 @@
(uint16_t maxLocals) (uint16_t maxLocals)
(array uint8_t body)) (array uint8_t body))
(pod nativeExceptionHandler
(uint32_t start)
(uint32_t end)
(uint32_t ip)
(uint32_t catchType))
(pod nativeLineNumber
(uint32_t ip)
(uint32_t line))
(type reference (type reference
(object class) (object class)
(object name) (object name)