Merge branch 'master' of git://factorcode.org/git/factor
[factor/jcg.git] / vm / types.h
blob5850489a4c1ae5bb07c3a17ff88b09443a51530a
1 /* Inline functions */
2 INLINE CELL array_size(CELL size)
4 return sizeof(F_ARRAY) + size * CELLS;
7 INLINE CELL string_capacity(F_STRING* str)
9 return untag_fixnum_fast(str->length);
12 INLINE CELL string_size(CELL size)
14 return sizeof(F_STRING) + size;
17 DEFINE_UNTAG(F_BYTE_ARRAY,BYTE_ARRAY_TYPE,byte_array)
19 INLINE CELL byte_array_capacity(F_BYTE_ARRAY *array)
21 return untag_fixnum_fast(array->capacity);
24 INLINE CELL byte_array_size(CELL size)
26 return sizeof(F_BYTE_ARRAY) + size;
29 INLINE CELL callstack_size(CELL size)
31 return sizeof(F_CALLSTACK) + size;
34 DEFINE_UNTAG(F_CALLSTACK,CALLSTACK_TYPE,callstack)
36 INLINE CELL tag_boolean(CELL untagged)
38 return (untagged == false ? F : T);
41 DEFINE_UNTAG(F_ARRAY,ARRAY_TYPE,array)
43 #define AREF(array,index) ((CELL)(array) + sizeof(F_ARRAY) + (index) * CELLS)
44 #define UNAREF(array,ptr) (((CELL)(ptr)-(CELL)(array)-sizeof(F_ARRAY)) / CELLS)
46 INLINE CELL array_nth(F_ARRAY *array, CELL slot)
48 return get(AREF(array,slot));
51 INLINE void set_array_nth(F_ARRAY *array, CELL slot, CELL value)
53 put(AREF(array,slot),value);
54 write_barrier((CELL)array);
57 INLINE CELL array_capacity(F_ARRAY* array)
59 return array->capacity >> TAG_BITS;
62 #define BREF(byte_array,index) ((CELL)byte_array + sizeof(F_BYTE_ARRAY) + (index))
63 #define SREF(string,index) ((CELL)string + sizeof(F_STRING) + (index))
65 INLINE F_STRING* untag_string(CELL tagged)
67 type_check(STRING_TYPE,tagged);
68 return untag_object(tagged);
71 DEFINE_UNTAG(F_QUOTATION,QUOTATION_TYPE,quotation)
73 DEFINE_UNTAG(F_WORD,WORD_TYPE,word)
75 INLINE CELL tag_tuple(F_TUPLE *tuple)
77 return RETAG(tuple,TUPLE_TYPE);
80 INLINE F_TUPLE *untag_tuple(CELL object)
82 type_check(TUPLE_TYPE,object);
83 return untag_object(object);
86 INLINE CELL tuple_size(F_TUPLE_LAYOUT *layout)
88 CELL size = untag_fixnum_fast(layout->size);
89 return sizeof(F_TUPLE) + size * CELLS;
92 INLINE CELL tuple_nth(F_TUPLE *tuple, CELL slot)
94 return get(AREF(tuple,slot));
97 INLINE void set_tuple_nth(F_TUPLE *tuple, CELL slot, CELL value)
99 put(AREF(tuple,slot),value);
100 write_barrier((CELL)tuple);
103 /* Prototypes */
104 DLLEXPORT void box_boolean(bool value);
105 DLLEXPORT bool to_boolean(CELL value);
107 F_ARRAY *allot_array_internal(CELL type, CELL capacity);
108 F_ARRAY *allot_array(CELL type, CELL capacity, CELL fill);
109 F_BYTE_ARRAY *allot_byte_array(CELL size);
111 CELL allot_array_1(CELL obj);
112 CELL allot_array_4(CELL v1, CELL v2, CELL v3, CELL v4);
114 void primitive_array(void);
115 void primitive_tuple(void);
116 void primitive_tuple_boa(void);
117 void primitive_tuple_layout(void);
118 void primitive_byte_array(void);
119 void primitive_uninitialized_byte_array(void);
120 void primitive_clone(void);
122 F_ARRAY *reallot_array(F_ARRAY* array, CELL capacity);
123 F_BYTE_ARRAY *reallot_byte_array(F_BYTE_ARRAY *array, CELL capacity);
124 void primitive_resize_array(void);
125 void primitive_resize_byte_array(void);
127 F_STRING* allot_string_internal(CELL capacity);
128 F_STRING* allot_string(CELL capacity, CELL fill);
129 void primitive_uninitialized_string(void);
130 void primitive_string(void);
131 F_STRING *reallot_string(F_STRING *string, CELL capacity);
132 void primitive_resize_string(void);
134 F_STRING *memory_to_char_string(const char *string, CELL length);
135 F_STRING *from_char_string(const char *c_string);
136 DLLEXPORT void box_char_string(const char *c_string);
138 F_STRING *memory_to_u16_string(const u16 *string, CELL length);
139 F_STRING *from_u16_string(const u16 *c_string);
140 DLLEXPORT void box_u16_string(const u16 *c_string);
142 void char_string_to_memory(F_STRING *s, char *string);
143 F_BYTE_ARRAY *string_to_char_alien(F_STRING *s, bool check);
144 char* to_char_string(F_STRING *s, bool check);
145 DLLEXPORT char *unbox_char_string(void);
147 void u16_string_to_memory(F_STRING *s, u16 *string);
148 F_BYTE_ARRAY *string_to_u16_alien(F_STRING *s, bool check);
149 u16* to_u16_string(F_STRING *s, bool check);
150 DLLEXPORT u16 *unbox_u16_string(void);
152 /* String getters and setters */
153 CELL string_nth(F_STRING* string, CELL index);
154 void set_string_nth(F_STRING* string, CELL index, CELL value);
156 void primitive_string_nth(void);
157 void primitive_set_string_nth_slow(void);
158 void primitive_set_string_nth_fast(void);
160 F_WORD *allot_word(CELL vocab, CELL name);
161 void primitive_word(void);
162 void primitive_word_xt(void);
164 void primitive_wrapper(void);
166 /* Macros to simulate a vector in C */
167 #define GROWABLE_ARRAY(result) \
168 CELL result##_count = 0; \
169 CELL result = tag_object(allot_array(ARRAY_TYPE,100,F))
171 F_ARRAY *growable_array_add(F_ARRAY *result, CELL elt, CELL *result_count);
173 #define GROWABLE_ARRAY_ADD(result,elt) \
174 result = tag_object(growable_array_add(untag_object(result),elt,&result##_count))
176 F_ARRAY *growable_array_append(F_ARRAY *result, F_ARRAY *elts, CELL *result_count);
178 #define GROWABLE_ARRAY_APPEND(result,elts) \
179 result = tag_object(growable_array_append(untag_object(result),elts,&result##_count))
181 #define GROWABLE_ARRAY_TRIM(result) \
182 result = tag_object(reallot_array(untag_object(result),result##_count))
184 /* Macros to simulate a byte vector in C */
185 #define GROWABLE_BYTE_ARRAY(result) \
186 CELL result##_count = 0; \
187 CELL result = tag_object(allot_byte_array(100))
189 F_ARRAY *growable_byte_array_append(F_BYTE_ARRAY *result, void *elts, CELL len, CELL *result_count);
191 #define GROWABLE_BYTE_ARRAY_APPEND(result,elts,len) \
192 result = tag_object(growable_byte_array_append(untag_object(result),elts,len,&result##_count))
194 #define GROWABLE_BYTE_ARRAY_TRIM(result) \
195 result = tag_object(reallot_byte_array(untag_object(result),result##_count))