]> gitweb.factorcode.org Git - factor.git/blob - vm/types.h
c1ebb104d97a98ada6b66621380d1eca13036e84
[factor.git] / vm / types.h
1 INLINE CELL tag_boolean(CELL untagged)
2 {
3         return (untagged == false ? F : T);
4 }
5
6 DLLEXPORT void box_boolean(bool value);
7 DLLEXPORT bool unbox_boolean(void);
8
9 INLINE F_ARRAY* untag_array_fast(CELL tagged)
10 {
11         return (F_ARRAY*)UNTAG(tagged);
12 }
13
14 INLINE F_ARRAY* untag_array(CELL tagged)
15 {
16         type_check(ARRAY_TYPE,tagged);
17         return untag_array_fast(tagged);
18 }
19
20 INLINE CELL array_size(CELL size)
21 {
22         return sizeof(F_ARRAY) + size * CELLS;
23 }
24
25 F_ARRAY *allot_array_internal(CELL type, F_FIXNUM capacity);
26 F_ARRAY *allot_array(CELL type, F_FIXNUM capacity, CELL fill);
27 F_ARRAY *allot_byte_array(F_FIXNUM size);
28
29 CELL allot_array_4(CELL v1, CELL v2, CELL v3, CELL v4);
30
31 void primitive_array(void);
32 void primitive_byte_array(void);
33
34 F_ARRAY *reallot_array(F_ARRAY* array, F_FIXNUM capacity, CELL fill);
35 void primitive_resize_array(void);
36
37 void primitive_become(void);
38
39 #define AREF(array,index) ((CELL)(array) + sizeof(F_ARRAY) + (index) * CELLS)
40 #define UNAREF(array,ptr) (((CELL)(ptr)-(CELL)(array)-sizeof(F_ARRAY)) / CELLS)
41
42 INLINE void set_array_nth(F_ARRAY *array, CELL slot, CELL value)
43 {
44         put(AREF(array,slot),value);
45         write_barrier((CELL)array);
46 }
47
48 INLINE CELL array_capacity(F_ARRAY* array)
49 {
50         return array->capacity >> TAG_BITS;
51 }
52
53 #define GROWABLE_ARRAY(result) \
54         CELL result##_count = 0; \
55         F_ARRAY *result = allot_array(ARRAY_TYPE,100,F)
56
57 INLINE F_ARRAY *growable_add(F_ARRAY *result, CELL elt, CELL *result_count)
58 {
59         REGISTER_ROOT(elt);
60
61         if(*result_count == array_capacity(result))
62         {
63                 result = reallot_array(result,
64                         *result_count * 2,F);
65         }
66
67         UNREGISTER_ROOT(elt);
68         set_array_nth(result,*result_count,elt);
69         *result_count = *result_count + 1;
70
71         return result;
72 }
73
74 #define GROWABLE_ADD(result,elt) \
75         result = growable_add(result,elt,&result##_count)
76
77 #define GROWABLE_TRIM(result) result = reallot_array(result,result##_count,F)
78
79 INLINE F_VECTOR* untag_vector(CELL tagged)
80 {
81         type_check(VECTOR_TYPE,tagged);
82         return (F_VECTOR*)UNTAG(tagged);
83 }
84
85 F_VECTOR* vector(F_FIXNUM capacity);
86
87 void primitive_array_to_vector(void);
88
89 #define SREF(string,index) ((CELL)string + sizeof(F_STRING) + index * CHARS)
90
91 INLINE F_STRING* untag_string_fast(CELL tagged)
92 {
93         return (F_STRING*)UNTAG(tagged);
94 }
95
96 INLINE F_STRING* untag_string(CELL tagged)
97 {
98         type_check(STRING_TYPE,tagged);
99         return untag_string_fast(tagged);
100 }
101
102 INLINE CELL string_capacity(F_STRING* str)
103 {
104         return str->length >> TAG_BITS;
105 }
106
107 INLINE CELL string_size(CELL size)
108 {
109         return sizeof(F_STRING) + (size + 1) * CHARS;
110 }
111
112 F_STRING* allot_string_internal(F_FIXNUM capacity);
113 void rehash_string(F_STRING* str);
114 void primitive_rehash_string(void);
115 F_STRING* allot_string(F_FIXNUM capacity, CELL fill);
116 void primitive_string(void);
117 F_STRING *reallot_string(F_STRING *string, F_FIXNUM capacity, u16 fill);
118 void primitive_resize_string(void);
119
120 bool check_string(F_STRING *s, CELL max);
121
122 F_STRING *memory_to_char_string(const char *string, CELL length);
123 void primitive_memory_to_char_string(void);
124 F_STRING *from_char_string(const char *c_string);
125 DLLEXPORT void box_char_string(const char *c_string);
126 void primitive_alien_to_char_string(void);
127
128 F_STRING *memory_to_u16_string(const u16 *string, CELL length);
129 void primitive_memory_to_u16_string(void);
130 F_STRING *from_u16_string(const u16 *c_string);
131 DLLEXPORT void box_u16_string(const u16 *c_string);
132 void primitive_alien_to_u16_string(void);
133
134 void char_string_to_memory(F_STRING *s, char *string);
135 void primitive_char_string_to_memory(void);
136 F_ARRAY *string_to_char_alien(F_STRING *s, bool check);
137 char* to_char_string(F_STRING *s, bool check);
138 DLLEXPORT char *unbox_char_string(void);
139 void primitive_string_to_char_alien(void);
140
141 void u16_string_to_memory(F_STRING *s, u16 *string);
142 void primitive_u16_string_to_memory(void);
143 F_ARRAY *string_to_u16_alien(F_STRING *s, bool check);
144 u16* to_u16_string(F_STRING *s, bool check);
145 DLLEXPORT u16 *unbox_u16_string(void);
146 void primitive_string_to_u16_alien(void);
147
148 /* untagged & unchecked */
149 INLINE CELL string_nth(F_STRING* string, CELL index)
150 {
151         return cget(SREF(string,index));
152 }
153
154 /* untagged & unchecked */
155 INLINE void set_string_nth(F_STRING* string, CELL index, u16 value)
156 {
157         cput(SREF(string,index),value);
158 }
159
160 void primitive_char_slot(void);
161 void primitive_set_char_slot(void);
162
163 void primitive_string_to_sbuf(void);
164
165 void primitive_hashtable(void);
166
167 typedef void (*XT)(F_WORD *word);
168
169 INLINE F_WORD *untag_word_fast(CELL tagged)
170 {
171         return (F_WORD*)UNTAG(tagged);
172 }
173
174 INLINE F_WORD *untag_word(CELL tagged)
175 {
176         type_check(WORD_TYPE,tagged);
177         return untag_word_fast(tagged);
178 }
179
180 INLINE CELL tag_word(F_WORD *word)
181 {
182         return RETAG(word,WORD_TYPE);
183 }
184
185 void update_xt(F_WORD* word);
186 F_WORD *allot_word(CELL vocab, CELL name);
187 void primitive_word(void);
188 void primitive_update_xt(void);
189 void primitive_word_xt(void);
190 void fixup_word(F_WORD* word);
191
192 INLINE F_WRAPPER *untag_wrapper_fast(CELL tagged)
193 {
194         return (F_WRAPPER*)UNTAG(tagged);
195 }
196
197 INLINE CELL tag_wrapper(F_WRAPPER *wrapper)
198 {
199         return RETAG(wrapper,WRAPPER_TYPE);
200 }
201
202 void primitive_wrapper(void);