/
VMArray.c
1598 lines (1472 loc) · 63.7 KB
/
VMArray.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "moar.h"
#include "limits.h"
/* This representation's function pointer table. */
static const MVMREPROps VMArray_this_repr;
MVM_STATIC_INLINE void enter_single_user(MVMThreadContext *tc, MVMArrayBody *arr) {
#if MVM_ARRAY_CONC_DEBUG
if (!MVM_trycas(&(arr->in_use), 0, 1)) {
MVM_dump_backtrace(tc);
MVM_exception_throw_adhoc(tc, "Array may not be used concurrently");
}
#endif
}
static void exit_single_user(MVMThreadContext *tc, MVMArrayBody *arr) {
#if MVM_ARRAY_CONC_DEBUG
arr->in_use = 0;
#endif
}
#define MVM_MAX(a,b) ((a)>(b)?(a):(b))
#define MVM_MIN(a,b) ((a)<(b)?(a):(b))
/* Creates a new type object of this representation, and associates it with
* the given HOW. */
static MVMObject * type_object_for(MVMThreadContext *tc, MVMObject *HOW) {
MVMSTable *st = MVM_gc_allocate_stable(tc, &VMArray_this_repr, HOW);
MVMROOT(tc, st, {
MVMObject *obj = MVM_gc_allocate_type_object(tc, st);
MVMArrayREPRData *repr_data = (MVMArrayREPRData *)MVM_malloc(sizeof(MVMArrayREPRData));
repr_data->slot_type = MVM_ARRAY_OBJ;
repr_data->elem_size = sizeof(MVMObject *);
repr_data->elem_type = NULL;
MVM_ASSIGN_REF(tc, &(st->header), st->WHAT, obj);
st->size = sizeof(MVMArray);
st->REPR_data = repr_data;
});
return st->WHAT;
}
/* Copies the body of one object to another. The result has the space
* needed for the current number of elements, which may not be the
* entire allocated slot size. */
static void copy_to(MVMThreadContext *tc, MVMSTable *st, void *src, MVMObject *dest_root, void *dest) {
MVMArrayREPRData *repr_data = (MVMArrayREPRData *)st->REPR_data;
MVMArrayBody *src_body = (MVMArrayBody *)src;
MVMArrayBody *dest_body = (MVMArrayBody *)dest;
dest_body->elems = src_body->elems;
dest_body->ssize = src_body->elems;
dest_body->start = 0;
if (dest_body->elems > 0) {
size_t mem_size = dest_body->ssize * repr_data->elem_size;
size_t start_pos = src_body->start * repr_data->elem_size;
char *copy_start = ((char *)src_body->slots.any) + start_pos;
dest_body->slots.any = MVM_malloc(mem_size);
memcpy(dest_body->slots.any, copy_start, mem_size);
}
else {
dest_body->slots.any = NULL;
}
}
/* Adds held objects to the GC worklist. */
static void VMArray_gc_mark(MVMThreadContext *tc, MVMSTable *st, void *data, MVMGCWorklist *worklist) {
MVMArrayREPRData *repr_data = (MVMArrayREPRData *)st->REPR_data;
MVMArrayBody *body = (MVMArrayBody *)data;
MVMuint64 elems = body->elems;
MVMuint64 start = body->start;
MVMuint64 i = 0;
/* Aren't holding anything, nothing to do. */
if (elems == 0)
return;
switch (repr_data->slot_type) {
case MVM_ARRAY_OBJ: {
MVMObject **slots = body->slots.o;
slots += start;
MVM_gc_worklist_presize_for(tc, worklist, elems);
if (worklist->include_gen2) {
for (; i < elems; i++)
MVM_gc_worklist_add_include_gen2_nocheck(tc, worklist, &slots[i]);
}
else {
for (; i < elems; i++)
MVM_gc_worklist_add_no_include_gen2_nocheck(tc, worklist, &slots[i]);
}
break;
}
case MVM_ARRAY_STR: {
MVMString **slots = body->slots.s;
slots += start;
MVM_gc_worklist_presize_for(tc, worklist, elems);
if (worklist->include_gen2) {
for (; i < elems; i++)
MVM_gc_worklist_add_include_gen2_nocheck(tc, worklist, &slots[i]);
}
else {
for (; i < elems; i++)
MVM_gc_worklist_add_no_include_gen2_nocheck(tc, worklist, &slots[i]);
}
break;
}
}
}
/* Called by the VM in order to free memory associated with this object. */
static void gc_free(MVMThreadContext *tc, MVMObject *obj) {
MVMArray *arr = (MVMArray *)obj;
MVM_free(arr->body.slots.any);
}
/* Marks the representation data in an STable.*/
static void gc_mark_repr_data(MVMThreadContext *tc, MVMSTable *st, MVMGCWorklist *worklist) {
MVMArrayREPRData *repr_data = (MVMArrayREPRData *)st->REPR_data;
if (repr_data == NULL)
return;
MVM_gc_worklist_add(tc, worklist, &repr_data->elem_type);
}
/* Frees the representation data in an STable.*/
static void gc_free_repr_data(MVMThreadContext *tc, MVMSTable *st) {
MVM_free(st->REPR_data);
}
static const MVMStorageSpec storage_spec = {
MVM_STORAGE_SPEC_REFERENCE, /* inlineable */
0, /* bits */
0, /* align */
MVM_STORAGE_SPEC_BP_NONE, /* boxed_primitive */
0, /* can_box */
0, /* is_unsigned */
};
/* Gets the storage specification for this representation. */
static const MVMStorageSpec * get_storage_spec(MVMThreadContext *tc, MVMSTable *st) {
return &storage_spec;
}
void MVM_VMArray_at_pos(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMint64 index, MVMRegister *value, MVMuint16 kind) {
MVMArrayREPRData *repr_data = (MVMArrayREPRData *)st->REPR_data;
MVMArrayBody *body = (MVMArrayBody *)data;
MVMuint64 real_index;
/* Handle negative indexes. */
if (index < 0) {
index += body->elems;
if (index < 0)
MVM_exception_throw_adhoc(tc, "MVMArray: Index out of bounds");
}
real_index = (MVMuint64)index;
/* Go by type. */
switch (repr_data->slot_type) {
case MVM_ARRAY_OBJ:
if (kind != MVM_reg_obj)
MVM_exception_throw_adhoc(tc, "MVMArray: atpos expected object register");
if (real_index >= body->elems) {
value->o = tc->instance->VMNull;
}
else {
MVMObject *found = body->slots.o[body->start + real_index];
value->o = found ? found : tc->instance->VMNull;
}
break;
case MVM_ARRAY_STR:
if (kind != MVM_reg_str)
MVM_exception_throw_adhoc(tc, "MVMArray: atpos expected string register");
if (real_index >= body->elems)
value->s = NULL;
else
value->s = body->slots.s[body->start + real_index];
break;
case MVM_ARRAY_I64:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: atpos I64 expected int register");
if (real_index >= body->elems)
value->i64 = 0;
else
value->i64 = (MVMint64)body->slots.i64[body->start + real_index];
break;
case MVM_ARRAY_I32:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: atpos I32 expected int register");
if (real_index >= body->elems)
value->i64 = 0;
else
value->i64 = (MVMint64)body->slots.i32[body->start + real_index];
break;
case MVM_ARRAY_I16:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: atpos I16 expected int register");
if (real_index >= body->elems)
value->i64 = 0;
else
value->i64 = (MVMint64)body->slots.i16[body->start + real_index];
break;
case MVM_ARRAY_I8:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: atpos I8 expected int register");
if (real_index >= body->elems)
value->i64 = 0;
else
value->i64 = (MVMint64)body->slots.i8[body->start + real_index];
break;
case MVM_ARRAY_N64:
if (kind != MVM_reg_num64)
MVM_exception_throw_adhoc(tc, "MVMArray: atpos expected num register");
if (real_index >= body->elems)
value->n64 = 0.0;
else
value->n64 = (MVMnum64)body->slots.n64[body->start + real_index];
break;
case MVM_ARRAY_N32:
if (kind != MVM_reg_num64)
MVM_exception_throw_adhoc(tc, "MVMArray: atpos expected num register");
if (real_index >= body->elems)
value->n64 = 0.0;
else
value->n64 = (MVMnum64)body->slots.n32[body->start + real_index];
break;
case MVM_ARRAY_U64:
if (kind != MVM_reg_uint64 && kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: atpos U64 expected int register, got %d instead", kind);
if (real_index >= body->elems)
value->u64 = 0;
else
value->u64 = (MVMint64)body->slots.u64[body->start + real_index];
break;
case MVM_ARRAY_U32:
if (kind != MVM_reg_uint64 && kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: atpos U32 expected int register");
if (real_index >= body->elems)
value->u64 = 0;
else
value->u64 = (MVMint64)body->slots.u32[body->start + real_index];
break;
case MVM_ARRAY_U16:
if (kind != MVM_reg_uint64 && kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: atpos U16 expected int register");
if (real_index >= body->elems)
value->u64 = 0;
else
value->u64 = (MVMint64)body->slots.u16[body->start + real_index];
break;
case MVM_ARRAY_U8:
if (kind != MVM_reg_uint64 && kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: atpos U8 expected int register");
if (real_index >= body->elems)
value->u64 = 0;
else
value->u64 = (MVMint64)body->slots.u8[body->start + real_index];
break;
default:
MVM_exception_throw_adhoc(tc, "MVMArray: Unhandled slot type, got '%s'", MVM_reg_get_debug_name(tc, repr_data->slot_type));
}
}
static MVMuint64 zero_slots(MVMThreadContext *tc, MVMArrayBody *body,
MVMuint64 elems, MVMuint64 ssize, MVMuint8 slot_type) {
switch (slot_type) {
case MVM_ARRAY_OBJ:
memset(&(body->slots.o[elems]), 0, (ssize - elems) * sizeof(MVMObject *));
break;
case MVM_ARRAY_STR:
memset(&(body->slots.s[elems]), 0, (ssize - elems) * sizeof(MVMString *));
break;
case MVM_ARRAY_I64:
memset(&(body->slots.i64[elems]), 0, (ssize - elems) * sizeof(MVMint64));
break;
case MVM_ARRAY_I32:
memset(&(body->slots.i32[elems]), 0, (ssize - elems) * sizeof(MVMint32));
break;
case MVM_ARRAY_I16:
memset(&(body->slots.i16[elems]), 0, (ssize - elems) * sizeof(MVMint16));
break;
case MVM_ARRAY_I8:
memset(&(body->slots.i8[elems]), 0, (ssize - elems) * sizeof(MVMint8));
break;
case MVM_ARRAY_N64:
memset(&(body->slots.n64[elems]), 0, (ssize - elems) * sizeof(MVMnum64));
break;
case MVM_ARRAY_N32:
memset(&(body->slots.n32[elems]), 0, (ssize - elems) * sizeof(MVMnum32));
break;
case MVM_ARRAY_U64:
memset(&(body->slots.u64[elems]), 0, (ssize - elems) * sizeof(MVMuint64));
break;
case MVM_ARRAY_U32:
memset(&(body->slots.u32[elems]), 0, (ssize - elems) * sizeof(MVMuint32));
break;
case MVM_ARRAY_U16:
memset(&(body->slots.u16[elems]), 0, (ssize - elems) * sizeof(MVMuint16));
break;
case MVM_ARRAY_U8:
memset(&(body->slots.u8[elems]), 0, (ssize - elems) * sizeof(MVMuint8));
break;
default:
MVM_exception_throw_adhoc(tc, "MVMArray: Unhandled slot type");
}
return elems;
}
static void set_size_internal(MVMThreadContext *tc, MVMArrayBody *body, MVMuint64 n, MVMArrayREPRData *repr_data) {
MVMuint64 elems = body->elems;
MVMuint64 start = body->start;
MVMuint64 ssize = body->ssize;
void *slots = body->slots.any;
if (n == elems)
return;
if (start > 0 && n + start > ssize) {
/* if there aren't enough slots at the end, shift off empty slots
* from the beginning first */
if (elems > 0)
memmove(slots,
(char *)slots + start * repr_data->elem_size,
elems * repr_data->elem_size);
body->start = 0;
/* fill out any unused slots with NULL pointers or zero values */
zero_slots(tc, body, elems, start+elems, repr_data->slot_type);
elems = ssize; /* we'll use this as a point to clear from later */
}
else if (n < elems) {
/* we're downsizing; clear off extra slots */
zero_slots(tc, body, n+start, start+elems, repr_data->slot_type);
}
if (n <= ssize) {
/* we already have n slots available, we can just return */
body->elems = n;
return;
}
/* We need more slots. If the current slot size is less
* than 8K, use the larger of twice the current slot size
* or the actual number of elements needed. Otherwise,
* grow the slots to the next multiple of 4096 (0x1000). */
if (ssize < 8192) {
ssize *= 2;
if (n > ssize) ssize = n;
if (ssize < 8) ssize = 8;
}
else {
ssize = (n + 0x1000) & ~0xfffUL;
}
{
/* Our budget is 2^(
* <number of bits in an array index>
* - <number of bits to address individual bytes in an array element>
* ) */
size_t const elem_addr_size = repr_data->elem_size == 8 ? 4 :
repr_data->elem_size == 4 ? 3 :
repr_data->elem_size == 2 ? 2 :
1;
if (ssize > (1ULL << (CHAR_BIT * sizeof(size_t) - elem_addr_size)))
MVM_exception_throw_adhoc(tc,
"Unable to allocate an array of %"PRIu64" elements",
ssize);
}
/* now allocate the new slot buffer */
slots = (slots)
? MVM_realloc(slots, ssize * repr_data->elem_size)
: MVM_malloc(ssize * repr_data->elem_size);
/* fill out any unused slots with NULL pointers or zero values */
body->slots.any = slots;
zero_slots(tc, body, elems, ssize, repr_data->slot_type);
body->ssize = ssize;
/* set elems last so no thread tries to access slots before they are available */
body->elems = n;
}
void MVM_VMArray_bind_pos(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMint64 index, MVMRegister value, MVMuint16 kind) {
MVMArrayREPRData *repr_data = (MVMArrayREPRData *)st->REPR_data;
MVMArrayBody *body = (MVMArrayBody *)data;
MVMuint64 real_index;
/* Handle negative indexes and resizing if needed. */
enter_single_user(tc, body);
if (index < 0) {
index += body->elems;
if (index < 0)
MVM_exception_throw_adhoc(tc, "MVMArray: Index out of bounds");
}
else if ((MVMuint64)index >= body->elems)
set_size_internal(tc, body, (MVMuint64)index + 1, repr_data);
real_index = (MVMuint64)index;
/* Go by type. */
switch (repr_data->slot_type) {
case MVM_ARRAY_OBJ:
if (kind != MVM_reg_obj)
MVM_exception_throw_adhoc(tc, "MVMArray: bindpos expected object register");
MVM_ASSIGN_REF(tc, &(root->header), body->slots.o[body->start + real_index], value.o);
break;
case MVM_ARRAY_STR:
if (kind != MVM_reg_str)
MVM_exception_throw_adhoc(tc, "MVMArray: bindpos expected string register");
MVM_ASSIGN_REF(tc, &(root->header), body->slots.s[body->start + real_index], value.s);
break;
case MVM_ARRAY_I64:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: bindpos I64 expected int register");
body->slots.i64[body->start + real_index] = value.i64;
break;
case MVM_ARRAY_I32:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: bindpos I32 expected int register");
body->slots.i32[body->start + real_index] = (MVMint32)value.i64;
break;
case MVM_ARRAY_I16:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: bindpos I16 expected int register");
body->slots.i16[body->start + real_index] = (MVMint16)value.i64;
break;
case MVM_ARRAY_I8:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: bindpos I8 expected int register");
body->slots.i8[body->start + real_index] = (MVMint8)value.i64;
break;
case MVM_ARRAY_N64:
if (kind != MVM_reg_num64)
MVM_exception_throw_adhoc(tc, "MVMArray: bindpos expected num register");
body->slots.n64[body->start + real_index] = value.n64;
break;
case MVM_ARRAY_N32:
if (kind != MVM_reg_num64)
MVM_exception_throw_adhoc(tc, "MVMArray: bindpos expected num register");
body->slots.n32[body->start + real_index] = (MVMnum32)value.n64;
break;
case MVM_ARRAY_U64:
if (kind != MVM_reg_uint64 && kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: bindpos U64 expected int register");
body->slots.u64[body->start + real_index] = value.i64;
break;
case MVM_ARRAY_U32:
if (kind != MVM_reg_uint64 && kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: bindpos U32 expected int register");
body->slots.u32[body->start + real_index] = (MVMuint32)value.i64;
break;
case MVM_ARRAY_U16:
if (kind != MVM_reg_uint64 && kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: bindpos U16 expected int register");
body->slots.u16[body->start + real_index] = (MVMuint16)value.i64;
break;
case MVM_ARRAY_U8:
if (kind != MVM_reg_uint64 && kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: bindpos U8 expected int register");
body->slots.u8[body->start + real_index] = (MVMuint8)value.i64;
break;
default:
MVM_exception_throw_adhoc(tc, "MVMArray: Unhandled slot type");
}
exit_single_user(tc, body);
}
static MVMuint64 elems(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data) {
MVMArrayBody *body = (MVMArrayBody *)data;
return body->elems;
}
static void set_elems(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMuint64 count) {
MVMArrayREPRData *repr_data = (MVMArrayREPRData *)st->REPR_data;
MVMArrayBody *body = (MVMArrayBody *)data;
enter_single_user(tc, body);
set_size_internal(tc, body, count, repr_data);
exit_single_user(tc, body);
}
void MVM_VMArray_push(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMRegister value, MVMuint16 kind) {
MVMArrayBody *body = (MVMArrayBody *)data;
MVMArrayREPRData *repr_data = (MVMArrayREPRData *)st->REPR_data;
enter_single_user(tc, body);
set_size_internal(tc, body, body->elems + 1, repr_data);
switch (repr_data->slot_type) {
case MVM_ARRAY_OBJ:
if (kind != MVM_reg_obj)
MVM_exception_throw_adhoc(tc, "MVMArray: push expected object register");
MVM_ASSIGN_REF(tc, &(root->header), body->slots.o[body->start + body->elems - 1], value.o);
break;
case MVM_ARRAY_STR:
if (kind != MVM_reg_str)
MVM_exception_throw_adhoc(tc, "MVMArray: push expected string register");
MVM_ASSIGN_REF(tc, &(root->header), body->slots.s[body->start + body->elems - 1], value.s);
break;
case MVM_ARRAY_I64:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: push expected int register");
body->slots.i64[body->start + body->elems - 1] = value.i64;
break;
case MVM_ARRAY_I32:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: push expected int register");
body->slots.i32[body->start + body->elems - 1] = (MVMint32)value.i64;
break;
case MVM_ARRAY_I16:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: push expected int register");
body->slots.i16[body->start + body->elems - 1] = (MVMint16)value.i64;
break;
case MVM_ARRAY_I8:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: push expected int register");
body->slots.i8[body->start + body->elems - 1] = (MVMint8)value.i64;
break;
case MVM_ARRAY_N64:
if (kind != MVM_reg_num64)
MVM_exception_throw_adhoc(tc, "MVMArray: push expected num register");
body->slots.n64[body->start + body->elems - 1] = value.n64;
break;
case MVM_ARRAY_N32:
if (kind != MVM_reg_num64)
MVM_exception_throw_adhoc(tc, "MVMArray: push expected num register");
body->slots.n32[body->start + body->elems - 1] = (MVMnum32)value.n64;
break;
case MVM_ARRAY_U64:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: push expected int register");
body->slots.u64[body->start + body->elems - 1] = (MVMuint64)value.i64;
break;
case MVM_ARRAY_U32:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: push expected int register");
body->slots.u32[body->start + body->elems - 1] = (MVMuint32)value.i64;
break;
case MVM_ARRAY_U16:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: push expected int register");
body->slots.u16[body->start + body->elems - 1] = (MVMuint16)value.i64;
break;
case MVM_ARRAY_U8:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: push expected int register");
body->slots.u8[body->start + body->elems - 1] = (MVMuint8)value.i64;
break;
default:
MVM_exception_throw_adhoc(tc, "MVMArray: Unhandled slot type");
}
exit_single_user(tc, body);
}
static void pop(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMRegister *value, MVMuint16 kind) {
MVMArrayREPRData *repr_data = (MVMArrayREPRData *)st->REPR_data;
MVMArrayBody *body = (MVMArrayBody *)data;
const MVMuint64 slot = body->start + body->elems - 1;
if (body->elems < 1)
MVM_exception_throw_adhoc(tc,
"MVMArray: Can't pop from an empty array");
enter_single_user(tc, body);
body->elems--;
switch (repr_data->slot_type) {
case MVM_ARRAY_OBJ:
if (kind != MVM_reg_obj)
MVM_exception_throw_adhoc(tc, "MVMArray: pop expected object register");
value->o = body->slots.o[slot];
break;
case MVM_ARRAY_STR:
if (kind != MVM_reg_str)
MVM_exception_throw_adhoc(tc, "MVMArray: pop expected string register");
value->s = body->slots.s[slot];
break;
case MVM_ARRAY_I64:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: pop expected int register");
value->i64 = (MVMint64)body->slots.i64[slot];
break;
case MVM_ARRAY_I32:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: pop expected int register");
value->i64 = (MVMint64)body->slots.i32[slot];
break;
case MVM_ARRAY_I16:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: pop expected int register");
value->i64 = (MVMint64)body->slots.i16[slot];
break;
case MVM_ARRAY_I8:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: pop expected int register");
value->i64 = (MVMint64)body->slots.i8[slot];
break;
case MVM_ARRAY_N64:
if (kind != MVM_reg_num64)
MVM_exception_throw_adhoc(tc, "MVMArray: pop expected num register");
value->n64 = (MVMnum64)body->slots.n64[slot];
break;
case MVM_ARRAY_N32:
if (kind != MVM_reg_num64)
MVM_exception_throw_adhoc(tc, "MVMArray: pop expected num register");
value->n64 = (MVMnum64)body->slots.n32[slot];
break;
case MVM_ARRAY_U64:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: pop expected int register");
value->i64 = (MVMint64)body->slots.u64[slot];
break;
case MVM_ARRAY_U32:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: pop expected int register");
value->i64 = (MVMint64)body->slots.u32[slot];
break;
case MVM_ARRAY_U16:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: pop expected int register");
value->i64 = (MVMint64)body->slots.u16[slot];
break;
case MVM_ARRAY_U8:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: pop expected int register");
value->i64 = (MVMint64)body->slots.u8[slot];
break;
default:
MVM_exception_throw_adhoc(tc, "MVMArray: Unhandled slot type");
}
zero_slots(tc, body, slot, slot + 1, repr_data->slot_type);
exit_single_user(tc, body);
}
static void unshift(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMRegister value, MVMuint16 kind) {
MVMArrayREPRData *repr_data = (MVMArrayREPRData *)st->REPR_data;
MVMArrayBody *body = (MVMArrayBody *)data;
/* If we don't have room at the beginning of the slots, make some
* room for unshifting. We make room for a minimum of 8 elements, but
* for cases where we're just continuously unshifting factor in the
* body size too - however also apply an upper limit on that as in the
* push-based growth. */
enter_single_user(tc, body);
if (body->start < 1) {
MVMuint64 elems = body->elems;
MVMuint64 n = MVM_MIN(MVM_MAX(elems, 8), 8192);
/* grow the array */
set_size_internal(tc, body, elems + n, repr_data);
/* move elements and set start */
memmove(
(char *)body->slots.any + n * repr_data->elem_size,
body->slots.any,
elems * repr_data->elem_size);
body->start = n;
body->elems = elems;
/* clear out beginning elements */
zero_slots(tc, body, 0, n, repr_data->slot_type);
}
/* Now do the unshift */
body->start--;
switch (repr_data->slot_type) {
case MVM_ARRAY_OBJ:
if (kind != MVM_reg_obj)
MVM_exception_throw_adhoc(tc, "MVMArray: unshift expected object register");
MVM_ASSIGN_REF(tc, &(root->header), body->slots.o[body->start], value.o);
break;
case MVM_ARRAY_STR:
if (kind != MVM_reg_str)
MVM_exception_throw_adhoc(tc, "MVMArray: unshift expected string register");
MVM_ASSIGN_REF(tc, &(root->header), body->slots.s[body->start], value.s);
break;
case MVM_ARRAY_I64:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: unshift expected int register");
body->slots.i64[body->start] = value.i64;
break;
case MVM_ARRAY_I32:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: unshift expected int register");
body->slots.i32[body->start] = (MVMint32)value.i64;
break;
case MVM_ARRAY_I16:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: unshift expected int register");
body->slots.i16[body->start] = (MVMint16)value.i64;
break;
case MVM_ARRAY_I8:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: unshift expected int register");
body->slots.i8[body->start] = (MVMint8)value.i64;
break;
case MVM_ARRAY_N64:
if (kind != MVM_reg_num64)
MVM_exception_throw_adhoc(tc, "MVMArray: unshift expected num register");
body->slots.n64[body->start] = value.n64;
break;
case MVM_ARRAY_N32:
if (kind != MVM_reg_num64)
MVM_exception_throw_adhoc(tc, "MVMArray: unshift expected num register");
body->slots.n32[body->start] = (MVMnum32)value.n64;
break;
case MVM_ARRAY_U64:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: unshift expected int register");
body->slots.u64[body->start] = (MVMuint64)value.i64;
break;
case MVM_ARRAY_U32:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: unshift expected int register");
body->slots.u32[body->start] = (MVMuint32)value.i64;
break;
case MVM_ARRAY_U16:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: unshift expected int register");
body->slots.u16[body->start] = (MVMuint16)value.i64;
break;
case MVM_ARRAY_U8:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: unshift expected int register");
body->slots.u8[body->start] = (MVMuint8)value.i64;
break;
default:
MVM_exception_throw_adhoc(tc, "MVMArray: Unhandled slot type");
}
body->elems++;
exit_single_user(tc, body);
}
static void shift(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMRegister *value, MVMuint16 kind) {
MVMArrayBody *body = (MVMArrayBody *)data;
MVMArrayREPRData *repr_data = (MVMArrayREPRData *)st->REPR_data;
if (body->elems < 1)
MVM_exception_throw_adhoc(tc,
"MVMArray: Can't shift from an empty array");
enter_single_user(tc, body);
switch (repr_data->slot_type) {
case MVM_ARRAY_OBJ:
if (kind != MVM_reg_obj)
MVM_exception_throw_adhoc(tc, "MVMArray: shift expected object register");
value->o = body->slots.o[body->start];
break;
case MVM_ARRAY_STR:
if (kind != MVM_reg_str)
MVM_exception_throw_adhoc(tc, "MVMArray: shift expected string register");
value->s = body->slots.s[body->start];
break;
case MVM_ARRAY_I64:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: shift expected int register");
value->i64 = (MVMint64)body->slots.i64[body->start];
break;
case MVM_ARRAY_I32:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: shift expected int register");
value->i64 = (MVMint64)body->slots.i32[body->start];
break;
case MVM_ARRAY_I16:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: shift expected int register");
value->i64 = (MVMint64)body->slots.i16[body->start];
break;
case MVM_ARRAY_I8:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: shift expected int register");
value->i64 = (MVMint64)body->slots.i8[body->start];
break;
case MVM_ARRAY_N64:
if (kind != MVM_reg_num64)
MVM_exception_throw_adhoc(tc, "MVMArray: shift expected num register");
value->n64 = (MVMnum64)body->slots.n64[body->start];
break;
case MVM_ARRAY_N32:
if (kind != MVM_reg_num64)
MVM_exception_throw_adhoc(tc, "MVMArray: shift expected num register");
value->n64 = (MVMnum64)body->slots.n32[body->start];
break;
case MVM_ARRAY_U64:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: shift expected int register");
value->i64 = (MVMint64)body->slots.u64[body->start];
break;
case MVM_ARRAY_U32:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: shift expected int register");
value->i64 = (MVMint64)body->slots.u32[body->start];
break;
case MVM_ARRAY_U16:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: shift expected int register");
value->i64 = (MVMint64)body->slots.u16[body->start];
break;
case MVM_ARRAY_U8:
if (kind != MVM_reg_int64)
MVM_exception_throw_adhoc(tc, "MVMArray: shift expected int register");
value->i64 = (MVMint64)body->slots.u8[body->start];
break;
default:
MVM_exception_throw_adhoc(tc, "MVMArray: Unhandled slot type");
}
body->start++;
body->elems--;
exit_single_user(tc, body);
}
static MVMuint16 slot_type_to_kind(MVMuint8 slot_type) {
switch (slot_type) {
case MVM_ARRAY_OBJ:
return MVM_reg_obj;
break;
case MVM_ARRAY_STR:
return MVM_reg_str;
break;
case MVM_ARRAY_I64:
case MVM_ARRAY_I32:
case MVM_ARRAY_I16:
case MVM_ARRAY_I8:
return MVM_reg_int64;
break;
case MVM_ARRAY_N64:
case MVM_ARRAY_N32:
return MVM_reg_num64;
break;
case MVM_ARRAY_U64:
case MVM_ARRAY_U32:
case MVM_ARRAY_U16:
case MVM_ARRAY_U8:
return MVM_reg_uint64;
break;
default:
abort(); /* never reached, silence compiler warnings */
}
}
static void copy_elements(MVMThreadContext *tc, MVMObject *src, MVMObject *dest, MVMint64 s_offset, MVMint64 d_offset, MVMint64 elems) {
MVMArrayBody *s_body = (MVMArrayBody *)OBJECT_BODY(src);
MVMArrayBody *d_body = (MVMArrayBody *)OBJECT_BODY(dest);
MVMArrayREPRData *s_repr_data = REPR(src)->ID == MVM_REPR_ID_VMArray
? (MVMArrayREPRData *)STABLE(src)->REPR_data : NULL;
MVMArrayREPRData *d_repr_data = (MVMArrayREPRData *)STABLE(dest)->REPR_data;
if (elems > 0) {
MVMint64 i;
MVMuint8 d_needs_barrier = dest->header.flags2 & MVM_CF_SECOND_GEN;
if (s_repr_data
&& s_repr_data->slot_type == d_repr_data->slot_type
&& s_repr_data->elem_size == d_repr_data->elem_size
&& (d_repr_data->slot_type != MVM_ARRAY_OBJ || !d_needs_barrier)
&& d_repr_data->slot_type != MVM_ARRAY_STR) {
/* Optimized for copying from a VMArray with same slot type */
MVMint64 s_start = s_body->start;
MVMint64 d_start = d_body->start;
memcpy( d_body->slots.u8 + (d_start + d_offset) * d_repr_data->elem_size,
s_body->slots.u8 + (s_start + s_offset) * s_repr_data->elem_size,
d_repr_data->elem_size * elems
);
}
else {
MVMuint16 target_kind = slot_type_to_kind(d_repr_data->slot_type);
MVMuint16 source_kind = slot_type_to_kind(s_repr_data->slot_type);
for (i = 0; i < elems; i++) {
MVMRegister to_copy;
REPR(src)->pos_funcs.at_pos(tc, STABLE(src), src, s_body, s_offset + i, &to_copy, source_kind);
/* actually should coerce between source_kind and target_kind here */
MVM_VMArray_bind_pos(tc, STABLE(dest), dest, d_body, d_offset + i, to_copy, target_kind);
}
}
}
}
static void aslice(MVMThreadContext *tc, MVMSTable *st, MVMObject *src, void *data, MVMObject *dest, MVMint64 start, MVMint64 end) {
MVMArrayBody *s_body = (MVMArrayBody *)data;
MVMArrayBody *d_body = (MVMArrayBody *)OBJECT_BODY(dest);
MVMArrayREPRData *d_repr_data = STABLE(dest)->REPR_data;
MVMint64 total_elems = REPR(src)->elems(tc, st, src, s_body);
MVMint64 elems;
start = start < 0 ? total_elems + start : start;
end = end < 0 ? total_elems + end : end;
if ( end < start || start < 0 || end < 0 || total_elems <= start || total_elems <= end ) {
MVM_exception_throw_adhoc(tc, "MVMArray: Slice index out of bounds");
}
elems = end - start + 1;
set_size_internal(tc, d_body, elems, d_repr_data);
copy_elements(tc, src, dest, start, 0, elems);
}
static void write_buf(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, char *from, MVMint64 offset, MVMuint64 count) {
MVMArrayREPRData *repr_data = (MVMArrayREPRData *)st->REPR_data;
MVMArrayBody *body = (MVMArrayBody *)data;
MVMuint64 start = body->start;
MVMuint64 elems = body->elems;
/* Throw on invalid slot type */
if (repr_data->slot_type < MVM_ARRAY_I64) {
MVM_exception_throw_adhoc(tc, "MVMArray: write_buf requires an integer type");
}
/* Throw on negative offset. */
if (offset < 0) {
MVM_exception_throw_adhoc(tc, "MVMArray: Index out of bounds");
}
/* resize the array if necessary*/
size_t elem_size = repr_data->elem_size;
/* No need to account for start, set_size_internal will take care of that */
if (elems * elem_size < offset * elem_size + count)
set_size_internal(tc, body, offset + count, repr_data);
memcpy(body->slots.u8 + (start + offset) * repr_data->elem_size, from, count);
}
static MVMint64 read_buf(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMint64 offset, MVMuint64 count) {
MVMArrayREPRData *repr_data = (MVMArrayREPRData *)st->REPR_data;
MVMArrayBody *body = (MVMArrayBody *)data;
MVMint64 start = body->start;
MVMint64 result = 0;
size_t elem_size = repr_data->elem_size;
/* Throw on invalid slot type */
if (repr_data->slot_type < MVM_ARRAY_I64) {
MVM_exception_throw_adhoc(tc, "MVMArray: read_buf requires an integer type");
}
if (offset < 0 || (start + body->elems) * elem_size < offset * elem_size + count) {
MVM_exception_throw_adhoc(tc, "MVMArray: read_buf out of bounds offset %"PRIi64" start %"PRIi64" elems %"PRIu64" count %"PRIu64, offset, start, body->elems, count);
}
memcpy(((char*)&result)
#if MVM_BIGENDIAN
+ (8 - count)
#endif
, body->slots.u8 + (start + offset) * repr_data->elem_size, count);
return result;
}
/* This whole splice optimization can be optimized for the case we have two
* MVMArray representation objects. */
static void asplice(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMObject *from, MVMint64 offset, MVMuint64 count) {
MVMArrayREPRData *repr_data = (MVMArrayREPRData *)st->REPR_data;
MVMArrayBody *body = (MVMArrayBody *)data;
MVMuint64 elems0 = body->elems;
MVMuint64 elems1 = REPR(from)->elems(tc, STABLE(from), from, OBJECT_BODY(from));
MVMint64 start;
MVMint64 tail;
/* start from end? */
if (offset < 0) {
offset += elems0;
if (offset < 0)
MVM_exception_throw_adhoc(tc,
"MVMArray: Illegal splice offset");
}
enter_single_user(tc, body);
/* When offset == 0, then we may be able to reduce the memmove
* calls and reallocs by adjusting SELF's start, elems0, and
* count to better match the incoming splice. In particular,
* we're seeking to adjust C<count> to as close to C<elems1>
* as we can. */
if (offset == 0) {
MVMint64 n = elems1 - count;
start = body->start;
if (n > start)
n = start;
if (n <= -(MVMint64)elems0) {
elems0 = 0;
count = 0;
body->start = 0;
body->elems = elems0;
}
else if (n != 0) {
elems0 += n;
count += n;
body->start = start - n;
body->elems = elems0;
}
}
/* if count == 0 and elems1 == 0, there's nothing left
* to copy or remove, so the splice is done! */
if (count == 0 && elems1 == 0) {
exit_single_user(tc, body);
return;
}
/* number of elements to right of splice (the "tail") */
tail = elems0 - offset - count;
if (tail < 0)
tail = 0;
else if (tail > 0 && count > elems1) {