VM: Refactor layouts/hpp to Factor style

db4
Erik Charlebois 2013-05-11 22:08:13 -04:00
parent 1eaddb0068
commit 0b5a12fdea
1 changed files with 201 additions and 233 deletions

View File

@ -1,5 +1,4 @@
namespace factor namespace factor {
{
typedef unsigned char u8; typedef unsigned char u8;
typedef unsigned short u16; typedef unsigned short u16;
@ -11,32 +10,26 @@ typedef signed int s32;
typedef signed long long s64; typedef signed long long s64;
#ifdef _WIN64 #ifdef _WIN64
typedef long long fixnum; typedef long long fixnum;
typedef unsigned long long cell; typedef unsigned long long cell;
#else #else
typedef long fixnum; typedef long fixnum;
typedef unsigned long cell; typedef unsigned long cell;
#endif #endif
inline static cell align(cell a, cell b) inline static cell align(cell a, cell b) { return (a + (b - 1)) & ~(b - 1); }
{
return (a + (b-1)) & ~(b-1);
}
inline static cell alignment_for(cell a, cell b) inline static cell alignment_for(cell a, cell b) { return align(a, b) - a; }
{
return align(a,b) - a;
}
static const cell data_alignment = 16; static const cell data_alignment = 16;
#define WORD_SIZE (signed)(sizeof(cell)*8) #define WORD_SIZE (signed)(sizeof(cell) * 8)
#define TAG_MASK 15 #define TAG_MASK 15
#define TAG_BITS 4 #define TAG_BITS 4
#define TAG(x) ((cell)(x) & TAG_MASK) #define TAG(x) ((cell)(x) & TAG_MASK)
#define UNTAG(x) ((cell)(x) & ~TAG_MASK) #define UNTAG(x) ((cell)(x) & ~TAG_MASK)
#define RETAG(x,tag) (UNTAG(x) | (tag)) #define RETAG(x, tag) (UNTAG(x) | (tag))
/*** Tags ***/ /*** Tags ***/
#define FIXNUM_TYPE 0 #define FIXNUM_TYPE 0
@ -56,10 +49,8 @@ static const cell data_alignment = 16;
#define TYPE_COUNT 14 #define TYPE_COUNT 14
static inline const char *type_name(cell type) static inline const char* type_name(cell type) {
{ switch (type) {
switch (type)
{
case FIXNUM_TYPE: case FIXNUM_TYPE:
return "fixnum"; return "fixnum";
case F_TYPE: case F_TYPE:
@ -94,16 +85,14 @@ static inline const char *type_name(cell type)
} }
} }
enum code_block_type enum code_block_type {
{
code_block_unoptimized, code_block_unoptimized,
code_block_optimized, code_block_optimized,
code_block_pic code_block_pic
}; };
/* Constants used when floating-point trap exceptions are thrown */ /* Constants used when floating-point trap exceptions are thrown */
enum enum {
{
FP_TRAP_INVALID_OPERATION = 1 << 0, FP_TRAP_INVALID_OPERATION = 1 << 0,
FP_TRAP_OVERFLOW = 1 << 1, FP_TRAP_OVERFLOW = 1 << 1,
FP_TRAP_UNDERFLOW = 1 << 2, FP_TRAP_UNDERFLOW = 1 << 2,
@ -114,22 +103,19 @@ enum
/* What Factor calls 'f' */ /* What Factor calls 'f' */
static const cell false_object = F_TYPE; static const cell false_object = F_TYPE;
inline static bool immediate_p(cell obj) inline static bool immediate_p(cell obj) {
{
/* We assume that fixnums have tag 0 and false_object has tag 1 */ /* We assume that fixnums have tag 0 and false_object has tag 1 */
return TAG(obj) <= F_TYPE; return TAG(obj) <= F_TYPE;
} }
inline static fixnum untag_fixnum(cell tagged) inline static fixnum untag_fixnum(cell tagged) {
{
#ifdef FACTOR_DEBUG #ifdef FACTOR_DEBUG
FACTOR_ASSERT(TAG(tagged) == FIXNUM_TYPE); FACTOR_ASSERT(TAG(tagged) == FIXNUM_TYPE);
#endif #endif
return ((fixnum)tagged) >> TAG_BITS; return ((fixnum) tagged) >> TAG_BITS;
} }
inline static cell tag_fixnum(fixnum untagged) inline static cell tag_fixnum(fixnum untagged) {
{
return (untagged << TAG_BITS) | FIXNUM_TYPE; return (untagged << TAG_BITS) | FIXNUM_TYPE;
} }
@ -140,56 +126,34 @@ struct object {
cell header; cell header;
cell size() const; cell size() const;
template<typename Fixup> cell size(Fixup fixup) const; template <typename Fixup> cell size(Fixup fixup) const;
cell binary_payload_start() const; cell binary_payload_start() const;
template<typename Fixup> cell binary_payload_start(Fixup fixup) const; template <typename Fixup> cell binary_payload_start(Fixup fixup) const;
cell *slots() const { return (cell *)this; } cell* slots() const { return (cell*)this; }
template<typename Iterator> void each_slot(Iterator &iter); template <typename Iterator> void each_slot(Iterator& iter);
/* Only valid for objects in tenured space; must cast to free_heap_block /* Only valid for objects in tenured space; must cast to free_heap_block
to do anything with it if its free */ to do anything with it if its free */
bool free_p() const bool free_p() const { return (header & 1) == 1; }
{
return (header & 1) == 1;
}
cell type() const cell type() const { return (header >> 2) & TAG_MASK; }
{
return (header >> 2) & TAG_MASK;
}
void initialize(cell type) void initialize(cell type) { header = type << 2; }
{
header = type << 2;
}
cell hashcode() const cell hashcode() const { return (header >> 6); }
{
return (header >> 6);
}
void set_hashcode(cell hashcode) void set_hashcode(cell hashcode) {
{
header = (header & 0x3f) | (hashcode << 6); header = (header & 0x3f) | (hashcode << 6);
} }
bool forwarding_pointer_p() const bool forwarding_pointer_p() const { return (header & 2) == 2; }
{
return (header & 2) == 2;
}
object *forwarding_pointer() const object* forwarding_pointer() const { return (object*)UNTAG(header); }
{
return (object *)UNTAG(header);
}
void forward_to(object *pointer) void forward_to(object* pointer) { header = ((cell) pointer | 2); }
{
header = ((cell)pointer | 2);
}
}; };
/* Assembly code makes assumptions about the layout of this struct */ /* Assembly code makes assumptions about the layout of this struct */
@ -199,11 +163,11 @@ struct array : public object {
/* tagged */ /* tagged */
cell capacity; cell capacity;
cell *data() const { return (cell *)(this + 1); } cell* data() const { return (cell*)(this + 1); }
}; };
/* These are really just arrays, but certain elements have special /* These are really just arrays, but certain elements have special
significance */ significance */
struct tuple_layout : public array { struct tuple_layout : public array {
NO_TYPE_CHECK; NO_TYPE_CHECK;
/* tagged */ /* tagged */
@ -220,7 +184,7 @@ struct bignum : public object {
/* tagged */ /* tagged */
cell capacity; cell capacity;
cell *data() const { return (cell *)(this + 1); } cell* data() const { return (cell*)(this + 1); }
}; };
struct byte_array : public object { struct byte_array : public object {
@ -234,7 +198,9 @@ struct byte_array : public object {
cell padding1; cell padding1;
#endif #endif
template<typename Scalar> Scalar *data() const { return (Scalar *)(this + 1); } template <typename Scalar> Scalar* data() const {
return (Scalar*)(this + 1);
}
}; };
/* Assembly code makes assumptions about the layout of this struct */ /* Assembly code makes assumptions about the layout of this struct */
@ -247,7 +213,7 @@ struct string : public object {
/* tagged */ /* tagged */
cell hashcode; cell hashcode;
u8 *data() const { return (u8 *)(this + 1); } u8* data() const { return (u8*)(this + 1); }
}; };
struct code_block; struct code_block;
@ -269,18 +235,20 @@ struct word : public object {
cell def; cell def;
/* TAGGED property assoc for library code */ /* TAGGED property assoc for library code */
cell props; cell props;
/* TAGGED alternative entry point for direct non-tail calls. Used for inline caching */ /* TAGGED alternative entry point for direct non-tail calls. Used for inline
* caching */
cell pic_def; cell pic_def;
/* TAGGED alternative entry point for direct tail calls. Used for inline caching */ /* TAGGED alternative entry point for direct tail calls. Used for inline
* caching */
cell pic_tail_def; cell pic_tail_def;
/* TAGGED machine code for sub-primitive */ /* TAGGED machine code for sub-primitive */
cell subprimitive; cell subprimitive;
/* UNTAGGED entry point: jump here to execute word */ /* UNTAGGED entry point: jump here to execute word */
void *entry_point; void* entry_point;
/* UNTAGGED compiled code block */ /* UNTAGGED compiled code block */
/* defined in code_blocks.hpp */ /* defined in code_blocks.hpp */
code_block *code() const; code_block* code() const;
}; };
/* Assembly code makes assumptions about the layout of this struct */ /* Assembly code makes assumptions about the layout of this struct */
@ -314,10 +282,10 @@ struct quotation : public object {
/* tagged */ /* tagged */
cell cache_counter; cell cache_counter;
/* UNTAGGED entry point; jump here to call quotation */ /* UNTAGGED entry point; jump here to call quotation */
void *entry_point; void* entry_point;
/* defined in code_blocks.hpp */ /* defined in code_blocks.hpp */
code_block *code() const; code_block* code() const;
}; };
/* Assembly code makes assumptions about the layout of this struct */ /* Assembly code makes assumptions about the layout of this struct */
@ -332,9 +300,8 @@ struct alien : public object {
/* untagged */ /* untagged */
cell address; cell address;
void update_address() void update_address() {
{ if (base == false_object)
if(base == false_object)
address = displacement; address = displacement;
else else
address = UNTAG(base) + sizeof(byte_array) + displacement; address = UNTAG(base) + sizeof(byte_array) + displacement;
@ -346,7 +313,7 @@ struct dll : public object {
/* tagged byte array holding a C string */ /* tagged byte array holding a C string */
cell path; cell path;
/* OS-specific handle */ /* OS-specific handle */
void *handle; void* handle;
}; };
struct callstack : public object { struct callstack : public object {
@ -354,13 +321,14 @@ struct callstack : public object {
/* tagged */ /* tagged */
cell length; cell length;
void *frame_top_at(cell offset) const void* frame_top_at(cell offset) const {
{ return (void*)((char*)(this + 1) + offset);
return (void *)((char *)(this + 1) + offset);
} }
void *top() const { return (void *)(this + 1); } void* top() const { return (void*)(this + 1); }
void *bottom() const { return (void *)((cell)(this + 1) + untag_fixnum(length)); } void* bottom() const {
return (void*)((cell)(this + 1) + untag_fixnum(length));
}
}; };
struct tuple : public object { struct tuple : public object {
@ -368,15 +336,15 @@ struct tuple : public object {
/* tagged layout */ /* tagged layout */
cell layout; cell layout;
cell *data() const { return (cell *)(this + 1); } cell* data() const { return (cell*)(this + 1); }
}; };
struct data_root_range { struct data_root_range {
cell *start; cell* start;
cell len; cell len;
explicit data_root_range(cell *start_, cell len_) : explicit data_root_range(cell* start_, cell len_)
start(start_), len(len_) {} : start(start_), len(len_) {}
}; };
} }