]> gitweb.factorcode.org Git - factor.git/blob - vm/layouts.hpp
VM: Refactor layouts/hpp to Factor style
[factor.git] / vm / layouts.hpp
1 namespace factor {
2
3 typedef unsigned char u8;
4 typedef unsigned short u16;
5 typedef unsigned int u32;
6 typedef unsigned long long u64;
7 typedef signed char s8;
8 typedef signed short s16;
9 typedef signed int s32;
10 typedef signed long long s64;
11
12 #ifdef _WIN64
13 typedef long long fixnum;
14 typedef unsigned long long cell;
15 #else
16 typedef long fixnum;
17 typedef unsigned long cell;
18 #endif
19
20 inline static cell align(cell a, cell b) { return (a + (b - 1)) & ~(b - 1); }
21
22 inline static cell alignment_for(cell a, cell b) { return align(a, b) - a; }
23
24 static const cell data_alignment = 16;
25
26 #define WORD_SIZE (signed)(sizeof(cell) * 8)
27
28 #define TAG_MASK 15
29 #define TAG_BITS 4
30 #define TAG(x) ((cell)(x) & TAG_MASK)
31 #define UNTAG(x) ((cell)(x) & ~TAG_MASK)
32 #define RETAG(x, tag) (UNTAG(x) | (tag))
33
34 /*** Tags ***/
35 #define FIXNUM_TYPE 0
36 #define F_TYPE 1
37 #define ARRAY_TYPE 2
38 #define FLOAT_TYPE 3
39 #define QUOTATION_TYPE 4
40 #define BIGNUM_TYPE 5
41 #define ALIEN_TYPE 6
42 #define TUPLE_TYPE 7
43 #define WRAPPER_TYPE 8
44 #define BYTE_ARRAY_TYPE 9
45 #define CALLSTACK_TYPE 10
46 #define STRING_TYPE 11
47 #define WORD_TYPE 12
48 #define DLL_TYPE 13
49
50 #define TYPE_COUNT 14
51
52 static inline const char* type_name(cell type) {
53   switch (type) {
54     case FIXNUM_TYPE:
55       return "fixnum";
56     case F_TYPE:
57       return "f";
58     case ARRAY_TYPE:
59       return "array";
60     case FLOAT_TYPE:
61       return "float";
62     case QUOTATION_TYPE:
63       return "quotation";
64     case BIGNUM_TYPE:
65       return "bignum";
66     case ALIEN_TYPE:
67       return "alien";
68     case TUPLE_TYPE:
69       return "tuple";
70     case WRAPPER_TYPE:
71       return "wrapper";
72     case BYTE_ARRAY_TYPE:
73       return "byte-array";
74     case CALLSTACK_TYPE:
75       return "callstack";
76     case STRING_TYPE:
77       return "string";
78     case WORD_TYPE:
79       return "word";
80     case DLL_TYPE:
81       return "dll";
82     default:
83       FACTOR_ASSERT(false);
84       return "";
85   }
86 }
87
88 enum code_block_type {
89   code_block_unoptimized,
90   code_block_optimized,
91   code_block_pic
92 };
93
94 /* Constants used when floating-point trap exceptions are thrown */
95 enum {
96   FP_TRAP_INVALID_OPERATION = 1 << 0,
97   FP_TRAP_OVERFLOW = 1 << 1,
98   FP_TRAP_UNDERFLOW = 1 << 2,
99   FP_TRAP_ZERO_DIVIDE = 1 << 3,
100   FP_TRAP_INEXACT = 1 << 4,
101 };
102
103 /* What Factor calls 'f' */
104 static const cell false_object = F_TYPE;
105
106 inline static bool immediate_p(cell obj) {
107   /* We assume that fixnums have tag 0 and false_object has tag 1 */
108   return TAG(obj) <= F_TYPE;
109 }
110
111 inline static fixnum untag_fixnum(cell tagged) {
112 #ifdef FACTOR_DEBUG
113   FACTOR_ASSERT(TAG(tagged) == FIXNUM_TYPE);
114 #endif
115   return ((fixnum) tagged) >> TAG_BITS;
116 }
117
118 inline static cell tag_fixnum(fixnum untagged) {
119   return (untagged << TAG_BITS) | FIXNUM_TYPE;
120 }
121
122 #define NO_TYPE_CHECK static const cell type_number = TYPE_COUNT
123
124 struct object {
125   NO_TYPE_CHECK;
126   cell header;
127
128   cell size() const;
129   template <typename Fixup> cell size(Fixup fixup) const;
130
131   cell binary_payload_start() const;
132   template <typename Fixup> cell binary_payload_start(Fixup fixup) const;
133
134   cell* slots() const { return (cell*)this; }
135
136   template <typename Iterator> void each_slot(Iterator& iter);
137
138   /* Only valid for objects in tenured space; must cast to free_heap_block
139      to do anything with it if its free */
140   bool free_p() const { return (header & 1) == 1; }
141
142   cell type() const { return (header >> 2) & TAG_MASK; }
143
144   void initialize(cell type) { header = type << 2; }
145
146   cell hashcode() const { return (header >> 6); }
147
148   void set_hashcode(cell hashcode) {
149     header = (header & 0x3f) | (hashcode << 6);
150   }
151
152   bool forwarding_pointer_p() const { return (header & 2) == 2; }
153
154   object* forwarding_pointer() const { return (object*)UNTAG(header); }
155
156   void forward_to(object* pointer) { header = ((cell) pointer | 2); }
157 };
158
159 /* Assembly code makes assumptions about the layout of this struct */
160 struct array : public object {
161   static const cell type_number = ARRAY_TYPE;
162   static const cell element_size = sizeof(cell);
163   /* tagged */
164   cell capacity;
165
166   cell* data() const { return (cell*)(this + 1); }
167 };
168
169 /* These are really just arrays, but certain elements have special
170    significance */
171 struct tuple_layout : public array {
172   NO_TYPE_CHECK;
173   /* tagged */
174   cell klass;
175   /* tagged fixnum */
176   cell size;
177   /* tagged fixnum */
178   cell echelon;
179 };
180
181 struct bignum : public object {
182   static const cell type_number = BIGNUM_TYPE;
183   static const cell element_size = sizeof(cell);
184   /* tagged */
185   cell capacity;
186
187   cell* data() const { return (cell*)(this + 1); }
188 };
189
190 struct byte_array : public object {
191   static const cell type_number = BYTE_ARRAY_TYPE;
192   static const cell element_size = 1;
193   /* tagged */
194   cell capacity;
195
196 #ifndef FACTOR_64
197   cell padding0;
198   cell padding1;
199 #endif
200
201   template <typename Scalar> Scalar* data() const {
202     return (Scalar*)(this + 1);
203   }
204 };
205
206 /* Assembly code makes assumptions about the layout of this struct */
207 struct string : public object {
208   static const cell type_number = STRING_TYPE;
209   /* tagged num of chars */
210   cell length;
211   /* tagged */
212   cell aux;
213   /* tagged */
214   cell hashcode;
215
216   u8* data() const { return (u8*)(this + 1); }
217 };
218
219 struct code_block;
220
221 /* Assembly code makes assumptions about the layout of this struct:
222      basis/bootstrap/images/images.factor
223      basis/compiler/constants/constants.factor
224      core/bootstrap/primitives.factor
225 */
226 struct word : public object {
227   static const cell type_number = WORD_TYPE;
228   /* TAGGED hashcode */
229   cell hashcode;
230   /* TAGGED word name */
231   cell name;
232   /* TAGGED word vocabulary */
233   cell vocabulary;
234   /* TAGGED definition */
235   cell def;
236   /* TAGGED property assoc for library code */
237   cell props;
238   /* TAGGED alternative entry point for direct non-tail calls. Used for inline
239    * caching */
240   cell pic_def;
241   /* TAGGED alternative entry point for direct tail calls. Used for inline
242    * caching */
243   cell pic_tail_def;
244   /* TAGGED machine code for sub-primitive */
245   cell subprimitive;
246   /* UNTAGGED entry point: jump here to execute word */
247   void* entry_point;
248   /* UNTAGGED compiled code block */
249
250   /* defined in code_blocks.hpp */
251   code_block* code() const;
252 };
253
254 /* Assembly code makes assumptions about the layout of this struct */
255 struct wrapper : public object {
256   static const cell type_number = WRAPPER_TYPE;
257   cell object;
258 };
259
260 /* Assembly code makes assumptions about the layout of this struct */
261 struct boxed_float : object {
262   static const cell type_number = FLOAT_TYPE;
263
264 #ifndef FACTOR_64
265   cell padding;
266 #endif
267
268   double n;
269 };
270
271 /* Assembly code makes assumptions about the layout of this struct:
272      basis/bootstrap/images/images.factor
273      basis/compiler/constants/constants.factor
274      core/bootstrap/primitives.factor
275 */
276 struct quotation : public object {
277   static const cell type_number = QUOTATION_TYPE;
278   /* tagged */
279   cell array;
280   /* tagged */
281   cell cached_effect;
282   /* tagged */
283   cell cache_counter;
284   /* UNTAGGED entry point; jump here to call quotation */
285   void* entry_point;
286
287   /* defined in code_blocks.hpp */
288   code_block* code() const;
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 base;
296   /* tagged */
297   cell expired;
298   /* untagged */
299   cell displacement;
300   /* untagged */
301   cell address;
302
303   void update_address() {
304     if (base == false_object)
305       address = displacement;
306     else
307       address = UNTAG(base) + sizeof(byte_array) + displacement;
308   }
309 };
310
311 struct dll : public object {
312   static const cell type_number = DLL_TYPE;
313   /* tagged byte array holding a C string */
314   cell path;
315   /* OS-specific handle */
316   void* handle;
317 };
318
319 struct callstack : public object {
320   static const cell type_number = CALLSTACK_TYPE;
321   /* tagged */
322   cell length;
323
324   void* frame_top_at(cell offset) const {
325     return (void*)((char*)(this + 1) + offset);
326   }
327
328   void* top() const { return (void*)(this + 1); }
329   void* bottom() const {
330     return (void*)((cell)(this + 1) + untag_fixnum(length));
331   }
332 };
333
334 struct tuple : public object {
335   static const cell type_number = TUPLE_TYPE;
336   /* tagged layout */
337   cell layout;
338
339   cell* data() const { return (cell*)(this + 1); }
340 };
341
342 struct data_root_range {
343   cell* start;
344   cell len;
345
346   explicit data_root_range(cell* start_, cell len_)
347       : start(start_), len(len_) {}
348 };
349
350 }