Skip to content
This repository
  • 4 commits
  • 1 file changed
  • 0 comments
  • 1 contributor

Showing 1 changed file with 207 additions and 185 deletions. Show diff stats Hide diff stats

  1. +207 185 src/6model/reprs/VMArray.c
392 src/6model/reprs/VMArray.c
@@ -7,6 +7,155 @@
7 7 /* This representation's function pointer table. */
8 8 static REPROps *this_repr;
9 9
  10 +/* Wrapper functions to set and get an array offset to a value for the various
  11 + * types we support. */
  12 +static void set_pos_int(PARROT_INTERP, VMArrayBody *body, VMArrayREPRData *repr_data, INTVAL offset, INTVAL val) {
  13 + switch(repr_data->elem_size) {
  14 + case 8:
  15 + ((Parrot_Int1 *) body->slots)[offset] = (Parrot_Int1) val;
  16 + break;
  17 + case 16:
  18 + ((Parrot_Int2 *) body->slots)[offset] = (Parrot_Int2) val;
  19 + break;
  20 + case 32:
  21 + ((Parrot_Int4 *) body->slots)[offset] = (Parrot_Int4) val;
  22 + break;
  23 + case 64:
  24 + ((Parrot_Int8 *) body->slots)[offset] = (Parrot_Int8) val;
  25 + break;
  26 + default:
  27 + Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
  28 + "VMArray: Only supports 8, 16, 32 and 64 bit integers.");
  29 + }
  30 +}
  31 +
  32 +static void set_pos_float(PARROT_INTERP, VMArrayBody *body, VMArrayREPRData *repr_data, INTVAL offset, FLOATVAL val) {
  33 + switch(repr_data->elem_size) {
  34 + case 32:
  35 + ((Parrot_Float4 *) body->slots)[offset] = (Parrot_Float4) val;
  36 + break;
  37 + case 64:
  38 + ((Parrot_Float8 *) body->slots)[offset] = (Parrot_Float8) val;
  39 + break;
  40 + default:
  41 + Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
  42 + "VMArray: Only supports 32 and 64 bit floats.");
  43 + }
  44 +}
  45 +
  46 +static void set_pos_pmc(PMC **slots, INTVAL offset, PMC *obj) { slots[offset] = obj; }
  47 +
  48 +/* Convenience method to set a given offset to a sensible NULL value. */
  49 +static void null_pos(PARROT_INTERP, VMArrayBody *body, VMArrayREPRData *repr_data, INTVAL offset) {
  50 + if(!repr_data->elem_size) {
  51 + set_pos_pmc((PMC **) body->slots, offset, PMCNULL);
  52 + }
  53 + else if(repr_data->elem_kind == STORAGE_SPEC_BP_INT) {
  54 + set_pos_int(interp, body, repr_data, offset, 0);
  55 + }
  56 + else if(repr_data->elem_kind == STORAGE_SPEC_BP_NUM) {
  57 + set_pos_float(interp, body, repr_data, offset, 0.0);
  58 + }
  59 + else {
  60 + Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
  61 + "VMArray: unsupported elem_kind (%d) in null_pos", repr_data->elem_kind);
  62 + }
  63 +}
  64 +
  65 +/* Ensure that the array has enough size */
  66 +static void ensure_size(PARROT_INTERP, VMArrayBody *body, VMArrayREPRData *repr_data, INTVAL n) {
  67 + INTVAL elems = body->elems;
  68 + INTVAL start = body->start;
  69 + INTVAL ssize = body->ssize;
  70 + void *slots = body->slots;
  71 + INTVAL elem_size = repr_data->elem_size/8;
  72 +
  73 + if(n < 0) {
  74 + Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
  75 + "VMArray: Can't resize to negative size");
  76 + }
  77 +
  78 + if(n == elems) { return; }
  79 +
  80 + /* If there aren't enough slots at the end, shift off empty slots
  81 + * from the beginning first */
  82 + if (start > 0 && n + start > ssize) {
  83 + if (elems > 0) {
  84 + void *src = ((char *) slots) + start*elem_size;
  85 + memmove(slots, src, elems * elem_size);
  86 + }
  87 + body->start = 0;
  88 + /* fill out any unused slots with PMCNULL pointers */
  89 + while (elems < ssize) {
  90 + null_pos(interp, body, repr_data, elems);
  91 + elems++;
  92 + }
  93 + }
  94 +
  95 + body->elems = n;
  96 + if (n <= ssize) {
  97 + /* We already have n slots available, we can just return */
  98 + return;
  99 + }
  100 +
  101 + /* We need more slots. If the current slot size is less
  102 + * than 8K, use the larger of twice the current slot size
  103 + * or the actual number of elements needed. Otherwise,
  104 + * grow the slots to the next multiple of 4096 (0x1000). */
  105 + if (ssize < 8192) {
  106 + ssize *= 2;
  107 + if (n > ssize) ssize = n;
  108 + if (ssize < 8) ssize = 8;
  109 + }
  110 + else {
  111 + ssize = (n + 0x1000) & ~0xfff;
  112 + }
  113 +
  114 + /* Now allocate the new slot buffer */
  115 + slots = (slots)
  116 + ? mem_sys_realloc(slots, ssize*elem_size)
  117 + : mem_sys_allocate(ssize*elem_size);
  118 +
  119 + /* Fill out any unused slots with PMCNULL pointers */
  120 + while (elems < ssize) {
  121 + null_pos(interp, body, repr_data, elems);
  122 + elems++;
  123 + }
  124 +
  125 + body->ssize = ssize;
  126 + body->slots = slots;
  127 +}
  128 +
  129 +static INTVAL get_pos_int(PARROT_INTERP, VMArrayBody *body, VMArrayREPRData *repr_data, INTVAL offset) {
  130 + switch(repr_data->elem_size) {
  131 + case 8:
  132 + return ((Parrot_Int1 *) body->slots)[offset];
  133 + case 16:
  134 + return ((Parrot_Int2 *) body->slots)[offset];
  135 + case 32:
  136 + return ((Parrot_Int4 *) body->slots)[offset];
  137 + case 64:
  138 + return ((Parrot_Int8 *) body->slots)[offset];
  139 + default:
  140 + Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
  141 + "VMArray: Only supports 8, 16, 32 and 64 bit integers.");
  142 + }
  143 +}
  144 +
  145 +static FLOATVAL get_pos_float(PARROT_INTERP, VMArrayBody *body, VMArrayREPRData *repr_data, INTVAL offset) {
  146 + switch(repr_data->elem_size) {
  147 + case 32:
  148 + return ((Parrot_Float4 *) body->slots)[offset];
  149 + case 64:
  150 + return ((Parrot_Float8 *) body->slots)[offset];
  151 + default:
  152 + Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
  153 + "VMArray: Only supports 32 and 64 bit floats.");
  154 + }
  155 +}
  156 +
  157 +static PMC *get_pos_pmc(PMC **slots, INTVAL offset) { return slots[offset]; }
  158 +
10 159 /* Creates a new type object of this representation, and associates it with
11 160 * the given HOW. */
12 161 static PMC * type_object_for(PARROT_INTERP, PMC *HOW) {
@@ -70,19 +219,69 @@ static void copy_to(PARROT_INTERP, STable *st, void *src, void *dest) {
70 219 }
71 220
72 221 static void serialize(PARROT_INTERP, STable *st, void *data, SerializationWriter *writer) {
73   - /* TODO */
  222 + VMArrayBody *body = (VMArrayBody *) data;
  223 + VMArrayREPRData *repr_data = (VMArrayREPRData *) st->REPR_data;
  224 + INTVAL i;
  225 +
  226 + writer->write_int(interp, writer, body->elems);
  227 + for(i = 0; i < body->elems; i++) {
  228 + INTVAL offset = body->start + i;
  229 + if(repr_data->elem_size && repr_data->elem_kind == STORAGE_SPEC_BP_INT) {
  230 + writer->write_int(interp, writer, get_pos_int(interp, body, repr_data, offset));
  231 + }
  232 + else if(repr_data->elem_size && repr_data->elem_kind == STORAGE_SPEC_BP_NUM) {
  233 + writer->write_num(interp, writer, get_pos_float(interp, body, repr_data, offset));
  234 + }
  235 + else {
  236 + writer->write_ref(interp, writer, get_pos_pmc((PMC **) body->slots, offset));
  237 + }
  238 + }
74 239 }
75 240
76 241 static void deserialize(PARROT_INTERP, STable *st, void *data, SerializationReader *reader) {
77   - /* TODO */
  242 + VMArrayBody *body = (VMArrayBody *) data;
  243 + VMArrayREPRData *repr_data = (VMArrayREPRData *) st->REPR_data;
  244 + INTVAL elems = reader->read_int(interp, reader);
  245 + INTVAL i;
  246 +
  247 + ensure_size(interp, body, repr_data, elems);
  248 + body->elems = elems;
  249 +
  250 + for(i = 0; i < body->elems; i++) {
  251 + INTVAL offset = body->start + i;
  252 + if(repr_data->elem_size && repr_data->elem_kind == STORAGE_SPEC_BP_INT) {
  253 + set_pos_int(interp, body, repr_data, offset, reader->read_int(interp, reader));
  254 + }
  255 + else if(repr_data->elem_size && repr_data->elem_kind == STORAGE_SPEC_BP_NUM) {
  256 + set_pos_float(interp, body, repr_data, offset, reader->read_num(interp, reader));
  257 + }
  258 + else {
  259 + set_pos_pmc((PMC **) body->slots, offset, reader->read_ref(interp, reader));
  260 + }
  261 + }
78 262 }
79 263
80 264 static void serialize_repr_data(PARROT_INTERP, STable *st, SerializationWriter *writer) {
81   - /* TODO */
  265 + VMArrayREPRData *repr_data = (VMArrayREPRData *) st->REPR_data;
  266 + writer->write_ref(interp, writer, repr_data->elem_type);
82 267 }
83 268
84 269 static void deserialize_repr_data(PARROT_INTERP, STable *st, SerializationReader *reader) {
85   - /* TODO */
  270 + VMArrayREPRData *repr_data = mem_allocate_zeroed_typed(VMArrayREPRData);
  271 + PMC *type = reader->read_ref(interp, reader);
  272 +
  273 + repr_data->elem_type = type;
  274 + if(!PMC_IS_NULL(type)) {
  275 + storage_spec spec = REPR(type)->get_storage_spec(interp, STABLE(type));
  276 + if(spec.inlineable == STORAGE_SPEC_INLINED &&
  277 + (spec.boxed_primitive == STORAGE_SPEC_BP_INT ||
  278 + spec.boxed_primitive == STORAGE_SPEC_BP_NUM)) {
  279 + repr_data->elem_size = spec.bits;
  280 + repr_data->elem_kind = spec.boxed_primitive;
  281 + }
  282 + }
  283 +
  284 + st->REPR_data = repr_data;
86 285 }
87 286
88 287 /* Gets the storage specification for this representation. */
@@ -106,24 +305,6 @@ PARROT_DOES_NOT_RETURN static void die_no_boxed(PARROT_INTERP, const char *opera
106 305 "VMArray: Can't perform boxed %s when containing native types", operation);
107 306 }
108 307
109   -/* Wrapper functions to set and get an array offset to a value for the various
110   - * types we support. */
111   -static void set_pos_int1(Parrot_Int1 *slots, INTVAL offset, Parrot_Int1 val) { slots[offset] = val; }
112   -static void set_pos_int2(Parrot_Int2 *slots, INTVAL offset, Parrot_Int2 val) { slots[offset] = val; }
113   -static void set_pos_int4(Parrot_Int4 *slots, INTVAL offset, Parrot_Int4 val) { slots[offset] = val; }
114   -static void set_pos_int8(Parrot_Int8 *slots, INTVAL offset, Parrot_Int8 val) { slots[offset] = val; }
115   -static void set_pos_float4(Parrot_Float4 *slots, INTVAL offset, Parrot_Float4 val) { slots[offset] = val; }
116   -static void set_pos_float8(Parrot_Float8 *slots, INTVAL offset, Parrot_Float8 val) { slots[offset] = val; }
117   -static void set_pos_pmc(PMC **slots, INTVAL offset, PMC *obj) { slots[offset] = obj; }
118   -
119   -static INTVAL get_pos_int1(Parrot_Int1 *slots, INTVAL offset) { return slots[offset]; }
120   -static INTVAL get_pos_int2(Parrot_Int2 *slots, INTVAL offset) { return slots[offset]; }
121   -static INTVAL get_pos_int4(Parrot_Int4 *slots, INTVAL offset) { return slots[offset]; }
122   -static INTVAL get_pos_int8(Parrot_Int8 *slots, INTVAL offset) { return slots[offset]; }
123   -static FLOATVAL get_pos_float4(Parrot_Float4 *slots, INTVAL offset) { return slots[offset]; }
124   -static FLOATVAL get_pos_float8(Parrot_Float8 *slots, INTVAL offset) { return slots[offset]; }
125   -static PMC *get_pos_pmc(PMC **slots, INTVAL offset) { return slots[offset]; }
126   -
127 308 static void gc_mark(PARROT_INTERP, STable *st, void *data) {
128 309 /* TODO: If we contain PMC, mark all of them that are non-NULL. */
129 310 VMArrayBody *body = (VMArrayBody *) data;
@@ -151,113 +332,6 @@ static void gc_free(PARROT_INTERP, PMC *obj) {
151 332 mem_sys_free(instance);
152 333 }
153 334
154   -/* Convenience method to set a given offset to a sensible NULL value. */
155   -static void null_pos(PARROT_INTERP, VMArrayREPRData *repr_data, void *slots, INTVAL offset) {
156   - if(!repr_data->elem_size) {
157   - set_pos_pmc((PMC **) slots, offset, PMCNULL);
158   - }
159   - else if(repr_data->elem_kind == STORAGE_SPEC_BP_INT) {
160   - switch(repr_data->elem_size) {
161   - case 8:
162   - set_pos_int1((Parrot_Int1 *) slots, offset, 0);
163   - break;
164   - case 16:
165   - set_pos_int2((Parrot_Int2 *) slots, offset, 0);
166   - break;
167   - case 32:
168   - set_pos_int4((Parrot_Int4 *) slots, offset, 0);
169   - break;
170   - case 64:
171   - set_pos_int8((Parrot_Int8 *) slots, offset, 0);
172   - break;
173   - default:
174   - Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
175   - "VMArray: Only supports 8, 16, 32 and 64 bit integers.");
176   - }
177   - }
178   - else if(repr_data->elem_kind == STORAGE_SPEC_BP_NUM) {
179   - switch(repr_data->elem_size) {
180   - case 32:
181   - set_pos_float4((Parrot_Float4 *) slots, offset, 0.0);
182   - break;
183   - case 64:
184   - set_pos_float8((Parrot_Float8 *) slots, offset, 0.0);
185   - break;
186   - default:
187   - Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
188   - "VMArray: Only supports 32 and 64 bit floats.");
189   - }
190   - }
191   - else {
192   - Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
193   - "VMArray: unsupported elem_kind (%d) in null_pos", repr_data->elem_kind);
194   - }
195   -}
196   -
197   -/* Ensure that the array has enough size */
198   -static void ensure_size(PARROT_INTERP, VMArrayBody *body, VMArrayREPRData *repr_data, INTVAL n) {
199   - INTVAL elems = body->elems;
200   - INTVAL start = body->start;
201   - INTVAL ssize = body->ssize;
202   - void *slots = body->slots;
203   - INTVAL elem_size = repr_data->elem_size/8;
204   -
205   - if(n < 0) {
206   - Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
207   - "VMArray: Can't resize to negative size");
208   - }
209   -
210   - if(n == elems) { return; }
211   -
212   - /* If there aren't enough slots at the end, shift off empty slots
213   - * from the beginning first */
214   - if (start > 0 && n + start > ssize) {
215   - if (elems > 0) {
216   - void *src = ((char *) slots) + start*elem_size;
217   - memmove(slots, src, elems * elem_size);
218   - }
219   - body->start = 0;
220   - /* fill out any unused slots with PMCNULL pointers */
221   - while (elems < ssize) {
222   - null_pos(interp, repr_data, slots, elems);
223   - elems++;
224   - }
225   - }
226   -
227   - body->elems = n;
228   - if (n <= ssize) {
229   - /* We already have n slots available, we can just return */
230   - return;
231   - }
232   -
233   - /* We need more slots. If the current slot size is less
234   - * than 8K, use the larger of twice the current slot size
235   - * or the actual number of elements needed. Otherwise,
236   - * grow the slots to the next multiple of 4096 (0x1000). */
237   - if (ssize < 8192) {
238   - ssize *= 2;
239   - if (n > ssize) ssize = n;
240   - if (ssize < 8) ssize = 8;
241   - }
242   - else {
243   - ssize = (n + 0x1000) & ~0xfff;
244   - }
245   -
246   - /* Now allocate the new slot buffer */
247   - slots = (slots)
248   - ? mem_sys_realloc(slots, ssize*elem_size)
249   - : mem_sys_allocate(ssize*elem_size);
250   -
251   - /* Fill out any unused slots with PMCNULL pointers */
252   - while (elems < ssize) {
253   - null_pos(interp, repr_data, slots, elems);
254   - elems++;
255   - }
256   -
257   - body->ssize = ssize;
258   - body->slots = slots;
259   -}
260   -
261 335 static void at_pos_native(PARROT_INTERP, STable *st, void *data, INTVAL index, NativeValue *value) {
262 336 VMArrayBody *body = (VMArrayBody *) data;
263 337 VMArrayREPRData *repr_data = (VMArrayREPRData *) st->REPR_data;
@@ -270,36 +344,10 @@ static void at_pos_native(PARROT_INTERP, STable *st, void *data, INTVAL index, N
270 344 "VMArray: Can't get unboxed string value");
271 345
272 346 if(repr_data->elem_kind == STORAGE_SPEC_BP_INT) {
273   - switch(repr_data->elem_size) {
274   - case 8:
275   - value->value.intval = get_pos_int1((Parrot_Int1 *) body->slots, body->start + index);
276   - break;
277   - case 16:
278   - value->value.intval = get_pos_int2((Parrot_Int2 *) body->slots, body->start + index);
279   - break;
280   - case 32:
281   - value->value.intval = get_pos_int4((Parrot_Int4 *) body->slots, body->start + index);
282   - break;
283   - case 64:
284   - value->value.intval = get_pos_int8((Parrot_Int8 *) body->slots, body->start + index);
285   - break;
286   - default:
287   - Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
288   - "VMArray: Only supports 8, 16, 32 and 64 bit integers.");
289   - }
  347 + value->value.intval = get_pos_int(interp, body, repr_data, body->start + index);
290 348 }
291 349 else if(repr_data->elem_kind == STORAGE_SPEC_BP_NUM) {
292   - switch(repr_data->elem_size) {
293   - case 32:
294   - value->value.floatval = get_pos_float4((Parrot_Float4 *) body->slots, body->start + index);
295   - break;
296   - case 64:
297   - value->value.floatval = get_pos_float8((Parrot_Float8 *) body->slots, body->start + index);
298   - break;
299   - default:
300   - Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
301   - "VMArray: Only supports 32 and 64 bit floats.");
302   - }
  350 + value->value.floatval = get_pos_float(interp, body, repr_data, body->start + index);
303 351 }
304 352 else {
305 353 Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
@@ -349,36 +397,10 @@ static void bind_pos_native(PARROT_INTERP, STable *st, void *data, INTVAL index,
349 397 }
350 398
351 399 if(repr_data->elem_kind == STORAGE_SPEC_BP_INT) {
352   - switch(repr_data->elem_size) {
353   - case 8:
354   - set_pos_int1((Parrot_Int1 *) body->slots, body->start + index, value->value.intval);
355   - break;
356   - case 16:
357   - set_pos_int2((Parrot_Int2 *) body->slots, body->start + index, value->value.intval);
358   - break;
359   - case 32:
360   - set_pos_int4((Parrot_Int4 *) body->slots, body->start + index, value->value.intval);
361   - break;
362   - case 64:
363   - set_pos_int8((Parrot_Int8 *) body->slots, body->start + index, value->value.intval);
364   - break;
365   - default:
366   - Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
367   - "VMArray: Only supports 8, 16, 32 and 64 bit integers.");
368   - }
  400 + set_pos_int(interp, body, repr_data, body->start + index, value->value.intval);
369 401 }
370 402 else if(repr_data->elem_kind == STORAGE_SPEC_BP_NUM) {
371   - switch(repr_data->elem_size) {
372   - case 32:
373   - set_pos_float4((Parrot_Float4 *) body->slots, body->start + index, value->value.floatval);
374   - break;
375   - case 64:
376   - set_pos_float8((Parrot_Float8 *) body->slots, body->start + index, value->value.floatval);
377   - break;
378   - default:
379   - Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
380   - "VMArray: Only supports 32 and 64 bit floats.");
381   - }
  403 + set_pos_float(interp, body, repr_data, body->start + index, value->value.floatval);
382 404 }
383 405 else {
384 406 Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,

No commit comments for this range

Something went wrong with that request. Please try again.