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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
|
/* scope.c
*
* Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
* 2000, 2001, 2002, 2003, 2004, 2005, by Larry Wall and others
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*
*/
/*
* "For the fashion of Minas Tirith was such that it was built on seven
* levels..."
*/
/* This file contains functions to manipulate several of Perl's stacks;
* in particular it contains code to push various types of things onto
* the savestack, then to pop them off and perform the correct restorative
* action for each one. This corresponds to the cleanup Perl does at
* each scope exit.
*/
#include "EXTERN.h"
#define PERL_IN_SCOPE_C
#include "perl.h"
SV**
Perl_stack_grow(pTHX_ SV **sp, SV **p, int n)
{
PL_stack_sp = sp;
#ifndef STRESS_REALLOC
av_extend(PL_curstack, (p - PL_stack_base) + (n) + 128);
#else
av_extend(PL_curstack, (p - PL_stack_base) + (n) + 1);
#endif
return PL_stack_sp;
}
#ifndef STRESS_REALLOC
#define GROW(old) ((old) * 3 / 2)
#else
#define GROW(old) ((old) + 1)
#endif
PERL_SI *
Perl_new_stackinfo(pTHX_ I32 stitems, I32 cxitems)
{
PERL_SI *si;
New(56, si, 1, PERL_SI);
si->si_stack = newAV();
AvREAL_off(si->si_stack);
av_extend(si->si_stack, stitems > 0 ? stitems-1 : 0);
AvALLOC(si->si_stack)[0] = &PL_sv_undef;
AvFILLp(si->si_stack) = 0;
si->si_prev = 0;
si->si_next = 0;
si->si_cxmax = cxitems - 1;
si->si_cxix = -1;
si->si_type = PERLSI_UNDEF;
New(56, si->si_cxstack, cxitems, PERL_CONTEXT);
/* Without any kind of initialising PUSHSUBST()
* in pp_subst() will read uninitialised heap. */
Poison(si->si_cxstack, cxitems, PERL_CONTEXT);
return si;
}
I32
Perl_cxinc(pTHX)
{
const IV old_max = cxstack_max;
cxstack_max = GROW(cxstack_max);
Renew(cxstack, cxstack_max + 1, PERL_CONTEXT); /* XXX should fix CXINC macro */
/* Without any kind of initialising deep enough recursion
* will end up reading uninitialised PERL_CONTEXTs. */
Poison(cxstack + old_max + 1, cxstack_max - old_max, PERL_CONTEXT);
return cxstack_ix + 1;
}
void
Perl_push_scope(pTHX)
{
if (PL_scopestack_ix == PL_scopestack_max) {
PL_scopestack_max = GROW(PL_scopestack_max);
Renew(PL_scopestack, PL_scopestack_max, I32);
}
PL_scopestack[PL_scopestack_ix++] = PL_savestack_ix;
}
void
Perl_pop_scope(pTHX)
{
const I32 oldsave = PL_scopestack[--PL_scopestack_ix];
LEAVE_SCOPE(oldsave);
}
void
Perl_markstack_grow(pTHX)
{
const I32 oldmax = PL_markstack_max - PL_markstack;
const I32 newmax = GROW(oldmax);
Renew(PL_markstack, newmax, I32);
PL_markstack_ptr = PL_markstack + oldmax;
PL_markstack_max = PL_markstack + newmax;
}
void
Perl_savestack_grow(pTHX)
{
PL_savestack_max = GROW(PL_savestack_max) + 4;
Renew(PL_savestack, PL_savestack_max, ANY);
}
void
Perl_savestack_grow_cnt(pTHX_ I32 need)
{
PL_savestack_max = PL_savestack_ix + need;
Renew(PL_savestack, PL_savestack_max, ANY);
}
#undef GROW
void
Perl_tmps_grow(pTHX_ I32 n)
{
#ifndef STRESS_REALLOC
if (n < 128)
n = (PL_tmps_max < 512) ? 128 : 512;
#endif
PL_tmps_max = PL_tmps_ix + n + 1;
Renew(PL_tmps_stack, PL_tmps_max, SV*);
}
void
Perl_free_tmps(pTHX)
{
/* XXX should tmps_floor live in cxstack? */
const I32 myfloor = PL_tmps_floor;
while (PL_tmps_ix > myfloor) { /* clean up after last statement */
SV* sv = PL_tmps_stack[PL_tmps_ix];
PL_tmps_stack[PL_tmps_ix--] = Nullsv;
if (sv && sv != &PL_sv_undef) {
SvTEMP_off(sv);
SvREFCNT_dec(sv); /* note, can modify tmps_ix!!! */
}
}
}
STATIC SV *
S_save_scalar_at(pTHX_ SV **sptr)
{
register SV *sv;
SV *osv = *sptr;
sv = *sptr = NEWSV(0,0);
if (SvTYPE(osv) >= SVt_PVMG && SvMAGIC(osv) && SvTYPE(osv) != SVt_PVGV) {
MAGIC *mg;
sv_upgrade(sv, SvTYPE(osv));
if (SvGMAGICAL(osv)) {
const bool oldtainted = PL_tainted;
mg_get(osv); /* note, can croak! */
if (PL_tainting && PL_tainted &&
(mg = mg_find(osv, PERL_MAGIC_taint))) {
SAVESPTR(mg->mg_obj);
mg->mg_obj = osv;
}
SvFLAGS(osv) |= (SvFLAGS(osv) &
(SVp_NOK|SVp_POK)) >> PRIVSHIFT;
PL_tainted = oldtainted;
}
SvMAGIC_set(sv, SvMAGIC(osv));
/* if it's a special scalar or if it has no 'set' magic,
* propagate the SvREADONLY flag. --rgs 20030922 */
for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
if (mg->mg_type == '\0'
|| !(mg->mg_virtual && mg->mg_virtual->svt_set))
{
SvFLAGS(sv) |= SvREADONLY(osv);
break;
}
}
SvFLAGS(sv) |= SvMAGICAL(osv);
/* XXX SvMAGIC() is *shared* between osv and sv. This can
* lead to coredumps when both SVs are destroyed without one
* of their SvMAGIC() slots being NULLed. */
PL_localizing = 1;
SvSETMAGIC(sv);
PL_localizing = 0;
}
return sv;
}
SV *
Perl_save_scalar(pTHX_ GV *gv)
{
SV **sptr = &GvSV(gv);
SSCHECK(3);
SSPUSHPTR(SvREFCNT_inc(gv));
SSPUSHPTR(SvREFCNT_inc(*sptr));
SSPUSHINT(SAVEt_SV);
return save_scalar_at(sptr);
}
SV*
Perl_save_svref(pTHX_ SV **sptr)
{
SSCHECK(3);
SSPUSHPTR(sptr);
SSPUSHPTR(SvREFCNT_inc(*sptr));
SSPUSHINT(SAVEt_SVREF);
return save_scalar_at(sptr);
}
/* Like save_sptr(), but also SvREFCNT_dec()s the new value. Can be used to
* restore a global SV to its prior contents, freeing new value. */
void
Perl_save_generic_svref(pTHX_ SV **sptr)
{
SSCHECK(3);
SSPUSHPTR(sptr);
SSPUSHPTR(SvREFCNT_inc(*sptr));
SSPUSHINT(SAVEt_GENERIC_SVREF);
}
/* Like save_pptr(), but also Safefree()s the new value if it is different
* from the old one. Can be used to restore a global char* to its prior
* contents, freeing new value. */
void
Perl_save_generic_pvref(pTHX_ char **str)
{
SSCHECK(3);
SSPUSHPTR(str);
SSPUSHPTR(*str);
SSPUSHINT(SAVEt_GENERIC_PVREF);
}
/* Like save_generic_pvref(), but uses PerlMemShared_free() rather than Safefree().
* Can be used to restore a shared global char* to its prior
* contents, freeing new value. */
void
Perl_save_shared_pvref(pTHX_ char **str)
{
SSCHECK(3);
SSPUSHPTR(str);
SSPUSHPTR(*str);
SSPUSHINT(SAVEt_SHARED_PVREF);
}
/* set the SvFLAGS specified by mask to the values in val */
void
Perl_save_set_svflags(pTHX_ SV* sv, U32 mask, U32 val)
{
SSCHECK(4);
SSPUSHPTR(sv);
SSPUSHINT(mask);
SSPUSHINT(val);
SSPUSHINT(SAVEt_SET_SVFLAGS);
}
void
Perl_save_gp(pTHX_ GV *gv, I32 empty)
{
SSGROW(6);
SSPUSHIV((IV)SvLEN(gv));
SvLEN_set(gv, 0); /* forget that anything was allocated here */
SSPUSHIV((IV)SvCUR(gv));
SSPUSHPTR(SvPVX(gv));
SvPOK_off(gv);
SSPUSHPTR(SvREFCNT_inc(gv));
SSPUSHPTR(GvGP(gv));
SSPUSHINT(SAVEt_GP);
if (empty) {
register GP *gp;
Newz(602, gp, 1, GP);
if (GvCVu(gv))
PL_sub_generation++; /* taking a method out of circulation */
if (GvIOp(gv) && (IoFLAGS(GvIOp(gv)) & IOf_ARGV)) {
gp->gp_io = newIO();
IoFLAGS(gp->gp_io) |= IOf_ARGV|IOf_START;
}
GvGP(gv) = gp_ref(gp);
GvSV(gv) = NEWSV(72,0);
GvLINE(gv) = CopLINE(PL_curcop);
/* XXX Ideally this cast would be replaced with a change to const char*
in the struct. */
GvFILE(gv) = CopFILE(PL_curcop) ? CopFILE(PL_curcop) : (char *) "";
GvEGV(gv) = gv;
}
else {
gp_ref(GvGP(gv));
GvINTRO_on(gv);
}
}
AV *
Perl_save_ary(pTHX_ GV *gv)
{
AV *oav = GvAVn(gv);
AV *av;
if (!AvREAL(oav) && AvREIFY(oav))
av_reify(oav);
SSCHECK(3);
SSPUSHPTR(gv);
SSPUSHPTR(oav);
SSPUSHINT(SAVEt_AV);
GvAV(gv) = Null(AV*);
av = GvAVn(gv);
if (SvMAGIC(oav)) {
SvMAGIC_set(av, SvMAGIC(oav));
SvFLAGS((SV*)av) |= SvMAGICAL(oav);
SvMAGICAL_off(oav);
SvMAGIC_set(oav, NULL);
PL_localizing = 1;
SvSETMAGIC((SV*)av);
PL_localizing = 0;
}
return av;
}
HV *
Perl_save_hash(pTHX_ GV *gv)
{
HV *ohv, *hv;
SSCHECK(3);
SSPUSHPTR(gv);
SSPUSHPTR(ohv = GvHVn(gv));
SSPUSHINT(SAVEt_HV);
GvHV(gv) = Null(HV*);
hv = GvHVn(gv);
if (SvMAGIC(ohv)) {
SvMAGIC_set(hv, SvMAGIC(ohv));
SvFLAGS((SV*)hv) |= SvMAGICAL(ohv);
SvMAGICAL_off(ohv);
SvMAGIC_set(ohv, NULL);
PL_localizing = 1;
SvSETMAGIC((SV*)hv);
PL_localizing = 0;
}
return hv;
}
void
Perl_save_item(pTHX_ register SV *item)
{
register SV *sv = newSVsv(item);
SSCHECK(3);
SSPUSHPTR(item); /* remember the pointer */
SSPUSHPTR(sv); /* remember the value */
SSPUSHINT(SAVEt_ITEM);
}
void
Perl_save_int(pTHX_ int *intp)
{
SSCHECK(3);
SSPUSHINT(*intp);
SSPUSHPTR(intp);
SSPUSHINT(SAVEt_INT);
}
void
Perl_save_long(pTHX_ long int *longp)
{
SSCHECK(3);
SSPUSHLONG(*longp);
SSPUSHPTR(longp);
SSPUSHINT(SAVEt_LONG);
}
void
Perl_save_bool(pTHX_ bool *boolp)
{
SSCHECK(3);
SSPUSHBOOL(*boolp);
SSPUSHPTR(boolp);
SSPUSHINT(SAVEt_BOOL);
}
void
Perl_save_I32(pTHX_ I32 *intp)
{
SSCHECK(3);
SSPUSHINT(*intp);
SSPUSHPTR(intp);
SSPUSHINT(SAVEt_I32);
}
void
Perl_save_I16(pTHX_ I16 *intp)
{
SSCHECK(3);
SSPUSHINT(*intp);
SSPUSHPTR(intp);
SSPUSHINT(SAVEt_I16);
}
void
Perl_save_I8(pTHX_ I8 *bytep)
{
SSCHECK(3);
SSPUSHINT(*bytep);
SSPUSHPTR(bytep);
SSPUSHINT(SAVEt_I8);
}
void
Perl_save_iv(pTHX_ IV *ivp)
{
SSCHECK(3);
SSPUSHIV(*ivp);
SSPUSHPTR(ivp);
SSPUSHINT(SAVEt_IV);
}
/* Cannot use save_sptr() to store a char* since the SV** cast will
* force word-alignment and we'll miss the pointer.
*/
void
Perl_save_pptr(pTHX_ char **pptr)
{
SSCHECK(3);
SSPUSHPTR(*pptr);
SSPUSHPTR(pptr);
SSPUSHINT(SAVEt_PPTR);
}
void
Perl_save_vptr(pTHX_ void *ptr)
{
SSCHECK(3);
SSPUSHPTR(*(char**)ptr);
SSPUSHPTR(ptr);
SSPUSHINT(SAVEt_VPTR);
}
void
Perl_save_sptr(pTHX_ SV **sptr)
{
SSCHECK(3);
SSPUSHPTR(*sptr);
SSPUSHPTR(sptr);
SSPUSHINT(SAVEt_SPTR);
}
void
Perl_save_padsv(pTHX_ PADOFFSET off)
{
SSCHECK(4);
ASSERT_CURPAD_ACTIVE("save_padsv");
SSPUSHPTR(PL_curpad[off]);
SSPUSHPTR(PL_comppad);
SSPUSHLONG((long)off);
SSPUSHINT(SAVEt_PADSV);
}
SV **
Perl_save_threadsv(pTHX_ PADOFFSET i)
{
Perl_croak(aTHX_ "panic: save_threadsv called in non-threaded perl");
(void)i;
NORETURN_FUNCTION_END;
}
void
Perl_save_nogv(pTHX_ GV *gv)
{
SSCHECK(2);
SSPUSHPTR(gv);
SSPUSHINT(SAVEt_NSTAB);
}
void
Perl_save_hptr(pTHX_ HV **hptr)
{
SSCHECK(3);
SSPUSHPTR(*hptr);
SSPUSHPTR(hptr);
SSPUSHINT(SAVEt_HPTR);
}
void
Perl_save_aptr(pTHX_ AV **aptr)
{
SSCHECK(3);
SSPUSHPTR(*aptr);
SSPUSHPTR(aptr);
SSPUSHINT(SAVEt_APTR);
}
void
Perl_save_freesv(pTHX_ SV *sv)
{
SSCHECK(2);
SSPUSHPTR(sv);
SSPUSHINT(SAVEt_FREESV);
}
void
Perl_save_mortalizesv(pTHX_ SV *sv)
{
SSCHECK(2);
SSPUSHPTR(sv);
SSPUSHINT(SAVEt_MORTALIZESV);
}
void
Perl_save_freeop(pTHX_ OP *o)
{
SSCHECK(2);
SSPUSHPTR(o);
SSPUSHINT(SAVEt_FREEOP);
}
void
Perl_save_freepv(pTHX_ char *pv)
{
SSCHECK(2);
SSPUSHPTR(pv);
SSPUSHINT(SAVEt_FREEPV);
}
void
Perl_save_clearsv(pTHX_ SV **svp)
{
ASSERT_CURPAD_ACTIVE("save_clearsv");
SSCHECK(2);
SSPUSHLONG((long)(svp-PL_curpad));
SSPUSHINT(SAVEt_CLEARSV);
SvPADSTALE_off(*svp); /* mark lexical as active */
}
void
Perl_save_delete(pTHX_ HV *hv, char *key, I32 klen)
{
SSCHECK(4);
SSPUSHINT(klen);
SSPUSHPTR(key);
SSPUSHPTR(SvREFCNT_inc(hv));
SSPUSHINT(SAVEt_DELETE);
}
void
Perl_save_list(pTHX_ register SV **sarg, I32 maxsarg)
{
register SV *sv;
register I32 i;
for (i = 1; i <= maxsarg; i++) {
sv = NEWSV(0,0);
sv_setsv(sv,sarg[i]);
SSCHECK(3);
SSPUSHPTR(sarg[i]); /* remember the pointer */
SSPUSHPTR(sv); /* remember the value */
SSPUSHINT(SAVEt_ITEM);
}
}
void
Perl_save_destructor(pTHX_ DESTRUCTORFUNC_NOCONTEXT_t f, void* p)
{
SSCHECK(3);
SSPUSHDPTR(f);
SSPUSHPTR(p);
SSPUSHINT(SAVEt_DESTRUCTOR);
}
void
Perl_save_destructor_x(pTHX_ DESTRUCTORFUNC_t f, void* p)
{
SSCHECK(3);
SSPUSHDXPTR(f);
SSPUSHPTR(p);
SSPUSHINT(SAVEt_DESTRUCTOR_X);
}
void
Perl_save_aelem(pTHX_ const AV *av, I32 idx, SV **sptr)
{
SV *sv;
SSCHECK(4);
SSPUSHPTR(SvREFCNT_inc(av));
SSPUSHINT(idx);
SSPUSHPTR(SvREFCNT_inc(*sptr));
SSPUSHINT(SAVEt_AELEM);
/* if it gets reified later, the restore will have the wrong refcnt */
if (!AvREAL(av) && AvREIFY(av))
(void)SvREFCNT_inc(*sptr);
save_scalar_at(sptr);
sv = *sptr;
/* If we're localizing a tied array element, this new sv
* won't actually be stored in the array - so it won't get
* reaped when the localize ends. Ensure it gets reaped by
* mortifying it instead. DAPM */
if (SvTIED_mg(sv, PERL_MAGIC_tiedelem))
sv_2mortal(sv);
}
void
Perl_save_helem(pTHX_ HV *hv, SV *key, SV **sptr)
{
SV *sv;
SSCHECK(4);
SSPUSHPTR(SvREFCNT_inc(hv));
SSPUSHPTR(SvREFCNT_inc(key));
SSPUSHPTR(SvREFCNT_inc(*sptr));
SSPUSHINT(SAVEt_HELEM);
save_scalar_at(sptr);
sv = *sptr;
/* If we're localizing a tied hash element, this new sv
* won't actually be stored in the hash - so it won't get
* reaped when the localize ends. Ensure it gets reaped by
* mortifying it instead. DAPM */
if (SvTIED_mg(sv, PERL_MAGIC_tiedelem))
sv_2mortal(sv);
}
void
Perl_save_op(pTHX)
{
SSCHECK(2);
SSPUSHPTR(PL_op);
SSPUSHINT(SAVEt_OP);
}
I32
Perl_save_alloc(pTHX_ I32 size, I32 pad)
{
register const I32 start = pad + ((char*)&PL_savestack[PL_savestack_ix]
- (char*)PL_savestack);
register const I32 elems = 1 + ((size + pad - 1) / sizeof(*PL_savestack));
/* SSCHECK may not be good enough */
while (PL_savestack_ix + elems + 2 > PL_savestack_max)
savestack_grow();
PL_savestack_ix += elems;
SSPUSHINT(elems);
SSPUSHINT(SAVEt_ALLOC);
return start;
}
void
Perl_leave_scope(pTHX_ I32 base)
{
register SV *sv;
register SV *value;
register GV *gv;
register AV *av;
register HV *hv;
register void* ptr;
register char* str;
I32 i;
if (base < -1)
Perl_croak(aTHX_ "panic: corrupt saved stack index");
while (PL_savestack_ix > base) {
switch (SSPOPINT) {
case SAVEt_ITEM: /* normal string */
value = (SV*)SSPOPPTR;
sv = (SV*)SSPOPPTR;
sv_replace(sv,value);
PL_localizing = 2;
SvSETMAGIC(sv);
PL_localizing = 0;
break;
case SAVEt_SV: /* scalar reference */
value = (SV*)SSPOPPTR;
gv = (GV*)SSPOPPTR;
ptr = &GvSV(gv);
av = (AV*)gv; /* what to refcnt_dec */
goto restore_sv;
case SAVEt_GENERIC_PVREF: /* generic pv */
str = (char*)SSPOPPTR;
ptr = SSPOPPTR;
if (*(char**)ptr != str) {
Safefree(*(char**)ptr);
*(char**)ptr = str;
}
break;
case SAVEt_SHARED_PVREF: /* shared pv */
str = (char*)SSPOPPTR;
ptr = SSPOPPTR;
if (*(char**)ptr != str) {
#ifdef NETWARE
PerlMem_free(*(char**)ptr);
#else
PerlMemShared_free(*(char**)ptr);
#endif
*(char**)ptr = str;
}
break;
case SAVEt_GENERIC_SVREF: /* generic sv */
value = (SV*)SSPOPPTR;
ptr = SSPOPPTR;
sv = *(SV**)ptr;
*(SV**)ptr = value;
SvREFCNT_dec(sv);
SvREFCNT_dec(value);
break;
case SAVEt_SVREF: /* scalar reference */
value = (SV*)SSPOPPTR;
ptr = SSPOPPTR;
av = Nullav; /* what to refcnt_dec */
restore_sv:
sv = *(SV**)ptr;
DEBUG_S(PerlIO_printf(Perl_debug_log,
"restore svref: %p %p:%s -> %p:%s\n",
ptr, sv, SvPEEK(sv), value, SvPEEK(value)));
if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv) &&
SvTYPE(sv) != SVt_PVGV)
{
(void)SvUPGRADE(value, SvTYPE(sv));
SvMAGIC_set(value, SvMAGIC(sv));
SvFLAGS(value) |= SvMAGICAL(sv);
SvMAGICAL_off(sv);
SvMAGIC_set(sv, 0);
}
/* XXX This branch is pretty bogus. This code irretrievably
* clears(!) the magic on the SV (either to avoid further
* croaking that might ensue when the SvSETMAGIC() below is
* called, or to avoid two different SVs pointing at the same
* SvMAGIC()). This needs a total rethink. --GSAR */
else if (SvTYPE(value) >= SVt_PVMG && SvMAGIC(value) &&
SvTYPE(value) != SVt_PVGV)
{
SvFLAGS(value) |= (SvFLAGS(value) &
(SVp_NOK|SVp_POK)) >> PRIVSHIFT;
SvMAGICAL_off(value);
/* XXX this is a leak when we get here because the
* mg_get() in save_scalar_at() croaked */
SvMAGIC_set(value, NULL);
}
*(SV**)ptr = value;
SvREFCNT_dec(sv);
PL_localizing = 2;
SvSETMAGIC(value);
PL_localizing = 0;
SvREFCNT_dec(value);
if (av) /* actually an av, hv or gv */
SvREFCNT_dec(av);
break;
case SAVEt_AV: /* array reference */
av = (AV*)SSPOPPTR;
gv = (GV*)SSPOPPTR;
if (GvAV(gv)) {
AV * const goner = GvAV(gv);
SvMAGIC_set(av, SvMAGIC(goner));
SvFLAGS((SV*)av) |= SvMAGICAL(goner);
SvMAGICAL_off(goner);
SvMAGIC_set(goner, NULL);
SvREFCNT_dec(goner);
}
GvAV(gv) = av;
if (SvMAGICAL(av)) {
PL_localizing = 2;
SvSETMAGIC((SV*)av);
PL_localizing = 0;
}
break;
case SAVEt_HV: /* hash reference */
hv = (HV*)SSPOPPTR;
gv = (GV*)SSPOPPTR;
if (GvHV(gv)) {
HV * const goner = GvHV(gv);
SvMAGIC_set(hv, SvMAGIC(goner));
SvFLAGS(hv) |= SvMAGICAL(goner);
SvMAGICAL_off(goner);
SvMAGIC_set(goner, NULL);
SvREFCNT_dec(goner);
}
GvHV(gv) = hv;
if (SvMAGICAL(hv)) {
PL_localizing = 2;
SvSETMAGIC((SV*)hv);
PL_localizing = 0;
}
break;
case SAVEt_INT: /* int reference */
ptr = SSPOPPTR;
*(int*)ptr = (int)SSPOPINT;
break;
case SAVEt_LONG: /* long reference */
ptr = SSPOPPTR;
*(long*)ptr = (long)SSPOPLONG;
break;
case SAVEt_BOOL: /* bool reference */
ptr = SSPOPPTR;
*(bool*)ptr = (bool)SSPOPBOOL;
break;
case SAVEt_I32: /* I32 reference */
ptr = SSPOPPTR;
*(I32*)ptr = (I32)SSPOPINT;
break;
case SAVEt_I16: /* I16 reference */
ptr = SSPOPPTR;
*(I16*)ptr = (I16)SSPOPINT;
break;
case SAVEt_I8: /* I8 reference */
ptr = SSPOPPTR;
*(I8*)ptr = (I8)SSPOPINT;
break;
case SAVEt_IV: /* IV reference */
ptr = SSPOPPTR;
*(IV*)ptr = (IV)SSPOPIV;
break;
case SAVEt_SPTR: /* SV* reference */
ptr = SSPOPPTR;
*(SV**)ptr = (SV*)SSPOPPTR;
break;
case SAVEt_VPTR: /* random* reference */
case SAVEt_PPTR: /* char* reference */
ptr = SSPOPPTR;
*(char**)ptr = (char*)SSPOPPTR;
break;
case SAVEt_HPTR: /* HV* reference */
ptr = SSPOPPTR;
*(HV**)ptr = (HV*)SSPOPPTR;
break;
case SAVEt_APTR: /* AV* reference */
ptr = SSPOPPTR;
*(AV**)ptr = (AV*)SSPOPPTR;
break;
case SAVEt_NSTAB:
gv = (GV*)SSPOPPTR;
(void)sv_clear((SV*)gv);
break;
case SAVEt_GP: /* scalar reference */
ptr = SSPOPPTR;
gv = (GV*)SSPOPPTR;
if (SvPVX(gv) && SvLEN(gv) > 0) {
Safefree(SvPVX(gv));
}
SvPV_set(gv, (char *)SSPOPPTR);
SvCUR_set(gv, (STRLEN)SSPOPIV);
SvLEN_set(gv, (STRLEN)SSPOPIV);
gp_free(gv);
GvGP(gv) = (GP*)ptr;
if (GvCVu(gv))
PL_sub_generation++; /* putting a method back into circulation */
SvREFCNT_dec(gv);
break;
case SAVEt_FREESV:
ptr = SSPOPPTR;
SvREFCNT_dec((SV*)ptr);
break;
case SAVEt_MORTALIZESV:
ptr = SSPOPPTR;
sv_2mortal((SV*)ptr);
break;
case SAVEt_FREEOP:
ptr = SSPOPPTR;
ASSERT_CURPAD_LEGAL("SAVEt_FREEOP"); /* XXX DAPM tmp */
op_free((OP*)ptr);
break;
case SAVEt_FREEPV:
ptr = SSPOPPTR;
Safefree((char*)ptr);
break;
case SAVEt_CLEARSV:
ptr = (void*)&PL_curpad[SSPOPLONG];
sv = *(SV**)ptr;
DEBUG_Xv(PerlIO_printf(Perl_debug_log,
"Pad 0x%"UVxf"[0x%"UVxf"] clearsv: %ld sv=0x%"UVxf"<%"IVdf"> %s\n",
PTR2UV(PL_comppad), PTR2UV(PL_curpad),
(long)((SV **)ptr-PL_curpad), PTR2UV(sv), (IV)SvREFCNT(sv),
(SvREFCNT(sv) <= 1 && !SvOBJECT(sv)) ? "clear" : "abandon"
));
/* Can clear pad variable in place? */
if (SvREFCNT(sv) <= 1 && !SvOBJECT(sv)) {
/*
* if a my variable that was made readonly is going out of
* scope, we want to remove the readonlyness so that it can
* go out of scope quietly
*/
if (SvPADMY(sv) && !SvFAKE(sv))
SvREADONLY_off(sv);
if (SvTHINKFIRST(sv))
sv_force_normal_flags(sv, SV_IMMEDIATE_UNREF);
if (SvMAGICAL(sv))
mg_free(sv);
switch (SvTYPE(sv)) {
case SVt_NULL:
break;
case SVt_PVAV:
av_clear((AV*)sv);
break;
case SVt_PVHV:
hv_clear((HV*)sv);
break;
case SVt_PVCV:
Perl_croak(aTHX_ "panic: leave_scope pad code");
default:
SvOK_off(sv);
break;
}
SvPADSTALE_on(sv); /* mark as no longer live */
}
else { /* Someone has a claim on this, so abandon it. */
const U32 padflags = SvFLAGS(sv) & (SVs_PADMY|SVs_PADTMP);
switch (SvTYPE(sv)) { /* Console ourselves with a new value */
case SVt_PVAV: *(SV**)ptr = (SV*)newAV(); break;
case SVt_PVHV: *(SV**)ptr = (SV*)newHV(); break;
default: *(SV**)ptr = NEWSV(0,0); break;
}
SvREFCNT_dec(sv); /* Cast current value to the winds. */
/* preserve pad nature, but also mark as not live
* for any closure capturing */
SvFLAGS(*(SV**)ptr) |= padflags | SVs_PADSTALE;
}
break;
case SAVEt_DELETE:
ptr = SSPOPPTR;
hv = (HV*)ptr;
ptr = SSPOPPTR;
(void)hv_delete(hv, (char*)ptr, (U32)SSPOPINT, G_DISCARD);
SvREFCNT_dec(hv);
Safefree(ptr);
break;
case SAVEt_DESTRUCTOR:
ptr = SSPOPPTR;
(*SSPOPDPTR)(ptr);
break;
case SAVEt_DESTRUCTOR_X:
ptr = SSPOPPTR;
(*SSPOPDXPTR)(aTHX_ ptr);
break;
case SAVEt_REGCONTEXT:
case SAVEt_ALLOC:
i = SSPOPINT;
PL_savestack_ix -= i; /* regexp must have croaked */
break;
case SAVEt_STACK_POS: /* Position on Perl stack */
i = SSPOPINT;
PL_stack_sp = PL_stack_base + i;
break;
case SAVEt_AELEM: /* array element */
value = (SV*)SSPOPPTR;
i = SSPOPINT;
av = (AV*)SSPOPPTR;
if (!AvREAL(av) && AvREIFY(av)) /* undo reify guard */
SvREFCNT_dec(value);
ptr = av_fetch(av,i,1);
if (ptr) {
sv = *(SV**)ptr;
if (sv && sv != &PL_sv_undef) {
if (SvTIED_mg((SV*)av, PERL_MAGIC_tied))
(void)SvREFCNT_inc(sv);
goto restore_sv;
}
}
SvREFCNT_dec(av);
SvREFCNT_dec(value);
break;
case SAVEt_HELEM: /* hash element */
value = (SV*)SSPOPPTR;
sv = (SV*)SSPOPPTR;
hv = (HV*)SSPOPPTR;
ptr = hv_fetch_ent(hv, sv, 1, 0);
if (ptr) {
const SV * const oval = HeVAL((HE*)ptr);
if (oval && oval != &PL_sv_undef) {
ptr = &HeVAL((HE*)ptr);
if (SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
(void)SvREFCNT_inc(*(SV**)ptr);
SvREFCNT_dec(sv);
av = (AV*)hv; /* what to refcnt_dec */
goto restore_sv;
}
}
SvREFCNT_dec(hv);
SvREFCNT_dec(sv);
SvREFCNT_dec(value);
break;
case SAVEt_OP:
PL_op = (OP*)SSPOPPTR;
break;
case SAVEt_HINTS:
if ((PL_hints & HINT_LOCALIZE_HH) && GvHV(PL_hintgv)) {
SvREFCNT_dec((SV*)GvHV(PL_hintgv));
GvHV(PL_hintgv) = NULL;
}
*(I32*)&PL_hints = (I32)SSPOPINT;
if (PL_hints & HINT_LOCALIZE_HH) {
SvREFCNT_dec((SV*)GvHV(PL_hintgv));
GvHV(PL_hintgv) = (HV*)SSPOPPTR;
}
break;
case SAVEt_COMPPAD:
PL_comppad = (PAD*)SSPOPPTR;
if (PL_comppad)
PL_curpad = AvARRAY(PL_comppad);
else
PL_curpad = Null(SV**);
break;
case SAVEt_PADSV:
{
const PADOFFSET off = (PADOFFSET)SSPOPLONG;
ptr = SSPOPPTR;
if (ptr)
AvARRAY((PAD*)ptr)[off] = (SV*)SSPOPPTR;
}
break;
case SAVEt_SAVESWITCHSTACK:
{
dSP;
AV* t = (AV*)SSPOPPTR;
AV* f = (AV*)SSPOPPTR;
SWITCHSTACK(t,f);
PL_curstackinfo->si_stack = f;
}
break;
case SAVEt_SET_SVFLAGS:
{
const U32 val = (U32)SSPOPINT;
const U32 mask = (U32)SSPOPINT;
sv = (SV*)SSPOPPTR;
SvFLAGS(sv) &= ~mask;
SvFLAGS(sv) |= val;
}
break;
default:
Perl_croak(aTHX_ "panic: leave_scope inconsistency");
}
}
}
void
Perl_cx_dump(pTHX_ PERL_CONTEXT *cx)
{
#ifdef DEBUGGING
PerlIO_printf(Perl_debug_log, "CX %ld = %s\n", (long)(cx - cxstack), PL_block_type[CxTYPE(cx)]);
if (CxTYPE(cx) != CXt_SUBST) {
PerlIO_printf(Perl_debug_log, "BLK_OLDSP = %ld\n", (long)cx->blk_oldsp);
PerlIO_printf(Perl_debug_log, "BLK_OLDCOP = 0x%"UVxf"\n",
PTR2UV(cx->blk_oldcop));
PerlIO_printf(Perl_debug_log, "BLK_OLDMARKSP = %ld\n", (long)cx->blk_oldmarksp);
PerlIO_printf(Perl_debug_log, "BLK_OLDSCOPESP = %ld\n", (long)cx->blk_oldscopesp);
PerlIO_printf(Perl_debug_log, "BLK_OLDPM = 0x%"UVxf"\n",
PTR2UV(cx->blk_oldpm));
PerlIO_printf(Perl_debug_log, "BLK_GIMME = %s\n", cx->blk_gimme ? "LIST" : "SCALAR");
}
switch (CxTYPE(cx)) {
case CXt_NULL:
case CXt_BLOCK:
break;
case CXt_FORMAT:
PerlIO_printf(Perl_debug_log, "BLK_SUB.CV = 0x%"UVxf"\n",
PTR2UV(cx->blk_sub.cv));
PerlIO_printf(Perl_debug_log, "BLK_SUB.GV = 0x%"UVxf"\n",
PTR2UV(cx->blk_sub.gv));
PerlIO_printf(Perl_debug_log, "BLK_SUB.DFOUTGV = 0x%"UVxf"\n",
PTR2UV(cx->blk_sub.dfoutgv));
PerlIO_printf(Perl_debug_log, "BLK_SUB.HASARGS = %d\n",
(int)cx->blk_sub.hasargs);
PerlIO_printf(Perl_debug_log, "BLK_SUB.RETOP = 0x%"UVxf"\n",
PTR2UV(cx->blk_sub.retop));
break;
case CXt_SUB:
PerlIO_printf(Perl_debug_log, "BLK_SUB.CV = 0x%"UVxf"\n",
PTR2UV(cx->blk_sub.cv));
PerlIO_printf(Perl_debug_log, "BLK_SUB.OLDDEPTH = %ld\n",
(long)cx->blk_sub.olddepth);
PerlIO_printf(Perl_debug_log, "BLK_SUB.HASARGS = %d\n",
(int)cx->blk_sub.hasargs);
PerlIO_printf(Perl_debug_log, "BLK_SUB.LVAL = %d\n",
(int)cx->blk_sub.lval);
PerlIO_printf(Perl_debug_log, "BLK_SUB.RETOP = 0x%"UVxf"\n",
PTR2UV(cx->blk_sub.retop));
break;
case CXt_EVAL:
PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_IN_EVAL = %ld\n",
(long)cx->blk_eval.old_in_eval);
PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_OP_TYPE = %s (%s)\n",
PL_op_name[cx->blk_eval.old_op_type],
PL_op_desc[cx->blk_eval.old_op_type]);
if (cx->blk_eval.old_namesv)
PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_NAME = %s\n",
SvPVX(cx->blk_eval.old_namesv));
PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_EVAL_ROOT = 0x%"UVxf"\n",
PTR2UV(cx->blk_eval.old_eval_root));
PerlIO_printf(Perl_debug_log, "BLK_EVAL.RETOP = 0x%"UVxf"\n",
PTR2UV(cx->blk_eval.retop));
break;
case CXt_LOOP:
PerlIO_printf(Perl_debug_log, "BLK_LOOP.LABEL = %s\n",
cx->blk_loop.label);
PerlIO_printf(Perl_debug_log, "BLK_LOOP.RESETSP = %ld\n",
(long)cx->blk_loop.resetsp);
PerlIO_printf(Perl_debug_log, "BLK_LOOP.REDO_OP = 0x%"UVxf"\n",
PTR2UV(cx->blk_loop.redo_op));
PerlIO_printf(Perl_debug_log, "BLK_LOOP.NEXT_OP = 0x%"UVxf"\n",
PTR2UV(cx->blk_loop.next_op));
PerlIO_printf(Perl_debug_log, "BLK_LOOP.LAST_OP = 0x%"UVxf"\n",
PTR2UV(cx->blk_loop.last_op));
PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERIX = %ld\n",
(long)cx->blk_loop.iterix);
PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERARY = 0x%"UVxf"\n",
PTR2UV(cx->blk_loop.iterary));
PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERVAR = 0x%"UVxf"\n",
PTR2UV(CxITERVAR(cx)));
if (CxITERVAR(cx))
PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERSAVE = 0x%"UVxf"\n",
PTR2UV(cx->blk_loop.itersave));
PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERLVAL = 0x%"UVxf"\n",
PTR2UV(cx->blk_loop.iterlval));
break;
case CXt_SUBST:
PerlIO_printf(Perl_debug_log, "SB_ITERS = %ld\n",
(long)cx->sb_iters);
PerlIO_printf(Perl_debug_log, "SB_MAXITERS = %ld\n",
(long)cx->sb_maxiters);
PerlIO_printf(Perl_debug_log, "SB_RFLAGS = %ld\n",
(long)cx->sb_rflags);
PerlIO_printf(Perl_debug_log, "SB_ONCE = %ld\n",
(long)cx->sb_once);
PerlIO_printf(Perl_debug_log, "SB_ORIG = %s\n",
cx->sb_orig);
PerlIO_printf(Perl_debug_log, "SB_DSTR = 0x%"UVxf"\n",
PTR2UV(cx->sb_dstr));
PerlIO_printf(Perl_debug_log, "SB_TARG = 0x%"UVxf"\n",
PTR2UV(cx->sb_targ));
PerlIO_printf(Perl_debug_log, "SB_S = 0x%"UVxf"\n",
PTR2UV(cx->sb_s));
PerlIO_printf(Perl_debug_log, "SB_M = 0x%"UVxf"\n",
PTR2UV(cx->sb_m));
PerlIO_printf(Perl_debug_log, "SB_STREND = 0x%"UVxf"\n",
PTR2UV(cx->sb_strend));
PerlIO_printf(Perl_debug_log, "SB_RXRES = 0x%"UVxf"\n",
PTR2UV(cx->sb_rxres));
break;
}
#endif /* DEBUGGING */
}
/*
* Local variables:
* c-indentation-style: bsd
* c-basic-offset: 4
* indent-tabs-mode: t
* End:
*
* ex: set ts=8 sts=4 sw=4 noet:
*/
|