]> gitweb.factorcode.org Git - factor.git/blob - vm/layouts.hpp
Solution to Project Euler problem 65
[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 inline static cell align8(cell a)
27 {
28         return align(a,8);
29 }
30
31 #define WORD_SIZE (signed)(sizeof(cell)*8)
32
33 #define TAG_MASK 7
34 #define TAG_BITS 3
35 #define TAG(x) ((cell)(x) & TAG_MASK)
36 #define UNTAG(x) ((cell)(x) & ~TAG_MASK)
37 #define RETAG(x,tag) (UNTAG(x) | (tag))
38
39 /*** Tags ***/
40 #define FIXNUM_TYPE 0
41 #define BIGNUM_TYPE 1
42 #define ARRAY_TYPE 2
43 #define FLOAT_TYPE 3
44 #define QUOTATION_TYPE 4
45 #define F_TYPE 5
46 #define OBJECT_TYPE 6
47 #define TUPLE_TYPE 7
48
49 /* Canonical F object */
50 #define F F_TYPE
51
52 #define HEADER_TYPE 8 /* anything less than this is a tag */
53
54 #define GC_COLLECTED 5 /* can be anything other than FIXNUM_TYPE */
55
56 /*** Header types ***/
57 #define WRAPPER_TYPE 8
58 #define BYTE_ARRAY_TYPE 9
59 #define CALLSTACK_TYPE 10
60 #define STRING_TYPE 11
61 #define WORD_TYPE 12
62 #define DLL_TYPE 13
63 #define ALIEN_TYPE 14
64
65 #define TYPE_COUNT 15
66
67 /* Not a real type, but code_block's type field can be set to this */
68 #define PIC_TYPE 69
69
70 /* Constants used when floating-point trap exceptions are thrown */
71 enum
72 {
73         FP_TRAP_INVALID_OPERATION = 1 << 0,
74         FP_TRAP_OVERFLOW          = 1 << 1,
75         FP_TRAP_UNDERFLOW         = 1 << 2,
76         FP_TRAP_ZERO_DIVIDE       = 1 << 3,
77         FP_TRAP_INEXACT           = 1 << 4,
78 };
79
80 inline static bool immediate_p(cell obj)
81 {
82         return (obj == F || TAG(obj) == FIXNUM_TYPE);
83 }
84
85 inline static fixnum untag_fixnum(cell tagged)
86 {
87 #ifdef FACTOR_DEBUG
88         assert(TAG(tagged) == FIXNUM_TYPE);
89 #endif
90         return ((fixnum)tagged) >> TAG_BITS;
91 }
92
93 inline static cell tag_fixnum(fixnum untagged)
94 {
95         return RETAG(untagged << TAG_BITS,FIXNUM_TYPE);
96 }
97
98 inline static cell tag_for(cell type)
99 {
100         return type < HEADER_TYPE ? type : OBJECT_TYPE;
101 }
102
103 struct object;
104
105 struct header {
106         cell value;
107
108         /* Default ctor to make gcc 3.x happy */
109         header() { abort(); }
110
111         header(cell value_) : value(value_ << TAG_BITS) {}
112
113         void check_header() {
114 #ifdef FACTOR_DEBUG
115                 assert(TAG(value) == FIXNUM_TYPE && untag_fixnum(value) < TYPE_COUNT);
116 #endif
117         }
118
119         cell hi_tag() {
120                 check_header();
121                 return value >> TAG_BITS;
122         }
123
124         bool forwarding_pointer_p() {
125                 return TAG(value) == GC_COLLECTED;
126         }
127
128         object *forwarding_pointer() {
129                 return (object *)UNTAG(value);
130         }
131
132         void forward_to(object *pointer) {
133                 value = RETAG(pointer,GC_COLLECTED);
134         }
135 };
136
137 #define NO_TYPE_CHECK static const cell type_number = TYPE_COUNT
138
139 struct object {
140         NO_TYPE_CHECK;
141         header h;
142         cell *slots() { return (cell *)this; }
143 };
144
145 /* Assembly code makes assumptions about the layout of this struct */
146 struct array : public object {
147         static const cell type_number = ARRAY_TYPE;
148         static const cell element_size = sizeof(cell);
149         /* tagged */
150         cell capacity;
151
152         cell *data() { return (cell *)(this + 1); }
153 };
154
155 /* These are really just arrays, but certain elements have special
156 significance */
157 struct tuple_layout : public array {
158         NO_TYPE_CHECK;
159         /* tagged */
160         cell klass;
161         /* tagged fixnum */
162         cell size;
163         /* tagged fixnum */
164         cell echelon;
165 };
166
167 struct bignum : public object {
168         static const cell type_number = BIGNUM_TYPE;
169         static const cell element_size = sizeof(cell);
170         /* tagged */
171         cell capacity;
172
173         cell *data() { return (cell *)(this + 1); }
174 };
175
176 struct byte_array : public object {
177         static const cell type_number = BYTE_ARRAY_TYPE;
178         static const cell element_size = 1;
179         /* tagged */
180         cell capacity;
181
182         template<typename T> T *data() { return (T *)(this + 1); }
183 };
184
185 /* Assembly code makes assumptions about the layout of this struct */
186 struct string : public object {
187         static const cell type_number = STRING_TYPE;
188         /* tagged num of chars */
189         cell length;
190         /* tagged */
191         cell aux;
192         /* tagged */
193         cell hashcode;
194
195         u8 *data() { return (u8 *)(this + 1); }
196 };
197
198 /* The compiled code heap is structured into blocks. */
199 enum block_status
200 {
201         B_FREE,
202         B_ALLOCATED,
203         B_MARKED
204 };
205
206 struct heap_block
207 {
208         unsigned char status; /* free or allocated? */
209         unsigned char type; /* this is WORD_TYPE or QUOTATION_TYPE */
210         unsigned char last_scan; /* the youngest generation in which this block's literals may live */
211         unsigned char needs_fixup; /* is this a new block that needs full fixup? */
212
213         /* In bytes, includes this header */
214         cell size;
215 };
216
217 struct free_heap_block : public heap_block
218 {
219         free_heap_block *next_free;
220 };
221
222 struct code_block : public heap_block
223 {
224         cell literals; /* # bytes */
225         cell relocation; /* tagged pointer to byte-array or f */
226         
227         void *xt() { return (void *)(this + 1); }
228 };
229
230 /* Assembly code makes assumptions about the layout of this struct */
231 struct word : public object {
232         static const cell type_number = WORD_TYPE;
233         /* TAGGED hashcode */
234         cell hashcode;
235         /* TAGGED word name */
236         cell name;
237         /* TAGGED word vocabulary */
238         cell vocabulary;
239         /* TAGGED definition */
240         cell def;
241         /* TAGGED property assoc for library code */
242         cell props;
243         /* TAGGED alternative entry point for direct non-tail calls. Used for inline caching */
244         cell pic_def;
245         /* TAGGED alternative entry point for direct tail calls. Used for inline caching */
246         cell pic_tail_def;
247         /* TAGGED call count for profiling */
248         cell counter;
249         /* TAGGED machine code for sub-primitive */
250         cell subprimitive;
251         /* UNTAGGED execution token: jump here to execute word */
252         void *xt;
253         /* UNTAGGED compiled code block */
254         code_block *code;
255         /* UNTAGGED profiler stub */
256         code_block *profiling;
257 };
258
259 /* Assembly code makes assumptions about the layout of this struct */
260 struct wrapper : public object {
261         static const cell type_number = WRAPPER_TYPE;
262         cell object;
263 };
264
265 /* Assembly code makes assumptions about the layout of this struct */
266 struct boxed_float : object {
267         static const cell type_number = FLOAT_TYPE;
268
269 #ifndef FACTOR_64
270         cell padding;
271 #endif
272
273         double n;
274 };
275
276 /* Assembly code makes assumptions about the layout of this struct */
277 struct quotation : public object {
278         static const cell type_number = QUOTATION_TYPE;
279         /* tagged */
280         cell array;
281         /* tagged */
282         cell cached_effect;
283         /* tagged */
284         cell cache_counter;
285         /* UNTAGGED */
286         void *xt;
287         /* UNTAGGED compiled code block */
288         code_block *code;
289 };
290
291 /* Assembly code makes assumptions about the layout of this struct */
292 struct alien : public object {
293         static const cell type_number = ALIEN_TYPE;
294         /* tagged */
295         cell alien;
296         /* tagged */
297         cell expired;
298         /* untagged */
299         cell displacement;
300 };
301
302 struct dll : public object {
303         static const cell type_number = DLL_TYPE;
304         /* tagged byte array holding a C string */
305         cell path;
306         /* OS-specific handle */
307         void *dll;
308 };
309
310 struct stack_frame
311 {
312         void *xt;
313         /* Frame size in bytes */
314         cell size;
315 };
316
317 struct callstack : public object {
318         static const cell type_number = CALLSTACK_TYPE;
319         /* tagged */
320         cell length;
321         
322         stack_frame *frame_at(cell offset)
323         {
324                 return (stack_frame *)((char *)(this + 1) + offset);
325         }
326
327         stack_frame *top() { return (stack_frame *)(this + 1); }
328         stack_frame *bottom() { return (stack_frame *)((cell)(this + 1) + untag_fixnum(length)); }
329 };
330
331 struct tuple : public object {
332         static const cell type_number = TUPLE_TYPE;
333         /* tagged layout */
334         cell layout;
335
336         cell *data() { return (cell *)(this + 1); }
337 };
338
339 }