From 89e4e5e3d97ca1a61f9062ea551f72d793517508 Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Tue, 12 Apr 2022 10:16:02 -0700 Subject: [PATCH 1/9] Try out computed gotos in the regex engine --- Modules/_sre/sre_lib.h | 439 ++++++++++++++++++++++++++++++++++------- 1 file changed, 366 insertions(+), 73 deletions(-) diff --git a/Modules/_sre/sre_lib.h b/Modules/_sre/sre_lib.h index 34cd0552532f70..6ff066e6b15f42 100644 --- a/Modules/_sre/sre_lib.h +++ b/Modules/_sre/sre_lib.h @@ -517,6 +517,33 @@ typedef struct { int toplevel; } SRE(match_context); +#define DO_STUFF() \ +do { \ + if ((0 == (++sigcount & 0xfff)) && PyErr_CheckSignals()) { \ + RETURN_ERROR(SRE_ERROR_INTERRUPTED); \ + } \ +} while (0) + +#ifdef HAVE_COMPUTED_GOTOS + #ifndef USE_COMPUTED_GOTOS + #define USE_COMPUTED_GOTOS 1 + #endif +#else + #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS + #error "Computed gotos are not supported on this compiler." + #endif + #undef USE_COMPUTED_GOTOS + #define USE_COMPUTED_GOTOS 0 +#endif + +#if USE_COMPUTED_GOTOS +#define TARGET(OP) LABEL_ ## OP +#define DISPATCH() do { DO_STUFF(); goto *sre_op_targets[*ctx->pattern++]; } while (0) +#else +#define TARGET(OP) case OP +#define DISPATCH() break +#endif + /* check if string matches the given pattern. returns <0 for error, 0 for failure, and 1 for success */ LOCAL(Py_ssize_t) @@ -555,14 +582,276 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) ctx->pattern += ctx->pattern[1] + 1; } - for (;;) { - ++sigcount; - if ((0 == (sigcount & 0xfff)) && PyErr_CheckSignals()) - RETURN_ERROR(SRE_ERROR_INTERRUPTED); - - switch (*ctx->pattern++) { +#if USE_COMPUTED_GOTOS +static void *sre_op_targets[256] = { + &&LABEL_SRE_OP_FAILURE, + &&LABEL_SRE_OP_SUCCESS, + &&LABEL_SRE_OP_ANY, + &&LABEL_SRE_OP_ANY_ALL, + &&LABEL_SRE_OP_ASSERT, + &&LABEL_SRE_OP_ASSERT_NOT, + &&LABEL_SRE_OP_AT, + &&LABEL_SRE_OP_BRANCH, + &&unknown, // &&LABEL_SRE_OP_CALL, + &&LABEL_SRE_OP_CATEGORY, + &&unknown, // &&LABEL_SRE_OP_CHARSET, + &&unknown, // &&LABEL_SRE_OP_BIGCHARSET, + &&LABEL_SRE_OP_GROUPREF, + &&LABEL_SRE_OP_GROUPREF_EXISTS, + &&LABEL_SRE_OP_IN, + &&LABEL_SRE_OP_INFO, + &&LABEL_SRE_OP_JUMP, + &&LABEL_SRE_OP_LITERAL, + &&LABEL_SRE_OP_MARK, + &&LABEL_SRE_OP_MAX_UNTIL, + &&LABEL_SRE_OP_MIN_UNTIL, + &&LABEL_SRE_OP_NOT_LITERAL, + &&unknown, // &&LABEL_SRE_OP_NEGATE, + &&unknown, // &&LABEL_SRE_OP_RANGE, + &&LABEL_SRE_OP_REPEAT, + &&LABEL_SRE_OP_REPEAT_ONE, + &&unknown, // &&LABEL_SRE_OP_SUBPATTERN, + &&LABEL_SRE_OP_MIN_REPEAT_ONE, + &&LABEL_SRE_OP_ATOMIC_GROUP, + &&LABEL_SRE_OP_POSSESSIVE_REPEAT, + &&LABEL_SRE_OP_POSSESSIVE_REPEAT_ONE, + &&LABEL_SRE_OP_GROUPREF_IGNORE, + &&LABEL_SRE_OP_IN_IGNORE, + &&LABEL_SRE_OP_LITERAL_IGNORE, + &&LABEL_SRE_OP_NOT_LITERAL_IGNORE, + &&LABEL_SRE_OP_GROUPREF_LOC_IGNORE, + &&LABEL_SRE_OP_IN_LOC_IGNORE, + &&LABEL_SRE_OP_LITERAL_LOC_IGNORE, + &&LABEL_SRE_OP_NOT_LITERAL_LOC_IGNORE, + &&LABEL_SRE_OP_GROUPREF_UNI_IGNORE, + &&LABEL_SRE_OP_IN_UNI_IGNORE, + &&LABEL_SRE_OP_LITERAL_UNI_IGNORE, + &&LABEL_SRE_OP_NOT_LITERAL_UNI_IGNORE, + &&unknown, // &&LABEL_SRE_OP_RANGE_UNI_IGNORE, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, + &&unknown, +}; +#endif - case SRE_OP_MARK: + for (;;) { +#if USE_COMPUTED_GOTOS + DISPATCH(); +#else + DO_STUFF(); + switch (*ctx->pattern++) +#endif + { + TARGET(SRE_OP_MARK): /* set mark */ /* */ TRACE(("|%p|%p|MARK %d\n", ctx->pattern, @@ -582,9 +871,9 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) } state->mark[i] = ctx->ptr; ctx->pattern++; - break; + DISPATCH(); - case SRE_OP_LITERAL: + TARGET(SRE_OP_LITERAL): /* match literal string */ /* */ TRACE(("|%p|%p|LITERAL %d\n", ctx->pattern, @@ -593,9 +882,9 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) RETURN_FAILURE; ctx->pattern++; ctx->ptr++; - break; + DISPATCH(); - case SRE_OP_NOT_LITERAL: + TARGET(SRE_OP_NOT_LITERAL): /* match anything that is not literal character */ /* */ TRACE(("|%p|%p|NOT_LITERAL %d\n", ctx->pattern, @@ -604,9 +893,9 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) RETURN_FAILURE; ctx->pattern++; ctx->ptr++; - break; + DISPATCH(); - case SRE_OP_SUCCESS: + TARGET(SRE_OP_SUCCESS): /* end of pattern */ TRACE(("|%p|%p|SUCCESS\n", ctx->pattern, ctx->ptr)); if (ctx->toplevel && @@ -618,16 +907,16 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) state->ptr = ctx->ptr; RETURN_SUCCESS; - case SRE_OP_AT: + TARGET(SRE_OP_AT): /* match at given position */ /* */ TRACE(("|%p|%p|AT %d\n", ctx->pattern, ctx->ptr, *ctx->pattern)); if (!SRE(at)(state, ctx->ptr, *ctx->pattern)) RETURN_FAILURE; ctx->pattern++; - break; + DISPATCH(); - case SRE_OP_CATEGORY: + TARGET(SRE_OP_CATEGORY): /* match at given category */ /* */ TRACE(("|%p|%p|CATEGORY %d\n", ctx->pattern, @@ -636,27 +925,27 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) RETURN_FAILURE; ctx->pattern++; ctx->ptr++; - break; + DISPATCH(); - case SRE_OP_ANY: + TARGET(SRE_OP_ANY): /* match anything (except a newline) */ /* */ TRACE(("|%p|%p|ANY\n", ctx->pattern, ctx->ptr)); if (ctx->ptr >= end || SRE_IS_LINEBREAK(ctx->ptr[0])) RETURN_FAILURE; ctx->ptr++; - break; + DISPATCH(); - case SRE_OP_ANY_ALL: + TARGET(SRE_OP_ANY_ALL): /* match anything */ /* */ TRACE(("|%p|%p|ANY_ALL\n", ctx->pattern, ctx->ptr)); if (ctx->ptr >= end) RETURN_FAILURE; ctx->ptr++; - break; + DISPATCH(); - case SRE_OP_IN: + TARGET(SRE_OP_IN): /* match set member (or non_member) */ /* */ TRACE(("|%p|%p|IN\n", ctx->pattern, ctx->ptr)); @@ -665,9 +954,9 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) RETURN_FAILURE; ctx->pattern += ctx->pattern[0]; ctx->ptr++; - break; + DISPATCH(); - case SRE_OP_LITERAL_IGNORE: + TARGET(SRE_OP_LITERAL_IGNORE): TRACE(("|%p|%p|LITERAL_IGNORE %d\n", ctx->pattern, ctx->ptr, ctx->pattern[0])); if (ctx->ptr >= end || @@ -675,9 +964,9 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) RETURN_FAILURE; ctx->pattern++; ctx->ptr++; - break; + DISPATCH(); - case SRE_OP_LITERAL_UNI_IGNORE: + TARGET(SRE_OP_LITERAL_UNI_IGNORE): TRACE(("|%p|%p|LITERAL_UNI_IGNORE %d\n", ctx->pattern, ctx->ptr, ctx->pattern[0])); if (ctx->ptr >= end || @@ -685,9 +974,9 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) RETURN_FAILURE; ctx->pattern++; ctx->ptr++; - break; + DISPATCH(); - case SRE_OP_LITERAL_LOC_IGNORE: + TARGET(SRE_OP_LITERAL_LOC_IGNORE): TRACE(("|%p|%p|LITERAL_LOC_IGNORE %d\n", ctx->pattern, ctx->ptr, ctx->pattern[0])); if (ctx->ptr >= end @@ -695,9 +984,9 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) RETURN_FAILURE; ctx->pattern++; ctx->ptr++; - break; + DISPATCH(); - case SRE_OP_NOT_LITERAL_IGNORE: + TARGET(SRE_OP_NOT_LITERAL_IGNORE): TRACE(("|%p|%p|NOT_LITERAL_IGNORE %d\n", ctx->pattern, ctx->ptr, *ctx->pattern)); if (ctx->ptr >= end || @@ -705,9 +994,9 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) RETURN_FAILURE; ctx->pattern++; ctx->ptr++; - break; + DISPATCH(); - case SRE_OP_NOT_LITERAL_UNI_IGNORE: + TARGET(SRE_OP_NOT_LITERAL_UNI_IGNORE): TRACE(("|%p|%p|NOT_LITERAL_UNI_IGNORE %d\n", ctx->pattern, ctx->ptr, *ctx->pattern)); if (ctx->ptr >= end || @@ -715,9 +1004,9 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) RETURN_FAILURE; ctx->pattern++; ctx->ptr++; - break; + DISPATCH(); - case SRE_OP_NOT_LITERAL_LOC_IGNORE: + TARGET(SRE_OP_NOT_LITERAL_LOC_IGNORE): TRACE(("|%p|%p|NOT_LITERAL_LOC_IGNORE %d\n", ctx->pattern, ctx->ptr, *ctx->pattern)); if (ctx->ptr >= end @@ -725,9 +1014,9 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) RETURN_FAILURE; ctx->pattern++; ctx->ptr++; - break; + DISPATCH(); - case SRE_OP_IN_IGNORE: + TARGET(SRE_OP_IN_IGNORE): TRACE(("|%p|%p|IN_IGNORE\n", ctx->pattern, ctx->ptr)); if (ctx->ptr >= end || !SRE(charset)(state, ctx->pattern+1, @@ -735,9 +1024,9 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) RETURN_FAILURE; ctx->pattern += ctx->pattern[0]; ctx->ptr++; - break; + DISPATCH(); - case SRE_OP_IN_UNI_IGNORE: + TARGET(SRE_OP_IN_UNI_IGNORE): TRACE(("|%p|%p|IN_UNI_IGNORE\n", ctx->pattern, ctx->ptr)); if (ctx->ptr >= end || !SRE(charset)(state, ctx->pattern+1, @@ -745,27 +1034,27 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) RETURN_FAILURE; ctx->pattern += ctx->pattern[0]; ctx->ptr++; - break; + DISPATCH(); - case SRE_OP_IN_LOC_IGNORE: + TARGET(SRE_OP_IN_LOC_IGNORE): TRACE(("|%p|%p|IN_LOC_IGNORE\n", ctx->pattern, ctx->ptr)); if (ctx->ptr >= end || !SRE(charset_loc_ignore)(state, ctx->pattern+1, *ctx->ptr)) RETURN_FAILURE; ctx->pattern += ctx->pattern[0]; ctx->ptr++; - break; + DISPATCH(); - case SRE_OP_JUMP: - case SRE_OP_INFO: + TARGET(SRE_OP_JUMP): + TARGET(SRE_OP_INFO): /* jump forward */ /* */ TRACE(("|%p|%p|JUMP %d\n", ctx->pattern, ctx->ptr, ctx->pattern[0])); ctx->pattern += ctx->pattern[0]; - break; + DISPATCH(); - case SRE_OP_BRANCH: + TARGET(SRE_OP_BRANCH): /* alternation */ /* <0=skip> code ... */ TRACE(("|%p|%p|BRANCH\n", ctx->pattern, ctx->ptr)); @@ -798,7 +1087,7 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) MARK_POP_DISCARD(ctx->lastmark); RETURN_FAILURE; - case SRE_OP_REPEAT_ONE: + TARGET(SRE_OP_REPEAT_ONE): /* match repeated sequence (maximizing regexp) */ /* this operator only works if the repeated item is @@ -897,7 +1186,7 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) } RETURN_FAILURE; - case SRE_OP_MIN_REPEAT_ONE: + TARGET(SRE_OP_MIN_REPEAT_ONE): /* match repeated sequence (minimizing regexp) */ /* this operator only works if the repeated item is @@ -975,7 +1264,7 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) } RETURN_FAILURE; - case SRE_OP_POSSESSIVE_REPEAT_ONE: + TARGET(SRE_OP_POSSESSIVE_REPEAT_ONE): /* match repeated sequence (maximizing regexp) without backtracking */ @@ -1027,9 +1316,9 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) } /* Attempt to match the rest of the string */ - break; + DISPATCH(); - case SRE_OP_REPEAT: + TARGET(SRE_OP_REPEAT): /* create repeat context. all the hard work is done by the UNTIL operator (MAX_UNTIL, MIN_UNTIL) */ /* <1=min> <2=max> @@ -1056,7 +1345,7 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) } RETURN_FAILURE; - case SRE_OP_MAX_UNTIL: + TARGET(SRE_OP_MAX_UNTIL): /* maximizing repeat */ /* <1=min> <2=max> <3=repeat_index> item tail */ @@ -1124,7 +1413,7 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) state->ptr = ctx->ptr; RETURN_FAILURE; - case SRE_OP_MIN_UNTIL: + TARGET(SRE_OP_MIN_UNTIL): /* minimizing repeat */ /* <1=min> <2=max> <3=repeat_index> item tail */ @@ -1197,7 +1486,7 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) state->ptr = ctx->ptr; RETURN_FAILURE; - case SRE_OP_POSSESSIVE_REPEAT: + TARGET(SRE_OP_POSSESSIVE_REPEAT): /* create possessive repeat contexts. */ /* <1=min> <2=max> pattern tail */ @@ -1295,9 +1584,9 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) the SUCCESS op code that follows it. */ ctx->pattern += ctx->pattern[0] + 1; ctx->ptr = state->ptr; - break; + DISPATCH(); - case SRE_OP_ATOMIC_GROUP: + TARGET(SRE_OP_ATOMIC_GROUP): /* Atomic Group Sub Pattern */ /* pattern tail */ TRACE(("|%p|%p|ATOMIC_GROUP\n", ctx->pattern, ctx->ptr)); @@ -1327,9 +1616,9 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) the SUCCESS op code that follows it. */ ctx->pattern += ctx->pattern[0]; ctx->ptr = state->ptr; - break; + DISPATCH(); - case SRE_OP_GROUPREF: + TARGET(SRE_OP_GROUPREF): /* match backreference */ TRACE(("|%p|%p|GROUPREF %d\n", ctx->pattern, ctx->ptr, ctx->pattern[0])); @@ -1352,9 +1641,9 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) } } ctx->pattern++; - break; + DISPATCH(); - case SRE_OP_GROUPREF_IGNORE: + TARGET(SRE_OP_GROUPREF_IGNORE): /* match backreference */ TRACE(("|%p|%p|GROUPREF_IGNORE %d\n", ctx->pattern, ctx->ptr, ctx->pattern[0])); @@ -1378,9 +1667,9 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) } } ctx->pattern++; - break; + DISPATCH(); - case SRE_OP_GROUPREF_UNI_IGNORE: + TARGET(SRE_OP_GROUPREF_UNI_IGNORE): /* match backreference */ TRACE(("|%p|%p|GROUPREF_UNI_IGNORE %d\n", ctx->pattern, ctx->ptr, ctx->pattern[0])); @@ -1404,9 +1693,9 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) } } ctx->pattern++; - break; + DISPATCH(); - case SRE_OP_GROUPREF_LOC_IGNORE: + TARGET(SRE_OP_GROUPREF_LOC_IGNORE): /* match backreference */ TRACE(("|%p|%p|GROUPREF_LOC_IGNORE %d\n", ctx->pattern, ctx->ptr, ctx->pattern[0])); @@ -1430,9 +1719,9 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) } } ctx->pattern++; - break; + DISPATCH(); - case SRE_OP_GROUPREF_EXISTS: + TARGET(SRE_OP_GROUPREF_EXISTS): TRACE(("|%p|%p|GROUPREF_EXISTS %d\n", ctx->pattern, ctx->ptr, ctx->pattern[0])); /* codeyes codeno ... */ @@ -1441,20 +1730,20 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) Py_ssize_t groupref = i+i; if (groupref >= state->lastmark) { ctx->pattern += ctx->pattern[1]; - break; + DISPATCH(); } else { SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref]; SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1]; if (!p || !e || e < p) { ctx->pattern += ctx->pattern[1]; - break; + DISPATCH(); } } } ctx->pattern += 2; - break; + DISPATCH(); - case SRE_OP_ASSERT: + TARGET(SRE_OP_ASSERT): /* assert subpattern */ /* */ TRACE(("|%p|%p|ASSERT %d\n", ctx->pattern, @@ -1465,9 +1754,9 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) DO_JUMP0(JUMP_ASSERT, jump_assert, ctx->pattern+2); RETURN_ON_FAILURE(ret); ctx->pattern += ctx->pattern[0]; - break; + DISPATCH(); - case SRE_OP_ASSERT_NOT: + TARGET(SRE_OP_ASSERT_NOT): /* assert not subpattern */ /* */ TRACE(("|%p|%p|ASSERT_NOT %d\n", ctx->pattern, @@ -1490,14 +1779,18 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) LASTMARK_RESTORE(); } ctx->pattern += ctx->pattern[0]; - break; + DISPATCH(); - case SRE_OP_FAILURE: + TARGET(SRE_OP_FAILURE): /* immediate failure */ TRACE(("|%p|%p|FAILURE\n", ctx->pattern, ctx->ptr)); RETURN_FAILURE; +#if USE_COMPUTED_GOTOS + unknown: +#else default: +#endif TRACE(("|%p|%p|UNKNOWN %d\n", ctx->pattern, ctx->ptr, ctx->pattern[-1])); RETURN_ERROR(SRE_ERROR_ILLEGAL); From bc378d8e98f4aacc49191731a3aeca40e486bf40 Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Tue, 12 Apr 2022 13:06:56 -0700 Subject: [PATCH 2/9] Reduce indirection --- Modules/_sre/sre_lib.h | 350 +++++++++++++++++++++-------------------- 1 file changed, 177 insertions(+), 173 deletions(-) diff --git a/Modules/_sre/sre_lib.h b/Modules/_sre/sre_lib.h index 6ff066e6b15f42..4c872531b2e7db 100644 --- a/Modules/_sre/sre_lib.h +++ b/Modules/_sre/sre_lib.h @@ -490,10 +490,13 @@ do { \ nextctx->jump = jumpvalue; \ nextctx->pattern = nextpattern; \ nextctx->toplevel = toplevel_; \ + ctx->pattern = pattern; \ + pattern = nextpattern; \ ctx_pos = alloc_pos; \ ctx = nextctx; \ goto entrance; \ jumplabel: \ + pattern = ctx->pattern; \ while (0) /* gcc doesn't like labels at end of scopes */ \ #define DO_JUMP(jumpvalue, jumplabel, nextpattern) \ @@ -522,6 +525,7 @@ do { \ if ((0 == (++sigcount & 0xfff)) && PyErr_CheckSignals()) { \ RETURN_ERROR(SRE_ERROR_INTERRUPTED); \ } \ + ctx->pattern = pattern + 1; \ } while (0) #ifdef HAVE_COMPUTED_GOTOS @@ -538,7 +542,7 @@ do { \ #if USE_COMPUTED_GOTOS #define TARGET(OP) LABEL_ ## OP -#define DISPATCH() do { DO_STUFF(); goto *sre_op_targets[*ctx->pattern++]; } while (0) +#define DISPATCH() do { DO_STUFF(); goto *sre_op_targets[*pattern++]; } while (0) #else #define TARGET(OP) case OP #define DISPATCH() break @@ -571,15 +575,15 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) ctx->ptr = (SRE_CHAR *)state->ptr; - if (ctx->pattern[0] == SRE_OP_INFO) { + if (pattern[0] == SRE_OP_INFO) { /* optimization info block */ /* <1=skip> <2=flags> <3=min> ... */ - if (ctx->pattern[3] && (uintptr_t)(end - ctx->ptr) < ctx->pattern[3]) { + if (pattern[3] && (uintptr_t)(end - ctx->ptr) < pattern[3]) { TRACE(("reject (got %zd chars, need %zd)\n", - end - ctx->ptr, (Py_ssize_t) ctx->pattern[3])); + end - ctx->ptr, (Py_ssize_t) pattern[3])); RETURN_FAILURE; } - ctx->pattern += ctx->pattern[1] + 1; + pattern += pattern[1] + 1; } #if USE_COMPUTED_GOTOS @@ -848,15 +852,15 @@ static void *sre_op_targets[256] = { DISPATCH(); #else DO_STUFF(); - switch (*ctx->pattern++) + switch (*pattern++) #endif { TARGET(SRE_OP_MARK): /* set mark */ /* */ - TRACE(("|%p|%p|MARK %d\n", ctx->pattern, - ctx->ptr, ctx->pattern[0])); - i = ctx->pattern[0]; + TRACE(("|%p|%p|MARK %d\n", pattern, + ctx->ptr, pattern[0])); + i = pattern[0]; if (i & 1) state->lastindex = i/2 + 1; if (i > state->lastmark) { @@ -870,34 +874,34 @@ static void *sre_op_targets[256] = { state->lastmark = i; } state->mark[i] = ctx->ptr; - ctx->pattern++; + pattern++; DISPATCH(); TARGET(SRE_OP_LITERAL): /* match literal string */ /* */ - TRACE(("|%p|%p|LITERAL %d\n", ctx->pattern, - ctx->ptr, *ctx->pattern)); - if (ctx->ptr >= end || (SRE_CODE) ctx->ptr[0] != ctx->pattern[0]) + TRACE(("|%p|%p|LITERAL %d\n", pattern, + ctx->ptr, *pattern)); + if (ctx->ptr >= end || (SRE_CODE) ctx->ptr[0] != pattern[0]) RETURN_FAILURE; - ctx->pattern++; + pattern++; ctx->ptr++; DISPATCH(); TARGET(SRE_OP_NOT_LITERAL): /* match anything that is not literal character */ /* */ - TRACE(("|%p|%p|NOT_LITERAL %d\n", ctx->pattern, - ctx->ptr, *ctx->pattern)); - if (ctx->ptr >= end || (SRE_CODE) ctx->ptr[0] == ctx->pattern[0]) + TRACE(("|%p|%p|NOT_LITERAL %d\n", pattern, + ctx->ptr, *pattern)); + if (ctx->ptr >= end || (SRE_CODE) ctx->ptr[0] == pattern[0]) RETURN_FAILURE; - ctx->pattern++; + pattern++; ctx->ptr++; DISPATCH(); TARGET(SRE_OP_SUCCESS): /* end of pattern */ - TRACE(("|%p|%p|SUCCESS\n", ctx->pattern, ctx->ptr)); + TRACE(("|%p|%p|SUCCESS\n", pattern, ctx->ptr)); if (ctx->toplevel && ((state->match_all && ctx->ptr != state->end) || (state->must_advance && ctx->ptr == state->start))) @@ -910,27 +914,27 @@ static void *sre_op_targets[256] = { TARGET(SRE_OP_AT): /* match at given position */ /* */ - TRACE(("|%p|%p|AT %d\n", ctx->pattern, ctx->ptr, *ctx->pattern)); - if (!SRE(at)(state, ctx->ptr, *ctx->pattern)) + TRACE(("|%p|%p|AT %d\n", pattern, ctx->ptr, *pattern)); + if (!SRE(at)(state, ctx->ptr, *pattern)) RETURN_FAILURE; - ctx->pattern++; + pattern++; DISPATCH(); TARGET(SRE_OP_CATEGORY): /* match at given category */ /* */ - TRACE(("|%p|%p|CATEGORY %d\n", ctx->pattern, - ctx->ptr, *ctx->pattern)); - if (ctx->ptr >= end || !sre_category(ctx->pattern[0], ctx->ptr[0])) + TRACE(("|%p|%p|CATEGORY %d\n", pattern, + ctx->ptr, *pattern)); + if (ctx->ptr >= end || !sre_category(pattern[0], ctx->ptr[0])) RETURN_FAILURE; - ctx->pattern++; + pattern++; ctx->ptr++; DISPATCH(); TARGET(SRE_OP_ANY): /* match anything (except a newline) */ /* */ - TRACE(("|%p|%p|ANY\n", ctx->pattern, ctx->ptr)); + TRACE(("|%p|%p|ANY\n", pattern, ctx->ptr)); if (ctx->ptr >= end || SRE_IS_LINEBREAK(ctx->ptr[0])) RETURN_FAILURE; ctx->ptr++; @@ -939,7 +943,7 @@ static void *sre_op_targets[256] = { TARGET(SRE_OP_ANY_ALL): /* match anything */ /* */ - TRACE(("|%p|%p|ANY_ALL\n", ctx->pattern, ctx->ptr)); + TRACE(("|%p|%p|ANY_ALL\n", pattern, ctx->ptr)); if (ctx->ptr >= end) RETURN_FAILURE; ctx->ptr++; @@ -948,100 +952,100 @@ static void *sre_op_targets[256] = { TARGET(SRE_OP_IN): /* match set member (or non_member) */ /* */ - TRACE(("|%p|%p|IN\n", ctx->pattern, ctx->ptr)); + TRACE(("|%p|%p|IN\n", pattern, ctx->ptr)); if (ctx->ptr >= end || - !SRE(charset)(state, ctx->pattern + 1, *ctx->ptr)) + !SRE(charset)(state, pattern + 1, *ctx->ptr)) RETURN_FAILURE; - ctx->pattern += ctx->pattern[0]; + pattern += pattern[0]; ctx->ptr++; DISPATCH(); TARGET(SRE_OP_LITERAL_IGNORE): TRACE(("|%p|%p|LITERAL_IGNORE %d\n", - ctx->pattern, ctx->ptr, ctx->pattern[0])); + pattern, ctx->ptr, pattern[0])); if (ctx->ptr >= end || - sre_lower_ascii(*ctx->ptr) != *ctx->pattern) + sre_lower_ascii(*ctx->ptr) != *pattern) RETURN_FAILURE; - ctx->pattern++; + pattern++; ctx->ptr++; DISPATCH(); TARGET(SRE_OP_LITERAL_UNI_IGNORE): TRACE(("|%p|%p|LITERAL_UNI_IGNORE %d\n", - ctx->pattern, ctx->ptr, ctx->pattern[0])); + pattern, ctx->ptr, pattern[0])); if (ctx->ptr >= end || - sre_lower_unicode(*ctx->ptr) != *ctx->pattern) + sre_lower_unicode(*ctx->ptr) != *pattern) RETURN_FAILURE; - ctx->pattern++; + pattern++; ctx->ptr++; DISPATCH(); TARGET(SRE_OP_LITERAL_LOC_IGNORE): TRACE(("|%p|%p|LITERAL_LOC_IGNORE %d\n", - ctx->pattern, ctx->ptr, ctx->pattern[0])); + pattern, ctx->ptr, pattern[0])); if (ctx->ptr >= end - || !char_loc_ignore(*ctx->pattern, *ctx->ptr)) + || !char_loc_ignore(*pattern, *ctx->ptr)) RETURN_FAILURE; - ctx->pattern++; + pattern++; ctx->ptr++; DISPATCH(); TARGET(SRE_OP_NOT_LITERAL_IGNORE): TRACE(("|%p|%p|NOT_LITERAL_IGNORE %d\n", - ctx->pattern, ctx->ptr, *ctx->pattern)); + pattern, ctx->ptr, *pattern)); if (ctx->ptr >= end || - sre_lower_ascii(*ctx->ptr) == *ctx->pattern) + sre_lower_ascii(*ctx->ptr) == *pattern) RETURN_FAILURE; - ctx->pattern++; + pattern++; ctx->ptr++; DISPATCH(); TARGET(SRE_OP_NOT_LITERAL_UNI_IGNORE): TRACE(("|%p|%p|NOT_LITERAL_UNI_IGNORE %d\n", - ctx->pattern, ctx->ptr, *ctx->pattern)); + pattern, ctx->ptr, *pattern)); if (ctx->ptr >= end || - sre_lower_unicode(*ctx->ptr) == *ctx->pattern) + sre_lower_unicode(*ctx->ptr) == *pattern) RETURN_FAILURE; - ctx->pattern++; + pattern++; ctx->ptr++; DISPATCH(); TARGET(SRE_OP_NOT_LITERAL_LOC_IGNORE): TRACE(("|%p|%p|NOT_LITERAL_LOC_IGNORE %d\n", - ctx->pattern, ctx->ptr, *ctx->pattern)); + pattern, ctx->ptr, *pattern)); if (ctx->ptr >= end - || char_loc_ignore(*ctx->pattern, *ctx->ptr)) + || char_loc_ignore(*pattern, *ctx->ptr)) RETURN_FAILURE; - ctx->pattern++; + pattern++; ctx->ptr++; DISPATCH(); TARGET(SRE_OP_IN_IGNORE): - TRACE(("|%p|%p|IN_IGNORE\n", ctx->pattern, ctx->ptr)); + TRACE(("|%p|%p|IN_IGNORE\n", pattern, ctx->ptr)); if (ctx->ptr >= end - || !SRE(charset)(state, ctx->pattern+1, + || !SRE(charset)(state, pattern+1, (SRE_CODE)sre_lower_ascii(*ctx->ptr))) RETURN_FAILURE; - ctx->pattern += ctx->pattern[0]; + pattern += pattern[0]; ctx->ptr++; DISPATCH(); TARGET(SRE_OP_IN_UNI_IGNORE): - TRACE(("|%p|%p|IN_UNI_IGNORE\n", ctx->pattern, ctx->ptr)); + TRACE(("|%p|%p|IN_UNI_IGNORE\n", pattern, ctx->ptr)); if (ctx->ptr >= end - || !SRE(charset)(state, ctx->pattern+1, + || !SRE(charset)(state, pattern+1, (SRE_CODE)sre_lower_unicode(*ctx->ptr))) RETURN_FAILURE; - ctx->pattern += ctx->pattern[0]; + pattern += pattern[0]; ctx->ptr++; DISPATCH(); TARGET(SRE_OP_IN_LOC_IGNORE): - TRACE(("|%p|%p|IN_LOC_IGNORE\n", ctx->pattern, ctx->ptr)); + TRACE(("|%p|%p|IN_LOC_IGNORE\n", pattern, ctx->ptr)); if (ctx->ptr >= end - || !SRE(charset_loc_ignore)(state, ctx->pattern+1, *ctx->ptr)) + || !SRE(charset_loc_ignore)(state, pattern+1, *ctx->ptr)) RETURN_FAILURE; - ctx->pattern += ctx->pattern[0]; + pattern += pattern[0]; ctx->ptr++; DISPATCH(); @@ -1049,30 +1053,30 @@ static void *sre_op_targets[256] = { TARGET(SRE_OP_INFO): /* jump forward */ /* */ - TRACE(("|%p|%p|JUMP %d\n", ctx->pattern, - ctx->ptr, ctx->pattern[0])); - ctx->pattern += ctx->pattern[0]; + TRACE(("|%p|%p|JUMP %d\n", pattern, + ctx->ptr, pattern[0])); + pattern += pattern[0]; DISPATCH(); TARGET(SRE_OP_BRANCH): /* alternation */ /* <0=skip> code ... */ - TRACE(("|%p|%p|BRANCH\n", ctx->pattern, ctx->ptr)); + TRACE(("|%p|%p|BRANCH\n", pattern, ctx->ptr)); LASTMARK_SAVE(); if (state->repeat) MARK_PUSH(ctx->lastmark); - for (; ctx->pattern[0]; ctx->pattern += ctx->pattern[0]) { - if (ctx->pattern[1] == SRE_OP_LITERAL && + for (; pattern[0]; pattern += pattern[0]) { + if (pattern[1] == SRE_OP_LITERAL && (ctx->ptr >= end || - (SRE_CODE) *ctx->ptr != ctx->pattern[2])) + (SRE_CODE) *ctx->ptr != pattern[2])) continue; - if (ctx->pattern[1] == SRE_OP_IN && + if (pattern[1] == SRE_OP_IN && (ctx->ptr >= end || - !SRE(charset)(state, ctx->pattern + 3, + !SRE(charset)(state, pattern + 3, (SRE_CODE) *ctx->ptr))) continue; state->ptr = ctx->ptr; - DO_JUMP(JUMP_BRANCH, jump_branch, ctx->pattern+1); + DO_JUMP(JUMP_BRANCH, jump_branch, pattern+1); if (ret) { if (state->repeat) MARK_POP_DISCARD(ctx->lastmark); @@ -1097,15 +1101,15 @@ static void *sre_op_targets[256] = { /* <1=min> <2=max> item tail */ - TRACE(("|%p|%p|REPEAT_ONE %d %d\n", ctx->pattern, ctx->ptr, - ctx->pattern[1], ctx->pattern[2])); + TRACE(("|%p|%p|REPEAT_ONE %d %d\n", pattern, ctx->ptr, + pattern[1], pattern[2])); - if ((Py_ssize_t) ctx->pattern[1] > end - ctx->ptr) + if ((Py_ssize_t) pattern[1] > end - ctx->ptr) RETURN_FAILURE; /* cannot match */ state->ptr = ctx->ptr; - ret = SRE(count)(state, ctx->pattern+3, ctx->pattern[2]); + ret = SRE(count)(state, pattern+3, pattern[2]); RETURN_ON_ERROR(ret); DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos); ctx->count = ret; @@ -1116,10 +1120,10 @@ static void *sre_op_targets[256] = { string. check if the rest of the pattern matches, and backtrack if not. */ - if (ctx->count < (Py_ssize_t) ctx->pattern[1]) + if (ctx->count < (Py_ssize_t) pattern[1]) RETURN_FAILURE; - if (ctx->pattern[ctx->pattern[0]] == SRE_OP_SUCCESS && + if (pattern[pattern[0]] == SRE_OP_SUCCESS && ctx->ptr == state->end && !(ctx->toplevel && state->must_advance && ctx->ptr == state->start)) { @@ -1132,21 +1136,21 @@ static void *sre_op_targets[256] = { if (state->repeat) MARK_PUSH(ctx->lastmark); - if (ctx->pattern[ctx->pattern[0]] == SRE_OP_LITERAL) { + if (pattern[pattern[0]] == SRE_OP_LITERAL) { /* tail starts with a literal. skip positions where the rest of the pattern cannot possibly match */ - ctx->u.chr = ctx->pattern[ctx->pattern[0]+1]; + ctx->u.chr = pattern[pattern[0]+1]; for (;;) { - while (ctx->count >= (Py_ssize_t) ctx->pattern[1] && + while (ctx->count >= (Py_ssize_t) pattern[1] && (ctx->ptr >= end || *ctx->ptr != ctx->u.chr)) { ctx->ptr--; ctx->count--; } - if (ctx->count < (Py_ssize_t) ctx->pattern[1]) + if (ctx->count < (Py_ssize_t) pattern[1]) break; state->ptr = ctx->ptr; DO_JUMP(JUMP_REPEAT_ONE_1, jump_repeat_one_1, - ctx->pattern+ctx->pattern[0]); + pattern+pattern[0]); if (ret) { if (state->repeat) MARK_POP_DISCARD(ctx->lastmark); @@ -1164,10 +1168,10 @@ static void *sre_op_targets[256] = { MARK_POP_DISCARD(ctx->lastmark); } else { /* general case */ - while (ctx->count >= (Py_ssize_t) ctx->pattern[1]) { + while (ctx->count >= (Py_ssize_t) pattern[1]) { state->ptr = ctx->ptr; DO_JUMP(JUMP_REPEAT_ONE_2, jump_repeat_one_2, - ctx->pattern+ctx->pattern[0]); + pattern+pattern[0]); if (ret) { if (state->repeat) MARK_POP_DISCARD(ctx->lastmark); @@ -1196,22 +1200,22 @@ static void *sre_op_targets[256] = { /* <1=min> <2=max> item tail */ - TRACE(("|%p|%p|MIN_REPEAT_ONE %d %d\n", ctx->pattern, ctx->ptr, - ctx->pattern[1], ctx->pattern[2])); + TRACE(("|%p|%p|MIN_REPEAT_ONE %d %d\n", pattern, ctx->ptr, + pattern[1], pattern[2])); - if ((Py_ssize_t) ctx->pattern[1] > end - ctx->ptr) + if ((Py_ssize_t) pattern[1] > end - ctx->ptr) RETURN_FAILURE; /* cannot match */ state->ptr = ctx->ptr; - if (ctx->pattern[1] == 0) + if (pattern[1] == 0) ctx->count = 0; else { /* count using pattern min as the maximum */ - ret = SRE(count)(state, ctx->pattern+3, ctx->pattern[1]); + ret = SRE(count)(state, pattern+3, pattern[1]); RETURN_ON_ERROR(ret); DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos); - if (ret < (Py_ssize_t) ctx->pattern[1]) + if (ret < (Py_ssize_t) pattern[1]) /* didn't match minimum number of times */ RETURN_FAILURE; /* advance past minimum matches of repeat */ @@ -1219,7 +1223,7 @@ static void *sre_op_targets[256] = { ctx->ptr += ctx->count; } - if (ctx->pattern[ctx->pattern[0]] == SRE_OP_SUCCESS && + if (pattern[pattern[0]] == SRE_OP_SUCCESS && !(ctx->toplevel && ((state->match_all && ctx->ptr != state->end) || (state->must_advance && ctx->ptr == state->start)))) @@ -1234,11 +1238,11 @@ static void *sre_op_targets[256] = { if (state->repeat) MARK_PUSH(ctx->lastmark); - while ((Py_ssize_t)ctx->pattern[2] == SRE_MAXREPEAT - || ctx->count <= (Py_ssize_t)ctx->pattern[2]) { + while ((Py_ssize_t)pattern[2] == SRE_MAXREPEAT + || ctx->count <= (Py_ssize_t)pattern[2]) { state->ptr = ctx->ptr; DO_JUMP(JUMP_MIN_REPEAT_ONE,jump_min_repeat_one, - ctx->pattern+ctx->pattern[0]); + pattern+pattern[0]); if (ret) { if (state->repeat) MARK_POP_DISCARD(ctx->lastmark); @@ -1250,7 +1254,7 @@ static void *sre_op_targets[256] = { LASTMARK_RESTORE(); state->ptr = ctx->ptr; - ret = SRE(count)(state, ctx->pattern+3, 1); + ret = SRE(count)(state, pattern+3, 1); RETURN_ON_ERROR(ret); DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos); if (ret == 0) @@ -1276,16 +1280,16 @@ static void *sre_op_targets[256] = { /* <1=min> <2=max> item tail */ - TRACE(("|%p|%p|POSSESSIVE_REPEAT_ONE %d %d\n", ctx->pattern, - ctx->ptr, ctx->pattern[1], ctx->pattern[2])); + TRACE(("|%p|%p|POSSESSIVE_REPEAT_ONE %d %d\n", pattern, + ctx->ptr, pattern[1], pattern[2])); - if (ctx->ptr + ctx->pattern[1] > end) { + if (ctx->ptr + pattern[1] > end) { RETURN_FAILURE; /* cannot match */ } state->ptr = ctx->ptr; - ret = SRE(count)(state, ctx->pattern + 3, ctx->pattern[2]); + ret = SRE(count)(state, pattern + 3, pattern[2]); RETURN_ON_ERROR(ret); DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos); ctx->count = ret; @@ -1297,16 +1301,16 @@ static void *sre_op_targets[256] = { and fail if not. */ /* Test for not enough repetitions in match */ - if (ctx->count < (Py_ssize_t) ctx->pattern[1]) { + if (ctx->count < (Py_ssize_t) pattern[1]) { RETURN_FAILURE; } /* Update the pattern to point to the next op code */ - ctx->pattern += ctx->pattern[0]; + pattern += pattern[0]; /* Let the tail be evaluated separately and consider this match successful. */ - if (*ctx->pattern == SRE_OP_SUCCESS && + if (*pattern == SRE_OP_SUCCESS && ctx->ptr == state->end && !(ctx->toplevel && state->must_advance && ctx->ptr == state->start)) { @@ -1323,20 +1327,20 @@ static void *sre_op_targets[256] = { by the UNTIL operator (MAX_UNTIL, MIN_UNTIL) */ /* <1=min> <2=max> <3=repeat_index> item tail */ - TRACE(("|%p|%p|REPEAT %d %d %d\n", ctx->pattern, ctx->ptr, - ctx->pattern[1], ctx->pattern[2], ctx->pattern[3])); + TRACE(("|%p|%p|REPEAT %d %d %d\n", pattern, ctx->ptr, + pattern[1], pattern[2], pattern[3])); /* install repeat context */ - ctx->u.rep = &state->repeats_array[ctx->pattern[3]]; + ctx->u.rep = &state->repeats_array[pattern[3]]; ctx->u.rep->count = -1; - ctx->u.rep->pattern = ctx->pattern; + ctx->u.rep->pattern = pattern; ctx->u.rep->prev = state->repeat; ctx->u.rep->last_ptr = NULL; state->repeat = ctx->u.rep; state->ptr = ctx->ptr; - DO_JUMP(JUMP_REPEAT, jump_repeat, ctx->pattern+ctx->pattern[0]); + DO_JUMP(JUMP_REPEAT, jump_repeat, pattern+pattern[0]); state->repeat = ctx->u.rep->prev; if (ret) { @@ -1361,7 +1365,7 @@ static void *sre_op_targets[256] = { ctx->count = ctx->u.rep->count+1; - TRACE(("|%p|%p|MAX_UNTIL %zd\n", ctx->pattern, + TRACE(("|%p|%p|MAX_UNTIL %zd\n", pattern, ctx->ptr, ctx->count)); if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) { @@ -1406,7 +1410,7 @@ static void *sre_op_targets[256] = { /* cannot match more repeated items here. make sure the tail matches */ state->repeat = ctx->u.rep->prev; - DO_JUMP(JUMP_MAX_UNTIL_3, jump_max_until_3, ctx->pattern); + DO_JUMP(JUMP_MAX_UNTIL_3, jump_max_until_3, pattern); state->repeat = ctx->u.rep; // restore repeat before return RETURN_ON_SUCCESS(ret); @@ -1426,7 +1430,7 @@ static void *sre_op_targets[256] = { ctx->count = ctx->u.rep->count+1; - TRACE(("|%p|%p|MIN_UNTIL %zd %p\n", ctx->pattern, + TRACE(("|%p|%p|MIN_UNTIL %zd %p\n", pattern, ctx->ptr, ctx->count, ctx->u.rep->pattern)); if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) { @@ -1450,7 +1454,7 @@ static void *sre_op_targets[256] = { if (state->repeat) MARK_PUSH(ctx->lastmark); - DO_JUMP(JUMP_MIN_UNTIL_2, jump_min_until_2, ctx->pattern); + DO_JUMP(JUMP_MIN_UNTIL_2, jump_min_until_2, pattern); SRE_REPEAT *repeat_of_tail = state->repeat; state->repeat = ctx->u.rep; // restore repeat before return @@ -1490,8 +1494,8 @@ static void *sre_op_targets[256] = { /* create possessive repeat contexts. */ /* <1=min> <2=max> pattern tail */ - TRACE(("|%p|%p|POSSESSIVE_REPEAT %d %d\n", ctx->pattern, - ctx->ptr, ctx->pattern[1], ctx->pattern[2])); + TRACE(("|%p|%p|POSSESSIVE_REPEAT %d %d\n", pattern, + ctx->ptr, pattern[1], pattern[2])); /* Set the global Input pointer to this context's Input pointer */ @@ -1501,10 +1505,10 @@ static void *sre_op_targets[256] = { ctx->count = 0; /* Check for minimum required matches. */ - while (ctx->count < (Py_ssize_t)ctx->pattern[1]) { + while (ctx->count < (Py_ssize_t)pattern[1]) { /* not enough matches */ DO_JUMP(JUMP_POSS_REPEAT_1, jump_poss_repeat_1, - &ctx->pattern[3]); + &pattern[3]); if (ret) { RETURN_ON_ERROR(ret); ctx->count++; @@ -1522,8 +1526,8 @@ static void *sre_op_targets[256] = { /* Keep trying to parse the sub-pattern until the end is reached, creating a new context each time. */ - while ((ctx->count < (Py_ssize_t)ctx->pattern[2] || - (Py_ssize_t)ctx->pattern[2] == SRE_MAXREPEAT) && + while ((ctx->count < (Py_ssize_t)pattern[2] || + (Py_ssize_t)pattern[2] == SRE_MAXREPEAT) && state->ptr != ctx->ptr) { /* Save the Capture Group Marker state into the current Context and back up the current highest number @@ -1551,7 +1555,7 @@ static void *sre_op_targets[256] = { /* We have not reached the maximin matches, so try to match once more. */ DO_JUMP(JUMP_POSS_REPEAT_2, jump_poss_repeat_2, - &ctx->pattern[3]); + &pattern[3]); /* Check to see if the last attempted match succeeded. */ @@ -1582,14 +1586,14 @@ static void *sre_op_targets[256] = { /* Evaluate Tail */ /* Jump to end of pattern indicated by skip, and then skip the SUCCESS op code that follows it. */ - ctx->pattern += ctx->pattern[0] + 1; + pattern += pattern[0] + 1; ctx->ptr = state->ptr; DISPATCH(); TARGET(SRE_OP_ATOMIC_GROUP): /* Atomic Group Sub Pattern */ /* pattern tail */ - TRACE(("|%p|%p|ATOMIC_GROUP\n", ctx->pattern, ctx->ptr)); + TRACE(("|%p|%p|ATOMIC_GROUP\n", pattern, ctx->ptr)); /* Set the global Input pointer to this context's Input pointer */ @@ -1600,7 +1604,7 @@ static void *sre_op_targets[256] = { code, is reached. */ /* Group Pattern begins at an offset of 1 code. */ DO_JUMP(JUMP_ATOMIC_GROUP, jump_atomic_group, - &ctx->pattern[1]); + &pattern[1]); /* Test Exit Condition */ RETURN_ON_ERROR(ret); @@ -1614,15 +1618,15 @@ static void *sre_op_targets[256] = { /* Evaluate Tail */ /* Jump to end of pattern indicated by skip, and then skip the SUCCESS op code that follows it. */ - ctx->pattern += ctx->pattern[0]; + pattern += pattern[0]; ctx->ptr = state->ptr; DISPATCH(); TARGET(SRE_OP_GROUPREF): /* match backreference */ - TRACE(("|%p|%p|GROUPREF %d\n", ctx->pattern, - ctx->ptr, ctx->pattern[0])); - i = ctx->pattern[0]; + TRACE(("|%p|%p|GROUPREF %d\n", pattern, + ctx->ptr, pattern[0])); + i = pattern[0]; { Py_ssize_t groupref = i+i; if (groupref >= state->lastmark) { @@ -1640,14 +1644,14 @@ static void *sre_op_targets[256] = { } } } - ctx->pattern++; + pattern++; DISPATCH(); TARGET(SRE_OP_GROUPREF_IGNORE): /* match backreference */ - TRACE(("|%p|%p|GROUPREF_IGNORE %d\n", ctx->pattern, - ctx->ptr, ctx->pattern[0])); - i = ctx->pattern[0]; + TRACE(("|%p|%p|GROUPREF_IGNORE %d\n", pattern, + ctx->ptr, pattern[0])); + i = pattern[0]; { Py_ssize_t groupref = i+i; if (groupref >= state->lastmark) { @@ -1666,14 +1670,14 @@ static void *sre_op_targets[256] = { } } } - ctx->pattern++; + pattern++; DISPATCH(); TARGET(SRE_OP_GROUPREF_UNI_IGNORE): /* match backreference */ - TRACE(("|%p|%p|GROUPREF_UNI_IGNORE %d\n", ctx->pattern, - ctx->ptr, ctx->pattern[0])); - i = ctx->pattern[0]; + TRACE(("|%p|%p|GROUPREF_UNI_IGNORE %d\n", pattern, + ctx->ptr, pattern[0])); + i = pattern[0]; { Py_ssize_t groupref = i+i; if (groupref >= state->lastmark) { @@ -1692,14 +1696,14 @@ static void *sre_op_targets[256] = { } } } - ctx->pattern++; + pattern++; DISPATCH(); TARGET(SRE_OP_GROUPREF_LOC_IGNORE): /* match backreference */ - TRACE(("|%p|%p|GROUPREF_LOC_IGNORE %d\n", ctx->pattern, - ctx->ptr, ctx->pattern[0])); - i = ctx->pattern[0]; + TRACE(("|%p|%p|GROUPREF_LOC_IGNORE %d\n", pattern, + ctx->ptr, pattern[0])); + i = pattern[0]; { Py_ssize_t groupref = i+i; if (groupref >= state->lastmark) { @@ -1718,56 +1722,56 @@ static void *sre_op_targets[256] = { } } } - ctx->pattern++; + pattern++; DISPATCH(); TARGET(SRE_OP_GROUPREF_EXISTS): - TRACE(("|%p|%p|GROUPREF_EXISTS %d\n", ctx->pattern, - ctx->ptr, ctx->pattern[0])); + TRACE(("|%p|%p|GROUPREF_EXISTS %d\n", pattern, + ctx->ptr, pattern[0])); /* codeyes codeno ... */ - i = ctx->pattern[0]; + i = pattern[0]; { Py_ssize_t groupref = i+i; if (groupref >= state->lastmark) { - ctx->pattern += ctx->pattern[1]; + pattern += pattern[1]; DISPATCH(); } else { SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref]; SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1]; if (!p || !e || e < p) { - ctx->pattern += ctx->pattern[1]; + pattern += pattern[1]; DISPATCH(); } } } - ctx->pattern += 2; + pattern += 2; DISPATCH(); TARGET(SRE_OP_ASSERT): /* assert subpattern */ /* */ - TRACE(("|%p|%p|ASSERT %d\n", ctx->pattern, - ctx->ptr, ctx->pattern[1])); - if (ctx->ptr - (SRE_CHAR *)state->beginning < (Py_ssize_t)ctx->pattern[1]) + TRACE(("|%p|%p|ASSERT %d\n", pattern, + ctx->ptr, pattern[1])); + if (ctx->ptr - (SRE_CHAR *)state->beginning < (Py_ssize_t)pattern[1]) RETURN_FAILURE; - state->ptr = ctx->ptr - ctx->pattern[1]; - DO_JUMP0(JUMP_ASSERT, jump_assert, ctx->pattern+2); + state->ptr = ctx->ptr - pattern[1]; + DO_JUMP0(JUMP_ASSERT, jump_assert, pattern+2); RETURN_ON_FAILURE(ret); - ctx->pattern += ctx->pattern[0]; + pattern += pattern[0]; DISPATCH(); TARGET(SRE_OP_ASSERT_NOT): /* assert not subpattern */ /* */ - TRACE(("|%p|%p|ASSERT_NOT %d\n", ctx->pattern, - ctx->ptr, ctx->pattern[1])); - if (ctx->ptr - (SRE_CHAR *)state->beginning >= (Py_ssize_t)ctx->pattern[1]) { - state->ptr = ctx->ptr - ctx->pattern[1]; + TRACE(("|%p|%p|ASSERT_NOT %d\n", pattern, + ctx->ptr, pattern[1])); + if (ctx->ptr - (SRE_CHAR *)state->beginning >= (Py_ssize_t)pattern[1]) { + state->ptr = ctx->ptr - pattern[1]; LASTMARK_SAVE(); if (state->repeat) MARK_PUSH(ctx->lastmark); - DO_JUMP0(JUMP_ASSERT_NOT, jump_assert_not, ctx->pattern+2); + DO_JUMP0(JUMP_ASSERT_NOT, jump_assert_not, pattern+2); if (ret) { if (state->repeat) MARK_POP_DISCARD(ctx->lastmark); @@ -1778,12 +1782,12 @@ static void *sre_op_targets[256] = { MARK_POP(ctx->lastmark); LASTMARK_RESTORE(); } - ctx->pattern += ctx->pattern[0]; + pattern += pattern[0]; DISPATCH(); TARGET(SRE_OP_FAILURE): /* immediate failure */ - TRACE(("|%p|%p|FAILURE\n", ctx->pattern, ctx->ptr)); + TRACE(("|%p|%p|FAILURE\n", pattern, ctx->ptr)); RETURN_FAILURE; #if USE_COMPUTED_GOTOS @@ -1791,8 +1795,8 @@ static void *sre_op_targets[256] = { #else default: #endif - TRACE(("|%p|%p|UNKNOWN %d\n", ctx->pattern, ctx->ptr, - ctx->pattern[-1])); + TRACE(("|%p|%p|UNKNOWN %d\n", pattern, ctx->ptr, + pattern[-1])); RETURN_ERROR(SRE_ERROR_ILLEGAL); } } @@ -1807,55 +1811,55 @@ static void *sre_op_targets[256] = { switch (jump) { case JUMP_MAX_UNTIL_2: - TRACE(("|%p|%p|JUMP_MAX_UNTIL_2\n", ctx->pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_MAX_UNTIL_2\n", pattern, ctx->ptr)); goto jump_max_until_2; case JUMP_MAX_UNTIL_3: - TRACE(("|%p|%p|JUMP_MAX_UNTIL_3\n", ctx->pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_MAX_UNTIL_3\n", pattern, ctx->ptr)); goto jump_max_until_3; case JUMP_MIN_UNTIL_2: - TRACE(("|%p|%p|JUMP_MIN_UNTIL_2\n", ctx->pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_MIN_UNTIL_2\n", pattern, ctx->ptr)); goto jump_min_until_2; case JUMP_MIN_UNTIL_3: - TRACE(("|%p|%p|JUMP_MIN_UNTIL_3\n", ctx->pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_MIN_UNTIL_3\n", pattern, ctx->ptr)); goto jump_min_until_3; case JUMP_BRANCH: - TRACE(("|%p|%p|JUMP_BRANCH\n", ctx->pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_BRANCH\n", pattern, ctx->ptr)); goto jump_branch; case JUMP_MAX_UNTIL_1: - TRACE(("|%p|%p|JUMP_MAX_UNTIL_1\n", ctx->pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_MAX_UNTIL_1\n", pattern, ctx->ptr)); goto jump_max_until_1; case JUMP_MIN_UNTIL_1: - TRACE(("|%p|%p|JUMP_MIN_UNTIL_1\n", ctx->pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_MIN_UNTIL_1\n", pattern, ctx->ptr)); goto jump_min_until_1; case JUMP_POSS_REPEAT_1: - TRACE(("|%p|%p|JUMP_POSS_REPEAT_1\n", ctx->pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_POSS_REPEAT_1\n", pattern, ctx->ptr)); goto jump_poss_repeat_1; case JUMP_POSS_REPEAT_2: - TRACE(("|%p|%p|JUMP_POSS_REPEAT_2\n", ctx->pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_POSS_REPEAT_2\n", pattern, ctx->ptr)); goto jump_poss_repeat_2; case JUMP_REPEAT: - TRACE(("|%p|%p|JUMP_REPEAT\n", ctx->pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_REPEAT\n", pattern, ctx->ptr)); goto jump_repeat; case JUMP_REPEAT_ONE_1: - TRACE(("|%p|%p|JUMP_REPEAT_ONE_1\n", ctx->pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_REPEAT_ONE_1\n", pattern, ctx->ptr)); goto jump_repeat_one_1; case JUMP_REPEAT_ONE_2: - TRACE(("|%p|%p|JUMP_REPEAT_ONE_2\n", ctx->pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_REPEAT_ONE_2\n", pattern, ctx->ptr)); goto jump_repeat_one_2; case JUMP_MIN_REPEAT_ONE: - TRACE(("|%p|%p|JUMP_MIN_REPEAT_ONE\n", ctx->pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_MIN_REPEAT_ONE\n", pattern, ctx->ptr)); goto jump_min_repeat_one; case JUMP_ATOMIC_GROUP: - TRACE(("|%p|%p|JUMP_ATOMIC_GROUP\n", ctx->pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_ATOMIC_GROUP\n", pattern, ctx->ptr)); goto jump_atomic_group; case JUMP_ASSERT: - TRACE(("|%p|%p|JUMP_ASSERT\n", ctx->pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_ASSERT\n", pattern, ctx->ptr)); goto jump_assert; case JUMP_ASSERT_NOT: - TRACE(("|%p|%p|JUMP_ASSERT_NOT\n", ctx->pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_ASSERT_NOT\n", pattern, ctx->ptr)); goto jump_assert_not; case JUMP_NONE: - TRACE(("|%p|%p|RETURN %zd\n", ctx->pattern, + TRACE(("|%p|%p|RETURN %zd\n", pattern, ctx->ptr, ret)); break; } From fa98784f278b5d350ee3d10124ab106ce51a18e0 Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Tue, 12 Apr 2022 13:36:12 -0700 Subject: [PATCH 3/9] More of that --- Modules/_sre/sre_lib.h | 368 +++++++++++++++++++++-------------------- 1 file changed, 185 insertions(+), 183 deletions(-) diff --git a/Modules/_sre/sre_lib.h b/Modules/_sre/sre_lib.h index 4c872531b2e7db..2106ca7e202c30 100644 --- a/Modules/_sre/sre_lib.h +++ b/Modules/_sre/sre_lib.h @@ -485,19 +485,20 @@ do { \ #define JUMP_ATOMIC_GROUP 16 #define DO_JUMPX(jumpvalue, jumplabel, nextpattern, toplevel_) \ + ctx->pattern = pattern; \ + ctx->ptr = ptr;\ DATA_ALLOC(SRE(match_context), nextctx); \ nextctx->last_ctx_pos = ctx_pos; \ nextctx->jump = jumpvalue; \ nextctx->pattern = nextpattern; \ nextctx->toplevel = toplevel_; \ - ctx->pattern = pattern; \ pattern = nextpattern; \ ctx_pos = alloc_pos; \ ctx = nextctx; \ goto entrance; \ jumplabel: \ pattern = ctx->pattern; \ - while (0) /* gcc doesn't like labels at end of scopes */ \ + ptr = ctx->ptr; #define DO_JUMP(jumpvalue, jumplabel, nextpattern) \ DO_JUMPX(jumpvalue, jumplabel, nextpattern, ctx->toplevel) @@ -520,14 +521,6 @@ typedef struct { int toplevel; } SRE(match_context); -#define DO_STUFF() \ -do { \ - if ((0 == (++sigcount & 0xfff)) && PyErr_CheckSignals()) { \ - RETURN_ERROR(SRE_ERROR_INTERRUPTED); \ - } \ - ctx->pattern = pattern + 1; \ -} while (0) - #ifdef HAVE_COMPUTED_GOTOS #ifndef USE_COMPUTED_GOTOS #define USE_COMPUTED_GOTOS 1 @@ -540,9 +533,19 @@ do { \ #define USE_COMPUTED_GOTOS 0 #endif +#define CHECK_SIGNALS() \ +do { \ + if ((0 == (++sigcount & 0xfff)) && PyErr_CheckSignals()) { \ + RETURN_ERROR(SRE_ERROR_INTERRUPTED); \ + } \ +} while (0) + #if USE_COMPUTED_GOTOS #define TARGET(OP) LABEL_ ## OP -#define DISPATCH() do { DO_STUFF(); goto *sre_op_targets[*pattern++]; } while (0) +#define DISPATCH() do { \ + CHECK_SIGNALS(); \ + goto *sre_op_targets[*pattern++]; \ +} while (0) #else #define TARGET(OP) case OP #define DISPATCH() break @@ -567,20 +570,19 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) DATA_ALLOC(SRE(match_context), ctx); ctx->last_ctx_pos = -1; ctx->jump = JUMP_NONE; - ctx->pattern = pattern; ctx->toplevel = toplevel; ctx_pos = alloc_pos; -entrance: +entrance:; - ctx->ptr = (SRE_CHAR *)state->ptr; + const SRE_CHAR *ptr = (SRE_CHAR *)state->ptr; if (pattern[0] == SRE_OP_INFO) { /* optimization info block */ /* <1=skip> <2=flags> <3=min> ... */ - if (pattern[3] && (uintptr_t)(end - ctx->ptr) < pattern[3]) { + if (pattern[3] && (uintptr_t)(end - ptr) < pattern[3]) { TRACE(("reject (got %zd chars, need %zd)\n", - end - ctx->ptr, (Py_ssize_t) pattern[3])); + end - ptr, (Py_ssize_t) pattern[3])); RETURN_FAILURE; } pattern += pattern[1] + 1; @@ -851,7 +853,7 @@ static void *sre_op_targets[256] = { #if USE_COMPUTED_GOTOS DISPATCH(); #else - DO_STUFF(); + CHECK_SIGNALS(); switch (*pattern++) #endif { @@ -859,7 +861,7 @@ static void *sre_op_targets[256] = { /* set mark */ /* */ TRACE(("|%p|%p|MARK %d\n", pattern, - ctx->ptr, pattern[0])); + ptr, pattern[0])); i = pattern[0]; if (i & 1) state->lastindex = i/2 + 1; @@ -873,7 +875,7 @@ static void *sre_op_targets[256] = { state->mark[j++] = NULL; state->lastmark = i; } - state->mark[i] = ctx->ptr; + state->mark[i] = ptr; pattern++; DISPATCH(); @@ -881,41 +883,41 @@ static void *sre_op_targets[256] = { /* match literal string */ /* */ TRACE(("|%p|%p|LITERAL %d\n", pattern, - ctx->ptr, *pattern)); - if (ctx->ptr >= end || (SRE_CODE) ctx->ptr[0] != pattern[0]) + ptr, *pattern)); + if (ptr >= end || (SRE_CODE) ptr[0] != pattern[0]) RETURN_FAILURE; pattern++; - ctx->ptr++; + ptr++; DISPATCH(); TARGET(SRE_OP_NOT_LITERAL): /* match anything that is not literal character */ /* */ TRACE(("|%p|%p|NOT_LITERAL %d\n", pattern, - ctx->ptr, *pattern)); - if (ctx->ptr >= end || (SRE_CODE) ctx->ptr[0] == pattern[0]) + ptr, *pattern)); + if (ptr >= end || (SRE_CODE) ptr[0] == pattern[0]) RETURN_FAILURE; pattern++; - ctx->ptr++; + ptr++; DISPATCH(); TARGET(SRE_OP_SUCCESS): /* end of pattern */ - TRACE(("|%p|%p|SUCCESS\n", pattern, ctx->ptr)); + TRACE(("|%p|%p|SUCCESS\n", pattern, ptr)); if (ctx->toplevel && - ((state->match_all && ctx->ptr != state->end) || - (state->must_advance && ctx->ptr == state->start))) + ((state->match_all && ptr != state->end) || + (state->must_advance && ptr == state->start))) { RETURN_FAILURE; } - state->ptr = ctx->ptr; + state->ptr = ptr; RETURN_SUCCESS; TARGET(SRE_OP_AT): /* match at given position */ /* */ - TRACE(("|%p|%p|AT %d\n", pattern, ctx->ptr, *pattern)); - if (!SRE(at)(state, ctx->ptr, *pattern)) + TRACE(("|%p|%p|AT %d\n", pattern, ptr, *pattern)); + if (!SRE(at)(state, ptr, *pattern)) RETURN_FAILURE; pattern++; DISPATCH(); @@ -924,129 +926,129 @@ static void *sre_op_targets[256] = { /* match at given category */ /* */ TRACE(("|%p|%p|CATEGORY %d\n", pattern, - ctx->ptr, *pattern)); - if (ctx->ptr >= end || !sre_category(pattern[0], ctx->ptr[0])) + ptr, *pattern)); + if (ptr >= end || !sre_category(pattern[0], ptr[0])) RETURN_FAILURE; pattern++; - ctx->ptr++; + ptr++; DISPATCH(); TARGET(SRE_OP_ANY): /* match anything (except a newline) */ /* */ - TRACE(("|%p|%p|ANY\n", pattern, ctx->ptr)); - if (ctx->ptr >= end || SRE_IS_LINEBREAK(ctx->ptr[0])) + TRACE(("|%p|%p|ANY\n", pattern, ptr)); + if (ptr >= end || SRE_IS_LINEBREAK(ptr[0])) RETURN_FAILURE; - ctx->ptr++; + ptr++; DISPATCH(); TARGET(SRE_OP_ANY_ALL): /* match anything */ /* */ - TRACE(("|%p|%p|ANY_ALL\n", pattern, ctx->ptr)); - if (ctx->ptr >= end) + TRACE(("|%p|%p|ANY_ALL\n", pattern, ptr)); + if (ptr >= end) RETURN_FAILURE; - ctx->ptr++; + ptr++; DISPATCH(); TARGET(SRE_OP_IN): /* match set member (or non_member) */ /* */ - TRACE(("|%p|%p|IN\n", pattern, ctx->ptr)); - if (ctx->ptr >= end || - !SRE(charset)(state, pattern + 1, *ctx->ptr)) + TRACE(("|%p|%p|IN\n", pattern, ptr)); + if (ptr >= end || + !SRE(charset)(state, pattern + 1, *ptr)) RETURN_FAILURE; pattern += pattern[0]; - ctx->ptr++; + ptr++; DISPATCH(); TARGET(SRE_OP_LITERAL_IGNORE): TRACE(("|%p|%p|LITERAL_IGNORE %d\n", - pattern, ctx->ptr, pattern[0])); - if (ctx->ptr >= end || - sre_lower_ascii(*ctx->ptr) != *pattern) + pattern, ptr, pattern[0])); + if (ptr >= end || + sre_lower_ascii(*ptr) != *pattern) RETURN_FAILURE; pattern++; - ctx->ptr++; + ptr++; DISPATCH(); TARGET(SRE_OP_LITERAL_UNI_IGNORE): TRACE(("|%p|%p|LITERAL_UNI_IGNORE %d\n", - pattern, ctx->ptr, pattern[0])); - if (ctx->ptr >= end || - sre_lower_unicode(*ctx->ptr) != *pattern) + pattern, ptr, pattern[0])); + if (ptr >= end || + sre_lower_unicode(*ptr) != *pattern) RETURN_FAILURE; pattern++; - ctx->ptr++; + ptr++; DISPATCH(); TARGET(SRE_OP_LITERAL_LOC_IGNORE): TRACE(("|%p|%p|LITERAL_LOC_IGNORE %d\n", - pattern, ctx->ptr, pattern[0])); - if (ctx->ptr >= end - || !char_loc_ignore(*pattern, *ctx->ptr)) + pattern, ptr, pattern[0])); + if (ptr >= end + || !char_loc_ignore(*pattern, *ptr)) RETURN_FAILURE; pattern++; - ctx->ptr++; + ptr++; DISPATCH(); TARGET(SRE_OP_NOT_LITERAL_IGNORE): TRACE(("|%p|%p|NOT_LITERAL_IGNORE %d\n", - pattern, ctx->ptr, *pattern)); - if (ctx->ptr >= end || - sre_lower_ascii(*ctx->ptr) == *pattern) + pattern, ptr, *pattern)); + if (ptr >= end || + sre_lower_ascii(*ptr) == *pattern) RETURN_FAILURE; pattern++; - ctx->ptr++; + ptr++; DISPATCH(); TARGET(SRE_OP_NOT_LITERAL_UNI_IGNORE): TRACE(("|%p|%p|NOT_LITERAL_UNI_IGNORE %d\n", - pattern, ctx->ptr, *pattern)); - if (ctx->ptr >= end || - sre_lower_unicode(*ctx->ptr) == *pattern) + pattern, ptr, *pattern)); + if (ptr >= end || + sre_lower_unicode(*ptr) == *pattern) RETURN_FAILURE; pattern++; - ctx->ptr++; + ptr++; DISPATCH(); TARGET(SRE_OP_NOT_LITERAL_LOC_IGNORE): TRACE(("|%p|%p|NOT_LITERAL_LOC_IGNORE %d\n", - pattern, ctx->ptr, *pattern)); - if (ctx->ptr >= end - || char_loc_ignore(*pattern, *ctx->ptr)) + pattern, ptr, *pattern)); + if (ptr >= end + || char_loc_ignore(*pattern, *ptr)) RETURN_FAILURE; pattern++; - ctx->ptr++; + ptr++; DISPATCH(); TARGET(SRE_OP_IN_IGNORE): - TRACE(("|%p|%p|IN_IGNORE\n", pattern, ctx->ptr)); - if (ctx->ptr >= end + TRACE(("|%p|%p|IN_IGNORE\n", pattern, ptr)); + if (ptr >= end || !SRE(charset)(state, pattern+1, - (SRE_CODE)sre_lower_ascii(*ctx->ptr))) + (SRE_CODE)sre_lower_ascii(*ptr))) RETURN_FAILURE; pattern += pattern[0]; - ctx->ptr++; + ptr++; DISPATCH(); TARGET(SRE_OP_IN_UNI_IGNORE): - TRACE(("|%p|%p|IN_UNI_IGNORE\n", pattern, ctx->ptr)); - if (ctx->ptr >= end + TRACE(("|%p|%p|IN_UNI_IGNORE\n", pattern, ptr)); + if (ptr >= end || !SRE(charset)(state, pattern+1, - (SRE_CODE)sre_lower_unicode(*ctx->ptr))) + (SRE_CODE)sre_lower_unicode(*ptr))) RETURN_FAILURE; pattern += pattern[0]; - ctx->ptr++; + ptr++; DISPATCH(); TARGET(SRE_OP_IN_LOC_IGNORE): - TRACE(("|%p|%p|IN_LOC_IGNORE\n", pattern, ctx->ptr)); - if (ctx->ptr >= end - || !SRE(charset_loc_ignore)(state, pattern+1, *ctx->ptr)) + TRACE(("|%p|%p|IN_LOC_IGNORE\n", pattern, ptr)); + if (ptr >= end + || !SRE(charset_loc_ignore)(state, pattern+1, *ptr)) RETURN_FAILURE; pattern += pattern[0]; - ctx->ptr++; + ptr++; DISPATCH(); TARGET(SRE_OP_JUMP): @@ -1054,28 +1056,28 @@ static void *sre_op_targets[256] = { /* jump forward */ /* */ TRACE(("|%p|%p|JUMP %d\n", pattern, - ctx->ptr, pattern[0])); + ptr, pattern[0])); pattern += pattern[0]; DISPATCH(); TARGET(SRE_OP_BRANCH): /* alternation */ /* <0=skip> code ... */ - TRACE(("|%p|%p|BRANCH\n", pattern, ctx->ptr)); + TRACE(("|%p|%p|BRANCH\n", pattern, ptr)); LASTMARK_SAVE(); if (state->repeat) MARK_PUSH(ctx->lastmark); for (; pattern[0]; pattern += pattern[0]) { if (pattern[1] == SRE_OP_LITERAL && - (ctx->ptr >= end || - (SRE_CODE) *ctx->ptr != pattern[2])) + (ptr >= end || + (SRE_CODE) *ptr != pattern[2])) continue; if (pattern[1] == SRE_OP_IN && - (ctx->ptr >= end || + (ptr >= end || !SRE(charset)(state, pattern + 3, - (SRE_CODE) *ctx->ptr))) + (SRE_CODE) *ptr))) continue; - state->ptr = ctx->ptr; + state->ptr = ptr; DO_JUMP(JUMP_BRANCH, jump_branch, pattern+1); if (ret) { if (state->repeat) @@ -1101,22 +1103,22 @@ static void *sre_op_targets[256] = { /* <1=min> <2=max> item tail */ - TRACE(("|%p|%p|REPEAT_ONE %d %d\n", pattern, ctx->ptr, + TRACE(("|%p|%p|REPEAT_ONE %d %d\n", pattern, ptr, pattern[1], pattern[2])); - if ((Py_ssize_t) pattern[1] > end - ctx->ptr) + if ((Py_ssize_t) pattern[1] > end - ptr) RETURN_FAILURE; /* cannot match */ - state->ptr = ctx->ptr; + state->ptr = ptr; ret = SRE(count)(state, pattern+3, pattern[2]); RETURN_ON_ERROR(ret); DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos); ctx->count = ret; - ctx->ptr += ctx->count; + ptr += ctx->count; /* when we arrive here, count contains the number of - matches, and ctx->ptr points to the tail of the target + matches, and ptr points to the tail of the target string. check if the rest of the pattern matches, and backtrack if not. */ @@ -1124,11 +1126,11 @@ static void *sre_op_targets[256] = { RETURN_FAILURE; if (pattern[pattern[0]] == SRE_OP_SUCCESS && - ctx->ptr == state->end && - !(ctx->toplevel && state->must_advance && ctx->ptr == state->start)) + ptr == state->end && + !(ctx->toplevel && state->must_advance && ptr == state->start)) { /* tail is empty. we're finished */ - state->ptr = ctx->ptr; + state->ptr = ptr; RETURN_SUCCESS; } @@ -1142,13 +1144,13 @@ static void *sre_op_targets[256] = { ctx->u.chr = pattern[pattern[0]+1]; for (;;) { while (ctx->count >= (Py_ssize_t) pattern[1] && - (ctx->ptr >= end || *ctx->ptr != ctx->u.chr)) { - ctx->ptr--; + (ptr >= end || *ptr != ctx->u.chr)) { + ptr--; ctx->count--; } if (ctx->count < (Py_ssize_t) pattern[1]) break; - state->ptr = ctx->ptr; + state->ptr = ptr; DO_JUMP(JUMP_REPEAT_ONE_1, jump_repeat_one_1, pattern+pattern[0]); if (ret) { @@ -1161,7 +1163,7 @@ static void *sre_op_targets[256] = { MARK_POP_KEEP(ctx->lastmark); LASTMARK_RESTORE(); - ctx->ptr--; + ptr--; ctx->count--; } if (state->repeat) @@ -1169,7 +1171,7 @@ static void *sre_op_targets[256] = { } else { /* general case */ while (ctx->count >= (Py_ssize_t) pattern[1]) { - state->ptr = ctx->ptr; + state->ptr = ptr; DO_JUMP(JUMP_REPEAT_ONE_2, jump_repeat_one_2, pattern+pattern[0]); if (ret) { @@ -1182,7 +1184,7 @@ static void *sre_op_targets[256] = { MARK_POP_KEEP(ctx->lastmark); LASTMARK_RESTORE(); - ctx->ptr--; + ptr--; ctx->count--; } if (state->repeat) @@ -1200,13 +1202,13 @@ static void *sre_op_targets[256] = { /* <1=min> <2=max> item tail */ - TRACE(("|%p|%p|MIN_REPEAT_ONE %d %d\n", pattern, ctx->ptr, + TRACE(("|%p|%p|MIN_REPEAT_ONE %d %d\n", pattern, ptr, pattern[1], pattern[2])); - if ((Py_ssize_t) pattern[1] > end - ctx->ptr) + if ((Py_ssize_t) pattern[1] > end - ptr) RETURN_FAILURE; /* cannot match */ - state->ptr = ctx->ptr; + state->ptr = ptr; if (pattern[1] == 0) ctx->count = 0; @@ -1220,16 +1222,16 @@ static void *sre_op_targets[256] = { RETURN_FAILURE; /* advance past minimum matches of repeat */ ctx->count = ret; - ctx->ptr += ctx->count; + ptr += ctx->count; } if (pattern[pattern[0]] == SRE_OP_SUCCESS && !(ctx->toplevel && - ((state->match_all && ctx->ptr != state->end) || - (state->must_advance && ctx->ptr == state->start)))) + ((state->match_all && ptr != state->end) || + (state->must_advance && ptr == state->start)))) { /* tail is empty. we're finished */ - state->ptr = ctx->ptr; + state->ptr = ptr; RETURN_SUCCESS; } else { @@ -1240,7 +1242,7 @@ static void *sre_op_targets[256] = { while ((Py_ssize_t)pattern[2] == SRE_MAXREPEAT || ctx->count <= (Py_ssize_t)pattern[2]) { - state->ptr = ctx->ptr; + state->ptr = ptr; DO_JUMP(JUMP_MIN_REPEAT_ONE,jump_min_repeat_one, pattern+pattern[0]); if (ret) { @@ -1253,14 +1255,14 @@ static void *sre_op_targets[256] = { MARK_POP_KEEP(ctx->lastmark); LASTMARK_RESTORE(); - state->ptr = ctx->ptr; + state->ptr = ptr; ret = SRE(count)(state, pattern+3, 1); RETURN_ON_ERROR(ret); DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos); if (ret == 0) break; assert(ret == 1); - ctx->ptr++; + ptr++; ctx->count++; } if (state->repeat) @@ -1281,22 +1283,22 @@ static void *sre_op_targets[256] = { tail */ TRACE(("|%p|%p|POSSESSIVE_REPEAT_ONE %d %d\n", pattern, - ctx->ptr, pattern[1], pattern[2])); + ptr, pattern[1], pattern[2])); - if (ctx->ptr + pattern[1] > end) { + if (ptr + pattern[1] > end) { RETURN_FAILURE; /* cannot match */ } - state->ptr = ctx->ptr; + state->ptr = ptr; ret = SRE(count)(state, pattern + 3, pattern[2]); RETURN_ON_ERROR(ret); DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos); ctx->count = ret; - ctx->ptr += ctx->count; + ptr += ctx->count; /* when we arrive here, count contains the number of - matches, and ctx->ptr points to the tail of the target + matches, and ptr points to the tail of the target string. check if the rest of the pattern matches, and fail if not. */ @@ -1311,11 +1313,11 @@ static void *sre_op_targets[256] = { /* Let the tail be evaluated separately and consider this match successful. */ if (*pattern == SRE_OP_SUCCESS && - ctx->ptr == state->end && - !(ctx->toplevel && state->must_advance && ctx->ptr == state->start)) + ptr == state->end && + !(ctx->toplevel && state->must_advance && ptr == state->start)) { /* tail is empty. we're finished */ - state->ptr = ctx->ptr; + state->ptr = ptr; RETURN_SUCCESS; } @@ -1327,7 +1329,7 @@ static void *sre_op_targets[256] = { by the UNTIL operator (MAX_UNTIL, MIN_UNTIL) */ /* <1=min> <2=max> <3=repeat_index> item tail */ - TRACE(("|%p|%p|REPEAT %d %d %d\n", pattern, ctx->ptr, + TRACE(("|%p|%p|REPEAT %d %d %d\n", pattern, ptr, pattern[1], pattern[2], pattern[3])); /* install repeat context */ @@ -1339,7 +1341,7 @@ static void *sre_op_targets[256] = { ctx->u.rep->last_ptr = NULL; state->repeat = ctx->u.rep; - state->ptr = ctx->ptr; + state->ptr = ptr; DO_JUMP(JUMP_REPEAT, jump_repeat, pattern+pattern[0]); state->repeat = ctx->u.rep->prev; @@ -1361,12 +1363,12 @@ static void *sre_op_targets[256] = { if (!ctx->u.rep) RETURN_ERROR(SRE_ERROR_STATE); - state->ptr = ctx->ptr; + state->ptr = ptr; ctx->count = ctx->u.rep->count+1; TRACE(("|%p|%p|MAX_UNTIL %zd\n", pattern, - ctx->ptr, ctx->count)); + ptr, ctx->count)); if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) { /* not enough matches */ @@ -1378,7 +1380,7 @@ static void *sre_op_targets[256] = { RETURN_SUCCESS; } ctx->u.rep->count = ctx->count-1; - state->ptr = ctx->ptr; + state->ptr = ptr; RETURN_FAILURE; } @@ -1404,7 +1406,7 @@ static void *sre_op_targets[256] = { MARK_POP(ctx->lastmark); LASTMARK_RESTORE(); ctx->u.rep->count = ctx->count-1; - state->ptr = ctx->ptr; + state->ptr = ptr; } /* cannot match more repeated items here. make sure the @@ -1414,7 +1416,7 @@ static void *sre_op_targets[256] = { state->repeat = ctx->u.rep; // restore repeat before return RETURN_ON_SUCCESS(ret); - state->ptr = ctx->ptr; + state->ptr = ptr; RETURN_FAILURE; TARGET(SRE_OP_MIN_UNTIL): @@ -1426,12 +1428,12 @@ static void *sre_op_targets[256] = { if (!ctx->u.rep) RETURN_ERROR(SRE_ERROR_STATE); - state->ptr = ctx->ptr; + state->ptr = ptr; ctx->count = ctx->u.rep->count+1; TRACE(("|%p|%p|MIN_UNTIL %zd %p\n", pattern, - ctx->ptr, ctx->count, ctx->u.rep->pattern)); + ptr, ctx->count, ctx->u.rep->pattern)); if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) { /* not enough matches */ @@ -1443,7 +1445,7 @@ static void *sre_op_targets[256] = { RETURN_SUCCESS; } ctx->u.rep->count = ctx->count-1; - state->ptr = ctx->ptr; + state->ptr = ptr; RETURN_FAILURE; } @@ -1468,7 +1470,7 @@ static void *sre_op_targets[256] = { MARK_POP(ctx->lastmark); LASTMARK_RESTORE(); - state->ptr = ctx->ptr; + state->ptr = ptr; if ((ctx->count >= (Py_ssize_t) ctx->u.rep->pattern[2] && ctx->u.rep->pattern[2] != SRE_MAXREPEAT) || @@ -1487,7 +1489,7 @@ static void *sre_op_targets[256] = { RETURN_SUCCESS; } ctx->u.rep->count = ctx->count-1; - state->ptr = ctx->ptr; + state->ptr = ptr; RETURN_FAILURE; TARGET(SRE_OP_POSSESSIVE_REPEAT): @@ -1495,11 +1497,11 @@ static void *sre_op_targets[256] = { /* <1=min> <2=max> pattern tail */ TRACE(("|%p|%p|POSSESSIVE_REPEAT %d %d\n", pattern, - ctx->ptr, pattern[1], pattern[2])); + ptr, pattern[1], pattern[2])); /* Set the global Input pointer to this context's Input pointer */ - state->ptr = ctx->ptr; + state->ptr = ptr; /* Initialize Count to 0 */ ctx->count = 0; @@ -1514,7 +1516,7 @@ static void *sre_op_targets[256] = { ctx->count++; } else { - state->ptr = ctx->ptr; + state->ptr = ptr; RETURN_FAILURE; } } @@ -1522,13 +1524,13 @@ static void *sre_op_targets[256] = { /* Clear the context's Input stream pointer so that it doesn't match the global state so that the while loop can be entered. */ - ctx->ptr = NULL; + ptr = NULL; /* Keep trying to parse the sub-pattern until the end is reached, creating a new context each time. */ while ((ctx->count < (Py_ssize_t)pattern[2] || (Py_ssize_t)pattern[2] == SRE_MAXREPEAT) && - state->ptr != ctx->ptr) { + state->ptr != ptr) { /* Save the Capture Group Marker state into the current Context and back up the current highest number Capture Group marker. */ @@ -1550,7 +1552,7 @@ static void *sre_op_targets[256] = { maximum number of matches are counted, and because of this, we could immediately stop at that point and consider this match successful. */ - ctx->ptr = state->ptr; + ptr = state->ptr; /* We have not reached the maximin matches, so try to match once more. */ @@ -1587,17 +1589,17 @@ static void *sre_op_targets[256] = { /* Jump to end of pattern indicated by skip, and then skip the SUCCESS op code that follows it. */ pattern += pattern[0] + 1; - ctx->ptr = state->ptr; + ptr = state->ptr; DISPATCH(); TARGET(SRE_OP_ATOMIC_GROUP): /* Atomic Group Sub Pattern */ /* pattern tail */ - TRACE(("|%p|%p|ATOMIC_GROUP\n", pattern, ctx->ptr)); + TRACE(("|%p|%p|ATOMIC_GROUP\n", pattern, ptr)); /* Set the global Input pointer to this context's Input pointer */ - state->ptr = ctx->ptr; + state->ptr = ptr; /* Evaluate the Atomic Group in a new context, terminating when the end of the group, represented by a SUCCESS op @@ -1611,7 +1613,7 @@ static void *sre_op_targets[256] = { if (ret == 0) { /* Atomic Group failed to Match. */ - state->ptr = ctx->ptr; + state->ptr = ptr; RETURN_FAILURE; } @@ -1619,13 +1621,13 @@ static void *sre_op_targets[256] = { /* Jump to end of pattern indicated by skip, and then skip the SUCCESS op code that follows it. */ pattern += pattern[0]; - ctx->ptr = state->ptr; + ptr = state->ptr; DISPATCH(); TARGET(SRE_OP_GROUPREF): /* match backreference */ TRACE(("|%p|%p|GROUPREF %d\n", pattern, - ctx->ptr, pattern[0])); + ptr, pattern[0])); i = pattern[0]; { Py_ssize_t groupref = i+i; @@ -1637,10 +1639,10 @@ static void *sre_op_targets[256] = { if (!p || !e || e < p) RETURN_FAILURE; while (p < e) { - if (ctx->ptr >= end || *ctx->ptr != *p) + if (ptr >= end || *ptr != *p) RETURN_FAILURE; p++; - ctx->ptr++; + ptr++; } } } @@ -1650,7 +1652,7 @@ static void *sre_op_targets[256] = { TARGET(SRE_OP_GROUPREF_IGNORE): /* match backreference */ TRACE(("|%p|%p|GROUPREF_IGNORE %d\n", pattern, - ctx->ptr, pattern[0])); + ptr, pattern[0])); i = pattern[0]; { Py_ssize_t groupref = i+i; @@ -1662,11 +1664,11 @@ static void *sre_op_targets[256] = { if (!p || !e || e < p) RETURN_FAILURE; while (p < e) { - if (ctx->ptr >= end || - sre_lower_ascii(*ctx->ptr) != sre_lower_ascii(*p)) + if (ptr >= end || + sre_lower_ascii(*ptr) != sre_lower_ascii(*p)) RETURN_FAILURE; p++; - ctx->ptr++; + ptr++; } } } @@ -1676,7 +1678,7 @@ static void *sre_op_targets[256] = { TARGET(SRE_OP_GROUPREF_UNI_IGNORE): /* match backreference */ TRACE(("|%p|%p|GROUPREF_UNI_IGNORE %d\n", pattern, - ctx->ptr, pattern[0])); + ptr, pattern[0])); i = pattern[0]; { Py_ssize_t groupref = i+i; @@ -1688,11 +1690,11 @@ static void *sre_op_targets[256] = { if (!p || !e || e < p) RETURN_FAILURE; while (p < e) { - if (ctx->ptr >= end || - sre_lower_unicode(*ctx->ptr) != sre_lower_unicode(*p)) + if (ptr >= end || + sre_lower_unicode(*ptr) != sre_lower_unicode(*p)) RETURN_FAILURE; p++; - ctx->ptr++; + ptr++; } } } @@ -1702,7 +1704,7 @@ static void *sre_op_targets[256] = { TARGET(SRE_OP_GROUPREF_LOC_IGNORE): /* match backreference */ TRACE(("|%p|%p|GROUPREF_LOC_IGNORE %d\n", pattern, - ctx->ptr, pattern[0])); + ptr, pattern[0])); i = pattern[0]; { Py_ssize_t groupref = i+i; @@ -1714,11 +1716,11 @@ static void *sre_op_targets[256] = { if (!p || !e || e < p) RETURN_FAILURE; while (p < e) { - if (ctx->ptr >= end || - sre_lower_locale(*ctx->ptr) != sre_lower_locale(*p)) + if (ptr >= end || + sre_lower_locale(*ptr) != sre_lower_locale(*p)) RETURN_FAILURE; p++; - ctx->ptr++; + ptr++; } } } @@ -1727,7 +1729,7 @@ static void *sre_op_targets[256] = { TARGET(SRE_OP_GROUPREF_EXISTS): TRACE(("|%p|%p|GROUPREF_EXISTS %d\n", pattern, - ctx->ptr, pattern[0])); + ptr, pattern[0])); /* codeyes codeno ... */ i = pattern[0]; { @@ -1751,10 +1753,10 @@ static void *sre_op_targets[256] = { /* assert subpattern */ /* */ TRACE(("|%p|%p|ASSERT %d\n", pattern, - ctx->ptr, pattern[1])); - if (ctx->ptr - (SRE_CHAR *)state->beginning < (Py_ssize_t)pattern[1]) + ptr, pattern[1])); + if (ptr - (SRE_CHAR *)state->beginning < (Py_ssize_t)pattern[1]) RETURN_FAILURE; - state->ptr = ctx->ptr - pattern[1]; + state->ptr = ptr - pattern[1]; DO_JUMP0(JUMP_ASSERT, jump_assert, pattern+2); RETURN_ON_FAILURE(ret); pattern += pattern[0]; @@ -1764,9 +1766,9 @@ static void *sre_op_targets[256] = { /* assert not subpattern */ /* */ TRACE(("|%p|%p|ASSERT_NOT %d\n", pattern, - ctx->ptr, pattern[1])); - if (ctx->ptr - (SRE_CHAR *)state->beginning >= (Py_ssize_t)pattern[1]) { - state->ptr = ctx->ptr - pattern[1]; + ptr, pattern[1])); + if (ptr - (SRE_CHAR *)state->beginning >= (Py_ssize_t)pattern[1]) { + state->ptr = ptr - pattern[1]; LASTMARK_SAVE(); if (state->repeat) MARK_PUSH(ctx->lastmark); @@ -1787,7 +1789,7 @@ static void *sre_op_targets[256] = { TARGET(SRE_OP_FAILURE): /* immediate failure */ - TRACE(("|%p|%p|FAILURE\n", pattern, ctx->ptr)); + TRACE(("|%p|%p|FAILURE\n", pattern, ptr)); RETURN_FAILURE; #if USE_COMPUTED_GOTOS @@ -1795,7 +1797,7 @@ static void *sre_op_targets[256] = { #else default: #endif - TRACE(("|%p|%p|UNKNOWN %d\n", pattern, ctx->ptr, + TRACE(("|%p|%p|UNKNOWN %d\n", pattern, ptr, pattern[-1])); RETURN_ERROR(SRE_ERROR_ILLEGAL); } @@ -1811,56 +1813,56 @@ static void *sre_op_targets[256] = { switch (jump) { case JUMP_MAX_UNTIL_2: - TRACE(("|%p|%p|JUMP_MAX_UNTIL_2\n", pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_MAX_UNTIL_2\n", pattern, ptr)); goto jump_max_until_2; case JUMP_MAX_UNTIL_3: - TRACE(("|%p|%p|JUMP_MAX_UNTIL_3\n", pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_MAX_UNTIL_3\n", pattern, ptr)); goto jump_max_until_3; case JUMP_MIN_UNTIL_2: - TRACE(("|%p|%p|JUMP_MIN_UNTIL_2\n", pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_MIN_UNTIL_2\n", pattern, ptr)); goto jump_min_until_2; case JUMP_MIN_UNTIL_3: - TRACE(("|%p|%p|JUMP_MIN_UNTIL_3\n", pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_MIN_UNTIL_3\n", pattern, ptr)); goto jump_min_until_3; case JUMP_BRANCH: - TRACE(("|%p|%p|JUMP_BRANCH\n", pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_BRANCH\n", pattern, ptr)); goto jump_branch; case JUMP_MAX_UNTIL_1: - TRACE(("|%p|%p|JUMP_MAX_UNTIL_1\n", pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_MAX_UNTIL_1\n", pattern, ptr)); goto jump_max_until_1; case JUMP_MIN_UNTIL_1: - TRACE(("|%p|%p|JUMP_MIN_UNTIL_1\n", pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_MIN_UNTIL_1\n", pattern, ptr)); goto jump_min_until_1; case JUMP_POSS_REPEAT_1: - TRACE(("|%p|%p|JUMP_POSS_REPEAT_1\n", pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_POSS_REPEAT_1\n", pattern, ptr)); goto jump_poss_repeat_1; case JUMP_POSS_REPEAT_2: - TRACE(("|%p|%p|JUMP_POSS_REPEAT_2\n", pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_POSS_REPEAT_2\n", pattern, ptr)); goto jump_poss_repeat_2; case JUMP_REPEAT: - TRACE(("|%p|%p|JUMP_REPEAT\n", pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_REPEAT\n", pattern, ptr)); goto jump_repeat; case JUMP_REPEAT_ONE_1: - TRACE(("|%p|%p|JUMP_REPEAT_ONE_1\n", pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_REPEAT_ONE_1\n", pattern, ptr)); goto jump_repeat_one_1; case JUMP_REPEAT_ONE_2: - TRACE(("|%p|%p|JUMP_REPEAT_ONE_2\n", pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_REPEAT_ONE_2\n", pattern, ptr)); goto jump_repeat_one_2; case JUMP_MIN_REPEAT_ONE: - TRACE(("|%p|%p|JUMP_MIN_REPEAT_ONE\n", pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_MIN_REPEAT_ONE\n", pattern, ptr)); goto jump_min_repeat_one; case JUMP_ATOMIC_GROUP: - TRACE(("|%p|%p|JUMP_ATOMIC_GROUP\n", pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_ATOMIC_GROUP\n", pattern, ptr)); goto jump_atomic_group; case JUMP_ASSERT: - TRACE(("|%p|%p|JUMP_ASSERT\n", pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_ASSERT\n", pattern, ptr)); goto jump_assert; case JUMP_ASSERT_NOT: - TRACE(("|%p|%p|JUMP_ASSERT_NOT\n", pattern, ctx->ptr)); + TRACE(("|%p|%p|JUMP_ASSERT_NOT\n", pattern, ptr)); goto jump_assert_not; case JUMP_NONE: TRACE(("|%p|%p|RETURN %zd\n", pattern, - ctx->ptr, ret)); + ptr, ret)); break; } From 48a7f1c3d5959fdade79db3240e18247c3e437b1 Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Tue, 12 Apr 2022 14:33:58 -0700 Subject: [PATCH 4/9] Rearrange some stuff --- Modules/_sre/sre_lib.h | 387 +++++++------------------------------ Modules/_sre/sre_targets.h | 258 +++++++++++++++++++++++++ 2 files changed, 328 insertions(+), 317 deletions(-) create mode 100644 Modules/_sre/sre_targets.h diff --git a/Modules/_sre/sre_lib.h b/Modules/_sre/sre_lib.h index 2106ca7e202c30..95445766360e04 100644 --- a/Modules/_sre/sre_lib.h +++ b/Modules/_sre/sre_lib.h @@ -486,7 +486,7 @@ do { \ #define DO_JUMPX(jumpvalue, jumplabel, nextpattern, toplevel_) \ ctx->pattern = pattern; \ - ctx->ptr = ptr;\ + ctx->ptr = ptr; \ DATA_ALLOC(SRE(match_context), nextctx); \ nextctx->last_ctx_pos = ctx_pos; \ nextctx->jump = jumpvalue; \ @@ -521,34 +521,34 @@ typedef struct { int toplevel; } SRE(match_context); +#define MAYBE_CHECK_SIGNALS \ + do { \ + if ((0 == (++sigcount & 0xfff)) && PyErr_CheckSignals()) { \ + RETURN_ERROR(SRE_ERROR_INTERRUPTED); \ + } \ + } while (0) + #ifdef HAVE_COMPUTED_GOTOS #ifndef USE_COMPUTED_GOTOS #define USE_COMPUTED_GOTOS 1 #endif -#else - #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS +#elif defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS #error "Computed gotos are not supported on this compiler." - #endif +#else #undef USE_COMPUTED_GOTOS #define USE_COMPUTED_GOTOS 0 #endif -#define CHECK_SIGNALS() \ -do { \ - if ((0 == (++sigcount & 0xfff)) && PyErr_CheckSignals()) { \ - RETURN_ERROR(SRE_ERROR_INTERRUPTED); \ - } \ -} while (0) - #if USE_COMPUTED_GOTOS -#define TARGET(OP) LABEL_ ## OP -#define DISPATCH() do { \ - CHECK_SIGNALS(); \ - goto *sre_op_targets[*pattern++]; \ -} while (0) + #define TARGET(OP) TARGET_ ## OP + #define DISPATCH \ + do { \ + MAYBE_CHECK_SIGNALS; \ + goto *sre_targets[*pattern++]; \ + } while (0) #else -#define TARGET(OP) case OP -#define DISPATCH() break + #define TARGET(OP) case OP + #define DISPATCH goto dispatch #endif /* check if string matches the given pattern. returns <0 for @@ -573,8 +573,13 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) ctx->toplevel = toplevel; ctx_pos = alloc_pos; -entrance:; +#if USE_COMPUTED_GOTOS +#include "sre_targets.h" +#endif +entrance: + + ; // Fashion statement. const SRE_CHAR *ptr = (SRE_CHAR *)state->ptr; if (pattern[0] == SRE_OP_INFO) { @@ -589,274 +594,14 @@ entrance:; } #if USE_COMPUTED_GOTOS -static void *sre_op_targets[256] = { - &&LABEL_SRE_OP_FAILURE, - &&LABEL_SRE_OP_SUCCESS, - &&LABEL_SRE_OP_ANY, - &&LABEL_SRE_OP_ANY_ALL, - &&LABEL_SRE_OP_ASSERT, - &&LABEL_SRE_OP_ASSERT_NOT, - &&LABEL_SRE_OP_AT, - &&LABEL_SRE_OP_BRANCH, - &&unknown, // &&LABEL_SRE_OP_CALL, - &&LABEL_SRE_OP_CATEGORY, - &&unknown, // &&LABEL_SRE_OP_CHARSET, - &&unknown, // &&LABEL_SRE_OP_BIGCHARSET, - &&LABEL_SRE_OP_GROUPREF, - &&LABEL_SRE_OP_GROUPREF_EXISTS, - &&LABEL_SRE_OP_IN, - &&LABEL_SRE_OP_INFO, - &&LABEL_SRE_OP_JUMP, - &&LABEL_SRE_OP_LITERAL, - &&LABEL_SRE_OP_MARK, - &&LABEL_SRE_OP_MAX_UNTIL, - &&LABEL_SRE_OP_MIN_UNTIL, - &&LABEL_SRE_OP_NOT_LITERAL, - &&unknown, // &&LABEL_SRE_OP_NEGATE, - &&unknown, // &&LABEL_SRE_OP_RANGE, - &&LABEL_SRE_OP_REPEAT, - &&LABEL_SRE_OP_REPEAT_ONE, - &&unknown, // &&LABEL_SRE_OP_SUBPATTERN, - &&LABEL_SRE_OP_MIN_REPEAT_ONE, - &&LABEL_SRE_OP_ATOMIC_GROUP, - &&LABEL_SRE_OP_POSSESSIVE_REPEAT, - &&LABEL_SRE_OP_POSSESSIVE_REPEAT_ONE, - &&LABEL_SRE_OP_GROUPREF_IGNORE, - &&LABEL_SRE_OP_IN_IGNORE, - &&LABEL_SRE_OP_LITERAL_IGNORE, - &&LABEL_SRE_OP_NOT_LITERAL_IGNORE, - &&LABEL_SRE_OP_GROUPREF_LOC_IGNORE, - &&LABEL_SRE_OP_IN_LOC_IGNORE, - &&LABEL_SRE_OP_LITERAL_LOC_IGNORE, - &&LABEL_SRE_OP_NOT_LITERAL_LOC_IGNORE, - &&LABEL_SRE_OP_GROUPREF_UNI_IGNORE, - &&LABEL_SRE_OP_IN_UNI_IGNORE, - &&LABEL_SRE_OP_LITERAL_UNI_IGNORE, - &&LABEL_SRE_OP_NOT_LITERAL_UNI_IGNORE, - &&unknown, // &&LABEL_SRE_OP_RANGE_UNI_IGNORE, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, - &&unknown, -}; -#endif - - for (;;) { -#if USE_COMPUTED_GOTOS - DISPATCH(); + DISPATCH; #else - CHECK_SIGNALS(); - switch (*pattern++) +dispatch: + MAYBE_CHECK_SIGNALS; + switch (*pattern++) #endif - { + { + TARGET(SRE_OP_MARK): /* set mark */ /* */ @@ -877,7 +622,7 @@ static void *sre_op_targets[256] = { } state->mark[i] = ptr; pattern++; - DISPATCH(); + DISPATCH; TARGET(SRE_OP_LITERAL): /* match literal string */ @@ -888,7 +633,7 @@ static void *sre_op_targets[256] = { RETURN_FAILURE; pattern++; ptr++; - DISPATCH(); + DISPATCH; TARGET(SRE_OP_NOT_LITERAL): /* match anything that is not literal character */ @@ -899,7 +644,7 @@ static void *sre_op_targets[256] = { RETURN_FAILURE; pattern++; ptr++; - DISPATCH(); + DISPATCH; TARGET(SRE_OP_SUCCESS): /* end of pattern */ @@ -920,7 +665,7 @@ static void *sre_op_targets[256] = { if (!SRE(at)(state, ptr, *pattern)) RETURN_FAILURE; pattern++; - DISPATCH(); + DISPATCH; TARGET(SRE_OP_CATEGORY): /* match at given category */ @@ -931,7 +676,7 @@ static void *sre_op_targets[256] = { RETURN_FAILURE; pattern++; ptr++; - DISPATCH(); + DISPATCH; TARGET(SRE_OP_ANY): /* match anything (except a newline) */ @@ -940,7 +685,7 @@ static void *sre_op_targets[256] = { if (ptr >= end || SRE_IS_LINEBREAK(ptr[0])) RETURN_FAILURE; ptr++; - DISPATCH(); + DISPATCH; TARGET(SRE_OP_ANY_ALL): /* match anything */ @@ -949,7 +694,7 @@ static void *sre_op_targets[256] = { if (ptr >= end) RETURN_FAILURE; ptr++; - DISPATCH(); + DISPATCH; TARGET(SRE_OP_IN): /* match set member (or non_member) */ @@ -960,7 +705,7 @@ static void *sre_op_targets[256] = { RETURN_FAILURE; pattern += pattern[0]; ptr++; - DISPATCH(); + DISPATCH; TARGET(SRE_OP_LITERAL_IGNORE): TRACE(("|%p|%p|LITERAL_IGNORE %d\n", @@ -970,7 +715,7 @@ static void *sre_op_targets[256] = { RETURN_FAILURE; pattern++; ptr++; - DISPATCH(); + DISPATCH; TARGET(SRE_OP_LITERAL_UNI_IGNORE): TRACE(("|%p|%p|LITERAL_UNI_IGNORE %d\n", @@ -980,7 +725,7 @@ static void *sre_op_targets[256] = { RETURN_FAILURE; pattern++; ptr++; - DISPATCH(); + DISPATCH; TARGET(SRE_OP_LITERAL_LOC_IGNORE): TRACE(("|%p|%p|LITERAL_LOC_IGNORE %d\n", @@ -990,7 +735,7 @@ static void *sre_op_targets[256] = { RETURN_FAILURE; pattern++; ptr++; - DISPATCH(); + DISPATCH; TARGET(SRE_OP_NOT_LITERAL_IGNORE): TRACE(("|%p|%p|NOT_LITERAL_IGNORE %d\n", @@ -1000,7 +745,7 @@ static void *sre_op_targets[256] = { RETURN_FAILURE; pattern++; ptr++; - DISPATCH(); + DISPATCH; TARGET(SRE_OP_NOT_LITERAL_UNI_IGNORE): TRACE(("|%p|%p|NOT_LITERAL_UNI_IGNORE %d\n", @@ -1010,7 +755,7 @@ static void *sre_op_targets[256] = { RETURN_FAILURE; pattern++; ptr++; - DISPATCH(); + DISPATCH; TARGET(SRE_OP_NOT_LITERAL_LOC_IGNORE): TRACE(("|%p|%p|NOT_LITERAL_LOC_IGNORE %d\n", @@ -1020,7 +765,7 @@ static void *sre_op_targets[256] = { RETURN_FAILURE; pattern++; ptr++; - DISPATCH(); + DISPATCH; TARGET(SRE_OP_IN_IGNORE): TRACE(("|%p|%p|IN_IGNORE\n", pattern, ptr)); @@ -1030,7 +775,7 @@ static void *sre_op_targets[256] = { RETURN_FAILURE; pattern += pattern[0]; ptr++; - DISPATCH(); + DISPATCH; TARGET(SRE_OP_IN_UNI_IGNORE): TRACE(("|%p|%p|IN_UNI_IGNORE\n", pattern, ptr)); @@ -1040,7 +785,7 @@ static void *sre_op_targets[256] = { RETURN_FAILURE; pattern += pattern[0]; ptr++; - DISPATCH(); + DISPATCH; TARGET(SRE_OP_IN_LOC_IGNORE): TRACE(("|%p|%p|IN_LOC_IGNORE\n", pattern, ptr)); @@ -1049,7 +794,7 @@ static void *sre_op_targets[256] = { RETURN_FAILURE; pattern += pattern[0]; ptr++; - DISPATCH(); + DISPATCH; TARGET(SRE_OP_JUMP): TARGET(SRE_OP_INFO): @@ -1058,7 +803,7 @@ static void *sre_op_targets[256] = { TRACE(("|%p|%p|JUMP %d\n", pattern, ptr, pattern[0])); pattern += pattern[0]; - DISPATCH(); + DISPATCH; TARGET(SRE_OP_BRANCH): /* alternation */ @@ -1322,7 +1067,7 @@ static void *sre_op_targets[256] = { } /* Attempt to match the rest of the string */ - DISPATCH(); + DISPATCH; TARGET(SRE_OP_REPEAT): /* create repeat context. all the hard work is done @@ -1590,7 +1335,7 @@ static void *sre_op_targets[256] = { the SUCCESS op code that follows it. */ pattern += pattern[0] + 1; ptr = state->ptr; - DISPATCH(); + DISPATCH; TARGET(SRE_OP_ATOMIC_GROUP): /* Atomic Group Sub Pattern */ @@ -1622,7 +1367,7 @@ static void *sre_op_targets[256] = { the SUCCESS op code that follows it. */ pattern += pattern[0]; ptr = state->ptr; - DISPATCH(); + DISPATCH; TARGET(SRE_OP_GROUPREF): /* match backreference */ @@ -1647,7 +1392,7 @@ static void *sre_op_targets[256] = { } } pattern++; - DISPATCH(); + DISPATCH; TARGET(SRE_OP_GROUPREF_IGNORE): /* match backreference */ @@ -1673,7 +1418,7 @@ static void *sre_op_targets[256] = { } } pattern++; - DISPATCH(); + DISPATCH; TARGET(SRE_OP_GROUPREF_UNI_IGNORE): /* match backreference */ @@ -1699,7 +1444,7 @@ static void *sre_op_targets[256] = { } } pattern++; - DISPATCH(); + DISPATCH; TARGET(SRE_OP_GROUPREF_LOC_IGNORE): /* match backreference */ @@ -1725,7 +1470,7 @@ static void *sre_op_targets[256] = { } } pattern++; - DISPATCH(); + DISPATCH; TARGET(SRE_OP_GROUPREF_EXISTS): TRACE(("|%p|%p|GROUPREF_EXISTS %d\n", pattern, @@ -1736,18 +1481,18 @@ static void *sre_op_targets[256] = { Py_ssize_t groupref = i+i; if (groupref >= state->lastmark) { pattern += pattern[1]; - DISPATCH(); + DISPATCH; } else { SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref]; SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1]; if (!p || !e || e < p) { pattern += pattern[1]; - DISPATCH(); + DISPATCH; } } } pattern += 2; - DISPATCH(); + DISPATCH; TARGET(SRE_OP_ASSERT): /* assert subpattern */ @@ -1760,7 +1505,7 @@ static void *sre_op_targets[256] = { DO_JUMP0(JUMP_ASSERT, jump_assert, pattern+2); RETURN_ON_FAILURE(ret); pattern += pattern[0]; - DISPATCH(); + DISPATCH; TARGET(SRE_OP_ASSERT_NOT): /* assert not subpattern */ @@ -1785,7 +1530,7 @@ static void *sre_op_targets[256] = { LASTMARK_RESTORE(); } pattern += pattern[0]; - DISPATCH(); + DISPATCH; TARGET(SRE_OP_FAILURE): /* immediate failure */ @@ -1793,14 +1538,22 @@ static void *sre_op_targets[256] = { RETURN_FAILURE; #if USE_COMPUTED_GOTOS - unknown: + _unknown: #else default: #endif + // Also any unused opcodes: + TARGET(SRE_OP_RANGE_UNI_IGNORE): + TARGET(SRE_OP_SUBPATTERN): + TARGET(SRE_OP_RANGE): + TARGET(SRE_OP_NEGATE): + TARGET(SRE_OP_BIGCHARSET): + TARGET(SRE_OP_CHARSET): + TARGET(SRE_OP_CALL): TRACE(("|%p|%p|UNKNOWN %d\n", pattern, ptr, pattern[-1])); RETURN_ERROR(SRE_ERROR_ILLEGAL); - } + } exit: diff --git a/Modules/_sre/sre_targets.h b/Modules/_sre/sre_targets.h new file mode 100644 index 00000000000000..454e9710d7cf20 --- /dev/null +++ b/Modules/_sre/sre_targets.h @@ -0,0 +1,258 @@ +static void *sre_targets[256] = { + &&TARGET_SRE_OP_FAILURE, + &&TARGET_SRE_OP_SUCCESS, + &&TARGET_SRE_OP_ANY, + &&TARGET_SRE_OP_ANY_ALL, + &&TARGET_SRE_OP_ASSERT, + &&TARGET_SRE_OP_ASSERT_NOT, + &&TARGET_SRE_OP_AT, + &&TARGET_SRE_OP_BRANCH, + &&TARGET_SRE_OP_CALL, + &&TARGET_SRE_OP_CATEGORY, + &&TARGET_SRE_OP_CHARSET, + &&TARGET_SRE_OP_BIGCHARSET, + &&TARGET_SRE_OP_GROUPREF, + &&TARGET_SRE_OP_GROUPREF_EXISTS, + &&TARGET_SRE_OP_IN, + &&TARGET_SRE_OP_INFO, + &&TARGET_SRE_OP_JUMP, + &&TARGET_SRE_OP_LITERAL, + &&TARGET_SRE_OP_MARK, + &&TARGET_SRE_OP_MAX_UNTIL, + &&TARGET_SRE_OP_MIN_UNTIL, + &&TARGET_SRE_OP_NOT_LITERAL, + &&TARGET_SRE_OP_NEGATE, + &&TARGET_SRE_OP_RANGE, + &&TARGET_SRE_OP_REPEAT, + &&TARGET_SRE_OP_REPEAT_ONE, + &&TARGET_SRE_OP_SUBPATTERN, + &&TARGET_SRE_OP_MIN_REPEAT_ONE, + &&TARGET_SRE_OP_ATOMIC_GROUP, + &&TARGET_SRE_OP_POSSESSIVE_REPEAT, + &&TARGET_SRE_OP_POSSESSIVE_REPEAT_ONE, + &&TARGET_SRE_OP_GROUPREF_IGNORE, + &&TARGET_SRE_OP_IN_IGNORE, + &&TARGET_SRE_OP_LITERAL_IGNORE, + &&TARGET_SRE_OP_NOT_LITERAL_IGNORE, + &&TARGET_SRE_OP_GROUPREF_LOC_IGNORE, + &&TARGET_SRE_OP_IN_LOC_IGNORE, + &&TARGET_SRE_OP_LITERAL_LOC_IGNORE, + &&TARGET_SRE_OP_NOT_LITERAL_LOC_IGNORE, + &&TARGET_SRE_OP_GROUPREF_UNI_IGNORE, + &&TARGET_SRE_OP_IN_UNI_IGNORE, + &&TARGET_SRE_OP_LITERAL_UNI_IGNORE, + &&TARGET_SRE_OP_NOT_LITERAL_UNI_IGNORE, + &&TARGET_SRE_OP_RANGE_UNI_IGNORE, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, + &&_unknown, +}; \ No newline at end of file From 6f8a9a08f9a8fa7e9181c516d26c32aa409b99ae Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Tue, 12 Apr 2022 15:01:24 -0700 Subject: [PATCH 5/9] Whitespace --- Modules/_sre/sre_targets.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_sre/sre_targets.h b/Modules/_sre/sre_targets.h index 454e9710d7cf20..0ac070198e8cb2 100644 --- a/Modules/_sre/sre_targets.h +++ b/Modules/_sre/sre_targets.h @@ -255,4 +255,4 @@ static void *sre_targets[256] = { &&_unknown, &&_unknown, &&_unknown, -}; \ No newline at end of file +}; From 03d3194c75de50f8352cd1066a45ec6d07809c13 Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Tue, 12 Apr 2022 15:21:40 -0700 Subject: [PATCH 6/9] Generate sre_targets.h --- Lib/re/_constants.py | 8 ++ Modules/_sre/sre_lib.h | 4 +- Modules/_sre/sre_targets.h | 216 +------------------------------------ 3 files changed, 12 insertions(+), 216 deletions(-) diff --git a/Lib/re/_constants.py b/Lib/re/_constants.py index 4c7e93e67ec607..90f8dcd51d0214 100644 --- a/Lib/re/_constants.py +++ b/Lib/re/_constants.py @@ -257,4 +257,12 @@ def dump(f, d, prefix): f.write("#define SRE_INFO_LITERAL %d\n" % SRE_INFO_LITERAL) f.write("#define SRE_INFO_CHARSET %d\n" % SRE_INFO_CHARSET) + with open("sre_targets.h", "w") as f: + f.write("// NOTE: This file is generated by Lib/re/_constants.py!\n\n") + f.write(f"static void *sre_targets[{len(OPCODES)}] = {{\n") + for i, item in enumerate(sorted(OPCODES)): + assert i == item + f.write(f" &&TARGET_SRE_OP_{item},\n") + f.write("};\n") + print("done") diff --git a/Modules/_sre/sre_lib.h b/Modules/_sre/sre_lib.h index 95445766360e04..3472e65b87ae6f 100644 --- a/Modules/_sre/sre_lib.h +++ b/Modules/_sre/sre_lib.h @@ -1537,9 +1537,7 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) TRACE(("|%p|%p|FAILURE\n", pattern, ptr)); RETURN_FAILURE; -#if USE_COMPUTED_GOTOS - _unknown: -#else +#if !USE_COMPUTED_GOTOS default: #endif // Also any unused opcodes: diff --git a/Modules/_sre/sre_targets.h b/Modules/_sre/sre_targets.h index 0ac070198e8cb2..8fd7ae92f235a4 100644 --- a/Modules/_sre/sre_targets.h +++ b/Modules/_sre/sre_targets.h @@ -1,4 +1,6 @@ -static void *sre_targets[256] = { +// NOTE: This file is generated by Lib/re/_constants.py! + +static void *sre_targets[44] = { &&TARGET_SRE_OP_FAILURE, &&TARGET_SRE_OP_SUCCESS, &&TARGET_SRE_OP_ANY, @@ -43,216 +45,4 @@ static void *sre_targets[256] = { &&TARGET_SRE_OP_LITERAL_UNI_IGNORE, &&TARGET_SRE_OP_NOT_LITERAL_UNI_IGNORE, &&TARGET_SRE_OP_RANGE_UNI_IGNORE, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, - &&_unknown, }; From 0d303287724f6d261bb49e6cba9c56d969b0fefc Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Tue, 12 Apr 2022 19:08:20 -0700 Subject: [PATCH 7/9] blurb add --- .../next/Library/2022-04-12-19-08-13.gh-issue-91404.zjqYHo.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2022-04-12-19-08-13.gh-issue-91404.zjqYHo.rst diff --git a/Misc/NEWS.d/next/Library/2022-04-12-19-08-13.gh-issue-91404.zjqYHo.rst b/Misc/NEWS.d/next/Library/2022-04-12-19-08-13.gh-issue-91404.zjqYHo.rst new file mode 100644 index 00000000000000..58464fceeba5a1 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-04-12-19-08-13.gh-issue-91404.zjqYHo.rst @@ -0,0 +1,3 @@ +Improve the performance of :mod:`re` matching by using computed gotos (or +"threaded code") on supported platforms and removing expensive pointer +indirections. From fdcde9fceea3eb7d3283ee0690c52b11055d0eb0 Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Tue, 12 Apr 2022 20:52:25 -0700 Subject: [PATCH 8/9] fixup --- Makefile.pre.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile.pre.in b/Makefile.pre.in index 451ef6ba7505e2..ee7e227e7112e8 100644 --- a/Makefile.pre.in +++ b/Makefile.pre.in @@ -1356,7 +1356,7 @@ regen-sre: $(PYTHON_FOR_REGEN) $(srcdir)/Tools/scripts/generate_sre_constants.py \ $(srcdir)/Lib/re/_constants.py \ $(srcdir)/Modules/_sre/sre_constants.h \ - $(srcdir)/Modules/_sre/sre_targets.h \ + $(srcdir)/Modules/_sre/sre_targets.h Python/compile.o Python/symtable.o Python/ast_unparse.o Python/ast.o Python/future.o: $(srcdir)/Include/internal/pycore_ast.h From d38743f79bb0cdcc04fc12fe1c8b38a59f124119 Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Thu, 14 Apr 2022 15:50:41 -0700 Subject: [PATCH 9/9] What's New --- Doc/whatsnew/3.11.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index d803801f273c55..0ce70cf5b50352 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -512,6 +512,12 @@ Optimizations becomes 272 bytes from 352 bytes on 64bit platform. (Contributed by Inada Naoki in :issue:`46845`.) +* :mod:`re`'s regular expression matching engine has been partially refactored, + and now uses computed gotos (or "threaded code") on supported platforms. As a + result, Python 3.11 executes the `pyperformance regular expression benchmarks + `_ up to 10% + faster than Python 3.10. + Faster CPython ==============