/
P6opaque.c
1225 lines (1075 loc) · 46.6 KB
/
P6opaque.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
/* A mostly complete implementation of P6opaque, which supports inlining native
* types, box/unbox of native types where it's declared possible and doing just
* a single memory allocation in addition to the PMC header per object. For
* single inheritance, memory is laid out parent to child, so that the slot
* given to an attribute with be valid in all SI subclasses. For MI we just
* go with the MRO to do slot allocations, but we never allow an actual index
* lookup with a hint to take place and always slow-path it. */
#include "parrot/parrot.h"
#include "parrot/extend.h"
#include "parrot/6model/sixmodelobject.h"
#include "parrot/6model/repr/P6opaque.h"
/* HEADERIZER HFILE: none */
/* HEADERIZER BEGIN: static */
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
static PMC * accessor_call(PARROT_INTERP, PMC *obj, STRING *name)
__attribute__nonnull__(1);
static void bind_attribute(PARROT_INTERP,
PMC *obj,
PMC *class_handle,
STRING *name,
INTVAL hint,
PMC *value)
__attribute__nonnull__(1);
static void bind_attribute_int(PARROT_INTERP,
PMC *obj,
PMC *class_handle,
STRING *name,
INTVAL hint,
INTVAL value)
__attribute__nonnull__(1);
static void bind_attribute_num(PARROT_INTERP,
PMC *obj,
PMC *class_handle,
STRING *name,
INTVAL hint,
FLOATVAL value)
__attribute__nonnull__(1);
static void bind_attribute_str(PARROT_INTERP,
PMC *obj,
PMC *class_handle,
STRING *name,
INTVAL hint,
STRING *value)
__attribute__nonnull__(1);
static void change_type(PARROT_INTERP, PMC *obj, PMC *new_type)
__attribute__nonnull__(1);
static void compute_allocation_strategy(PARROT_INTERP,
PMC *WHAT,
P6opaqueREPRData *repr_data)
__attribute__nonnull__(1);
static INTVAL defined(PARROT_INTERP, PMC *obj)
__attribute__nonnull__(1);
static void gc_free(PARROT_INTERP, PMC *obj)
__attribute__nonnull__(1);
static void gc_free_repr(PARROT_INTERP, STable *st)
__attribute__nonnull__(1);
static void gc_mark(PARROT_INTERP, PMC *obj)
__attribute__nonnull__(1);
static void gc_mark_repr(PARROT_INTERP, STable *st)
__attribute__nonnull__(1);
static PMC * get_attribute(PARROT_INTERP,
PMC *obj,
PMC *class_handle,
STRING *name,
INTVAL hint)
__attribute__nonnull__(1);
static INTVAL get_attribute_int(PARROT_INTERP,
PMC *obj,
PMC *class_handle,
STRING *name,
INTVAL hint)
__attribute__nonnull__(1);
static FLOATVAL get_attribute_num(PARROT_INTERP,
PMC *obj,
PMC *class_handle,
STRING *name,
INTVAL hint)
__attribute__nonnull__(1);
static STRING * get_attribute_str(PARROT_INTERP,
PMC *obj,
PMC *class_handle,
STRING *name,
INTVAL hint)
__attribute__nonnull__(1);
static INTVAL get_int(PARROT_INTERP, PMC *obj)
__attribute__nonnull__(1);
static INTVAL get_int_at_offset(void *data, INTVAL offset);
static FLOATVAL get_num(PARROT_INTERP, PMC *obj)
__attribute__nonnull__(1);
static FLOATVAL get_num_at_offset(void *data, INTVAL offset);
static PMC * get_pmc_at_offset(void *data, INTVAL offset);
static storage_spec get_storage_spec(PARROT_INTERP, STable *st)
__attribute__nonnull__(1);
static STRING * get_str(PARROT_INTERP, PMC *obj)
__attribute__nonnull__(1);
static STRING * get_str_at_offset(void *data, INTVAL offset);
static INTVAL hint_for(PARROT_INTERP, PMC *class_handle, STRING *name)
__attribute__nonnull__(1);
static PMC * instance_of(PARROT_INTERP, PMC *WHAT)
__attribute__nonnull__(1);
static PMC * introspection_call(PARROT_INTERP,
PMC *WHAT,
PMC *HOW,
STRING *name,
INTVAL local)
__attribute__nonnull__(1);
static INTVAL is_attribute_initialized(PARROT_INTERP,
PMC *obj,
PMC *class_handle,
STRING *name,
INTVAL hint)
__attribute__nonnull__(1);
static void no_such_attribute(PARROT_INTERP,
char *action,
PMC *class_handle,
STRING *name)
__attribute__nonnull__(1);
static PMC * repr_clone(PARROT_INTERP, PMC *to_clone)
__attribute__nonnull__(1);
static void set_int(PARROT_INTERP, PMC *obj, INTVAL value)
__attribute__nonnull__(1);
static void set_int_at_offset(void *data, INTVAL offset, INTVAL value);
static void set_num(PARROT_INTERP, PMC *obj, FLOATVAL value)
__attribute__nonnull__(1);
static void set_num_at_offset(void *data, INTVAL offset, FLOATVAL value);
static void set_pmc_at_offset(void *data, INTVAL offset, PMC *value);
static void set_str(PARROT_INTERP, PMC *obj, STRING *value)
__attribute__nonnull__(1);
static void set_str_at_offset(void *data, INTVAL offset, STRING *value);
static INTVAL try_get_slot(PARROT_INTERP,
P6opaqueREPRData *repr_data,
PMC *class_key,
STRING *name)
__attribute__nonnull__(1);
static PMC * type_object_for(PARROT_INTERP, PMC *HOW)
__attribute__nonnull__(1);
#define ASSERT_ARGS_accessor_call __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_bind_attribute __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_bind_attribute_int __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_bind_attribute_num __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_bind_attribute_str __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_change_type __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_compute_allocation_strategy __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_defined __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_free __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_free_repr __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_mark __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_mark_repr __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_get_attribute __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_get_attribute_int __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_get_attribute_num __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_get_attribute_str __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_get_int __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_get_int_at_offset __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_get_num __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_get_num_at_offset __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_get_pmc_at_offset __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_get_storage_spec __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_get_str __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_get_str_at_offset __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_hint_for __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_instance_of __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_introspection_call __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_is_attribute_initialized __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_no_such_attribute __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_repr_clone __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_set_int __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_set_int_at_offset __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_set_num __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_set_num_at_offset __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_set_pmc_at_offset __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_set_str __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_set_str_at_offset __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_try_get_slot __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_type_object_for __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
/* HEADERIZER END: static */
/* This representation's function pointer table. */
static REPROps *this_repr;
/* How do we go from type-object to a hash value? For now, we make an integer
* that is the address of the STable struct, which not being subject to GC will
* never move, and is unique per type object too. */
#define CLASS_KEY(c) ((INTVAL)PMC_data(STABLE_PMC(c)))
/* Helper to make an introspection call, possibly with :local. */
static PMC *
introspection_call(PARROT_INTERP, PMC *WHAT, PMC *HOW, STRING *name, INTVAL local)
{
PMC *old_ctx, *cappy;
/* Look up method; if there is none hand back a null. */
PMC *meth = VTABLE_find_method(interp, HOW, name);
if (PMC_IS_NULL(meth))
return meth;
/* Set up call capture. */
old_ctx = Parrot_pcc_get_signature(interp, CURRENT_CONTEXT(interp));
cappy = Parrot_pmc_new(interp, enum_class_CallContext);
VTABLE_push_pmc(interp, cappy, HOW);
VTABLE_push_pmc(interp, cappy, WHAT);
if (local)
VTABLE_set_integer_keyed_str(interp, cappy, Parrot_str_new_constant(interp, "local"), 1);
/* Call. */
Parrot_pcc_invoke_from_sig_object(interp, meth, cappy);
/* Grab result. */
cappy = Parrot_pcc_get_signature(interp, CURRENT_CONTEXT(interp));
Parrot_pcc_set_signature(interp, CURRENT_CONTEXT(interp), old_ctx);
return VTABLE_get_pmc_keyed_int(interp, cappy, 0);
}
/* Helper to make an accessor call. */
static PMC *
accessor_call(PARROT_INTERP, PMC *obj, STRING *name)
{
PMC *old_ctx, *cappy;
/* Look up method; if there is none hand back a null. */
PMC *meth = VTABLE_find_method(interp, obj, name);
if (PMC_IS_NULL(meth))
return meth;
/* Set up call capture. */
old_ctx = Parrot_pcc_get_signature(interp, CURRENT_CONTEXT(interp));
cappy = Parrot_pmc_new(interp, enum_class_CallContext);
VTABLE_push_pmc(interp, cappy, obj);
/* Call. */
Parrot_pcc_invoke_from_sig_object(interp, meth, cappy);
/* Grab result. */
cappy = Parrot_pcc_get_signature(interp, CURRENT_CONTEXT(interp));
Parrot_pcc_set_signature(interp, CURRENT_CONTEXT(interp), old_ctx);
return VTABLE_get_pmc_keyed_int(interp, cappy, 0);
}
/* Locates all of the attributes. Puts them onto a flattened, ordered
* list of attributes (populating the passed flat_list). Also builds
* the index mapping for doing named lookups. Note index is not related
* to the storage position. */
PMC *
index_mapping_and_flat_list(PARROT_INTERP, PMC *WHAT, P6opaqueREPRData *repr_data)
{
PMC *flat_list = pmc_new(interp, enum_class_ResizablePMCArray);
PMC *class_list = pmc_new(interp, enum_class_ResizablePMCArray);
PMC *attr_map_list = pmc_new(interp, enum_class_ResizablePMCArray);
STRING *attributes_str = Parrot_str_new_constant(interp, "attributes");
STRING *parents_str = Parrot_str_new_constant(interp, "parents");
STRING *name_str = Parrot_str_new_constant(interp, "name");
STRING *mro_str = Parrot_str_new_constant(interp, "mro");
INTVAL current_slot = 0;
INTVAL num_classes, i;
P6opaqueNameMap * result = NULL;
/* Get the MRO. */
PMC *mro = introspection_call(interp, WHAT, STABLE(WHAT)->HOW, mro_str, 0);
INTVAL mro_idx = VTABLE_elements(interp, mro);
/* Walk through the parents list. */
while (mro_idx)
{
/* Get current class in MRO. */
PMC *current_class = decontainerize(interp, VTABLE_get_pmc_keyed_int(interp, mro, --mro_idx));
PMC *HOW = STABLE(current_class)->HOW;
/* Get its local parents. */
PMC *parents = introspection_call(interp, current_class, HOW, parents_str, 1);
INTVAL num_parents = VTABLE_elements(interp, parents);
/* Get attributes and iterate over them. */
PMC *attributes = introspection_call(interp, current_class, HOW, attributes_str, 1);
PMC *attr_map = PMCNULL;
PMC *attr_iter = VTABLE_get_iter(interp, attributes);
while (VTABLE_get_bool(interp, attr_iter)) {
/* Get attribute. */
PMC * attr = VTABLE_shift_pmc(interp, attr_iter);
/* Get its name. */
PMC *name_pmc = accessor_call(interp, attr, name_str);
STRING *name = VTABLE_get_string(interp, name_pmc);
/* Allocate a slot. */
if (PMC_IS_NULL(attr_map))
attr_map = pmc_new(interp, enum_class_Hash);
VTABLE_set_pmc_keyed_str(interp, attr_map, name,
Parrot_pmc_new_init_int(interp, enum_class_Integer, current_slot));
current_slot++;
/* Push attr onto the flat list. */
VTABLE_push_pmc(interp, flat_list, attr);
}
/* Add to class list and map list. */
VTABLE_push_pmc(interp, class_list, current_class);
VTABLE_push_pmc(interp, attr_map_list, attr_map);
/* If there's more than one parent, flag that we in an MI
* situation. */
if (num_parents > 1)
repr_data->mi = 1;
}
/* We can now form the name map. */
num_classes = VTABLE_elements(interp, class_list);
result = (P6opaqueNameMap *) mem_sys_allocate_zeroed(sizeof(P6opaqueNameMap) * (1 + num_classes));
for (i = 0; i < num_classes; i++) {
result[i].class_key = VTABLE_get_pmc_keyed_int(interp, class_list, i);
result[i].name_map = VTABLE_get_pmc_keyed_int(interp, attr_map_list, i);
}
repr_data->name_to_index_mapping = result;
return flat_list;
}
/* This works out an allocation strategy for the object. It takes care of
* "inlining" storage of attributes that are natively typed, as well as
* noting unbox targets. */
static void
compute_allocation_strategy(PARROT_INTERP, PMC *WHAT, P6opaqueREPRData *repr_data)
{
STRING *type_str = Parrot_str_new_constant(interp, "type");
STRING *box_target_str = Parrot_str_new_constant(interp, "box_target");
STRING *avcont_str = Parrot_str_new_constant(interp, "auto_viv_container");
PMC *flat_list;
/*
* We have to block GC mark here. Because "repr" is assotiated with some
* PMC which is not accessible in this function. And we have to write
* barrier this PMC because we are poking inside it guts directly. We
* do have WB in caller function, but it can be triggered too late is
* any of allocation will cause GC run.
*
* This is kind of minor evil until after I'll find better solution.
*/
Parrot_block_GC_mark(interp);
/* Compute index mapping table and get flat list of attributes. */
flat_list = index_mapping_and_flat_list(interp, WHAT, repr_data);
/* If we have no attributes in the index mapping, then just the header. */
if (repr_data->name_to_index_mapping[0].class_key == NULL) {
repr_data->allocation_size = sizeof(SixModelObjectCommonalities) + sizeof(PMC *);
}
/* Otherwise, we need to compute the allocation strategy. */
else {
/* Initial size is for commonalities (e.g. shared table pointer) and
* spill hash space. */
INTVAL cur_size = sizeof(SixModelObjectCommonalities) + sizeof(PMC *);
/* Get number of attributes and set up various counters. */
INTVAL num_attrs = VTABLE_elements(interp, flat_list);
INTVAL info_alloc = num_attrs == 0 ? 1 : num_attrs;
INTVAL cur_pmc_attr = 0;
INTVAL cur_str_attr = 0;
INTVAL i;
/* Allocate offset array and GC mark info arrays. */
repr_data->num_attributes = num_attrs;
repr_data->attribute_offsets = (INTVAL *) mem_sys_allocate(info_alloc * sizeof(INTVAL));
/* Go over the attributes and arrange their allocation. */
for (i = 0; i < num_attrs; i++) {
PMC *attr = VTABLE_get_pmc_keyed_int(interp, flat_list, i);
/* Fetch its type and box target flag, if available. */
PMC *type = accessor_call(interp, attr, type_str);
PMC *box_target = accessor_call(interp, attr, box_target_str);
PMC *av_cont = accessor_call(interp, attr, avcont_str);
/* Work out what unboxed type it is, if any. Default to a boxed. */
INTVAL unboxed_type = STORAGE_SPEC_BP_NONE;
INTVAL bits = sizeof(PMC *) * 8;
if (!PMC_IS_NULL(type)) {
/* Get the storage spec of the type and see what it wants. */
storage_spec spec = REPR(type)->get_storage_spec(interp, STABLE(type));
if (spec.inlineable == STORAGE_SPEC_INLINED) {
/* Yes, it's something we'll inline. */
unboxed_type = spec.boxed_primitive;
bits = spec.bits;
/* Is it a target for box/unbox operations? */
if (!PMC_IS_NULL(box_target) && VTABLE_get_bool(interp, box_target)) {
switch (unboxed_type) {
case STORAGE_SPEC_BP_INT:
if (repr_data->unbox_int_offset)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Duplicate box_target for native int");
repr_data->unbox_int_offset = cur_size;
break;
case STORAGE_SPEC_BP_NUM:
if (repr_data->unbox_num_offset)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Duplicate box_target for native num");
repr_data->unbox_num_offset = cur_size;
break;
case STORAGE_SPEC_BP_STR:
if (repr_data->unbox_str_offset)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Duplicate box_target for native str");
repr_data->unbox_str_offset = cur_size;
break;
default:
/* nothing, just suppress 'missing default' warning */
break;
}
}
}
}
/* Do allocation. */
repr_data->attribute_offsets[i] = cur_size;
if (unboxed_type == STORAGE_SPEC_BP_NONE) {
if (!repr_data->gc_pmc_mark_offsets)
repr_data->gc_pmc_mark_offsets = (INTVAL *) mem_sys_allocate_zeroed(info_alloc * sizeof(INTVAL));
repr_data->gc_pmc_mark_offsets[cur_pmc_attr] = cur_size;
cur_pmc_attr++;
if (!PMC_IS_NULL(av_cont)) {
/* Stash away auto-viv container info. */
if (!repr_data->auto_viv_values)
repr_data->auto_viv_values = (PMC **) mem_sys_allocate_zeroed(info_alloc * sizeof(PMC *));
repr_data->auto_viv_values[i] = av_cont;
}
}
if (unboxed_type == STORAGE_SPEC_BP_STR) {
if (!repr_data->gc_str_mark_offsets)
repr_data->gc_str_mark_offsets = (INTVAL *) mem_sys_allocate_zeroed(info_alloc * sizeof(INTVAL));
repr_data->gc_str_mark_offsets[cur_str_attr] = cur_size;
cur_str_attr++;
}
/* XXX TODO Alignment! Important when we get int1, int8, etc. */
cur_size += bits / 8;
}
/* Finally, put computed allocation size in place. */
repr_data->allocation_size = cur_size;
}
Parrot_unblock_GC_mark(interp);
}
/* Helper for reading an int at the specified offset. */
static INTVAL
get_int_at_offset(void *data, INTVAL offset)
{
void *location = (char *)data + offset;
return *((INTVAL *)location);
}
/* Helper for writing an int at the specified offset. */
static void
set_int_at_offset(void *data, INTVAL offset, INTVAL value)
{
void *location = (char *)data + offset;
*((INTVAL *)location) = value;
}
/* Helper for reading a num at the specified offset. */
static FLOATVAL
get_num_at_offset(void *data, INTVAL offset)
{
void *location = (char *)data + offset;
return *((FLOATVAL *)location);
}
/* Helper for writing a num at the specified offset. */
static void
set_num_at_offset(void *data, INTVAL offset, FLOATVAL value)
{
void *location = (char *)data + offset;
*((FLOATVAL *)location) = value;
}
/* Helper for reading a string at the specified offset. */
static STRING *
get_str_at_offset(void *data, INTVAL offset)
{
void *location = (char *)data + offset;
return *((STRING **)location);
}
/* Helper for writing a string at the specified offset. */
static void
set_str_at_offset(void *data, INTVAL offset, STRING *value)
{
void *location = (char *)data + offset;
*((STRING **)location) = value;
}
/* Helper for reading a PMC at the specified offset. */
static PMC *
get_pmc_at_offset(void *data, INTVAL offset)
{
void *location = (char *)data + offset;
return *((PMC **)location);
}
/* Helper for writing a PMC at the specified offset. */
static void
set_pmc_at_offset(void *data, INTVAL offset, PMC *value)
{
void *location = (char *)data + offset;
*((PMC **)location) = value;
}
/* Helper for finding a slot number. */
static INTVAL
try_get_slot(PARROT_INTERP, P6opaqueREPRData *repr_data, PMC *class_key, STRING *name)
{
INTVAL slot = -1;
if (repr_data->name_to_index_mapping) {
P6opaqueNameMap *cur_map_entry = repr_data->name_to_index_mapping;
while (cur_map_entry->class_key != NULL) {
if (cur_map_entry->class_key == class_key) {
PMC *slot_pmc = VTABLE_get_pmc_keyed_str(interp, cur_map_entry->name_map, name);
if (!PMC_IS_NULL(slot_pmc))
slot = VTABLE_get_integer(interp, slot_pmc);
break;
}
cur_map_entry++;
}
}
return slot;
}
/* Creates a new type object of this representation, and associates it with
* the given HOW. */
static PMC *
type_object_for(PARROT_INTERP, PMC *HOW)
{
/* Create new object instance. */
P6opaqueInstance *obj = mem_allocate_zeroed_typed(P6opaqueInstance);
/* Build an STable. */
PMC *st_pmc = create_stable(interp, this_repr, HOW);
STable *st = STABLE_STRUCT(st_pmc);
/* Create REPR data structure and hand it off the STable. */
st->REPR_data = mem_allocate_zeroed_typed(P6opaqueREPRData);
/* Create type object and point it back at the STable. Note that we
* do *not* populate the spill pointer at all, we leave it null. A
* non-null value (even PMCNULL) is what indicates we have a defined
* object. Yes, I know, it's sick. */
obj->common.stable = st_pmc;
st->WHAT = wrap_object(interp, obj);
PARROT_GC_WRITE_BARRIER(interp, st_pmc);
return st->WHAT;
}
/* Creates a new instance based on the type object. */
static PMC *
instance_of(PARROT_INTERP, PMC *WHAT)
{
P6opaqueInstance * obj;
/* Compute allocation strategy if we've not already done so. */
P6opaqueREPRData * repr_data = (P6opaqueREPRData *) STABLE(WHAT)->REPR_data;
if (!repr_data->allocation_size) {
compute_allocation_strategy(interp, WHAT, repr_data);
PARROT_GC_WRITE_BARRIER(interp, STABLE_PMC(WHAT));
}
/* Allocate and set up object instance. */
obj = (P6opaqueInstance *) Parrot_gc_allocate_fixed_size_storage(interp, repr_data->allocation_size);
memset(obj, 0, repr_data->allocation_size);
obj->common.stable = STABLE_PMC(WHAT);
/* The spill slot gets set to PMCNULL; it not being (C) NULL is what
* lets us know it's actually a real instance, not a type object. */
obj->spill = PMCNULL;
return wrap_object(interp, obj);
}
/* Checks if a given object is defined (from the point of view of the
* representation). */
static INTVAL
defined(PARROT_INTERP, PMC *obj)
{
P6opaqueInstance *instance = (P6opaqueInstance *)PMC_data(obj);
return instance->spill != NULL;
}
/* Helper for complaining about attrbiute access errors. */
static void
no_such_attribute(PARROT_INTERP, char *action, PMC *class_handle, STRING *name)
{
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Can not %s non-existant attribute '%Ss' on class '%Ss'",
action, name, VTABLE_get_string(interp, introspection_call(interp,
class_handle, STABLE(class_handle)->HOW,
Parrot_str_new_constant(interp, "name"), 0)));
}
/* Gets the current value for an attribute. */
static PMC *
get_attribute(PARROT_INTERP, PMC *obj, PMC *class_handle, STRING *name, INTVAL hint)
{
P6opaqueInstance *instance = (P6opaqueInstance *)PMC_data(obj);
P6opaqueREPRData *repr_data = (P6opaqueREPRData *)STABLE(obj)->REPR_data;
INTVAL slot;
/* Ensure it is a defined object. */
if (!instance->spill)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Cannot access attributes in a type object");
/* Try the slot allocation first. */
slot = try_get_slot(interp, repr_data, class_handle, name);
if (slot >= 0) {
PMC *result = get_pmc_at_offset(instance, repr_data->attribute_offsets[slot]);
if (result) {
return result;
}
else {
/* Maybe we know how to auto-viv it to a container. */
if (repr_data->auto_viv_values) {
PMC *value = repr_data->auto_viv_values[slot];
if (value != NULL) {
value = REPR(value)->clone(interp, value);
set_pmc_at_offset(instance, repr_data->attribute_offsets[slot], value);
return value;
}
}
return PMCNULL;
}
}
/* Otherwise, complain that the attribute doesn't exist. */
no_such_attribute(interp, "get", class_handle, name);
}
static INTVAL
get_attribute_int(PARROT_INTERP, PMC *obj, PMC *class_handle, STRING *name, INTVAL hint)
{
P6opaqueInstance *instance = (P6opaqueInstance *)PMC_data(obj);
P6opaqueREPRData *repr_data = (P6opaqueREPRData *)STABLE(obj)->REPR_data;
INTVAL slot;
/* Ensure it is a defined object. */
if (!instance->spill)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Cannot access attributes in a type object");
/* Try the slot allocation first. */
slot = try_get_slot(interp, repr_data, class_handle, name);
if (slot >= 0)
return get_int_at_offset(instance, repr_data->attribute_offsets[slot]);
/* Otherwise, complain that the attribute doesn't exist. */
no_such_attribute(interp, "get", class_handle, name);
}
static FLOATVAL
get_attribute_num(PARROT_INTERP, PMC *obj, PMC *class_handle, STRING *name, INTVAL hint)
{
P6opaqueInstance *instance = (P6opaqueInstance *)PMC_data(obj);
P6opaqueREPRData *repr_data = (P6opaqueREPRData *)STABLE(obj)->REPR_data;
INTVAL slot;
/* Ensure it is a defined object. */
if (!instance->spill)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Cannot access attributes in a type object");
/* Try the slot allocation first. */
slot = try_get_slot(interp, repr_data, class_handle, name);
if (slot >= 0)
return get_num_at_offset(instance, repr_data->attribute_offsets[slot]);
/* Otherwise, complain that the attribute doesn't exist. */
no_such_attribute(interp, "get", class_handle, name);
}
static STRING *
get_attribute_str(PARROT_INTERP, PMC *obj, PMC *class_handle, STRING *name, INTVAL hint)
{
P6opaqueInstance *instance = (P6opaqueInstance *)PMC_data(obj);
P6opaqueREPRData *repr_data = (P6opaqueREPRData *)STABLE(obj)->REPR_data;
INTVAL slot;
/* Ensure it is a defined object. */
if (!instance->spill)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Cannot access attributes in a type object");
/* Try the slot allocation first. */
slot = try_get_slot(interp, repr_data, class_handle, name);
if (slot >= 0) {
STRING *result = get_str_at_offset(instance, repr_data->attribute_offsets[slot]);
return result ? result : STRINGNULL;
}
/* Otherwise, complain that the attribute doesn't exist. */
no_such_attribute(interp, "get", class_handle, name);
}
/* Binds the given value to the specified attribute. */
static void
bind_attribute(PARROT_INTERP, PMC *obj, PMC *class_handle, STRING *name, INTVAL hint, PMC *value)
{
P6opaqueInstance *instance = (P6opaqueInstance *)PMC_data(obj);
P6opaqueREPRData *repr_data = (P6opaqueREPRData *)STABLE(obj)->REPR_data;
INTVAL slot;
/* Ensure it is a defined object. */
if (!instance->spill)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Cannot access attributes in a type object");
/* Try the slot allocation first. */
slot = try_get_slot(interp, repr_data, class_handle, name);
if (slot >= 0) {
set_pmc_at_offset(instance, repr_data->attribute_offsets[slot], value);
return;
}
/* Otherwise, complain that the attribute doesn't exist. */
no_such_attribute(interp, "bind", class_handle, name);
}
static void
bind_attribute_int(PARROT_INTERP, PMC *obj, PMC *class_handle, STRING *name, INTVAL hint, INTVAL value)
{
P6opaqueInstance *instance = (P6opaqueInstance *)PMC_data(obj);
P6opaqueREPRData *repr_data = (P6opaqueREPRData *)STABLE(obj)->REPR_data;
INTVAL slot;
/* Ensure it is a defined object. */
if (!instance->spill)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Cannot access attributes in a type object");
/* Try the slot allocation first. */
slot = try_get_slot(interp, repr_data, class_handle, name);
if (slot >= 0) {
set_int_at_offset(instance, repr_data->attribute_offsets[slot], value);
return;
}
/* Otherwise, complain that the attribute doesn't exist. */
no_such_attribute(interp, "bind", class_handle, name);
}
static void
bind_attribute_num(PARROT_INTERP, PMC *obj, PMC *class_handle, STRING *name, INTVAL hint, FLOATVAL value)
{
P6opaqueInstance *instance = (P6opaqueInstance *)PMC_data(obj);
P6opaqueREPRData *repr_data = (P6opaqueREPRData *)STABLE(obj)->REPR_data;
INTVAL slot;
/* Ensure it is a defined object. */
if (!instance->spill)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Cannot access attributes in a type object");
/* Try the slot allocation first. */
slot = try_get_slot(interp, repr_data, class_handle, name);
if (slot >= 0) {
set_num_at_offset(instance, repr_data->attribute_offsets[slot], value);
return;
}
/* Otherwise, complain that the attribute doesn't exist. */
no_such_attribute(interp, "bind", class_handle, name);
}
static void
bind_attribute_str(PARROT_INTERP, PMC *obj, PMC *class_handle, STRING *name, INTVAL hint, STRING *value)
{
P6opaqueInstance *instance = (P6opaqueInstance *)PMC_data(obj);
P6opaqueREPRData *repr_data = (P6opaqueREPRData *)STABLE(obj)->REPR_data;
INTVAL slot;
/* Ensure it is a defined object. */
if (!instance->spill)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Cannot access attributes in a type object");
/* Try the slot allocation first. */
slot = try_get_slot(interp, repr_data, class_handle, name);
if (slot >= 0) {
set_str_at_offset(instance, repr_data->attribute_offsets[slot], value);
return;
}
/* Otherwise, complain that the attribute doesn't exist. */
no_such_attribute(interp, "bind", class_handle, name);
}
/* Gets the hint for the given attribute ID. */
static INTVAL
hint_for(PARROT_INTERP, PMC *class_handle, STRING *name)
{
return NO_HINT;
}
/* Clones the current object. */
static PMC *
repr_clone(PARROT_INTERP, PMC *to_clone)
{
P6opaqueInstance *obj;
P6opaqueREPRData *repr_data = (P6opaqueREPRData *)STABLE(to_clone)->REPR_data;
if (defined(interp, to_clone)) {
obj = (P6opaqueInstance *)Parrot_gc_allocate_fixed_size_storage(interp, repr_data->allocation_size);
memcpy(obj, PMC_data(to_clone), repr_data->allocation_size);
if (!PMC_IS_NULL(obj->spill))
obj->spill = VTABLE_clone(interp, obj->spill);
}
else {
obj = mem_allocate_zeroed_typed(P6opaqueInstance);
memcpy(obj, PMC_data(to_clone), sizeof(P6opaqueInstance));
}
return wrap_object(interp, obj);
}
/* Used with boxing. Sets an integer value, for representations that can hold
* one. */
static void
set_int(PARROT_INTERP, PMC *obj, INTVAL value)
{
P6opaqueInstance *instance = (P6opaqueInstance *)PMC_data(obj);
P6opaqueREPRData *repr_data = (P6opaqueREPRData *)STABLE(obj)->REPR_data;
if (repr_data->unbox_int_offset) {
set_int_at_offset(instance, repr_data->unbox_int_offset, value);
}
else {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"This type cannot box a native integer");
}
}
/* Used with boxing. Gets an integer value, for representations that can
* hold one. */
static INTVAL
get_int(PARROT_INTERP, PMC *obj)
{
P6opaqueInstance *instance = (P6opaqueInstance *)PMC_data(obj);
P6opaqueREPRData *repr_data = (P6opaqueREPRData *)STABLE(obj)->REPR_data;
if (repr_data->unbox_int_offset) {
if (!instance->spill) {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Cannot unbox type object to a native integer");
}
return get_int_at_offset(instance, repr_data->unbox_int_offset);
}
else {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"This type cannot unbox to a native integer");
}
}
/* Used with boxing. Sets a floating point value, for representations that can
* hold one. */
static void
set_num(PARROT_INTERP, PMC *obj, FLOATVAL value)
{
P6opaqueInstance *instance = (P6opaqueInstance *)PMC_data(obj);
P6opaqueREPRData *repr_data = (P6opaqueREPRData *)STABLE(obj)->REPR_data;
if (repr_data->unbox_num_offset) {
set_num_at_offset(instance, repr_data->unbox_num_offset, value);
}
else {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"This type cannot box a native number");
}
}
/* Used with boxing. Gets a floating point value, for representations that can
* hold one. */
static FLOATVAL
get_num(PARROT_INTERP, PMC *obj)
{
P6opaqueInstance *instance = (P6opaqueInstance *)PMC_data(obj);
P6opaqueREPRData *repr_data = (P6opaqueREPRData *)STABLE(obj)->REPR_data;
if (repr_data->unbox_num_offset) {
if (!instance->spill) {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Cannot unbox type object to a native number");
}
return get_num_at_offset(instance, repr_data->unbox_num_offset);
}
else {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"This type cannot unbox to a native number");
}
}
/* Used with boxing. Sets a string value, for representations that can hold
* one. */
static void
set_str(PARROT_INTERP, PMC *obj, STRING *value)
{
P6opaqueInstance *instance = (P6opaqueInstance *)PMC_data(obj);
P6opaqueREPRData *repr_data = (P6opaqueREPRData *)STABLE(obj)->REPR_data;
if (repr_data->unbox_str_offset) {
set_str_at_offset(instance, repr_data->unbox_str_offset, value);
}
else {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"This type cannot box a native string");
}
}
/* Used with boxing. Gets a string value, for representations that can hold
* one. */
static STRING *
get_str(PARROT_INTERP, PMC *obj)
{
P6opaqueInstance *instance = (P6opaqueInstance *)PMC_data(obj);
P6opaqueREPRData *repr_data = (P6opaqueREPRData *)STABLE(obj)->REPR_data;
if (repr_data->unbox_str_offset) {
if (!instance->spill) {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Cannot unbox type object to a native string");
}
return get_str_at_offset(instance, repr_data->unbox_str_offset);
}
else {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"This type cannot unbox to a native string");
}
}
/* This Parrot-specific addition to the API is used to mark an object. */
static void
gc_mark(PARROT_INTERP, PMC *obj)
{
P6opaqueInstance *instance = (P6opaqueInstance *)PMC_data(obj);
/* Mark STable and SC. */
if (!PMC_IS_NULL(instance->common.stable))
Parrot_gc_mark_PMC_alive(interp, instance->common.stable);