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