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