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