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))
28 #define QUOTATION_TYPE 4
32 #define WRAPPER_TYPE 8
33 #define BYTE_ARRAY_TYPE 9
34 #define CALLSTACK_TYPE 10
35 #define STRING_TYPE 11
41 static inline const char* type_name(cell type) {
77 enum code_block_type {
78 code_block_unoptimized,
83 /* Constants used when floating-point trap exceptions are thrown */
85 FP_TRAP_INVALID_OPERATION = 1 << 0,
86 FP_TRAP_OVERFLOW = 1 << 1,
87 FP_TRAP_UNDERFLOW = 1 << 2,
88 FP_TRAP_ZERO_DIVIDE = 1 << 3,
89 FP_TRAP_INEXACT = 1 << 4,
92 /* What Factor calls 'f' */
93 static const cell false_object = F_TYPE;
95 inline static bool immediate_p(cell obj) {
96 /* We assume that fixnums have tag 0 and false_object has tag 1 */
97 return TAG(obj) <= F_TYPE;
100 inline static fixnum untag_fixnum(cell tagged) {
101 FACTOR_ASSERT(TAG(tagged) == FIXNUM_TYPE);
102 return ((fixnum)tagged) >> TAG_BITS;
105 inline static cell tag_fixnum(fixnum untagged) {
106 return (untagged << TAG_BITS) | FIXNUM_TYPE;
109 #define NO_TYPE_CHECK static const cell type_number = TYPE_COUNT
113 // header format (bits indexed with least significant as zero):
115 // if not forwarding:
116 // bit 1 : forwarding pointer?
118 // bit 7-end : hashcode
120 // bit 2-end : forwarding pointer
123 template <typename Fixup> cell base_size(Fixup fixup) const;
124 template <typename Fixup> cell size(Fixup fixup) const;
127 cell slot_count() const;
128 template <typename Fixup> cell slot_count(Fixup fixup) const;
130 cell* slots() const { return (cell*)this; }
132 template <typename Iterator> void each_slot(Iterator& iter);
134 /* Only valid for objects in tenured space; must cast to free_heap_block
135 to do anything with it if its free */
136 bool free_p() const { return (header & 1) == 1; }
138 cell type() const { return (header >> 2) & TAG_MASK; }
140 void initialize(cell type) { header = type << 2; }
142 cell hashcode() const { return (header >> 6); }
144 void set_hashcode(cell hashcode) {
145 header = (header & 0x3f) | (hashcode << 6);
148 bool forwarding_pointer_p() const { return (header & 2) == 2; }
150 object* forwarding_pointer() const { return (object*)UNTAG(header); }
152 void forward_to(object* pointer) { header = ((cell)pointer | 2); }
155 /* Assembly code makes assumptions about the layout of this struct */
156 struct array : public object {
157 static const cell type_number = ARRAY_TYPE;
158 static const cell element_size = sizeof(cell);
162 cell* data() const { return (cell*)(this + 1); }
165 /* These are really just arrays, but certain elements have special
167 struct tuple_layout : public array {
177 struct bignum : public object {
178 static const cell type_number = BIGNUM_TYPE;
179 static const cell element_size = sizeof(cell);
183 cell* data() const { return (cell*)(this + 1); }
186 struct byte_array : public object {
187 static const cell type_number = BYTE_ARRAY_TYPE;
188 static const cell element_size = 1;
197 template <typename Scalar> Scalar* data() const {
198 return (Scalar*)(this + 1);
202 /* Assembly code makes assumptions about the layout of this struct */
203 struct string : public object {
204 static const cell type_number = STRING_TYPE;
205 /* tagged num of chars */
212 uint8_t* data() const { return (uint8_t*)(this + 1); }
217 /* Assembly code makes assumptions about the layout of this struct:
218 basis/bootstrap/images/images.factor
219 basis/compiler/constants/constants.factor
220 core/bootstrap/primitives.factor
222 struct word : public object {
223 static const cell type_number = WORD_TYPE;
224 /* TAGGED hashcode */
226 /* TAGGED word name */
228 /* TAGGED word vocabulary */
230 /* TAGGED definition */
232 /* TAGGED property assoc for library code */
234 /* TAGGED alternative entry point for direct non-tail calls. Used for inline
237 /* TAGGED alternative entry point for direct tail calls. Used for inline
240 /* TAGGED machine code for sub-primitive */
242 /* UNTAGGED entry point: jump here to execute word */
244 /* UNTAGGED compiled code block */
246 /* defined in code_blocks.hpp */
247 code_block* code() const;
250 /* Assembly code makes assumptions about the layout of this struct */
251 struct wrapper : public object {
252 static const cell type_number = WRAPPER_TYPE;
256 /* Assembly code makes assumptions about the layout of this struct */
257 struct boxed_float : object {
258 static const cell type_number = FLOAT_TYPE;
267 /* Assembly code makes assumptions about the layout of this struct:
268 basis/bootstrap/images/images.factor
269 basis/compiler/constants/constants.factor
270 core/bootstrap/primitives.factor
272 struct quotation : public object {
273 static const cell type_number = QUOTATION_TYPE;
280 /* UNTAGGED entry point; jump here to call quotation */
283 /* defined in code_blocks.hpp */
284 code_block* code() const;
287 /* Assembly code makes assumptions about the layout of this struct */
288 struct alien : public object {
289 static const cell type_number = ALIEN_TYPE;
299 void update_address() {
300 if (base == false_object)
301 address = displacement;
303 address = UNTAG(base) + sizeof(byte_array) + displacement;
307 struct dll : public object {
308 static const cell type_number = DLL_TYPE;
309 /* tagged byte array holding a C string */
311 /* OS-specific handle */
315 struct callstack : public object {
316 static const cell type_number = CALLSTACK_TYPE;
320 cell frame_top_at(cell offset) const {
321 return (cell)(this + 1) + offset;
324 void* top() const { return (void*)(this + 1); }
325 void* bottom() const {
326 return (void*)((cell)(this + 1) + untag_fixnum(length));
330 struct tuple : public object {
331 static const cell type_number = TUPLE_TYPE;
335 cell* data() const { return (cell*)(this + 1); }
338 inline static cell tuple_capacity(const tuple_layout *layout) {
339 return untag_fixnum(layout->size);
342 inline static cell tuple_size(const tuple_layout* layout) {
343 return sizeof(tuple) + tuple_capacity(layout) * sizeof(cell);
346 inline static cell string_capacity(const string* str) {
347 return untag_fixnum(str->length);
350 inline static cell string_size(cell size) { return sizeof(string) + size; }