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