]> gitweb.factorcode.org Git - factor.git/blob - vm/layouts.hpp
Fix conflict
[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 execution token: jump here to execute word */
236         void *xt;
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 /* Assembly code makes assumptions about the layout of this struct */
250 struct boxed_float : object {
251         static const cell type_number = FLOAT_TYPE;
252
253 #ifndef FACTOR_64
254         cell padding;
255 #endif
256
257         double n;
258 };
259
260 /* Assembly code makes assumptions about the layout of this struct */
261 struct quotation : public object {
262         static const cell type_number = QUOTATION_TYPE;
263         /* tagged */
264         cell array;
265         /* tagged */
266         cell cached_effect;
267         /* tagged */
268         cell cache_counter;
269         /* UNTAGGED */
270         void *xt;
271         /* UNTAGGED compiled code block */
272         code_block *code;
273 };
274
275 /* Assembly code makes assumptions about the layout of this struct */
276 struct alien : public object {
277         static const cell type_number = ALIEN_TYPE;
278         /* tagged */
279         cell base;
280         /* tagged */
281         cell expired;
282         /* untagged */
283         cell displacement;
284         /* untagged */
285         cell address;
286
287         void update_address()
288         {
289                 if(base == false_object)
290                         address = displacement;
291                 else
292                         address = UNTAG(base) + sizeof(byte_array) + displacement;
293         }
294 };
295
296 struct dll : public object {
297         static const cell type_number = DLL_TYPE;
298         /* tagged byte array holding a C string */
299         cell path;
300         /* OS-specific handle */
301         void *dll;
302 };
303
304 struct stack_frame {
305         void *xt;
306         /* Frame size in bytes */
307         cell size;
308 };
309
310 struct callstack : public object {
311         static const cell type_number = CALLSTACK_TYPE;
312         /* tagged */
313         cell length;
314         
315         stack_frame *frame_at(cell offset) const
316         {
317                 return (stack_frame *)((char *)(this + 1) + offset);
318         }
319
320         stack_frame *top() const { return (stack_frame *)(this + 1); }
321         stack_frame *bottom() const { return (stack_frame *)((cell)(this + 1) + untag_fixnum(length)); }
322 };
323
324 struct tuple : public object {
325         static const cell type_number = TUPLE_TYPE;
326         /* tagged layout */
327         cell layout;
328
329         cell *data() const { return (cell *)(this + 1); }
330 };
331
332 struct data_root_range {
333         cell *start;
334         cell len;
335
336         explicit data_root_range(cell *start_, cell len_) :
337                 start(start_), len(len_) {}
338 };
339
340 }