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