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