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,327 +49,302 @@ 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:
{ return "fixnum";
case FIXNUM_TYPE: case F_TYPE:
return "fixnum"; return "f";
case F_TYPE: case ARRAY_TYPE:
return "f"; return "array";
case ARRAY_TYPE: case FLOAT_TYPE:
return "array"; return "float";
case FLOAT_TYPE: case QUOTATION_TYPE:
return "float"; return "quotation";
case QUOTATION_TYPE: case BIGNUM_TYPE:
return "quotation"; return "bignum";
case BIGNUM_TYPE: case ALIEN_TYPE:
return "bignum"; return "alien";
case ALIEN_TYPE: case TUPLE_TYPE:
return "alien"; return "tuple";
case TUPLE_TYPE: case WRAPPER_TYPE:
return "tuple"; return "wrapper";
case WRAPPER_TYPE: case BYTE_ARRAY_TYPE:
return "wrapper"; return "byte-array";
case BYTE_ARRAY_TYPE: case CALLSTACK_TYPE:
return "byte-array"; return "callstack";
case CALLSTACK_TYPE: case STRING_TYPE:
return "callstack"; return "string";
case STRING_TYPE: case WORD_TYPE:
return "string"; return "word";
case WORD_TYPE: case DLL_TYPE:
return "word"; return "dll";
case DLL_TYPE: default:
return "dll"; FACTOR_ASSERT(false);
default: return "";
FACTOR_ASSERT(false); }
return "";
}
} }
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, FP_TRAP_ZERO_DIVIDE = 1 << 3,
FP_TRAP_ZERO_DIVIDE = 1 << 3, FP_TRAP_INEXACT = 1 << 4,
FP_TRAP_INEXACT = 1 << 4,
}; };
/* 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;
} }
#define NO_TYPE_CHECK static const cell type_number = TYPE_COUNT #define NO_TYPE_CHECK static const cell type_number = TYPE_COUNT
struct object { struct object {
NO_TYPE_CHECK; NO_TYPE_CHECK;
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 */
struct array : public object { struct array : public object {
static const cell type_number = ARRAY_TYPE; static const cell type_number = ARRAY_TYPE;
static const cell element_size = sizeof(cell); static const cell element_size = sizeof(cell);
/* 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 */
cell klass; cell klass;
/* tagged fixnum */ /* tagged fixnum */
cell size; cell size;
/* tagged fixnum */ /* tagged fixnum */
cell echelon; cell echelon;
}; };
struct bignum : public object { struct bignum : public object {
static const cell type_number = BIGNUM_TYPE; static const cell type_number = BIGNUM_TYPE;
static const cell element_size = sizeof(cell); static const cell element_size = sizeof(cell);
/* 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 {
static const cell type_number = BYTE_ARRAY_TYPE; static const cell type_number = BYTE_ARRAY_TYPE;
static const cell element_size = 1; static const cell element_size = 1;
/* tagged */ /* tagged */
cell capacity; cell capacity;
#ifndef FACTOR_64 #ifndef FACTOR_64
cell padding0; cell padding0;
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 */
struct string : public object { struct string : public object {
static const cell type_number = STRING_TYPE; static const cell type_number = STRING_TYPE;
/* tagged num of chars */ /* tagged num of chars */
cell length; cell length;
/* tagged */ /* tagged */
cell aux; cell aux;
/* 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;
/* Assembly code makes assumptions about the layout of this struct: /* Assembly code makes assumptions about the layout of this struct:
basis/bootstrap/images/images.factor basis/bootstrap/images/images.factor
basis/compiler/constants/constants.factor basis/compiler/constants/constants.factor
core/bootstrap/primitives.factor core/bootstrap/primitives.factor
*/ */
struct word : public object { struct word : public object {
static const cell type_number = WORD_TYPE; static const cell type_number = WORD_TYPE;
/* TAGGED hashcode */ /* TAGGED hashcode */
cell hashcode; cell hashcode;
/* TAGGED word name */ /* TAGGED word name */
cell name; cell name;
/* TAGGED word vocabulary */ /* TAGGED word vocabulary */
cell vocabulary; cell vocabulary;
/* TAGGED definition */ /* TAGGED definition */
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
cell pic_def; * caching */
/* TAGGED alternative entry point for direct tail calls. Used for inline caching */ cell pic_def;
cell pic_tail_def; /* TAGGED alternative entry point for direct tail calls. Used for inline
/* TAGGED machine code for sub-primitive */ * caching */
cell subprimitive; cell pic_tail_def;
/* UNTAGGED entry point: jump here to execute word */ /* TAGGED machine code for sub-primitive */
void *entry_point; cell subprimitive;
/* UNTAGGED compiled code block */ /* UNTAGGED entry point: jump here to execute word */
void* entry_point;
/* 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 */
struct wrapper : public object { struct wrapper : public object {
static const cell type_number = WRAPPER_TYPE; static const cell type_number = WRAPPER_TYPE;
cell object; cell object;
}; };
/* Assembly code makes assumptions about the layout of this struct */ /* Assembly code makes assumptions about the layout of this struct */
struct boxed_float : object { struct boxed_float : object {
static const cell type_number = FLOAT_TYPE; static const cell type_number = FLOAT_TYPE;
#ifndef FACTOR_64 #ifndef FACTOR_64
cell padding; cell padding;
#endif #endif
double n; double n;
}; };
/* Assembly code makes assumptions about the layout of this struct: /* Assembly code makes assumptions about the layout of this struct:
basis/bootstrap/images/images.factor basis/bootstrap/images/images.factor
basis/compiler/constants/constants.factor basis/compiler/constants/constants.factor
core/bootstrap/primitives.factor core/bootstrap/primitives.factor
*/ */
struct quotation : public object { struct quotation : public object {
static const cell type_number = QUOTATION_TYPE; static const cell type_number = QUOTATION_TYPE;
/* tagged */ /* tagged */
cell array; cell array;
/* tagged */ /* tagged */
cell cached_effect; cell cached_effect;
/* 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 */
struct alien : public object { struct alien : public object {
static const cell type_number = ALIEN_TYPE; static const cell type_number = ALIEN_TYPE;
/* tagged */ /* tagged */
cell base; cell base;
/* tagged */ /* tagged */
cell expired; cell expired;
/* untagged */ /* untagged */
cell displacement; cell displacement;
/* 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; }
}
}; };
struct dll : public object { struct dll : public object {
static const cell type_number = DLL_TYPE; static const cell type_number = DLL_TYPE;
/* 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 {
static const cell type_number = CALLSTACK_TYPE; static const cell type_number = CALLSTACK_TYPE;
/* 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 {
static const cell type_number = TUPLE_TYPE; static const cell type_number = TUPLE_TYPE;
/* 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_) {}
}; };
} }