3 typedef intptr_t fixnum;
4 typedef uintptr_t cell;
6 inline static cell align(cell a, cell b) { return (a + (b - 1)) & ~(b - 1); }
8 inline static cell alignment_for(cell a, cell b) { return align(a, b) - a; }
10 static const cell data_alignment = 16;
12 // Must match leaf-stack-frame-size in core/layouts/layouts.factor
13 #define LEAF_FRAME_SIZE 16
15 #define WORD_SIZE (signed)(sizeof(cell) * 8)
19 #define TAG(x) ((cell)(x) & TAG_MASK)
20 #define UNTAG(x) ((cell)(x) & ~TAG_MASK)
21 #define RETAG(x, tag) (UNTAG(x) | (tag))
23 // Type tags, should be kept in sync with:
24 // core/bootstrap/layouts/layouts.factor
29 #define QUOTATION_TYPE 4
33 #define WRAPPER_TYPE 8
34 #define BYTE_ARRAY_TYPE 9
35 #define CALLSTACK_TYPE 10
36 #define STRING_TYPE 11
42 static inline const char* type_name(cell type) {
78 enum code_block_type {
79 CODE_BLOCK_UNOPTIMIZED,
84 // Constants used when floating-point trap exceptions are thrown
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,
93 // What Factor calls 'f'
94 static const cell false_object = F_TYPE;
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;
101 inline static fixnum untag_fixnum(cell tagged) {
102 FACTOR_ASSERT(TAG(tagged) == FIXNUM_TYPE);
103 return ((fixnum)tagged) >> TAG_BITS;
106 inline static cell tag_fixnum(fixnum untagged) {
107 return ( (cell)untagged << TAG_BITS) | FIXNUM_TYPE;
110 #define NO_TYPE_CHECK static const cell type_number = TYPE_COUNT
114 // header format (bits indexed with least significant as zero):
116 // bit 1 : forwarding pointer?
117 // if not forwarding:
119 // bit 7-end : hashcode
121 // bit 2-end : forwarding pointer
124 template <typename Fixup> cell base_size(Fixup fixup) const;
125 template <typename Fixup> cell size(Fixup fixup) const;
128 cell slot_count() const;
129 template <typename Fixup> cell slot_count(Fixup fixup) const;
131 cell* slots() const { return (cell*)this; }
133 template <typename Iterator> void each_slot(Iterator& iter);
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; }
139 cell type() const { return (header >> 2) & TAG_MASK; }
141 void initialize(cell type) { header = type << 2; }
143 cell hashcode() const { return (header >> 6); }
145 void set_hashcode(cell hashcode) {
146 header = (header & 0x3f) | (hashcode << 6);
149 bool forwarding_pointer_p() const { return (header & 2) == 2; }
151 object* forwarding_pointer() const { return (object*)UNTAG(header); }
153 void forward_to(object* pointer) { header = ((cell)pointer | 2); }
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);
163 cell* data() const { return (cell*)(this + 1); }
166 // These are really just arrays, but certain elements have special
168 struct tuple_layout : public array {
178 struct bignum : public object {
179 static const cell type_number = BIGNUM_TYPE;
180 static const cell element_size = sizeof(cell);
184 cell* data() const { return (cell*)(this + 1); }
187 struct byte_array : public object {
188 static const cell type_number = BYTE_ARRAY_TYPE;
189 static const cell element_size = 1;
198 template <typename Scalar> Scalar* data() const {
199 return (Scalar*)(this + 1);
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
213 uint8_t* data() const { return (uint8_t*)(this + 1); }
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
223 struct word : public object {
224 static const cell type_number = WORD_TYPE;
229 // TAGGED word vocabulary
233 // TAGGED property assoc for library code
235 // TAGGED alternative entry point for direct non-tail calls. Used for inline
238 // TAGGED alternative entry point for direct tail calls. Used for inline
241 // TAGGED machine code for sub-primitive
243 // UNTAGGED entry point: jump here to execute word
245 // UNTAGGED compiled code block
247 // defined in code_blocks.hpp
248 code_block* code() const;
251 // Assembly code makes assumptions about the layout of this struct
252 struct wrapper : public object {
253 static const cell type_number = WRAPPER_TYPE;
257 // Assembly code makes assumptions about the layout of this struct
258 struct boxed_float : object {
259 static const cell type_number = FLOAT_TYPE;
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
273 struct quotation : public object {
274 static const cell type_number = QUOTATION_TYPE;
281 // UNTAGGED entry point; jump here to call quotation
284 // defined in code_blocks.hpp
285 code_block* code() const;
288 // Assembly code makes assumptions about the layout of this struct
289 struct alien : public object {
290 static const cell type_number = ALIEN_TYPE;
300 void update_address() {
301 if (base == false_object)
302 address = displacement;
304 address = UNTAG(base) + sizeof(byte_array) + displacement;
308 struct dll : public object {
309 static const cell type_number = DLL_TYPE;
310 // tagged byte array holding a C string
312 // OS-specific handle
316 struct callstack : public object {
317 static const cell type_number = CALLSTACK_TYPE;
321 cell frame_top_at(cell offset) const {
322 return (cell)(this + 1) + offset;
325 void* top() const { return (void*)(this + 1); }
326 void* bottom() const {
327 return (void*)((cell)(this + 1) + untag_fixnum(length));
331 struct tuple : public object {
332 static const cell type_number = TUPLE_TYPE;
336 cell* data() const { return (cell*)(this + 1); }
339 inline static cell tuple_capacity(const tuple_layout *layout) {
340 return untag_fixnum(layout->size);
343 inline static cell tuple_size(const tuple_layout* layout) {
344 return sizeof(tuple) + tuple_capacity(layout) * sizeof(cell);
347 inline static cell string_capacity(const string* str) {
348 return untag_fixnum(str->length);
351 inline static cell string_size(cell size) { return sizeof(string) + size; }