/home/travis/build/MoarVM/MoarVM/src/6model/reprs/P6int.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include "moar.h" |
2 | | #ifdef MVM_BOOL |
3 | | #include <stdbool.h> |
4 | | #endif |
5 | | |
6 | | /* This representation's function pointer table. */ |
7 | | static const MVMREPROps P6int_this_repr; |
8 | | |
9 | 1.59k | static void mk_storage_spec(MVMThreadContext *tc, MVMuint16 bits, MVMuint16 is_unsigned, MVMStorageSpec *spec) { |
10 | 1.59k | /* create storage spec */ |
11 | 1.59k | spec->inlineable = MVM_STORAGE_SPEC_INLINED; |
12 | 1.59k | spec->boxed_primitive = MVM_STORAGE_SPEC_BP_INT; |
13 | 1.59k | spec->can_box = MVM_STORAGE_SPEC_CAN_BOX_INT; |
14 | 1.59k | spec->bits = bits; |
15 | 1.59k | spec->is_unsigned = is_unsigned; |
16 | 1.59k | switch (bits) { |
17 | 735 | case 64: spec->align = ALIGNOF(MVMint64); break; |
18 | 288 | case 32: spec->align = ALIGNOF(MVMint32); break; |
19 | 288 | case 16: spec->align = ALIGNOF(MVMint16); break; |
20 | 288 | default: spec->align = ALIGNOF(MVMint8); break; |
21 | 1.59k | } |
22 | 1.59k | } |
23 | | |
24 | | |
25 | | /* Creates a new type object of this representation, and associates it with |
26 | | * the given HOW. */ |
27 | 151 | static MVMObject * type_object_for(MVMThreadContext *tc, MVMObject *HOW) { |
28 | 151 | MVMSTable *st = MVM_gc_allocate_stable(tc, &P6int_this_repr, HOW); |
29 | 151 | |
30 | 151 | MVMROOT(tc, st, { |
31 | 151 | MVMObject *obj = MVM_gc_allocate_type_object(tc, st); |
32 | 151 | MVMP6intREPRData *repr_data = (MVMP6intREPRData *)MVM_malloc(sizeof(MVMP6intREPRData)); |
33 | 151 | |
34 | 151 | repr_data->bits = sizeof(MVMint64) * 8; |
35 | 151 | repr_data->is_unsigned = 0; |
36 | 151 | mk_storage_spec(tc, repr_data->bits, repr_data->is_unsigned, &repr_data->storage_spec); |
37 | 151 | MVM_ASSIGN_REF(tc, &(st->header), st->WHAT, obj); |
38 | 151 | st->size = sizeof(MVMP6int); |
39 | 151 | st->REPR_data = repr_data; |
40 | 151 | |
41 | 151 | }); |
42 | 151 | |
43 | 151 | return st->WHAT; |
44 | 151 | } |
45 | | |
46 | | /* Copies the body of one object to another. */ |
47 | 48.9k | static void copy_to(MVMThreadContext *tc, MVMSTable *st, void *src, MVMObject *dest_root, void *dest) { |
48 | 48.9k | MVMP6intREPRData *repr_data = (MVMP6intREPRData *)st->REPR_data; |
49 | 48.9k | MVMP6intBody *src_body = (MVMP6intBody *)src; |
50 | 48.9k | MVMP6intBody *dest_body = (MVMP6intBody *)dest; |
51 | 48.9k | switch (repr_data->bits) { |
52 | 48.9k | case 64: dest_body->value.i64 = src_body->value.i64; break; |
53 | 0 | case 32: dest_body->value.i32 = src_body->value.i32; break; |
54 | 0 | case 16: dest_body->value.i16 = src_body->value.i16; break; |
55 | 0 | default: dest_body->value.i8 = src_body->value.i8; break; |
56 | 48.9k | } |
57 | 48.9k | } |
58 | | |
59 | 0 | static void set_uint(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMuint64 value) { |
60 | 0 | MVMP6intREPRData *repr_data = (MVMP6intREPRData *)st->REPR_data; |
61 | 0 | switch (repr_data->bits) { |
62 | 0 | case 64: ((MVMP6intBody *)data)->value.u64 = value; break; |
63 | 0 | case 32: ((MVMP6intBody *)data)->value.u32 = (MVMuint32)value; break; |
64 | 0 | case 16: ((MVMP6intBody *)data)->value.u16 = (MVMuint16)value; break; |
65 | 0 | default: ((MVMP6intBody *)data)->value.u8 = (MVMuint8)value; break; |
66 | 0 | } |
67 | 0 | } |
68 | | |
69 | 0 | static MVMuint64 get_uint(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data) { |
70 | 0 | MVMP6intREPRData *repr_data = (MVMP6intREPRData *)st->REPR_data; |
71 | 0 | switch (repr_data->bits) { |
72 | 0 | case 64: return ((MVMP6intBody *)data)->value.u64; |
73 | 0 | case 32: return ((MVMP6intBody *)data)->value.u32; |
74 | 0 | case 16: return ((MVMP6intBody *)data)->value.u16; |
75 | 0 | default: return ((MVMP6intBody *)data)->value.u8; |
76 | 0 | } |
77 | 0 | } |
78 | | |
79 | 10.4M | static void set_int(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMint64 value) { |
80 | 10.4M | MVMP6intREPRData *repr_data = (MVMP6intREPRData *)st->REPR_data; |
81 | 10.4M | switch (repr_data->bits) { |
82 | 10.4M | case 64: ((MVMP6intBody *)data)->value.i64 = value; break; |
83 | 0 | case 32: ((MVMP6intBody *)data)->value.i32 = (MVMint32)value; break; |
84 | 0 | case 16: ((MVMP6intBody *)data)->value.i16 = (MVMint16)value; break; |
85 | 2 | default: ((MVMP6intBody *)data)->value.i8 = (MVMint8)value; break; |
86 | 10.4M | } |
87 | 10.4M | } |
88 | | |
89 | 24.2M | static MVMint64 get_int(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data) { |
90 | 24.2M | MVMP6intREPRData *repr_data = (MVMP6intREPRData *)st->REPR_data; |
91 | 24.2M | switch (repr_data->bits) { |
92 | 24.2M | case 64: return ((MVMP6intBody *)data)->value.i64; |
93 | 0 | case 32: return ((MVMP6intBody *)data)->value.i32; |
94 | 0 | case 16: return ((MVMP6intBody *)data)->value.i16; |
95 | 2 | default: return ((MVMP6intBody *)data)->value.i8; |
96 | 24.2M | } |
97 | 24.2M | } |
98 | | |
99 | | /* Marks the representation data in an STable.*/ |
100 | 0 | static void gc_free_repr_data(MVMThreadContext *tc, MVMSTable *st) { |
101 | 0 | MVM_free(st->REPR_data); |
102 | 0 | } |
103 | | |
104 | | static const MVMStorageSpec default_storage_spec = { |
105 | | MVM_STORAGE_SPEC_INLINED, /* inlineable */ |
106 | | sizeof(MVMint64) * 8, /* bits */ |
107 | | ALIGNOF(MVMint64), /* align */ |
108 | | MVM_STORAGE_SPEC_BP_INT, /* boxed_primitive */ |
109 | | MVM_STORAGE_SPEC_CAN_BOX_INT, /* can_box */ |
110 | | 0, /* is_unsigned */ |
111 | | }; |
112 | | |
113 | | |
114 | | /* Gets the storage specification for this representation. */ |
115 | 19.2M | static const MVMStorageSpec * get_storage_spec(MVMThreadContext *tc, MVMSTable *st) { |
116 | 19.2M | MVMP6intREPRData *repr_data = (MVMP6intREPRData *)st->REPR_data; |
117 | 19.2M | if (repr_data && repr_data->bits) |
118 | 19.2M | return &repr_data->storage_spec; |
119 | 280 | return &default_storage_spec; |
120 | 19.2M | } |
121 | | |
122 | | /* Compose the representation. */ |
123 | 7 | static void compose(MVMThreadContext *tc, MVMSTable *st, MVMObject *info_hash) { |
124 | 7 | MVMP6intREPRData *repr_data = (MVMP6intREPRData *)st->REPR_data; |
125 | 7 | MVMStringConsts str_consts = tc->instance->str_consts; |
126 | 7 | |
127 | 7 | MVMObject *info = MVM_repr_at_key_o(tc, info_hash, str_consts.integer); |
128 | 7 | if (!MVM_is_null(tc, info)) { |
129 | 0 | MVMObject *bits_o = MVM_repr_at_key_o(tc, info, str_consts.bits); |
130 | 0 | MVMObject *is_unsigned_o = MVM_repr_at_key_o(tc, info, str_consts.unsigned_str); |
131 | 0 |
|
132 | 0 | if (!MVM_is_null(tc, bits_o)) { |
133 | 0 | repr_data->bits = MVM_repr_get_int(tc, bits_o); |
134 | 0 |
|
135 | 0 | switch (repr_data->bits) { |
136 | 0 | case MVM_P6INT_C_TYPE_CHAR: repr_data->bits = 8 * sizeof(char); break; |
137 | 0 | case MVM_P6INT_C_TYPE_SHORT: repr_data->bits = 8 * sizeof(short); break; |
138 | 0 | case MVM_P6INT_C_TYPE_INT: repr_data->bits = 8 * sizeof(int); break; |
139 | 0 | case MVM_P6INT_C_TYPE_LONG: repr_data->bits = 8 * sizeof(long); break; |
140 | 0 | case MVM_P6INT_C_TYPE_LONGLONG: repr_data->bits = 8 * sizeof(long long); break; |
141 | 0 | case MVM_P6INT_C_TYPE_SIZE_T: repr_data->bits = 8 * sizeof(size_t); break; |
142 | 0 | #ifdef MVM_BOOL |
143 | 0 | case MVM_P6INT_C_TYPE_BOOL: repr_data->bits = 8 * sizeof(MVM_BOOL); break; |
144 | 0 | #else |
145 | | case MVM_P6INT_C_TYPE_BOOL: repr_data->bits = 8 * sizeof(char); break; |
146 | | #endif |
147 | 0 | case MVM_P6INT_C_TYPE_ATOMIC: repr_data->bits = 8 * sizeof(AO_t); break; |
148 | 0 | } |
149 | 0 |
|
150 | 0 | if (repr_data->bits != 1 && repr_data->bits != 2 && repr_data->bits != 4 && repr_data->bits != 8 |
151 | 0 | && repr_data->bits != 16 && repr_data->bits != 32 && repr_data->bits != 64) |
152 | 0 | MVM_exception_throw_adhoc(tc, "MVMP6int: Unsupported int size (%dbit)", repr_data->bits); |
153 | 0 | } else { |
154 | 0 | repr_data->bits = default_storage_spec.bits; |
155 | 0 | } |
156 | 0 |
|
157 | 0 | if (!MVM_is_null(tc, is_unsigned_o)) { |
158 | 0 | repr_data->is_unsigned = MVM_repr_get_int(tc, is_unsigned_o); |
159 | 0 | } |
160 | 0 | } |
161 | 7 | if (repr_data->bits) |
162 | 7 | mk_storage_spec(tc, repr_data->bits, repr_data->is_unsigned, &repr_data->storage_spec); |
163 | 7 | } |
164 | | |
165 | | /* Set the size of the STable. */ |
166 | 1.44k | static void deserialize_stable_size(MVMThreadContext *tc, MVMSTable *st, MVMSerializationReader *reader) { |
167 | 1.44k | st->size = sizeof(MVMP6int); |
168 | 1.44k | } |
169 | | |
170 | | /* Serializes the REPR data. */ |
171 | 1 | static void serialize_repr_data(MVMThreadContext *tc, MVMSTable *st, MVMSerializationWriter *writer) { |
172 | 1 | MVMP6intREPRData *repr_data = (MVMP6intREPRData *)st->REPR_data; |
173 | 1 | MVM_serialization_write_int(tc, writer, repr_data->bits); |
174 | 1 | MVM_serialization_write_int(tc, writer, repr_data->is_unsigned); |
175 | 1 | } |
176 | | |
177 | | /* Deserializes representation data. */ |
178 | 1.44k | static void deserialize_repr_data(MVMThreadContext *tc, MVMSTable *st, MVMSerializationReader *reader) { |
179 | 1.44k | MVMP6intREPRData *repr_data = (MVMP6intREPRData *)MVM_malloc(sizeof(MVMP6intREPRData)); |
180 | 1.44k | |
181 | 1.44k | |
182 | 1.44k | repr_data->bits = MVM_serialization_read_int(tc, reader); |
183 | 1.44k | repr_data->is_unsigned = MVM_serialization_read_int(tc, reader); |
184 | 1.44k | |
185 | 1.44k | if (repr_data->bits != 1 && repr_data->bits != 2 && repr_data->bits != 4 && repr_data->bits != 8 |
186 | 1.15k | && repr_data->bits != 16 && repr_data->bits != 32 && repr_data->bits != 64) |
187 | 0 | MVM_exception_throw_adhoc(tc, "MVMP6int: Unsupported int size (%dbit)", repr_data->bits); |
188 | 1.44k | |
189 | 1.44k | mk_storage_spec(tc, repr_data->bits, repr_data->is_unsigned, &repr_data->storage_spec); |
190 | 1.44k | |
191 | 1.44k | st->REPR_data = repr_data; |
192 | 1.44k | } |
193 | | |
194 | 160k | static void deserialize(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMSerializationReader *reader) { |
195 | 160k | set_int(tc, st, root, data, MVM_serialization_read_int(tc, reader)); |
196 | 160k | } |
197 | | |
198 | 18 | static void serialize(MVMThreadContext *tc, MVMSTable *st, void *data, MVMSerializationWriter *writer) { |
199 | 18 | MVM_serialization_write_int(tc, writer, get_int(tc, st, NULL, data)); |
200 | 18 | } |
201 | | |
202 | | /* Initializes the representation. */ |
203 | 144 | const MVMREPROps * MVMP6int_initialize(MVMThreadContext *tc) { |
204 | 144 | return &P6int_this_repr; |
205 | 144 | } |
206 | | |
207 | | static const MVMREPROps P6int_this_repr = { |
208 | | type_object_for, |
209 | | MVM_gc_allocate_object, |
210 | | NULL, /* initialize */ |
211 | | copy_to, |
212 | | MVM_REPR_DEFAULT_ATTR_FUNCS, |
213 | | { |
214 | | set_int, |
215 | | get_int, |
216 | | MVM_REPR_DEFAULT_SET_NUM, |
217 | | MVM_REPR_DEFAULT_GET_NUM, |
218 | | MVM_REPR_DEFAULT_SET_STR, |
219 | | MVM_REPR_DEFAULT_GET_STR, |
220 | | set_uint, |
221 | | get_uint, |
222 | | MVM_REPR_DEFAULT_GET_BOXED_REF |
223 | | }, /* box_funcs */ |
224 | | MVM_REPR_DEFAULT_POS_FUNCS, |
225 | | MVM_REPR_DEFAULT_ASS_FUNCS, |
226 | | MVM_REPR_DEFAULT_ELEMS, |
227 | | get_storage_spec, |
228 | | NULL, /* change_type */ |
229 | | serialize, |
230 | | deserialize, |
231 | | serialize_repr_data, |
232 | | deserialize_repr_data, |
233 | | deserialize_stable_size, |
234 | | NULL, /* gc_mark */ |
235 | | NULL, /* gc_free */ |
236 | | NULL, /* gc_cleanup */ |
237 | | NULL, /* gc_mark_repr_data */ |
238 | | gc_free_repr_data, |
239 | | compose, |
240 | | NULL, /* spesh */ |
241 | | "P6int", /* name */ |
242 | | MVM_REPR_ID_P6int, |
243 | | NULL, /* unmanaged_size */ |
244 | | NULL, /* describe_refs */ |
245 | | }; |