clean up compile2.cpp so it compiles (but does not yet link)

This commit is contained in:
Joel Dice 2007-11-20 09:21:17 -07:00
parent 6c8a35c341
commit ded1016b32
3 changed files with 783 additions and 162 deletions

114
src/buffer.h Normal file
View File

@ -0,0 +1,114 @@
#ifndef BUFFER_H
#define BUFFER_H
#include "system.h"
namespace vm {
class Buffer {
public:
Buffer(System* s, unsigned minimumCapacity):
s(s),
data(0),
position(0),
capacity(0),
minimumCapacity(minimumCapacity)
{ }
~Buffer() {
if (data) {
s->free(data);
}
}
void ensure(unsigned space) {
if (position + space > capacity) {
unsigned newCapacity = max
(position + space, max(minimumCapacity, capacity * 2));
uint8_t* newData = static_cast<uint8_t*>(s->allocate(newCapacity));
if (data) {
memcpy(newData, data, position);
s->free(data);
}
data = newData;
}
}
void append(uint8_t v) {
ensure(1);
data[position++] = v;
}
void append2(uint16_t v) {
ensure(2);
memcpy(data + position, &v, 2);
position += 2;
}
void append4(uint32_t v) {
ensure(4);
memcpy(data + position, &v, 4);
position += 4;
}
void set2(unsigned offset, uint32_t v) {
assert(s, offset + 2 <= position);
memcpy(data + offset, &v, 2);
}
void set4(unsigned offset, uint32_t v) {
assert(s, offset + 4 <= position);
memcpy(data + offset, &v, 4);
}
uint8_t& get(unsigned offset) {
assert(s, offset + 1 <= position);
return data[offset];
}
uint16_t& get2(unsigned offset) {
assert(s, offset + 2 <= position);
return *reinterpret_cast<uint16_t*>(data + offset);
}
uint32_t& get4(unsigned offset) {
assert(s, offset + 4 <= position);
return *reinterpret_cast<uint32_t*>(data + offset);
}
uintptr_t& getAddress(unsigned offset) {
assert(s, offset + 4 <= position);
return *reinterpret_cast<uintptr_t*>(data + offset);
}
void appendAddress(uintptr_t v) {
append4(v);
if (BytesPerWord == 8) {
// we have to use the preprocessor here to avoid a warning on
// 32-bit systems
#ifdef __x86_64__
append4(v >> 32);
#endif
}
}
unsigned length() {
return position;
}
void copyTo(void* b) {
if (data) {
memcpy(b, data, position);
}
}
System* s;
uint8_t* data;
unsigned position;
unsigned capacity;
unsigned minimumCapacity;
};
} // namespace vm
#endif//BUFFER_H

File diff suppressed because it is too large Load Diff

86
src/compiler.h Normal file
View File

@ -0,0 +1,86 @@
#ifndef COMPILER_H
#define COMPILER_H
namespace vm {
class Operand { };
class Compiler {
public:
virtual ~Compiler() { }
virtual Operand* append(Operand*) = 0;
virtual Operand* constant(intptr_t) = 0;
virtual unsigned poolOffset() = 0;
virtual unsigned poolOffset(Operand*) = 0;
virtual void push(Operand*) = 0;
virtual void push2(Operand*) = 0;
virtual Operand* stack() = 0;
virtual Operand* stack(unsigned) = 0;
virtual Operand* stack2(unsigned) = 0;
virtual Operand* pop() = 0;
virtual Operand* pop2() = 0;
virtual void pop(Operand*) = 0;
virtual void pop2(Operand*) = 0;
virtual Operand* base() = 0;
virtual Operand* thread() = 0;
virtual Operand* temporary() = 0;
virtual Operand* label() = 0;
virtual Operand* call(Operand*) = 0;
virtual Operand* alignedCall(Operand*) = 0;
virtual Operand* indirectCall(Operand*, unsigned, ...) = 0;
virtual Operand* indirectCallNoReturn(Operand*, unsigned, ...) = 0;
virtual Operand* directCall(Operand*, unsigned, ...) = 0;
virtual void mov(Operand*, Operand*) = 0;
virtual void cmp(Operand*, Operand*) = 0;
virtual void jl(Operand*) = 0;
virtual void jg(Operand*) = 0;
virtual void jle(Operand*) = 0;
virtual void jge(Operand*) = 0;
virtual void je(Operand*) = 0;
virtual void jne(Operand*) = 0;
virtual void jmp(Operand*) = 0;
virtual void add(Operand*, Operand*) = 0;
virtual void sub(Operand*, Operand*) = 0;
virtual void mul(Operand*, Operand*) = 0;
virtual void div(Operand*, Operand*) = 0;
virtual void rem(Operand*, Operand*) = 0;
virtual void shl(Operand*, Operand*) = 0;
virtual void shr(Operand*, Operand*) = 0;
virtual void ushr(Operand*, Operand*) = 0;
virtual void and_(Operand*, Operand*) = 0;
virtual void or_(Operand*, Operand*) = 0;
virtual void xor_(Operand*, Operand*) = 0;
virtual void neg(Operand*) = 0;
virtual void mark(Operand*) = 0;
virtual Operand* offset(Operand*, Operand*) = 0;
virtual Operand* offset(Operand*, unsigned) = 0;
virtual Operand* offset1(Operand*, unsigned) = 0;
virtual Operand* offset2(Operand*, unsigned) = 0;
virtual Operand* offset2z(Operand*, unsigned) = 0;
virtual Operand* offset4(Operand*, unsigned) = 0;
virtual Operand* offset8(Operand*, unsigned) = 0;
virtual Operand* dereference(Operand*) = 0;
virtual Operand* dereference1(Operand*) = 0;
virtual Operand* dereference2(Operand*) = 0;
virtual Operand* dereference2z(Operand*) = 0;
virtual Operand* dereference4(Operand*) = 0;
virtual Operand* dereference8(Operand*) = 0;
virtual Operand* select(Operand*) = 0;
virtual Operand* select1(Operand*) = 0;
virtual Operand* select2(Operand*) = 0;
virtual Operand* select2z(Operand*) = 0;
virtual Operand* select4(Operand*) = 0;
virtual Operand* select8(Operand*) = 0;
virtual void prologue(unsigned, unsigned) = 0;
virtual void epilogue(Operand*) = 0;
virtual void epilogue() = 0;
virtual Operand* logicalIp(unsigned) = 0;
virtual void startLogicalIp(unsigned) = 0;
virtual unsigned size() = 0;
virtual unsigned writeTo(uintptr_t*) = 0;
};
} // namespace vm
#endif//COMPILER_H