]> gitweb.factorcode.org Git - factor.git/blob - vm/layouts.hpp
VM: merge strings.hpp and tuples.hpp into layouts.hpp, because those
[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   // header format (bits indexed with least significant as zero):
111   // bit 0      : free?
112   // if not forwarding:
113   //   bit 1      : forwarding pointer?
114   //   bit 2-5    : tag
115   //   bit 7-end  : hashcode
116   // if forwarding:
117   //   bit 2-end  : forwarding pointer
118   cell header;
119
120   template <typename Fixup> cell base_size(Fixup fixup) const;
121   template <typename Fixup> cell size(Fixup fixup) const;
122   cell size() const;
123
124   cell binary_payload_start() const;
125   template <typename Fixup> cell binary_payload_start(Fixup fixup) const;
126
127   cell* slots() const { return (cell*)this; }
128
129   template <typename Iterator> void each_slot(Iterator& iter);
130
131   /* Only valid for objects in tenured space; must cast to free_heap_block
132      to do anything with it if its free */
133   bool free_p() const { return (header & 1) == 1; }
134
135   cell type() const { return (header >> 2) & TAG_MASK; }
136
137   void initialize(cell type) { header = type << 2; }
138
139   cell hashcode() const { return (header >> 6); }
140
141   void set_hashcode(cell hashcode) {
142     header = (header & 0x3f) | (hashcode << 6);
143   }
144
145   bool forwarding_pointer_p() const { return (header & 2) == 2; }
146
147   object* forwarding_pointer() const { return (object*)UNTAG(header); }
148
149   void forward_to(object* pointer) { header = ((cell)pointer | 2); }
150 };
151
152 /* Assembly code makes assumptions about the layout of this struct */
153 struct array : public object {
154   static const cell type_number = ARRAY_TYPE;
155   static const cell element_size = sizeof(cell);
156   /* tagged */
157   cell capacity;
158
159   cell* data() const { return (cell*)(this + 1); }
160 };
161
162 /* These are really just arrays, but certain elements have special
163    significance */
164 struct tuple_layout : public array {
165   NO_TYPE_CHECK;
166   /* tagged */
167   cell klass;
168   /* tagged fixnum */
169   cell size;
170   /* tagged fixnum */
171   cell echelon;
172 };
173
174 struct bignum : public object {
175   static const cell type_number = BIGNUM_TYPE;
176   static const cell element_size = sizeof(cell);
177   /* tagged */
178   cell capacity;
179
180   cell* data() const { return (cell*)(this + 1); }
181 };
182
183 struct byte_array : public object {
184   static const cell type_number = BYTE_ARRAY_TYPE;
185   static const cell element_size = 1;
186   /* tagged */
187   cell capacity;
188
189 #ifndef FACTOR_64
190   cell padding0;
191   cell padding1;
192 #endif
193
194   template <typename Scalar> Scalar* data() const {
195     return (Scalar*)(this + 1);
196   }
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   uint8_t* data() const { return (uint8_t*)(this + 1); }
210 };
211
212 struct code_block;
213
214 /* Assembly code makes assumptions about the layout of this struct:
215      basis/bootstrap/images/images.factor
216      basis/compiler/constants/constants.factor
217      core/bootstrap/primitives.factor
218 */
219 struct word : public object {
220   static const cell type_number = WORD_TYPE;
221   /* TAGGED hashcode */
222   cell hashcode;
223   /* TAGGED word name */
224   cell name;
225   /* TAGGED word vocabulary */
226   cell vocabulary;
227   /* TAGGED definition */
228   cell def;
229   /* TAGGED property assoc for library code */
230   cell props;
231   /* TAGGED alternative entry point for direct non-tail calls. Used for inline
232    * caching */
233   cell pic_def;
234   /* TAGGED alternative entry point for direct tail calls. Used for inline
235    * caching */
236   cell pic_tail_def;
237   /* TAGGED machine code for sub-primitive */
238   cell subprimitive;
239   /* UNTAGGED entry point: jump here to execute word */
240   cell entry_point;
241   /* UNTAGGED compiled code block */
242
243   /* defined in code_blocks.hpp */
244   code_block* code() const;
245 };
246
247 /* Assembly code makes assumptions about the layout of this struct */
248 struct wrapper : public object {
249   static const cell type_number = WRAPPER_TYPE;
250   cell object;
251 };
252
253 /* Assembly code makes assumptions about the layout of this struct */
254 struct boxed_float : object {
255   static const cell type_number = FLOAT_TYPE;
256
257 #ifndef FACTOR_64
258   cell padding;
259 #endif
260
261   double n;
262 };
263
264 /* Assembly code makes assumptions about the layout of this struct:
265      basis/bootstrap/images/images.factor
266      basis/compiler/constants/constants.factor
267      core/bootstrap/primitives.factor
268 */
269 struct quotation : public object {
270   static const cell type_number = QUOTATION_TYPE;
271   /* tagged */
272   cell array;
273   /* tagged */
274   cell cached_effect;
275   /* tagged */
276   cell cache_counter;
277   /* UNTAGGED entry point; jump here to call quotation */
278   cell entry_point;
279
280   /* defined in code_blocks.hpp */
281   code_block* code() const;
282 };
283
284 /* Assembly code makes assumptions about the layout of this struct */
285 struct alien : public object {
286   static const cell type_number = ALIEN_TYPE;
287   /* tagged */
288   cell base;
289   /* tagged */
290   cell expired;
291   /* untagged */
292   cell displacement;
293   /* untagged */
294   cell address;
295
296   void update_address() {
297     if (base == false_object)
298       address = displacement;
299     else
300       address = UNTAG(base) + sizeof(byte_array) + displacement;
301   }
302 };
303
304 struct dll : public object {
305   static const cell type_number = DLL_TYPE;
306   /* tagged byte array holding a C string */
307   cell path;
308   /* OS-specific handle */
309   void* handle;
310 };
311
312 struct callstack : public object {
313   static const cell type_number = CALLSTACK_TYPE;
314   /* tagged */
315   cell length;
316
317   cell frame_top_at(cell offset) const {
318     return (cell)(this + 1) + offset;
319   }
320
321   void* top() const { return (void*)(this + 1); }
322   void* bottom() const {
323     return (void*)((cell)(this + 1) + untag_fixnum(length));
324   }
325 };
326
327 struct tuple : public object {
328   static const cell type_number = TUPLE_TYPE;
329   /* tagged layout */
330   cell layout;
331
332   cell* data() const { return (cell*)(this + 1); }
333 };
334
335 inline static cell tuple_size(const tuple_layout* layout) {
336   cell size = untag_fixnum(layout->size);
337   return sizeof(tuple) + size * sizeof(cell);
338 }
339
340 inline static cell string_capacity(const string* str) {
341   return untag_fixnum(str->length);
342 }
343
344 inline static cell string_size(cell size) { return sizeof(string) + size; }
345
346 }