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 #define WORD_SIZE (signed)(sizeof(cell) * 8)
16 #define TAG(x) ((cell)(x) & TAG_MASK)
17 #define UNTAG(x) ((cell)(x) & ~TAG_MASK)
18 #define RETAG(x, tag) (UNTAG(x) | (tag))
25 #define QUOTATION_TYPE 4
29 #define WRAPPER_TYPE 8
30 #define BYTE_ARRAY_TYPE 9
31 #define CALLSTACK_TYPE 10
32 #define STRING_TYPE 11
38 static inline const char* type_name(cell type) {
74 enum code_block_type {
75 code_block_unoptimized,
80 /* Constants used when floating-point trap exceptions are thrown */
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,
89 /* What Factor calls 'f' */
90 static const cell false_object = F_TYPE;
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;
97 inline static fixnum untag_fixnum(cell tagged) {
98 FACTOR_ASSERT(TAG(tagged) == FIXNUM_TYPE);
99 return ((fixnum)tagged) >> TAG_BITS;
102 inline static cell tag_fixnum(fixnum untagged) {
103 return (untagged << TAG_BITS) | FIXNUM_TYPE;
106 #define NO_TYPE_CHECK static const cell type_number = TYPE_COUNT
110 // header format (bits indexed with least significant as zero):
112 // if not forwarding:
113 // bit 1 : forwarding pointer?
115 // bit 7-end : hashcode
117 // bit 2-end : forwarding pointer
120 template <typename Fixup> cell base_size(Fixup fixup) const;
121 template <typename Fixup> cell size(Fixup fixup) const;
124 cell binary_payload_start() const;
125 template <typename Fixup> cell binary_payload_start(Fixup fixup) const;
127 cell* slots() const { return (cell*)this; }
129 template <typename Iterator> void each_slot(Iterator& iter);
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; }
135 cell type() const { return (header >> 2) & TAG_MASK; }
137 void initialize(cell type) { header = type << 2; }
139 cell hashcode() const { return (header >> 6); }
141 void set_hashcode(cell hashcode) {
142 header = (header & 0x3f) | (hashcode << 6);
145 bool forwarding_pointer_p() const { return (header & 2) == 2; }
147 object* forwarding_pointer() const { return (object*)UNTAG(header); }
149 void forward_to(object* pointer) { header = ((cell)pointer | 2); }
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);
159 cell* data() const { return (cell*)(this + 1); }
162 /* These are really just arrays, but certain elements have special
164 struct tuple_layout : public array {
174 struct bignum : public object {
175 static const cell type_number = BIGNUM_TYPE;
176 static const cell element_size = sizeof(cell);
180 cell* data() const { return (cell*)(this + 1); }
183 struct byte_array : public object {
184 static const cell type_number = BYTE_ARRAY_TYPE;
185 static const cell element_size = 1;
194 template <typename Scalar> Scalar* data() const {
195 return (Scalar*)(this + 1);
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 */
209 uint8_t* data() const { return (uint8_t*)(this + 1); }
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
219 struct word : public object {
220 static const cell type_number = WORD_TYPE;
221 /* TAGGED hashcode */
223 /* TAGGED word name */
225 /* TAGGED word vocabulary */
227 /* TAGGED definition */
229 /* TAGGED property assoc for library code */
231 /* TAGGED alternative entry point for direct non-tail calls. Used for inline
234 /* TAGGED alternative entry point for direct tail calls. Used for inline
237 /* TAGGED machine code for sub-primitive */
239 /* UNTAGGED entry point: jump here to execute word */
241 /* UNTAGGED compiled code block */
243 /* defined in code_blocks.hpp */
244 code_block* code() const;
247 /* Assembly code makes assumptions about the layout of this struct */
248 struct wrapper : public object {
249 static const cell type_number = WRAPPER_TYPE;
253 /* Assembly code makes assumptions about the layout of this struct */
254 struct boxed_float : object {
255 static const cell type_number = FLOAT_TYPE;
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
269 struct quotation : public object {
270 static const cell type_number = QUOTATION_TYPE;
277 /* UNTAGGED entry point; jump here to call quotation */
280 /* defined in code_blocks.hpp */
281 code_block* code() const;
284 /* Assembly code makes assumptions about the layout of this struct */
285 struct alien : public object {
286 static const cell type_number = ALIEN_TYPE;
296 void update_address() {
297 if (base == false_object)
298 address = displacement;
300 address = UNTAG(base) + sizeof(byte_array) + displacement;
304 struct dll : public object {
305 static const cell type_number = DLL_TYPE;
306 /* tagged byte array holding a C string */
308 /* OS-specific handle */
312 struct callstack : public object {
313 static const cell type_number = CALLSTACK_TYPE;
317 cell frame_top_at(cell offset) const {
318 return (cell)(this + 1) + offset;
321 void* top() const { return (void*)(this + 1); }
322 void* bottom() const {
323 return (void*)((cell)(this + 1) + untag_fixnum(length));
327 struct tuple : public object {
328 static const cell type_number = TUPLE_TYPE;
332 cell* data() const { return (cell*)(this + 1); }
335 inline static cell tuple_size(const tuple_layout* layout) {
336 cell size = untag_fixnum(layout->size);
337 return sizeof(tuple) + size * sizeof(cell);
340 inline static cell string_capacity(const string* str) {
341 return untag_fixnum(str->length);
344 inline static cell string_size(cell size) { return sizeof(string) + size; }