]> gitweb.factorcode.org Git - factor.git/blob - vm/layouts.hpp
vm: eliminating literal table work in progress
[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 struct code_block;
215
216 /* Assembly code makes assumptions about the layout of this struct */
217 struct word : public object {
218         static const cell type_number = WORD_TYPE;
219         /* TAGGED hashcode */
220         cell hashcode;
221         /* TAGGED word name */
222         cell name;
223         /* TAGGED word vocabulary */
224         cell vocabulary;
225         /* TAGGED definition */
226         cell def;
227         /* TAGGED property assoc for library code */
228         cell props;
229         /* TAGGED alternative entry point for direct non-tail calls. Used for inline caching */
230         cell pic_def;
231         /* TAGGED alternative entry point for direct tail calls. Used for inline caching */
232         cell pic_tail_def;
233         /* TAGGED call count for profiling */
234         cell counter;
235         /* TAGGED machine code for sub-primitive */
236         cell subprimitive;
237         /* UNTAGGED execution token: jump here to execute word */
238         void *xt;
239         /* UNTAGGED compiled code block */
240         code_block *code;
241         /* UNTAGGED profiler stub */
242         code_block *profiling;
243 };
244
245 /* Assembly code makes assumptions about the layout of this struct */
246 struct wrapper : public object {
247         static const cell type_number = WRAPPER_TYPE;
248         cell object;
249 };
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 */
272         void *xt;
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 *dll;
304 };
305
306 struct stack_frame {
307         void *xt;
308         /* Frame size in bytes */
309         cell size;
310 };
311
312 struct callstack : public object {
313         static const cell type_number = CALLSTACK_TYPE;
314         /* tagged */
315         cell length;
316         
317         stack_frame *frame_at(cell offset) const
318         {
319                 return (stack_frame *)((char *)(this + 1) + offset);
320         }
321
322         stack_frame *top() const { return (stack_frame *)(this + 1); }
323         stack_frame *bottom() const { return (stack_frame *)((cell)(this + 1) + untag_fixnum(length)); }
324 };
325
326 struct tuple : public object {
327         static const cell type_number = TUPLE_TYPE;
328         /* tagged layout */
329         cell layout;
330
331         cell *data() const { return (cell *)(this + 1); }
332 };
333
334 struct data_root_range {
335         cell *start;
336         cell len;
337
338         explicit data_root_range(cell *start_, cell len_) :
339                 start(start_), len(len_) {}
340 };
341
342 }