mirror of
https://github.com/corda/corda.git
synced 2025-01-09 14:33:30 +00:00
more work on new compiler
This commit is contained in:
parent
4860060bf8
commit
7bb69b1c56
89
src/assember.h
Normal file
89
src/assember.h
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
#ifndef ASSEMBLER_H
|
||||||
|
#define ASSEMBLER_H
|
||||||
|
|
||||||
|
namespace vm {
|
||||||
|
|
||||||
|
enum OperationType {
|
||||||
|
Call,
|
||||||
|
Return,
|
||||||
|
Move,
|
||||||
|
Store1,
|
||||||
|
Store2,
|
||||||
|
Store4,
|
||||||
|
Store8,
|
||||||
|
Load1,
|
||||||
|
Load2,
|
||||||
|
Load2z,
|
||||||
|
Load4,
|
||||||
|
Load8,
|
||||||
|
JumpIfLess,
|
||||||
|
JumpIfGreater,
|
||||||
|
JumpIfLessOrEqual,
|
||||||
|
JumpIfGreaterOrEqual,
|
||||||
|
JumpIfEqual,
|
||||||
|
JumpIfNotEqual,
|
||||||
|
Jump,
|
||||||
|
Add,
|
||||||
|
Subtract,
|
||||||
|
Multiply,
|
||||||
|
Divide,
|
||||||
|
Remainder,
|
||||||
|
ShiftLeft,
|
||||||
|
ShiftRight,
|
||||||
|
UnsignedShiftRight,
|
||||||
|
And,
|
||||||
|
Or,
|
||||||
|
Xor,
|
||||||
|
Negate
|
||||||
|
};
|
||||||
|
|
||||||
|
class Assembler {
|
||||||
|
public:
|
||||||
|
virtual unsigned registerCount() = 0;
|
||||||
|
|
||||||
|
virtual int base() = 0;
|
||||||
|
virtual int stack() = 0;
|
||||||
|
virtual int thread() = 0;
|
||||||
|
virtual int returnLow() = 0;
|
||||||
|
virtual int returnHigh() = 0;
|
||||||
|
|
||||||
|
virtual unsigned argumentRegisterCount() = 0;
|
||||||
|
virtual int argumentRegister(unsigned index) = 0;
|
||||||
|
|
||||||
|
virtual int stackSyncRegister(unsigned index) = 0;
|
||||||
|
|
||||||
|
virtual void getTargets(OperationType op, unsigned size,
|
||||||
|
int* aLow, int* aHigh,
|
||||||
|
int* bLow, int* bHigh) = 0;
|
||||||
|
|
||||||
|
virtual void appendC(OperationType op, unsigned size, Promise* value) = 0;
|
||||||
|
virtual void appendR(OperationType op, unsigned size, int low, int high) = 0;
|
||||||
|
virtual void appendM(OperationType op, unsigned size, int base, int offset,
|
||||||
|
int index, unsigned scale,
|
||||||
|
TraceHandler* traceHandler) = 0;
|
||||||
|
|
||||||
|
virtual void appendCR(OperationType op, unsigned size, Promise* aValue,
|
||||||
|
int bLow, int bHigh) = 0;
|
||||||
|
virtual void appendRR(OperationType op, unsigned size, int aLow, int aHigh,
|
||||||
|
int bLow, int bHigh) = 0;
|
||||||
|
virtual void appendMR(OperationType op, unsigned size,
|
||||||
|
int aBase, int aOffset, int aIndex, unsigned aScale,
|
||||||
|
TraceHandler* aTraceHandler,
|
||||||
|
int bLow, int bHigh) = 0;
|
||||||
|
|
||||||
|
virtual void appendCM(OperationType op, unsigned size, Promise* aValue,
|
||||||
|
int bBase, int bOffset, int bIndex, unsigned bScale,
|
||||||
|
TraceHandler* bTraceHandler) = 0;
|
||||||
|
virtual void appendRM(OperationType op, unsigned size, int aLow, int aHigh,
|
||||||
|
int bBase, int bOffset, int bIndex, unsigned bScale,
|
||||||
|
TraceHandler* bTraceHandler) = 0;
|
||||||
|
virtual void appendMM(OperationType op, unsigned size,
|
||||||
|
int aBase, int aOffset, int aIndex, unsigned aScale,
|
||||||
|
TraceHandler* aTraceHandler,
|
||||||
|
int bBase, int bOffset, int bIndex, unsigned bScale,
|
||||||
|
TraceHandler* bTraceHandler) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace vm
|
||||||
|
|
||||||
|
#endif//ASSEMBLER_H
|
@ -1,42 +1,10 @@
|
|||||||
#include "compiler.h"
|
#include "compiler.h"
|
||||||
|
#include "assembler.h"
|
||||||
|
|
||||||
using namespace vm;
|
using namespace vm;
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
enum OperationType {
|
|
||||||
Call,
|
|
||||||
Return,
|
|
||||||
Store1,
|
|
||||||
Store2,
|
|
||||||
Store4,
|
|
||||||
Store8,
|
|
||||||
Load1,
|
|
||||||
Load2,
|
|
||||||
Load2z,
|
|
||||||
Load4,
|
|
||||||
Load8,
|
|
||||||
JumpIfLess,
|
|
||||||
JumpIfGreater,
|
|
||||||
JumpIfLessOrEqual,
|
|
||||||
JumpIfGreaterOrEqual,
|
|
||||||
JumpIfEqual,
|
|
||||||
JumpIfNotEqual,
|
|
||||||
Jump,
|
|
||||||
Add,
|
|
||||||
Subtract,
|
|
||||||
Multiply,
|
|
||||||
Divide,
|
|
||||||
Remainder,
|
|
||||||
ShiftLeft,
|
|
||||||
ShiftRight,
|
|
||||||
UnsignedShiftRight,
|
|
||||||
And,
|
|
||||||
Or,
|
|
||||||
Xor,
|
|
||||||
Negate
|
|
||||||
};
|
|
||||||
|
|
||||||
class Context;
|
class Context;
|
||||||
class MyOperand;
|
class MyOperand;
|
||||||
class ConstantValue;
|
class ConstantValue;
|
||||||
@ -51,7 +19,7 @@ class Value {
|
|||||||
virtual bool equals(RegisterValue* o) { return false; }
|
virtual bool equals(RegisterValue* o) { return false; }
|
||||||
virtual bool equals(ConstantValue* o) { return false; }
|
virtual bool equals(ConstantValue* o) { return false; }
|
||||||
|
|
||||||
virtual void preserve(Context*) { ]
|
virtual void preserve(Context*) { }
|
||||||
virtual void acquire(Context*, MyOperand*) { }
|
virtual void acquire(Context*, MyOperand*) { }
|
||||||
virtual void release(Context*, MyOperand*) { }
|
virtual void release(Context*, MyOperand*) { }
|
||||||
|
|
||||||
@ -104,24 +72,24 @@ class Register {
|
|||||||
|
|
||||||
class Context {
|
class Context {
|
||||||
public:
|
public:
|
||||||
Context(NativeMachine* machine, Zone* zone):
|
Context(Assembler* assembler, Zone* zone):
|
||||||
machine(machine),
|
assembler(assembler),
|
||||||
zone(zone),
|
zone(zone),
|
||||||
logicalIp(-1),
|
logicalIp(-1),
|
||||||
state(new (c->zone->allocate(sizeof(State))) State(0)),
|
state(new (c->zone->allocate(sizeof(State))) State(0)),
|
||||||
event(0),
|
event(0),
|
||||||
logicalCode(0),
|
logicalCode(0),
|
||||||
registers(static_cast<Register*>
|
registers(static_cast<Register*>
|
||||||
(zone->allocate(sizeof(Register) * machine->registerCount())))
|
(zone->allocate(sizeof(Register) * assembler->registerCount())))
|
||||||
{
|
{
|
||||||
memset(registers, 0, sizeof(Register) * machine->registerCount());
|
memset(registers, 0, sizeof(Register) * assembler->registerCount());
|
||||||
|
|
||||||
registers[machine->base()].reserved = true;
|
registers[assembler->base()].reserved = true;
|
||||||
registers[machine->stack()].reserved = true;
|
registers[assembler->stack()].reserved = true;
|
||||||
registers[machine->thread()].reserved = true;
|
registers[assembler->thread()].reserved = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
NativeMachine* machine;
|
Assembler* assembler;
|
||||||
Zone* zone;
|
Zone* zone;
|
||||||
unsigned logicalIp;
|
unsigned logicalIp;
|
||||||
State* state;
|
State* state;
|
||||||
@ -139,7 +107,7 @@ class ConstantValue: public Value {
|
|||||||
virtual bool equals(ConstantValue* o) { return o->value == value; }
|
virtual bool equals(ConstantValue* o) { return o->value == value; }
|
||||||
|
|
||||||
virtual void apply(Context* c, OperationType op, unsigned size) {
|
virtual void apply(Context* c, OperationType op, unsigned size) {
|
||||||
c->machine->appendC(op, size, value);
|
c->assembler->appendC(op, size, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void apply(Context* c, OperationType op, unsigned size, Value* b) {
|
virtual void apply(Context* c, OperationType op, unsigned size, Value* b) {
|
||||||
@ -196,7 +164,7 @@ class RegisterValue: public Value {
|
|||||||
}
|
}
|
||||||
|
|
||||||
virtual void apply(Context* c, OperationType op, unsigned size) {
|
virtual void apply(Context* c, OperationType op, unsigned size) {
|
||||||
c->machine->appendR(op, size, low, high);
|
c->assembler->appendR(op, size, low, high);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void apply(Context* c, OperationType op, unsigned size, Value* b) {
|
virtual void apply(Context* c, OperationType op, unsigned size, Value* b) {
|
||||||
@ -206,13 +174,13 @@ class RegisterValue: public Value {
|
|||||||
virtual void accept(Context* c, OperationType op, unsigned size,
|
virtual void accept(Context* c, OperationType op, unsigned size,
|
||||||
ConstantValue* a)
|
ConstantValue* a)
|
||||||
{
|
{
|
||||||
c->machine->appendCR(op, size, a->value, low, high);
|
c->assembler->appendCR(op, size, a->value, low, high);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void accept(Context* c, OperationType op, unsigned size,
|
virtual void accept(Context* c, OperationType op, unsigned size,
|
||||||
RegisterValue* a)
|
RegisterValue* a)
|
||||||
{
|
{
|
||||||
c->machine->appendRR(op, size, a->low, a->high, low, high);
|
c->assembler->appendRR(op, size, a->low, a->high, low, high);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void accept(Context* c, OperationType op, unsigned size,
|
virtual void accept(Context* c, OperationType op, unsigned size,
|
||||||
@ -246,7 +214,7 @@ class MemoryValue: public Value {
|
|||||||
}
|
}
|
||||||
|
|
||||||
virtual void apply(Context* c, OperationType op, unsigned size) {
|
virtual void apply(Context* c, OperationType op, unsigned size) {
|
||||||
c->machine->appendM(op, size, base, offset, index, scale, traceHandler);
|
c->assembler->appendM(op, size, base, offset, index, scale, traceHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void apply(Context* c, OperationType op, unsigned size, Value* b) {
|
virtual void apply(Context* c, OperationType op, unsigned size, Value* b) {
|
||||||
@ -254,23 +222,23 @@ class MemoryValue: public Value {
|
|||||||
}
|
}
|
||||||
|
|
||||||
virtual void accept(Context* c, OperationType op, unsigned size,
|
virtual void accept(Context* c, OperationType op, unsigned size,
|
||||||
RegisterValue* a)
|
ConstantValue* a)
|
||||||
{
|
{
|
||||||
c->machine->appendRM(op, size, a->low, a->high,
|
c->assembler->appendCM(op, size, a->value,
|
||||||
base, offset, index, scale, traceHandler);
|
base, offset, index, scale, traceHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void accept(Context* c, OperationType op, unsigned size,
|
virtual void accept(Context* c, OperationType op, unsigned size,
|
||||||
ConstantValue* a)
|
RegisterValue* a)
|
||||||
{
|
{
|
||||||
c->machine->appendCM(op, size, a->value,
|
c->assembler->appendRM(op, size, a->low, a->high,
|
||||||
base, offset, index, scale, traceHandler);
|
base, offset, index, scale, traceHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void accept(Context* c, OperationType op, unsigned size,
|
virtual void accept(Context* c, OperationType op, unsigned size,
|
||||||
MemoryValue* a)
|
MemoryValue* a)
|
||||||
{
|
{
|
||||||
c->machine->appendMM
|
c->assembler->appendMM
|
||||||
(op, size, a->base, a->offset, a->index, a->scale, a->traceHandler,
|
(op, size, a->base, a->offset, a->index, a->scale, a->traceHandler,
|
||||||
base, offset, index, scale, traceHandler);
|
base, offset, index, scale, traceHandler);
|
||||||
}
|
}
|
||||||
@ -322,10 +290,10 @@ class ArgumentEvent: public Event {
|
|||||||
virtual Value* target(Context* c, MyOperand* v) {
|
virtual Value* target(Context* c, MyOperand* v) {
|
||||||
assert(c, v == a);
|
assert(c, v == a);
|
||||||
|
|
||||||
if (index < c->machine->argumentRegisterCount()) {
|
if (index < c->assembler->argumentRegisterCount()) {
|
||||||
return register_(c, c->machine->argumentRegister(index));
|
return register_(c, c->assembler->argumentRegister(index));
|
||||||
} else {
|
} else {
|
||||||
return memory(c, c->machine->base(),
|
return memory(c, c->assembler->base(),
|
||||||
(v->index + c->stackOffset) * BytesPerWord,
|
(v->index + c->stackOffset) * BytesPerWord,
|
||||||
NoRegister, 0, 0);
|
NoRegister, 0, 0);
|
||||||
}
|
}
|
||||||
@ -366,7 +334,7 @@ class ReturnEvent: public Event {
|
|||||||
virtual Value* target(Context* c, MyOperand* v) {
|
virtual Value* target(Context* c, MyOperand* v) {
|
||||||
assert(c, v == a);
|
assert(c, v == a);
|
||||||
|
|
||||||
return register_(c, c->machine->returnLow(), c->machine->returnHigh());
|
return register_(c, c->assembler->returnLow(), c->assembler->returnHigh());
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void replace(Context* c, MyOperand* old, MyOperand* new_) {
|
virtual void replace(Context* c, MyOperand* old, MyOperand* new_) {
|
||||||
@ -444,10 +412,10 @@ class SyncForJumpEvent: public Event {
|
|||||||
assert(c, v == src);
|
assert(c, v == src);
|
||||||
|
|
||||||
if (BytesPerWord == 4 and v->size == 8) {
|
if (BytesPerWord == 4 and v->size == 8) {
|
||||||
return register_(c, c->machine->stackSyncRegister(c, v->index),
|
return register_(c, c->assembler->stackSyncRegister(c, v->index),
|
||||||
c->machine->stackSyncRegister(c, v->index + 4));
|
c->assembler->stackSyncRegister(c, v->index + 4));
|
||||||
} else {
|
} else {
|
||||||
return register_(c, c->machine->stackSyncRegister(c, v->index));
|
return register_(c, c->assembler->stackSyncRegister(c, v->index));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -508,13 +476,13 @@ class CallEvent: public Event {
|
|||||||
|
|
||||||
if (result->event) {
|
if (result->event) {
|
||||||
result->value = register_
|
result->value = register_
|
||||||
(c, c->machine->returnLow(), c->machine->returnHigh());
|
(c, c->assembler->returnLow(), c->assembler->returnHigh());
|
||||||
result->value->acquire(c, result);
|
result->value->acquire(c, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
register_(c, StackRegister)->accept
|
register_(c, StackRegister)->accept
|
||||||
(c, LoadAddress, BytesPerWord,
|
(c, LoadAddress, BytesPerWord,
|
||||||
memory(c, c->machine->base(), stackOffset * BytesPerWord,
|
memory(c, c->assembler->base(), stackOffset * BytesPerWord,
|
||||||
NoRegister, 0, 0));
|
NoRegister, 0, 0));
|
||||||
|
|
||||||
address->value->apply(c, Call);
|
address->value->apply(c, Call);
|
||||||
@ -539,7 +507,7 @@ appendCall(Context* c, MyOperand* address, MyOperand* result,
|
|||||||
int
|
int
|
||||||
freeRegister(Context* c)
|
freeRegister(Context* c)
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < c->machine->registerCount(); ++i) {
|
for (unsigned i = 0; i < c->assembler->registerCount(); ++i) {
|
||||||
if ((not c->registers[i].reserved)
|
if ((not c->registers[i].reserved)
|
||||||
and c->registers[i].operand == 0)
|
and c->registers[i].operand == 0)
|
||||||
{
|
{
|
||||||
@ -547,7 +515,7 @@ freeRegister(Context* c)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (unsigned i = 0; i < c->machine->registerCount(); ++i) {
|
for (unsigned i = 0; i < c->assembler->registerCount(); ++i) {
|
||||||
if (not c->registers[i].reserved) {
|
if (not c->registers[i].reserved) {
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
@ -692,7 +660,7 @@ class CombineEvent: public Event {
|
|||||||
|
|
||||||
virtual Value* target(Context* c, MyOperand* v) {
|
virtual Value* target(Context* c, MyOperand* v) {
|
||||||
int aLow, aHigh, bLow, bHigh;
|
int aLow, aHigh, bLow, bHigh;
|
||||||
c->machine->getTargets(c, v->size, &aLow, &aHigh, &bLow, &bHigh);
|
c->assembler->getTargets(op, v->size, &aLow, &aHigh, &bLow, &bHigh);
|
||||||
|
|
||||||
if (v == a) {
|
if (v == a) {
|
||||||
if (aLow == NoRegister) {
|
if (aLow == NoRegister) {
|
||||||
@ -706,7 +674,7 @@ class CombineEvent: public Event {
|
|||||||
if (bLow == NoRegister) {
|
if (bLow == NoRegister) {
|
||||||
return result->event->target(c, result);
|
return result->event->target(c, result);
|
||||||
} else {
|
} else {
|
||||||
return register_(c, aLow, aHigh);
|
return register_(c, bLow, bHigh);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -805,7 +773,7 @@ void
|
|||||||
RegisterValue::accept(Context* c, OperationType op, unsigned size,
|
RegisterValue::accept(Context* c, OperationType op, unsigned size,
|
||||||
MemoryValue* a)
|
MemoryValue* a)
|
||||||
{
|
{
|
||||||
c->machine->appendMR(op, size, a->base, a->offset, a->index, a->scale,
|
c->assembler->appendMR(op, size, a->base, a->offset, a->index, a->scale,
|
||||||
a->traceHandler, low, high);
|
a->traceHandler, low, high);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -815,10 +783,10 @@ preserve(Context* c, int reg)
|
|||||||
MyOperand* a = c->registers[reg].operand;
|
MyOperand* a = c->registers[reg].operand;
|
||||||
if (a) {
|
if (a) {
|
||||||
MemoryValue* dst = memory
|
MemoryValue* dst = memory
|
||||||
(c, machine->base(), (a->index + c->stackOffset) * BytesPerWord,
|
(c, assembler->base(), (a->index + c->stackOffset) * BytesPerWord,
|
||||||
-1, 0, 0);
|
-1, 0, 0);
|
||||||
|
|
||||||
c->machine->appendRM
|
c->assembler->appendRM
|
||||||
(Move, a->size,
|
(Move, a->size,
|
||||||
static_cast<RegisterValue*>(a->value)->low,
|
static_cast<RegisterValue*>(a->value)->low,
|
||||||
static_cast<RegisterValue*>(a->value)->high,
|
static_cast<RegisterValue*>(a->value)->high,
|
||||||
|
Loading…
Reference in New Issue
Block a user