/
mmd.c
2229 lines (1911 loc) · 61.9 KB
/
mmd.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
/*
Copyright: 2003 The Perl Foundation. All Rights Reserved.
$Id$
=head1 NAME
src/mmd.c - Multimethod dispatch for binary opcode functions
=head1 SYNOPSIS
This system is set up to handle type-based dispatching for binary (i.e.
two-arg) functions. This includes, though isn't necessarily limited to,
binary operators such as addition or subtraction.
=head1 DESCRIPTION
The MMD system is straightforward, and currently must be explicitly
invoked, for example by a vtable function. (We may reserve the right to
use MMD in all circumstances, but currently do not).
=head2 API
For the purposes of the API, each MMD-able function is assigned a unique
number which is used to find the correct function table. This is the
C<func_num> parameter in the following functions. While Parrot isn't
restricted to a predefined set of functions, it I<does> set things up so
that all the binary vtable functions have a MMD table preinstalled for
them, with default behaviour.
=head2 Remarks
C<< binop_mmd_funcs->x >> and C<< ->y >> are table sizes
not highest type in table.
=head2 Functions
=over 4
=cut
*/
#include "parrot/parrot.h"
#include "mmd.str"
#include <assert.h>
#define MMD_DEBUG 0
static void mmd_create_builtin_multi_meth_2(Interp *,
INTVAL func_nr, INTVAL type, INTVAL right, funcptr_t func_ptr);
#ifndef NDEBUG
static void
dump_mmd(Interp *interpreter, INTVAL function)
{
UINTVAL x, y;
UINTVAL offset, x_funcs, y_funcs;
MMD_table *table = interpreter->binop_mmd_funcs + function;
funcptr_t func, def;
x_funcs = table->x;
y_funcs = table->y;
printf(" ");
for (x = 0; x < x_funcs; ++x) {
if (! (x % 10))
printf("%d", (int) x / 10);
else
printf(" ");
}
printf("\n");
for (y = 0; y < y_funcs; ++y) {
printf("%3d ", (int)y);
for (x = 0; x < x_funcs; ++x) {
offset = x_funcs * y + x;
func = table->mmd_funcs[offset];
printf("%c",
func == def ? '.' :
(UINTVAL)func & 1 ? 'P' :
!func ? '0' : 'F');
}
printf("\n");
}
for (y = 0; y < y_funcs; ++y) {
for (x = 0; x < x_funcs; ++x) {
offset = x_funcs * y + x;
func = table->mmd_funcs[offset];
if (func && func != def && !((UINTVAL) func & 1))
printf("%3d %3d: %p\n", (int)x, (int)y, (void*) func);
}
}
}
#endif
/*
* isa_str contains for real objects and for deleg_pmc objects
* both the string "delegate" - so compare a vtable slot
*/
extern void Parrot_deleg_pmc_mark(Interp*, PMC* pmc);
static int
isa_deleg_pmc(Interp *interpreter, PMC *p)
{
return p->vtable->mark == Parrot_deleg_pmc_mark;
}
funcptr_t
get_mmd_dispatch_type(Interp *interpreter, INTVAL func_nr, INTVAL left_type,
INTVAL right_type, int *is_pmc)
{
funcptr_t func, func_;
UINTVAL offset, x_funcs, y_funcs;
INTVAL r;
MMD_table *table = interpreter->binop_mmd_funcs + func_nr;
x_funcs = table->x;
y_funcs = table->y;
func = NULL;
assert(left_type >= 0);
assert(right_type >=0 ||
(right_type >= enum_type_INTVAL && right_type <= enum_type_PMC));
r = right_type;
if (right_type < 0)
right_type -= enum_type_INTVAL;
else
right_type += 4;
if ((UINTVAL)left_type < x_funcs && (UINTVAL)right_type < y_funcs) {
offset = x_funcs * right_type + left_type;
func = table->mmd_funcs[offset];
}
if (!func) {
const char *meth_c = Parrot_MMD_method_name(interpreter, func_nr);
STRING *meth_s = const_string(interpreter, meth_c);
PMC *method = Parrot_MMD_search_default_infix(interpreter,
meth_s, left_type, r);
if (!method)
real_exception(interpreter, 0, 1, "MMD function %s not found"
"for types (%d, %d)", meth_c, left_type, r);
if (method->vtable->base_type == enum_class_NCI) {
PMC *nci;
/* C function is at struct_val */
func = D2FPTR(PMC_struct_val(method));
/*
* if one of the arguments isa deleg_pmc
* install the mmd_wrapper as real function
*/
if (isa_deleg_pmc(interpreter,
Parrot_base_vtables[left_type]->class)
|| (r > 0 && isa_deleg_pmc(interpreter,
Parrot_base_vtables[r]->class))) {
/* TODO check dest too */
nci = pmc_new(interpreter, enum_class_Bound_NCI);
dod_register_pmc(interpreter, nci); /* XXX */
*is_pmc = 2;
PMC_struct_val(nci) = func;
return D2FPTR(nci);
}
*is_pmc = 0;
mmd_register(interpreter, func_nr, left_type, r,
PMC_struct_val(method));
}
else {
*is_pmc = 1;
func = D2FPTR(method);
mmd_register_sub(interpreter, func_nr, left_type, r, method);
}
return func;
}
*is_pmc = (UINTVAL)func & 3;
func_ = (funcptr_t)((UINTVAL)func & ~3);
#ifndef PARROT_HAS_ALIGNED_FUNCPTR
if (!*is_pmc) {
return func;
}
else if (!is_pmc_ptr(interpreter, F2DPTR(func_))) {
*is_pmc = 0;
return func;
}
#endif
return func_;
}
static funcptr_t
get_mmd_dispatcher(Interp *interpreter, PMC *left, PMC * right,
INTVAL function, int *is_pmc)
{
UINTVAL left_type, right_type;
left_type = left->vtable->base_type;
right_type = right->vtable->base_type;
return get_mmd_dispatch_type(interpreter, function, left_type, right_type,
is_pmc);
}
/*
=item C<PMC*
mmd_dispatch_p_ppp(Interp *,
PMC *left, PMC *right, PMC *dest, INTVAL function)>
Dispatch to a multimethod that returns a PMC. C<left>, C<right>, and
C<dest> are all PMC pointers, while C<func_num> is the MMD table that
should be used to do the dispatching.
If the C<dest> pointer is NULL, it dispatches two a two-argument function
that returns a new C<dest> always.
The MMD system will figure out which function should be called based on
the types of C<left> and C<right> and call it, passing in C<left>,
C<right>, and possibly C<dest> like any other binary vtable function.
=item C<PMC*
mmd_dispatch_p_pip(Interp *,
PMC *left, INTVAL right, PMC *dest, INTVAL function)>
Like above, right argument is a native INTVAL.
=item C<PMC*
mmd_dispatch_p_pnp(Interp *,
PMC *left, FLOATVAL right, PMC *dest, INTVAL function)>
Like above, right argument is a native FLOATVAL.
=item C<PMC*
mmd_dispatch_p_psp(Interp *,
PMC *left, STRING *right, PMC *dest, INTVAL function)>
Like above, right argument is a native STRING *.
=cut
=item C<void
mmd_dispatch_v_pp(Interp *, PMC *left, PMC *right, INTVAL function)>
=item C<void
mmd_dispatch_v_pi(Interp *, PMC *left, INTVAL right, INTVAL function)>
=item C<void
mmd_dispatch_v_pn(Interp *, PMC *left, FLOATVAL right, INTVAL function)>
=item C<void
mmd_dispatch_v_ps(Interp *, PMC *left, STRING *right, INTVAL function)>
Inplace dispatch functions for C<< left <op=> right >>.
*/
static PMC*
mmd_wrap_p_ppp(Interp *interpreter, PMC *nci, PMC *right, PMC *dest) {
PMC *left = PMC_pmc_val(nci);
PMC *l, *r, *d, *n = NULL;
mmd_f_p_ppp real_function;
SLOTTYPE *attrib_array;
if (isa_deleg_pmc(interpreter, left)) {
attrib_array = PMC_data(left);
l = get_attrib_num(attrib_array, POD_FIRST_ATTRIB);
n = left;
}
else
l = left;
if (isa_deleg_pmc(interpreter, right)) {
attrib_array = PMC_data(right);
r = get_attrib_num(attrib_array, POD_FIRST_ATTRIB);
n = right;
}
else
r = right;
if (dest && isa_deleg_pmc(interpreter, dest)) {
attrib_array = PMC_data(dest);
d = get_attrib_num(attrib_array, POD_FIRST_ATTRIB);
}
else {
d = dest;
attrib_array = NULL;
}
real_function = (mmd_f_p_ppp)D2FPTR(PMC_struct_val(nci));
d = (real_function)(interpreter, l, r, d);
if (attrib_array) {
attrib_array[POD_FIRST_ATTRIB] = d;
return dest;
}
if (!n)
return d;
dest = VTABLE_clone(interpreter, n);
attrib_array = PMC_data(dest);
attrib_array[POD_FIRST_ATTRIB] = d;
return dest;
}
PMC*
mmd_dispatch_p_ppp(Interp *interpreter,
PMC *left, PMC *right, PMC *dest, INTVAL func_nr)
{
mmd_f_p_ppp real_function;
PMC *sub;
int is_pmc;
real_function = (mmd_f_p_ppp)get_mmd_dispatcher(interpreter,
left, right, func_nr, &is_pmc);
if (is_pmc) {
sub = (PMC*)real_function;
if (is_pmc == 2) {
/* mmd_register the wrapper */
mmd_register(interpreter, func_nr, left->vtable->base_type,
right->vtable->base_type,
D2FPTR((UINTVAL) sub | 3));
#if 0
mmd_create_builtin_multi_meth_2(interpreter,
func_nr, left->vtable->base_type,
right->vtable->base_type, (funcptr_t)mmd_wrap_p_ppp);
#endif
is_pmc = 3;
}
if (is_pmc == 3) {
PMC_pmc_val(sub) = left;
return mmd_wrap_p_ppp(interpreter, sub, right, dest);
}
if (dest)
return Parrot_runops_fromc_args(interpreter, sub, "PPPP",
left, right, dest);
else
return Parrot_runops_fromc_args(interpreter, sub, "PPP",
left, right);
}
else {
return (*real_function)(interpreter, left, right, dest);
}
}
static PMC*
mmd_wrap_p_pip(Interp *interpreter, PMC *nci, INTVAL right, PMC *dest) {
PMC *left = PMC_pmc_val(nci);
PMC *l, *d, *n = NULL;
mmd_f_p_pip real_function;
SLOTTYPE *attrib_array;
if (isa_deleg_pmc(interpreter, left)) {
attrib_array = PMC_data(left);
l = get_attrib_num(attrib_array, POD_FIRST_ATTRIB);
n = left;
}
else
l = left;
if (dest && isa_deleg_pmc(interpreter, dest)) {
attrib_array = PMC_data(dest);
d = get_attrib_num(attrib_array, POD_FIRST_ATTRIB);
}
else {
d = dest;
attrib_array = NULL;
}
real_function = (mmd_f_p_pip)D2FPTR(PMC_struct_val(nci));
d = (real_function)(interpreter, l, right, d);
if (attrib_array) {
attrib_array[POD_FIRST_ATTRIB] = d;
return dest;
}
if (!n)
return d;
dest = VTABLE_clone(interpreter, n);
attrib_array = PMC_data(dest);
attrib_array[POD_FIRST_ATTRIB] = d;
return dest;
}
PMC*
mmd_dispatch_p_pip(Interp *interpreter,
PMC *left, INTVAL right, PMC *dest, INTVAL func_nr)
{
mmd_f_p_pip real_function;
PMC *sub;
int is_pmc;
UINTVAL left_type;
left_type = left->vtable->base_type;
real_function = (mmd_f_p_pip)get_mmd_dispatch_type(interpreter,
func_nr, left_type, enum_type_INTVAL, &is_pmc);
if (is_pmc) {
sub = (PMC*)real_function;
if (is_pmc == 2) {
/* mmd_register the wrapper */
mmd_register(interpreter, func_nr, left->vtable->base_type,
enum_type_INTVAL,
D2FPTR((UINTVAL) sub | 3));
is_pmc = 3;
}
if (is_pmc == 3) {
PMC_pmc_val(sub) = left;
return mmd_wrap_p_pip(interpreter, sub, right, dest);
}
if (dest)
return Parrot_runops_fromc_args(interpreter, sub, "PPIP",
left, right, dest);
else
return Parrot_runops_fromc_args(interpreter, sub, "PPI",
left, right);
}
else {
return (*real_function)(interpreter, left, right, dest);
}
}
static PMC*
mmd_wrap_p_pnp(Interp *interpreter, PMC *nci, FLOATVAL right, PMC *dest) {
PMC *left = PMC_pmc_val(nci);
PMC *l, *d, *n = NULL;
mmd_f_p_pnp real_function;
SLOTTYPE *attrib_array;
if (isa_deleg_pmc(interpreter, left)) {
attrib_array = PMC_data(left);
l = get_attrib_num(attrib_array, POD_FIRST_ATTRIB);
n = left;
}
else
l = left;
if (dest && isa_deleg_pmc(interpreter, dest)) {
attrib_array = PMC_data(dest);
d = get_attrib_num(attrib_array, POD_FIRST_ATTRIB);
}
else {
d = dest;
attrib_array = NULL;
}
real_function = (mmd_f_p_pnp)D2FPTR(PMC_struct_val(nci));
d = (real_function)(interpreter, l, right, d);
if (attrib_array) {
attrib_array[POD_FIRST_ATTRIB] = d;
return dest;
}
if (!n)
return d;
dest = VTABLE_clone(interpreter, n);
attrib_array = PMC_data(dest);
attrib_array[POD_FIRST_ATTRIB] = d;
return dest;
}
PMC*
mmd_dispatch_p_pnp(Interp *interpreter,
PMC *left, FLOATVAL right, PMC *dest, INTVAL func_nr)
{
mmd_f_p_pnp real_function;
PMC *sub;
int is_pmc;
UINTVAL left_type;
left_type = left->vtable->base_type;
real_function = (mmd_f_p_pnp)get_mmd_dispatch_type(interpreter,
func_nr, left_type, enum_type_FLOATVAL, &is_pmc);
if (is_pmc) {
sub = (PMC*)real_function;
if (is_pmc == 2) {
/* mmd_register the wrapper */
mmd_register(interpreter, func_nr, left->vtable->base_type,
enum_type_FLOATVAL,
D2FPTR((UINTVAL) sub | 3));
is_pmc = 3;
}
if (is_pmc == 3) {
PMC_pmc_val(sub) = left;
return mmd_wrap_p_pnp(interpreter, sub, right, dest);
}
if (dest)
return Parrot_runops_fromc_args(interpreter, sub, "PPNP",
left, right, dest);
else
return Parrot_runops_fromc_args(interpreter, sub, "PPN",
left, right);
}
else {
return (*real_function)(interpreter, left, right, dest);
}
}
static PMC*
mmd_wrap_p_psp(Interp *interpreter, PMC *nci, STRING *right, PMC *dest) {
PMC *left = PMC_pmc_val(nci);
PMC *l, *d, *n = NULL;
mmd_f_p_psp real_function;
SLOTTYPE *attrib_array;
if (isa_deleg_pmc(interpreter, left)) {
attrib_array = PMC_data(left);
l = get_attrib_num(attrib_array, POD_FIRST_ATTRIB);
n = left;
}
else
l = left;
if (dest && isa_deleg_pmc(interpreter, dest)) {
attrib_array = PMC_data(dest);
d = get_attrib_num(attrib_array, POD_FIRST_ATTRIB);
}
else {
d = dest;
attrib_array = NULL;
}
real_function = (mmd_f_p_psp)D2FPTR(PMC_struct_val(nci));
d = (real_function)(interpreter, l, right, d);
if (attrib_array) {
attrib_array[POD_FIRST_ATTRIB] = d;
return dest;
}
if (!n)
return d;
dest = VTABLE_clone(interpreter, n);
attrib_array = PMC_data(dest);
attrib_array[POD_FIRST_ATTRIB] = d;
return dest;
}
PMC*
mmd_dispatch_p_psp(Interp *interpreter,
PMC *left, STRING *right, PMC *dest, INTVAL func_nr)
{
mmd_f_p_psp real_function;
PMC *sub;
int is_pmc;
UINTVAL left_type;
left_type = left->vtable->base_type;
real_function = (mmd_f_p_psp)get_mmd_dispatch_type(interpreter,
func_nr, left_type, enum_type_STRING, &is_pmc);
if (is_pmc) {
sub = (PMC*)real_function;
if (is_pmc == 2) {
/* mmd_register the wrapper */
mmd_register(interpreter, func_nr, left->vtable->base_type,
enum_type_STRING,
D2FPTR((UINTVAL) sub | 3));
is_pmc = 3;
}
if (is_pmc == 3) {
PMC_pmc_val(sub) = left;
return mmd_wrap_p_psp(interpreter, sub, right, dest);
}
if (dest)
return Parrot_runops_fromc_args(interpreter, sub, "PPSP",
left, right, dest);
else
return Parrot_runops_fromc_args(interpreter, sub, "PPS",
left, right);
}
else {
return (*real_function)(interpreter, left, right, dest);
}
}
static void
mmd_wrap_v_pp(Interp *interpreter, PMC *nci, PMC *right) {
PMC *left = PMC_pmc_val(nci);
PMC *l, *r;
mmd_f_v_pp real_function;
SLOTTYPE *attrib_array;
if (isa_deleg_pmc(interpreter, left)) {
attrib_array = PMC_data(left);
l = get_attrib_num(attrib_array, POD_FIRST_ATTRIB);
}
else
l = left;
if (isa_deleg_pmc(interpreter, right)) {
attrib_array = PMC_data(right);
r = get_attrib_num(attrib_array, POD_FIRST_ATTRIB);
}
else
r = right;
real_function = (mmd_f_v_pp)D2FPTR(PMC_struct_val(nci));
(real_function)(interpreter, l, r);
}
/*
* inplace variants
*/
void
mmd_dispatch_v_pp(Interp *interpreter,
PMC *left, PMC *right, INTVAL func_nr)
{
mmd_f_v_pp real_function;
PMC *sub;
int is_pmc;
real_function = (mmd_f_v_pp)get_mmd_dispatcher(interpreter,
left, right, func_nr, &is_pmc);
if (is_pmc) {
sub = (PMC*)real_function;
if (is_pmc == 2) {
/* mmd_register the wrapper */
mmd_register(interpreter, func_nr, left->vtable->base_type,
right->vtable->base_type,
D2FPTR((UINTVAL) sub | 3));
is_pmc = 3;
}
if (is_pmc == 3) {
PMC_pmc_val(sub) = left;
mmd_wrap_v_pp(interpreter, sub, right);
return;
}
Parrot_runops_fromc_args(interpreter, sub, "vPP", left, right);
}
else {
(*real_function)(interpreter, left, right);
}
}
static void
mmd_wrap_v_pi(Interp *interpreter, PMC *nci, INTVAL right) {
PMC *left = PMC_pmc_val(nci);
PMC *l;
mmd_f_v_pi real_function;
SLOTTYPE *attrib_array;
if (isa_deleg_pmc(interpreter, left)) {
attrib_array = PMC_data(left);
l = get_attrib_num(attrib_array, POD_FIRST_ATTRIB);
}
else
l = left;
assert(l != left);
real_function = (mmd_f_v_pi)D2FPTR(PMC_struct_val(nci));
(real_function)(interpreter, l, right);
}
void
mmd_dispatch_v_pi(Interp *interpreter,
PMC *left, INTVAL right, INTVAL func_nr)
{
mmd_f_v_pi real_function;
PMC *sub;
int is_pmc;
UINTVAL left_type;
left_type = left->vtable->base_type;
real_function = (mmd_f_v_pi)get_mmd_dispatch_type(interpreter,
func_nr, left_type, enum_type_INTVAL, &is_pmc);
if (is_pmc) {
sub = (PMC*)real_function;
if (is_pmc == 2) {
/* mmd_register the wrapper */
mmd_register(interpreter, func_nr, left->vtable->base_type,
enum_type_INTVAL,
D2FPTR((UINTVAL) sub | 3));
is_pmc = 3;
}
if (is_pmc == 3) {
PMC_pmc_val(sub) = left;
mmd_wrap_v_pi(interpreter, sub, right);
return;
}
Parrot_runops_fromc_args(interpreter, sub, "vPI", left, right);
}
else {
(*real_function)(interpreter, left, right);
}
}
static void
mmd_wrap_v_pn(Interp *interpreter, PMC *nci, FLOATVAL right) {
PMC *left = PMC_pmc_val(nci);
PMC *l;
mmd_f_v_pn real_function;
SLOTTYPE *attrib_array;
if (isa_deleg_pmc(interpreter, left)) {
attrib_array = PMC_data(left);
l = get_attrib_num(attrib_array, POD_FIRST_ATTRIB);
}
else
l = left;
assert(l != left);
real_function = (mmd_f_v_pn)D2FPTR(PMC_struct_val(nci));
(real_function)(interpreter, l, right);
}
void
mmd_dispatch_v_pn(Interp *interpreter,
PMC *left, FLOATVAL right, INTVAL func_nr)
{
mmd_f_v_pn real_function;
PMC *sub;
int is_pmc;
UINTVAL left_type;
left_type = left->vtable->base_type;
real_function = (mmd_f_v_pn)get_mmd_dispatch_type(interpreter,
func_nr, left_type, enum_type_FLOATVAL, &is_pmc);
if (is_pmc) {
sub = (PMC*)real_function;
if (is_pmc == 2) {
/* mmd_register the wrapper */
mmd_register(interpreter, func_nr, left->vtable->base_type,
enum_type_FLOATVAL,
D2FPTR((UINTVAL) sub | 3));
is_pmc = 3;
}
if (is_pmc == 3) {
PMC_pmc_val(sub) = left;
mmd_wrap_v_pn(interpreter, sub, right);
return;
}
Parrot_runops_fromc_args(interpreter, sub, "vPN", left, right);
}
else {
(*real_function)(interpreter, left, right);
}
}
static void
mmd_wrap_v_ps(Interp *interpreter, PMC *nci, STRING* right) {
PMC *left = PMC_pmc_val(nci);
PMC *l;
mmd_f_v_ps real_function;
SLOTTYPE *attrib_array;
if (isa_deleg_pmc(interpreter, left)) {
attrib_array = PMC_data(left);
l = get_attrib_num(attrib_array, POD_FIRST_ATTRIB);
}
else
l = left;
assert(l != left);
real_function = (mmd_f_v_ps)D2FPTR(PMC_struct_val(nci));
(real_function)(interpreter, l, right);
}
void
mmd_dispatch_v_ps(Interp *interpreter,
PMC *left, STRING *right, INTVAL func_nr)
{
mmd_f_v_ps real_function;
PMC *sub;
int is_pmc;
UINTVAL left_type;
left_type = left->vtable->base_type;
real_function = (mmd_f_v_ps)get_mmd_dispatch_type(interpreter,
func_nr, left_type, enum_type_STRING, &is_pmc);
if (is_pmc) {
sub = (PMC*)real_function;
if (is_pmc == 2) {
/* mmd_register the wrapper */
mmd_register(interpreter, func_nr, left->vtable->base_type,
enum_type_STRING,
D2FPTR((UINTVAL) sub | 3));
is_pmc = 3;
}
if (is_pmc == 3) {
PMC_pmc_val(sub) = left;
mmd_wrap_v_ps(interpreter, sub, right);
return;
}
Parrot_runops_fromc_args(interpreter, sub, "vPS", left, right);
}
else {
(*real_function)(interpreter, left, right);
}
}
static INTVAL
mmd_wrap_i_pp(Interp *interpreter, PMC *nci, PMC *right) {
PMC *left = PMC_pmc_val(nci);
PMC *l, *r;
mmd_f_i_pp real_function;
SLOTTYPE *attrib_array;
if (isa_deleg_pmc(interpreter, left)) {
attrib_array = PMC_data(left);
l = get_attrib_num(attrib_array, POD_FIRST_ATTRIB);
}
else
l = left;
if (isa_deleg_pmc(interpreter, right)) {
attrib_array = PMC_data(right);
r = get_attrib_num(attrib_array, POD_FIRST_ATTRIB);
}
else
r = right;
real_function = (mmd_f_i_pp)D2FPTR(PMC_struct_val(nci));
return (real_function)(interpreter, l, r);
}
/*
=item C<INTVAL
mmd_dispatch_i_pp(Interp *interpreter,
PMC *left, PMC *right, INTVAL func_nr)>
Like C<mmd_dispatch_p_ppp()>, only it returns an C<INTVAL>. This is used
by MMD compare functions.
=cut
*/
INTVAL
mmd_dispatch_i_pp(Interp *interpreter,
PMC *left, PMC *right, INTVAL func_nr)
{
mmd_f_i_pp real_function;
PMC *sub;
int is_pmc;
INTVAL ret;
real_function = (mmd_f_i_pp)get_mmd_dispatcher(interpreter,
left, right, func_nr, &is_pmc);
if (is_pmc) {
sub = (PMC*)real_function;
if (is_pmc == 2) {
/* mmd_register the wrapper */
mmd_register(interpreter, func_nr, left->vtable->base_type,
right->vtable->base_type,
D2FPTR((UINTVAL) sub | 3));
is_pmc = 3;
}
if (is_pmc == 3) {
PMC_pmc_val(sub) = left;
return mmd_wrap_i_pp(interpreter, sub, right);
}
ret = Parrot_runops_fromc_args_reti(interpreter, sub, "IPP",
left, right);
}
else {
ret = (*real_function)(interpreter, left, right);
}
return ret;
}
int
Parrot_run_maybe_mmd_meth(Interp* interpreter, PMC *object,
STRING *meth, STRING *sig)
{
INTVAL mmd_func;
char *c_meth, *c_sig;
int ret = 0, inplace, compare;
/*
* check if it's a known MMD function
*/
c_meth = string_to_cstring(interpreter, meth);
c_sig = string_to_cstring(interpreter, sig);
if ( (mmd_func = Parrot_MMD_method_idx(interpreter, c_meth)) >= 0) {
/* yep - run it instantly */
ret = 1;
inplace = c_meth[2] == 'i' && c_meth[3] == '_';
compare = mmd_func >= MMD_EQ && mmd_func <= MMD_STRCMP;
assert(c_sig[0] == 'O');
switch (c_sig[1]) {
case 'P':
if (inplace)
mmd_dispatch_v_pp(interpreter,
object, REG_PMC(5), mmd_func);
else if (compare)
REG_INT(5) = mmd_dispatch_i_pp(interpreter,
object, REG_PMC(5), mmd_func);
else
REG_PMC(5) = mmd_dispatch_p_ppp(interpreter,
object, REG_PMC(5), NULL, mmd_func);
break;
case 'I':
if (inplace)
mmd_dispatch_v_pi(interpreter,
object, REG_INT(5), mmd_func);
else
REG_PMC(5) = mmd_dispatch_p_pip(interpreter,
object, REG_INT(5), NULL, mmd_func);
break;
case 'N':
if (inplace)
mmd_dispatch_v_pn(interpreter,
object, REG_NUM(5), mmd_func);
else
REG_PMC(5) = mmd_dispatch_p_pnp(interpreter,
object, REG_NUM(5), NULL, mmd_func);
break;
case 'S':
if (inplace)
mmd_dispatch_v_ps(interpreter,
object, REG_STR(5), mmd_func);
else
REG_PMC(5) = mmd_dispatch_p_psp(interpreter,
object, REG_STR(5), NULL, mmd_func);
break;
}
}
string_cstring_free(c_meth);
string_cstring_free(c_sig);
return ret;
}
/*
=item C<void
mmd_add_function(Interp *interpreter,
INTVAL funcnum, funcptr_t function)>
Add a new binary MMD function to the list of functions the MMD system knows
of. C<func_num> is the number of the new function. C<function> is ignored.
TODO change this to a MMD register interface that takes a function *name*.
=cut
*/
void
mmd_add_function(Interp *interpreter,
INTVAL func_nr, funcptr_t function)
{
INTVAL i;
if (func_nr >= (INTVAL)interpreter->n_binop_mmd_funcs) {
if (interpreter->binop_mmd_funcs) {
interpreter->binop_mmd_funcs =
mem_sys_realloc(interpreter->binop_mmd_funcs,
(func_nr + 1) * sizeof(MMD_table));
} else {
interpreter->binop_mmd_funcs =
mem_sys_allocate((func_nr + 1) * sizeof(MMD_table));
}
for (i = interpreter->n_binop_mmd_funcs; i <= func_nr; ++i) {
MMD_table *table = interpreter->binop_mmd_funcs + i;
table->x = table->y = 0;
table->mmd_funcs = NULL;
}
interpreter->n_binop_mmd_funcs = func_nr + 1;
}
}
/*
=item C<static void
mmd_expand_x(Interp *interpreter, INTVAL func_nr, INTVAL new_x)>
Expands the function table in the X dimension to include C<new_x>.
=cut
*/
static void
mmd_expand_x(Interp *interpreter, INTVAL func_nr, INTVAL new_x)
{
funcptr_t *new_table;
UINTVAL x;
UINTVAL y;
UINTVAL i;
MMD_table *table = interpreter->binop_mmd_funcs + func_nr;
char *src_ptr, *dest_ptr;
size_t old_dp, new_dp;
/* Is the Y 0? If so, nothing to expand, so just set the X for
later use */
if (table->y == 0) {
table->x = new_x;
return;
}
/* The Y is not zero. Bleah. This means we have to expand the
table in an unpleasant way. */
x = table->x;
y = table->y;
/* First, fill in the whole new table with the default function
pointer. We only really need to do the new part, but... */
new_table = mem_sys_allocate(sizeof(funcptr_t) * y * new_x);
for (i = 0; i < y * new_x; i++) {
new_table[i] = NULL;
}
/* Then copy the old table over. We have to do this row by row,
because the rows in the old and new tables are different
lengths */
src_ptr = (char*) table->mmd_funcs;
dest_ptr = (char*) new_table;
old_dp = sizeof(funcptr_t) * x;
new_dp = sizeof(funcptr_t) * new_x;
for (i = 0; i < y; i++) {
memcpy(dest_ptr, src_ptr, sizeof(funcptr_t) * x);
src_ptr += old_dp;
dest_ptr += new_dp;
}
if (table->mmd_funcs)
mem_sys_free(table->mmd_funcs);
table->x = new_x;
/* Set the old table to point to the new table */
table->mmd_funcs = new_table;
}
/*