From 92da3157402727e9c7bdca6b9497423ca094acbe Mon Sep 17 00:00:00 2001 From: David Mitchell Date: Sat, 15 Dec 2012 00:31:40 +0000 Subject: [PATCH] eliminate PL_regsize This var (or rather PL_reg_state.re_state_regsize, which it is #deffed to) just holds the index of the maximum opening paren index seen so far in S_regmatch(). So make it a local var of S_regmatch() and pass it as a param to the couple of static functions called from there that need it. (Also give the local var the more meaningful name 'maxopenparen'.) --- embed.fnc | 6 ++-- embed.h | 4 +-- proto.h | 9 +++--- regexec.c | 94 +++++++++++++++++++++++++++++++++------------------------------ regexp.h | 2 -- 5 files changed, 61 insertions(+), 54 deletions(-) diff --git a/embed.fnc b/embed.fnc index 2be18ad..a3ab8a2 100644 --- a/embed.fnc +++ b/embed.fnc @@ -2031,8 +2031,10 @@ ERs |I32 |regrepeat |NN const regexp *prog|NN char **startposp|NN const regnode ERs |I32 |regtry |NN regmatch_info *reginfo|NN char **startposp ERs |bool |reginclass |NULLOK const regexp * const prog|NN const regnode * const n|NN const U8 * const p\ |bool const utf8_target -Es |CHECKPOINT|regcppush |NN const regexp *rex|I32 parenfloor -Es |void |regcppop |NN regexp *rex +Es |CHECKPOINT|regcppush |NN const regexp *rex|I32 parenfloor\ + |U32 maxopenparen +Es |void |regcppop |NN regexp *rex\ + |NN U32 *maxopenparen_p ERsn |U8* |reghop3 |NN U8 *s|I32 off|NN const U8 *lim ERsM |SV* |core_regclass_swash|NULLOK const regexp *prog \ |NN const struct regnode *node|bool doinit \ diff --git a/embed.h b/embed.h index 4ae36e3..20450e9 100644 --- a/embed.h +++ b/embed.h @@ -971,8 +971,8 @@ #define find_byclass(a,b,c,d,e) S_find_byclass(aTHX_ a,b,c,d,e) #define isFOO_lc(a,b) S_isFOO_lc(aTHX_ a,b) #define reg_check_named_buff_matched(a,b) S_reg_check_named_buff_matched(aTHX_ a,b) -#define regcppop(a) S_regcppop(aTHX_ a) -#define regcppush(a,b) S_regcppush(aTHX_ a,b) +#define regcppop(a,b) S_regcppop(aTHX_ a,b) +#define regcppush(a,b,c) S_regcppush(aTHX_ a,b,c) #define reghop3 S_reghop3 #define reghopmaybe3 S_reghopmaybe3 #define reginclass(a,b,c,d) S_reginclass(aTHX_ a,b,c,d) diff --git a/proto.h b/proto.h index 2ab4429..e22d7c9 100644 --- a/proto.h +++ b/proto.h @@ -6797,12 +6797,13 @@ STATIC I32 S_reg_check_named_buff_matched(pTHX_ const regexp *rex, const regnode #define PERL_ARGS_ASSERT_REG_CHECK_NAMED_BUFF_MATCHED \ assert(rex); assert(scan) -STATIC void S_regcppop(pTHX_ regexp *rex) - __attribute__nonnull__(pTHX_1); +STATIC void S_regcppop(pTHX_ regexp *rex, U32 *maxopenparen_p) + __attribute__nonnull__(pTHX_1) + __attribute__nonnull__(pTHX_2); #define PERL_ARGS_ASSERT_REGCPPOP \ - assert(rex) + assert(rex); assert(maxopenparen_p) -STATIC CHECKPOINT S_regcppush(pTHX_ const regexp *rex, I32 parenfloor) +STATIC CHECKPOINT S_regcppush(pTHX_ const regexp *rex, I32 parenfloor, U32 maxopenparen) __attribute__nonnull__(pTHX_1); #define PERL_ARGS_ASSERT_REGCPPUSH \ assert(rex) diff --git a/regexec.c b/regexec.c index 483829b..0d378b2 100644 --- a/regexec.c +++ b/regexec.c @@ -346,11 +346,12 @@ static void restore_pos(pTHX_ void *arg); * are needed for the regexp context stack bookkeeping. */ STATIC CHECKPOINT -S_regcppush(pTHX_ const regexp *rex, I32 parenfloor) +S_regcppush(pTHX_ const regexp *rex, I32 parenfloor, U32 maxopenparen) { dVAR; const int retval = PL_savestack_ix; - const int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS; + const int paren_elems_to_push = + (maxopenparen - parenfloor) * REGCP_PAREN_ELEMS; const UV total_elems = paren_elems_to_push + REGCP_OTHER_ELEMS; const UV elems_shifted = total_elems << SAVE_TIGHT_SHIFT; I32 p; @@ -365,19 +366,21 @@ S_regcppush(pTHX_ const regexp *rex, I32 parenfloor) if ((elems_shifted >> SAVE_TIGHT_SHIFT) != total_elems) Perl_croak(aTHX_ "panic: paren_elems_to_push offset %"UVuf " out of range (%lu-%ld)", - total_elems, (unsigned long)PL_regsize, (long)parenfloor); + total_elems, + (unsigned long)maxopenparen, + (long)parenfloor); SSGROW(total_elems + REGCP_FRAME_ELEMS); DEBUG_BUFFERS_r( - if ((int)PL_regsize > (int)parenfloor) + if ((int)maxopenparen > (int)parenfloor) PerlIO_printf(Perl_debug_log, "rex=0x%"UVxf" offs=0x%"UVxf": saving capture indices:\n", PTR2UV(rex), PTR2UV(rex->offs) ); ); - for (p = parenfloor+1; p <= (I32)PL_regsize; p++) { + for (p = parenfloor+1; p <= (I32)maxopenparen; p++) { /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */ SSPUSHINT(rex->offs[p].end); SSPUSHINT(rex->offs[p].start); @@ -391,7 +394,7 @@ S_regcppush(pTHX_ const regexp *rex, I32 parenfloor) )); } /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */ - SSPUSHINT(PL_regsize); + SSPUSHINT(maxopenparen); SSPUSHINT(rex->lastparen); SSPUSHINT(rex->lastcloseparen); SSPUSHUV(SAVEt_REGCONTEXT | elems_shifted); /* Magic cookie. */ @@ -423,7 +426,7 @@ S_regcppush(pTHX_ const regexp *rex, I32 parenfloor) STATIC void -S_regcppop(pTHX_ regexp *rex) +S_regcppop(pTHX_ regexp *rex, U32 *maxopenparen_p) { dVAR; UV i; @@ -438,7 +441,7 @@ S_regcppop(pTHX_ regexp *rex) i >>= SAVE_TIGHT_SHIFT; /* Parentheses elements to pop. */ rex->lastcloseparen = SSPOPINT; rex->lastparen = SSPOPINT; - PL_regsize = SSPOPINT; + *maxopenparen_p = SSPOPINT; i -= REGCP_OTHER_ELEMS; /* Now restore the parentheses context. */ @@ -450,7 +453,7 @@ S_regcppop(pTHX_ regexp *rex) PTR2UV(rex->offs) ); ); - paren = PL_regsize; + paren = *maxopenparen_p; for ( ; i > 0; i -= REGCP_PAREN_ELEMS) { I32 tmps; rex->offs[paren].start_tmp = SSPOPINT; @@ -479,13 +482,13 @@ S_regcppop(pTHX_ regexp *rex) * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/ * --jhi updated by dapm */ for (i = rex->lastparen + 1; i <= rex->nparens; i++) { - if (i > PL_regsize) + if (i > *maxopenparen_p) rex->offs[i].start = -1; rex->offs[i].end = -1; DEBUG_BUFFERS_r( PerlIO_printf(Perl_debug_log, " \\%"UVuf": %s ..-1 undeffing\n", (UV)i, - (i > PL_regsize) ? "-1" : " " + (i > *maxopenparen_p) ? "-1" : " " )); } #endif @@ -495,11 +498,11 @@ S_regcppop(pTHX_ regexp *rex) * but without popping the stack */ STATIC void -S_regcp_restore(pTHX_ regexp *rex, I32 ix) +S_regcp_restore(pTHX_ regexp *rex, I32 ix, U32 *maxopenparen_p) { I32 tmpix = PL_savestack_ix; PL_savestack_ix = ix; - regcppop(rex); + regcppop(rex, maxopenparen_p); PL_savestack_ix = tmpix; } @@ -2910,7 +2913,6 @@ S_regtry(pTHX_ regmatch_info *reginfo, char **startposp) prog->offs[0].start = *startposp - PL_bostr; prog->lastparen = 0; prog->lastcloseparen = 0; - PL_regsize = 0; /* XXXX What this code is doing here?!!! There should be no need to do this again and again, prog->lastparen should take care of @@ -3631,6 +3633,7 @@ S_regmatch(pTHX_ regmatch_info *reginfo, char *startpos, regnode *prog) CV *caller_cv = NULL; /* who called us */ CV *last_pushed_cv = NULL; /* most recently called (?{}) CV */ CHECKPOINT runops_cp; /* savestack position before executing EVAL */ + U32 maxopenparen = 0; /* max '(' index seen so far */ #ifdef DEBUGGING GET_RE_DEBUG_FLAGS_DECL; @@ -4846,7 +4849,7 @@ S_regmatch(pTHX_ regmatch_info *reginfo, char *startpos, regnode *prog) CV *newcv; /* save *all* paren positions */ - regcppush(rex, 0); + regcppush(rex, 0, maxopenparen); REGCP_SET(runops_cp); /* To not corrupt the existing regex state while executing the @@ -5020,7 +5023,7 @@ S_regmatch(pTHX_ regmatch_info *reginfo, char *startpos, regnode *prog) PL_op = oop; PL_curcop = ocurcop; PL_regeol = saved_regeol; - S_regcp_restore(aTHX_ rex, runops_cp); + S_regcp_restore(aTHX_ rex, runops_cp, &maxopenparen); if (logical != 2) break; @@ -5037,7 +5040,6 @@ S_regmatch(pTHX_ regmatch_info *reginfo, char *startpos, regnode *prog) } else { U32 pm_flags = 0; - const I32 osize = PL_regsize; if (SvUTF8(ret) && IN_BYTES) { /* In use 'bytes': make a copy of the octet @@ -5067,11 +5069,10 @@ S_regmatch(pTHX_ regmatch_info *reginfo, char *startpos, regnode *prog) scalar. */ sv_magic(ret, MUTABLE_SV(re_sv), PERL_MAGIC_qr, 0, 0); } - PL_regsize = osize; /* safe to do now that any $1 etc has been * interpolated into the new pattern string and * compiled */ - S_regcp_restore(aTHX_ rex, runops_cp); + S_regcp_restore(aTHX_ rex, runops_cp, &maxopenparen); } SAVEFREESV(re_sv); re = ReANY(re_sv); @@ -5091,13 +5092,15 @@ S_regmatch(pTHX_ regmatch_info *reginfo, char *startpos, regnode *prog) eval_recurse_doit: /* Share code with GOSUB below this line */ /* run the pattern returned from (??{...}) */ - ST.cp = regcppush(rex, 0); /* Save *all* the positions. */ + + /* Save *all* the positions. */ + ST.cp = regcppush(rex, 0, maxopenparen); REGCP_SET(ST.lastcp); re->lastparen = 0; re->lastcloseparen = 0; - PL_regsize = 0; + maxopenparen = 0; /* XXXX This is too dramatic a measure... */ PL_reg_maxiter = 0; @@ -5151,7 +5154,7 @@ S_regmatch(pTHX_ regmatch_info *reginfo, char *startpos, regnode *prog) rexi = RXi_GET(rex); REGCP_UNWIND(ST.lastcp); - regcppop(rex); + regcppop(rex, &maxopenparen); cur_eval = ST.prev_eval; cur_curlyx = ST.prev_curlyx; /* XXXX This is too dramatic a measure... */ @@ -5164,15 +5167,15 @@ S_regmatch(pTHX_ regmatch_info *reginfo, char *startpos, regnode *prog) case OPEN: /* ( */ n = ARG(scan); /* which paren pair */ rex->offs[n].start_tmp = locinput - PL_bostr; - if (n > PL_regsize) - PL_regsize = n; + if (n > maxopenparen) + maxopenparen = n; DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log, - "rex=0x%"UVxf" offs=0x%"UVxf": \\%"UVuf": set %"IVdf" tmp; regsize=%"UVuf"\n", + "rex=0x%"UVxf" offs=0x%"UVxf": \\%"UVuf": set %"IVdf" tmp; maxopenparen=%"UVuf"\n", PTR2UV(rex), PTR2UV(rex->offs), (UV)n, (IV)rex->offs[n].start_tmp, - (UV)PL_regsize + (UV)maxopenparen )); lastopen = n; break; @@ -5193,8 +5196,6 @@ S_regmatch(pTHX_ regmatch_info *reginfo, char *startpos, regnode *prog) case CLOSE: /* ) */ n = ARG(scan); /* which paren pair */ CLOSE_CAPTURE; - /*if (n > PL_regsize) - PL_regsize = n;*/ if (n > rex->lastparen) rex->lastparen = n; rex->lastcloseparen = n; @@ -5214,8 +5215,6 @@ S_regmatch(pTHX_ regmatch_info *reginfo, char *startpos, regnode *prog) n = ARG(cursor); if ( n <= lastopen ) { CLOSE_CAPTURE; - /*if (n > PL_regsize) - PL_regsize = n;*/ if (n > rex->lastparen) rex->lastparen = n; rex->lastcloseparen = n; @@ -5358,7 +5357,7 @@ NULL next += ARG(next); /* XXXX Probably it is better to teach regpush to support - parenfloor > PL_regsize... */ + parenfloor > maxopenparen ... */ if (parenfloor > (I32)rex->lastparen) parenfloor = rex->lastparen; /* Pessimization... */ @@ -5418,7 +5417,8 @@ NULL /* First just match a string of min A's. */ if (n < min) { - ST.cp = regcppush(rex, cur_curlyx->u.curlyx.parenfloor); + ST.cp = regcppush(rex, cur_curlyx->u.curlyx.parenfloor, + maxopenparen); cur_curlyx->u.curlyx.lastloc = locinput; REGCP_SET(ST.lastcp); @@ -5494,7 +5494,8 @@ NULL if (cur_curlyx->u.curlyx.minmod) { ST.save_curlyx = cur_curlyx; cur_curlyx = cur_curlyx->u.curlyx.prev_curlyx; - ST.cp = regcppush(rex, ST.save_curlyx->u.curlyx.parenfloor); + ST.cp = regcppush(rex, ST.save_curlyx->u.curlyx.parenfloor, + maxopenparen); REGCP_SET(ST.lastcp); PUSH_YES_STATE_GOTO(WHILEM_B_min, ST.save_curlyx->u.curlyx.B, locinput); @@ -5504,7 +5505,8 @@ NULL /* Prefer A over B for maximal matching. */ if (n < max) { /* More greed allowed? */ - ST.cp = regcppush(rex, cur_curlyx->u.curlyx.parenfloor); + ST.cp = regcppush(rex, cur_curlyx->u.curlyx.parenfloor, + maxopenparen); cur_curlyx->u.curlyx.lastloc = locinput; REGCP_SET(ST.lastcp); PUSH_STATE_GOTO(WHILEM_A_max, A, locinput); @@ -5531,7 +5533,7 @@ NULL /* FALL THROUGH */ case WHILEM_A_pre_fail: /* just failed to match even minimal A */ REGCP_UNWIND(ST.lastcp); - regcppop(rex); + regcppop(rex, &maxopenparen); cur_curlyx->u.curlyx.lastloc = ST.save_lastloc; cur_curlyx->u.curlyx.count--; CACHEsayNO; @@ -5539,7 +5541,7 @@ NULL case WHILEM_A_max_fail: /* just failed to match A in a maximal match */ REGCP_UNWIND(ST.lastcp); - regcppop(rex); /* Restore some previous $s? */ + regcppop(rex, &maxopenparen); /* Restore some previous $s? */ DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%*s whilem: failed, trying continuation...\n", REPORT_CODE_OFF+depth*2, "") @@ -5566,7 +5568,7 @@ NULL case WHILEM_B_min_fail: /* just failed to match B in a minimal match */ cur_curlyx = ST.save_curlyx; REGCP_UNWIND(ST.lastcp); - regcppop(rex); + regcppop(rex, &maxopenparen); if (cur_curlyx->u.curlyx.count >= /*max*/ARG2(cur_curlyx->u.curlyx.me)) { /* Maximum greed exceeded */ @@ -5589,7 +5591,8 @@ NULL ); /* Try grabbing another A and see if it helps. */ cur_curlyx->u.curlyx.lastloc = locinput; - ST.cp = regcppush(rex, cur_curlyx->u.curlyx.parenfloor); + ST.cp = regcppush(rex, cur_curlyx->u.curlyx.parenfloor, + maxopenparen); REGCP_SET(ST.lastcp); PUSH_STATE_GOTO(WHILEM_A_min, /*A*/ NEXTOPER(ST.save_curlyx->u.curlyx.me) + EXTRA_STEP_2ARGS, @@ -5685,8 +5688,8 @@ NULL /* if paren positive, emulate an OPEN/CLOSE around A */ if (ST.me->flags) { U32 paren = ST.me->flags; - if (paren > PL_regsize) - PL_regsize = paren; + if (paren > maxopenparen) + maxopenparen = paren; scan += NEXT_OFF(scan); /* Skip former OPEN. */ } ST.A = scan; @@ -5892,8 +5895,8 @@ NULL ST.paren = scan->flags; /* Which paren to set */ ST.lastparen = rex->lastparen; ST.lastcloseparen = rex->lastcloseparen; - if (ST.paren > PL_regsize) - PL_regsize = ST.paren; + if (ST.paren > maxopenparen) + maxopenparen = ST.paren; ST.min = ARG1(scan); /* min to match */ ST.max = ARG2(scan); /* max to match */ if (cur_eval && cur_eval->u.eval.close_paren && @@ -6181,7 +6184,9 @@ NULL PL_reg_flags ^= st->u.eval.toggle_reg_flags; st->u.eval.prev_rex = rex_sv; /* inner */ - st->u.eval.cp = regcppush(rex, 0); /* Save *all* the positions. */ + + /* Save *all* the positions. */ + st->u.eval.cp = regcppush(rex, 0, maxopenparen); rex_sv = cur_eval->u.eval.prev_rex; SET_reg_curpm(rex_sv); rex = ReANY(rex_sv); @@ -6192,7 +6197,8 @@ NULL /* Restore parens of the outer rex without popping the * savestack */ - S_regcp_restore(aTHX_ rex, cur_eval->u.eval.lastcp); + S_regcp_restore(aTHX_ rex, cur_eval->u.eval.lastcp, + &maxopenparen); st->u.eval.prev_eval = cur_eval; cur_eval = cur_eval->u.eval.prev_eval; diff --git a/regexp.h b/regexp.h index 8a067eb..664915b 100644 --- a/regexp.h +++ b/regexp.h @@ -765,7 +765,6 @@ typedef struct regmatch_slab { #define PL_reg_leftiter PL_reg_state.re_state_reg_leftiter #define PL_reg_poscache PL_reg_state.re_state_reg_poscache #define PL_reg_poscache_size PL_reg_state.re_state_reg_poscache_size -#define PL_regsize PL_reg_state.re_state_regsize #define PL_reg_starttry PL_reg_state.re_state_reg_starttry #define PL_nrs PL_reg_state.re_state_nrs @@ -787,7 +786,6 @@ struct re_save_state { I32 re_state_reg_oldpos; /* from regexec.c */ I32 re_state_reg_maxiter; /* max wait until caching pos */ I32 re_state_reg_leftiter; /* wait until caching pos */ - U32 re_state_regsize; /* from regexec.c */ char *re_state_reg_poscache; /* cache of pos of WHILEM */ char *re_state_reg_starttry; /* from regexec.c */ #ifdef PERL_ANY_COW -- 2.7.4