/
APItest.xs
7622 lines (6817 loc) · 196 KB
/
APItest.xs
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
#define PERL_IN_XS_APITEST
/* We want to be able to test things that aren't API yet. */
#define PERL_EXT
/* Do *not* define PERL_NO_GET_CONTEXT. This is the one place where we get
to test implicit Perl_get_context(). */
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
typedef FILE NativeFile;
#include "fakesdio.h" /* Causes us to use PerlIO below */
typedef SV *SVREF;
typedef PTR_TBL_t *XS__APItest__PtrTable;
typedef PerlIO * InputStream;
typedef PerlIO * OutputStream;
#define croak_fail() croak("fail at " __FILE__ " line %d", __LINE__)
#define croak_fail_nep(h, w) croak("fail %p!=%p at " __FILE__ " line %d", (h), (w), __LINE__)
#define croak_fail_nei(h, w) croak("fail %d!=%d at " __FILE__ " line %d", (int)(h), (int)(w), __LINE__)
#if IVSIZE == 8
# define TEST_64BIT 1
#else
# define TEST_64BIT 0
#endif
#ifdef EBCDIC
void
cat_utf8a2n(SV* sv, const char * const ascii_utf8, STRLEN len)
{
/* Converts variant UTF-8 text pointed to by 'ascii_utf8' of length 'len',
* to UTF-EBCDIC, appending that text to the text already in 'sv'.
* Currently doesn't work on invariants, as that is unneeded here, and we
* could get double translations if we did.
*
* It has the algorithm for strict UTF-8 hard-coded in to find the code
* point it represents, then calls uvchr_to_utf8() to convert to
* UTF-EBCDIC).
*
* Note that this uses code points, not characters. Thus if the input is
* the UTF-8 for the code point 0xFF, the output will be the UTF-EBCDIC for
* 0xFF, even though that code point represents different characters on
* ASCII vs EBCDIC platforms. */
dTHX;
char * p = (char *) ascii_utf8;
const char * const e = p + len;
while (p < e) {
UV code_point;
U8 native_utf8[UTF8_MAXBYTES + 1];
U8 * char_end;
U8 start = (U8) *p;
/* Start bytes are the same in both UTF-8 and I8, therefore we can
* treat this ASCII UTF-8 byte as an I8 byte. But PL_utf8skip[] is
* indexed by NATIVE_UTF8 bytes, so transform to that */
STRLEN char_bytes_len = PL_utf8skip[I8_TO_NATIVE_UTF8(start)];
if (start < 0xc2) {
croak("fail: Expecting start byte, instead got 0x%X at %s line %d",
(U8) *p, __FILE__, __LINE__);
}
code_point = (start & (((char_bytes_len) >= 7)
? 0x00
: (0x1F >> ((char_bytes_len)-2))));
p++;
while (p < e && ((( (U8) *p) & 0xC0) == 0x80)) {
code_point = (code_point << 6) | (( (U8) *p) & 0x3F);
p++;
}
char_end = uvchr_to_utf8(native_utf8, code_point);
sv_catpvn(sv, (char *) native_utf8, char_end - native_utf8);
}
}
#endif
/* for my_cxt tests */
#define MY_CXT_KEY "XS::APItest::_guts" XS_VERSION
typedef struct {
int i;
SV *sv;
GV *cscgv;
AV *cscav;
AV *bhkav;
bool bhk_record;
peep_t orig_peep;
peep_t orig_rpeep;
int peep_recording;
AV *peep_recorder;
AV *rpeep_recorder;
AV *xop_record;
} my_cxt_t;
START_MY_CXT
static int
S_myset_set(pTHX_ SV* sv, MAGIC* mg)
{
SV *isv = (SV*)mg->mg_ptr;
PERL_UNUSED_ARG(sv);
SvIVX(isv)++;
return 0;
}
static MGVTBL vtbl_foo, vtbl_bar;
static MGVTBL vtbl_myset = { 0, S_myset_set, 0, 0, 0, 0, 0, 0 };
static int
S_mycopy_copy(pTHX_ SV *sv, MAGIC* mg, SV *nsv, const char *name, I32 namlen) {
PERL_UNUSED_ARG(sv);
PERL_UNUSED_ARG(nsv);
PERL_UNUSED_ARG(name);
PERL_UNUSED_ARG(namlen);
/* Count that we were called to "copy".
There's actually no point in copying *this* magic onto nsv, as it's a
SCALAR, whereas mg_copy is only triggered for ARRAYs and HASHes.
It's not *exactly* generic. :-( */
++mg->mg_private;
return 0;
}
STATIC MGVTBL vtbl_mycopy = { 0, 0, 0, 0, 0, S_mycopy_copy, 0, 0 };
/* indirect functions to test the [pa]MY_CXT macros */
int
my_cxt_getint_p(pMY_CXT)
{
return MY_CXT.i;
}
void
my_cxt_setint_p(pMY_CXT_ int i)
{
MY_CXT.i = i;
}
SV*
my_cxt_getsv_interp_context(void)
{
dTHX;
dMY_CXT_INTERP(my_perl);
return MY_CXT.sv;
}
SV*
my_cxt_getsv_interp(void)
{
dMY_CXT;
return MY_CXT.sv;
}
void
my_cxt_setsv_p(SV* sv _pMY_CXT)
{
MY_CXT.sv = sv;
}
/* from exception.c */
int apitest_exception(int);
/* from core_or_not.inc */
bool sv_setsv_cow_hashkey_core(void);
bool sv_setsv_cow_hashkey_notcore(void);
/* A routine to test hv_delayfree_ent
(which itself is tested by testing on hv_free_ent */
typedef void (freeent_function)(pTHX_ HV *, HE *);
void
test_freeent(freeent_function *f) {
dSP;
HV *test_hash = newHV();
HE *victim;
SV *test_scalar;
U32 results[4];
int i;
#ifdef PURIFY
victim = (HE*)safemalloc(sizeof(HE));
#else
/* Storing then deleting something should ensure that a hash entry is
available. */
(void) hv_stores(test_hash, "", &PL_sv_yes);
(void) hv_deletes(test_hash, "", 0);
/* We need to "inline" new_he here as it's static, and the functions we
test expect to be able to call del_HE on the HE */
if (!PL_body_roots[HE_SVSLOT])
croak("PL_he_root is 0");
victim = (HE*) PL_body_roots[HE_SVSLOT];
PL_body_roots[HE_SVSLOT] = HeNEXT(victim);
#endif
victim->hent_hek = Perl_share_hek(aTHX_ "", 0, 0);
test_scalar = newSV(0);
SvREFCNT_inc(test_scalar);
HeVAL(victim) = test_scalar;
/* Need this little game else we free the temps on the return stack. */
results[0] = SvREFCNT(test_scalar);
SAVETMPS;
results[1] = SvREFCNT(test_scalar);
f(aTHX_ test_hash, victim);
results[2] = SvREFCNT(test_scalar);
FREETMPS;
results[3] = SvREFCNT(test_scalar);
i = 0;
do {
mXPUSHu(results[i]);
} while (++i < (int)(sizeof(results)/sizeof(results[0])));
/* Goodbye to our extra reference. */
SvREFCNT_dec(test_scalar);
}
/* Not that it matters much, but it's handy for the flipped character to just
* be the opposite case (at least for ASCII-range and most Latin1 as well). */
#define FLIP_BIT ('A' ^ 'a')
static I32
bitflip_key(pTHX_ IV action, SV *field) {
MAGIC *mg = mg_find(field, PERL_MAGIC_uvar);
SV *keysv;
PERL_UNUSED_ARG(action);
if (mg && (keysv = mg->mg_obj)) {
STRLEN len;
const char *p = SvPV(keysv, len);
if (len) {
/* Allow for the flipped val to be longer than the original. This
* is just for testing, so can afford to have some slop */
const STRLEN newlen = len * 2;
SV *newkey = newSV(newlen);
const char * const new_p_orig = SvPVX(newkey);
char *new_p = (char *) new_p_orig;
if (SvUTF8(keysv)) {
const char *const end = p + len;
while (p < end) {
STRLEN curlen;
UV chr = utf8_to_uvchr_buf((U8 *)p, (U8 *) end, &curlen);
/* Make sure don't exceed bounds */
assert(new_p - new_p_orig + curlen < newlen);
new_p = (char *)uvchr_to_utf8((U8 *)new_p, chr ^ FLIP_BIT);
p += curlen;
}
SvUTF8_on(newkey);
} else {
while (len--)
*new_p++ = *p++ ^ FLIP_BIT;
}
*new_p = '\0';
SvCUR_set(newkey, new_p - new_p_orig);
SvPOK_on(newkey);
mg->mg_obj = newkey;
}
}
return 0;
}
static I32
rot13_key(pTHX_ IV action, SV *field) {
MAGIC *mg = mg_find(field, PERL_MAGIC_uvar);
SV *keysv;
PERL_UNUSED_ARG(action);
if (mg && (keysv = mg->mg_obj)) {
STRLEN len;
const char *p = SvPV(keysv, len);
if (len) {
SV *newkey = newSV(len);
char *new_p = SvPVX(newkey);
/* There's a deliberate fencepost error here to loop len + 1 times
to copy the trailing \0 */
do {
char new_c = *p++;
/* Try doing this cleanly and clearly in EBCDIC another way: */
switch (new_c) {
case 'A': new_c = 'N'; break;
case 'B': new_c = 'O'; break;
case 'C': new_c = 'P'; break;
case 'D': new_c = 'Q'; break;
case 'E': new_c = 'R'; break;
case 'F': new_c = 'S'; break;
case 'G': new_c = 'T'; break;
case 'H': new_c = 'U'; break;
case 'I': new_c = 'V'; break;
case 'J': new_c = 'W'; break;
case 'K': new_c = 'X'; break;
case 'L': new_c = 'Y'; break;
case 'M': new_c = 'Z'; break;
case 'N': new_c = 'A'; break;
case 'O': new_c = 'B'; break;
case 'P': new_c = 'C'; break;
case 'Q': new_c = 'D'; break;
case 'R': new_c = 'E'; break;
case 'S': new_c = 'F'; break;
case 'T': new_c = 'G'; break;
case 'U': new_c = 'H'; break;
case 'V': new_c = 'I'; break;
case 'W': new_c = 'J'; break;
case 'X': new_c = 'K'; break;
case 'Y': new_c = 'L'; break;
case 'Z': new_c = 'M'; break;
case 'a': new_c = 'n'; break;
case 'b': new_c = 'o'; break;
case 'c': new_c = 'p'; break;
case 'd': new_c = 'q'; break;
case 'e': new_c = 'r'; break;
case 'f': new_c = 's'; break;
case 'g': new_c = 't'; break;
case 'h': new_c = 'u'; break;
case 'i': new_c = 'v'; break;
case 'j': new_c = 'w'; break;
case 'k': new_c = 'x'; break;
case 'l': new_c = 'y'; break;
case 'm': new_c = 'z'; break;
case 'n': new_c = 'a'; break;
case 'o': new_c = 'b'; break;
case 'p': new_c = 'c'; break;
case 'q': new_c = 'd'; break;
case 'r': new_c = 'e'; break;
case 's': new_c = 'f'; break;
case 't': new_c = 'g'; break;
case 'u': new_c = 'h'; break;
case 'v': new_c = 'i'; break;
case 'w': new_c = 'j'; break;
case 'x': new_c = 'k'; break;
case 'y': new_c = 'l'; break;
case 'z': new_c = 'm'; break;
}
*new_p++ = new_c;
} while (len--);
SvCUR_set(newkey, SvCUR(keysv));
SvPOK_on(newkey);
if (SvUTF8(keysv))
SvUTF8_on(newkey);
mg->mg_obj = newkey;
}
}
return 0;
}
STATIC I32
rmagical_a_dummy(pTHX_ IV idx, SV *sv) {
PERL_UNUSED_ARG(idx);
PERL_UNUSED_ARG(sv);
return 0;
}
/* We could do "= { 0 };" but some versions of gcc do warn
* (with -Wextra) about missing initializer, this is probably gcc
* being a bit too paranoid. But since this is file-static, we can
* just have it without initializer, since it should get
* zero-initialized. */
STATIC MGVTBL rmagical_b;
STATIC void
blockhook_csc_start(pTHX_ int full)
{
dMY_CXT;
AV *const cur = GvAV(MY_CXT.cscgv);
PERL_UNUSED_ARG(full);
SAVEGENERICSV(GvAV(MY_CXT.cscgv));
if (cur) {
Size_t i;
AV *const new_av = newAV();
for (i = 0; i < av_count(cur); i++) {
av_store(new_av, i, newSVsv(*av_fetch(cur, i, 0)));
}
GvAV(MY_CXT.cscgv) = new_av;
}
}
STATIC void
blockhook_csc_pre_end(pTHX_ OP **o)
{
dMY_CXT;
PERL_UNUSED_ARG(o);
/* if we hit the end of a scope we missed the start of, we need to
* unconditionally clear @CSC */
if (GvAV(MY_CXT.cscgv) == MY_CXT.cscav && MY_CXT.cscav) {
av_clear(MY_CXT.cscav);
}
}
STATIC void
blockhook_test_start(pTHX_ int full)
{
dMY_CXT;
AV *av;
if (MY_CXT.bhk_record) {
av = newAV();
av_push(av, newSVpvs("start"));
av_push(av, newSViv(full));
av_push(MY_CXT.bhkav, newRV_noinc(MUTABLE_SV(av)));
}
}
STATIC void
blockhook_test_pre_end(pTHX_ OP **o)
{
dMY_CXT;
PERL_UNUSED_ARG(o);
if (MY_CXT.bhk_record)
av_push(MY_CXT.bhkav, newSVpvs("pre_end"));
}
STATIC void
blockhook_test_post_end(pTHX_ OP **o)
{
dMY_CXT;
PERL_UNUSED_ARG(o);
if (MY_CXT.bhk_record)
av_push(MY_CXT.bhkav, newSVpvs("post_end"));
}
STATIC void
blockhook_test_eval(pTHX_ OP *const o)
{
dMY_CXT;
AV *av;
if (MY_CXT.bhk_record) {
av = newAV();
av_push(av, newSVpvs("eval"));
av_push(av, newSVpv(OP_NAME(o), 0));
av_push(MY_CXT.bhkav, newRV_noinc(MUTABLE_SV(av)));
}
}
STATIC BHK bhk_csc, bhk_test;
STATIC void
my_peep (pTHX_ OP *o)
{
dMY_CXT;
if (!o)
return;
MY_CXT.orig_peep(aTHX_ o);
if (!MY_CXT.peep_recording)
return;
for (; o; o = o->op_next) {
if (o->op_type == OP_CONST && cSVOPx_sv(o) && SvPOK(cSVOPx_sv(o))) {
av_push(MY_CXT.peep_recorder, newSVsv(cSVOPx_sv(o)));
}
}
}
STATIC void
my_rpeep (pTHX_ OP *first)
{
dMY_CXT;
OP *o, *t;
if (!first)
return;
MY_CXT.orig_rpeep(aTHX_ first);
if (!MY_CXT.peep_recording)
return;
for (o = first, t = first; o; o = o->op_next, t = t->op_next) {
if (o->op_type == OP_CONST && cSVOPx_sv(o) && SvPOK(cSVOPx_sv(o))) {
av_push(MY_CXT.rpeep_recorder, newSVsv(cSVOPx_sv(o)));
}
o = o->op_next;
if (!o || o == t) break;
if (o->op_type == OP_CONST && cSVOPx_sv(o) && SvPOK(cSVOPx_sv(o))) {
av_push(MY_CXT.rpeep_recorder, newSVsv(cSVOPx_sv(o)));
}
}
}
STATIC OP *
THX_ck_entersub_args_lists(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
{
PERL_UNUSED_ARG(namegv);
PERL_UNUSED_ARG(ckobj);
return ck_entersub_args_list(entersubop);
}
STATIC OP *
THX_ck_entersub_args_scalars(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
{
OP *aop = cUNOPx(entersubop)->op_first;
PERL_UNUSED_ARG(namegv);
PERL_UNUSED_ARG(ckobj);
if (!OpHAS_SIBLING(aop))
aop = cUNOPx(aop)->op_first;
for (aop = OpSIBLING(aop); OpHAS_SIBLING(aop); aop = OpSIBLING(aop)) {
op_contextualize(aop, G_SCALAR);
}
return entersubop;
}
STATIC OP *
THX_ck_entersub_multi_sum(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
{
OP *sumop = NULL;
OP *parent = entersubop;
OP *pushop = cUNOPx(entersubop)->op_first;
PERL_UNUSED_ARG(namegv);
PERL_UNUSED_ARG(ckobj);
if (!OpHAS_SIBLING(pushop)) {
parent = pushop;
pushop = cUNOPx(pushop)->op_first;
}
while (1) {
OP *aop = OpSIBLING(pushop);
if (!OpHAS_SIBLING(aop))
break;
/* cut out first arg */
op_sibling_splice(parent, pushop, 1, NULL);
op_contextualize(aop, G_SCALAR);
if (sumop) {
sumop = newBINOP(OP_ADD, 0, sumop, aop);
} else {
sumop = aop;
}
}
if (!sumop)
sumop = newSVOP(OP_CONST, 0, newSViv(0));
op_free(entersubop);
return sumop;
}
STATIC void test_op_list_describe_part(SV *res, OP *o);
STATIC void
test_op_list_describe_part(SV *res, OP *o)
{
sv_catpv(res, PL_op_name[o->op_type]);
switch (o->op_type) {
case OP_CONST: {
sv_catpvf(res, "(%d)", (int)SvIV(cSVOPx(o)->op_sv));
} break;
}
if (o->op_flags & OPf_KIDS) {
OP *k;
sv_catpvs(res, "[");
for (k = cUNOPx(o)->op_first; k; k = OpSIBLING(k))
test_op_list_describe_part(res, k);
sv_catpvs(res, "]");
} else {
sv_catpvs(res, ".");
}
}
STATIC char *
test_op_list_describe(OP *o)
{
SV *res = sv_2mortal(newSVpvs(""));
if (o)
test_op_list_describe_part(res, o);
return SvPVX(res);
}
/* the real new*OP functions have a tendency to call fold_constants, and
* other such unhelpful things, so we need our own versions for testing */
#define mkUNOP(t, f) THX_mkUNOP(aTHX_ (t), (f))
static OP *
THX_mkUNOP(pTHX_ U32 type, OP *first)
{
UNOP *unop;
NewOp(1103, unop, 1, UNOP);
unop->op_type = (OPCODE)type;
op_sibling_splice((OP*)unop, NULL, 0, first);
return (OP *)unop;
}
#define mkBINOP(t, f, l) THX_mkBINOP(aTHX_ (t), (f), (l))
static OP *
THX_mkBINOP(pTHX_ U32 type, OP *first, OP *last)
{
BINOP *binop;
NewOp(1103, binop, 1, BINOP);
binop->op_type = (OPCODE)type;
op_sibling_splice((OP*)binop, NULL, 0, last);
op_sibling_splice((OP*)binop, NULL, 0, first);
return (OP *)binop;
}
#define mkLISTOP(t, f, s, l) THX_mkLISTOP(aTHX_ (t), (f), (s), (l))
static OP *
THX_mkLISTOP(pTHX_ U32 type, OP *first, OP *sib, OP *last)
{
LISTOP *listop;
NewOp(1103, listop, 1, LISTOP);
listop->op_type = (OPCODE)type;
op_sibling_splice((OP*)listop, NULL, 0, last);
op_sibling_splice((OP*)listop, NULL, 0, sib);
op_sibling_splice((OP*)listop, NULL, 0, first);
return (OP *)listop;
}
static char *
test_op_linklist_describe(OP *start)
{
SV *rv = sv_2mortal(newSVpvs(""));
OP *o;
o = start = LINKLIST(start);
do {
sv_catpvs(rv, ".");
sv_catpv(rv, OP_NAME(o));
if (o->op_type == OP_CONST)
sv_catsv(rv, cSVOPo->op_sv);
o = o->op_next;
} while (o && o != start);
return SvPVX(rv);
}
/** establish_cleanup operator, ripped off from Scope::Cleanup **/
STATIC void
THX_run_cleanup(pTHX_ void *cleanup_code_ref)
{
dSP;
PUSHSTACK;
ENTER;
SAVETMPS;
PUSHMARK(SP);
call_sv((SV*)cleanup_code_ref, G_VOID|G_DISCARD);
FREETMPS;
LEAVE;
POPSTACK;
}
STATIC OP *
THX_pp_establish_cleanup(pTHX)
{
dSP;
SV *cleanup_code_ref;
cleanup_code_ref = newSVsv(POPs);
SAVEFREESV(cleanup_code_ref);
SAVEDESTRUCTOR_X(THX_run_cleanup, cleanup_code_ref);
if(GIMME_V != G_VOID) PUSHs(&PL_sv_undef);
RETURN;
}
STATIC OP *
THX_ck_entersub_establish_cleanup(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
{
OP *parent, *pushop, *argop, *estop;
ck_entersub_args_proto(entersubop, namegv, ckobj);
parent = entersubop;
pushop = cUNOPx(entersubop)->op_first;
if(!OpHAS_SIBLING(pushop)) {
parent = pushop;
pushop = cUNOPx(pushop)->op_first;
}
/* extract out first arg, then delete the rest of the tree */
argop = OpSIBLING(pushop);
op_sibling_splice(parent, pushop, 1, NULL);
op_free(entersubop);
estop = mkUNOP(OP_RAND, argop);
estop->op_ppaddr = THX_pp_establish_cleanup;
PL_hints |= HINT_BLOCK_SCOPE;
return estop;
}
STATIC OP *
THX_ck_entersub_postinc(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
{
OP *parent, *pushop, *argop;
ck_entersub_args_proto(entersubop, namegv, ckobj);
parent = entersubop;
pushop = cUNOPx(entersubop)->op_first;
if(!OpHAS_SIBLING(pushop)) {
parent = pushop;
pushop = cUNOPx(pushop)->op_first;
}
argop = OpSIBLING(pushop);
op_sibling_splice(parent, pushop, 1, NULL);
op_free(entersubop);
return newUNOP(OP_POSTINC, 0,
op_lvalue(op_contextualize(argop, G_SCALAR), OP_POSTINC));
}
STATIC OP *
THX_ck_entersub_pad_scalar(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
{
OP *pushop, *argop;
PADOFFSET padoff = NOT_IN_PAD;
SV *a0, *a1;
ck_entersub_args_proto(entersubop, namegv, ckobj);
pushop = cUNOPx(entersubop)->op_first;
if(!OpHAS_SIBLING(pushop))
pushop = cUNOPx(pushop)->op_first;
argop = OpSIBLING(pushop);
if(argop->op_type != OP_CONST || OpSIBLING(argop)->op_type != OP_CONST)
croak("bad argument expression type for pad_scalar()");
a0 = cSVOPx_sv(argop);
a1 = cSVOPx_sv(OpSIBLING(argop));
switch(SvIV(a0)) {
case 1: {
SV *namesv = sv_2mortal(newSVpvs("$"));
sv_catsv(namesv, a1);
padoff = pad_findmy_sv(namesv, 0);
} break;
case 2: {
char *namepv;
STRLEN namelen;
SV *namesv = sv_2mortal(newSVpvs("$"));
sv_catsv(namesv, a1);
namepv = SvPV(namesv, namelen);
padoff = pad_findmy_pvn(namepv, namelen, SvUTF8(namesv));
} break;
case 3: {
char *namepv;
SV *namesv = sv_2mortal(newSVpvs("$"));
sv_catsv(namesv, a1);
namepv = SvPV_nolen(namesv);
padoff = pad_findmy_pv(namepv, SvUTF8(namesv));
} break;
case 4: {
padoff = pad_findmy_pvs("$foo", 0);
} break;
default: croak("bad type value for pad_scalar()");
}
op_free(entersubop);
if(padoff == NOT_IN_PAD) {
return newSVOP(OP_CONST, 0, newSVpvs("NOT_IN_PAD"));
} else if(PAD_COMPNAME_FLAGS_isOUR(padoff)) {
return newSVOP(OP_CONST, 0, newSVpvs("NOT_MY"));
} else {
OP *padop = newOP(OP_PADSV, 0);
padop->op_targ = padoff;
return padop;
}
}
/** RPN keyword parser **/
#define sv_is_glob(sv) (SvTYPE(sv) == SVt_PVGV)
#define sv_is_regexp(sv) (SvTYPE(sv) == SVt_REGEXP)
#define sv_is_string(sv) \
(!sv_is_glob(sv) && !sv_is_regexp(sv) && \
(SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK|SVp_IOK|SVp_NOK|SVp_POK)))
static SV *hintkey_rpn_sv, *hintkey_calcrpn_sv, *hintkey_stufftest_sv;
static SV *hintkey_swaptwostmts_sv, *hintkey_looprest_sv;
static SV *hintkey_scopelessblock_sv;
static SV *hintkey_stmtasexpr_sv, *hintkey_stmtsasexpr_sv;
static SV *hintkey_loopblock_sv, *hintkey_blockasexpr_sv;
static SV *hintkey_swaplabel_sv, *hintkey_labelconst_sv;
static SV *hintkey_arrayfullexpr_sv, *hintkey_arraylistexpr_sv;
static SV *hintkey_arraytermexpr_sv, *hintkey_arrayarithexpr_sv;
static SV *hintkey_arrayexprflags_sv;
static SV *hintkey_subsignature_sv;
static SV *hintkey_DEFSV_sv;
static SV *hintkey_with_vars_sv;
static SV *hintkey_join_with_space_sv;
static int (*next_keyword_plugin)(pTHX_ char *, STRLEN, OP **);
/* low-level parser helpers */
#define PL_bufptr (PL_parser->bufptr)
#define PL_bufend (PL_parser->bufend)
/* RPN parser */
#define parse_var() THX_parse_var(aTHX)
static OP *THX_parse_var(pTHX)
{
char *s = PL_bufptr;
char *start = s;
PADOFFSET varpos;
OP *padop;
if(*s != '$') croak("RPN syntax error");
while(1) {
char c = *++s;
if(!isALNUM(c)) break;
}
if(s-start < 2) croak("RPN syntax error");
lex_read_to(s);
varpos = pad_findmy_pvn(start, s-start, 0);
if(varpos == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(varpos))
croak("RPN only supports \"my\" variables");
padop = newOP(OP_PADSV, 0);
padop->op_targ = varpos;
return padop;
}
#define push_rpn_item(o) \
op_sibling_splice(parent, NULL, 0, o);
#define pop_rpn_item() ( \
(tmpop = op_sibling_splice(parent, NULL, 1, NULL)) \
? tmpop : (croak("RPN stack underflow"), (OP*)NULL))
#define parse_rpn_expr() THX_parse_rpn_expr(aTHX)
static OP *THX_parse_rpn_expr(pTHX)
{
OP *tmpop;
/* fake parent for splice to mess with */
OP *parent = mkBINOP(OP_NULL, NULL, NULL);
while(1) {
I32 c;
lex_read_space(0);
c = lex_peek_unichar(0);
switch(c) {
case /*(*/')': case /*{*/'}': {
OP *result = pop_rpn_item();
if(cLISTOPx(parent)->op_first)
croak("RPN expression must return a single value");
op_free(parent);
return result;
} break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9': {
UV val = 0;
do {
lex_read_unichar(0);
val = 10*val + (c - '0');
c = lex_peek_unichar(0);
} while(c >= '0' && c <= '9');
push_rpn_item(newSVOP(OP_CONST, 0, newSVuv(val)));
} break;
case '$': {
push_rpn_item(parse_var());
} break;
case '+': {
OP *b = pop_rpn_item();
OP *a = pop_rpn_item();
lex_read_unichar(0);
push_rpn_item(newBINOP(OP_I_ADD, 0, a, b));
} break;
case '-': {
OP *b = pop_rpn_item();
OP *a = pop_rpn_item();
lex_read_unichar(0);
push_rpn_item(newBINOP(OP_I_SUBTRACT, 0, a, b));
} break;
case '*': {
OP *b = pop_rpn_item();
OP *a = pop_rpn_item();
lex_read_unichar(0);
push_rpn_item(newBINOP(OP_I_MULTIPLY, 0, a, b));
} break;
case '/': {
OP *b = pop_rpn_item();
OP *a = pop_rpn_item();
lex_read_unichar(0);
push_rpn_item(newBINOP(OP_I_DIVIDE, 0, a, b));
} break;
case '%': {
OP *b = pop_rpn_item();
OP *a = pop_rpn_item();
lex_read_unichar(0);
push_rpn_item(newBINOP(OP_I_MODULO, 0, a, b));
} break;
default: {
croak("RPN syntax error");
} break;
}
}
}
#define parse_keyword_rpn() THX_parse_keyword_rpn(aTHX)
static OP *THX_parse_keyword_rpn(pTHX)
{
OP *op;
lex_read_space(0);
if(lex_peek_unichar(0) != '('/*)*/)
croak("RPN expression must be parenthesised");
lex_read_unichar(0);
op = parse_rpn_expr();
if(lex_peek_unichar(0) != /*(*/')')
croak("RPN expression must be parenthesised");
lex_read_unichar(0);
return op;
}
#define parse_keyword_calcrpn() THX_parse_keyword_calcrpn(aTHX)
static OP *THX_parse_keyword_calcrpn(pTHX)
{
OP *varop, *exprop;
lex_read_space(0);
varop = parse_var();
lex_read_space(0);
if(lex_peek_unichar(0) != '{'/*}*/)
croak("RPN expression must be braced");
lex_read_unichar(0);
exprop = parse_rpn_expr();
if(lex_peek_unichar(0) != /*{*/'}')
croak("RPN expression must be braced");
lex_read_unichar(0);
return newASSIGNOP(OPf_STACKED, varop, 0, exprop);
}
#define parse_keyword_stufftest() THX_parse_keyword_stufftest(aTHX)
static OP *THX_parse_keyword_stufftest(pTHX)
{
I32 c;
bool do_stuff;
lex_read_space(0);
do_stuff = lex_peek_unichar(0) == '+';
if(do_stuff) {
lex_read_unichar(0);
lex_read_space(0);
}
c = lex_peek_unichar(0);
if(c == ';') {
lex_read_unichar(0);
} else if(c != /*{*/'}') {
croak("syntax error");
}
if(do_stuff) lex_stuff_pvs(" ", 0);
return newOP(OP_NULL, 0);
}
#define parse_keyword_swaptwostmts() THX_parse_keyword_swaptwostmts(aTHX)
static OP *THX_parse_keyword_swaptwostmts(pTHX)
{
OP *a, *b;
a = parse_fullstmt(0);
b = parse_fullstmt(0);
if(a && b)
PL_hints |= HINT_BLOCK_SCOPE;
return op_append_list(OP_LINESEQ, b, a);
}
#define parse_keyword_looprest() THX_parse_keyword_looprest(aTHX)
static OP *THX_parse_keyword_looprest(pTHX)
{
return newWHILEOP(0, 1, NULL, newSVOP(OP_CONST, 0, &PL_sv_yes),
parse_stmtseq(0), NULL, 1);
}
#define parse_keyword_scopelessblock() THX_parse_keyword_scopelessblock(aTHX)
static OP *THX_parse_keyword_scopelessblock(pTHX)
{
I32 c;
OP *body;
lex_read_space(0);
if(lex_peek_unichar(0) != '{'/*}*/) croak("syntax error");
lex_read_unichar(0);
body = parse_stmtseq(0);
c = lex_peek_unichar(0);
if(c != /*{*/'}' && c != /*[*/']' && c != /*(*/')') croak("syntax error");
lex_read_unichar(0);
return body;
}
#define parse_keyword_stmtasexpr() THX_parse_keyword_stmtasexpr(aTHX)
static OP *THX_parse_keyword_stmtasexpr(pTHX)
{
OP *o = parse_barestmt(0);
if (!o) o = newOP(OP_STUB, 0);
if (PL_hints & HINT_BLOCK_SCOPE) o->op_flags |= OPf_PARENS;
return op_scope(o);
}
#define parse_keyword_stmtsasexpr() THX_parse_keyword_stmtsasexpr(aTHX)
static OP *THX_parse_keyword_stmtsasexpr(pTHX)
{
OP *o;
lex_read_space(0);
if(lex_peek_unichar(0) != '{'/*}*/) croak("syntax error");