]> gitweb.factorcode.org Git - factor.git/blob - vm/layouts.hpp
vm: change code heap layout somewhat, remove unused allocation bitmap from mark_bits
[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         inline heap_block *next()
236         {
237                 return (heap_block *)((cell)this + size());
238         }
239 };
240
241 struct free_heap_block : public heap_block
242 {
243         free_heap_block *next_free;
244 };
245
246 struct code_block : public heap_block
247 {
248         cell owner; /* tagged pointer to word, quotation or f */
249         cell literals; /* tagged pointer to array or f */
250         cell relocation; /* tagged pointer to byte-array or f */
251
252         void *xt()
253         {
254                 return (void *)(this + 1);
255         }
256
257         cell type()
258         {
259                 return (header >> 1) & 0x3;
260         }
261
262         void set_type(code_block_type type)
263         {
264                 header = ((header & ~0x7) | (type << 1));
265         }
266
267         bool pic_p()
268         {
269                 return type() == code_block_pic;
270         }
271
272         bool optimized_p()
273         {
274                 return type() == code_block_optimized;
275         }
276 };
277
278 /* Assembly code makes assumptions about the layout of this struct */
279 struct word : public object {
280         static const cell type_number = WORD_TYPE;
281         /* TAGGED hashcode */
282         cell hashcode;
283         /* TAGGED word name */
284         cell name;
285         /* TAGGED word vocabulary */
286         cell vocabulary;
287         /* TAGGED definition */
288         cell def;
289         /* TAGGED property assoc for library code */
290         cell props;
291         /* TAGGED alternative entry point for direct non-tail calls. Used for inline caching */
292         cell pic_def;
293         /* TAGGED alternative entry point for direct tail calls. Used for inline caching */
294         cell pic_tail_def;
295         /* TAGGED call count for profiling */
296         cell counter;
297         /* TAGGED machine code for sub-primitive */
298         cell subprimitive;
299         /* UNTAGGED execution token: jump here to execute word */
300         void *xt;
301         /* UNTAGGED compiled code block */
302         code_block *code;
303         /* UNTAGGED profiler stub */
304         code_block *profiling;
305 };
306
307 /* Assembly code makes assumptions about the layout of this struct */
308 struct wrapper : public object {
309         static const cell type_number = WRAPPER_TYPE;
310         cell object;
311 };
312
313 /* Assembly code makes assumptions about the layout of this struct */
314 struct boxed_float : object {
315         static const cell type_number = FLOAT_TYPE;
316
317 #ifndef FACTOR_64
318         cell padding;
319 #endif
320
321         double n;
322 };
323
324 /* Assembly code makes assumptions about the layout of this struct */
325 struct quotation : public object {
326         static const cell type_number = QUOTATION_TYPE;
327         /* tagged */
328         cell array;
329         /* tagged */
330         cell cached_effect;
331         /* tagged */
332         cell cache_counter;
333         /* UNTAGGED */
334         void *xt;
335         /* UNTAGGED compiled code block */
336         code_block *code;
337 };
338
339 /* Assembly code makes assumptions about the layout of this struct */
340 struct alien : public object {
341         static const cell type_number = ALIEN_TYPE;
342         /* tagged */
343         cell base;
344         /* tagged */
345         cell expired;
346         /* untagged */
347         cell displacement;
348 };
349
350 struct dll : public object {
351         static const cell type_number = DLL_TYPE;
352         /* tagged byte array holding a C string */
353         cell path;
354         /* OS-specific handle */
355         void *dll;
356 };
357
358 struct stack_frame
359 {
360         void *xt;
361         /* Frame size in bytes */
362         cell size;
363 };
364
365 struct callstack : public object {
366         static const cell type_number = CALLSTACK_TYPE;
367         /* tagged */
368         cell length;
369         
370         stack_frame *frame_at(cell offset)
371         {
372                 return (stack_frame *)((char *)(this + 1) + offset);
373         }
374
375         stack_frame *top() { return (stack_frame *)(this + 1); }
376         stack_frame *bottom() { return (stack_frame *)((cell)(this + 1) + untag_fixnum(length)); }
377 };
378
379 struct tuple : public object {
380         static const cell type_number = TUPLE_TYPE;
381         /* tagged layout */
382         cell layout;
383
384         cell *data() { return (cell *)(this + 1); }
385 };
386
387 }