1N/A/* scope.h
1N/A *
1N/A * Copyright (C) 1993, 1994, 1996, 1997, 1998, 1999,
1N/A * 2000, 2001, 2002, 2004, by Larry Wall and others
1N/A *
1N/A * You may distribute under the terms of either the GNU General Public
1N/A * License or the Artistic License, as specified in the README file.
1N/A *
1N/A */
1N/A
1N/A#define SAVEt_ITEM 0
1N/A#define SAVEt_SV 1
1N/A#define SAVEt_AV 2
1N/A#define SAVEt_HV 3
1N/A#define SAVEt_INT 4
1N/A#define SAVEt_LONG 5
1N/A#define SAVEt_I32 6
1N/A#define SAVEt_IV 7
1N/A#define SAVEt_SPTR 8
1N/A#define SAVEt_APTR 9
1N/A#define SAVEt_HPTR 10
1N/A#define SAVEt_PPTR 11
1N/A#define SAVEt_NSTAB 12
1N/A#define SAVEt_SVREF 13
1N/A#define SAVEt_GP 14
1N/A#define SAVEt_FREESV 15
1N/A#define SAVEt_FREEOP 16
1N/A#define SAVEt_FREEPV 17
1N/A#define SAVEt_CLEARSV 18
1N/A#define SAVEt_DELETE 19
1N/A#define SAVEt_DESTRUCTOR 20
1N/A#define SAVEt_REGCONTEXT 21
1N/A#define SAVEt_STACK_POS 22
1N/A#define SAVEt_I16 23
1N/A#define SAVEt_AELEM 24
1N/A#define SAVEt_HELEM 25
1N/A#define SAVEt_OP 26
1N/A#define SAVEt_HINTS 27
1N/A#define SAVEt_ALLOC 28
1N/A#define SAVEt_GENERIC_SVREF 29
1N/A#define SAVEt_DESTRUCTOR_X 30
1N/A#define SAVEt_VPTR 31
1N/A#define SAVEt_I8 32
1N/A#define SAVEt_COMPPAD 33
1N/A#define SAVEt_GENERIC_PVREF 34
1N/A#define SAVEt_PADSV 35
1N/A#define SAVEt_MORTALIZESV 36
1N/A#define SAVEt_SHARED_PVREF 37
1N/A#define SAVEt_BOOL 38
1N/A
1N/A#ifndef SCOPE_SAVES_SIGNAL_MASK
1N/A#define SCOPE_SAVES_SIGNAL_MASK 0
1N/A#endif
1N/A
1N/A#define SSCHECK(need) if (PL_savestack_ix + (need) > PL_savestack_max) savestack_grow()
1N/A#define SSGROW(need) if (PL_savestack_ix + (need) > PL_savestack_max) savestack_grow_cnt(need)
1N/A#define SSPUSHINT(i) (PL_savestack[PL_savestack_ix++].any_i32 = (I32)(i))
1N/A#define SSPUSHLONG(i) (PL_savestack[PL_savestack_ix++].any_long = (long)(i))
1N/A#define SSPUSHBOOL(p) (PL_savestack[PL_savestack_ix++].any_bool = (p))
1N/A#define SSPUSHIV(i) (PL_savestack[PL_savestack_ix++].any_iv = (IV)(i))
1N/A#define SSPUSHPTR(p) (PL_savestack[PL_savestack_ix++].any_ptr = (void*)(p))
1N/A#define SSPUSHDPTR(p) (PL_savestack[PL_savestack_ix++].any_dptr = (p))
1N/A#define SSPUSHDXPTR(p) (PL_savestack[PL_savestack_ix++].any_dxptr = (p))
1N/A#define SSPOPINT (PL_savestack[--PL_savestack_ix].any_i32)
1N/A#define SSPOPLONG (PL_savestack[--PL_savestack_ix].any_long)
1N/A#define SSPOPBOOL (PL_savestack[--PL_savestack_ix].any_bool)
1N/A#define SSPOPIV (PL_savestack[--PL_savestack_ix].any_iv)
1N/A#define SSPOPPTR (PL_savestack[--PL_savestack_ix].any_ptr)
1N/A#define SSPOPDPTR (PL_savestack[--PL_savestack_ix].any_dptr)
1N/A#define SSPOPDXPTR (PL_savestack[--PL_savestack_ix].any_dxptr)
1N/A
1N/A/*
1N/A=head1 Callback Functions
1N/A
1N/A=for apidoc Ams||SAVETMPS
1N/AOpening bracket for temporaries on a callback. See C<FREETMPS> and
1N/AL<perlcall>.
1N/A
1N/A=for apidoc Ams||FREETMPS
1N/AClosing bracket for temporaries on a callback. See C<SAVETMPS> and
1N/AL<perlcall>.
1N/A
1N/A=for apidoc Ams||ENTER
1N/AOpening bracket on a callback. See C<LEAVE> and L<perlcall>.
1N/A
1N/A=for apidoc Ams||LEAVE
1N/AClosing bracket on a callback. See C<ENTER> and L<perlcall>.
1N/A
1N/A=cut
1N/A*/
1N/A
1N/A#define SAVETMPS save_int((int*)&PL_tmps_floor), PL_tmps_floor = PL_tmps_ix
1N/A#define FREETMPS if (PL_tmps_ix > PL_tmps_floor) free_tmps()
1N/A
1N/A#ifdef DEBUGGING
1N/A#define ENTER \
1N/A STMT_START { \
1N/A push_scope(); \
1N/A DEBUG_SCOPE("ENTER") \
1N/A } STMT_END
1N/A#define LEAVE \
1N/A STMT_START { \
1N/A DEBUG_SCOPE("LEAVE") \
1N/A pop_scope(); \
1N/A } STMT_END
1N/A#else
1N/A#define ENTER push_scope()
1N/A#define LEAVE pop_scope()
1N/A#endif
1N/A#define LEAVE_SCOPE(old) if (PL_savestack_ix > old) leave_scope(old)
1N/A
1N/A/*
1N/A * Not using SOFT_CAST on SAVESPTR, SAVEGENERICSV and SAVEFREESV
1N/A * because these are used for several kinds of pointer values
1N/A */
1N/A#define SAVEI8(i) save_I8(SOFT_CAST(I8*)&(i))
1N/A#define SAVEI16(i) save_I16(SOFT_CAST(I16*)&(i))
1N/A#define SAVEI32(i) save_I32(SOFT_CAST(I32*)&(i))
1N/A#define SAVEINT(i) save_int(SOFT_CAST(int*)&(i))
1N/A#define SAVEIV(i) save_iv(SOFT_CAST(IV*)&(i))
1N/A#define SAVELONG(l) save_long(SOFT_CAST(long*)&(l))
1N/A#define SAVEBOOL(b) save_bool(SOFT_CAST(bool*)&(b))
1N/A#define SAVESPTR(s) save_sptr((SV**)&(s))
1N/A#define SAVEPPTR(s) save_pptr(SOFT_CAST(char**)&(s))
1N/A#define SAVEVPTR(s) save_vptr((void*)&(s))
1N/A#define SAVEPADSV(s) save_padsv(s)
1N/A#define SAVEFREESV(s) save_freesv((SV*)(s))
1N/A#define SAVEMORTALIZESV(s) save_mortalizesv((SV*)(s))
1N/A#define SAVEFREEOP(o) save_freeop(SOFT_CAST(OP*)(o))
1N/A#define SAVEFREEPV(p) save_freepv(SOFT_CAST(char*)(p))
1N/A#define SAVECLEARSV(sv) save_clearsv(SOFT_CAST(SV**)&(sv))
1N/A#define SAVEGENERICSV(s) save_generic_svref((SV**)&(s))
1N/A#define SAVEGENERICPV(s) save_generic_pvref((char**)&(s))
1N/A#define SAVESHAREDPV(s) save_shared_pvref((char**)&(s))
1N/A#define SAVEDELETE(h,k,l) \
1N/A save_delete(SOFT_CAST(HV*)(h), SOFT_CAST(char*)(k), (I32)(l))
1N/A#define SAVEDESTRUCTOR(f,p) \
1N/A save_destructor((DESTRUCTORFUNC_NOCONTEXT_t)(f), SOFT_CAST(void*)(p))
1N/A
1N/A#define SAVEDESTRUCTOR_X(f,p) \
1N/A save_destructor_x((DESTRUCTORFUNC_t)(f), SOFT_CAST(void*)(p))
1N/A
1N/A#define SAVESTACK_POS() \
1N/A STMT_START { \
1N/A SSCHECK(2); \
1N/A SSPUSHINT(PL_stack_sp - PL_stack_base); \
1N/A SSPUSHINT(SAVEt_STACK_POS); \
1N/A } STMT_END
1N/A
1N/A#define SAVEOP() save_op()
1N/A
1N/A#define SAVEHINTS() \
1N/A STMT_START { \
1N/A SSCHECK(3); \
1N/A if (PL_hints & HINT_LOCALIZE_HH) { \
1N/A SSPUSHPTR(GvHV(PL_hintgv)); \
1N/A GvHV(PL_hintgv) = newHVhv(GvHV(PL_hintgv)); \
1N/A } \
1N/A SSPUSHINT(PL_hints); \
1N/A SSPUSHINT(SAVEt_HINTS); \
1N/A } STMT_END
1N/A
1N/A#define SAVECOMPPAD() \
1N/A STMT_START { \
1N/A SSCHECK(2); \
1N/A SSPUSHPTR((SV*)PL_comppad); \
1N/A SSPUSHINT(SAVEt_COMPPAD); \
1N/A } STMT_END
1N/A
1N/A#ifdef USE_ITHREADS
1N/A# define SAVECOPSTASH(c) SAVEPPTR(CopSTASHPV(c))
1N/A# define SAVECOPSTASH_FREE(c) SAVESHAREDPV(CopSTASHPV(c))
1N/A# define SAVECOPFILE(c) SAVEPPTR(CopFILE(c))
1N/A# define SAVECOPFILE_FREE(c) SAVESHAREDPV(CopFILE(c))
1N/A#else
1N/A# define SAVECOPSTASH(c) SAVESPTR(CopSTASH(c))
1N/A# define SAVECOPSTASH_FREE(c) SAVECOPSTASH(c) /* XXX not refcounted */
1N/A# define SAVECOPFILE(c) SAVESPTR(CopFILEGV(c))
1N/A# define SAVECOPFILE_FREE(c) SAVEGENERICSV(CopFILEGV(c))
1N/A#endif
1N/A
1N/A#define SAVECOPLINE(c) SAVEI32(CopLINE(c))
1N/A
1N/A/* SSNEW() temporarily allocates a specified number of bytes of data on the
1N/A * savestack. It returns an integer index into the savestack, because a
1N/A * pointer would get broken if the savestack is moved on reallocation.
1N/A * SSNEWa() works like SSNEW(), but also aligns the data to the specified
1N/A * number of bytes. MEM_ALIGNBYTES is perhaps the most useful. The
1N/A * alignment will be preserved therough savestack reallocation *only* if
1N/A * realloc returns data aligned to a size divisible by `align'!
1N/A *
1N/A * SSPTR() converts the index returned by SSNEW/SSNEWa() into a pointer.
1N/A */
1N/A
1N/A#define SSNEW(size) Perl_save_alloc(aTHX_ (size), 0)
1N/A#define SSNEWt(n,t) SSNEW((n)*sizeof(t))
1N/A#define SSNEWa(size,align) Perl_save_alloc(aTHX_ (size), \
1N/A (align - ((int)((caddr_t)&PL_savestack[PL_savestack_ix]) % align)) % align)
1N/A#define SSNEWat(n,t,align) SSNEWa((n)*sizeof(t), align)
1N/A
1N/A#define SSPTR(off,type) ((type) ((char*)PL_savestack + off))
1N/A#define SSPTRt(off,type) ((type*) ((char*)PL_savestack + off))
1N/A
1N/A/* A jmpenv packages the state required to perform a proper non-local jump.
1N/A * Note that there is a start_env initialized when perl starts, and top_env
1N/A * points to this initially, so top_env should always be non-null.
1N/A *
1N/A * Existence of a non-null top_env->je_prev implies it is valid to call
1N/A * longjmp() at that runlevel (we make sure start_env.je_prev is always
1N/A * null to ensure this).
1N/A *
1N/A * je_mustcatch, when set at any runlevel to TRUE, means eval ops must
1N/A * establish a local jmpenv to handle exception traps. Care must be taken
1N/A * to restore the previous value of je_mustcatch before exiting the
1N/A * stack frame iff JMPENV_PUSH was not called in that stack frame.
1N/A * GSAR 97-03-27
1N/A */
1N/A
1N/Astruct jmpenv {
1N/A struct jmpenv * je_prev;
1N/A Sigjmp_buf je_buf; /* only for use if !je_throw */
1N/A int je_ret; /* last exception thrown */
1N/A bool je_mustcatch; /* need to call longjmp()? */
1N/A#ifdef PERL_FLEXIBLE_EXCEPTIONS
1N/A void (*je_throw)(int v); /* last for bincompat */
1N/A bool je_noset; /* no need for setjmp() */
1N/A#endif
1N/A};
1N/A
1N/Atypedef struct jmpenv JMPENV;
1N/A
1N/A#ifdef OP_IN_REGISTER
1N/A#define OP_REG_TO_MEM PL_opsave = op
1N/A#define OP_MEM_TO_REG op = PL_opsave
1N/A#else
1N/A#define OP_REG_TO_MEM NOOP
1N/A#define OP_MEM_TO_REG NOOP
1N/A#endif
1N/A
1N/A/*
1N/A * How to build the first jmpenv.
1N/A *
1N/A * top_env needs to be non-zero. It points to an area
1N/A * in which longjmp() stuff is stored, as C callstack
1N/A * info there at least is thread specific this has to
1N/A * be per-thread. Otherwise a 'die' in a thread gives
1N/A * that thread the C stack of last thread to do an eval {}!
1N/A */
1N/A
1N/A#define JMPENV_BOOTSTRAP \
1N/A STMT_START { \
1N/A Zero(&PL_start_env, 1, JMPENV); \
1N/A PL_start_env.je_ret = -1; \
1N/A PL_start_env.je_mustcatch = TRUE; \
1N/A PL_top_env = &PL_start_env; \
1N/A } STMT_END
1N/A
1N/A#ifdef PERL_FLEXIBLE_EXCEPTIONS
1N/A
1N/A/*
1N/A * These exception-handling macros are split up to
1N/A * ease integration with C++ exceptions.
1N/A *
1N/A * To use C++ try+catch to catch Perl exceptions, an extension author
1N/A * needs to first write an extern "C" function to throw an appropriate
1N/A * exception object; typically it will be or contain an integer,
1N/A * because Perl's internals use integers to track exception types:
1N/A * extern "C" { static void thrower(int i) { throw i; } }
1N/A *
1N/A * Then (as shown below) the author needs to use, not the simple
1N/A * JMPENV_PUSH, but several of its constitutent macros, to arrange for
1N/A * the Perl internals to call thrower() rather than longjmp() to
1N/A * report exceptions:
1N/A *
1N/A * dJMPENV;
1N/A * JMPENV_PUSH_INIT(thrower);
1N/A * try {
1N/A * ... stuff that may throw exceptions ...
1N/A * }
1N/A * catch (int why) { // or whatever matches thrower()
1N/A * JMPENV_POST_CATCH;
1N/A * EXCEPT_SET(why);
1N/A * switch (why) {
1N/A * ... // handle various Perl exception codes
1N/A * }
1N/A * }
1N/A * JMPENV_POP; // don't forget this!
1N/A */
1N/A
1N/A/*
1N/A * Function that catches/throws, and its callback for the
1N/A * body of protected processing.
1N/A */
1N/Atypedef void *(CPERLscope(*protect_body_t)) (pTHX_ va_list);
1N/Atypedef void *(CPERLscope(*protect_proc_t)) (pTHX_ volatile JMPENV *pcur_env,
1N/A int *, protect_body_t, ...);
1N/A
1N/A#define dJMPENV JMPENV cur_env; \
1N/A volatile JMPENV *pcur_env = ((cur_env.je_noset = 0),&cur_env)
1N/A
1N/A#define JMPENV_PUSH_INIT_ENV(ce,THROWFUNC) \
1N/A STMT_START { \
1N/A (ce).je_throw = (THROWFUNC); \
1N/A (ce).je_ret = -1; \
1N/A (ce).je_mustcatch = FALSE; \
1N/A (ce).je_prev = PL_top_env; \
1N/A PL_top_env = &(ce); \
1N/A OP_REG_TO_MEM; \
1N/A } STMT_END
1N/A
1N/A#define JMPENV_PUSH_INIT(THROWFUNC) JMPENV_PUSH_INIT_ENV(*(JMPENV*)pcur_env,THROWFUNC)
1N/A
1N/A#define JMPENV_POST_CATCH_ENV(ce) \
1N/A STMT_START { \
1N/A OP_MEM_TO_REG; \
1N/A PL_top_env = &(ce); \
1N/A } STMT_END
1N/A
1N/A#define JMPENV_POST_CATCH JMPENV_POST_CATCH_ENV(*(JMPENV*)pcur_env)
1N/A
1N/A#define JMPENV_PUSH_ENV(ce,v) \
1N/A STMT_START { \
1N/A if (!(ce).je_noset) { \
1N/A DEBUG_l(Perl_deb(aTHX_ "Setting up jumplevel %p, was %p\n", \
1N/A ce, PL_top_env)); \
1N/A JMPENV_PUSH_INIT_ENV(ce,NULL); \
1N/A EXCEPT_SET_ENV(ce,PerlProc_setjmp((ce).je_buf, SCOPE_SAVES_SIGNAL_MASK));\
1N/A (ce).je_noset = 1; \
1N/A } \
1N/A else \
1N/A EXCEPT_SET_ENV(ce,0); \
1N/A JMPENV_POST_CATCH_ENV(ce); \
1N/A (v) = EXCEPT_GET_ENV(ce); \
1N/A } STMT_END
1N/A
1N/A#define JMPENV_PUSH(v) JMPENV_PUSH_ENV(*(JMPENV*)pcur_env,v)
1N/A
1N/A#define JMPENV_POP_ENV(ce) \
1N/A STMT_START { \
1N/A if (PL_top_env == &(ce)) \
1N/A PL_top_env = (ce).je_prev; \
1N/A } STMT_END
1N/A
1N/A#define JMPENV_POP JMPENV_POP_ENV(*(JMPENV*)pcur_env)
1N/A
1N/A#define JMPENV_JUMP(v) \
1N/A STMT_START { \
1N/A OP_REG_TO_MEM; \
1N/A if (PL_top_env->je_prev) { \
1N/A if (PL_top_env->je_throw) \
1N/A PL_top_env->je_throw(v); \
1N/A else \
1N/A PerlProc_longjmp(PL_top_env->je_buf, (v)); \
1N/A } \
1N/A if ((v) == 2) \
1N/A PerlProc_exit(STATUS_NATIVE_EXPORT); \
1N/A PerlIO_printf(Perl_error_log, "panic: top_env\n"); \
1N/A PerlProc_exit(1); \
1N/A } STMT_END
1N/A
1N/A#define EXCEPT_GET_ENV(ce) ((ce).je_ret)
1N/A#define EXCEPT_GET EXCEPT_GET_ENV(*(JMPENV*)pcur_env)
1N/A#define EXCEPT_SET_ENV(ce,v) ((ce).je_ret = (v))
1N/A#define EXCEPT_SET(v) EXCEPT_SET_ENV(*(JMPENV*)pcur_env,v)
1N/A
1N/A#else /* !PERL_FLEXIBLE_EXCEPTIONS */
1N/A
1N/A#define dJMPENV JMPENV cur_env
1N/A
1N/A#define JMPENV_PUSH(v) \
1N/A STMT_START { \
1N/A DEBUG_l(Perl_deb(aTHX_ "Setting up jumplevel %p, was %p\n", \
1N/A &cur_env, PL_top_env)); \
1N/A cur_env.je_prev = PL_top_env; \
1N/A OP_REG_TO_MEM; \
1N/A cur_env.je_ret = PerlProc_setjmp(cur_env.je_buf, SCOPE_SAVES_SIGNAL_MASK); \
1N/A OP_MEM_TO_REG; \
1N/A PL_top_env = &cur_env; \
1N/A cur_env.je_mustcatch = FALSE; \
1N/A (v) = cur_env.je_ret; \
1N/A } STMT_END
1N/A
1N/A#define JMPENV_POP \
1N/A STMT_START { PL_top_env = cur_env.je_prev; } STMT_END
1N/A
1N/A#define JMPENV_JUMP(v) \
1N/A STMT_START { \
1N/A OP_REG_TO_MEM; \
1N/A if (PL_top_env->je_prev) \
1N/A PerlProc_longjmp(PL_top_env->je_buf, (v)); \
1N/A if ((v) == 2) \
1N/A PerlProc_exit(STATUS_NATIVE_EXPORT); \
1N/A PerlIO_printf(PerlIO_stderr(), "panic: top_env\n"); \
1N/A PerlProc_exit(1); \
1N/A } STMT_END
1N/A
1N/A#endif /* PERL_FLEXIBLE_EXCEPTIONS */
1N/A
1N/A#define CATCH_GET (PL_top_env->je_mustcatch)
1N/A#define CATCH_SET(v) (PL_top_env->je_mustcatch = (v))