factor/vm/layouts.hpp

322 lines
6.8 KiB
C++
Raw Normal View History

2009-05-04 02:46:13 -04:00
namespace factor
{
2009-05-02 05:04:19 -04:00
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned long long u64;
typedef signed char s8;
typedef signed short s16;
typedef signed int s32;
typedef signed long long s64;
#ifdef _WIN64
2009-05-04 05:50:24 -04:00
typedef long long fixnum;
typedef unsigned long long cell;
2009-05-02 05:04:19 -04:00
#else
2009-05-04 05:50:24 -04:00
typedef long fixnum;
typedef unsigned long cell;
2009-05-02 05:04:19 -04:00
#endif
2009-05-04 05:50:24 -04:00
inline static cell align(cell a, cell b)
{
return (a + (b-1)) & ~(b-1);
}
#define align8(a) align(a,8)
#define align_page(a) align(a,getpagesize())
2009-05-04 05:50:24 -04:00
#define WORD_SIZE (signed)(sizeof(cell)*8)
2009-05-02 05:04:19 -04:00
#define TAG_MASK 7
#define TAG_BITS 3
2009-05-04 05:50:24 -04:00
#define TAG(x) ((cell)(x) & TAG_MASK)
#define UNTAG(x) ((cell)(x) & ~TAG_MASK)
#define RETAG(x,tag) (UNTAG(x) | (tag))
2009-05-02 05:04:19 -04:00
/*** Tags ***/
#define FIXNUM_TYPE 0
#define BIGNUM_TYPE 1
#define ARRAY_TYPE 2
#define FLOAT_TYPE 3
#define QUOTATION_TYPE 4
#define F_TYPE 5
#define OBJECT_TYPE 6
#define TUPLE_TYPE 7
/* Canonical F object */
#define F F_TYPE
#define HEADER_TYPE 8 /* anything less than this is a tag */
#define GC_COLLECTED 5 /* can be anything other than FIXNUM_TYPE */
/*** Header types ***/
#define WRAPPER_TYPE 8
#define BYTE_ARRAY_TYPE 9
#define CALLSTACK_TYPE 10
#define STRING_TYPE 11
#define WORD_TYPE 12
#define DLL_TYPE 13
#define ALIEN_TYPE 14
#define TYPE_COUNT 15
2009-05-04 05:50:24 -04:00
/* Not a real type, but code_block's type field can be set to this */
2009-05-02 05:04:19 -04:00
#define PIC_TYPE 69
2009-05-04 05:50:24 -04:00
inline static bool immediate_p(cell obj)
2009-05-02 05:04:19 -04:00
{
return (obj == F || TAG(obj) == FIXNUM_TYPE);
}
2009-05-04 05:50:24 -04:00
inline static fixnum untag_fixnum(cell tagged)
2009-05-02 05:04:19 -04:00
{
#ifdef FACTOR_DEBUG
assert(TAG(tagged) == FIXNUM_TYPE);
#endif
2009-05-04 05:50:24 -04:00
return ((fixnum)tagged) >> TAG_BITS;
2009-05-02 05:04:19 -04:00
}
2009-05-04 05:50:24 -04:00
inline static cell tag_fixnum(fixnum untagged)
2009-05-02 05:04:19 -04:00
{
return RETAG(untagged << TAG_BITS,FIXNUM_TYPE);
}
2009-05-04 05:50:24 -04:00
inline static cell tag_for(cell type)
{
return type < HEADER_TYPE ? type : OBJECT_TYPE;
}
2009-05-04 05:50:24 -04:00
class object;
2009-05-04 05:50:24 -04:00
struct header {
cell value;
/* Default ctor to make gcc 3.x happy */
header() { abort(); }
2009-05-04 05:50:24 -04:00
header(cell value_) : value(value_ << TAG_BITS) {}
void check_header() {
#ifdef FACTOR_DEBUG
2009-05-04 05:50:24 -04:00
assert(TAG(value) == FIXNUM_TYPE && untag_fixnum(value) < TYPE_COUNT);
#endif
}
2009-05-04 05:50:24 -04:00
cell hi_tag() {
check_header();
2009-05-04 05:50:24 -04:00
return value >> TAG_BITS;
}
bool forwarding_pointer_p() {
2009-05-04 05:50:24 -04:00
return TAG(value) == GC_COLLECTED;
}
2009-05-04 05:50:24 -04:00
object *forwarding_pointer() {
return (object *)UNTAG(value);
}
2009-05-04 05:50:24 -04:00
void forward_to(object *pointer) {
value = RETAG(pointer,GC_COLLECTED);
}
};
2009-05-04 05:50:24 -04:00
#define NO_TYPE_CHECK static const cell type_number = TYPE_COUNT
2009-05-04 05:50:24 -04:00
struct object {
NO_TYPE_CHECK;
2009-05-04 05:50:24 -04:00
header h;
cell *slots() { return (cell *)this; }
2009-05-02 05:04:19 -04:00
};
/* Assembly code makes assumptions about the layout of this struct */
2009-05-04 05:50:24 -04:00
struct array : public object {
static const cell type_number = ARRAY_TYPE;
static const cell element_size = sizeof(cell);
2009-05-02 05:04:19 -04:00
/* tagged */
2009-05-04 05:50:24 -04:00
cell capacity;
2009-05-04 05:50:24 -04:00
cell *data() { return (cell *)(this + 1); }
2009-05-02 05:04:19 -04:00
};
/* These are really just arrays, but certain elements have special
significance */
2009-05-04 05:50:24 -04:00
struct tuple_layout : public array {
NO_TYPE_CHECK;
2009-05-02 05:04:19 -04:00
/* tagged */
2009-05-04 05:50:24 -04:00
cell klass;
2009-05-02 05:04:19 -04:00
/* tagged fixnum */
2009-05-04 05:50:24 -04:00
cell size;
2009-05-02 05:04:19 -04:00
/* tagged fixnum */
2009-05-04 05:50:24 -04:00
cell echelon;
2009-05-02 05:04:19 -04:00
};
2009-05-04 05:50:24 -04:00
struct bignum : public object {
static const cell type_number = BIGNUM_TYPE;
static const cell element_size = sizeof(cell);
2009-05-02 05:04:19 -04:00
/* tagged */
2009-05-04 05:50:24 -04:00
cell capacity;
2009-05-04 05:50:24 -04:00
cell *data() { return (cell *)(this + 1); }
2009-05-02 05:04:19 -04:00
};
2009-05-04 05:50:24 -04:00
struct byte_array : public object {
static const cell type_number = BYTE_ARRAY_TYPE;
static const cell element_size = 1;
2009-05-02 05:04:19 -04:00
/* tagged */
2009-05-04 05:50:24 -04:00
cell capacity;
template<typename T> T *data() { return (T *)(this + 1); }
2009-05-02 05:04:19 -04:00
};
/* Assembly code makes assumptions about the layout of this struct */
2009-05-04 05:50:24 -04:00
struct string : public object {
static const cell type_number = STRING_TYPE;
2009-05-02 05:04:19 -04:00
/* tagged num of chars */
2009-05-04 05:50:24 -04:00
cell length;
2009-05-02 05:04:19 -04:00
/* tagged */
2009-05-04 05:50:24 -04:00
cell aux;
2009-05-02 05:04:19 -04:00
/* tagged */
2009-05-04 05:50:24 -04:00
cell hashcode;
u8 *data() { return (u8 *)(this + 1); }
2009-05-02 05:04:19 -04:00
};
/* The compiled code heap is structured into blocks. */
2009-05-04 05:50:24 -04:00
enum block_status
2009-05-02 05:04:19 -04:00
{
B_FREE,
B_ALLOCATED,
B_MARKED
2009-05-04 05:50:24 -04:00
};
2009-05-02 05:04:19 -04:00
2009-05-04 05:50:24 -04:00
struct heap_block
2009-05-02 05:04:19 -04:00
{
unsigned char status; /* free or allocated? */
unsigned char type; /* this is WORD_TYPE or QUOTATION_TYPE */
unsigned char last_scan; /* the youngest generation in which this block's literals may live */
unsigned char needs_fixup; /* is this a new block that needs full fixup? */
2009-05-02 05:04:19 -04:00
/* In bytes, includes this header */
2009-05-04 05:50:24 -04:00
cell size;
2009-05-02 05:04:19 -04:00
};
struct free_heap_block : public heap_block
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
free_heap_block *next_free;
2009-05-02 05:04:19 -04:00
};
struct code_block : public heap_block
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
cell literals; /* # bytes */
cell relocation; /* tagged pointer to byte-array or f */
void *xt() { return (void *)(this + 1); }
2009-05-02 05:04:19 -04:00
};
/* Assembly code makes assumptions about the layout of this struct */
2009-05-04 05:50:24 -04:00
struct word : public object {
static const cell type_number = WORD_TYPE;
2009-05-02 05:04:19 -04:00
/* TAGGED hashcode */
2009-05-04 05:50:24 -04:00
cell hashcode;
2009-05-02 05:04:19 -04:00
/* TAGGED word name */
2009-05-04 05:50:24 -04:00
cell name;
2009-05-02 05:04:19 -04:00
/* TAGGED word vocabulary */
2009-05-04 05:50:24 -04:00
cell vocabulary;
2009-05-02 05:04:19 -04:00
/* TAGGED definition */
2009-05-04 05:50:24 -04:00
cell def;
2009-05-02 05:04:19 -04:00
/* TAGGED property assoc for library code */
2009-05-04 05:50:24 -04:00
cell props;
2009-05-02 05:04:19 -04:00
/* TAGGED alternative entry point for direct non-tail calls. Used for inline caching */
cell pic_def;
/* TAGGED alternative entry point for direct tail calls. Used for inline caching */
cell pic_tail_def;
2009-05-02 05:04:19 -04:00
/* TAGGED call count for profiling */
2009-05-04 05:50:24 -04:00
cell counter;
2009-05-02 05:04:19 -04:00
/* TAGGED machine code for sub-primitive */
2009-05-04 05:50:24 -04:00
cell subprimitive;
2009-05-02 05:04:19 -04:00
/* UNTAGGED execution token: jump here to execute word */
2009-05-04 05:50:24 -04:00
void *xt;
2009-05-02 05:04:19 -04:00
/* UNTAGGED compiled code block */
2009-05-04 05:50:24 -04:00
code_block *code;
2009-05-02 05:04:19 -04:00
/* UNTAGGED profiler stub */
2009-05-04 05:50:24 -04:00
code_block *profiling;
2009-05-02 05:04:19 -04:00
};
/* Assembly code makes assumptions about the layout of this struct */
2009-05-04 05:50:24 -04:00
struct wrapper : public object {
static const cell type_number = WRAPPER_TYPE;
cell object;
2009-05-02 05:04:19 -04:00
};
/* Assembly code makes assumptions about the layout of this struct */
2009-05-04 05:50:24 -04:00
struct boxed_float : object {
static const cell type_number = FLOAT_TYPE;
#ifndef FACTOR_64
2009-05-04 05:50:24 -04:00
cell padding;
#endif
2009-05-02 05:04:19 -04:00
double n;
};
/* Assembly code makes assumptions about the layout of this struct */
2009-05-04 05:50:24 -04:00
struct quotation : public object {
static const cell type_number = QUOTATION_TYPE;
2009-05-02 05:04:19 -04:00
/* tagged */
2009-05-04 05:50:24 -04:00
cell array;
2009-05-02 05:04:19 -04:00
/* tagged */
2009-05-04 05:50:24 -04:00
cell compiledp;
2009-05-02 05:04:19 -04:00
/* tagged */
2009-05-04 05:50:24 -04:00
cell cached_effect;
2009-05-02 05:04:19 -04:00
/* tagged */
2009-05-04 05:50:24 -04:00
cell cache_counter;
2009-05-02 05:04:19 -04:00
/* UNTAGGED */
2009-05-04 05:50:24 -04:00
void *xt;
2009-05-02 05:04:19 -04:00
/* UNTAGGED compiled code block */
2009-05-04 05:50:24 -04:00
code_block *code;
2009-05-02 05:04:19 -04:00
};
/* Assembly code makes assumptions about the layout of this struct */
2009-05-04 05:50:24 -04:00
struct alien : public object {
static const cell type_number = ALIEN_TYPE;
2009-05-02 05:04:19 -04:00
/* tagged */
2009-05-04 05:50:24 -04:00
cell alien;
2009-05-02 05:04:19 -04:00
/* tagged */
2009-05-04 05:50:24 -04:00
cell expired;
2009-05-02 05:04:19 -04:00
/* untagged */
2009-05-04 05:50:24 -04:00
cell displacement;
2009-05-02 05:04:19 -04:00
};
2009-05-04 05:50:24 -04:00
struct dll : public object {
static const cell type_number = DLL_TYPE;
2009-05-02 05:04:19 -04:00
/* tagged byte array holding a C string */
2009-05-04 05:50:24 -04:00
cell path;
2009-05-02 05:04:19 -04:00
/* OS-specific handle */
void *dll;
};
2009-05-04 05:50:24 -04:00
struct callstack : public object {
static const cell type_number = CALLSTACK_TYPE;
2009-05-02 05:04:19 -04:00
/* tagged */
2009-05-04 05:50:24 -04:00
cell length;
2009-05-02 05:04:19 -04:00
};
2009-05-04 05:50:24 -04:00
struct stack_frame
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
void *xt;
2009-05-02 05:04:19 -04:00
/* Frame size in bytes */
2009-05-04 05:50:24 -04:00
cell size;
2009-05-02 05:04:19 -04:00
};
2009-05-04 05:50:24 -04:00
struct tuple : public object {
static const cell type_number = TUPLE_TYPE;
2009-05-02 05:04:19 -04:00
/* tagged layout */
2009-05-04 05:50:24 -04:00
cell layout;
2009-05-04 05:50:24 -04:00
cell *data() { return (cell *)(this + 1); }
2009-05-02 05:04:19 -04:00
};
2009-05-04 02:46:13 -04:00
}