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 basis/bootstrap/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 // basis/bootstrap/layouts.factor
44 static inline const char* type_name(cell type) {
45 static const char* const type_names[]={
62 if (type>=TYPE_COUNT) {
66 return type_names[type];
69 enum code_block_type {
70 CODE_BLOCK_UNOPTIMIZED,
75 // Constants used when floating-point trap exceptions are thrown
77 FP_TRAP_INVALID_OPERATION = 1 << 0,
78 FP_TRAP_OVERFLOW = 1 << 1,
79 FP_TRAP_UNDERFLOW = 1 << 2,
80 FP_TRAP_ZERO_DIVIDE = 1 << 3,
81 FP_TRAP_INEXACT = 1 << 4,
84 // What Factor calls 'f'
85 static const cell false_object = F_TYPE;
87 inline static bool immediate_p(cell obj) {
88 // We assume that fixnums have tag 0 and false_object has tag 1
89 return TAG(obj) <= F_TYPE;
92 inline static fixnum untag_fixnum(cell tagged) {
93 FACTOR_ASSERT(TAG(tagged) == FIXNUM_TYPE);
94 return ((fixnum)tagged) >> TAG_BITS;
97 inline static cell tag_fixnum(fixnum untagged) {
98 return ( (cell)untagged << TAG_BITS) | FIXNUM_TYPE;
101 #define NO_TYPE_CHECK static const cell type_number = TYPE_COUNT
105 // header format (bits indexed with least significant as zero):
107 // bit 1 : forwarding pointer?
108 // if not forwarding:
110 // bit 7-end : hashcode
112 // bit 2-end : forwarding pointer
115 template <typename Fixup> cell base_size(Fixup fixup) const;
116 template <typename Fixup> cell size(Fixup fixup) const;
119 cell slot_count() const;
120 template <typename Fixup> cell slot_count(Fixup fixup) const;
122 cell* slots() const { return (cell*)this; }
124 template <typename Iterator> void each_slot(Iterator& iter);
126 // Only valid for objects in tenured space; must cast to free_heap_block
127 // to do anything with it if its free
128 bool free_p() const { return (header & 1) == 1; }
130 cell type() const { return (header >> 2) & TAG_MASK; }
132 void initialize(cell type) { header = type << 2; }
134 cell hashcode() const { return (header >> 6); }
136 void set_hashcode(cell hashcode) {
137 header = (header & 0x3f) | (hashcode << 6);
140 bool forwarding_pointer_p() const { return (header & 2) == 2; }
142 object* forwarding_pointer() const { return (object*)UNTAG(header); }
144 void forward_to(object* pointer) { header = ((cell)pointer | 2); }
147 // Assembly code makes assumptions about the layout of this struct
148 struct array : public object {
149 static const cell type_number = ARRAY_TYPE;
150 static const cell element_size = sizeof(cell);
154 cell* data() const { return (cell*)(this + 1); }
157 // These are really just arrays, but certain elements have special
159 struct tuple_layout : public array {
169 struct bignum : public object {
170 static const cell type_number = BIGNUM_TYPE;
171 static const cell element_size = sizeof(cell);
175 cell* data() const { return (cell*)(this + 1); }
178 struct byte_array : public object {
179 static const cell type_number = BYTE_ARRAY_TYPE;
180 static const cell element_size = 1;
189 template <typename Scalar> Scalar* data() const {
190 return (Scalar*)(this + 1);
194 // Assembly code makes assumptions about the layout of this struct
195 struct string : public object {
196 static const cell type_number = STRING_TYPE;
197 // tagged num of chars
204 uint8_t* data() const { return (uint8_t*)(this + 1); }
209 // Assembly code makes assumptions about the layout of this struct:
210 // basis/bootstrap/images/images.factor
211 // basis/compiler/constants/constants.factor
212 // core/bootstrap/primitives.factor
214 struct word : public object {
215 static const cell type_number = WORD_TYPE;
220 // TAGGED word vocabulary
224 // TAGGED property assoc for library code
226 // TAGGED alternative entry point for direct non-tail calls. Used for inline
229 // TAGGED alternative entry point for direct tail calls. Used for inline
232 // TAGGED machine code for sub-primitive
234 // UNTAGGED entry point: jump here to execute word
236 // UNTAGGED compiled code block
238 // defined in code_blocks.hpp
239 code_block* code() const;
242 // Assembly code makes assumptions about the layout of this struct
243 struct wrapper : public object {
244 static const cell type_number = WRAPPER_TYPE;
248 // Assembly code makes assumptions about the layout of this struct
249 struct boxed_float : object {
250 static const cell type_number = FLOAT_TYPE;
259 // Assembly code makes assumptions about the layout of this struct:
260 // basis/bootstrap/images/images.factor
261 // basis/compiler/constants/constants.factor
262 // core/bootstrap/primitives.factor
264 struct quotation : public object {
265 static const cell type_number = QUOTATION_TYPE;
272 // UNTAGGED entry point; jump here to call quotation
275 // defined in code_blocks.hpp
276 code_block* code() const;
279 // Assembly code makes assumptions about the layout of this struct
280 struct alien : public object {
281 static const cell type_number = ALIEN_TYPE;
291 void update_address() {
292 if (base == false_object)
293 address = displacement;
295 address = UNTAG(base) + sizeof(byte_array) + displacement;
299 struct dll : public object {
300 static const cell type_number = DLL_TYPE;
301 // tagged byte array holding a C string
303 // OS-specific handle
307 struct callstack : public object {
308 static const cell type_number = CALLSTACK_TYPE;
312 cell frame_top_at(cell offset) const {
313 return (cell)(this + 1) + offset;
316 void* top() const { return (void*)(this + 1); }
317 void* bottom() const {
318 return (void*)((cell)(this + 1) + untag_fixnum(length));
322 struct tuple : public object {
323 static const cell type_number = TUPLE_TYPE;
327 cell* data() const { return (cell*)(this + 1); }
330 inline static cell tuple_capacity(const tuple_layout *layout) {
331 return untag_fixnum(layout->size);
334 inline static cell tuple_size(const tuple_layout* layout) {
335 return sizeof(tuple) + tuple_capacity(layout) * sizeof(cell);
338 inline static cell string_capacity(const string* str) {
339 return untag_fixnum(str->length);
342 inline static cell string_size(cell size) { return sizeof(string) + size; }