]> gitweb.factorcode.org Git - factor.git/blob - vm/layouts.hpp
merge project-euler.factor
[factor.git] / vm / layouts.hpp
1 namespace factor
2 {
3
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;
12
13 #ifdef _WIN64
14         typedef long long fixnum;
15         typedef unsigned long long cell;
16 #else
17         typedef long fixnum;
18         typedef unsigned long cell;
19 #endif
20
21 inline static cell align(cell a, cell b)
22 {
23         return (a + (b-1)) & ~(b-1);
24 }
25
26 inline static cell align8(cell a)
27 {
28         return align(a,8);
29 }
30
31 #define WORD_SIZE (signed)(sizeof(cell)*8)
32
33 #define TAG_MASK 7
34 #define TAG_BITS 3
35 #define TAG(x) ((cell)(x) & TAG_MASK)
36 #define UNTAG(x) ((cell)(x) & ~TAG_MASK)
37 #define RETAG(x,tag) (UNTAG(x) | (tag))
38
39 /*** Tags ***/
40 #define FIXNUM_TYPE 0
41 #define BIGNUM_TYPE 1
42 #define ARRAY_TYPE 2
43 #define FLOAT_TYPE 3
44 #define QUOTATION_TYPE 4
45 #define F_TYPE 5
46 #define OBJECT_TYPE 6
47 #define TUPLE_TYPE 7
48
49 /* Canonical F object */
50 #define F F_TYPE
51
52 #define HEADER_TYPE 8 /* anything less than this is a tag */
53
54 #define GC_COLLECTED 5 /* can be anything other than FIXNUM_TYPE */
55
56 /*** Header types ***/
57 #define WRAPPER_TYPE 8
58 #define BYTE_ARRAY_TYPE 9
59 #define CALLSTACK_TYPE 10
60 #define STRING_TYPE 11
61 #define WORD_TYPE 12
62 #define DLL_TYPE 13
63 #define ALIEN_TYPE 14
64
65 #define TYPE_COUNT 15
66
67 /* Not real types, but code_block's type can be set to this */
68 #define PIC_TYPE 16
69 #define FREE_BLOCK_TYPE 17
70
71 /* Constants used when floating-point trap exceptions are thrown */
72 enum
73 {
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,
79 };
80
81 inline static bool immediate_p(cell obj)
82 {
83         return (obj == F || TAG(obj) == FIXNUM_TYPE);
84 }
85
86 inline static fixnum untag_fixnum(cell tagged)
87 {
88 #ifdef FACTOR_DEBUG
89         assert(TAG(tagged) == FIXNUM_TYPE);
90 #endif
91         return ((fixnum)tagged) >> TAG_BITS;
92 }
93
94 inline static cell tag_fixnum(fixnum untagged)
95 {
96         return RETAG(untagged << TAG_BITS,FIXNUM_TYPE);
97 }
98
99 inline static cell tag_for(cell type)
100 {
101         return type < HEADER_TYPE ? type : OBJECT_TYPE;
102 }
103
104 struct object;
105
106 struct header {
107         cell value;
108
109         /* Default ctor to make gcc 3.x happy */
110         explicit header() { abort(); }
111
112         explicit header(cell value_) : value(value_ << TAG_BITS) {}
113
114         void check_header() {
115 #ifdef FACTOR_DEBUG
116                 assert(TAG(value) == FIXNUM_TYPE && untag_fixnum(value) < TYPE_COUNT);
117 #endif
118         }
119
120         cell hi_tag() {
121                 check_header();
122                 return value >> TAG_BITS;
123         }
124
125         bool forwarding_pointer_p() {
126                 return TAG(value) == GC_COLLECTED;
127         }
128
129         object *forwarding_pointer() {
130                 return (object *)UNTAG(value);
131         }
132
133         void forward_to(object *pointer) {
134                 value = RETAG(pointer,GC_COLLECTED);
135         }
136 };
137
138 #define NO_TYPE_CHECK static const cell type_number = TYPE_COUNT
139
140 struct object {
141         NO_TYPE_CHECK;
142         header h;
143         cell *slots() { return (cell *)this; }
144 };
145
146 /* Assembly code makes assumptions about the layout of this struct */
147 struct array : public object {
148         static const cell type_number = ARRAY_TYPE;
149         static const cell element_size = sizeof(cell);
150         /* tagged */
151         cell capacity;
152
153         cell *data() { return (cell *)(this + 1); }
154 };
155
156 /* These are really just arrays, but certain elements have special
157 significance */
158 struct tuple_layout : public array {
159         NO_TYPE_CHECK;
160         /* tagged */
161         cell klass;
162         /* tagged fixnum */
163         cell size;
164         /* tagged fixnum */
165         cell echelon;
166 };
167
168 struct bignum : public object {
169         static const cell type_number = BIGNUM_TYPE;
170         static const cell element_size = sizeof(cell);
171         /* tagged */
172         cell capacity;
173
174         cell *data() { return (cell *)(this + 1); }
175 };
176
177 struct byte_array : public object {
178         static const cell type_number = BYTE_ARRAY_TYPE;
179         static const cell element_size = 1;
180         /* tagged */
181         cell capacity;
182
183         template<typename Scalar> Scalar *data() { return (Scalar *)(this + 1); }
184 };
185
186 /* Assembly code makes assumptions about the layout of this struct */
187 struct string : public object {
188         static const cell type_number = STRING_TYPE;
189         /* tagged num of chars */
190         cell length;
191         /* tagged */
192         cell aux;
193         /* tagged */
194         cell hashcode;
195
196         u8 *data() { return (u8 *)(this + 1); }
197 };
198
199 /* The compiled code heap is structured into blocks. */
200 struct heap_block
201 {
202         /* Bit 0: mark
203            Bit 1-7: type
204            Bit 8-...: size */
205         cell header;
206
207         bool marked_p() { return header & 1; }
208         void set_marked_p(bool marked)
209         {
210                 if(marked)
211                         header |= 1;
212                 else
213                         header &= ~1;
214         }
215
216         cell type() { return (header >> 1) & 0x1f; }
217         void set_type(cell type)
218         {
219                 header = ((header & ~(0x1f << 1)) | (type << 1));
220         }
221
222         cell size() { return (header >> 6); }
223         void set_size(cell size)
224         {
225                 header = (header & 0x2f) | (size << 6);
226         }
227 };
228
229 struct free_heap_block : public heap_block
230 {
231         free_heap_block *next_free;
232 };
233
234 struct code_block : public heap_block
235 {
236         cell owner; /* tagged pointer to word, quotation or f */
237         cell literals; /* tagged pointer to array or f */
238         cell relocation; /* tagged pointer to byte-array or f */
239
240         void *xt() { return (void *)(this + 1); }
241 };
242
243 /* Assembly code makes assumptions about the layout of this struct */
244 struct word : public object {
245         static const cell type_number = WORD_TYPE;
246         /* TAGGED hashcode */
247         cell hashcode;
248         /* TAGGED word name */
249         cell name;
250         /* TAGGED word vocabulary */
251         cell vocabulary;
252         /* TAGGED definition */
253         cell def;
254         /* TAGGED property assoc for library code */
255         cell props;
256         /* TAGGED alternative entry point for direct non-tail calls. Used for inline caching */
257         cell pic_def;
258         /* TAGGED alternative entry point for direct tail calls. Used for inline caching */
259         cell pic_tail_def;
260         /* TAGGED call count for profiling */
261         cell counter;
262         /* TAGGED machine code for sub-primitive */
263         cell subprimitive;
264         /* UNTAGGED execution token: jump here to execute word */
265         void *xt;
266         /* UNTAGGED compiled code block */
267         code_block *code;
268         /* UNTAGGED profiler stub */
269         code_block *profiling;
270 };
271
272 /* Assembly code makes assumptions about the layout of this struct */
273 struct wrapper : public object {
274         static const cell type_number = WRAPPER_TYPE;
275         cell object;
276 };
277
278 /* Assembly code makes assumptions about the layout of this struct */
279 struct boxed_float : object {
280         static const cell type_number = FLOAT_TYPE;
281
282 #ifndef FACTOR_64
283         cell padding;
284 #endif
285
286         double n;
287 };
288
289 /* Assembly code makes assumptions about the layout of this struct */
290 struct quotation : public object {
291         static const cell type_number = QUOTATION_TYPE;
292         /* tagged */
293         cell array;
294         /* tagged */
295         cell cached_effect;
296         /* tagged */
297         cell cache_counter;
298         /* UNTAGGED */
299         void *xt;
300         /* UNTAGGED compiled code block */
301         code_block *code;
302 };
303
304 /* Assembly code makes assumptions about the layout of this struct */
305 struct alien : public object {
306         static const cell type_number = ALIEN_TYPE;
307         /* tagged */
308         cell base;
309         /* tagged */
310         cell expired;
311         /* untagged */
312         cell displacement;
313 };
314
315 struct dll : public object {
316         static const cell type_number = DLL_TYPE;
317         /* tagged byte array holding a C string */
318         cell path;
319         /* OS-specific handle */
320         void *dll;
321 };
322
323 struct stack_frame
324 {
325         void *xt;
326         /* Frame size in bytes */
327         cell size;
328 };
329
330 struct callstack : public object {
331         static const cell type_number = CALLSTACK_TYPE;
332         /* tagged */
333         cell length;
334         
335         stack_frame *frame_at(cell offset)
336         {
337                 return (stack_frame *)((char *)(this + 1) + offset);
338         }
339
340         stack_frame *top() { return (stack_frame *)(this + 1); }
341         stack_frame *bottom() { return (stack_frame *)((cell)(this + 1) + untag_fixnum(length)); }
342 };
343
344 struct tuple : public object {
345         static const cell type_number = TUPLE_TYPE;
346         /* tagged layout */
347         cell layout;
348
349         cell *data() { return (cell *)(this + 1); }
350 };
351
352 }