]> gitweb.factorcode.org Git - factor.git/blob - vm/layouts.hpp
Merge branch 'master' of factorcode.org:/git/factor
[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 struct object;
95
96 #define NO_TYPE_CHECK static const cell type_number = TYPE_COUNT
97
98 struct object {
99         NO_TYPE_CHECK;
100         cell header;
101
102         cell size() const;
103         cell binary_payload_start() const;
104
105         cell *slots() const { return (cell *)this; }
106
107         template<typename Iterator> void each_slot(Iterator &iter);
108
109         /* Only valid for objects in tenured space; must cast to free_heap_block
110         to do anything with it if its free */
111         bool free_p() const
112         {
113                 return (header & 1) == 1;
114         }
115
116         cell type() const
117         {
118                 return (header >> 2) & TAG_MASK;
119         }
120
121         void initialize(cell type)
122         {
123                 header = type << 2;
124         }
125
126         cell hashcode() const
127         {
128                 return (header >> 6);
129         }
130
131         void set_hashcode(cell hashcode)
132         {
133                 header = (header & 0x3f) | (hashcode << 6);
134         }
135
136         bool forwarding_pointer_p() const
137         {
138                 return (header & 2) == 2;
139         }
140
141         object *forwarding_pointer() const
142         {
143                 return (object *)UNTAG(header);
144         }
145
146         void forward_to(object *pointer)
147         {
148                 header = ((cell)pointer | 2);
149         }
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() const { 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() const { 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 #ifndef FACTOR_64
190         cell padding0;
191         cell padding1;
192 #endif
193
194         template<typename Scalar> Scalar *data() const { return (Scalar *)(this + 1); }
195 };
196
197 /* Assembly code makes assumptions about the layout of this struct */
198 struct string : public object {
199         static const cell type_number = STRING_TYPE;
200         /* tagged num of chars */
201         cell length;
202         /* tagged */
203         cell aux;
204         /* tagged */
205         cell hashcode;
206
207         u8 *data() const { return (u8 *)(this + 1); }
208
209         cell nth(cell i) const;
210 };
211
212 struct code_block;
213
214 /* Assembly code makes assumptions about the layout of this struct */
215 struct word : public object {
216         static const cell type_number = WORD_TYPE;
217         /* TAGGED hashcode */
218         cell hashcode;
219         /* TAGGED word name */
220         cell name;
221         /* TAGGED word vocabulary */
222         cell vocabulary;
223         /* TAGGED definition */
224         cell def;
225         /* TAGGED property assoc for library code */
226         cell props;
227         /* TAGGED alternative entry point for direct non-tail calls. Used for inline caching */
228         cell pic_def;
229         /* TAGGED alternative entry point for direct tail calls. Used for inline caching */
230         cell pic_tail_def;
231         /* TAGGED call count for profiling */
232         cell counter;
233         /* TAGGED machine code for sub-primitive */
234         cell subprimitive;
235         /* UNTAGGED entry point: jump here to execute word */
236         void *entry_point;
237         /* UNTAGGED compiled code block */
238         code_block *code;
239         /* UNTAGGED profiler stub */
240         code_block *profiling;
241 };
242
243 /* Assembly code makes assumptions about the layout of this struct */
244 struct wrapper : public object {
245         static const cell type_number = WRAPPER_TYPE;
246         cell object;
247 };
248
249 const fixnum boxed_float_offset = 8 - FLOAT_TYPE;
250
251 /* Assembly code makes assumptions about the layout of this struct */
252 struct boxed_float : object {
253         static const cell type_number = FLOAT_TYPE;
254
255 #ifndef FACTOR_64
256         cell padding;
257 #endif
258
259         double n;
260 };
261
262 /* Assembly code makes assumptions about the layout of this struct */
263 struct quotation : public object {
264         static const cell type_number = QUOTATION_TYPE;
265         /* tagged */
266         cell array;
267         /* tagged */
268         cell cached_effect;
269         /* tagged */
270         cell cache_counter;
271         /* UNTAGGED entry point; jump here to call quotation */
272         void *entry_point;
273         /* UNTAGGED compiled code block */
274         code_block *code;
275 };
276
277 /* Assembly code makes assumptions about the layout of this struct */
278 struct alien : public object {
279         static const cell type_number = ALIEN_TYPE;
280         /* tagged */
281         cell base;
282         /* tagged */
283         cell expired;
284         /* untagged */
285         cell displacement;
286         /* untagged */
287         cell address;
288
289         void update_address()
290         {
291                 if(base == false_object)
292                         address = displacement;
293                 else
294                         address = UNTAG(base) + sizeof(byte_array) + displacement;
295         }
296 };
297
298 struct dll : public object {
299         static const cell type_number = DLL_TYPE;
300         /* tagged byte array holding a C string */
301         cell path;
302         /* OS-specific handle */
303         void *handle;
304 };
305
306 struct stack_frame {
307         /* Updated by procedure prologue with procedure start address */
308         void *entry_point;
309         /* Frame size in bytes */
310         cell size;
311 };
312
313 struct callstack : public object {
314         static const cell type_number = CALLSTACK_TYPE;
315         /* tagged */
316         cell length;
317         
318         stack_frame *frame_at(cell offset) const
319         {
320                 return (stack_frame *)((char *)(this + 1) + offset);
321         }
322
323         stack_frame *top() const { return (stack_frame *)(this + 1); }
324         stack_frame *bottom() const { return (stack_frame *)((cell)(this + 1) + untag_fixnum(length)); }
325 };
326
327 struct tuple : public object {
328         static const cell type_number = TUPLE_TYPE;
329         /* tagged layout */
330         cell layout;
331
332         cell *data() const { return (cell *)(this + 1); }
333 };
334
335 struct data_root_range {
336         cell *start;
337         cell len;
338
339         explicit data_root_range(cell *start_, cell len_) :
340                 start(start_), len(len_) {}
341 };
342
343 }