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