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