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
45 #define WRAPPER_TYPE 8
46 #define BYTE_ARRAY_TYPE 9
47 #define CALLSTACK_TYPE 10
48 #define STRING_TYPE 11
54 #define GC_COLLECTED 5 /* can be anything other than FIXNUM_TYPE */
58 code_block_unoptimized,
64 /* Constants used when floating-point trap exceptions are thrown */
67 FP_TRAP_INVALID_OPERATION = 1 << 0,
68 FP_TRAP_OVERFLOW = 1 << 1,
69 FP_TRAP_UNDERFLOW = 1 << 2,
70 FP_TRAP_ZERO_DIVIDE = 1 << 3,
71 FP_TRAP_INEXACT = 1 << 4,
74 /* What Factor calls 'f' */
75 static const cell false_object = F_TYPE;
77 inline static bool immediate_p(cell obj)
79 return (obj == false_object || TAG(obj) == FIXNUM_TYPE);
82 inline static fixnum untag_fixnum(cell tagged)
85 assert(TAG(tagged) == FIXNUM_TYPE);
87 return ((fixnum)tagged) >> TAG_BITS;
90 inline static cell tag_fixnum(fixnum untagged)
92 return RETAG(untagged << TAG_BITS,FIXNUM_TYPE);
100 /* Default ctor to make gcc 3.x happy */
101 explicit header() { abort(); }
103 explicit header(cell value_) : value(value_ << TAG_BITS) {}
105 void check_header() const
108 assert(TAG(value) == FIXNUM_TYPE && untag_fixnum(value) < TYPE_COUNT);
115 return value >> TAG_BITS;
118 bool forwarding_pointer_p() const
120 return TAG(value) == GC_COLLECTED;
123 object *forwarding_pointer() const
125 return (object *)UNTAG(value);
128 void forward_to(object *pointer)
130 value = RETAG(pointer,GC_COLLECTED);
134 #define NO_TYPE_CHECK static const cell type_number = TYPE_COUNT
141 cell binary_payload_start() const;
143 cell *slots() const { return (cell *)this; }
145 /* Only valid for objects in tenured space; must fast to free_heap_block
146 to do anything with it if its free */
149 return h.value & 1 == 1;
153 /* Assembly code makes assumptions about the layout of this struct */
154 struct array : public object {
155 static const cell type_number = ARRAY_TYPE;
156 static const cell element_size = sizeof(cell);
160 cell *data() const { return (cell *)(this + 1); }
163 /* These are really just arrays, but certain elements have special
165 struct tuple_layout : public array {
175 struct bignum : public object {
176 static const cell type_number = BIGNUM_TYPE;
177 static const cell element_size = sizeof(cell);
181 cell *data() const { return (cell *)(this + 1); }
184 struct byte_array : public object {
185 static const cell type_number = BYTE_ARRAY_TYPE;
186 static const cell element_size = 1;
195 template<typename Scalar> Scalar *data() const { return (Scalar *)(this + 1); }
198 /* Assembly code makes assumptions about the layout of this struct */
199 struct string : public object {
200 static const cell type_number = STRING_TYPE;
201 /* tagged num of chars */
208 u8 *data() const { return (u8 *)(this + 1); }
210 cell nth(cell i) const;
213 /* The compiled code heap is structured into blocks. */
217 cell owner; /* tagged pointer to word, quotation or f */
218 cell literals; /* tagged pointer to array or f */
219 cell relocation; /* tagged pointer to byte-array or f */
223 return header & 1 == 1;
226 code_block_type type() const
228 return (code_block_type)((header >> 1) & 0x3);
231 void set_type(code_block_type type)
233 header = ((header & ~0x7) | (type << 1));
238 return type() == code_block_pic;
241 bool optimized_p() const
243 return type() == code_block_optimized;
253 return (void *)(this + 1);
257 /* Assembly code makes assumptions about the layout of this struct */
258 struct word : public object {
259 static const cell type_number = WORD_TYPE;
260 /* TAGGED hashcode */
262 /* TAGGED word name */
264 /* TAGGED word vocabulary */
266 /* TAGGED definition */
268 /* TAGGED property assoc for library code */
270 /* TAGGED alternative entry point for direct non-tail calls. Used for inline caching */
272 /* TAGGED alternative entry point for direct tail calls. Used for inline caching */
274 /* TAGGED call count for profiling */
276 /* TAGGED machine code for sub-primitive */
278 /* UNTAGGED execution token: jump here to execute word */
280 /* UNTAGGED compiled code block */
282 /* UNTAGGED profiler stub */
283 code_block *profiling;
286 /* Assembly code makes assumptions about the layout of this struct */
287 struct wrapper : public object {
288 static const cell type_number = WRAPPER_TYPE;
292 /* Assembly code makes assumptions about the layout of this struct */
293 struct boxed_float : object {
294 static const cell type_number = FLOAT_TYPE;
303 /* Assembly code makes assumptions about the layout of this struct */
304 struct quotation : public object {
305 static const cell type_number = QUOTATION_TYPE;
314 /* UNTAGGED compiled code block */
318 /* Assembly code makes assumptions about the layout of this struct */
319 struct alien : public object {
320 static const cell type_number = ALIEN_TYPE;
330 void update_address()
332 if(base == false_object)
333 address = displacement;
335 address = UNTAG(base) + sizeof(byte_array) + displacement;
339 struct dll : public object {
340 static const cell type_number = DLL_TYPE;
341 /* tagged byte array holding a C string */
343 /* OS-specific handle */
350 /* Frame size in bytes */
354 struct callstack : public object {
355 static const cell type_number = CALLSTACK_TYPE;
359 stack_frame *frame_at(cell offset) const
361 return (stack_frame *)((char *)(this + 1) + offset);
364 stack_frame *top() const { return (stack_frame *)(this + 1); }
365 stack_frame *bottom() const { return (stack_frame *)((cell)(this + 1) + untag_fixnum(length)); }
368 struct tuple : public object {
369 static const cell type_number = TUPLE_TYPE;
373 cell *data() const { return (cell *)(this + 1); }