]> gitweb.factorcode.org Git - factor.git/blob - vm/layouts.hpp
ebffa644b571c01e53d5ba1b22f57d6126ee1963
[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 alignment_for(cell a, cell b)
27 {
28         return align(a,b) - a;
29 }
30
31 static const cell data_alignment = 16;
32
33 #define WORD_SIZE (signed)(sizeof(cell)*8)
34
35 #define TAG_MASK 15
36 #define TAG_BITS 4
37 #define TAG(x) ((cell)(x) & TAG_MASK)
38 #define UNTAG(x) ((cell)(x) & ~TAG_MASK)
39 #define RETAG(x,tag) (UNTAG(x) | (tag))
40
41 /*** Tags ***/
42 #define FIXNUM_TYPE 0
43 #define F_TYPE 1
44 #define ARRAY_TYPE 2
45 #define FLOAT_TYPE 3
46 #define QUOTATION_TYPE 4
47 #define BIGNUM_TYPE 5
48 #define ALIEN_TYPE 6
49 #define TUPLE_TYPE 7
50 #define WRAPPER_TYPE 8
51 #define BYTE_ARRAY_TYPE 9
52 #define CALLSTACK_TYPE 10
53 #define STRING_TYPE 11
54 #define WORD_TYPE 12
55 #define DLL_TYPE 13
56
57 #define TYPE_COUNT 14
58
59 static inline const char *type_name(cell type)
60 {
61         switch (type)
62         {
63         case FIXNUM_TYPE:
64                 return "fixnum";
65         case F_TYPE:
66                 return "f";
67         case ARRAY_TYPE:
68                 return "array";
69         case FLOAT_TYPE:
70                 return "float";
71         case QUOTATION_TYPE:
72                 return "quotation";
73         case BIGNUM_TYPE:
74                 return "bignum";
75         case ALIEN_TYPE:
76                 return "alien";
77         case TUPLE_TYPE:
78                 return "tuple";
79         case WRAPPER_TYPE:
80                 return "wrapper";
81         case BYTE_ARRAY_TYPE:
82                 return "byte-array";
83         case CALLSTACK_TYPE:
84                 return "callstack";
85         case STRING_TYPE:
86                 return "string";
87         case WORD_TYPE:
88                 return "word";
89         case DLL_TYPE:
90                 return "dll";
91         default:
92                 assert(false);
93                 return "";
94         }
95 }
96
97 enum code_block_type
98 {
99         code_block_unoptimized,
100         code_block_optimized,
101         code_block_counting_profiler,
102         code_block_pic
103 };
104
105 /* Constants used when floating-point trap exceptions are thrown */
106 enum
107 {
108         FP_TRAP_INVALID_OPERATION = 1 << 0,
109         FP_TRAP_OVERFLOW          = 1 << 1,
110         FP_TRAP_UNDERFLOW         = 1 << 2,
111         FP_TRAP_ZERO_DIVIDE       = 1 << 3,
112         FP_TRAP_INEXACT           = 1 << 4,
113 };
114
115 /* What Factor calls 'f' */
116 static const cell false_object = F_TYPE;
117
118 inline static bool immediate_p(cell obj)
119 {
120         /* We assume that fixnums have tag 0 and false_object has tag 1 */
121         return TAG(obj) <= F_TYPE;
122 }
123
124 inline static fixnum untag_fixnum(cell tagged)
125 {
126 #ifdef FACTOR_DEBUG
127         assert(TAG(tagged) == FIXNUM_TYPE);
128 #endif
129         return ((fixnum)tagged) >> TAG_BITS;
130 }
131
132 inline static cell tag_fixnum(fixnum untagged)
133 {
134         return RETAG(untagged << TAG_BITS,FIXNUM_TYPE);
135 }
136
137 #define NO_TYPE_CHECK static const cell type_number = TYPE_COUNT
138
139 struct object {
140         NO_TYPE_CHECK;
141         cell header;
142
143         cell size() const;
144         template<typename Fixup> cell size(Fixup fixup) const;
145
146         cell binary_payload_start() const;
147         template<typename Fixup> cell binary_payload_start(Fixup fixup) const;
148
149         cell *slots() const { return (cell *)this; }
150
151         template<typename Iterator> void each_slot(Iterator &iter);
152
153         /* Only valid for objects in tenured space; must cast to free_heap_block
154         to do anything with it if its free */
155         bool free_p() const
156         {
157                 return (header & 1) == 1;
158         }
159
160         cell type() const
161         {
162                 return (header >> 2) & TAG_MASK;
163         }
164
165         void initialize(cell type)
166         {
167                 header = type << 2;
168         }
169
170         cell hashcode() const
171         {
172                 return (header >> 6);
173         }
174
175         void set_hashcode(cell hashcode)
176         {
177                 header = (header & 0x3f) | (hashcode << 6);
178         }
179
180         bool forwarding_pointer_p() const
181         {
182                 return (header & 2) == 2;
183         }
184
185         object *forwarding_pointer() const
186         {
187                 return (object *)UNTAG(header);
188         }
189
190         void forward_to(object *pointer)
191         {
192                 header = ((cell)pointer | 2);
193         }
194 };
195
196 /* Assembly code makes assumptions about the layout of this struct */
197 struct array : public object {
198         static const cell type_number = ARRAY_TYPE;
199         static const cell element_size = sizeof(cell);
200         /* tagged */
201         cell capacity;
202
203         cell *data() const { return (cell *)(this + 1); }
204 };
205
206 /* These are really just arrays, but certain elements have special
207 significance */
208 struct tuple_layout : public array {
209         NO_TYPE_CHECK;
210         /* tagged */
211         cell klass;
212         /* tagged fixnum */
213         cell size;
214         /* tagged fixnum */
215         cell echelon;
216 };
217
218 struct bignum : public object {
219         static const cell type_number = BIGNUM_TYPE;
220         static const cell element_size = sizeof(cell);
221         /* tagged */
222         cell capacity;
223
224         cell *data() const { return (cell *)(this + 1); }
225 };
226
227 struct byte_array : public object {
228         static const cell type_number = BYTE_ARRAY_TYPE;
229         static const cell element_size = 1;
230         /* tagged */
231         cell capacity;
232
233 #ifndef FACTOR_64
234         cell padding0;
235         cell padding1;
236 #endif
237
238         template<typename Scalar> Scalar *data() const { return (Scalar *)(this + 1); }
239 };
240
241 /* Assembly code makes assumptions about the layout of this struct */
242 struct string : public object {
243         static const cell type_number = STRING_TYPE;
244         /* tagged num of chars */
245         cell length;
246         /* tagged */
247         cell aux;
248         /* tagged */
249         cell hashcode;
250
251         u8 *data() const { return (u8 *)(this + 1); }
252 };
253
254 struct code_block;
255
256 /* Assembly code makes assumptions about the layout of this struct */
257 struct word : public object {
258         static const cell type_number = WORD_TYPE;
259         /* TAGGED hashcode */
260         cell hashcode;
261         /* TAGGED word name */
262         cell name;
263         /* TAGGED word vocabulary */
264         cell vocabulary;
265         /* TAGGED definition */
266         cell def;
267         /* TAGGED property assoc for library code */
268         cell props;
269         /* TAGGED alternative entry point for direct non-tail calls. Used for inline caching */
270         cell pic_def;
271         /* TAGGED alternative entry point for direct tail calls. Used for inline caching */
272         cell pic_tail_def;
273         /* TAGGED call count for counting_profiler */
274         cell counter;
275         /* TAGGED machine code for sub-primitive */
276         cell subprimitive;
277         /* UNTAGGED entry point: jump here to execute word */
278         void *entry_point;
279         /* UNTAGGED compiled code block */
280         code_block *code;
281         /* UNTAGGED counting_profiler stub */
282         code_block *counting_profiler;
283 };
284
285 /* Assembly code makes assumptions about the layout of this struct */
286 struct wrapper : public object {
287         static const cell type_number = WRAPPER_TYPE;
288         cell object;
289 };
290
291 /* Assembly code makes assumptions about the layout of this struct */
292 struct boxed_float : object {
293         static const cell type_number = FLOAT_TYPE;
294
295 #ifndef FACTOR_64
296         cell padding;
297 #endif
298
299         double n;
300 };
301
302 /* Assembly code makes assumptions about the layout of this struct */
303 struct quotation : public object {
304         static const cell type_number = QUOTATION_TYPE;
305         /* tagged */
306         cell array;
307         /* tagged */
308         cell cached_effect;
309         /* tagged */
310         cell cache_counter;
311         /* UNTAGGED entry point; jump here to call quotation */
312         void *entry_point;
313         /* UNTAGGED compiled code block */
314         code_block *code;
315 };
316
317 /* Assembly code makes assumptions about the layout of this struct */
318 struct alien : public object {
319         static const cell type_number = ALIEN_TYPE;
320         /* tagged */
321         cell base;
322         /* tagged */
323         cell expired;
324         /* untagged */
325         cell displacement;
326         /* untagged */
327         cell address;
328
329         void update_address()
330         {
331                 if(base == false_object)
332                         address = displacement;
333                 else
334                         address = UNTAG(base) + sizeof(byte_array) + displacement;
335         }
336 };
337
338 struct dll : public object {
339         static const cell type_number = DLL_TYPE;
340         /* tagged byte array holding a C string */
341         cell path;
342         /* OS-specific handle */
343         void *handle;
344 };
345
346 struct stack_frame {
347         /* Updated by procedure prologue with procedure start address */
348         void *entry_point;
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 struct data_root_range {
376         cell *start;
377         cell len;
378
379         explicit data_root_range(cell *start_, cell len_) :
380                 start(start_), len(len_) {}
381 };
382
383 }