]> gitweb.factorcode.org Git - factor.git/blob - vm/layouts.hpp
Merge branch 'master' into startup
[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 /* The compiled code heap is structured into blocks. */
213 struct code_block
214 {
215         cell header;
216         cell owner; /* tagged pointer to word, quotation or f */
217         cell literals; /* tagged pointer to array or f */
218         cell relocation; /* tagged pointer to byte-array or f */
219
220         bool free_p() const
221         {
222                 return (header & 1) == 1;
223         }
224
225         code_block_type type() const
226         {
227                 return (code_block_type)((header >> 1) & 0x3);
228         }
229
230         void set_type(code_block_type type)
231         {
232                 header = ((header & ~0x7) | (type << 1));
233         }
234
235         bool pic_p() const
236         {
237                 return type() == code_block_pic;
238         }
239
240         bool optimized_p() const
241         {
242                 return type() == code_block_optimized;
243         }
244
245         cell size() const
246         {
247                 return header & ~7;
248         }
249
250         void *xt() const
251         {
252                 return (void *)(this + 1);
253         }
254 };
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 profiling */
274         cell counter;
275         /* TAGGED machine code for sub-primitive */
276         cell subprimitive;
277         /* UNTAGGED execution token: jump here to execute word */
278         void *xt;
279         /* UNTAGGED compiled code block */
280         code_block *code;
281         /* UNTAGGED profiler stub */
282         code_block *profiling;
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 */
312         void *xt;
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 *dll;
344 };
345
346 struct stack_frame {
347         void *xt;
348         /* Frame size in bytes */
349         cell size;
350 };
351
352 struct callstack : public object {
353         static const cell type_number = CALLSTACK_TYPE;
354         /* tagged */
355         cell length;
356         
357         stack_frame *frame_at(cell offset) const
358         {
359                 return (stack_frame *)((char *)(this + 1) + offset);
360         }
361
362         stack_frame *top() const { return (stack_frame *)(this + 1); }
363         stack_frame *bottom() const { return (stack_frame *)((cell)(this + 1) + untag_fixnum(length)); }
364 };
365
366 struct tuple : public object {
367         static const cell type_number = TUPLE_TYPE;
368         /* tagged layout */
369         cell layout;
370
371         cell *data() const { return (cell *)(this + 1); }
372 };
373
374 struct data_root_range {
375         cell *start;
376         cell len;
377
378         explicit data_root_range(cell *start_, cell len_) :
379                 start(start_), len(len_) {}
380 };
381
382 }