This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
regcomp.c: Extract duplicated code to fcn
authorKarl Williamson <khw@cpan.org>
Mon, 15 Sep 2014 14:46:35 +0000 (08:46 -0600)
committerKarl Williamson <khw@cpan.org>
Mon, 29 Sep 2014 17:07:39 +0000 (11:07 -0600)
This causes the nearly-duplicate code of S_reg_node and S_reganode to be
placed into a single function, S_regnode_guts.

There is one place where it might not be obvious that this doesn't
change things.  And that is under DEBUGGING, reg_node() called

    Set_Node_Offset(RExC_emit, RExC_parse + (op == END));

and reganode called

    Set_Cur_Node_Offset;

However Set_Cur_Node_Offset is defined to be

    Set_Node_Offset(RExC_emit, RExC_parse)

and since op will never be END for reganode, the two statements are
equivalent.

embed.fnc
embed.h
proto.h
regcomp.c

index 8764e7d..1c3d0bc 100644 (file)
--- a/embed.fnc
+++ b/embed.fnc
@@ -2086,6 +2086,10 @@ p        |OP *   |tied_method|NN SV *methname|NN SV **sp \
 #if defined(PERL_IN_REGCOMP_C)
 Es     |regnode*|reg           |NN RExC_state_t *pRExC_state \
                                |I32 paren|NN I32 *flagp|U32 depth
+Es     |regnode*|regnode_guts  |NN RExC_state_t *pRExC_state              \
+                               |const U8 op                               \
+                               |const STRLEN extra_len                    \
+                               |NN const char* const name
 Es     |regnode*|reganode      |NN RExC_state_t *pRExC_state|U8 op \
                                |U32 arg
 Es     |regnode*|regatom       |NN RExC_state_t *pRExC_state \
diff --git a/embed.h b/embed.h
index 300f55f..b6aabef 100644 (file)
--- a/embed.h
+++ b/embed.h
 #define regbranch(a,b,c,d)     S_regbranch(aTHX_ a,b,c,d)
 #define regclass(a,b,c,d,e,f,g)        S_regclass(aTHX_ a,b,c,d,e,f,g)
 #define reginsert(a,b,c,d)     S_reginsert(aTHX_ a,b,c,d)
+#define regnode_guts(a,b,c,d)  S_regnode_guts(aTHX_ a,b,c,d)
 #define regpatws               S_regpatws
 #define regpiece(a,b,c)                S_regpiece(aTHX_ a,b,c)
 #define regpposixcc(a,b,c)     S_regpposixcc(aTHX_ a,b,c)
diff --git a/proto.h b/proto.h
index 78b107b..2f9e62f 100644 (file)
--- a/proto.h
+++ b/proto.h
@@ -6968,6 +6968,12 @@ STATIC void      S_reginsert(pTHX_ RExC_state_t *pRExC_state, U8 op, regnode *opnd, U
 #define PERL_ARGS_ASSERT_REGINSERT     \
        assert(pRExC_state); assert(opnd)
 
+STATIC regnode*        S_regnode_guts(pTHX_ RExC_state_t *pRExC_state, const U8 op, const STRLEN extra_len, const char* const name)
+                       __attribute__nonnull__(pTHX_1)
+                       __attribute__nonnull__(pTHX_4);
+#define PERL_ARGS_ASSERT_REGNODE_GUTS  \
+       assert(pRExC_state); assert(name)
+
 STATIC char *  S_regpatws(RExC_state_t *pRExC_state, char *p, const bool recognize_comment)
                        __attribute__warn_unused_result__
                        __attribute__nonnull__(1)
index 350fdd5..f424a2b 100644 (file)
--- a/regcomp.c
+++ b/regcomp.c
@@ -15416,21 +15416,21 @@ S_nextchar(pTHX_ RExC_state_t *pRExC_state)
     }
 }
 
-/*
-- reg_node - emit a node
-*/
-STATIC regnode *                       /* Location. */
-S_reg_node(pTHX_ RExC_state_t *pRExC_state, U8 op)
+STATIC regnode *
+S_regnode_guts(pTHX_ RExC_state_t *pRExC_state, const U8 op, const STRLEN extra_size, const char* const name)
 {
-    regnode *ptr;
+    /* Allocate a regnode for 'op' and returns it, with 'extra_size' extra
+     * space.  In pass1, it aligns and increments RExC_size; in pass2,
+     * RExC_emit */
+
     regnode * const ret = RExC_emit;
     GET_RE_DEBUG_FLAGS_DECL;
 
-    PERL_ARGS_ASSERT_REG_NODE;
+    PERL_ARGS_ASSERT_REGNODE_GUTS;
 
     if (SIZE_ONLY) {
        SIZE_ALIGN(RExC_size);
-       RExC_size += 1;
+       RExC_size += 1 + extra_size;
        return(ret);
     }
     if (RExC_emit >= RExC_emit_bound)
@@ -15438,11 +15438,13 @@ S_reg_node(pTHX_ RExC_state_t *pRExC_state, U8 op)
                   op, (void*)RExC_emit, (void*)RExC_emit_bound);
 
     NODE_ALIGN_FILL(ret);
-#ifdef RE_TRACK_PATTERN_OFFSETS
+#ifndef RE_TRACK_PATTERN_OFFSETS
+    PERL_UNUSED_ARG(name);
+#else
     if (RExC_offsets) {         /* MJD */
        MJD_OFFSET_DEBUG(
               ("%s:%d: (op %s) %s %"UVuf" (len %"UVuf") (max %"UVuf").\n",
-              "reg_node", __LINE__,
+              name, __LINE__,
               PL_reg_name[op],
               (UV)(RExC_emit - RExC_emit_start) > RExC_offsets[0]
                ? "Overwriting end of array!\n" : "OK",
@@ -15452,66 +15454,42 @@ S_reg_node(pTHX_ RExC_state_t *pRExC_state, U8 op)
        Set_Node_Offset(RExC_emit, RExC_parse + (op == END));
     }
 #endif
-    ptr = ret;
-    FILL_ADVANCE_NODE(ptr, op);
-    RExC_emit = ptr;
     return(ret);
 }
 
 /*
-- reganode - emit a node with an argument
+- reg_node - emit a node
 */
 STATIC regnode *                       /* Location. */
-S_reganode(pTHX_ RExC_state_t *pRExC_state, U8 op, U32 arg)
+S_reg_node(pTHX_ RExC_state_t *pRExC_state, U8 op)
 {
-    regnode *ptr;
-    regnode * const ret = RExC_emit;
-    GET_RE_DEBUG_FLAGS_DECL;
+    regnode * const ret = regnode_guts(pRExC_state, op, 0, "reg_node");
 
-    PERL_ARGS_ASSERT_REGANODE;
-
-    if (SIZE_ONLY) {
-       SIZE_ALIGN(RExC_size);
-       RExC_size += 2;
-       /*
-          We can't do this:
-
-          assert(2==regarglen[op]+1);
+    PERL_ARGS_ASSERT_REG_NODE;
 
-          Anything larger than this has to allocate the extra amount.
-          If we changed this to be:
+    if (PASS2) {
+        regnode *ptr = ret;
+        FILL_ADVANCE_NODE(ptr, op);
+        RExC_emit = ptr;
+    }
+    return(ret);
+}
 
-          RExC_size += (1 + regarglen[op]);
+/*
+- reganode - emit a node with an argument
+*/
+STATIC regnode *                       /* Location. */
+S_reganode(pTHX_ RExC_state_t *pRExC_state, U8 op, U32 arg)
+{
+    regnode * const ret = regnode_guts(pRExC_state, op, 1, "reganode");
 
-          then it wouldn't matter. Its not clear what side effect
-          might come from that so its not done so far.
-          -- dmq
-       */
-       return(ret);
-    }
-    if (RExC_emit >= RExC_emit_bound)
-        Perl_croak(aTHX_ "panic: reg_node overrun trying to emit %d, %p>=%p",
-                  op, (void*)RExC_emit, (void*)RExC_emit_bound);
+    PERL_ARGS_ASSERT_REGANODE;
 
-    NODE_ALIGN_FILL(ret);
-#ifdef RE_TRACK_PATTERN_OFFSETS
-    if (RExC_offsets) {         /* MJD */
-       MJD_OFFSET_DEBUG(
-              ("%s(%d): (op %s) %s %"UVuf" <- %"UVuf" (max %"UVuf").\n",
-              "reganode",
-             __LINE__,
-             PL_reg_name[op],
-              (UV)(RExC_emit - RExC_emit_start) > RExC_offsets[0] ?
-              "Overwriting end of array!\n" : "OK",
-              (UV)(RExC_emit - RExC_emit_start),
-              (UV)(RExC_parse - RExC_start),
-              (UV)RExC_offsets[0]));
-       Set_Cur_Node_Offset;
+    if (PASS2) {
+        regnode *ptr = ret;
+        FILL_ADVANCE_NODE_ARG(ptr, op, arg);
+        RExC_emit = ptr;
     }
-#endif
-    ptr = ret;
-    FILL_ADVANCE_NODE_ARG(ptr, op, arg);
-    RExC_emit = ptr;
     return(ret);
 }