RPA Toolkit
Fixed abort directive to work with bitmap enable. rpa_record_dump is now more flexibl...
authorMartin Stoilov <martin@rpasearch.com>
Sat, 24 Oct 2015 02:49:19 +0000 (19:49 -0700)
committerMartin Stoilov <martin@rpasearch.com>
Fri, 23 Oct 2015 23:49:44 +0000 (16:49 -0700)
13 files changed:
rjs/rjs.c
rjs/rjscompiler.c
rpa/rpabitmap.c
rpa/rpabitmap.h
rpa/rpadbex.c
rpa/rpadbex.h
rpa/rpadbexpriv.h
rpa/rparecord.c
rpa/rparecord.h
rpagrep/rpagrep.c
tests/testrpa/rpacompiler-rulealtrec.c
tests/testrpa/rpacompiler-ruleloop.c
tests/testrpa/rpacompiler-ruleloopcls.c

index 0955cca..37462f4 100644 (file)
--- a/rjs/rjs.c
+++ b/rjs/rjs.c
@@ -1188,7 +1188,7 @@ int rjs_engine_dumpast(rjs_engine_t *jse, const char *script, unsigned long size
        if (records) {
                long i;
                for (i = 0; i < rpa_records_length(records); i++)
-                       rpa_record_dump(records, i);
+                       rpa_record_dump(records, i, 0);
        }
 
        rpa_records_destroy(records);
index 8fcc13a..8920ab9 100644 (file)
@@ -36,7 +36,7 @@ void rjs_compiler_debughead(rjs_compiler_t *co, rarray_t *records, long rec)
                rparecord_t *prec = (rparecord_t *) r_array_slot(records, rec);
                co->headoff = rvm_codegen_getcodesize(co->cg);
                if (prec->type & RPA_RECORD_START) {
-                       rpa_record_dump(records, rec);
+                       rpa_record_dump(records, rec, 0);
                }
 
        }
@@ -50,7 +50,7 @@ void rjs_compiler_debugtail(rjs_compiler_t *co, rarray_t *records, long rec)
                rparecord_t *prec = (rparecord_t *) r_array_slot(records, rec);
                rvm_asm_dump(co->cpu, rvm_codegen_getcode(co->cg, co->headoff), rvm_codegen_getcodesize(co->cg) - co->headoff);
                if (prec->type & RPA_RECORD_END) {
-                       rpa_record_dump(records, rec);
+                       rpa_record_dump(records, rec, 0);
                }
        }
 
index dc1d0c2..0344ff7 100644 (file)
@@ -26,8 +26,6 @@
 #include "rpa/rpastatpriv.h"
 #include "rpa/rpacompiler.h"
 
-static long rpa_bitmap_set(rarray_t *records, long rec, rpointer userdata);
-
 static long rpa_bitmap_set_startrec(rarray_t *records, long rec, rpabitmap_t bitmap)
 {
        rparecord_t *record = rpa_record_get(records, rpa_recordtree_get(records, rec, RPA_RECORD_START));
@@ -35,59 +33,6 @@ static long rpa_bitmap_set_startrec(rarray_t *records, long rec, rpabitmap_t bit
        return 0;
 }
 
-
-void rpa_dbex_buildbitmapinfo_for_rule(rpadbex_t *dbex, rparule_t rid)
-{
-       rpa_bitmapcompiler_t bc;
-       rharray_t *rules = dbex->rules;
-       rpa_ruleinfo_t *info;
-       const char *name = rpa_dbex_name(dbex, rid);
-       unsigned long namesize;
-
-       r_memset(&bc, 0, sizeof(bc));
-       bc.dbex = dbex;
-       if ((info = (rpa_ruleinfo_t *)r_harray_get(rules, rid)) != NULL) {
-               rparecord_t *record = rpa_record_get(dbex->records, info->startrec);
-               RPA_BITMAP_SETALL(RPA_RECORD2BITMAP(record));
-               rpa_recordtree_walk(dbex->records, info->startrec, 0, rpa_bitmap_set, (rpointer)&bc);
-               if (r_harray_lookup_s(dbex->abortrules, rpa_dbex_name(dbex, rid)) >= 0) {
-                       RPA_BITMAP_SETALL(RPA_RECORD2BITMAP(rpa_record_get(dbex->records, info->startrec)));
-                       RPA_BITMAP_SETALL(RPA_RECORD2BITMAP(rpa_record_get(dbex->records, rpa_recordtree_get(dbex->records, info->startrec, RPA_RECORD_END))));
-               }
-       }
-}
-
-
-ruword rpa_dbex_getrulebitmap(rpadbex_t *dbex, rparule_t rid)
-{
-       ruword bitmap = 0L;
-       rharray_t *rules = dbex->rules;
-       rpa_ruleinfo_t *info;
-
-       if ((info = (rpa_ruleinfo_t *)r_harray_get(rules, rid)) != NULL) {
-               rparecord_t *record = rpa_record_get(dbex->records, info->startrec);
-               if (record) {
-                       if (!RPA_BITMAP_GETVAL(RPA_RECORD2BITMAP(record))) {
-                               rpa_dbex_buildbitmapinfo_for_rule(dbex, rid);
-                       }
-                       bitmap = RPA_BITMAP_GETVAL(RPA_RECORD2BITMAP(record));
-               }
-       }
-       return bitmap;
-}
-
-void rpa_dbex_buildbitmapinfo(rpadbex_t *dbex)
-{
-       unsigned int i;
-       rharray_t *rules = dbex->rules;
-
-       for (i = 0; i < r_array_length(rules->members); i++) {
-               rpa_dbex_getrulebitmap(dbex, i);
-       }
-}
-
-
-
 static long rpa_bitmap_set_char(rarray_t *records, rparecord_t *record, long rec, rpointer userdata)
 {
        ruint32 wc = 0;
@@ -357,7 +302,7 @@ static long rpa_bitmap_set_notop(rarray_t *records, rparecord_t *record, long re
 }
 
 
-static long rpa_bitmap_set(rarray_t *records, long rec, rpointer userdata)
+long rpa_bitmap_set(rarray_t *records, long rec, rpointer userdata)
 {
 //     rpa_bitmapcompiler_t *bc = (rpa_bitmapcompiler_t*)userdata;
        rparecord_t *record = rpa_record_get(records, rec);
index d8fea31..7bb3efe 100644 (file)
@@ -62,8 +62,7 @@ typedef struct rpa_bitmapcompiler_s {
        ruint32 endchar;
 } rpa_bitmapcompiler_t;
 
-void rpa_dbex_buildbitmapinfo(rpadbex_t *dbex);
-void rpa_dbex_buildbitmapinfo_for_rule(rpadbex_t *dbex, rparule_t rid);
+long rpa_bitmap_set(rarray_t *records, long rec, rpointer userdata);
 
 
 #ifdef __cplusplus
index 7f36d8d..992e19d 100644 (file)
@@ -51,6 +51,69 @@ static int rpa_dbex_playchildrecords(rpadbex_t *dbex, long rec);
 static int rpa_dbex_playreversechildrecords(rpadbex_t *dbex, long rec);
 static int rpa_dbex_playrecord(rpadbex_t *dbex, long rec);
 static int rpa_dbex_rh_default(rpadbex_t *dbex, long rec);
+static void rpa_dbex_buildbitmapinfo(rpadbex_t *dbex);
+static void rpa_dbex_buildbitmapinfo_for_rule(rpadbex_t *dbex, rparule_t rid);
+static void rpa_dbex_bitmapinfo_set_all(rpadbex_t *dbex, rparule_t rid);
+
+static void rpa_dbex_buildbitmapinfo_for_rule(rpadbex_t *dbex, rparule_t rid)
+{
+       rpa_bitmapcompiler_t bc;
+       rharray_t *rules = dbex->rules;
+       rpa_ruleinfo_t *info;
+
+       r_memset(&bc, 0, sizeof(bc));
+       bc.dbex = dbex;
+       if ((info = (rpa_ruleinfo_t *)r_harray_get(rules, rid)) != NULL) {
+               rpa_rulepref_t *rulepref = rpa_compiler_rulepref_lookup_s(dbex->co, rpa_dbex_name(dbex, rid));
+               rparecord_t *record = rpa_record_get(dbex->records, info->startrec);
+               RPA_BITMAP_SETALL(RPA_RECORD2BITMAP(record));
+               rpa_recordtree_walk(dbex->records, info->startrec, 0, rpa_bitmap_set, (rpointer)&bc);
+               if (rulepref && (rulepref->flags & RPA_RFLAG_ABORTONFAIL))
+                       rpa_dbex_bitmapinfo_set_all(dbex, rid);
+       }
+}
+
+
+ruword rpa_dbex_getrulebitmap(rpadbex_t *dbex, rparule_t rid)
+{
+       ruword bitmap = 0L;
+       rharray_t *rules = dbex->rules;
+       rpa_ruleinfo_t *info;
+
+       if ((info = (rpa_ruleinfo_t *)r_harray_get(rules, rid)) != NULL) {
+               rparecord_t *record = rpa_record_get(dbex->records, info->startrec);
+               if (record) {
+                       if (!RPA_BITMAP_GETVAL(RPA_RECORD2BITMAP(record))) {
+                               rpa_dbex_buildbitmapinfo_for_rule(dbex, rid);
+                       }
+                       bitmap = RPA_BITMAP_GETVAL(RPA_RECORD2BITMAP(record));
+               }
+       }
+       return bitmap;
+}
+
+
+static void rpa_dbex_bitmapinfo_set_all(rpadbex_t *dbex, rparule_t rid)
+{
+       rharray_t *rules = dbex->rules;
+       rpa_ruleinfo_t *info;
+
+       if ((info = (rpa_ruleinfo_t *)r_harray_get(rules, rid)) != NULL) {
+               RPA_BITMAP_SETALL(RPA_RECORD2BITMAP(rpa_record_get(dbex->records, info->startrec)));
+               RPA_BITMAP_SETALL(RPA_RECORD2BITMAP(rpa_record_get(dbex->records, rpa_recordtree_get(dbex->records, info->startrec, RPA_RECORD_END))));
+       }
+}
+
+
+static void rpa_dbex_buildbitmapinfo(rpadbex_t *dbex)
+{
+       unsigned int i;
+       rharray_t *rules = dbex->rules;
+
+       for (i = 0; i < r_array_length(rules->members); i++) {
+               rpa_dbex_getrulebitmap(dbex, i);
+       }
+}
 
 
 void rpa_dbex_debug_recordhead(rpadbex_t *dbex, long rec)
@@ -60,10 +123,9 @@ void rpa_dbex_debug_recordhead(rpadbex_t *dbex, long rec)
                rparecord_t *prec = (rparecord_t *) r_array_slot(records, rec);
                dbex->headoff = rvm_codegen_getcodesize(dbex->co->cg);
                if (prec->type & RPA_RECORD_START) {
-                       rpa_record_dump(records, rec);
+                       rpa_record_dump(records, rec, 0);
                }
        }
-
 }
 
 
@@ -74,7 +136,7 @@ void rpa_dbex_debug_recordtail(rpadbex_t *dbex, long rec)
                rparecord_t *prec = (rparecord_t *) r_array_slot(records, rec);
                rvm_asm_dump(NULL, rvm_codegen_getcode(dbex->co->cg, dbex->headoff), rvm_codegen_getcodesize(dbex->co->cg) - dbex->headoff);
                if (prec->type & RPA_RECORD_END) {
-                       rpa_record_dump(records, rec);
+                       rpa_record_dump(records, rec, 0);
                }
        }
 }
@@ -249,7 +311,6 @@ static int rpa_dbex_rh_abort(rpadbex_t *dbex, long rec)
        unsigned long namesize;
        rarray_t *records = dbex->records;
        rparecord_t *prec;
-       rparule_t notset = 0L;
 
        rec = rpa_recordtree_get(records, rec, RPA_RECORD_START);
        prec = rpa_dbex_record(dbex, rec);
@@ -258,7 +319,6 @@ static int rpa_dbex_rh_abort(rpadbex_t *dbex, long rec)
        if (rpa_dbex_rulename(dbex, rec, &name, &namesize) < 0) {
                return -1;
        }
-       r_harray_add(dbex->abortrules, name, namesize, &notset);
        rpa_compiler_rulepref_set_flag(dbex->co, name, namesize, RPA_RFLAG_ABORTONFAIL);
        rpa_dbex_debug_recordtail(dbex, rec);
        if (rpa_dbex_playchildrecords(dbex, rec) < 0)
@@ -998,7 +1058,6 @@ rpadbex_t *rpa_dbex_create(void)
        dbex->records = r_array_create(sizeof(rparecord_t));
        dbex->temprecords = r_array_create(sizeof(rparecord_t));
        dbex->rules = r_harray_create(sizeof(rpa_ruleinfo_t));
-       dbex->abortrules = r_harray_create(sizeof(rparule_t));          /* We never populate the rule id anyway. we use the name for now */
        dbex->recstack = r_array_create(sizeof(unsigned long));
        dbex->inlinestack = r_array_create(sizeof(unsigned long));
        dbex->handlers = r_zmalloc(sizeof(rpa_dbex_recordhandler) * RPA_PRODUCTION_COUNT);
@@ -1048,7 +1107,6 @@ void rpa_dbex_destroy(rpadbex_t *dbex)
                rpa_compiler_destroy(dbex->co);
                rpa_parser_destroy(dbex->pa);
                r_harray_destroy(dbex->rules);
-               r_harray_destroy(dbex->abortrules);
                r_array_destroy(dbex->records);
                r_array_destroy(dbex->temprecords);
                r_array_destroy(dbex->recstack);
@@ -1566,7 +1624,7 @@ int rpa_dbex_dumprecords(rpadbex_t *dbex)
                return -1;
        }
        for (i = 0; i < r_array_length(dbex->records); i++) {
-               rpa_record_dump(dbex->records, i);
+               rpa_record_dump(dbex->records, i, 1);
        }
        return 0;
 }
@@ -1845,6 +1903,12 @@ static int rpa_dbex_compile_rule(rpadbex_t *dbex, rparule_t rid)
         * with the correct rid.
         */
        rpa_compiler_rulepref_set_ruleid_s(dbex->co, rpa_dbex_name(dbex, rid), rid);
+
+       /*
+        * Build the bitmap
+        */
+       if (dbex->bitmap)
+               rpa_dbex_getrulebitmap(dbex, rid);
        if (rpa_dbex_playrecord(dbex, info->startrec) < 0)
                return -1;
        info->codeoff = codeoff;
@@ -1873,9 +1937,6 @@ int rpa_dbex_compile(rpadbex_t *dbex)
        rpa_dbex_setemit(dbex, TRUE);
 
        for (rid = rpa_dbex_first(dbex); rid >= 0; rid = rpa_dbex_next(dbex, rid)) {
-               if (dbex->bitmap)
-                       rpa_dbex_getrulebitmap(dbex, rid);
-
                if (rpa_dbex_compile_rule(dbex, rid) < 0) {
                        RPA_DBEX_SETERRINFO_CODE(dbex, RPA_E_COMPILE);
                        return -1;
index 2cfebed..b4c5749 100644 (file)
@@ -417,6 +417,9 @@ int rpa_dbex_dumpinfo(rpadbex_t *dbex);
  */
 int rpa_dbex_dumpuids(rpadbex_t *dbex);
 
+
+ruword rpa_dbex_getrulebitmap(rpadbex_t *dbex, rparule_t rid);
+
 /**
  * @example personname.c
  */
index c160f7f..8eadaa7 100644 (file)
@@ -57,7 +57,6 @@ struct rpadbex_s {
        rarray_t *records;
        rarray_t *temprecords;
        rharray_t *rules;
-       rharray_t *abortrules; // these are rules used in (#!abort rulename) directive
        rarray_t *recstack;
        rarray_t *inlinestack;
        rarray_t *text;
index 88e384c..989dea4 100644 (file)
@@ -347,7 +347,7 @@ int rpa_record_loopchar(rparecord_t *prec, int defc)
 }
 
 
-void rpa_record_dump(rarray_t *records, long rec)
+void rpa_record_dump(rarray_t *records, long rec, int wantuserdata)
 {
        rparecord_t *prec;
        long start, end, first, last, next, prev, parent;
@@ -389,11 +389,12 @@ void rpa_record_dump(rarray_t *records, long rec)
        n = 90;
        n += r_snprintf(buf + n, n < bufsize ? bufsize - n : 0, " %5d, %4d", prec->top, prec->size);
 
-#if 1
-       r_memset(buf + n, ' ', bufsize - n);
-       n = 105;
-       n += r_snprintf(buf + n, n < bufsize ? bufsize - n : 0, "[ 0x%016lx ]", prec->userdata);
-#endif
+       if (wantuserdata) {
+               r_memset(buf + n, ' ', bufsize - n);
+               n = 105;
+               n += r_snprintf(buf + n, n < bufsize ? bufsize - n : 0, "[ 0x%016lx ]", prec->userdata);
+       }
+
        r_memset(buf + n, ' ', bufsize - n);
        n = 130;
        n += r_snprintf(buf + n, n < bufsize ? bufsize - n : 0, " %c %c %c", optc,
index 1d1ace6..86b0189 100644 (file)
@@ -222,7 +222,7 @@ void rpa_records_destroy(rarray_t *records);
 int rpa_recordtree_move(rarray_t *records, long dst, long src, long size);
 
 void rpa_record_dumpindented(rarray_t *records, long rec, int level);
-void rpa_record_dump(rarray_t *records, long rec);
+void rpa_record_dump(rarray_t *records, long rec, int wantuserdata);
 long rpa_record_getruleuid(rarray_t *records, long rec);
 void rpa_record_setusertype(rarray_t *records, long rec, ruint32 usertype, rvalset_t op);
 long rpa_record_getusertype(rarray_t *records, long rec);
index 4a25a31..85cd361 100644 (file)
@@ -184,6 +184,7 @@ void rpa_grep_list_patterns(rpa_grep_t *pGrep)
 
 void rpa_grep_dump_pattern_records(rpa_grep_t *pGrep)
 {
+       rpa_dbex_compile(pGrep->hDbex);
        rpa_dbex_dumprecords(pGrep->hDbex);
 }
 
@@ -240,7 +241,7 @@ void dump_ast_records(rarray_t *records)
 
        prec = (rparecord_t *)r_array_slot(records, i);
        while (prec->type & (RPA_RECORD_START | RPA_RECORD_END)) {
-               rpa_record_dump(records, i);
+               rpa_record_dump(records, i, 0);
                prec = (rparecord_t *)r_array_slot(records, ++i);
        }
 }
index dcf79b2..e966b85 100644 (file)
@@ -162,7 +162,7 @@ int main(int argc, char *argv[])
        rpa_stat_cachedisable(stat, 0);
        rvm_cpu_exec(stat->cpu, rvm_codegen_getcode(co->cg, 0), mainoff);
        for (i = 0;  i < r_array_length(records); i++) {
-               rpa_record_dump(records, i);
+               rpa_record_dump(records, i, 0);
        }
 
        r_printf("(%s) Matched size: %s(cache hits: %d)\n", argv[0], RVM_CPUREG_GETU(stat->cpu, R0) == 8 ? "PASSED" : "FAILED", stat->cache->hit);
index 1277fb7..8b67e60 100644 (file)
@@ -145,7 +145,7 @@ int main(int argc, char *argv[])
        rvm_cpu_exec(stat->cpu, rvm_codegen_getcode(co->cg, 0), mainoff);
 
        for (i = 0; i < r_array_length(stat->records); i++) {
-               rpa_record_dump(stat->records, i);
+               rpa_record_dump(stat->records, i, 0);
        }
 
        r_printf("(%s) Matched size: %s\n", argv[0], RVM_CPUREG_GETU(stat->cpu, R0) == 39 ? "PASSED" : "FAILED");
index 2676e01..3026c6d 100644 (file)
@@ -337,7 +337,7 @@ int main(int argc, char *argv[])
                rvm_cpu_exec(stat->cpu, rvm_codegen_getcode(co->cg, 0), mainoff);
        }
        for (i = 0; i < r_array_length(stat->records); i++) {
-               rpa_record_dump(stat->records, i);
+               rpa_record_dump(stat->records, i, 0);
        }
 
        r_printf("(%s) Matched size: %s\n", argv[0], RVM_CPUREG_GETU(stat->cpu, R0) == 151 ? "PASSED" : "FAILED");