From 12e10b57f5af3c6934600dcee704009e05c5014f Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Fri, 14 Dec 2007 11:27:56 -0700 Subject: [PATCH] JIT bugfixes and new instructions; exception propagation and stack traces are functional but not yet entirely correct --- src/compile.cpp | 32 +++++--- src/compiler.cpp | 204 ++++++++++++++++++++++++++++++++++++++++++----- src/machine.cpp | 4 +- 3 files changed, 208 insertions(+), 32 deletions(-) diff --git a/src/compile.cpp b/src/compile.cpp index f110ec5d02..e0647daca1 100644 --- a/src/compile.cpp +++ b/src/compile.cpp @@ -64,7 +64,7 @@ class MyThread: public Thread { object resolveTarget(MyThread* t, void* stack, object method) { - if (methodVirtual(t, method)) { + if (method and methodVirtual(t, method)) { unsigned parameterFootprint = methodParameterFootprint(t, method); object class_ = objectClass (t, reinterpret_cast(stack)[parameterFootprint]); @@ -111,7 +111,7 @@ class MyStackWalker: public Processor::StackWalker { base(t->base), stack(t->stack), trace(t->trace), - node(findTraceNode(t, *static_cast(stack))), + node(stack ? findTraceNode(t, *static_cast(stack)) : 0), nativeMethod(resolveNativeMethod(t, stack, node)), protector(this) { } @@ -137,6 +137,10 @@ class MyStackWalker: public Processor::StackWalker { } virtual void walk(Processor::StackVisitor* v) { + if (stack == 0) { + return; + } + if (not v->visit(this)) { return; } @@ -157,7 +161,7 @@ class MyStackWalker: public Processor::StackWalker { base = *static_cast(base); node = findTraceNode(t, *static_cast(stack)); if (node == 0) { - if (trace) { + if (trace and trace->stack) { base = trace->base; stack = static_cast(trace->stack); trace = trace->next; @@ -1494,7 +1498,7 @@ compile(MyThread* t, Frame* initialFrame, unsigned ip) Operand* result = c->directCall (c->constant(reinterpret_cast(isAssignableFrom)), - 2, classOperand, tmp); + 3, c->thread(), classOperand, tmp); c->release(tmp); @@ -2078,7 +2082,7 @@ compile(MyThread* t, Frame* initialFrame, unsigned ip) Operand* next = c->label(); Operand* zero = c->label(); - Operand* instance = frame->topObject(); + Operand* instance = frame->popObject(); Operand* tmp = c->temporary(); Operand* result = c->temporary(); @@ -2098,12 +2102,15 @@ compile(MyThread* t, Frame* initialFrame, unsigned ip) c->mov(c->constant(1), result); c->jmp(next); + c->mark(call); + c->mov (c->directCall (c->constant(reinterpret_cast(isAssignableFrom)), - 2, classOperand, tmp), result); + 3, c->thread(), classOperand, tmp), result); c->release(tmp); + c->release(instance); c->jmp(next); @@ -2211,9 +2218,11 @@ compile(MyThread* t, Frame* initialFrame, unsigned ip) } break; case ireturn: - case freturn: - c->return_(frame->popInt()); - return; + case freturn: { + Operand* a = frame->popInt(); + c->return_(a); + c->release(a); + } return; case ishl: { Operand* a = frame->popInt(); @@ -2966,7 +2975,7 @@ finish(MyThread* t, Compiler* c, object method, Vector* objectPool, "java/lang/String") == 0 and strcmp(reinterpret_cast (&byteArrayBody(t, methodName(t, method), 0)), - "getBytes") == 0) + "charAt") == 0) { asm("int3"); } @@ -3831,7 +3840,8 @@ findTraceNode(MyThread* t, void* address) return n; } } - abort(t); + + return 0; } object diff --git a/src/compiler.cpp b/src/compiler.cpp index 62eaa0f9f8..3a6f6aa8ab 100644 --- a/src/compiler.cpp +++ b/src/compiler.cpp @@ -33,6 +33,8 @@ enum SelectionType { S8Selection }; +const bool Verbose = true; + const unsigned RegisterCount = BytesPerWord * 2; class Context; @@ -146,22 +148,24 @@ class MyOperand: public Operand { virtual void setLabelValue(Context* c, CodePromise*) { abort(c); } - virtual void apply(Context* c, Operation) { abort(c); } + virtual void apply(Context*, Operation) = 0; - virtual void apply(Context* c, Operation, SelectionType) { abort(c); } + virtual void apply(Context*, Operation, MyOperand*) = 0; - virtual void apply(Context* c, Operation, MyOperand*) { abort(c); } + virtual void apply(Context*, Operation, SelectionType) = 0; - virtual void accept(Context* c, Operation, RegisterOperand*) { abort(c); } + virtual void apply(Context*, Operation, MyOperand*, SelectionType) = 0; - virtual void accept(Context* c, Operation, ImmediateOperand*) { abort(c); } + virtual void accept(Context* c, Operation, RegisterOperand*) = 0; - virtual void accept(Context* c, Operation, AbsoluteOperand*) { abort(c); } + virtual void accept(Context* c, Operation, ImmediateOperand*) = 0; - virtual void accept(Context* c, Operation, MemoryOperand*) { abort(c); } + virtual void accept(Context* c, Operation, AbsoluteOperand*) = 0; + + virtual void accept(Context* c, Operation, MemoryOperand*) = 0; virtual void accept(Context* c, Operation, MemoryOperand*, SelectionType) - { abort(c); } + = 0; }; class RegisterOperand: public MyOperand { @@ -188,12 +192,15 @@ class RegisterOperand: public MyOperand { virtual void apply(Context*, Operation); - virtual void apply(Context* c, Operation, SelectionType) { abort(c); } - virtual void apply(Context* c, Operation operation, MyOperand* operand) { operand->accept(c, operation, this); } + virtual void apply(Context* c, Operation, SelectionType) { abort(c); } + + virtual void apply(Context* c, Operation, MyOperand*, SelectionType) + { abort(c); } + virtual void accept(Context*, Operation, RegisterOperand*); virtual void accept(Context*, Operation, ImmediateOperand*); virtual void accept(Context*, Operation, AbsoluteOperand*); @@ -212,10 +219,26 @@ class ImmediateOperand: public MyOperand { virtual void apply(Context* c, Operation operation); + virtual void apply(Context* c, Operation, SelectionType) { abort(c); } + virtual void apply(Context* c, Operation operation, MyOperand* operand) { operand->accept(c, operation, this); } + virtual void apply(Context* c, Operation, MyOperand*, SelectionType) + { abort(c); } + + virtual void accept(Context* c, Operation, RegisterOperand*) { abort(c); } + + virtual void accept(Context* c, Operation, ImmediateOperand*) { abort(c); } + + virtual void accept(Context* c, Operation, AbsoluteOperand*) { abort(c); } + + virtual void accept(Context* c, Operation, MemoryOperand*) { abort(c); } + + virtual void accept(Context* c, Operation, MemoryOperand*, SelectionType) + { abort(c); } + intptr_t value; }; @@ -229,6 +252,24 @@ class AddressOperand: public MyOperand { virtual void apply(Context*, Operation); + virtual void apply(Context* c, Operation, MyOperand*) { abort(c); } + + virtual void apply(Context* c, Operation, SelectionType) { abort(c); } + + virtual void apply(Context* c, Operation, MyOperand*, SelectionType) + { abort(c); } + + virtual void accept(Context* c, Operation, RegisterOperand*) { abort(c); } + + virtual void accept(Context* c, Operation, ImmediateOperand*) { abort(c); } + + virtual void accept(Context* c, Operation, AbsoluteOperand*) { abort(c); } + + virtual void accept(Context* c, Operation, MemoryOperand*) { abort(c); } + + virtual void accept(Context* c, Operation, MemoryOperand*, SelectionType) + { abort(c); } + MyPromise* promise; }; @@ -244,6 +285,22 @@ class AbsoluteOperand: public MyOperand { operand->accept(c, operation, this); } + virtual void apply(Context* c, Operation, SelectionType) { abort(c); } + + virtual void apply(Context* c, Operation, MyOperand*, SelectionType) + { abort(c); } + + virtual void accept(Context* c, Operation, RegisterOperand*) { abort(c); } + + virtual void accept(Context* c, Operation, ImmediateOperand*) { abort(c); } + + virtual void accept(Context* c, Operation, AbsoluteOperand*) { abort(c); } + + virtual void accept(Context* c, Operation, MemoryOperand*) { abort(c); } + + virtual void accept(Context* c, Operation, MemoryOperand*, SelectionType) + { abort(c); } + MyPromise* promise; }; @@ -261,16 +318,26 @@ class MemoryOperand: public MyOperand { virtual void apply(Context*, Operation); - virtual void apply(Context* c, Operation, SelectionType); - virtual void apply(Context* c, Operation operation, MyOperand* operand) { operand->accept(c, operation, this); } + virtual void apply(Context* c, Operation, SelectionType); + + virtual void apply(Context* c, Operation operation, MyOperand* operand, + SelectionType selection) + { + operand->accept(c, operation, this, selection); + } + virtual void accept(Context*, Operation, RegisterOperand*); virtual void accept(Context*, Operation, ImmediateOperand*); virtual void accept(Context*, Operation, AbsoluteOperand*); + virtual void accept(Context* c, Operation, MemoryOperand*) { abort(c); } + + virtual void accept(Context* c, Operation, MemoryOperand*, SelectionType); + MyOperand* base; int displacement; MyOperand* index; @@ -295,6 +362,26 @@ class SelectionOperand: public MyOperand { base->apply(c, operation, selectionType); } + virtual void apply(Context* c, Operation operation, MyOperand* operand) { + base->apply(c, operation, operand, selectionType); + } + + virtual void apply(Context* c, Operation, SelectionType) { abort(c); } + + virtual void apply(Context* c, Operation, MyOperand*, SelectionType) + { abort(c); } + + virtual void accept(Context* c, Operation, RegisterOperand*) { abort(c); } + + virtual void accept(Context* c, Operation, ImmediateOperand*) { abort(c); } + + virtual void accept(Context* c, Operation, AbsoluteOperand*) { abort(c); } + + virtual void accept(Context* c, Operation, MemoryOperand*) { abort(c); } + + virtual void accept(Context* c, Operation, MemoryOperand*, SelectionType) + { abort(c); } + SelectionType selectionType; MyOperand* base; }; @@ -321,6 +408,17 @@ class WrapperOperand: public MyOperand { base->apply(c, operation, operand); } + virtual void apply(Context* c, Operation operation, SelectionType selection) + { + base->apply(c, operation, selection); + } + + virtual void apply(Context* c, Operation operation, MyOperand* operand, + SelectionType selection) + { + base->apply(c, operation, operand, selection); + } + virtual void accept(Context* c, Operation operation, RegisterOperand* operand) { @@ -344,6 +442,12 @@ class WrapperOperand: public MyOperand { base->accept(c, operation, operand); } + virtual void accept(Context* c, Operation operation, MemoryOperand* operand, + SelectionType selection) + { + base->accept(c, operation, operand, selection); + } + MyOperand* base; }; @@ -606,7 +710,7 @@ RegisterOperand* temporary(Context* c) { // we don't yet support using r9-r15 - for (unsigned i = 0; i < 8/*RegisterCount*/; ++i) { + for (int i = 8/*RegisterCount*/ - 1; i >= 0; --i) { if (not c->registers[i]->reserved) { c->registers[i]->acquire(c); return c->registers[i]; @@ -616,6 +720,14 @@ temporary(Context* c) abort(c); } +RegisterOperand* +acquire(Context* c, Register v) +{ + RegisterOperand* r = register_(c, v); + r->acquire(c); + return r; +} + void apply(Context* c, MyOperand::Operation op) { @@ -650,6 +762,9 @@ class UnaryOpEvent: public Event { { } virtual void run(Context* c) { + if (Verbose and c->codeLength >= 0) { + fprintf(stderr, "unary %d\n", operation); + } operand->apply(c, operation); } @@ -668,6 +783,9 @@ class BinaryOpEvent: public Event { { } virtual void run(Context* c) { + if (Verbose and c->codeLength >= 0) { + fprintf(stderr, "binary %d\n", operation); + } a->apply(c, operation, b); } @@ -684,6 +802,9 @@ class AcquireEvent: public Event { { } virtual void run(Context* c) { + if (Verbose and c->codeLength >= 0) { + fprintf(stderr, "acquire\n"); + } operand->base = temporary(c); } @@ -698,6 +819,9 @@ class ReleaseEvent: public Event { { } virtual void run(Context* c) { + if (Verbose and c->codeLength >= 0) { + fprintf(stderr, "release\n"); + } operand->release(c); } @@ -1302,9 +1426,19 @@ MemoryOperand::accept(Context* c, Operation operation, encode(c, 0x01, operand->value, this, true); } break; -// case div: { -// // todo -// } break; + case div: { + RegisterOperand* tmp = acquire(c, rax); + tmp->accept(c, mov, this); + + rex(c); + c->code.append(0x99); + rex(c); + c->code.append(0xf7); + c->code.append(0xf8 | operand->value); + + accept(c, mov, tmp); + tmp->release(c); + } break; case mov: { encode(c, 0x89, operand->value, this, true); @@ -1365,6 +1499,24 @@ MemoryOperand::accept(Context* c, Operation operation, } } +void +MemoryOperand::accept(Context* c, Operation operation, + MemoryOperand* operand, SelectionType selection) +{ + switch (operation) { + case mov: { + RegisterOperand* tmp = temporary(c); + + tmp->accept(c, mov, operand, selection); + accept(c, mov, tmp); + + tmp->release(c); + } break; + + default: abort(c); + } +} + int compareSegmentPointers(const void* a, const void* b) { @@ -1389,6 +1541,10 @@ writeCode(Context* c) for (unsigned i = 0; i < tableSize; ++i) { Segment* s = c->segmentTable[i]; + if (Verbose and c->codeLength >= 0) { + fprintf(stderr, "\nip %d\n", s->logicalIp); + } + Event* events[s->event->count]; unsigned ei = s->event->count; for (Event* e = s->event; e; e = e->next) { @@ -1396,6 +1552,10 @@ writeCode(Context* c) } for (unsigned ei = 0; ei < s->event->count; ++ei) { + if (Verbose and c->codeLength >= 0) { + fprintf(stderr, "address %p\n", c->code.data + c->code.length()); + } + events[ei]->offset = c->code.length(); events[ei]->run(c); @@ -1538,8 +1698,14 @@ class MyCompiler: public Compiler { } virtual void mark(Operand* label) { - static_cast(label)->setLabelValue - (&c, static_cast(codeOffset())); + CodePromise* p = new (c.zone.allocate(sizeof(CodePromise))) + CodePromise(true); + + Segment* s = currentSegment(&c); + s->event->task = new (c.zone.allocate(sizeof(CodePromiseTask))) + CodePromiseTask(p, s->event->task); + + static_cast(label)->setLabelValue(&c, p); } virtual Operand* indirectCall @@ -1608,7 +1774,7 @@ class MyCompiler: public Compiler { } virtual void cmp(Operand* subtrahend, Operand* minuend) { - appendOperation(&c, MyOperand::mov, subtrahend, minuend); + appendOperation(&c, MyOperand::cmp, subtrahend, minuend); } virtual void jl(Operand* v) { diff --git a/src/machine.cpp b/src/machine.cpp index a6cff9c1b1..de06be8065 100644 --- a/src/machine.cpp +++ b/src/machine.cpp @@ -2878,7 +2878,7 @@ makeTrace(Thread* t, Processor::StackWalker* walker) walker->walk(&v); - return v.trace; + return v.trace ? v.trace : makeArray(t, 0, true); } object @@ -2899,7 +2899,7 @@ makeTrace(Thread* t) t->m->processor->walkStack(t, &v); - return v.trace; + return v.trace ? v.trace : makeArray(t, 0, true); } void