4 * Copyright (C) 2008 Vincent Geddes
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #ifndef __ST_OBJECT_H__
26 #define __ST_OBJECT_H__
29 #include <st-small-integer.h>
31 #include <st-universe.h>
33 #define ST_HEADER(oop) ((struct st_header *) st_detag_pointer (oop))
34 #define ST_OBJECT_MARK(oop) (ST_HEADER (oop)->mark)
35 #define ST_OBJECT_CLASS(oop) (ST_HEADER (oop)->class)
36 #define ST_OBJECT_FIELDS(oop) (ST_HEADER (oop)->fields)
38 /* Every heap-allocated object starts with this header word */
40 * [ unused: 16 | instance-size: 8 | format: 6 | tag: 2 ]
43 * format: object format
44 * mark: object contains a forwarding pointer
45 * unused: not used yet (haven't implemented GC)
55 #define _ST_OBJECT_SET_BITFIELD(bitfield, field, value) \
56 ((bitfield) = (((bitfield) & ~(_ST_OBJECT_##field##_MASK << _ST_OBJECT_##field##_SHIFT)) \
57 | (((value) & _ST_OBJECT_##field##_MASK) << _ST_OBJECT_##field##_SHIFT)))
59 #define _ST_OBJECT_GET_BITFIELD(bitfield, field) \
60 (((bitfield) >> _ST_OBJECT_##field##_SHIFT) & _ST_OBJECT_##field##_MASK)
64 _ST_OBJECT_UNUSED_BITS
= 15,
65 _ST_OBJECT_HASH_BITS
= 1,
66 _ST_OBJECT_SIZE_BITS
= 8,
67 _ST_OBJECT_FORMAT_BITS
= 6,
69 _ST_OBJECT_FORMAT_SHIFT
= ST_TAG_SIZE
,
70 _ST_OBJECT_SIZE_SHIFT
= _ST_OBJECT_FORMAT_BITS
+ _ST_OBJECT_FORMAT_SHIFT
,
71 _ST_OBJECT_HASH_SHIFT
= _ST_OBJECT_SIZE_BITS
+ _ST_OBJECT_SIZE_SHIFT
,
72 _ST_OBJECT_UNUSED_SHIFT
= _ST_OBJECT_HASH_BITS
+ _ST_OBJECT_HASH_SHIFT
,
74 _ST_OBJECT_FORMAT_MASK
= ST_NTH_MASK (_ST_OBJECT_FORMAT_BITS
),
75 _ST_OBJECT_SIZE_MASK
= ST_NTH_MASK (_ST_OBJECT_SIZE_BITS
),
76 _ST_OBJECT_HASH_MASK
= ST_NTH_MASK (_ST_OBJECT_HASH_BITS
),
77 _ST_OBJECT_UNUSED_MASK
= ST_NTH_MASK (_ST_OBJECT_UNUSED_BITS
),
80 /* Make sure to update all cased code in VM when adding a new format */
81 typedef enum st_format
85 ST_FORMAT_LARGE_INTEGER
,
89 ST_FORMAT_FLOAT_ARRAY
,
90 ST_FORMAT_INTEGER_ARRAY
,
96 st_oop
st_object_allocate (st_oop
class);
97 void st_object_initialize_header (st_oop object
, st_oop
class);
98 bool st_object_equal (st_oop object
, st_oop other
);
99 st_uint
st_object_hash (st_oop object
);
102 st_object_set_format (st_oop object
, st_format format
)
104 _ST_OBJECT_SET_BITFIELD (ST_OBJECT_MARK (object
), FORMAT
, format
);
107 static inline st_format
108 st_object_format (st_oop object
)
110 return _ST_OBJECT_GET_BITFIELD (ST_OBJECT_MARK (object
), FORMAT
);
114 st_object_set_hashed (st_oop object
, bool hashed
)
116 _ST_OBJECT_SET_BITFIELD (ST_OBJECT_MARK (object
), HASH
, hashed
);
120 st_object_is_hashed (st_oop object
)
122 return _ST_OBJECT_GET_BITFIELD (ST_OBJECT_MARK (object
), HASH
);
126 static inline st_uint
127 st_object_instance_size (st_oop object
)
129 return _ST_OBJECT_GET_BITFIELD (ST_OBJECT_MARK (object
), SIZE
);
132 static inline st_uint
133 st_object_set_instance_size (st_oop object
, st_uint size
)
135 _ST_OBJECT_SET_BITFIELD (ST_OBJECT_MARK (object
), SIZE
, size
);
139 st_object_tag (st_oop object
)
141 return object
& st_tag_mask
;
145 st_object_is_heap (st_oop object
)
147 return st_object_tag (object
) == ST_POINTER_TAG
;
151 st_object_is_smi (st_oop object
)
153 return st_object_tag (object
) == ST_SMI_TAG
;
157 st_object_is_character (st_oop object
)
159 return st_object_tag (object
) == ST_CHARACTER_TAG
;
163 st_object_is_mark (st_oop object
)
165 return st_object_tag (object
) == ST_MARK_TAG
;
169 st_object_class (st_oop object
)
171 if (ST_UNLIKELY (st_object_is_smi (object
)))
174 if (ST_UNLIKELY (st_object_is_character (object
)))
175 return ST_CHARACTER_CLASS
;
177 return ST_OBJECT_CLASS (object
);
181 st_object_is_symbol (st_oop object
)
183 return st_object_class (object
) == ST_SYMBOL_CLASS
;
187 st_object_is_string (st_oop object
)
189 return st_object_class (object
) == ST_STRING_CLASS
;
193 st_object_is_array (st_oop object
)
195 return st_object_class (object
) == ST_ARRAY_CLASS
;
199 st_object_is_byte_array (st_oop object
)
201 return st_object_class (object
) == ST_BYTE_ARRAY_CLASS
;
205 st_object_is_float (st_oop object
)
207 return st_object_class (object
) == ST_FLOAT_CLASS
;
210 #endif /* __ST_OBJECT_H__ */