corda/src/compiler.cpp

1627 lines
34 KiB
C++
Raw Normal View History

2007-12-08 23:22:13 +00:00
#include "compiler.h"
2007-12-09 20:03:21 +00:00
#include "vector.h"
2007-12-11 00:48:09 +00:00
#include "zone.h"
2007-12-08 23:22:13 +00:00
using namespace vm;
namespace {
2007-12-09 22:45:43 +00:00
enum Register {
rax = 0,
rcx = 1,
rdx = 2,
rbx = 3,
rsp = 4,
rbp = 5,
rsi = 6,
rdi = 7,
r8 = 8,
r9 = 9,
r10 = 10,
r11 = 11,
r12 = 12,
r13 = 13,
r14 = 14,
r15 = 15,
};
2007-12-11 21:26:59 +00:00
const unsigned RegisterCount = BytesPerWord * 2;
class Context;
class ImmediateOperand;
2007-12-11 23:52:28 +00:00
class AbsoluteOperand;
2007-12-11 21:26:59 +00:00
class RegisterOperand;
class MemoryOperand;
class StackOperand;
void NO_RETURN abort(Context*);
#ifndef NDEBUG
void assert(Context*, bool);
#endif // not NDEBUG
2007-12-09 20:03:21 +00:00
2007-12-11 00:48:09 +00:00
inline bool
isInt8(intptr_t v)
2007-12-09 20:03:21 +00:00
{
2007-12-11 00:48:09 +00:00
return v == static_cast<int8_t>(v);
2007-12-09 20:03:21 +00:00
}
2007-12-11 00:48:09 +00:00
inline bool
isInt32(intptr_t v)
2007-12-09 20:03:21 +00:00
{
2007-12-11 00:48:09 +00:00
return v == static_cast<int32_t>(v);
2007-12-09 20:03:21 +00:00
}
2007-12-11 21:26:59 +00:00
class IpTask {
public:
2007-12-11 23:52:28 +00:00
enum Priority {
LowPriority,
HighPriority
};
2007-12-11 21:26:59 +00:00
IpTask(IpTask* next): next(next) { }
virtual ~IpTask() { }
virtual void run(Context* c, unsigned ip, unsigned start, unsigned end,
unsigned offset) = 0;
2007-12-11 23:52:28 +00:00
virtual Priority priority() {
return LowPriority;
}
2007-12-11 21:26:59 +00:00
IpTask* next;
};
2007-12-11 00:48:09 +00:00
class IpMapping {
public:
2007-12-11 21:26:59 +00:00
IpMapping(int ip, int start):
ip(ip), start(start), end(-1), task(0)
{ }
2007-12-11 00:48:09 +00:00
const int ip;
const int start;
int end;
2007-12-11 21:26:59 +00:00
IpTask* task;
2007-12-11 00:48:09 +00:00
};
int
compareIpMappingPointers(const void* a, const void* b)
2007-12-09 20:03:21 +00:00
{
2007-12-11 00:48:09 +00:00
return (*static_cast<IpMapping* const*>(a))->ip
- (*static_cast<IpMapping* const*>(b))->ip;
2007-12-09 20:03:21 +00:00
}
2007-12-08 23:22:13 +00:00
class MyPromise: public Promise {
public:
2007-12-11 21:26:59 +00:00
MyPromise(intptr_t key): key(key) { }
2007-12-08 23:22:13 +00:00
virtual intptr_t value(Compiler*);
2007-12-11 23:52:28 +00:00
virtual intptr_t value(Context*) = 0;
virtual bool resolved(Context*) = 0;
2007-12-11 23:52:28 +00:00
2007-12-11 21:26:59 +00:00
intptr_t key;
2007-12-09 20:03:21 +00:00
};
class ResolvedPromise: public MyPromise {
public:
ResolvedPromise(intptr_t key): MyPromise(key) { }
virtual intptr_t value(Context*) {
return key;
}
virtual bool resolved(Context*) {
return true;
}
};
2007-12-09 20:03:21 +00:00
class PoolPromise: public MyPromise {
public:
2007-12-11 21:26:59 +00:00
PoolPromise(intptr_t key): MyPromise(key) { }
2007-12-09 20:03:21 +00:00
virtual intptr_t value(Context*);
virtual bool resolved(Context*);
2007-12-09 20:03:21 +00:00
};
class CodePromise: public MyPromise {
public:
CodePromise(intptr_t key, bool absolute):
MyPromise(key), absolute(absolute)
{ }
2007-12-09 20:03:21 +00:00
virtual intptr_t value(Context*);
virtual bool resolved(Context*);
bool absolute;
2007-12-09 20:03:21 +00:00
};
2007-12-11 21:26:59 +00:00
class CodePromiseTask: public IpTask {
2007-12-09 20:03:21 +00:00
public:
2007-12-11 21:26:59 +00:00
CodePromiseTask(CodePromise* p, IpTask* next): IpTask(next), p(p) { }
2007-12-09 20:03:21 +00:00
virtual void run(Context*, unsigned, unsigned start, unsigned,
2007-12-11 21:26:59 +00:00
unsigned offset)
{
p->key = offset + (p->key - start);
}
2007-12-09 20:03:21 +00:00
2007-12-11 23:52:28 +00:00
virtual Priority priority() {
return HighPriority;
}
2007-12-11 21:26:59 +00:00
CodePromise* p;
};
2007-12-11 00:48:09 +00:00
2007-12-11 21:26:59 +00:00
class IpPromise: public MyPromise {
public:
IpPromise(intptr_t key, bool absolute):
MyPromise(key), absolute(absolute)
{ }
virtual intptr_t value(Context*);
virtual bool resolved(Context*);
2007-12-11 00:48:09 +00:00
bool absolute;
2007-12-11 21:26:59 +00:00
};
2007-12-11 00:48:09 +00:00
2007-12-09 20:03:21 +00:00
class MyOperand: public Operand {
public:
2007-12-11 00:48:09 +00:00
enum Operation {
push,
pop,
call,
alignedCall,
ret,
mov,
cmp,
jl,
jg,
jle,
jge,
je,
jne,
jmp,
add,
sub,
mul,
div,
rem,
shl,
shr,
ushr,
and_,
or_,
xor_,
neg
2007-12-09 20:03:21 +00:00
};
virtual ~MyOperand() { }
virtual unsigned footprint() {
return BytesPerWord;
}
2007-12-11 00:48:09 +00:00
virtual Register asRegister(Context* c) { abort(c); }
2007-12-09 22:45:43 +00:00
2007-12-11 00:48:09 +00:00
virtual void release(Context*) { /* ignore */ }
2007-12-09 20:03:21 +00:00
virtual void setImmediate(Context* c, intptr_t) { abort(c); }
2007-12-09 20:03:21 +00:00
2007-12-11 00:48:09 +00:00
virtual void apply(Context* c, Operation) { abort(c); }
2007-12-09 20:03:21 +00:00
2007-12-11 00:48:09 +00:00
virtual void apply(Context* c, Operation, MyOperand*) { abort(c); }
2007-12-09 20:03:21 +00:00
2007-12-11 00:48:09 +00:00
virtual void accept(Context* c, Operation, RegisterOperand*) { abort(c); }
2007-12-09 20:03:21 +00:00
2007-12-11 00:48:09 +00:00
virtual void accept(Context* c, Operation, ImmediateOperand*) { abort(c); }
2007-12-09 20:03:21 +00:00
2007-12-11 23:52:28 +00:00
virtual void accept(Context* c, Operation, AbsoluteOperand*) { abort(c); }
2007-12-11 00:48:09 +00:00
virtual void accept(Context* c, Operation, MemoryOperand*) { abort(c); }
2007-12-09 20:03:21 +00:00
};
class RegisterOperand: public MyOperand {
public:
RegisterOperand(Register value):
value(value), reserved(false)
2007-12-09 20:03:21 +00:00
{ }
2007-12-11 21:26:59 +00:00
virtual Register asRegister(Context*) {
return value;
}
2007-12-11 00:48:09 +00:00
virtual void release(Context* c UNUSED) {
assert(c, reserved);
reserved = false;
2007-12-09 20:03:21 +00:00
}
2007-12-11 21:26:59 +00:00
virtual void apply(Context*, Operation);
2007-12-09 20:03:21 +00:00
2007-12-11 00:48:09 +00:00
virtual void apply(Context* c, Operation operation, MyOperand* operand) {
operand->accept(c, operation, this);
2007-12-09 20:03:21 +00:00
}
2007-12-11 21:26:59 +00:00
virtual void accept(Context*, Operation, RegisterOperand*);
virtual void accept(Context*, Operation, ImmediateOperand*);
2007-12-11 23:52:28 +00:00
virtual void accept(Context*, Operation, AbsoluteOperand*);
2007-12-11 21:26:59 +00:00
virtual void accept(Context*, Operation, MemoryOperand*);
2007-12-09 20:03:21 +00:00
Register value;
2007-12-11 00:48:09 +00:00
bool reserved;
2007-12-08 23:22:13 +00:00
};
2007-12-09 22:45:43 +00:00
class ImmediateOperand: public MyOperand {
public:
ImmediateOperand(MyPromise* value):
2007-12-09 22:45:43 +00:00
value(value)
{ }
2007-12-09 20:03:21 +00:00
virtual void setImmediate(Context*, intptr_t v) {
value->key = v;
}
2007-12-11 21:26:59 +00:00
virtual void apply(Context* c, Operation operation);
2007-12-09 22:45:43 +00:00
2007-12-11 00:48:09 +00:00
virtual void apply(Context* c, Operation operation, MyOperand* operand) {
operand->accept(c, operation, this);
2007-12-09 22:45:43 +00:00
}
MyPromise* value;
2007-12-09 22:45:43 +00:00
};
class AbsoluteOperand: public MyOperand {
public:
2007-12-11 00:48:09 +00:00
AbsoluteOperand(MyPromise* value):
2007-12-09 22:45:43 +00:00
value(value)
{ }
2007-12-11 21:26:59 +00:00
virtual void apply(Context* c, Operation operation);
2007-12-11 23:52:28 +00:00
virtual void apply(Context* c, Operation operation, MyOperand* operand) {
operand->accept(c, operation, this);
}
2007-12-11 00:48:09 +00:00
MyPromise* value;
2007-12-09 22:45:43 +00:00
};
2007-12-09 20:03:21 +00:00
class MemoryOperand: public MyOperand {
public:
MemoryOperand(MyOperand* base, int displacement, MyOperand* index,
unsigned scale):
base(base),
displacement(displacement),
index(index),
scale(scale)
{
assert(static_cast<System*>(0), index == 0); // todo
assert(static_cast<System*>(0), scale == 1); // todo
}
2007-12-09 20:03:21 +00:00
virtual Register asRegister(Context*);
2007-12-11 21:26:59 +00:00
virtual void apply(Context* c, Operation operation);
2007-12-11 00:48:09 +00:00
virtual void apply(Context* c, Operation operation, MyOperand* operand) {
operand->accept(c, operation, this);
2007-12-09 20:03:21 +00:00
}
2007-12-11 21:26:59 +00:00
virtual void accept(Context*, Operation, RegisterOperand*);
virtual void accept(Context*, Operation, ImmediateOperand*);
virtual void accept(Context*, Operation, AbsoluteOperand*);
2007-12-11 21:26:59 +00:00
2007-12-09 20:03:21 +00:00
MyOperand* base;
int displacement;
MyOperand* index;
unsigned scale;
};
class SelectionOperand: public MyOperand {
public:
2007-12-09 22:45:43 +00:00
enum SelectionType {
S1Selection,
S2Selection,
Z2Selection,
S4Selection,
S8Selection
};
SelectionOperand(SelectionType type, MyOperand* base):
selectionType(type), base(base)
2007-12-09 20:03:21 +00:00
{ }
virtual unsigned footprint() {
2007-12-09 22:45:43 +00:00
if (selectionType == S8Selection) {
2007-12-09 20:03:21 +00:00
return 8;
} else {
return 4;
}
}
2007-12-11 00:48:09 +00:00
SelectionType selectionType;
MyOperand* base;
};
class StackOperand: public MyOperand {
public:
StackOperand(MyOperand* base, int index, StackOperand* next):
base(base), index(index), next(next)
{ }
2007-12-09 20:03:21 +00:00
virtual unsigned footprint() {
return base->footprint();
2007-12-11 23:52:28 +00:00
}
virtual Register asRegister(Context* c) {
return base->asRegister(c);
}
virtual void apply(Context* c, Operation operation) {
base->apply(c, operation);
}
2007-12-13 00:18:31 +00:00
virtual void apply(Context* c, Operation operation, MyOperand* operand) {
base->apply(c, operation, operand);
}
2007-12-11 21:26:59 +00:00
virtual void accept(Context* c, Operation operation,
RegisterOperand* operand)
{
base->accept(c, operation, operand);
}
2007-12-09 20:03:21 +00:00
MyOperand* base;
2007-12-11 00:48:09 +00:00
int index;
StackOperand* next;
2007-12-09 20:03:21 +00:00
};
2007-12-11 00:48:09 +00:00
class Context {
public:
Context(System* s, void* indirectCaller):
s(s),
code(s, 1024),
constantPool(s, BytesPerWord * 32),
ipMappings(s, 1024),
zone(s, 8 * 1024),
indirectCaller(reinterpret_cast<intptr_t>(indirectCaller)),
stack(0),
ipTable(0),
reserved(0),
output(0)
2007-12-11 00:48:09 +00:00
{
2007-12-11 21:26:59 +00:00
ipMappings.appendAddress
(new (zone.allocate(sizeof(IpMapping))) IpMapping(-1, 0));
for (unsigned i = 0; i < RegisterCount; ++i) {
2007-12-11 00:48:09 +00:00
registers[i] = new (zone.allocate(sizeof(RegisterOperand)))
RegisterOperand(static_cast<Register>(i));
}
registers[rsp]->reserved = true;
registers[rbp]->reserved = true;
registers[rbx]->reserved = true;
}
void dispose() {
zone.dispose();
ipMappings.dispose();
constantPool.dispose();
code.dispose();
2007-12-11 21:26:59 +00:00
if (ipTable) s->free(ipTable);
2007-12-11 00:48:09 +00:00
}
System* s;
Vector code;
Vector constantPool;
Vector ipMappings;
Zone zone;
intptr_t indirectCaller;
StackOperand* stack;
2007-12-11 21:26:59 +00:00
IpMapping** ipTable;
unsigned reserved;
uint8_t* output;
2007-12-11 21:26:59 +00:00
RegisterOperand* registers[RegisterCount];
2007-12-11 00:48:09 +00:00
};
inline void NO_RETURN
abort(Context* c)
{
abort(c->s);
}
#ifndef NDEBUG
inline void
assert(Context* c, bool v)
{
assert(c->s, v);
}
#endif // not NDEBUG
inline void
expect(Context* c, bool v)
{
expect(c->s, v);
}
ImmediateOperand*
immediate(Context* c, MyPromise* p)
{
return new (c->zone.allocate(sizeof(ImmediateOperand))) ImmediateOperand(p);
}
2007-12-09 20:03:21 +00:00
ImmediateOperand*
immediate(Context* c, intptr_t v)
{
return immediate(c, new (c->zone.allocate(sizeof(ResolvedPromise)))
ResolvedPromise(v));
2007-12-09 20:03:21 +00:00
}
AbsoluteOperand*
2007-12-11 00:48:09 +00:00
absolute(Context* c, MyPromise* v)
2007-12-09 20:03:21 +00:00
{
2007-12-11 00:48:09 +00:00
return new (c->zone.allocate(sizeof(AbsoluteOperand))) AbsoluteOperand(v);
2007-12-09 20:03:21 +00:00
}
RegisterOperand*
register_(Context* c, Register v)
{
2007-12-11 00:48:09 +00:00
return c->registers[v];
2007-12-09 20:03:21 +00:00
}
MemoryOperand*
memory(Context* c, MyOperand* base, int displacement,
MyOperand* index, unsigned scale)
{
2007-12-11 00:48:09 +00:00
return new (c->zone.allocate(sizeof(MemoryOperand)))
MemoryOperand(base, displacement, index, scale);
2007-12-09 20:03:21 +00:00
}
2007-12-11 21:26:59 +00:00
IpMapping*
currentMapping(Context* c)
{
IpMapping* mapping;
c->ipMappings.get
(c->ipMappings.length() - BytesPerWord, &mapping, BytesPerWord);
return mapping;
}
2007-12-11 00:48:09 +00:00
RegisterOperand*
temporary(Context* c)
2007-12-11 00:48:09 +00:00
{
2007-12-11 21:26:59 +00:00
// we don't yet support using r9-r15
for (unsigned i = 0; i < 8/*RegisterCount*/; ++i) {
2007-12-11 00:48:09 +00:00
if (not c->registers[i]->reserved) {
c->registers[i]->reserved = true;
return c->registers[i];
2007-12-11 00:48:09 +00:00
}
}
abort(c);
2007-12-11 00:48:09 +00:00
}
StackOperand*
pushed(Context* c)
2007-12-11 00:48:09 +00:00
{
int index = (c->stack ?
c->stack->index + (c->stack->footprint() / BytesPerWord) :
0);
MyOperand* base = memory
(c, register_(c, rbp), - (c->reserved + index + 1) * BytesPerWord, 0, 1);
return c->stack = new (c->zone.allocate(sizeof(StackOperand)))
StackOperand(base, index, c->stack);
2007-12-11 00:48:09 +00:00
}
void
push(Context* c, int count)
{
2007-12-13 00:18:31 +00:00
immediate(c, count * BytesPerWord)->apply
(c, MyOperand::sub, register_(c, rsp));
while (count) {
-- count;
pushed(c);
}
}
StackOperand*
push(Context* c, MyOperand* v)
{
v->apply(c, MyOperand::push);
return pushed(c);
}
void
pop(Context* c, int count)
{
2007-12-13 00:18:31 +00:00
immediate(c, count * BytesPerWord)->apply
(c, MyOperand::add, register_(c, rsp));
while (count) {
count -= (c->stack->footprint() / BytesPerWord);
assert(c, count >= 0);
c->stack = c->stack->next;
}
}
2007-12-11 00:48:09 +00:00
void
pop(Context* c, MyOperand* dst)
{
dst->apply(c, MyOperand::pop);
2007-12-11 00:48:09 +00:00
c->stack = c->stack->next;
}
MyOperand*
pop(Context* c)
{
RegisterOperand* tmp = temporary(c);
tmp->apply(c, MyOperand::pop);
MyOperand* r = tmp;
2007-12-11 00:48:09 +00:00
c->stack = c->stack->next;
2007-12-11 21:26:59 +00:00
return r;
2007-12-09 20:03:21 +00:00
}
MyOperand*
2007-12-09 22:45:43 +00:00
selection(Context* c, SelectionOperand::SelectionType type, MyOperand* base)
2007-12-09 20:03:21 +00:00
{
2007-12-09 22:45:43 +00:00
if ((type == SelectionOperand::S4Selection and BytesPerWord == 4)
or (type == SelectionOperand::S8Selection and BytesPerWord == 8))
2007-12-09 20:03:21 +00:00
{
return base;
} else {
2007-12-11 00:48:09 +00:00
return new (c->zone.allocate(sizeof(SelectionOperand)))
SelectionOperand(type, base);
2007-12-09 20:03:21 +00:00
}
}
Register
gpRegister(Context* c, unsigned index)
{
switch (index) {
case 0:
return rdi;
case 1:
return rsi;
case 2:
return rdx;
case 3:
return rcx;
case 4:
return r8;
case 5:
return r9;
default:
abort(c);
}
2007-12-09 22:45:43 +00:00
}
unsigned
pushArguments(Context* c, unsigned count, va_list list)
{
MyOperand* arguments[count];
unsigned footprint = 0;
for (unsigned i = 0; i < count; ++i) {
arguments[i] = va_arg(list, MyOperand*);
footprint += pad(arguments[i]->footprint());
}
const int GprCount = 6;
2007-12-09 22:45:43 +00:00
for (int i = count - 1; i >= 0; --i) {
if (BytesPerWord == 8 and i < GprCount) {
2007-12-11 00:48:09 +00:00
arguments[i]->apply(c, MyOperand::mov, register_(c, gpRegister(c, i)));
} else {
2007-12-11 00:48:09 +00:00
arguments[i]->apply(c, MyOperand::push);
}
2007-12-09 22:45:43 +00:00
}
if (BytesPerWord == 8) {
if (footprint > GprCount * BytesPerWord) {
return footprint - GprCount * BytesPerWord;
} else {
return 0;
}
} else {
return footprint;
}
2007-12-09 20:03:21 +00:00
}
2007-12-11 00:48:09 +00:00
void
rex(Context* c)
{
if (BytesPerWord == 8) {
c->code.append(0x48);
}
}
void
ret(Context* c)
{
c->code.append(0xc3);
}
void
encode(Context* c, uint8_t instruction, uint8_t zeroPrefix,
uint8_t bytePrefix, uint8_t wordPrefix,
Register a, Register b, int32_t offset)
{
c->code.append(instruction);
uint8_t prefix;
if (offset == 0 and b != rbp) {
prefix = zeroPrefix;
} else if (isInt8(offset)) {
prefix = bytePrefix;
} else {
prefix = wordPrefix;
}
c->code.append(prefix | (a << 3) | b);
if (b == rsp) {
c->code.append(0x24);
}
if (offset == 0 and b != rbp) {
// do nothing
} else if (isInt8(offset)) {
c->code.append(offset);
} else {
c->code.append4(offset);
}
}
2007-12-11 21:26:59 +00:00
void
RegisterOperand::apply(Context* c, Operation operation)
{
switch (operation) {
case call:
c->code.append(0xff);
c->code.append(0xd0 | value);
2007-12-11 21:26:59 +00:00
break;
case jmp:
c->code.append(0xff);
c->code.append(0xe0 | value);
break;
case pop:
c->code.append(0x58 | value);
break;
case push:
c->code.append(0x50 | value);
2007-12-11 21:26:59 +00:00
break;
default: abort(c);
}
}
void
RegisterOperand::accept(Context* c, Operation operation,
RegisterOperand* operand)
{
switch (operation) {
case add:
rex(c);
c->code.append(0x01);
c->code.append(0xc0 | (operand->value << 3) | value);
break;
case cmp:
rex(c);
c->code.append(0x39);
c->code.append(0xc0 | (operand->value << 3) | value);
break;
2007-12-11 21:26:59 +00:00
case mov:
if (value != operand->value) {
rex(c);
c->code.append(0x89);
c->code.append(0xc0 | (operand->value << 3) | value);
}
break;
default: abort(c);
}
}
void
RegisterOperand::accept(Context* c, Operation operation,
ImmediateOperand* operand)
{
switch (operation) {
case add: {
intptr_t v = operand->value->value(c);
if (v) {
assert(c, isInt8(v)); // todo
rex(c);
c->code.append(0x83);
c->code.append(0xc0 | value);
c->code.append(v);
}
} break;
case and_: {
intptr_t v = operand->value->value(c);
if (v) {
rex(c);
if (isInt8(v)) {
c->code.append(0x83);
c->code.append(0xe0 | value);
c->code.append(v);
} else {
assert(c, isInt32(v));
c->code.append(0x81);
c->code.append(0xe0 | value);
c->code.append(v);
}
}
} break;
case cmp: {
intptr_t v = operand->value->value(c);
2007-12-13 00:18:31 +00:00
assert(c, isInt8(v)); // todo
2007-12-13 00:18:31 +00:00
rex(c);
c->code.append(0x83);
c->code.append(0xf8 | value);
c->code.append(v);
} break;
case mov: {
intptr_t v = operand->value->value(c);
2007-12-11 23:52:28 +00:00
rex(c);
c->code.append(0xb8 | value);
c->code.appendAddress(v);
} break;
2007-12-11 23:52:28 +00:00
case sub: {
intptr_t v = operand->value->value(c);
if (v) {
assert(c, isInt8(v)); // todo
2007-12-11 21:26:59 +00:00
2007-12-11 23:52:28 +00:00
rex(c);
c->code.append(0x83);
c->code.append(0xe8 | value);
c->code.append(v);
2007-12-11 23:52:28 +00:00
}
} break;
2007-12-11 21:26:59 +00:00
default: abort(c);
}
}
void
RegisterOperand::accept(Context* c, Operation operation,
MemoryOperand* operand)
{
switch (operation) {
2007-12-13 00:18:31 +00:00
case cmp:
rex(c);
encode(c, 0x3b, 0, 0x40, 0x80, value, operand->base->asRegister(c),
operand->displacement);
break;
2007-12-11 21:26:59 +00:00
case mov:
rex(c);
encode(c, 0x8b, 0, 0x40, 0x80, value, operand->base->asRegister(c),
operand->displacement);
break;
default: abort(c);
}
}
2007-12-11 23:52:28 +00:00
class AbsoluteMovTask: public IpTask {
public:
AbsoluteMovTask(unsigned start, MyPromise* promise, IpTask* next):
IpTask(next), start(start), promise(promise)
{ }
virtual void run(Context* c UNUSED, unsigned, unsigned start, unsigned,
unsigned offset)
2007-12-11 23:52:28 +00:00
{
uint8_t* instruction = c->output + offset + (this->start - start);
intptr_t v = reinterpret_cast<intptr_t>(c->output + promise->value(c));
2007-12-11 23:52:28 +00:00
memcpy(instruction + (BytesPerWord / 8) + 1, &v, BytesPerWord);
}
unsigned start;
MyPromise* promise;
};
void
addAbsoluteMovTask(Context* c, MyPromise* p)
{
IpMapping* mapping = currentMapping(c);
mapping->task = new (c->zone.allocate(sizeof(AbsoluteMovTask)))
AbsoluteMovTask(c->code.length(), p, mapping->task);
}
2007-12-11 23:52:28 +00:00
void
RegisterOperand::accept(Context* c, Operation operation,
AbsoluteOperand* operand)
{
switch (operation) {
2007-12-13 00:18:31 +00:00
case cmp: {
RegisterOperand* tmp = temporary(c);
addAbsoluteMovTask(c, operand->value);
tmp->accept(c, mov, immediate(c, 0));
accept(c, cmp, memory(c, tmp, 0, 0, 1));
tmp->release(c);
} break;
2007-12-11 23:52:28 +00:00
case mov: {
addAbsoluteMovTask(c, operand->value);
2007-12-11 23:52:28 +00:00
accept(c, mov, immediate(c, 0));
accept(c, mov, memory(c, this, 0, 0, 1));
} break;
default: abort(c);
}
}
class DirectJumpTask: public IpTask {
2007-12-11 21:26:59 +00:00
public:
DirectJumpTask(unsigned start, unsigned offset, MyPromise* address,
IpTask* next):
IpTask(next), start(start), offset(offset), address(address)
2007-12-11 21:26:59 +00:00
{ }
virtual void run(Context* c UNUSED, unsigned, unsigned start, unsigned,
unsigned offset)
2007-12-11 21:26:59 +00:00
{
uint8_t* instruction = c->output + offset + (this->start - start);
intptr_t v = reinterpret_cast<uint8_t*>(address->value(c))
- instruction - 4 - this->offset;
2007-12-11 21:26:59 +00:00
assert(c, isInt32(v));
2007-12-11 23:52:28 +00:00
2007-12-11 21:26:59 +00:00
int32_t v32 = v;
memcpy(instruction + this->offset, &v32, 4);
2007-12-11 21:26:59 +00:00
}
unsigned start;
unsigned offset;
MyPromise* address;
2007-12-11 21:26:59 +00:00
};
void
addDirectJumpTask(Context* c, unsigned offset, MyPromise* p)
2007-12-11 21:26:59 +00:00
{
IpMapping* mapping = currentMapping(c);
mapping->task = new (c->zone.allocate(sizeof(DirectJumpTask)))
DirectJumpTask(c->code.length(), offset, p, mapping->task);
}
void
immediateConditional(Context* c, unsigned condition,
ImmediateOperand* operand)
{
addDirectJumpTask(c, 2, operand->value);
c->code.append(0x0f);
c->code.append(condition);
c->code.append4(0);
2007-12-11 21:26:59 +00:00
}
void
ImmediateOperand::apply(Context* c, Operation operation)
{
switch (operation) {
case alignedCall: {
while ((c->code.length() + 1) % 4) {
c->code.append(0x90);
}
apply(c, call);
} break;
case call:
addDirectJumpTask(c, 1, value);
2007-12-11 21:26:59 +00:00
c->code.append(0xe8);
2007-12-11 21:26:59 +00:00
c->code.append4(0);
break;
case jmp:
addDirectJumpTask(c, 1, value);
c->code.append(0xe9);
c->code.append4(0);
break;
case je:
immediateConditional(c, 0x84, this);
break;
case jne:
immediateConditional(c, 0x85, this);
break;
case jg:
immediateConditional(c, 0x8f, this);
break;
case jge:
immediateConditional(c, 0x8d, this);
break;
case jl:
immediateConditional(c, 0x8c, this);
break;
case jle:
immediateConditional(c, 0x8e, this);
break;
case push: {
intptr_t v = value->value(c);
if (isInt8(v)) {
c->code.append(0x6a);
c->code.append(v);
} else if (isInt32(v)) {
c->code.append(0x68);
c->code.append4(v);
} else {
RegisterOperand* tmp = temporary(c);
tmp->accept(c, mov, this);
tmp->apply(c, push);
tmp->release(c);
}
} break;
2007-12-11 21:26:59 +00:00
default: abort(c);
}
}
void
absoluteApply(Context* c, MyOperand::Operation operation,
AbsoluteOperand* operand)
{
addAbsoluteMovTask(c, operand->value);
RegisterOperand* tmp = temporary(c);
tmp->accept(c, MyOperand::mov, immediate(c, 0));
memory(c, tmp, 0, 0, 1)->apply(c, operation);
tmp->release(c);
}
2007-12-11 21:26:59 +00:00
void
AbsoluteOperand::apply(Context* c, Operation operation)
{
switch (operation) {
case push:
absoluteApply(c, operation, this);
break;
2007-12-11 21:26:59 +00:00
default: abort(c);
}
}
Register
MemoryOperand::asRegister(Context* c)
2007-12-11 21:26:59 +00:00
{
RegisterOperand* tmp = temporary(c);
2007-12-11 21:26:59 +00:00
tmp->accept(c, mov, this);
tmp->release(c);
return tmp->value;
2007-12-11 21:26:59 +00:00
}
void
MemoryOperand::apply(Context* c, Operation operation)
{
switch (operation) {
case call:
encode(c, 0xff, 0x10, 0x50, 0x90, rax, base->asRegister(c), displacement);
break;
2007-12-11 23:52:28 +00:00
case pop:
encode(c, 0x8f, 0, 0x40, 0x80, rax, base->asRegister(c), displacement);
break;
case push:
encode(c, 0xff, 0x30, 0x70, 0xb0, rax, base->asRegister(c), displacement);
break;
2007-12-11 21:26:59 +00:00
default: abort(c);
}
}
void
MemoryOperand::accept(Context* c, Operation operation,
RegisterOperand* operand)
{
switch (operation) {
case add:
rex(c);
encode(c, 0x01, 0, 0x40, 0x80, operand->value, base->asRegister(c),
displacement);
break;
2007-12-11 21:26:59 +00:00
case mov:
rex(c);
encode(c, 0x89, 0, 0x40, 0x80, operand->value, base->asRegister(c),
displacement);
break;
case sub:
rex(c);
encode(c, 0x29, 0, 0x40, 0x80, operand->value, base->asRegister(c),
displacement);
break;
2007-12-11 21:26:59 +00:00
default: abort(c);
}
}
void
MemoryOperand::accept(Context* c, Operation operation,
ImmediateOperand* operand)
{
switch (operation) {
case add: {
rex(c);
intptr_t v = operand->value->value(c);
unsigned i = (isInt8(v) ? 0x83 : 0x81);
encode(c, i, 0, 0x40, 0x80, rax, base->asRegister(c), displacement);
if (isInt8(v)) {
c->code.append(v);
} else if (isInt32(v)) {
c->code.append4(v);
} else {
abort(c);
}
} break;
case mov: {
intptr_t v = operand->value->value(c);
assert(c, isInt32(v)); // todo
2007-12-11 21:26:59 +00:00
rex(c);
encode(c, 0xc7, 0, 0x40, 0x80, rax, base->asRegister(c), displacement);
c->code.append4(v);
} break;
2007-12-11 21:26:59 +00:00
default: abort(c);
}
}
void
MemoryOperand::accept(Context* c, Operation operation,
AbsoluteOperand* operand)
{
switch (operation) {
case mov: {
RegisterOperand* tmp = temporary(c);
tmp->accept(c, mov, operand);
accept(c, mov, tmp);
tmp->release(c);
} break;
default: abort(c);
}
}
intptr_t
2007-12-11 23:52:28 +00:00
PoolPromise::value(Context* c)
{
if (resolved(c)) {
2007-12-11 23:52:28 +00:00
return c->code.length() + key;
}
abort(c);
}
bool
PoolPromise::resolved(Context* c)
{
return c->output != 0;
}
intptr_t
2007-12-11 23:52:28 +00:00
CodePromise::value(Context* c)
{
if (resolved(c)) {
if (absolute) {
return reinterpret_cast<intptr_t>(c->output + key);
} else {
return key;
}
2007-12-11 23:52:28 +00:00
}
abort(c);
}
bool
CodePromise::resolved(Context* c)
{
return c->output != 0;
}
intptr_t
2007-12-11 23:52:28 +00:00
IpPromise::value(Context* c)
{
if (resolved(c)) {
2007-12-11 23:52:28 +00:00
unsigned bottom = 0;
unsigned top = c->ipMappings.length() / BytesPerWord;
for (unsigned span = top - bottom; span; span = top - bottom) {
unsigned middle = bottom + (span / 2);
IpMapping* mapping = c->ipTable[middle];
if (key == mapping->ip) {
if (absolute) {
return reinterpret_cast<intptr_t>(c->output + mapping->start);
} else {
return mapping->start;
}
2007-12-11 23:52:28 +00:00
} else if (key < mapping->ip) {
top = middle;
} else if (key > mapping->ip) {
bottom = middle + 1;
}
}
}
abort(c);
}
bool
IpPromise::resolved(Context* c)
{
return c->output != 0;
}
2007-12-11 23:52:28 +00:00
void
runTasks(Context* c, IpTask::Priority priority)
2007-12-11 23:52:28 +00:00
{
uint8_t* p = c->output;
2007-12-11 23:52:28 +00:00
for (unsigned i = 0; i < c->ipMappings.length() / BytesPerWord; ++i) {
IpMapping* mapping = c->ipTable[i];
int length = mapping->end - mapping->start;
for (IpTask* t = mapping->task; t; t = t->next) {
if (t->priority() == priority) {
t->run(c, mapping->ip, mapping->start, mapping->end, p - c->output);
2007-12-11 23:52:28 +00:00
}
}
p += length;
}
}
2007-12-08 23:22:13 +00:00
class MyCompiler: public Compiler {
public:
MyCompiler(System* s, void* indirectCaller):
2007-12-09 20:03:21 +00:00
c(s, indirectCaller)
2007-12-08 23:22:13 +00:00
{ }
virtual Promise* poolOffset() {
2007-12-11 00:48:09 +00:00
return new (c.zone.allocate(sizeof(PoolPromise)))
PoolPromise(c.constantPool.length());
2007-12-08 23:22:13 +00:00
}
virtual Promise* codeOffset() {
2007-12-11 21:26:59 +00:00
if (c.code.length() == 0) {
return new (c.zone.allocate(sizeof(CodePromise))) CodePromise(0, false);
2007-12-11 21:26:59 +00:00
} else {
CodePromise* p = new (c.zone.allocate(sizeof(CodePromise)))
CodePromise(c.code.length(), false);
2007-12-11 21:26:59 +00:00
IpMapping* mapping = currentMapping(&c);
mapping->task = new (c.zone.allocate(sizeof(CodePromiseTask)))
CodePromiseTask(p, mapping->task);
return p;
}
2007-12-08 23:22:13 +00:00
}
virtual Operand* poolAppend(Operand* v) {
2007-12-11 00:48:09 +00:00
Operand* r = absolute(&c, static_cast<MyPromise*>(poolOffset()));
c.constantPool.appendAddress(v);
2007-12-08 23:22:13 +00:00
return r;
}
virtual Operand* constant(intptr_t v) {
2007-12-09 20:03:21 +00:00
return immediate(&c, v);
2007-12-08 23:22:13 +00:00
}
virtual void push(unsigned count) {
::push(&c, count);
}
2007-12-08 23:22:13 +00:00
virtual void push(Operand* v) {
2007-12-11 00:48:09 +00:00
::push(&c, static_cast<MyOperand*>(v));
2007-12-08 23:22:13 +00:00
}
virtual void push2(Operand* v) {
2007-12-09 20:03:21 +00:00
push(v);
if (BytesPerWord == 8) push(immediate(&c, 0));
2007-12-08 23:22:13 +00:00
}
virtual Operand* stack(unsigned index) {
2007-12-11 00:48:09 +00:00
StackOperand* s = c.stack;
unsigned i = 0;
if (s->footprint() / BytesPerWord == 2) ++ i;
for (; i < index; ++i) {
s = s->next;
if (s->footprint() / BytesPerWord == 2) ++ i;
}
2007-12-08 23:22:13 +00:00
2007-12-11 00:48:09 +00:00
return s;
2007-12-08 23:22:13 +00:00
}
virtual void pop(unsigned count) {
::pop(&c, count);
}
2007-12-08 23:22:13 +00:00
virtual Operand* pop() {
2007-12-11 00:48:09 +00:00
return ::pop(&c);
2007-12-08 23:22:13 +00:00
}
virtual Operand* pop2() {
2007-12-09 20:03:21 +00:00
if (BytesPerWord == 8) pop();
return pop();
2007-12-08 23:22:13 +00:00
}
virtual void pop(Operand* dst) {
2007-12-11 00:48:09 +00:00
::pop(&c, static_cast<MyOperand*>(dst));
2007-12-08 23:22:13 +00:00
}
virtual void pop2(Operand* dst) {
2007-12-09 20:03:21 +00:00
if (BytesPerWord == 8) pop();
pop(dst);
2007-12-08 23:22:13 +00:00
}
virtual Operand* stack() {
2007-12-09 20:03:21 +00:00
return register_(&c, rsp);
2007-12-08 23:22:13 +00:00
}
virtual Operand* base() {
2007-12-09 20:03:21 +00:00
return register_(&c, rbp);
2007-12-08 23:22:13 +00:00
}
virtual Operand* thread() {
2007-12-09 20:03:21 +00:00
return register_(&c, rbx);
2007-12-08 23:22:13 +00:00
}
virtual Operand* indirectTarget() {
2007-12-09 20:03:21 +00:00
return register_(&c, rax);
2007-12-08 23:22:13 +00:00
}
virtual Operand* temporary() {
return ::temporary(&c);
2007-12-08 23:22:13 +00:00
}
virtual void release(Operand* v) {
2007-12-11 00:48:09 +00:00
static_cast<MyOperand*>(v)->release(&c);
2007-12-08 23:22:13 +00:00
}
virtual Operand* label() {
return immediate
(&c, new (c.zone.allocate(sizeof(CodePromise))) CodePromise(0, true));
2007-12-08 23:22:13 +00:00
}
virtual void mark(Operand* label) {
static_cast<MyOperand*>(label)->setImmediate(&c, c.code.length());
2007-12-08 23:22:13 +00:00
}
virtual Operand* indirectCall
(Operand* address, unsigned argumentCount, ...)
{
va_list a; va_start(a, argumentCount);
2007-12-09 22:45:43 +00:00
unsigned footprint = pushArguments(&c, argumentCount, a);
2007-12-08 23:22:13 +00:00
va_end(a);
2007-12-11 00:48:09 +00:00
static_cast<MyOperand*>(address)->apply
(&c, MyOperand::mov, register_(&c, rax));
2007-12-08 23:22:13 +00:00
2007-12-11 00:48:09 +00:00
immediate(&c, c.indirectCaller)->apply(&c, MyOperand::call);
immediate(&c, footprint)->apply(&c, MyOperand::add, register_(&c, rsp));
2007-12-09 22:45:43 +00:00
return register_(&c, rax);
2007-12-08 23:22:13 +00:00
}
2007-12-09 22:45:43 +00:00
virtual void indirectCallNoReturn
2007-12-08 23:22:13 +00:00
(Operand* address, unsigned argumentCount, ...)
{
va_list a; va_start(a, argumentCount);
2007-12-09 20:03:21 +00:00
pushArguments(&c, argumentCount, a);
2007-12-08 23:22:13 +00:00
va_end(a);
2007-12-11 00:48:09 +00:00
static_cast<MyOperand*>(address)->apply
(&c, MyOperand::mov, register_(&c, rax));
immediate(&c, c.indirectCaller)->apply(&c, MyOperand::call);
2007-12-08 23:22:13 +00:00
}
virtual Operand* directCall
(Operand* address, unsigned argumentCount, ...)
{
va_list a; va_start(a, argumentCount);
2007-12-09 22:45:43 +00:00
unsigned footprint = pushArguments(&c, argumentCount, a);
2007-12-08 23:22:13 +00:00
va_end(a);
2007-12-11 00:48:09 +00:00
static_cast<MyOperand*>(address)->apply(&c, MyOperand::call);
2007-12-08 23:22:13 +00:00
immediate(&c, footprint)->apply(&c, MyOperand::add, register_(&c, rsp));
2007-12-09 22:45:43 +00:00
return register_(&c, rax);
2007-12-08 23:22:13 +00:00
}
virtual void return_(Operand* v) {
2007-12-11 00:48:09 +00:00
static_cast<MyOperand*>(v)->apply(&c, MyOperand::mov, register_(&c, rax));
epilogue();
2007-12-09 22:45:43 +00:00
ret();
2007-12-08 23:22:13 +00:00
}
2007-12-11 00:48:09 +00:00
virtual Operand* call(Operand* v) {
static_cast<MyOperand*>(v)->apply(&c, MyOperand::call);
return register_(&c, rax);
}
virtual Operand* alignedCall(Operand* v) {
static_cast<MyOperand*>(v)->apply(&c, MyOperand::alignedCall);
return register_(&c, rax);
}
2007-12-08 23:22:13 +00:00
virtual void ret() {
2007-12-09 22:45:43 +00:00
::ret(&c);
2007-12-08 23:22:13 +00:00
}
virtual void mov(Operand* src, Operand* dst) {
2007-12-11 00:48:09 +00:00
static_cast<MyOperand*>(src)->apply
(&c, MyOperand::mov, static_cast<MyOperand*>(dst));
2007-12-08 23:22:13 +00:00
}
virtual void cmp(Operand* subtrahend, Operand* minuend) {
2007-12-11 00:48:09 +00:00
static_cast<MyOperand*>(subtrahend)->apply
(&c, MyOperand::cmp, static_cast<MyOperand*>(minuend));
2007-12-08 23:22:13 +00:00
}
virtual void jl(Operand* v) {
2007-12-11 00:48:09 +00:00
static_cast<MyOperand*>(v)->apply(&c, MyOperand::jl);
2007-12-08 23:22:13 +00:00
}
virtual void jg(Operand* v) {
2007-12-11 00:48:09 +00:00
static_cast<MyOperand*>(v)->apply(&c, MyOperand::jg);
2007-12-08 23:22:13 +00:00
}
virtual void jle(Operand* v) {
2007-12-11 00:48:09 +00:00
static_cast<MyOperand*>(v)->apply(&c, MyOperand::jle);
2007-12-08 23:22:13 +00:00
}
virtual void jge(Operand* v) {
2007-12-11 00:48:09 +00:00
static_cast<MyOperand*>(v)->apply(&c, MyOperand::jge);
2007-12-08 23:22:13 +00:00
}
virtual void je(Operand* v) {
2007-12-11 00:48:09 +00:00
static_cast<MyOperand*>(v)->apply(&c, MyOperand::je);
2007-12-08 23:22:13 +00:00
}
virtual void jne(Operand* v) {
2007-12-11 00:48:09 +00:00
static_cast<MyOperand*>(v)->apply(&c, MyOperand::jne);
2007-12-08 23:22:13 +00:00
}
virtual void jmp(Operand* v) {
2007-12-11 00:48:09 +00:00
static_cast<MyOperand*>(v)->apply(&c, MyOperand::jmp);
2007-12-08 23:22:13 +00:00
}
virtual void add(Operand* v, Operand* dst) {
2007-12-11 00:48:09 +00:00
static_cast<MyOperand*>(v)->apply
(&c, MyOperand::add, static_cast<MyOperand*>(dst));
2007-12-08 23:22:13 +00:00
}
virtual void sub(Operand* v, Operand* dst) {
2007-12-11 00:48:09 +00:00
static_cast<MyOperand*>(v)->apply
(&c, MyOperand::sub, static_cast<MyOperand*>(dst));
2007-12-08 23:22:13 +00:00
}
virtual void mul(Operand* v, Operand* dst) {
2007-12-11 00:48:09 +00:00
static_cast<MyOperand*>(v)->apply
(&c, MyOperand::mul, static_cast<MyOperand*>(dst));
2007-12-08 23:22:13 +00:00
}
virtual void div(Operand* v, Operand* dst) {
2007-12-11 00:48:09 +00:00
static_cast<MyOperand*>(v)->apply
(&c, MyOperand::div, static_cast<MyOperand*>(dst));
2007-12-08 23:22:13 +00:00
}
virtual void rem(Operand* v, Operand* dst) {
2007-12-11 00:48:09 +00:00
static_cast<MyOperand*>(v)->apply
(&c, MyOperand::rem, static_cast<MyOperand*>(dst));
2007-12-08 23:22:13 +00:00
}
virtual void shl(Operand* v, Operand* dst) {
2007-12-11 00:48:09 +00:00
static_cast<MyOperand*>(v)->apply
(&c, MyOperand::shl, static_cast<MyOperand*>(dst));
2007-12-08 23:22:13 +00:00
}
virtual void shr(Operand* v, Operand* dst) {
2007-12-11 00:48:09 +00:00
static_cast<MyOperand*>(v)->apply
(&c, MyOperand::shr, static_cast<MyOperand*>(dst));
2007-12-08 23:22:13 +00:00
}
virtual void ushr(Operand* v, Operand* dst) {
2007-12-11 00:48:09 +00:00
static_cast<MyOperand*>(v)->apply
(&c, MyOperand::ushr, static_cast<MyOperand*>(dst));
2007-12-08 23:22:13 +00:00
}
virtual void and_(Operand* v, Operand* dst) {
2007-12-11 00:48:09 +00:00
static_cast<MyOperand*>(v)->apply
(&c, MyOperand::and_, static_cast<MyOperand*>(dst));
2007-12-08 23:22:13 +00:00
}
virtual void or_(Operand* v, Operand* dst) {
2007-12-11 00:48:09 +00:00
static_cast<MyOperand*>(v)->apply
(&c, MyOperand::or_, static_cast<MyOperand*>(dst));
2007-12-08 23:22:13 +00:00
}
virtual void xor_(Operand* v, Operand* dst) {
2007-12-11 00:48:09 +00:00
static_cast<MyOperand*>(v)->apply
(&c, MyOperand::xor_, static_cast<MyOperand*>(dst));
2007-12-08 23:22:13 +00:00
}
virtual void neg(Operand* v) {
2007-12-11 00:48:09 +00:00
static_cast<MyOperand*>(v)->apply(&c, MyOperand::neg);
2007-12-08 23:22:13 +00:00
}
2007-12-09 20:03:21 +00:00
virtual Operand* memory(Operand* base, int displacement,
2007-12-08 23:22:13 +00:00
Operand* index, unsigned scale)
{
2007-12-09 22:45:43 +00:00
return ::memory(&c, static_cast<MyOperand*>(base), displacement,
static_cast<MyOperand*>(index), scale);
}
virtual Operand* select1(Operand* v) {
return selection(&c, SelectionOperand::S1Selection,
static_cast<MyOperand*>(v));
}
virtual Operand* select2(Operand* v) {
return selection(&c, SelectionOperand::S2Selection,
static_cast<MyOperand*>(v));
}
virtual Operand* select2z(Operand* v) {
return selection(&c, SelectionOperand::Z2Selection,
static_cast<MyOperand*>(v));
}
virtual Operand* select4(Operand* v) {
return selection(&c, SelectionOperand::S4Selection,
static_cast<MyOperand*>(v));
2007-12-08 23:22:13 +00:00
}
2007-12-09 22:45:43 +00:00
virtual Operand* select8(Operand* v) {
return selection(&c, SelectionOperand::S8Selection,
static_cast<MyOperand*>(v));
2007-12-08 23:22:13 +00:00
}
virtual void prologue() {
2007-12-11 00:48:09 +00:00
register_(&c, rbp)->apply(&c, MyOperand::push);
register_(&c, rsp)->apply(&c, MyOperand::mov, register_(&c, rbp));
2007-12-08 23:22:13 +00:00
}
virtual void reserve(unsigned size) {
immediate(&c, size * BytesPerWord)->apply
(&c, MyOperand::sub, register_(&c, rsp));
c.reserved = size;
}
2007-12-08 23:22:13 +00:00
virtual void epilogue() {
2007-12-11 00:48:09 +00:00
register_(&c, rbp)->apply(&c, MyOperand::mov, register_(&c, rsp));
2007-12-11 21:26:59 +00:00
register_(&c, rbp)->apply(&c, MyOperand::pop);
2007-12-08 23:22:13 +00:00
}
2007-12-09 20:03:21 +00:00
virtual void startLogicalIp(unsigned ip) {
2007-12-11 21:26:59 +00:00
c.ipMappings.appendAddress
(new (c.zone.allocate(sizeof(IpMapping)))
IpMapping(ip, c.code.length()));
2007-12-08 23:22:13 +00:00
}
2007-12-09 20:03:21 +00:00
virtual Operand* logicalIp(unsigned ip) {
return immediate
(&c, new (c.zone.allocate(sizeof(IpPromise))) IpPromise(ip, true));
2007-12-08 23:22:13 +00:00
}
2007-12-11 00:48:09 +00:00
virtual Promise* logicalIpToOffset(unsigned ip) {
return new (c.zone.allocate(sizeof(IpPromise))) IpPromise(ip, false);
2007-12-11 00:48:09 +00:00
}
2007-12-08 23:22:13 +00:00
2007-12-11 23:52:28 +00:00
virtual unsigned codeSize() {
return c.code.length();
}
virtual unsigned poolSize() {
return c.constantPool.length();
2007-12-11 00:48:09 +00:00
}
2007-12-11 21:26:59 +00:00
virtual void writeTo(uint8_t* out) {
c.output = out;
2007-12-11 21:26:59 +00:00
unsigned tableSize = (c.ipMappings.length() / BytesPerWord);
2007-12-11 00:48:09 +00:00
2007-12-11 21:26:59 +00:00
c.ipTable = static_cast<IpMapping**>
(c.s->allocate(c.ipMappings.length()));
2007-12-11 00:48:09 +00:00
for (unsigned i = 0; i < tableSize; ++i) {
2007-12-11 21:26:59 +00:00
IpMapping* mapping;
c.ipMappings.get(i * BytesPerWord, &mapping, BytesPerWord);
2007-12-11 00:48:09 +00:00
2007-12-11 21:26:59 +00:00
if (i + 1 < tableSize) {
IpMapping* next;
c.ipMappings.get((i + 1) * BytesPerWord, &next, BytesPerWord);
mapping->end = next->start;
2007-12-11 00:48:09 +00:00
} else {
mapping->end = c.code.length();
2007-12-08 23:22:13 +00:00
}
2007-12-11 00:48:09 +00:00
2007-12-11 21:26:59 +00:00
c.ipTable[i] = mapping;
2007-12-08 23:22:13 +00:00
}
2007-12-11 21:26:59 +00:00
qsort(c.ipTable, tableSize, BytesPerWord, compareIpMappingPointers);
2007-12-08 23:22:13 +00:00
2007-12-11 21:26:59 +00:00
uint8_t* p = out;
2007-12-11 00:48:09 +00:00
for (unsigned i = 0; i < tableSize; ++i) {
2007-12-11 21:26:59 +00:00
IpMapping* mapping = c.ipTable[i];
2007-12-11 00:48:09 +00:00
int length = mapping->end - mapping->start;
2007-12-11 21:26:59 +00:00
2007-12-11 23:52:28 +00:00
memcpy(p, c.code.data + mapping->start, length);
2007-12-11 21:26:59 +00:00
2007-12-11 00:48:09 +00:00
p += length;
}
memcpy(p, c.constantPool.data, c.constantPool.length());
2007-12-11 23:52:28 +00:00
runTasks(&c, IpTask::HighPriority);
runTasks(&c, IpTask::LowPriority);
2007-12-08 23:22:13 +00:00
}
2007-12-11 00:48:09 +00:00
virtual void updateCall(void* returnAddress, void* newTarget) {
uint8_t* instruction = static_cast<uint8_t*>(returnAddress) - 5;
assert(&c, *instruction == 0xE8);
2007-12-11 23:52:28 +00:00
assert(&c, reinterpret_cast<uintptr_t>(instruction + 1) % 4 == 0);
2007-12-11 00:48:09 +00:00
int32_t v = static_cast<uint8_t*>(newTarget)
- static_cast<uint8_t*>(returnAddress);
memcpy(instruction + 1, &v, 4);
2007-12-09 20:03:21 +00:00
}
2007-12-08 23:22:13 +00:00
virtual void dispose() {
2007-12-09 20:03:21 +00:00
c.dispose();
2007-12-08 23:22:13 +00:00
2007-12-09 22:45:43 +00:00
c.s->free(this);
2007-12-08 23:22:13 +00:00
}
2007-12-09 20:03:21 +00:00
Context c;
2007-12-08 23:22:13 +00:00
};
intptr_t
2007-12-11 23:52:28 +00:00
MyPromise::value(Compiler* compiler)
2007-12-11 00:48:09 +00:00
{
2007-12-11 23:52:28 +00:00
return value(&(static_cast<MyCompiler*>(compiler)->c));
2007-12-11 00:48:09 +00:00
}
2007-12-08 23:22:13 +00:00
} // namespace
namespace vm {
Compiler*
makeCompiler(System* system, void* indirectCaller)
{
return new (system->allocate(sizeof(MyCompiler)))
MyCompiler(system, indirectCaller);
}
} // namespace v