]> gitweb.factorcode.org Git - factor.git/blob - vm/layouts.hpp
New identity-hashcode primitive
[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         /* 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         cell nth(cell i) const;
208 };
209
210 /* The compiled code heap is structured into blocks. */
211 struct code_block
212 {
213         cell header;
214         cell owner; /* tagged pointer to word, quotation or f */
215         cell literals; /* tagged pointer to array or f */
216         cell relocation; /* tagged pointer to byte-array or f */
217
218         bool free_p() const
219         {
220                 return (header & 1) == 1;
221         }
222
223         code_block_type type() const
224         {
225                 return (code_block_type)((header >> 1) & 0x3);
226         }
227
228         void set_type(code_block_type type)
229         {
230                 header = ((header & ~0x7) | (type << 1));
231         }
232
233         bool pic_p() const
234         {
235                 return type() == code_block_pic;
236         }
237
238         bool optimized_p() const
239         {
240                 return type() == code_block_optimized;
241         }
242
243         cell size() const
244         {
245                 return header & ~7;
246         }
247
248         void *xt() const
249         {
250                 return (void *)(this + 1);
251         }
252 };
253
254 /* Assembly code makes assumptions about the layout of this struct */
255 struct word : public object {
256         static const cell type_number = WORD_TYPE;
257         /* TAGGED hashcode */
258         cell hashcode;
259         /* TAGGED word name */
260         cell name;
261         /* TAGGED word vocabulary */
262         cell vocabulary;
263         /* TAGGED definition */
264         cell def;
265         /* TAGGED property assoc for library code */
266         cell props;
267         /* TAGGED alternative entry point for direct non-tail calls. Used for inline caching */
268         cell pic_def;
269         /* TAGGED alternative entry point for direct tail calls. Used for inline caching */
270         cell pic_tail_def;
271         /* TAGGED call count for profiling */
272         cell counter;
273         /* TAGGED machine code for sub-primitive */
274         cell subprimitive;
275         /* UNTAGGED execution token: jump here to execute word */
276         void *xt;
277         /* UNTAGGED compiled code block */
278         code_block *code;
279         /* UNTAGGED profiler stub */
280         code_block *profiling;
281 };
282
283 /* Assembly code makes assumptions about the layout of this struct */
284 struct wrapper : public object {
285         static const cell type_number = WRAPPER_TYPE;
286         cell object;
287 };
288
289 /* Assembly code makes assumptions about the layout of this struct */
290 struct boxed_float : object {
291         static const cell type_number = FLOAT_TYPE;
292
293 #ifndef FACTOR_64
294         cell padding;
295 #endif
296
297         double n;
298 };
299
300 /* Assembly code makes assumptions about the layout of this struct */
301 struct quotation : public object {
302         static const cell type_number = QUOTATION_TYPE;
303         /* tagged */
304         cell array;
305         /* tagged */
306         cell cached_effect;
307         /* tagged */
308         cell cache_counter;
309         /* UNTAGGED */
310         void *xt;
311         /* UNTAGGED compiled code block */
312         code_block *code;
313 };
314
315 /* Assembly code makes assumptions about the layout of this struct */
316 struct alien : public object {
317         static const cell type_number = ALIEN_TYPE;
318         /* tagged */
319         cell base;
320         /* tagged */
321         cell expired;
322         /* untagged */
323         cell displacement;
324         /* untagged */
325         cell address;
326
327         void update_address()
328         {
329                 if(base == false_object)
330                         address = displacement;
331                 else
332                         address = UNTAG(base) + sizeof(byte_array) + displacement;
333         }
334 };
335
336 struct dll : public object {
337         static const cell type_number = DLL_TYPE;
338         /* tagged byte array holding a C string */
339         cell path;
340         /* OS-specific handle */
341         void *dll;
342 };
343
344 struct stack_frame {
345         void *xt;
346         /* Frame size in bytes */
347         cell size;
348 };
349
350 struct callstack : public object {
351         static const cell type_number = CALLSTACK_TYPE;
352         /* tagged */
353         cell length;
354         
355         stack_frame *frame_at(cell offset) const
356         {
357                 return (stack_frame *)((char *)(this + 1) + offset);
358         }
359
360         stack_frame *top() const { return (stack_frame *)(this + 1); }
361         stack_frame *bottom() const { return (stack_frame *)((cell)(this + 1) + untag_fixnum(length)); }
362 };
363
364 struct tuple : public object {
365         static const cell type_number = TUPLE_TYPE;
366         /* tagged layout */
367         cell layout;
368
369         cell *data() const { return (cell *)(this + 1); }
370 };
371
372 struct data_root_range {
373         cell *start;
374         cell len;
375
376         explicit data_root_range(cell *start_, cell len_) :
377                 start(start_), len(len_) {}
378 };
379
380 }