factor/vm/layouts.hpp

338 lines
7.1 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);
}
2009-10-20 13:45:00 -04:00
static const cell data_alignment = 16;
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 15
#define TAG_BITS 4
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
2009-11-02 21:21:21 -05:00
#define F_TYPE 1
2009-05-02 05:04:19 -04:00
#define ARRAY_TYPE 2
#define FLOAT_TYPE 3
#define QUOTATION_TYPE 4
2009-11-02 21:21:21 -05:00
#define BIGNUM_TYPE 5
#define ALIEN_TYPE 6
2009-05-02 05:04:19 -04:00
#define TUPLE_TYPE 7
#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 TYPE_COUNT 14
enum code_block_type
{
code_block_unoptimized,
code_block_optimized,
code_block_profiling,
code_block_pic
};
2009-05-02 05:04:19 -04:00
/* Constants used when floating-point trap exceptions are thrown */
enum
{
2009-09-14 04:09:03 -04:00
FP_TRAP_INVALID_OPERATION = 1 << 0,
FP_TRAP_OVERFLOW = 1 << 1,
FP_TRAP_UNDERFLOW = 1 << 2,
FP_TRAP_ZERO_DIVIDE = 1 << 3,
FP_TRAP_INEXACT = 1 << 4,
};
/* What Factor calls 'f' */
static const cell false_object = F_TYPE;
2009-05-04 05:50:24 -04:00
inline static bool immediate_p(cell obj)
2009-05-02 05:04:19 -04:00
{
2009-11-02 21:21:21 -05:00
/* We assume that fixnums have tag 0 and false_object has tag 1 */
return TAG(obj) <= F_TYPE;
2009-05-02 05:04:19 -04:00
}
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-11-10 22:06:36 -05:00
#define NO_TYPE_CHECK static const cell type_number = TYPE_COUNT
2009-11-10 22:06:36 -05:00
struct object {
NO_TYPE_CHECK;
cell header;
2009-11-10 22:06:36 -05:00
cell size() const;
cell binary_payload_start() const;
cell *slots() const { return (cell *)this; }
template<typename Iterator> void each_slot(Iterator &iter);
2009-11-10 22:06:36 -05:00
/* Only valid for objects in tenured space; must cast to free_heap_block
to do anything with it if its free */
bool free_p() const
{
2009-11-10 22:06:36 -05:00
return (header & 1) == 1;
}
2009-11-10 22:06:36 -05:00
cell type() const
{
2009-11-10 22:06:36 -05:00
return (header >> 2) & TAG_MASK;
}
2009-11-10 22:06:36 -05:00
void initialize(cell type)
{
2009-11-10 22:06:36 -05:00
header = type << 2;
}
2009-11-10 22:06:36 -05:00
cell hashcode() const
{
2009-11-10 22:06:36 -05:00
return (header >> 6);
}
2009-11-10 22:06:36 -05:00
void set_hashcode(cell hashcode)
{
2009-11-10 22:06:36 -05:00
header = (header & 0x3f) | (hashcode << 6);
}
2009-11-10 22:06:36 -05:00
bool forwarding_pointer_p() const
{
return (header & 2) == 2;
}
2009-11-10 22:06:36 -05:00
object *forwarding_pointer() const
{
return (object *)UNTAG(header);
}
2009-11-10 22:06:36 -05:00
void forward_to(object *pointer)
{
2009-11-10 22:06:36 -05:00
header = ((cell)pointer | 2);
}
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;
cell *data() const { 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;
cell *data() const { 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;
2009-10-20 13:45:00 -04:00
#ifndef FACTOR_64
cell padding0;
cell padding1;
#endif
template<typename Scalar> Scalar *data() const { return (Scalar *)(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() const { return (u8 *)(this + 1); }
2009-05-02 05:04:19 -04:00
};
struct code_block;
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;
/* UNTAGGED entry point: jump here to execute word */
void *entry_point;
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 cached_effect;
2009-05-02 05:04:19 -04:00
/* tagged */
2009-05-04 05:50:24 -04:00
cell cache_counter;
/* UNTAGGED entry point; jump here to call quotation */
void *entry_point;
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-10-09 04:20:50 -04:00
cell base;
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;
/* untagged */
cell address;
void update_address()
{
if(base == false_object)
address = displacement;
else
address = UNTAG(base) + sizeof(byte_array) + 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 *handle;
2009-05-02 05:04:19 -04:00
};
struct stack_frame {
/* Updated by procedure prologue with procedure start address */
void *entry_point;
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
};
struct callstack : public object {
static const cell type_number = CALLSTACK_TYPE;
/* tagged */
cell length;
stack_frame *frame_at(cell offset) const
{
return (stack_frame *)((char *)(this + 1) + offset);
}
stack_frame *top() const { return (stack_frame *)(this + 1); }
stack_frame *bottom() const { return (stack_frame *)((cell)(this + 1) + untag_fixnum(length)); }
};
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;
cell *data() const { return (cell *)(this + 1); }
2009-05-02 05:04:19 -04:00
};
2009-05-04 02:46:13 -04:00
struct data_root_range {
cell *start;
cell len;
explicit data_root_range(cell *start_, cell len_) :
start(start_), len(len_) {}
};
2009-05-04 02:46:13 -04:00
}