1 /* scope.h 2 * 3 * Copyright (C) 1993, 1994, 1996, 1997, 1998, 1999, 2000, 2001, 4 * 2002, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others 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 * 9 */ 10 11 #include "scope_types.h" 12 13 #define SAVEf_SETMAGIC 1 14 #define SAVEf_KEEPOLDELEM 2 15 16 #define SAVE_TIGHT_SHIFT 6 17 #define SAVE_MASK 0x3F 18 19 #define save_aelem(av,idx,sptr) save_aelem_flags(av,idx,sptr,SAVEf_SETMAGIC) 20 #define save_helem(hv,key,sptr) save_helem_flags(hv,key,sptr,SAVEf_SETMAGIC) 21 22 #ifndef SCOPE_SAVES_SIGNAL_MASK 23 #define SCOPE_SAVES_SIGNAL_MASK 0 24 #endif 25 26 /* the maximum number of entries that might be pushed using the SS_ADD* 27 * macros */ 28 #define SS_MAXPUSH 4 29 30 #define SSGROW(need) if (UNLIKELY(PL_savestack_ix + (I32)(need) > PL_savestack_max)) savestack_grow_cnt(need) 31 #define SSCHECK(need) SSGROW(need) /* legacy */ 32 #define SSPUSHINT(i) (PL_savestack[PL_savestack_ix++].any_i32 = (I32)(i)) 33 #define SSPUSHLONG(i) (PL_savestack[PL_savestack_ix++].any_long = (long)(i)) 34 #define SSPUSHBOOL(p) (PL_savestack[PL_savestack_ix++].any_bool = (p)) 35 #define SSPUSHIV(i) (PL_savestack[PL_savestack_ix++].any_iv = (IV)(i)) 36 #define SSPUSHUV(u) (PL_savestack[PL_savestack_ix++].any_uv = (UV)(u)) 37 #define SSPUSHPTR(p) (PL_savestack[PL_savestack_ix++].any_ptr = (void*)(p)) 38 #define SSPUSHDPTR(p) (PL_savestack[PL_savestack_ix++].any_dptr = (p)) 39 #define SSPUSHDXPTR(p) (PL_savestack[PL_savestack_ix++].any_dxptr = (p)) 40 41 /* SS_ADD*: newer, faster versions of the above. Don't mix the two sets of 42 * macros. These are fast because they save reduce accesses to the PL_ 43 * vars and move the size check to the end. Doing the check last means 44 * that values in registers will have been pushed and no longer needed, so 45 * don't need saving around the call to grow. Also, tail-call elimination 46 * of the grow() can be done. These changes reduce the code of something 47 * like save_pushptrptr() to half its former size. 48 * Of course, doing the size check *after* pushing means we must always 49 * ensure there are SS_MAXPUSH free slots on the savestack. This is ensured by 50 * savestack_grow_cnt always allocating SS_MAXPUSH slots 51 * more than asked for, or that it sets PL_savestack_max to 52 * 53 * These are for internal core use only and are subject to change */ 54 55 #define dSS_ADD \ 56 I32 ix = PL_savestack_ix; \ 57 ANY *ssp = &PL_savestack[ix] 58 59 #define SS_ADD_END(need) \ 60 assert((need) <= SS_MAXPUSH); \ 61 ix += (need); \ 62 PL_savestack_ix = ix; \ 63 assert(ix <= PL_savestack_max + SS_MAXPUSH); \ 64 if (UNLIKELY(ix > PL_savestack_max)) savestack_grow_cnt(ix - PL_savestack_max); \ 65 assert(PL_savestack_ix <= PL_savestack_max); 66 67 #define SS_ADD_INT(i) ((ssp++)->any_i32 = (I32)(i)) 68 #define SS_ADD_LONG(i) ((ssp++)->any_long = (long)(i)) 69 #define SS_ADD_BOOL(p) ((ssp++)->any_bool = (p)) 70 #define SS_ADD_IV(i) ((ssp++)->any_iv = (IV)(i)) 71 #define SS_ADD_UV(u) ((ssp++)->any_uv = (UV)(u)) 72 #define SS_ADD_PTR(p) ((ssp++)->any_ptr = (void*)(p)) 73 #define SS_ADD_DPTR(p) ((ssp++)->any_dptr = (p)) 74 #define SS_ADD_DXPTR(p) ((ssp++)->any_dxptr = (p)) 75 76 #define SSPOPINT (PL_savestack[--PL_savestack_ix].any_i32) 77 #define SSPOPLONG (PL_savestack[--PL_savestack_ix].any_long) 78 #define SSPOPBOOL (PL_savestack[--PL_savestack_ix].any_bool) 79 #define SSPOPIV (PL_savestack[--PL_savestack_ix].any_iv) 80 #define SSPOPUV (PL_savestack[--PL_savestack_ix].any_uv) 81 #define SSPOPPTR (PL_savestack[--PL_savestack_ix].any_ptr) 82 #define SSPOPDPTR (PL_savestack[--PL_savestack_ix].any_dptr) 83 #define SSPOPDXPTR (PL_savestack[--PL_savestack_ix].any_dxptr) 84 85 86 /* 87 =for apidoc_section $callback 88 89 =for apidoc Amn;||SAVETMPS 90 Opening bracket for temporaries on a callback. See C<L</FREETMPS>> and 91 L<perlcall>. 92 93 =for apidoc Amn;||FREETMPS 94 Closing bracket for temporaries on a callback. See C<L</SAVETMPS>> and 95 L<perlcall>. 96 97 =for apidoc Amn;||ENTER 98 Opening bracket on a callback. See C<L</LEAVE>> and L<perlcall>. 99 100 =for apidoc Amn;||LEAVE 101 Closing bracket on a callback. See C<L</ENTER>> and L<perlcall>. 102 103 =for apidoc Am;||ENTER_with_name|"name" 104 105 Same as C<L</ENTER>>, but when debugging is enabled it also associates the 106 given literal string with the new scope. 107 108 =for apidoc Am;||LEAVE_with_name|"name" 109 110 Same as C<L</LEAVE>>, but when debugging is enabled it first checks that the 111 scope has the given name. C<name> must be a literal string. 112 113 =cut 114 */ 115 116 #define SAVETMPS Perl_savetmps(aTHX) 117 118 #define FREETMPS if (PL_tmps_ix > PL_tmps_floor) free_tmps() 119 120 #ifdef DEBUGGING 121 #define ENTER \ 122 STMT_START { \ 123 push_scope(); \ 124 DEBUG_SCOPE("ENTER") \ 125 } STMT_END 126 #define LEAVE \ 127 STMT_START { \ 128 DEBUG_SCOPE("LEAVE") \ 129 pop_scope(); \ 130 } STMT_END 131 #define ENTER_with_name(name) \ 132 STMT_START { \ 133 push_scope(); \ 134 if (PL_scopestack_name) \ 135 PL_scopestack_name[PL_scopestack_ix-1] = ASSERT_IS_LITERAL(name);\ 136 DEBUG_SCOPE("ENTER \"" name "\"") \ 137 } STMT_END 138 #define LEAVE_with_name(name) \ 139 STMT_START { \ 140 DEBUG_SCOPE("LEAVE \"" name "\"") \ 141 if (PL_scopestack_name) { \ 142 CLANG_DIAG_IGNORE_STMT(-Wstring-compare); \ 143 assert(((char*)PL_scopestack_name[PL_scopestack_ix-1] \ 144 == (char*)ASSERT_IS_LITERAL(name)) \ 145 || strEQ(PL_scopestack_name[PL_scopestack_ix-1], name)); \ 146 CLANG_DIAG_RESTORE_STMT; \ 147 } \ 148 pop_scope(); \ 149 } STMT_END 150 #else 151 #define ENTER push_scope() 152 #define LEAVE pop_scope() 153 #define ENTER_with_name(name) ENTER 154 #define LEAVE_with_name(name) LEAVE 155 #endif 156 #define LEAVE_SCOPE(old) STMT_START { \ 157 if (PL_savestack_ix > old) leave_scope(old); \ 158 } STMT_END 159 160 #define SAVEI8(i) save_I8((I8*)&(i)) 161 #define SAVEI16(i) save_I16((I16*)&(i)) 162 #define SAVEI32(i) save_I32((I32*)&(i)) 163 #define SAVEINT(i) save_int((int*)&(i)) 164 #define SAVEIV(i) save_iv((IV*)&(i)) 165 #define SAVELONG(l) save_long((long*)&(l)) 166 #define SAVESTRLEN(l) Perl_save_strlen(aTHX_ (STRLEN*)&(l)) 167 #define SAVEBOOL(b) save_bool(&(b)) 168 #define SAVESPTR(s) save_sptr((SV**)&(s)) 169 #define SAVEPPTR(s) save_pptr((char**)&(s)) 170 #define SAVEVPTR(s) save_vptr((void*)&(s)) 171 #define SAVEPADSVANDMORTALIZE(s) save_padsv_and_mortalize(s) 172 #define SAVEFREESV(s) save_freesv(MUTABLE_SV(s)) 173 #define SAVEFREEPADNAME(s) save_pushptr((void *)(s), SAVEt_FREEPADNAME) 174 #define SAVEMORTALIZESV(s) save_mortalizesv(MUTABLE_SV(s)) 175 #define SAVEFREEOP(o) save_freeop((OP*)(o)) 176 #define SAVEFREEPV(p) save_freepv((char*)(p)) 177 #define SAVECLEARSV(sv) save_clearsv((SV**)&(sv)) 178 #define SAVEGENERICSV(s) save_generic_svref((SV**)&(s)) 179 #define SAVEGENERICPV(s) save_generic_pvref((char**)&(s)) 180 #define SAVERCPV(s) save_rcpv((char**)&(s)) 181 #define SAVEFREERCPV(s) save_freercpv(s) 182 #define SAVESHAREDPV(s) save_shared_pvref((char**)&(s)) 183 #define SAVESETSVFLAGS(sv,mask,val) save_set_svflags(sv,mask,val) 184 #define SAVEFREECOPHH(h) save_pushptr((void *)(h), SAVEt_FREECOPHH) 185 186 #define SAVEDELETE(h,k,l) \ 187 save_delete(MUTABLE_HV(h), (char*)(k), (I32)(l)) 188 #define SAVEHDELETE(h,s) \ 189 save_hdelete(MUTABLE_HV(h), (s)) 190 #define SAVEADELETE(a,k) \ 191 save_adelete(MUTABLE_AV(a), (SSize_t)(k)) 192 #define SAVEDESTRUCTOR(f,p) \ 193 save_destructor((DESTRUCTORFUNC_NOCONTEXT_t)(f), (void*)(p)) 194 195 #define SAVEDESTRUCTOR_X(f,p) \ 196 save_destructor_x((DESTRUCTORFUNC_t)(f), (void*)(p)) 197 198 #define MORTALSVFUNC_X(f,sv) \ 199 mortal_svfunc_x((SVFUNC_t)(f), sv) 200 201 #define MORTALDESTRUCTOR_SV(coderef,args) \ 202 mortal_destructor_sv(coderef,args) 203 204 #define SAVESTACK_POS() \ 205 STMT_START { \ 206 dSS_ADD; \ 207 SS_ADD_INT(PL_stack_sp - PL_stack_base); \ 208 SS_ADD_UV(SAVEt_STACK_POS); \ 209 SS_ADD_END(2); \ 210 } STMT_END 211 212 #define SAVEOP() save_op() 213 214 #define SAVEHINTS() save_hints() 215 216 #define SAVECOMPPAD() save_pushptr(MUTABLE_SV(PL_comppad), SAVEt_COMPPAD) 217 218 #define SAVESWITCHSTACK(f,t) \ 219 STMT_START { \ 220 save_pushptrptr(MUTABLE_SV(f), MUTABLE_SV(t), SAVEt_SAVESWITCHSTACK); \ 221 SWITCHSTACK((f),(t)); \ 222 PL_curstackinfo->si_stack = (t); \ 223 } STMT_END 224 225 /* Note these are special, we can't just use a save_pushptrptr() on them 226 * as the target might change after a fork or thread start. */ 227 #define SAVECOMPILEWARNINGS() save_pushptr(PL_compiling.cop_warnings, SAVEt_COMPILE_WARNINGS) 228 #define SAVECURCOPWARNINGS() save_pushptr(PL_curcop->cop_warnings, SAVEt_CURCOP_WARNINGS) 229 230 231 #define SAVEPARSER(p) save_pushptr((p), SAVEt_PARSER) 232 233 #ifdef USE_ITHREADS 234 # define SAVECOPSTASH_FREE(c) SAVEIV((c)->cop_stashoff) 235 # define SAVECOPFILE_x(c) SAVEPPTR((c)->cop_file) 236 # define SAVECOPFILE(c) \ 237 STMT_START { \ 238 SAVECOPFILE_x(c); \ 239 CopFILE_debug((c),"SAVECOPFILE",0); \ 240 } STMT_END 241 # define SAVECOPFILE_FREE_x(c) SAVERCPV((c)->cop_file) 242 # define SAVECOPFILE_FREE(c) \ 243 STMT_START { \ 244 SAVECOPFILE_FREE_x(c); \ 245 CopFILE_debug((c),"SAVECOPFILE_FREE",0); \ 246 } STMT_END 247 #else 248 # /* XXX not refcounted */ 249 # define SAVECOPSTASH_FREE(c) SAVESPTR(CopSTASH(c)) 250 # define SAVECOPFILE(c) SAVESPTR(CopFILEGV(c)) 251 # define SAVECOPFILE_FREE(c) SAVEGENERICSV(CopFILEGV(c)) 252 #endif 253 254 #define SAVECOPLINE(c) SAVEI32(CopLINE(c)) 255 256 /* 257 =for apidoc_section $stack 258 =for apidoc Am|SSize_t|SSNEW |Size_t size 259 =for apidoc_item | |SSNEWa |Size_t size|Size_t align 260 =for apidoc_item | |SSNEWat|Size_t size|type|Size_t align 261 =for apidoc_item | |SSNEWt |Size_t size|type 262 263 These each temporarily allocate data on the savestack, returning an SSize_t 264 index into the savestack, because a pointer would get broken if the savestack 265 is moved on reallocation. Use L</C<SSPTR>> to convert the returned index into 266 a pointer. 267 268 The forms differ in that plain C<SSNEW> allocates C<size> bytes; 269 C<SSNEWt> and C<SSNEWat> allocate C<size> objects, each of which is type 270 C<type>; 271 and <SSNEWa> and C<SSNEWat> make sure to align the new data to an C<align> 272 boundary. The most useful value for the alignment is likely to be 273 L</C<MEM_ALIGNBYTES>>. The alignment will be preserved through savestack 274 reallocation B<only> if realloc returns data aligned to a size divisible by 275 "align"! 276 277 =for apidoc Am|type |SSPTR |SSize_t index|type 278 =for apidoc_item|type *|SSPTRt|SSize_t index|type 279 280 These convert the C<index> returned by L/<C<SSNEW>> and kin into actual pointers. 281 282 The difference is that C<SSPTR> casts the result to C<type>, and C<SSPTRt> 283 casts it to a pointer of that C<type>. 284 285 =cut 286 */ 287 288 #define SSNEW(size) Perl_save_alloc(aTHX_ (size), 0) 289 #define SSNEWt(n,t) SSNEW((n)*sizeof(t)) 290 #define SSNEWa(size,align) Perl_save_alloc(aTHX_ (size), \ 291 (I32)(align - ((size_t)((caddr_t)&PL_savestack[PL_savestack_ix]) % align)) % align) 292 #define SSNEWat(n,t,align) SSNEWa((n)*sizeof(t), align) 293 294 #define SSPTR(off,type) (assert(sizeof(off) >= sizeof(SSize_t)), (type) ((char*)PL_savestack + off)) 295 #define SSPTRt(off,type) (assert(sizeof(off) >= sizeof(SSize_t)), (type*) ((char*)PL_savestack + off)) 296 297 #define save_freesv(op) save_pushptr((void *)(op), SAVEt_FREESV) 298 #define save_mortalizesv(op) save_pushptr((void *)(op), SAVEt_MORTALIZESV) 299 300 # define save_freeop(op) \ 301 STMT_START { \ 302 OP * const _o = (OP *)(op); \ 303 assert(!_o->op_savefree); \ 304 _o->op_savefree = 1; \ 305 save_pushptr((void *)(_o), SAVEt_FREEOP); \ 306 } STMT_END 307 #define save_freepv(pv) save_pushptr((void *)(pv), SAVEt_FREEPV) 308 309 /* 310 =for apidoc_section $callback 311 =for apidoc save_op 312 313 Implements C<SAVEOP>. 314 315 =cut 316 */ 317 318 #define save_op() save_pushptr((void *)(PL_op), SAVEt_OP) 319 320 /* 321 * ex: set ts=8 sts=4 sw=4 et: 322 */ 323