4 typedef unsigned char u8;
5 typedef unsigned short u16;
6 typedef unsigned int u32;
7 typedef unsigned long long u64;
8 typedef signed char s8;
9 typedef signed short s16;
10 typedef signed int s32;
11 typedef signed long long s64;
14 typedef long long fixnum;
15 typedef unsigned long long cell;
18 typedef unsigned long cell;
21 inline static cell align(cell a, cell b)
23 return (a + (b-1)) & ~(b-1);
26 static const cell data_alignment = 16;
28 #define WORD_SIZE (signed)(sizeof(cell)*8)
32 #define TAG(x) ((cell)(x) & TAG_MASK)
33 #define UNTAG(x) ((cell)(x) & ~TAG_MASK)
34 #define RETAG(x,tag) (UNTAG(x) | (tag))
41 #define QUOTATION_TYPE 4
46 #define HEADER_TYPE 8 /* anything less than this is a tag */
48 #define GC_COLLECTED 5 /* can be anything other than FIXNUM_TYPE */
50 /*** Header types ***/
51 #define WRAPPER_TYPE 8
52 #define BYTE_ARRAY_TYPE 9
53 #define CALLSTACK_TYPE 10
54 #define STRING_TYPE 11
61 /* Not real types, but code_block's type can be set to this */
65 code_block_unoptimized,
71 /* Constants used when floating-point trap exceptions are thrown */
74 FP_TRAP_INVALID_OPERATION = 1 << 0,
75 FP_TRAP_OVERFLOW = 1 << 1,
76 FP_TRAP_UNDERFLOW = 1 << 2,
77 FP_TRAP_ZERO_DIVIDE = 1 << 3,
78 FP_TRAP_INEXACT = 1 << 4,
81 /* What Factor calls 'f' */
82 static const cell false_object = F_TYPE;
84 inline static bool immediate_p(cell obj)
86 return (obj == false_object || TAG(obj) == FIXNUM_TYPE);
89 inline static fixnum untag_fixnum(cell tagged)
92 assert(TAG(tagged) == FIXNUM_TYPE);
94 return ((fixnum)tagged) >> TAG_BITS;
97 inline static cell tag_fixnum(fixnum untagged)
99 return RETAG(untagged << TAG_BITS,FIXNUM_TYPE);
102 inline static cell tag_for(cell type)
104 return type < HEADER_TYPE ? type : OBJECT_TYPE;
112 /* Default ctor to make gcc 3.x happy */
113 explicit header() { abort(); }
115 explicit header(cell value_) : value(value_ << TAG_BITS) {}
117 void check_header() {
119 assert(TAG(value) == FIXNUM_TYPE && untag_fixnum(value) < TYPE_COUNT);
125 return value >> TAG_BITS;
128 bool forwarding_pointer_p() {
129 return TAG(value) == GC_COLLECTED;
132 object *forwarding_pointer() {
133 return (object *)UNTAG(value);
136 void forward_to(object *pointer) {
137 value = RETAG(pointer,GC_COLLECTED);
141 #define NO_TYPE_CHECK static const cell type_number = TYPE_COUNT
146 cell *slots() { return (cell *)this; }
149 /* Assembly code makes assumptions about the layout of this struct */
150 struct array : public object {
151 static const cell type_number = ARRAY_TYPE;
152 static const cell element_size = sizeof(cell);
156 cell *data() { return (cell *)(this + 1); }
159 /* These are really just arrays, but certain elements have special
161 struct tuple_layout : public array {
171 struct bignum : public object {
172 static const cell type_number = BIGNUM_TYPE;
173 static const cell element_size = sizeof(cell);
177 cell *data() { return (cell *)(this + 1); }
180 struct byte_array : public object {
181 static const cell type_number = BYTE_ARRAY_TYPE;
182 static const cell element_size = 1;
191 template<typename Scalar> Scalar *data() { return (Scalar *)(this + 1); }
194 /* Assembly code makes assumptions about the layout of this struct */
195 struct string : public object {
196 static const cell type_number = STRING_TYPE;
197 /* tagged num of chars */
204 u8 *data() { return (u8 *)(this + 1); }
207 /* The compiled code heap is structured into blocks. */
214 return header & 1 == 1;
232 void set_size(cell size)
234 header = (header & 0x7) | (size << 3);
238 struct free_heap_block : public heap_block
240 free_heap_block *next_free;
243 struct code_block : public heap_block
245 cell owner; /* tagged pointer to word, quotation or f */
246 cell literals; /* tagged pointer to array or f */
247 cell relocation; /* tagged pointer to byte-array or f */
251 return (void *)(this + 1);
254 code_block_type type()
256 return (code_block_type)((header >> 1) & 0x3);
259 void set_type(code_block_type type)
261 header = ((header & ~0x7) | (type << 1));
266 return type() == code_block_pic;
271 return type() == code_block_optimized;
275 /* Assembly code makes assumptions about the layout of this struct */
276 struct word : public object {
277 static const cell type_number = WORD_TYPE;
278 /* TAGGED hashcode */
280 /* TAGGED word name */
282 /* TAGGED word vocabulary */
284 /* TAGGED definition */
286 /* TAGGED property assoc for library code */
288 /* TAGGED alternative entry point for direct non-tail calls. Used for inline caching */
290 /* TAGGED alternative entry point for direct tail calls. Used for inline caching */
292 /* TAGGED call count for profiling */
294 /* TAGGED machine code for sub-primitive */
296 /* UNTAGGED execution token: jump here to execute word */
298 /* UNTAGGED compiled code block */
300 /* UNTAGGED profiler stub */
301 code_block *profiling;
304 /* Assembly code makes assumptions about the layout of this struct */
305 struct wrapper : public object {
306 static const cell type_number = WRAPPER_TYPE;
310 /* Assembly code makes assumptions about the layout of this struct */
311 struct boxed_float : object {
312 static const cell type_number = FLOAT_TYPE;
321 /* Assembly code makes assumptions about the layout of this struct */
322 struct quotation : public object {
323 static const cell type_number = QUOTATION_TYPE;
332 /* UNTAGGED compiled code block */
336 /* Assembly code makes assumptions about the layout of this struct */
337 struct alien : public object {
338 static const cell type_number = ALIEN_TYPE;
347 struct dll : public object {
348 static const cell type_number = DLL_TYPE;
349 /* tagged byte array holding a C string */
351 /* OS-specific handle */
358 /* Frame size in bytes */
362 struct callstack : public object {
363 static const cell type_number = CALLSTACK_TYPE;
367 stack_frame *frame_at(cell offset)
369 return (stack_frame *)((char *)(this + 1) + offset);
372 stack_frame *top() { return (stack_frame *)(this + 1); }
373 stack_frame *bottom() { return (stack_frame *)((cell)(this + 1) + untag_fixnum(length)); }
376 struct tuple : public object {
377 static const cell type_number = TUPLE_TYPE;
381 cell *data() { return (cell *)(this + 1); }