Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* scope.c |
79072805 | 2 | * |
1129b882 NC |
3 | * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, |
4 | * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others | |
79072805 LW |
5 | * |
6 | * You may distribute under the terms of either the GNU General Public | |
7 | * License or the Artistic License, as specified in the README file. | |
8 | * | |
a0d0e21e LW |
9 | */ |
10 | ||
11 | /* | |
4ac71550 TC |
12 | * For the fashion of Minas Tirith was such that it was built on seven |
13 | * levels... | |
14 | * | |
15 | * [p.751 of _The Lord of the Rings_, V/i: "Minas Tirith"] | |
79072805 LW |
16 | */ |
17 | ||
ddfa107c | 18 | /* This file contains functions to manipulate several of Perl's stacks; |
166f8a29 DM |
19 | * in particular it contains code to push various types of things onto |
20 | * the savestack, then to pop them off and perform the correct restorative | |
21 | * action for each one. This corresponds to the cleanup Perl does at | |
22 | * each scope exit. | |
23 | */ | |
24 | ||
79072805 | 25 | #include "EXTERN.h" |
864dbfa3 | 26 | #define PERL_IN_SCOPE_C |
79072805 | 27 | #include "perl.h" |
9f601cf3 | 28 | #include "feature.h" |
79072805 | 29 | |
a0d0e21e | 30 | SV** |
fc16c392 | 31 | Perl_stack_grow(pTHX_ SV **sp, SV **p, SSize_t n) |
a0d0e21e | 32 | { |
402d079e DM |
33 | SSize_t extra; |
34 | SSize_t current = (p - PL_stack_base); | |
35 | ||
7918f24d NC |
36 | PERL_ARGS_ASSERT_STACK_GROW; |
37 | ||
402d079e | 38 | if (UNLIKELY(n < 0)) |
6768377c | 39 | Perl_croak(aTHX_ |
147e3846 | 40 | "panic: stack_grow() negative count (%" IVdf ")", (IV)n); |
6768377c | 41 | |
3280af22 | 42 | PL_stack_sp = sp; |
402d079e DM |
43 | extra = |
44 | #ifdef STRESS_REALLOC | |
45 | 1; | |
2ce36478 | 46 | #else |
402d079e | 47 | 128; |
2ce36478 | 48 | #endif |
402d079e DM |
49 | /* If the total might wrap, panic instead. This is really testing |
50 | * that (current + n + extra < SSize_t_MAX), but done in a way that | |
51 | * can't wrap */ | |
52 | if (UNLIKELY( current > SSize_t_MAX - extra | |
53 | || current + extra > SSize_t_MAX - n | |
54 | )) | |
55 | /* diag_listed_as: Out of memory during %s extend */ | |
56 | Perl_croak(aTHX_ "Out of memory during stack extend"); | |
57 | ||
58 | av_extend(PL_curstack, current + n + extra); | |
87058c31 DM |
59 | #ifdef DEBUGGING |
60 | PL_curstackinfo->si_stack_hwm = current + n + extra; | |
61 | #endif | |
62 | ||
3280af22 | 63 | return PL_stack_sp; |
a0d0e21e LW |
64 | } |
65 | ||
2ce36478 SM |
66 | #ifndef STRESS_REALLOC |
67 | #define GROW(old) ((old) * 3 / 2) | |
68 | #else | |
69 | #define GROW(old) ((old) + 1) | |
70 | #endif | |
71 | ||
e336de0d | 72 | PERL_SI * |
864dbfa3 | 73 | Perl_new_stackinfo(pTHX_ I32 stitems, I32 cxitems) |
e336de0d GS |
74 | { |
75 | PERL_SI *si; | |
a02a5408 | 76 | Newx(si, 1, PERL_SI); |
e336de0d GS |
77 | si->si_stack = newAV(); |
78 | AvREAL_off(si->si_stack); | |
79 | av_extend(si->si_stack, stitems > 0 ? stitems-1 : 0); | |
3280af22 | 80 | AvALLOC(si->si_stack)[0] = &PL_sv_undef; |
e336de0d GS |
81 | AvFILLp(si->si_stack) = 0; |
82 | si->si_prev = 0; | |
83 | si->si_next = 0; | |
84 | si->si_cxmax = cxitems - 1; | |
85 | si->si_cxix = -1; | |
5b6f7443 | 86 | si->si_cxsubix = -1; |
e788e7d3 | 87 | si->si_type = PERLSI_UNDEF; |
a02a5408 | 88 | Newx(si->si_cxstack, cxitems, PERL_CONTEXT); |
490576d1 | 89 | /* Without any kind of initialising CX_PUSHSUBST() |
9965345d | 90 | * in pp_subst() will read uninitialised heap. */ |
7e337ee0 | 91 | PoisonNew(si->si_cxstack, cxitems, PERL_CONTEXT); |
e336de0d GS |
92 | return si; |
93 | } | |
94 | ||
79072805 | 95 | I32 |
864dbfa3 | 96 | Perl_cxinc(pTHX) |
79072805 | 97 | { |
a3b680e6 | 98 | const IV old_max = cxstack_max; |
00195859 HS |
99 | const IV new_max = GROW(cxstack_max); |
100 | Renew(cxstack, new_max + 1, PERL_CONTEXT); | |
101 | cxstack_max = new_max; | |
9965345d JH |
102 | /* Without any kind of initialising deep enough recursion |
103 | * will end up reading uninitialised PERL_CONTEXTs. */ | |
00195859 | 104 | PoisonNew(cxstack + old_max + 1, new_max - old_max, PERL_CONTEXT); |
79072805 LW |
105 | return cxstack_ix + 1; |
106 | } | |
107 | ||
79072805 | 108 | void |
864dbfa3 | 109 | Perl_push_scope(pTHX) |
79072805 | 110 | { |
5d9574c1 | 111 | if (UNLIKELY(PL_scopestack_ix == PL_scopestack_max)) { |
00195859 | 112 | const IV new_max = GROW(PL_scopestack_max); |
1604cfb0 | 113 | Renew(PL_scopestack, new_max, I32); |
d343c3ef | 114 | #ifdef DEBUGGING |
1604cfb0 | 115 | Renew(PL_scopestack_name, new_max, const char*); |
520bb150 | 116 | #endif |
1604cfb0 | 117 | PL_scopestack_max = new_max; |
79072805 | 118 | } |
d343c3ef GG |
119 | #ifdef DEBUGGING |
120 | PL_scopestack_name[PL_scopestack_ix] = "unknown"; | |
121 | #endif | |
3280af22 | 122 | PL_scopestack[PL_scopestack_ix++] = PL_savestack_ix; |
79072805 LW |
123 | |
124 | } | |
125 | ||
126 | void | |
864dbfa3 | 127 | Perl_pop_scope(pTHX) |
79072805 | 128 | { |
35a4481c | 129 | const I32 oldsave = PL_scopestack[--PL_scopestack_ix]; |
8990e307 | 130 | LEAVE_SCOPE(oldsave); |
79072805 LW |
131 | } |
132 | ||
ba5248fc | 133 | I32 * |
864dbfa3 | 134 | Perl_markstack_grow(pTHX) |
a0d0e21e | 135 | { |
35a4481c AL |
136 | const I32 oldmax = PL_markstack_max - PL_markstack; |
137 | const I32 newmax = GROW(oldmax); | |
a0d0e21e | 138 | |
3280af22 | 139 | Renew(PL_markstack, newmax, I32); |
3280af22 | 140 | PL_markstack_max = PL_markstack + newmax; |
ba5248fc | 141 | PL_markstack_ptr = PL_markstack + oldmax; |
30773234 | 142 | DEBUG_s(DEBUG_v(PerlIO_printf(Perl_debug_log, |
147e3846 | 143 | "MARK grow %p %" IVdf " by %" IVdf "\n", |
30773234 | 144 | PL_markstack_ptr, (IV)*PL_markstack_ptr, (IV)oldmax))); |
ba5248fc | 145 | return PL_markstack_ptr; |
a0d0e21e LW |
146 | } |
147 | ||
148 | void | |
864dbfa3 | 149 | Perl_savestack_grow(pTHX) |
79072805 | 150 | { |
00195859 | 151 | IV new_max; |
49706441 | 152 | #ifdef STRESS_REALLOC |
00195859 | 153 | new_max = PL_savestack_max + SS_MAXPUSH; |
49706441 | 154 | #else |
00195859 | 155 | new_max = GROW(PL_savestack_max); |
49706441 | 156 | #endif |
3caf0269 DM |
157 | /* Note that we allocate SS_MAXPUSH slots higher than ss_max |
158 | * so that SS_ADD_END(), SSGROW() etc can do a simper check */ | |
00195859 HS |
159 | Renew(PL_savestack, new_max + SS_MAXPUSH, ANY); |
160 | PL_savestack_max = new_max; | |
79072805 LW |
161 | } |
162 | ||
4b3c1a47 AE |
163 | void |
164 | Perl_savestack_grow_cnt(pTHX_ I32 need) | |
165 | { | |
00195859 | 166 | const IV new_max = PL_savestack_ix + need; |
3caf0269 DM |
167 | /* Note that we allocate SS_MAXPUSH slots higher than ss_max |
168 | * so that SS_ADD_END(), SSGROW() etc can do a simper check */ | |
00195859 HS |
169 | Renew(PL_savestack, new_max + SS_MAXPUSH, ANY); |
170 | PL_savestack_max = new_max; | |
4b3c1a47 AE |
171 | } |
172 | ||
2ce36478 SM |
173 | #undef GROW |
174 | ||
a953aca5 DD |
175 | /* The original function was called Perl_tmps_grow and was removed from public |
176 | API, Perl_tmps_grow_p is the replacement and it used in public macros but | |
177 | isn't public itself. | |
178 | ||
179 | Perl_tmps_grow_p takes a proposed ix. A proposed ix is PL_tmps_ix + extend_by, | |
180 | where the result of (PL_tmps_ix + extend_by) is >= PL_tmps_max | |
181 | Upon return, PL_tmps_stack[ix] will be a valid address. For machine code | |
182 | optimization and register usage reasons, the proposed ix passed into | |
183 | tmps_grow is returned to the caller which the caller can then use to write | |
184 | an SV * to PL_tmps_stack[ix]. If the caller was using tmps_grow in | |
185 | pre-extend mode (EXTEND_MORTAL macro), then it ignores the return value of | |
186 | tmps_grow. Note, tmps_grow DOES NOT write ix to PL_tmps_ix, the caller | |
187 | must assign ix or ret val of tmps_grow to PL_temps_ix themselves if that is | |
188 | appropriate. The assignment to PL_temps_ix can happen before or after | |
189 | tmps_grow call since tmps_grow doesn't look at PL_tmps_ix. | |
190 | */ | |
191 | ||
192 | SSize_t | |
193 | Perl_tmps_grow_p(pTHX_ SSize_t ix) | |
677b06e3 | 194 | { |
a953aca5 | 195 | SSize_t extend_to = ix; |
677b06e3 | 196 | #ifndef STRESS_REALLOC |
a953aca5 | 197 | if (ix - PL_tmps_max < 128) |
1604cfb0 | 198 | extend_to += (PL_tmps_max < 512) ? 128 : 512; |
677b06e3 | 199 | #endif |
00195859 | 200 | Renew(PL_tmps_stack, extend_to + 1, SV*); |
a953aca5 | 201 | PL_tmps_max = extend_to + 1; |
a953aca5 | 202 | return ix; |
677b06e3 GS |
203 | } |
204 | ||
205 | ||
206 | void | |
864dbfa3 | 207 | Perl_free_tmps(pTHX) |
79072805 LW |
208 | { |
209 | /* XXX should tmps_floor live in cxstack? */ | |
e8eb279c | 210 | const SSize_t myfloor = PL_tmps_floor; |
3280af22 | 211 | while (PL_tmps_ix > myfloor) { /* clean up after last statement */ |
1604cfb0 | 212 | SV* const sv = PL_tmps_stack[PL_tmps_ix--]; |
72581b5b | 213 | #ifdef PERL_POISON |
1604cfb0 | 214 | PoisonWith(PL_tmps_stack + PL_tmps_ix + 1, 1, SV *, 0xAB); |
22ade07d | 215 | #endif |
1604cfb0 MS |
216 | if (LIKELY(sv)) { |
217 | SvTEMP_off(sv); | |
218 | SvREFCNT_dec_NN(sv); /* note, can modify tmps_ix!!! */ | |
219 | } | |
79072805 LW |
220 | } |
221 | } | |
222 | ||
76e3520e | 223 | STATIC SV * |
af7df257 | 224 | S_save_scalar_at(pTHX_ SV **sptr, const U32 flags) |
79072805 | 225 | { |
75d34a09 | 226 | SV * osv; |
eb578fdb | 227 | SV *sv; |
79072805 | 228 | |
7918f24d NC |
229 | PERL_ARGS_ASSERT_SAVE_SCALAR_AT; |
230 | ||
75d34a09 | 231 | osv = *sptr; |
3c84a230 DM |
232 | if (flags & SAVEf_KEEPOLDELEM) |
233 | sv = osv; | |
234 | else { | |
8fcb2425 | 235 | sv = (*sptr = newSV_type(SVt_NULL)); |
3c84a230 DM |
236 | if (SvTYPE(osv) >= SVt_PVMG && SvMAGIC(osv)) |
237 | mg_localize(osv, sv, cBOOL(flags & SAVEf_SETMAGIC)); | |
79072805 | 238 | } |
75d34a09 | 239 | |
79072805 LW |
240 | return sv; |
241 | } | |
242 | ||
dfcd3de5 NC |
243 | void |
244 | Perl_save_pushptrptr(pTHX_ void *const ptr1, void *const ptr2, const int type) | |
e22024d3 | 245 | { |
a3444cc5 DM |
246 | dSS_ADD; |
247 | SS_ADD_PTR(ptr1); | |
248 | SS_ADD_PTR(ptr2); | |
249 | SS_ADD_UV(type); | |
250 | SS_ADD_END(3); | |
e22024d3 NC |
251 | } |
252 | ||
7a4c00b4 | 253 | SV * |
864dbfa3 | 254 | Perl_save_scalar(pTHX_ GV *gv) |
7a4c00b4 | 255 | { |
13c59d41 | 256 | SV ** const sptr = &GvSVn(gv); |
7918f24d NC |
257 | |
258 | PERL_ARGS_ASSERT_SAVE_SCALAR; | |
259 | ||
13c59d41 | 260 | if (UNLIKELY(SvGMAGICAL(*sptr))) { |
b492a59e | 261 | PL_localizing = 1; |
13c59d41 | 262 | (void)mg_get(*sptr); |
b492a59e DM |
263 | PL_localizing = 0; |
264 | } | |
13c59d41 MH |
265 | save_pushptrptr(SvREFCNT_inc_simple(gv), SvREFCNT_inc(*sptr), SAVEt_SV); |
266 | return save_scalar_at(sptr, SAVEf_SETMAGIC); /* XXX - FIXME - see #60360 */ | |
7a4c00b4 | 267 | } |
268 | ||
f4dd75d9 | 269 | /* Like save_sptr(), but also SvREFCNT_dec()s the new value. Can be used to |
b9d12d37 GS |
270 | * restore a global SV to its prior contents, freeing new value. */ |
271 | void | |
864dbfa3 | 272 | Perl_save_generic_svref(pTHX_ SV **sptr) |
b9d12d37 | 273 | { |
7918f24d NC |
274 | PERL_ARGS_ASSERT_SAVE_GENERIC_SVREF; |
275 | ||
e22024d3 | 276 | save_pushptrptr(sptr, SvREFCNT_inc(*sptr), SAVEt_GENERIC_SVREF); |
b9d12d37 GS |
277 | } |
278 | ||
f4dd75d9 GS |
279 | /* Like save_pptr(), but also Safefree()s the new value if it is different |
280 | * from the old one. Can be used to restore a global char* to its prior | |
281 | * contents, freeing new value. */ | |
282 | void | |
283 | Perl_save_generic_pvref(pTHX_ char **str) | |
284 | { | |
7918f24d NC |
285 | PERL_ARGS_ASSERT_SAVE_GENERIC_PVREF; |
286 | ||
e22024d3 | 287 | save_pushptrptr(*str, str, SAVEt_GENERIC_PVREF); |
f4dd75d9 GS |
288 | } |
289 | ||
05ec9bb3 NIS |
290 | /* Like save_generic_pvref(), but uses PerlMemShared_free() rather than Safefree(). |
291 | * Can be used to restore a shared global char* to its prior | |
292 | * contents, freeing new value. */ | |
293 | void | |
294 | Perl_save_shared_pvref(pTHX_ char **str) | |
295 | { | |
7918f24d NC |
296 | PERL_ARGS_ASSERT_SAVE_SHARED_PVREF; |
297 | ||
e22024d3 | 298 | save_pushptrptr(str, *str, SAVEt_SHARED_PVREF); |
05ec9bb3 NIS |
299 | } |
300 | ||
14f338dc DM |
301 | /* set the SvFLAGS specified by mask to the values in val */ |
302 | ||
303 | void | |
304 | Perl_save_set_svflags(pTHX_ SV* sv, U32 mask, U32 val) | |
305 | { | |
a3444cc5 | 306 | dSS_ADD; |
7918f24d NC |
307 | |
308 | PERL_ARGS_ASSERT_SAVE_SET_SVFLAGS; | |
309 | ||
a3444cc5 DM |
310 | SS_ADD_PTR(sv); |
311 | SS_ADD_INT(mask); | |
312 | SS_ADD_INT(val); | |
313 | SS_ADD_UV(SAVEt_SET_SVFLAGS); | |
314 | SS_ADD_END(4); | |
14f338dc DM |
315 | } |
316 | ||
364bbfa4 | 317 | /* |
d4f04357 | 318 | |
3f620621 | 319 | =for apidoc_section $GV |
d4f04357 | 320 | |
364bbfa4 TC |
321 | =for apidoc save_gp |
322 | ||
323 | Saves the current GP of gv on the save stack to be restored on scope exit. | |
324 | ||
325 | If empty is true, replace the GP with a new GP. | |
326 | ||
327 | If empty is false, mark gv with GVf_INTRO so the next reference | |
328 | assigned is localized, which is how C< local *foo = $someref; > works. | |
329 | ||
330 | =cut | |
331 | */ | |
332 | ||
79072805 | 333 | void |
864dbfa3 | 334 | Perl_save_gp(pTHX_ GV *gv, I32 empty) |
79072805 | 335 | { |
7918f24d NC |
336 | PERL_ARGS_ASSERT_SAVE_GP; |
337 | ||
6881372e FC |
338 | /* XXX For now, we just upgrade any coderef in the stash to a full GV |
339 | during localisation. Maybe at some point we could make localis- | |
340 | ation work without needing the upgrade. (In which case our | |
341 | callers should probably call a different function, not save_gp.) | |
342 | */ | |
343 | if (!isGV(gv)) { | |
344 | assert(isGV_or_RVCV(gv)); | |
345 | (void)CvGV(SvRV((SV *)gv)); /* CvGV does the upgrade */ | |
346 | assert(isGV(gv)); | |
347 | } | |
348 | ||
10507e11 | 349 | save_pushptrptr(SvREFCNT_inc(gv), GvGP(gv), SAVEt_GP); |
79072805 | 350 | |
5f05dabc | 351 | if (empty) { |
1604cfb0 MS |
352 | GP *gp = Perl_newGP(aTHX_ gv); |
353 | HV * const stash = GvSTASH(gv); | |
354 | bool isa_changed = 0; | |
355 | ||
356 | if (stash && HvENAME(stash)) { | |
357 | if (memEQs(GvNAME(gv), GvNAMELEN(gv), "ISA")) | |
358 | isa_changed = TRUE; | |
359 | else if (GvCVu(gv)) | |
360 | /* taking a method out of circulation ("local")*/ | |
3d460042 | 361 | mro_method_changed_in(stash); |
1604cfb0 MS |
362 | } |
363 | if (GvIOp(gv) && (IoFLAGS(GvIOp(gv)) & IOf_ARGV)) { | |
364 | gp->gp_io = newIO(); | |
365 | IoFLAGS(gp->gp_io) |= IOf_ARGV|IOf_START; | |
366 | } | |
367 | GvGP_set(gv,gp); | |
368 | if (isa_changed) mro_isa_changed_in(stash); | |
5f05dabc | 369 | } |
370 | else { | |
1604cfb0 MS |
371 | gp_ref(GvGP(gv)); |
372 | GvINTRO_on(gv); | |
5f05dabc | 373 | } |
79072805 | 374 | } |
79072805 | 375 | |
79072805 | 376 | AV * |
864dbfa3 | 377 | Perl_save_ary(pTHX_ GV *gv) |
79072805 | 378 | { |
901017d6 | 379 | AV * const oav = GvAVn(gv); |
67a38de0 | 380 | AV *av; |
fb73857a | 381 | |
7918f24d NC |
382 | PERL_ARGS_ASSERT_SAVE_ARY; |
383 | ||
5d9574c1 | 384 | if (UNLIKELY(!AvREAL(oav) && AvREIFY(oav))) |
1604cfb0 | 385 | av_reify(oav); |
01433346 | 386 | save_pushptrptr(SvREFCNT_inc_simple_NN(gv), oav, SAVEt_AV); |
79072805 | 387 | |
4608196e | 388 | GvAV(gv) = NULL; |
fb73857a | 389 | av = GvAVn(gv); |
5d9574c1 | 390 | if (UNLIKELY(SvMAGIC(oav))) |
1604cfb0 | 391 | mg_localize(MUTABLE_SV(oav), MUTABLE_SV(av), TRUE); |
fb73857a | 392 | return av; |
79072805 LW |
393 | } |
394 | ||
395 | HV * | |
864dbfa3 | 396 | Perl_save_hash(pTHX_ GV *gv) |
79072805 | 397 | { |
fb73857a | 398 | HV *ohv, *hv; |
399 | ||
7918f24d NC |
400 | PERL_ARGS_ASSERT_SAVE_HASH; |
401 | ||
01433346 | 402 | save_pushptrptr( |
1604cfb0 | 403 | SvREFCNT_inc_simple_NN(gv), (ohv = GvHVn(gv)), SAVEt_HV |
01433346 | 404 | ); |
79072805 | 405 | |
4608196e | 406 | GvHV(gv) = NULL; |
fb73857a | 407 | hv = GvHVn(gv); |
5d9574c1 | 408 | if (UNLIKELY(SvMAGIC(ohv))) |
1604cfb0 | 409 | mg_localize(MUTABLE_SV(ohv), MUTABLE_SV(hv), TRUE); |
fb73857a | 410 | return hv; |
79072805 LW |
411 | } |
412 | ||
413 | void | |
5aaab254 | 414 | Perl_save_item(pTHX_ SV *item) |
79072805 | 415 | { |
eb578fdb | 416 | SV * const sv = newSVsv(item); |
79072805 | 417 | |
7918f24d NC |
418 | PERL_ARGS_ASSERT_SAVE_ITEM; |
419 | ||
e22024d3 | 420 | save_pushptrptr(item, /* remember the pointer */ |
1604cfb0 MS |
421 | sv, /* remember the value */ |
422 | SAVEt_ITEM); | |
79072805 LW |
423 | } |
424 | ||
425 | void | |
1409bc06 | 426 | Perl_save_bool(pTHX_ bool *boolp) |
79072805 | 427 | { |
a3444cc5 | 428 | dSS_ADD; |
7918f24d | 429 | |
1409bc06 | 430 | PERL_ARGS_ASSERT_SAVE_BOOL; |
7918f24d | 431 | |
a3444cc5 DM |
432 | SS_ADD_PTR(boolp); |
433 | SS_ADD_UV(SAVEt_BOOL | (*boolp << 8)); | |
434 | SS_ADD_END(2); | |
1409bc06 NC |
435 | } |
436 | ||
7623d426 NC |
437 | void |
438 | Perl_save_pushi32ptr(pTHX_ const I32 i, void *const ptr, const int type) | |
1409bc06 | 439 | { |
a3444cc5 DM |
440 | dSS_ADD; |
441 | ||
442 | SS_ADD_INT(i); | |
443 | SS_ADD_PTR(ptr); | |
444 | SS_ADD_UV(type); | |
445 | SS_ADD_END(3); | |
79072805 LW |
446 | } |
447 | ||
448 | void | |
1409bc06 | 449 | Perl_save_int(pTHX_ int *intp) |
9febdf04 | 450 | { |
d172696c | 451 | const int i = *intp; |
9b139d09 | 452 | UV type = ((UV)((UV)i << SAVE_TIGHT_SHIFT) | SAVEt_INT_SMALL); |
d172696c DM |
453 | int size = 2; |
454 | dSS_ADD; | |
7918f24d | 455 | |
1409bc06 | 456 | PERL_ARGS_ASSERT_SAVE_INT; |
7918f24d | 457 | |
5d9574c1 | 458 | if (UNLIKELY((int)(type >> SAVE_TIGHT_SHIFT) != i)) { |
d172696c DM |
459 | SS_ADD_INT(i); |
460 | type = SAVEt_INT; | |
461 | size++; | |
462 | } | |
463 | SS_ADD_PTR(intp); | |
464 | SS_ADD_UV(type); | |
465 | SS_ADD_END(size); | |
9febdf04 RH |
466 | } |
467 | ||
468 | void | |
58188858 RGS |
469 | Perl_save_I8(pTHX_ I8 *bytep) |
470 | { | |
a3444cc5 | 471 | dSS_ADD; |
7918f24d NC |
472 | |
473 | PERL_ARGS_ASSERT_SAVE_I8; | |
474 | ||
a3444cc5 DM |
475 | SS_ADD_PTR(bytep); |
476 | SS_ADD_UV(SAVEt_I8 | ((UV)*bytep << 8)); | |
477 | SS_ADD_END(2); | |
58188858 RGS |
478 | } |
479 | ||
480 | void | |
87a84751 JH |
481 | Perl_save_I16(pTHX_ I16 *intp) |
482 | { | |
a3444cc5 | 483 | dSS_ADD; |
7918f24d NC |
484 | |
485 | PERL_ARGS_ASSERT_SAVE_I16; | |
486 | ||
a3444cc5 DM |
487 | SS_ADD_PTR(intp); |
488 | SS_ADD_UV(SAVEt_I16 | ((UV)*intp << 8)); | |
489 | SS_ADD_END(2); | |
87a84751 JH |
490 | } |
491 | ||
492 | void | |
864dbfa3 | 493 | Perl_save_I32(pTHX_ I32 *intp) |
79072805 | 494 | { |
d172696c | 495 | const I32 i = *intp; |
9b139d09 | 496 | UV type = ((I32)((U32)i << SAVE_TIGHT_SHIFT) | SAVEt_I32_SMALL); |
d172696c DM |
497 | int size = 2; |
498 | dSS_ADD; | |
7918f24d NC |
499 | |
500 | PERL_ARGS_ASSERT_SAVE_I32; | |
501 | ||
5d9574c1 | 502 | if (UNLIKELY((I32)(type >> SAVE_TIGHT_SHIFT) != i)) { |
d172696c DM |
503 | SS_ADD_INT(i); |
504 | type = SAVEt_I32; | |
505 | size++; | |
506 | } | |
507 | SS_ADD_PTR(intp); | |
508 | SS_ADD_UV(type); | |
509 | SS_ADD_END(size); | |
79072805 LW |
510 | } |
511 | ||
e8eb279c FC |
512 | void |
513 | Perl_save_strlen(pTHX_ STRLEN *ptr) | |
514 | { | |
8462890b PE |
515 | const IV i = *ptr; |
516 | UV type = ((I32)((U32)i << SAVE_TIGHT_SHIFT) | SAVEt_STRLEN_SMALL); | |
517 | int size = 2; | |
e8eb279c FC |
518 | dSS_ADD; |
519 | ||
520 | PERL_ARGS_ASSERT_SAVE_STRLEN; | |
521 | ||
8462890b PE |
522 | if (UNLIKELY((I32)(type >> SAVE_TIGHT_SHIFT) != i)) { |
523 | SS_ADD_IV(*ptr); | |
524 | type = SAVEt_STRLEN; | |
525 | size++; | |
526 | } | |
527 | ||
e8eb279c | 528 | SS_ADD_PTR(ptr); |
8462890b PE |
529 | SS_ADD_UV(type); |
530 | SS_ADD_END(size); | |
e8eb279c FC |
531 | } |
532 | ||
f8c11501 FC |
533 | void |
534 | Perl_save_iv(pTHX_ IV *ivp) | |
535 | { | |
536 | PERL_ARGS_ASSERT_SAVE_IV; | |
537 | ||
538 | SSCHECK(3); | |
539 | SSPUSHIV(*ivp); | |
540 | SSPUSHPTR(ivp); | |
541 | SSPUSHUV(SAVEt_IV); | |
542 | } | |
543 | ||
85e6fe83 LW |
544 | /* Cannot use save_sptr() to store a char* since the SV** cast will |
545 | * force word-alignment and we'll miss the pointer. | |
546 | */ | |
547 | void | |
864dbfa3 | 548 | Perl_save_pptr(pTHX_ char **pptr) |
85e6fe83 | 549 | { |
7918f24d NC |
550 | PERL_ARGS_ASSERT_SAVE_PPTR; |
551 | ||
e22024d3 | 552 | save_pushptrptr(*pptr, pptr, SAVEt_PPTR); |
85e6fe83 LW |
553 | } |
554 | ||
79072805 | 555 | void |
146174a9 CB |
556 | Perl_save_vptr(pTHX_ void *ptr) |
557 | { | |
7918f24d NC |
558 | PERL_ARGS_ASSERT_SAVE_VPTR; |
559 | ||
e22024d3 | 560 | save_pushptrptr(*(char**)ptr, ptr, SAVEt_VPTR); |
146174a9 CB |
561 | } |
562 | ||
563 | void | |
864dbfa3 | 564 | Perl_save_sptr(pTHX_ SV **sptr) |
79072805 | 565 | { |
7918f24d NC |
566 | PERL_ARGS_ASSERT_SAVE_SPTR; |
567 | ||
e22024d3 | 568 | save_pushptrptr(*sptr, sptr, SAVEt_SPTR); |
79072805 LW |
569 | } |
570 | ||
c3564e5c | 571 | void |
09edbca0 | 572 | Perl_save_padsv_and_mortalize(pTHX_ PADOFFSET off) |
c3564e5c | 573 | { |
a3444cc5 DM |
574 | dSS_ADD; |
575 | ||
f3548bdc | 576 | ASSERT_CURPAD_ACTIVE("save_padsv"); |
a3444cc5 DM |
577 | SS_ADD_PTR(SvREFCNT_inc_simple_NN(PL_curpad[off])); |
578 | SS_ADD_PTR(PL_comppad); | |
03dba561 | 579 | SS_ADD_UV((UV)off); |
a3444cc5 DM |
580 | SS_ADD_UV(SAVEt_PADSV_AND_MORTALIZE); |
581 | SS_ADD_END(4); | |
c3564e5c GS |
582 | } |
583 | ||
79072805 | 584 | void |
864dbfa3 | 585 | Perl_save_hptr(pTHX_ HV **hptr) |
79072805 | 586 | { |
7918f24d NC |
587 | PERL_ARGS_ASSERT_SAVE_HPTR; |
588 | ||
e22024d3 | 589 | save_pushptrptr(*hptr, hptr, SAVEt_HPTR); |
79072805 LW |
590 | } |
591 | ||
592 | void | |
864dbfa3 | 593 | Perl_save_aptr(pTHX_ AV **aptr) |
79072805 | 594 | { |
7918f24d NC |
595 | PERL_ARGS_ASSERT_SAVE_APTR; |
596 | ||
e22024d3 | 597 | save_pushptrptr(*aptr, aptr, SAVEt_APTR); |
79072805 LW |
598 | } |
599 | ||
600 | void | |
2fd8beea | 601 | Perl_save_pushptr(pTHX_ void *const ptr, const int type) |
8990e307 | 602 | { |
a3444cc5 DM |
603 | dSS_ADD; |
604 | SS_ADD_PTR(ptr); | |
605 | SS_ADD_UV(type); | |
606 | SS_ADD_END(2); | |
8990e307 LW |
607 | } |
608 | ||
609 | void | |
864dbfa3 | 610 | Perl_save_clearsv(pTHX_ SV **svp) |
8990e307 | 611 | { |
cdcdfc56 NC |
612 | const UV offset = svp - PL_curpad; |
613 | const UV offset_shifted = offset << SAVE_TIGHT_SHIFT; | |
7918f24d NC |
614 | |
615 | PERL_ARGS_ASSERT_SAVE_CLEARSV; | |
616 | ||
f3548bdc | 617 | ASSERT_CURPAD_ACTIVE("save_clearsv"); |
623e28c6 | 618 | SvPADSTALE_off(*svp); /* mark lexical as active */ |
5d9574c1 | 619 | if (UNLIKELY((offset_shifted >> SAVE_TIGHT_SHIFT) != offset)) { |
1604cfb0 MS |
620 | Perl_croak(aTHX_ "panic: pad offset %" UVuf " out of range (%p-%p)", |
621 | offset, svp, PL_curpad); | |
a3444cc5 | 622 | } |
cdcdfc56 | 623 | |
a3444cc5 DM |
624 | { |
625 | dSS_ADD; | |
626 | SS_ADD_UV(offset_shifted | SAVEt_CLEARSV); | |
627 | SS_ADD_END(1); | |
628 | } | |
8990e307 LW |
629 | } |
630 | ||
631 | void | |
864dbfa3 | 632 | Perl_save_delete(pTHX_ HV *hv, char *key, I32 klen) |
8990e307 | 633 | { |
7918f24d NC |
634 | PERL_ARGS_ASSERT_SAVE_DELETE; |
635 | ||
85a721ca | 636 | save_pushptri32ptr(key, klen, SvREFCNT_inc_simple(hv), SAVEt_DELETE); |
8990e307 LW |
637 | } |
638 | ||
639 | void | |
af097752 VP |
640 | Perl_save_hdelete(pTHX_ HV *hv, SV *keysv) |
641 | { | |
642 | STRLEN len; | |
643 | I32 klen; | |
644 | const char *key; | |
645 | ||
646 | PERL_ARGS_ASSERT_SAVE_HDELETE; | |
647 | ||
648 | key = SvPV_const(keysv, len); | |
649 | klen = SvUTF8(keysv) ? -(I32)len : (I32)len; | |
650 | SvREFCNT_inc_simple_void_NN(hv); | |
651 | save_pushptri32ptr(savepvn(key, len), klen, hv, SAVEt_DELETE); | |
652 | } | |
653 | ||
654 | void | |
c70927a6 | 655 | Perl_save_adelete(pTHX_ AV *av, SSize_t key) |
c68ec7a9 | 656 | { |
c70927a6 | 657 | dSS_ADD; |
c68ec7a9 VP |
658 | |
659 | PERL_ARGS_ASSERT_SAVE_ADELETE; | |
660 | ||
661 | SvREFCNT_inc_void(av); | |
c70927a6 FC |
662 | SS_ADD_UV(key); |
663 | SS_ADD_PTR(av); | |
664 | SS_ADD_IV(SAVEt_ADELETE); | |
665 | SS_ADD_END(3); | |
c68ec7a9 VP |
666 | } |
667 | ||
668 | void | |
12ab1f58 JH |
669 | Perl_save_destructor(pTHX_ DESTRUCTORFUNC_NOCONTEXT_t f, void* p) |
670 | { | |
a3444cc5 | 671 | dSS_ADD; |
7918f24d NC |
672 | PERL_ARGS_ASSERT_SAVE_DESTRUCTOR; |
673 | ||
a3444cc5 DM |
674 | SS_ADD_DPTR(f); |
675 | SS_ADD_PTR(p); | |
676 | SS_ADD_UV(SAVEt_DESTRUCTOR); | |
677 | SS_ADD_END(3); | |
12ab1f58 JH |
678 | } |
679 | ||
680 | void | |
146174a9 CB |
681 | Perl_save_destructor_x(pTHX_ DESTRUCTORFUNC_t f, void* p) |
682 | { | |
a3444cc5 DM |
683 | dSS_ADD; |
684 | ||
685 | SS_ADD_DXPTR(f); | |
686 | SS_ADD_PTR(p); | |
687 | SS_ADD_UV(SAVEt_DESTRUCTOR_X); | |
688 | SS_ADD_END(3); | |
146174a9 CB |
689 | } |
690 | ||
691 | void | |
da8315f8 NC |
692 | Perl_save_hints(pTHX) |
693 | { | |
20439bc7 | 694 | COPHH *save_cophh = cophh_copy(CopHINTHASH_get(&PL_compiling)); |
da8315f8 | 695 | if (PL_hints & HINT_LOCALIZE_HH) { |
1604cfb0 | 696 | HV *oldhh = GvHV(PL_hintgv); |
6c148c86 PE |
697 | { |
698 | dSS_ADD; | |
699 | SS_ADD_INT(PL_hints); | |
700 | SS_ADD_PTR(save_cophh); | |
701 | SS_ADD_PTR(oldhh); | |
78efaf03 | 702 | SS_ADD_UV(SAVEt_HINTS_HH | (PL_prevailing_version << 8)); |
6c148c86 PE |
703 | SS_ADD_END(4); |
704 | } | |
1604cfb0 MS |
705 | GvHV(PL_hintgv) = NULL; /* in case copying dies */ |
706 | GvHV(PL_hintgv) = hv_copy_hints_hv(oldhh); | |
9f601cf3 | 707 | SAVEFEATUREBITS(); |
be84297e | 708 | } else { |
78efaf03 | 709 | save_pushi32ptr(PL_hints, save_cophh, SAVEt_HINTS | (PL_prevailing_version << 8)); |
da8315f8 | 710 | } |
be84297e NC |
711 | } |
712 | ||
713 | static void | |
714 | S_save_pushptri32ptr(pTHX_ void *const ptr1, const I32 i, void *const ptr2, | |
1604cfb0 | 715 | const int type) |
be84297e | 716 | { |
a3444cc5 DM |
717 | dSS_ADD; |
718 | SS_ADD_PTR(ptr1); | |
719 | SS_ADD_INT(i); | |
720 | SS_ADD_PTR(ptr2); | |
721 | SS_ADD_UV(type); | |
722 | SS_ADD_END(4); | |
da8315f8 NC |
723 | } |
724 | ||
725 | void | |
c70927a6 | 726 | Perl_save_aelem_flags(pTHX_ AV *av, SSize_t idx, SV **sptr, |
1604cfb0 | 727 | const U32 flags) |
4e4c362e | 728 | { |
20b7effb | 729 | dSS_ADD; |
bfc4de9f | 730 | SV *sv; |
7918f24d | 731 | |
91d1c79f | 732 | PERL_ARGS_ASSERT_SAVE_AELEM_FLAGS; |
7918f24d | 733 | |
0cbee0a4 | 734 | SvGETMAGIC(*sptr); |
c70927a6 FC |
735 | SS_ADD_PTR(SvREFCNT_inc_simple(av)); |
736 | SS_ADD_IV(idx); | |
737 | SS_ADD_PTR(SvREFCNT_inc(*sptr)); | |
738 | SS_ADD_UV(SAVEt_AELEM); | |
739 | SS_ADD_END(4); | |
1cdc9186 FC |
740 | /* The array needs to hold a reference count on its new element, so it |
741 | must be AvREAL. */ | |
5d9574c1 | 742 | if (UNLIKELY(!AvREAL(av) && AvREIFY(av))) |
1604cfb0 | 743 | av_reify(av); |
91d1c79f | 744 | save_scalar_at(sptr, flags); /* XXX - FIXME - see #60360 */ |
75d34a09 | 745 | if (flags & SAVEf_KEEPOLDELEM) |
1604cfb0 | 746 | return; |
bfc4de9f DM |
747 | sv = *sptr; |
748 | /* If we're localizing a tied array element, this new sv | |
749 | * won't actually be stored in the array - so it won't get | |
750 | * reaped when the localize ends. Ensure it gets reaped by | |
751 | * mortifying it instead. DAPM */ | |
5d9574c1 | 752 | if (UNLIKELY(SvTIED_mg((const SV *)av, PERL_MAGIC_tied))) |
1604cfb0 | 753 | sv_2mortal(sv); |
4e4c362e GS |
754 | } |
755 | ||
756 | void | |
af7df257 | 757 | Perl_save_helem_flags(pTHX_ HV *hv, SV *key, SV **sptr, const U32 flags) |
4e4c362e | 758 | { |
bfc4de9f | 759 | SV *sv; |
7918f24d | 760 | |
af7df257 | 761 | PERL_ARGS_ASSERT_SAVE_HELEM_FLAGS; |
7918f24d | 762 | |
0cbee0a4 | 763 | SvGETMAGIC(*sptr); |
a3444cc5 DM |
764 | { |
765 | dSS_ADD; | |
766 | SS_ADD_PTR(SvREFCNT_inc_simple(hv)); | |
767 | SS_ADD_PTR(newSVsv(key)); | |
768 | SS_ADD_PTR(SvREFCNT_inc(*sptr)); | |
769 | SS_ADD_UV(SAVEt_HELEM); | |
770 | SS_ADD_END(4); | |
771 | } | |
af7df257 | 772 | save_scalar_at(sptr, flags); |
75d34a09 | 773 | if (flags & SAVEf_KEEPOLDELEM) |
1604cfb0 | 774 | return; |
bfc4de9f DM |
775 | sv = *sptr; |
776 | /* If we're localizing a tied hash element, this new sv | |
777 | * won't actually be stored in the hash - so it won't get | |
778 | * reaped when the localize ends. Ensure it gets reaped by | |
779 | * mortifying it instead. DAPM */ | |
5d9574c1 | 780 | if (UNLIKELY(SvTIED_mg((const SV *)hv, PERL_MAGIC_tied))) |
1604cfb0 | 781 | sv_2mortal(sv); |
4e4c362e GS |
782 | } |
783 | ||
2053acbf NC |
784 | SV* |
785 | Perl_save_svref(pTHX_ SV **sptr) | |
786 | { | |
7918f24d NC |
787 | PERL_ARGS_ASSERT_SAVE_SVREF; |
788 | ||
2053acbf | 789 | SvGETMAGIC(*sptr); |
e22024d3 | 790 | save_pushptrptr(sptr, SvREFCNT_inc(*sptr), SAVEt_SVREF); |
af7df257 | 791 | return save_scalar_at(sptr, SAVEf_SETMAGIC); /* XXX - FIXME - see #60360 */ |
2053acbf NC |
792 | } |
793 | ||
2ef9a108 DM |
794 | |
795 | void | |
796 | Perl_savetmps(pTHX) | |
797 | { | |
798 | dSS_ADD; | |
799 | SS_ADD_IV(PL_tmps_floor); | |
800 | PL_tmps_floor = PL_tmps_ix; | |
801 | SS_ADD_UV(SAVEt_TMPSFLOOR); | |
802 | SS_ADD_END(2); | |
803 | } | |
804 | ||
805 | ||
455ece5e | 806 | I32 |
864dbfa3 | 807 | Perl_save_alloc(pTHX_ I32 size, I32 pad) |
455ece5e | 808 | { |
eb578fdb KW |
809 | const I32 start = pad + ((char*)&PL_savestack[PL_savestack_ix] |
810 | - (char*)PL_savestack); | |
1be36ce0 NC |
811 | const UV elems = 1 + ((size + pad - 1) / sizeof(*PL_savestack)); |
812 | const UV elems_shifted = elems << SAVE_TIGHT_SHIFT; | |
455ece5e | 813 | |
5d9574c1 | 814 | if (UNLIKELY((elems_shifted >> SAVE_TIGHT_SHIFT) != elems)) |
1604cfb0 | 815 | Perl_croak(aTHX_ |
147e3846 | 816 | "panic: save_alloc elems %" UVuf " out of range (%" IVdf "-%" IVdf ")", |
1604cfb0 | 817 | elems, (IV)size, (IV)pad); |
1be36ce0 NC |
818 | |
819 | SSGROW(elems + 1); | |
455ece5e AD |
820 | |
821 | PL_savestack_ix += elems; | |
1be36ce0 | 822 | SSPUSHUV(SAVEt_ALLOC | elems_shifted); |
455ece5e AD |
823 | return start; |
824 | } | |
825 | ||
03dba561 | 826 | |
8c036433 | 827 | static const U8 arg_counts[] = { |
9a2fefd6 DM |
828 | 0, /* SAVEt_ALLOC */ |
829 | 0, /* SAVEt_CLEARPADRANGE */ | |
830 | 0, /* SAVEt_CLEARSV */ | |
831 | 0, /* SAVEt_REGCONTEXT */ | |
832 | 1, /* SAVEt_TMPSFLOOR */ | |
833 | 1, /* SAVEt_BOOL */ | |
834 | 1, /* SAVEt_COMPILE_WARNINGS */ | |
835 | 1, /* SAVEt_COMPPAD */ | |
836 | 1, /* SAVEt_FREECOPHH */ | |
837 | 1, /* SAVEt_FREEOP */ | |
838 | 1, /* SAVEt_FREEPV */ | |
839 | 1, /* SAVEt_FREESV */ | |
840 | 1, /* SAVEt_I16 */ | |
841 | 1, /* SAVEt_I32_SMALL */ | |
842 | 1, /* SAVEt_I8 */ | |
843 | 1, /* SAVEt_INT_SMALL */ | |
844 | 1, /* SAVEt_MORTALIZESV */ | |
845 | 1, /* SAVEt_NSTAB */ | |
846 | 1, /* SAVEt_OP */ | |
847 | 1, /* SAVEt_PARSER */ | |
848 | 1, /* SAVEt_STACK_POS */ | |
849 | 1, /* SAVEt_READONLY_OFF */ | |
850 | 1, /* SAVEt_FREEPADNAME */ | |
8462890b | 851 | 1, /* SAVEt_STRLEN_SMALL */ |
9a2fefd6 DM |
852 | 2, /* SAVEt_AV */ |
853 | 2, /* SAVEt_DESTRUCTOR */ | |
854 | 2, /* SAVEt_DESTRUCTOR_X */ | |
855 | 2, /* SAVEt_GENERIC_PVREF */ | |
856 | 2, /* SAVEt_GENERIC_SVREF */ | |
857 | 2, /* SAVEt_GP */ | |
858 | 2, /* SAVEt_GVSV */ | |
859 | 2, /* SAVEt_HINTS */ | |
860 | 2, /* SAVEt_HPTR */ | |
861 | 2, /* SAVEt_HV */ | |
862 | 2, /* SAVEt_I32 */ | |
863 | 2, /* SAVEt_INT */ | |
864 | 2, /* SAVEt_ITEM */ | |
865 | 2, /* SAVEt_IV */ | |
866 | 2, /* SAVEt_LONG */ | |
867 | 2, /* SAVEt_PPTR */ | |
868 | 2, /* SAVEt_SAVESWITCHSTACK */ | |
869 | 2, /* SAVEt_SHARED_PVREF */ | |
870 | 2, /* SAVEt_SPTR */ | |
871 | 2, /* SAVEt_STRLEN */ | |
872 | 2, /* SAVEt_SV */ | |
873 | 2, /* SAVEt_SVREF */ | |
874 | 2, /* SAVEt_VPTR */ | |
875 | 2, /* SAVEt_ADELETE */ | |
876 | 2, /* SAVEt_APTR */ | |
877 | 3, /* SAVEt_HELEM */ | |
878 | 3, /* SAVEt_PADSV_AND_MORTALIZE*/ | |
879 | 3, /* SAVEt_SET_SVFLAGS */ | |
880 | 3, /* SAVEt_GVSLOT */ | |
881 | 3, /* SAVEt_AELEM */ | |
6c148c86 PE |
882 | 3, /* SAVEt_DELETE */ |
883 | 3 /* SAVEt_HINTS_HH */ | |
9a2fefd6 DM |
884 | }; |
885 | ||
03dba561 | 886 | |
462e5cf6 | 887 | void |
864dbfa3 | 888 | Perl_leave_scope(pTHX_ I32 base) |
79072805 | 889 | { |
302c0c93 | 890 | /* Localise the effects of the TAINT_NOT inside the loop. */ |
284167a5 | 891 | bool was = TAINT_get; |
79072805 | 892 | |
5d9574c1 | 893 | if (UNLIKELY(base < -1)) |
1604cfb0 | 894 | Perl_croak(aTHX_ "panic: corrupt saved stack index %ld", (long) base); |
1c98cc53 | 895 | DEBUG_l(Perl_deb(aTHX_ "savestack: releasing items %ld -> %ld\n", |
1604cfb0 | 896 | (long)PL_savestack_ix, (long)base)); |
3280af22 | 897 | while (PL_savestack_ix > base) { |
1604cfb0 MS |
898 | UV uv; |
899 | U8 type; | |
9a2fefd6 | 900 | ANY *ap; /* arg pointer */ |
c9728782 | 901 | ANY a0, a1, a2; /* up to 3 args */ |
03dba561 | 902 | |
1604cfb0 | 903 | TAINT_NOT; |
c6ae7647 | 904 | |
03dba561 | 905 | { |
9a2fefd6 | 906 | U8 argcount; |
03dba561 | 907 | I32 ix = PL_savestack_ix - 1; |
9a2fefd6 DM |
908 | |
909 | ap = &PL_savestack[ix]; | |
910 | uv = ap->any_uv; | |
03dba561 | 911 | type = (U8)uv & SAVE_MASK; |
9a2fefd6 DM |
912 | argcount = arg_counts[type]; |
913 | PL_savestack_ix = ix - argcount; | |
914 | ap -= argcount; | |
03dba561 DM |
915 | } |
916 | ||
1604cfb0 MS |
917 | switch (type) { |
918 | case SAVEt_ITEM: /* normal string */ | |
c9728782 | 919 | a0 = ap[0]; a1 = ap[1]; |
1604cfb0 | 920 | sv_replace(a0.any_sv, a1.any_sv); |
c9728782 | 921 | if (UNLIKELY(SvSMAGICAL(a0.any_sv))) { |
b492a59e | 922 | PL_localizing = 2; |
c9728782 | 923 | mg_set(a0.any_sv); |
b492a59e DM |
924 | PL_localizing = 0; |
925 | } | |
1604cfb0 | 926 | break; |
03dba561 | 927 | |
1604cfb0 MS |
928 | /* This would be a mathom, but Perl_save_svref() calls a static |
929 | function, S_save_scalar_at(), so has to stay in this file. */ | |
930 | case SAVEt_SVREF: /* scalar reference */ | |
c9728782 | 931 | a0 = ap[0]; a1 = ap[1]; |
1604cfb0 MS |
932 | a2.any_svp = a0.any_svp; |
933 | a0.any_sv = NULL; /* what to refcnt_dec */ | |
934 | goto restore_sv; | |
03dba561 | 935 | |
1604cfb0 | 936 | case SAVEt_SV: /* scalar reference */ |
c9728782 | 937 | a0 = ap[0]; a1 = ap[1]; |
1604cfb0 MS |
938 | a2.any_svp = &GvSV(a0.any_gv); |
939 | restore_sv: | |
03dba561 | 940 | { |
75ebea3c | 941 | /* do *a2.any_svp = a1 and free a0 */ |
1604cfb0 MS |
942 | SV * const sv = *a2.any_svp; |
943 | *a2.any_svp = a1.any_sv; | |
944 | SvREFCNT_dec(sv); | |
c9728782 DM |
945 | if (UNLIKELY(SvSMAGICAL(a1.any_sv))) { |
946 | /* mg_set could die, skipping the freeing of a0 and | |
947 | * a1; Ensure that they're always freed in that case */ | |
2baebb98 | 948 | dSS_ADD; |
c9728782 | 949 | SS_ADD_PTR(a1.any_sv); |
2baebb98 | 950 | SS_ADD_UV(SAVEt_FREESV); |
c9728782 | 951 | SS_ADD_PTR(a0.any_sv); |
2baebb98 DM |
952 | SS_ADD_UV(SAVEt_FREESV); |
953 | SS_ADD_END(4); | |
b492a59e | 954 | PL_localizing = 2; |
c9728782 | 955 | mg_set(a1.any_sv); |
b492a59e | 956 | PL_localizing = 0; |
2baebb98 | 957 | break; |
b492a59e | 958 | } |
1604cfb0 MS |
959 | SvREFCNT_dec_NN(a1.any_sv); |
960 | SvREFCNT_dec(a0.any_sv); | |
961 | break; | |
03dba561 | 962 | } |
c9728782 | 963 | |
1604cfb0 | 964 | case SAVEt_GENERIC_PVREF: /* generic pv */ |
c9728782 | 965 | a0 = ap[0]; a1 = ap[1]; |
1604cfb0 MS |
966 | if (*a1.any_pvp != a0.any_pv) { |
967 | Safefree(*a1.any_pvp); | |
968 | *a1.any_pvp = a0.any_pv; | |
969 | } | |
970 | break; | |
c9728782 | 971 | |
1604cfb0 | 972 | case SAVEt_SHARED_PVREF: /* shared pv */ |
c9728782 | 973 | a0 = ap[0]; a1 = ap[1]; |
1604cfb0 | 974 | if (*a0.any_pvp != a1.any_pv) { |
1604cfb0 | 975 | PerlMemShared_free(*a0.any_pvp); |
1604cfb0 MS |
976 | *a0.any_pvp = a1.any_pv; |
977 | } | |
978 | break; | |
c9728782 | 979 | |
1604cfb0 | 980 | case SAVEt_GVSV: /* scalar slot in GV */ |
c9728782 | 981 | a0 = ap[0]; a1 = ap[1]; |
1604cfb0 MS |
982 | a0.any_svp = &GvSV(a0.any_gv); |
983 | goto restore_svp; | |
c9728782 | 984 | |
1604cfb0 | 985 | case SAVEt_GENERIC_SVREF: /* generic sv */ |
c9728782 | 986 | a0 = ap[0]; a1 = ap[1]; |
1604cfb0 | 987 | restore_svp: |
03dba561 | 988 | { |
75ebea3c | 989 | /* do *a0.any_svp = a1 */ |
1604cfb0 MS |
990 | SV * const sv = *a0.any_svp; |
991 | *a0.any_svp = a1.any_sv; | |
992 | SvREFCNT_dec(sv); | |
993 | SvREFCNT_dec(a1.any_sv); | |
994 | break; | |
03dba561 | 995 | } |
c9728782 | 996 | |
1604cfb0 | 997 | case SAVEt_GVSLOT: /* any slot in GV */ |
03dba561 | 998 | { |
9a2fefd6 | 999 | HV * hv; |
c9728782 DM |
1000 | a0 = ap[0]; a1 = ap[1]; a2 = ap[2]; |
1001 | hv = GvSTASH(a0.any_gv); | |
1604cfb0 MS |
1002 | if (hv && HvENAME(hv) && ( |
1003 | (a2.any_sv && SvTYPE(a2.any_sv) == SVt_PVCV) | |
1004 | || (*a1.any_svp && SvTYPE(*a1.any_svp) == SVt_PVCV) | |
1005 | )) | |
1006 | { | |
1007 | if ((char *)a1.any_svp < (char *)GvGP(a0.any_gv) | |
1008 | || (char *)a1.any_svp > (char *)GvGP(a0.any_gv) + sizeof(struct gp) | |
1009 | || GvREFCNT(a0.any_gv) > 2) /* "> 2" to ignore savestack's ref */ | |
1010 | PL_sub_generation++; | |
1011 | else mro_method_changed_in(hv); | |
1012 | } | |
75ebea3c DM |
1013 | a0.any_svp = a1.any_svp; |
1014 | a1.any_sv = a2.any_sv; | |
1604cfb0 | 1015 | goto restore_svp; |
03dba561 | 1016 | } |
c9728782 | 1017 | |
1604cfb0 | 1018 | case SAVEt_AV: /* array reference */ |
c9728782 | 1019 | a0 = ap[0]; a1 = ap[1]; |
1604cfb0 MS |
1020 | SvREFCNT_dec(GvAV(a0.any_gv)); |
1021 | GvAV(a0.any_gv) = a1.any_av; | |
2baebb98 | 1022 | avhv_common: |
c9728782 DM |
1023 | if (UNLIKELY(SvSMAGICAL(a1.any_sv))) { |
1024 | /* mg_set might die, so make sure a0 isn't leaked */ | |
2baebb98 | 1025 | dSS_ADD; |
c9728782 | 1026 | SS_ADD_PTR(a0.any_sv); |
2baebb98 DM |
1027 | SS_ADD_UV(SAVEt_FREESV); |
1028 | SS_ADD_END(2); | |
b492a59e | 1029 | PL_localizing = 2; |
c9728782 | 1030 | mg_set(a1.any_sv); |
b492a59e | 1031 | PL_localizing = 0; |
2baebb98 | 1032 | break; |
b492a59e | 1033 | } |
1604cfb0 MS |
1034 | SvREFCNT_dec_NN(a0.any_sv); |
1035 | break; | |
c9728782 | 1036 | |
1604cfb0 | 1037 | case SAVEt_HV: /* hash reference */ |
c9728782 | 1038 | a0 = ap[0]; a1 = ap[1]; |
1604cfb0 MS |
1039 | SvREFCNT_dec(GvHV(a0.any_gv)); |
1040 | GvHV(a0.any_gv) = a1.any_hv; | |
2baebb98 DM |
1041 | goto avhv_common; |
1042 | ||
1604cfb0 | 1043 | case SAVEt_INT_SMALL: |
c9728782 | 1044 | a0 = ap[0]; |
1604cfb0 MS |
1045 | *(int*)a0.any_ptr = (int)(uv >> SAVE_TIGHT_SHIFT); |
1046 | break; | |
c9728782 | 1047 | |
1604cfb0 | 1048 | case SAVEt_INT: /* int reference */ |
c9728782 | 1049 | a0 = ap[0]; a1 = ap[1]; |
1604cfb0 MS |
1050 | *(int*)a1.any_ptr = (int)a0.any_i32; |
1051 | break; | |
c9728782 | 1052 | |
8462890b | 1053 | case SAVEt_STRLEN_SMALL: |
1604cfb0 MS |
1054 | a0 = ap[0]; |
1055 | *(STRLEN*)a0.any_ptr = (STRLEN)(uv >> SAVE_TIGHT_SHIFT); | |
8462890b PE |
1056 | break; |
1057 | ||
1604cfb0 | 1058 | case SAVEt_STRLEN: /* STRLEN/size_t ref */ |
c9728782 | 1059 | a0 = ap[0]; a1 = ap[1]; |
1604cfb0 MS |
1060 | *(STRLEN*)a1.any_ptr = (STRLEN)a0.any_iv; |
1061 | break; | |
c9728782 | 1062 | |
1604cfb0 | 1063 | case SAVEt_TMPSFLOOR: /* restore PL_tmps_floor */ |
c9728782 | 1064 | a0 = ap[0]; |
1604cfb0 MS |
1065 | PL_tmps_floor = (SSize_t)a0.any_iv; |
1066 | break; | |
c9728782 | 1067 | |
1604cfb0 | 1068 | case SAVEt_BOOL: /* bool reference */ |
c9728782 | 1069 | a0 = ap[0]; |
1604cfb0 | 1070 | *(bool*)a0.any_ptr = cBOOL(uv >> 8); |
9a9b5ec9 DM |
1071 | #ifdef NO_TAINT_SUPPORT |
1072 | PERL_UNUSED_VAR(was); | |
1073 | #else | |
1604cfb0 MS |
1074 | if (UNLIKELY(a0.any_ptr == &(PL_tainted))) { |
1075 | /* If we don't update <was>, to reflect what was saved on the | |
1076 | * stack for PL_tainted, then we will overwrite this attempt to | |
1077 | * restore it when we exit this routine. Note that this won't | |
1078 | * work if this value was saved in a wider-than necessary type, | |
1079 | * such as I32 */ | |
1080 | was = *(bool*)a0.any_ptr; | |
1081 | } | |
284167a5 | 1082 | #endif |
1604cfb0 | 1083 | break; |
c9728782 | 1084 | |
1604cfb0 | 1085 | case SAVEt_I32_SMALL: |
c9728782 | 1086 | a0 = ap[0]; |
1604cfb0 MS |
1087 | *(I32*)a0.any_ptr = (I32)(uv >> SAVE_TIGHT_SHIFT); |
1088 | break; | |
c9728782 | 1089 | |
1604cfb0 | 1090 | case SAVEt_I32: /* I32 reference */ |
c9728782 | 1091 | a0 = ap[0]; a1 = ap[1]; |
3235b7a3 | 1092 | #ifdef PERL_DEBUG_READONLY_OPS |
c9728782 | 1093 | if (*(I32*)a1.any_ptr != a0.any_i32) |
3235b7a3 | 1094 | #endif |
c9728782 | 1095 | *(I32*)a1.any_ptr = a0.any_i32; |
1604cfb0 | 1096 | break; |
c9728782 | 1097 | |
1604cfb0 MS |
1098 | case SAVEt_SPTR: /* SV* reference */ |
1099 | case SAVEt_VPTR: /* random* reference */ | |
1100 | case SAVEt_PPTR: /* char* reference */ | |
1101 | case SAVEt_HPTR: /* HV* reference */ | |
1102 | case SAVEt_APTR: /* AV* reference */ | |
c9728782 | 1103 | a0 = ap[0]; a1 = ap[1]; |
1604cfb0 MS |
1104 | *a1.any_svp= a0.any_sv; |
1105 | break; | |
c9728782 | 1106 | |
1604cfb0 | 1107 | case SAVEt_GP: /* scalar reference */ |
03dba561 DM |
1108 | { |
1109 | HV *hv; | |
1604cfb0 | 1110 | bool had_method; |
9a2fefd6 | 1111 | |
c9728782 | 1112 | a0 = ap[0]; a1 = ap[1]; |
03dba561 | 1113 | /* possibly taking a method out of circulation */ |
1604cfb0 MS |
1114 | had_method = !!GvCVu(a0.any_gv); |
1115 | gp_free(a0.any_gv); | |
1116 | GvGP_set(a0.any_gv, (GP*)a1.any_ptr); | |
1117 | if ((hv=GvSTASH(a0.any_gv)) && HvENAME_get(hv)) { | |
1118 | if (memEQs(GvNAME(a0.any_gv), GvNAMELEN(a0.any_gv), "ISA")) | |
1119 | mro_isa_changed_in(hv); | |
c9728782 | 1120 | else if (had_method || GvCVu(a0.any_gv)) |
03dba561 | 1121 | /* putting a method back into circulation ("local")*/ |
c9728782 | 1122 | gv_method_changed(a0.any_gv); |
1604cfb0 MS |
1123 | } |
1124 | SvREFCNT_dec_NN(a0.any_gv); | |
1125 | break; | |
03dba561 | 1126 | } |
c9728782 | 1127 | |
1604cfb0 | 1128 | case SAVEt_FREESV: |
c9728782 | 1129 | a0 = ap[0]; |
1604cfb0 MS |
1130 | SvREFCNT_dec(a0.any_sv); |
1131 | break; | |
c9728782 | 1132 | |
1604cfb0 | 1133 | case SAVEt_FREEPADNAME: |
c9728782 | 1134 | a0 = ap[0]; |
1604cfb0 MS |
1135 | PadnameREFCNT_dec((PADNAME *)a0.any_ptr); |
1136 | break; | |
c9728782 | 1137 | |
1604cfb0 | 1138 | case SAVEt_FREECOPHH: |
c9728782 | 1139 | a0 = ap[0]; |
1604cfb0 MS |
1140 | cophh_free((COPHH *)a0.any_ptr); |
1141 | break; | |
c9728782 | 1142 | |
1604cfb0 | 1143 | case SAVEt_MORTALIZESV: |
c9728782 | 1144 | a0 = ap[0]; |
1604cfb0 MS |
1145 | sv_2mortal(a0.any_sv); |
1146 | break; | |
c9728782 | 1147 | |
1604cfb0 | 1148 | case SAVEt_FREEOP: |
c9728782 | 1149 | a0 = ap[0]; |
1604cfb0 MS |
1150 | ASSERT_CURPAD_LEGAL("SAVEt_FREEOP"); |
1151 | op_free(a0.any_op); | |
1152 | break; | |
c9728782 | 1153 | |
1604cfb0 | 1154 | case SAVEt_FREEPV: |
c9728782 | 1155 | a0 = ap[0]; |
1604cfb0 MS |
1156 | Safefree(a0.any_ptr); |
1157 | break; | |
4e09461c | 1158 | |
4e09461c | 1159 | case SAVEt_CLEARPADRANGE: |
75ebea3c | 1160 | { |
bdb0ae96 | 1161 | I32 i; |
1604cfb0 | 1162 | SV **svp; |
bdb0ae96 FC |
1163 | i = (I32)((uv >> SAVE_TIGHT_SHIFT) & OPpPADRANGE_COUNTMASK); |
1164 | svp = &PL_curpad[uv >> | |
4e09461c DM |
1165 | (OPpPADRANGE_COUNTSHIFT + SAVE_TIGHT_SHIFT)] + i - 1; |
1166 | goto clearsv; | |
1604cfb0 MS |
1167 | case SAVEt_CLEARSV: |
1168 | svp = &PL_curpad[uv >> SAVE_TIGHT_SHIFT]; | |
4e09461c DM |
1169 | i = 1; |
1170 | clearsv: | |
1171 | for (; i; i--, svp--) { | |
9a2fefd6 | 1172 | SV *sv = *svp; |
528ad060 DM |
1173 | |
1174 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, | |
147e3846 | 1175 | "Pad 0x%" UVxf "[0x%" UVxf "] clearsv: %ld sv=0x%" UVxf "<%" IVdf "> %s\n", |
528ad060 DM |
1176 | PTR2UV(PL_comppad), PTR2UV(PL_curpad), |
1177 | (long)(svp-PL_curpad), PTR2UV(sv), (IV)SvREFCNT(sv), | |
1178 | (SvREFCNT(sv) <= 1 && !SvOBJECT(sv)) ? "clear" : "abandon" | |
1179 | )); | |
1180 | ||
1181 | /* Can clear pad variable in place? */ | |
9af15990 | 1182 | if (SvREFCNT(sv) == 1 && !SvOBJECT(sv)) { |
a07f0bef | 1183 | |
a07f0bef DM |
1184 | /* these flags are the union of all the relevant flags |
1185 | * in the individual conditions within */ | |
1186 | if (UNLIKELY(SvFLAGS(sv) & ( | |
a623f893 | 1187 | SVf_READONLY|SVf_PROTECT /*for SvREADONLY_off*/ |
a07f0bef | 1188 | | (SVs_GMG|SVs_SMG|SVs_RMG) /* SvMAGICAL() */ |
7532eaae | 1189 | | SVf_OOK |
a07f0bef DM |
1190 | | SVf_THINKFIRST))) |
1191 | { | |
7500fc82 DM |
1192 | /* if a my variable that was made readonly is |
1193 | * going out of scope, we want to remove the | |
1194 | * readonlyness so that it can go out of scope | |
1195 | * quietly | |
1196 | */ | |
57c404c9 | 1197 | if (SvREADONLY(sv)) |
7500fc82 DM |
1198 | SvREADONLY_off(sv); |
1199 | ||
7532eaae DM |
1200 | if (SvOOK(sv)) { /* OOK or HvAUX */ |
1201 | if (SvTYPE(sv) == SVt_PVHV) | |
1202 | Perl_hv_kill_backrefs(aTHX_ MUTABLE_HV(sv)); | |
1203 | else | |
1204 | sv_backoff(sv); | |
1205 | } | |
1206 | ||
7500fc82 | 1207 | if (SvMAGICAL(sv)) { |
7532eaae DM |
1208 | /* note that backrefs (either in HvAUX or magic) |
1209 | * must be removed before other magic */ | |
7500fc82 DM |
1210 | sv_unmagic(sv, PERL_MAGIC_backref); |
1211 | if (SvTYPE(sv) != SVt_PVCV) | |
1212 | mg_free(sv); | |
1213 | } | |
1214 | if (SvTHINKFIRST(sv)) | |
1215 | sv_force_normal_flags(sv, SV_IMMEDIATE_UNREF | |
1216 | |SV_COW_DROP_PV); | |
528ad060 | 1217 | |
a07f0bef | 1218 | } |
528ad060 DM |
1219 | switch (SvTYPE(sv)) { |
1220 | case SVt_NULL: | |
1221 | break; | |
1222 | case SVt_PVAV: | |
1223 | av_clear(MUTABLE_AV(sv)); | |
1224 | break; | |
1225 | case SVt_PVHV: | |
1226 | hv_clear(MUTABLE_HV(sv)); | |
1227 | break; | |
1228 | case SVt_PVCV: | |
1229 | { | |
79656330 | 1230 | HEK *hek = CvGvNAME_HEK(sv); |
528ad060 | 1231 | assert(hek); |
6110d17a | 1232 | (void)share_hek_hek(hek); |
528ad060 DM |
1233 | cv_undef((CV *)sv); |
1234 | CvNAME_HEK_set(sv, hek); | |
f3feca7a | 1235 | CvLEXICAL_on(sv); |
528ad060 DM |
1236 | break; |
1237 | } | |
1238 | default: | |
c79d0076 NC |
1239 | /* This looks odd, but these two macros are for use in |
1240 | expressions and finish with a trailing comma, so | |
1241 | adding a ; after them would be wrong. */ | |
1242 | assert_not_ROK(sv) | |
1243 | assert_not_glob(sv) | |
5c85b638 | 1244 | SvFLAGS(sv) &=~ (SVf_OK|SVf_IVisUV|SVf_UTF8); |
528ad060 DM |
1245 | break; |
1246 | } | |
82e85a9c | 1247 | SvPADTMP_off(sv); |
528ad060 DM |
1248 | SvPADSTALE_on(sv); /* mark as no longer live */ |
1249 | } | |
1250 | else { /* Someone has a claim on this, so abandon it. */ | |
528ad060 DM |
1251 | switch (SvTYPE(sv)) { /* Console ourselves with a new value */ |
1252 | case SVt_PVAV: *svp = MUTABLE_SV(newAV()); break; | |
1253 | case SVt_PVHV: *svp = MUTABLE_SV(newHV()); break; | |
1254 | case SVt_PVCV: | |
1255 | { | |
79656330 | 1256 | HEK * const hek = CvGvNAME_HEK(sv); |
9c98a81f | 1257 | |
528ad060 DM |
1258 | /* Create a stub */ |
1259 | *svp = newSV_type(SVt_PVCV); | |
1260 | ||
1261 | /* Share name */ | |
528ad060 | 1262 | CvNAME_HEK_set(*svp, |
9c98a81f | 1263 | share_hek_hek(hek)); |
f3feca7a | 1264 | CvLEXICAL_on(*svp); |
528ad060 DM |
1265 | break; |
1266 | } | |
8fcb2425 | 1267 | default: *svp = newSV_type(SVt_NULL); break; |
528ad060 | 1268 | } |
4a9a56a7 | 1269 | SvREFCNT_dec_NN(sv); /* Cast current value to the winds. */ |
528ad060 DM |
1270 | /* preserve pad nature, but also mark as not live |
1271 | * for any closure capturing */ | |
145bf8ee | 1272 | SvFLAGS(*svp) |= SVs_PADSTALE; |
528ad060 | 1273 | } |
4e09461c | 1274 | } |
1604cfb0 | 1275 | break; |
75ebea3c | 1276 | } |
c9728782 | 1277 | |
1604cfb0 | 1278 | case SAVEt_DELETE: |
c9728782 | 1279 | a0 = ap[0]; a1 = ap[1]; a2 = ap[2]; |
85df897f DM |
1280 | /* hv_delete could die, so free the key and SvREFCNT_dec the |
1281 | * hv by pushing new save actions | |
1282 | */ | |
1283 | /* ap[0] is the key */ | |
1284 | ap[1].any_uv = SAVEt_FREEPV; /* was len */ | |
1285 | /* ap[2] is the hv */ | |
1286 | ap[3].any_uv = SAVEt_FREESV; /* was SAVEt_DELETE */ | |
1287 | PL_savestack_ix += 4; | |
1604cfb0 MS |
1288 | (void)hv_delete(a2.any_hv, a0.any_pv, a1.any_i32, G_DISCARD); |
1289 | break; | |
c9728782 | 1290 | |
1604cfb0 | 1291 | case SAVEt_ADELETE: |
c9728782 | 1292 | a0 = ap[0]; a1 = ap[1]; |
85df897f DM |
1293 | /* av_delete could die, so SvREFCNT_dec the av by pushing a |
1294 | * new save action | |
1295 | */ | |
1296 | ap[0].any_av = a1.any_av; | |
1297 | ap[1].any_uv = SAVEt_FREESV; | |
1298 | PL_savestack_ix += 2; | |
1604cfb0 MS |
1299 | (void)av_delete(a1.any_av, a0.any_iv, G_DISCARD); |
1300 | break; | |
c9728782 | 1301 | |
1604cfb0 | 1302 | case SAVEt_DESTRUCTOR_X: |
c9728782 | 1303 | a0 = ap[0]; a1 = ap[1]; |
1604cfb0 MS |
1304 | (*a0.any_dxptr)(aTHX_ a1.any_ptr); |
1305 | break; | |
c9728782 | 1306 | |
1604cfb0 MS |
1307 | case SAVEt_REGCONTEXT: |
1308 | /* regexp must have croaked */ | |
1309 | case SAVEt_ALLOC: | |
1310 | PL_savestack_ix -= uv >> SAVE_TIGHT_SHIFT; | |
1311 | break; | |
c9728782 | 1312 | |
1604cfb0 | 1313 | case SAVEt_STACK_POS: /* Position on Perl stack */ |
c9728782 | 1314 | a0 = ap[0]; |
1604cfb0 MS |
1315 | PL_stack_sp = PL_stack_base + a0.any_i32; |
1316 | break; | |
c9728782 | 1317 | |
1604cfb0 | 1318 | case SAVEt_AELEM: /* array element */ |
75ebea3c DM |
1319 | { |
1320 | SV **svp; | |
c9728782 | 1321 | a0 = ap[0]; a1 = ap[1]; a2 = ap[2]; |
1604cfb0 MS |
1322 | svp = av_fetch(a0.any_av, a1.any_iv, 1); |
1323 | if (UNLIKELY(!AvREAL(a0.any_av) && AvREIFY(a0.any_av))) /* undo reify guard */ | |
1324 | SvREFCNT_dec(a2.any_sv); | |
1325 | if (LIKELY(svp)) { | |
1326 | SV * const sv = *svp; | |
1327 | if (LIKELY(sv && sv != &PL_sv_undef)) { | |
1328 | if (UNLIKELY(SvTIED_mg((const SV *)a0.any_av, PERL_MAGIC_tied))) | |
1329 | SvREFCNT_inc_void_NN(sv); | |
75ebea3c DM |
1330 | a1.any_sv = a2.any_sv; |
1331 | a2.any_svp = svp; | |
1604cfb0 MS |
1332 | goto restore_sv; |
1333 | } | |
1334 | } | |
1335 | SvREFCNT_dec(a0.any_av); | |
1336 | SvREFCNT_dec(a2.any_sv); | |
1337 | break; | |
75ebea3c | 1338 | } |
c9728782 | 1339 | |
1604cfb0 | 1340 | case SAVEt_HELEM: /* hash element */ |
03dba561 | 1341 | { |
1604cfb0 | 1342 | HE *he; |
9a2fefd6 | 1343 | |
c9728782 | 1344 | a0 = ap[0]; a1 = ap[1]; a2 = ap[2]; |
1604cfb0 MS |
1345 | he = hv_fetch_ent(a0.any_hv, a1.any_sv, 1, 0); |
1346 | SvREFCNT_dec(a1.any_sv); | |
1347 | if (LIKELY(he)) { | |
1348 | const SV * const oval = HeVAL(he); | |
1349 | if (LIKELY(oval && oval != &PL_sv_undef)) { | |
75ebea3c | 1350 | SV **svp = &HeVAL(he); |
1604cfb0 MS |
1351 | if (UNLIKELY(SvTIED_mg((const SV *)a0.any_hv, PERL_MAGIC_tied))) |
1352 | SvREFCNT_inc_void(*svp); | |
75ebea3c DM |
1353 | a1.any_sv = a2.any_sv; |
1354 | a2.any_svp = svp; | |
1604cfb0 MS |
1355 | goto restore_sv; |
1356 | } | |
1357 | } | |
1358 | SvREFCNT_dec(a0.any_hv); | |
1359 | SvREFCNT_dec(a2.any_sv); | |
1360 | break; | |
03dba561 | 1361 | } |
75ebea3c | 1362 | |
1604cfb0 | 1363 | case SAVEt_OP: |
c9728782 | 1364 | a0 = ap[0]; |
1604cfb0 MS |
1365 | PL_op = (OP*)a0.any_ptr; |
1366 | break; | |
c9728782 | 1367 | |
6c148c86 PE |
1368 | case SAVEt_HINTS_HH: |
1369 | a2 = ap[2]; | |
1370 | /* FALLTHROUGH */ | |
1371 | case SAVEt_HINTS: | |
c9728782 | 1372 | a0 = ap[0]; a1 = ap[1]; |
1604cfb0 MS |
1373 | if ((PL_hints & HINT_LOCALIZE_HH)) { |
1374 | while (GvHV(PL_hintgv)) { | |
1375 | HV *hv = GvHV(PL_hintgv); | |
1376 | GvHV(PL_hintgv) = NULL; | |
1377 | SvREFCNT_dec(MUTABLE_SV(hv)); | |
1378 | } | |
1379 | } | |
1380 | cophh_free(CopHINTHASH_get(&PL_compiling)); | |
1381 | CopHINTHASH_set(&PL_compiling, (COPHH*)a1.any_ptr); | |
1382 | *(I32*)&PL_hints = a0.any_i32; | |
78efaf03 | 1383 | PL_prevailing_version = (U16)(uv >> 8); |
1604cfb0 MS |
1384 | if (type == SAVEt_HINTS_HH) { |
1385 | SvREFCNT_dec(MUTABLE_SV(GvHV(PL_hintgv))); | |
6c148c86 | 1386 | GvHV(PL_hintgv) = MUTABLE_HV(a2.any_ptr); |
1604cfb0 MS |
1387 | } |
1388 | if (!GvHV(PL_hintgv)) { | |
1389 | /* Need to add a new one manually, else rv2hv can | |
1390 | add one via GvHVn and it won't have the magic set. */ | |
1391 | HV *const hv = newHV(); | |
1392 | hv_magic(hv, NULL, PERL_MAGIC_hints); | |
1393 | GvHV(PL_hintgv) = hv; | |
1394 | } | |
1395 | assert(GvHV(PL_hintgv)); | |
1396 | break; | |
1397 | ||
1398 | case SAVEt_COMPPAD: | |
c9728782 | 1399 | a0 = ap[0]; |
1604cfb0 MS |
1400 | PL_comppad = (PAD*)a0.any_ptr; |
1401 | if (LIKELY(PL_comppad)) | |
1402 | PL_curpad = AvARRAY(PL_comppad); | |
1403 | else | |
1404 | PL_curpad = NULL; | |
1405 | break; | |
c9728782 | 1406 | |
1604cfb0 MS |
1407 | case SAVEt_PADSV_AND_MORTALIZE: |
1408 | { | |
1409 | SV **svp; | |
9a2fefd6 | 1410 | |
c9728782 | 1411 | a0 = ap[0]; a1 = ap[1]; a2 = ap[2]; |
1604cfb0 MS |
1412 | assert (a1.any_ptr); |
1413 | svp = AvARRAY((PAD*)a1.any_ptr) + (PADOFFSET)a2.any_uv; | |
b405d38b DM |
1414 | /* This mortalizing used to be done by CX_POOPLOOP() via |
1415 | itersave. But as we have all the information here, we | |
1416 | can do it here, save even having to have itersave in | |
1417 | the struct. | |
1418 | */ | |
1604cfb0 MS |
1419 | sv_2mortal(*svp); |
1420 | *svp = a0.any_sv; | |
1421 | } | |
1422 | break; | |
c9728782 | 1423 | |
1604cfb0 MS |
1424 | case SAVEt_SAVESWITCHSTACK: |
1425 | { | |
1426 | dSP; | |
9a2fefd6 | 1427 | |
c9728782 | 1428 | a0 = ap[0]; a1 = ap[1]; |
1604cfb0 MS |
1429 | SWITCHSTACK(a1.any_av, a0.any_av); |
1430 | PL_curstackinfo->si_stack = a0.any_av; | |
1431 | } | |
1432 | break; | |
c9728782 | 1433 | |
1604cfb0 | 1434 | case SAVEt_SET_SVFLAGS: |
c9728782 DM |
1435 | a0 = ap[0]; a1 = ap[1]; a2 = ap[2]; |
1436 | SvFLAGS(a0.any_sv) &= ~(a1.any_u32); | |
1437 | SvFLAGS(a0.any_sv) |= a2.any_u32; | |
1604cfb0 | 1438 | break; |
95e06916 | 1439 | |
1604cfb0 MS |
1440 | /* These are only saved in mathoms.c */ |
1441 | case SAVEt_NSTAB: | |
c9728782 | 1442 | a0 = ap[0]; |
1604cfb0 MS |
1443 | (void)sv_clear(a0.any_sv); |
1444 | break; | |
c9728782 | 1445 | |
1604cfb0 | 1446 | case SAVEt_LONG: /* long reference */ |
c9728782 | 1447 | a0 = ap[0]; a1 = ap[1]; |
1604cfb0 MS |
1448 | *(long*)a1.any_ptr = a0.any_long; |
1449 | break; | |
c9728782 | 1450 | |
1604cfb0 | 1451 | case SAVEt_IV: /* IV reference */ |
c9728782 | 1452 | a0 = ap[0]; a1 = ap[1]; |
1604cfb0 MS |
1453 | *(IV*)a1.any_ptr = a0.any_iv; |
1454 | break; | |
95e06916 | 1455 | |
1604cfb0 | 1456 | case SAVEt_I16: /* I16 reference */ |
c9728782 | 1457 | a0 = ap[0]; |
1604cfb0 MS |
1458 | *(I16*)a0.any_ptr = (I16)(uv >> 8); |
1459 | break; | |
c9728782 | 1460 | |
1604cfb0 | 1461 | case SAVEt_I8: /* I8 reference */ |
c9728782 | 1462 | a0 = ap[0]; |
1604cfb0 MS |
1463 | *(I8*)a0.any_ptr = (I8)(uv >> 8); |
1464 | break; | |
c9728782 | 1465 | |
1604cfb0 | 1466 | case SAVEt_DESTRUCTOR: |
c9728782 | 1467 | a0 = ap[0]; a1 = ap[1]; |
1604cfb0 MS |
1468 | (*a0.any_dptr)(a1.any_ptr); |
1469 | break; | |
c9728782 | 1470 | |
1604cfb0 | 1471 | case SAVEt_COMPILE_WARNINGS: |
c9728782 | 1472 | a0 = ap[0]; |
1943af61 | 1473 | free_and_set_cop_warnings(&PL_compiling, (STRLEN*) a0.any_ptr); |
1604cfb0 | 1474 | break; |
c9728782 | 1475 | |
1604cfb0 | 1476 | case SAVEt_PARSER: |
c9728782 | 1477 | a0 = ap[0]; |
1604cfb0 MS |
1478 | parser_free((yy_parser *)a0.any_ptr); |
1479 | break; | |
c9728782 | 1480 | |
1604cfb0 | 1481 | case SAVEt_READONLY_OFF: |
c9728782 | 1482 | a0 = ap[0]; |
1604cfb0 MS |
1483 | SvREADONLY_off(a0.any_sv); |
1484 | break; | |
c9728782 | 1485 | |
1604cfb0 MS |
1486 | default: |
1487 | Perl_croak(aTHX_ "panic: leave_scope inconsistency %u", | |
75ebea3c | 1488 | (U8)uv & SAVE_MASK); |
1604cfb0 | 1489 | } |
79072805 | 1490 | } |
302c0c93 | 1491 | |
284167a5 | 1492 | TAINT_set(was); |
79072805 | 1493 | } |
8990e307 | 1494 | |
8990e307 | 1495 | void |
864dbfa3 | 1496 | Perl_cx_dump(pTHX_ PERL_CONTEXT *cx) |
8990e307 | 1497 | { |
7918f24d NC |
1498 | PERL_ARGS_ASSERT_CX_DUMP; |
1499 | ||
35ff7856 | 1500 | #ifdef DEBUGGING |
22c35a8c | 1501 | PerlIO_printf(Perl_debug_log, "CX %ld = %s\n", (long)(cx - cxstack), PL_block_type[CxTYPE(cx)]); |
6b35e009 | 1502 | if (CxTYPE(cx) != CXt_SUBST) { |
1604cfb0 MS |
1503 | const char *gimme_text; |
1504 | PerlIO_printf(Perl_debug_log, "BLK_OLDSP = %ld\n", (long)cx->blk_oldsp); | |
1505 | PerlIO_printf(Perl_debug_log, "BLK_OLDCOP = 0x%" UVxf "\n", | |
1506 | PTR2UV(cx->blk_oldcop)); | |
1507 | PerlIO_printf(Perl_debug_log, "BLK_OLDMARKSP = %ld\n", (long)cx->blk_oldmarksp); | |
1508 | PerlIO_printf(Perl_debug_log, "BLK_OLDSCOPESP = %ld\n", (long)cx->blk_oldscopesp); | |
1509 | PerlIO_printf(Perl_debug_log, "BLK_OLDSAVEIX = %ld\n", (long)cx->blk_oldsaveix); | |
1510 | PerlIO_printf(Perl_debug_log, "BLK_OLDPM = 0x%" UVxf "\n", | |
1511 | PTR2UV(cx->blk_oldpm)); | |
1512 | switch (cx->blk_gimme) { | |
1513 | case G_VOID: | |
1514 | gimme_text = "VOID"; | |
1515 | break; | |
1516 | case G_SCALAR: | |
1517 | gimme_text = "SCALAR"; | |
1518 | break; | |
eb7e169e | 1519 | case G_LIST: |
1604cfb0 MS |
1520 | gimme_text = "LIST"; |
1521 | break; | |
1522 | default: | |
1523 | gimme_text = "UNKNOWN"; | |
1524 | break; | |
1525 | } | |
1526 | PerlIO_printf(Perl_debug_log, "BLK_GIMME = %s\n", gimme_text); | |
8990e307 | 1527 | } |
6b35e009 | 1528 | switch (CxTYPE(cx)) { |
8990e307 LW |
1529 | case CXt_NULL: |
1530 | case CXt_BLOCK: | |
f79e2ff9 | 1531 | case CXt_DEFER: |
1604cfb0 | 1532 | break; |
146174a9 | 1533 | case CXt_FORMAT: |
1604cfb0 MS |
1534 | PerlIO_printf(Perl_debug_log, "BLK_FORMAT.CV = 0x%" UVxf "\n", |
1535 | PTR2UV(cx->blk_format.cv)); | |
1536 | PerlIO_printf(Perl_debug_log, "BLK_FORMAT.GV = 0x%" UVxf "\n", | |
1537 | PTR2UV(cx->blk_format.gv)); | |
1538 | PerlIO_printf(Perl_debug_log, "BLK_FORMAT.DFOUTGV = 0x%" UVxf "\n", | |
1539 | PTR2UV(cx->blk_format.dfoutgv)); | |
1540 | PerlIO_printf(Perl_debug_log, "BLK_FORMAT.HASARGS = %d\n", | |
1541 | (int)CxHASARGS(cx)); | |
1542 | PerlIO_printf(Perl_debug_log, "BLK_FORMAT.RETOP = 0x%" UVxf "\n", | |
1543 | PTR2UV(cx->blk_format.retop)); | |
1544 | break; | |
8990e307 | 1545 | case CXt_SUB: |
1604cfb0 MS |
1546 | PerlIO_printf(Perl_debug_log, "BLK_SUB.CV = 0x%" UVxf "\n", |
1547 | PTR2UV(cx->blk_sub.cv)); | |
1548 | PerlIO_printf(Perl_debug_log, "BLK_SUB.OLDDEPTH = %ld\n", | |
1549 | (long)cx->blk_sub.olddepth); | |
1550 | PerlIO_printf(Perl_debug_log, "BLK_SUB.HASARGS = %d\n", | |
1551 | (int)CxHASARGS(cx)); | |
1552 | PerlIO_printf(Perl_debug_log, "BLK_SUB.LVAL = %d\n", (int)CxLVAL(cx)); | |
1553 | PerlIO_printf(Perl_debug_log, "BLK_SUB.RETOP = 0x%" UVxf "\n", | |
1554 | PTR2UV(cx->blk_sub.retop)); | |
1555 | break; | |
8990e307 | 1556 | case CXt_EVAL: |
1604cfb0 MS |
1557 | PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_IN_EVAL = %ld\n", |
1558 | (long)CxOLD_IN_EVAL(cx)); | |
1559 | PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_OP_TYPE = %s (%s)\n", | |
1560 | PL_op_name[CxOLD_OP_TYPE(cx)], | |
1561 | PL_op_desc[CxOLD_OP_TYPE(cx)]); | |
1562 | if (cx->blk_eval.old_namesv) | |
1563 | PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_NAME = %s\n", | |
1564 | SvPVX_const(cx->blk_eval.old_namesv)); | |
1565 | PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_EVAL_ROOT = 0x%" UVxf "\n", | |
1566 | PTR2UV(cx->blk_eval.old_eval_root)); | |
1567 | PerlIO_printf(Perl_debug_log, "BLK_EVAL.RETOP = 0x%" UVxf "\n", | |
1568 | PTR2UV(cx->blk_eval.retop)); | |
1569 | break; | |
8990e307 | 1570 | |
93661e56 | 1571 | case CXt_LOOP_PLAIN: |
c6fdafd0 | 1572 | case CXt_LOOP_LAZYIV: |
d01136d6 | 1573 | case CXt_LOOP_LAZYSV: |
93661e56 DM |
1574 | case CXt_LOOP_LIST: |
1575 | case CXt_LOOP_ARY: | |
1604cfb0 MS |
1576 | PerlIO_printf(Perl_debug_log, "BLK_LOOP.LABEL = %s\n", CxLABEL(cx)); |
1577 | PerlIO_printf(Perl_debug_log, "BLK_LOOP.MY_OP = 0x%" UVxf "\n", | |
1578 | PTR2UV(cx->blk_loop.my_op)); | |
0925dc7f | 1579 | if (CxTYPE(cx) != CXt_LOOP_PLAIN) { |
147e3846 | 1580 | PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERVAR = 0x%" UVxf "\n", |
0925dc7f | 1581 | PTR2UV(CxITERVAR(cx))); |
147e3846 | 1582 | PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERSAVE = 0x%" UVxf "\n", |
0925dc7f | 1583 | PTR2UV(cx->blk_loop.itersave)); |
1604cfb0 MS |
1584 | } |
1585 | if (CxTYPE(cx) == CXt_LOOP_ARY) { | |
147e3846 | 1586 | PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERARY = 0x%" UVxf "\n", |
0925dc7f DM |
1587 | PTR2UV(cx->blk_loop.state_u.ary.ary)); |
1588 | PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERIX = %ld\n", | |
1589 | (long)cx->blk_loop.state_u.ary.ix); | |
1590 | } | |
1604cfb0 | 1591 | break; |
8990e307 LW |
1592 | |
1593 | case CXt_SUBST: | |
1604cfb0 MS |
1594 | PerlIO_printf(Perl_debug_log, "SB_ITERS = %ld\n", |
1595 | (long)cx->sb_iters); | |
1596 | PerlIO_printf(Perl_debug_log, "SB_MAXITERS = %ld\n", | |
1597 | (long)cx->sb_maxiters); | |
1598 | PerlIO_printf(Perl_debug_log, "SB_RFLAGS = %ld\n", | |
1599 | (long)cx->sb_rflags); | |
1600 | PerlIO_printf(Perl_debug_log, "SB_ONCE = %ld\n", | |
1601 | (long)CxONCE(cx)); | |
1602 | PerlIO_printf(Perl_debug_log, "SB_ORIG = %s\n", | |
1603 | cx->sb_orig); | |
1604 | PerlIO_printf(Perl_debug_log, "SB_DSTR = 0x%" UVxf "\n", | |
1605 | PTR2UV(cx->sb_dstr)); | |
1606 | PerlIO_printf(Perl_debug_log, "SB_TARG = 0x%" UVxf "\n", | |
1607 | PTR2UV(cx->sb_targ)); | |
1608 | PerlIO_printf(Perl_debug_log, "SB_S = 0x%" UVxf "\n", | |
1609 | PTR2UV(cx->sb_s)); | |
1610 | PerlIO_printf(Perl_debug_log, "SB_M = 0x%" UVxf "\n", | |
1611 | PTR2UV(cx->sb_m)); | |
1612 | PerlIO_printf(Perl_debug_log, "SB_STREND = 0x%" UVxf "\n", | |
1613 | PTR2UV(cx->sb_strend)); | |
1614 | PerlIO_printf(Perl_debug_log, "SB_RXRES = 0x%" UVxf "\n", | |
1615 | PTR2UV(cx->sb_rxres)); | |
1616 | break; | |
8990e307 | 1617 | } |
65e66c80 | 1618 | #else |
96a5add6 | 1619 | PERL_UNUSED_CONTEXT; |
65e66c80 | 1620 | PERL_UNUSED_ARG(cx); |
17c3b450 | 1621 | #endif /* DEBUGGING */ |
35ff7856 | 1622 | } |
241d1a3b NC |
1623 | |
1624 | /* | |
14d04a33 | 1625 | * ex: set ts=8 sts=4 sw=4 et: |
37442d52 | 1626 | */ |