RPA Toolkit
fixed the caching system -> reverted back to the good old record copy cache system.
authorMartin Stoilov <martin@rpasearch.com>
Wed, 4 May 2011 04:14:29 +0000 (21:14 -0700)
committerMartin Stoilov <martin@rpasearch.com>
Wed, 4 May 2011 04:14:29 +0000 (21:14 -0700)
rpa2/rpacache.c
rpa2/rpacache.h
rpa2/rpacompiler.c
rpa2/rpadbex.c
rpa2/rpaparser.c
rpa2/rpastat.c
rpa2/rpavm.c
rpa2/rpavm.h

index 51a5d0a..2ea8a6e 100644 (file)
@@ -2,23 +2,31 @@
 #include "rmem.h"
 #include "rparecord.h"
 
-#define RPA_MCACHE_BUCKET(_top_, _ruleid_) ( ( (((rulong)(_top_))<<7) ^ ((((rulong)(_ruleid_))>>4)) ) & RPA_MCACHE_MASK)
+#define RPA_MCACHE_BUCKET(_top_, _ruleid_) ( ( (((rulong)(_top_))<<3) ^ ((((rulong)(_ruleid_))>>5)) ) & RPA_MCACHE_MASK)
 
 
 rpacache_t *rpa_cache_create()
 {
+       rlong i;
        rpacache_t *cache = (rpacache_t*) r_zmalloc(sizeof(*cache));
 
        if (!cache)
                return NULL;
+       for (i = 0; i < RPA_MCACHE_SIZE; i++) {
+               cache->entry[i].records = r_array_create(sizeof(rparecord_t));
+       }
        return cache;
 }
 
 
 void rpa_cache_destroy(rpacache_t *cache)
 {
+       rlong i;
        if (!cache)
                return;
+       for (i = 0; i < RPA_MCACHE_SIZE; i++) {
+               r_array_destroy(cache->entry[i].records);
+       }
        r_free(cache);
 }
 
@@ -34,19 +42,28 @@ void rpa_cache_disable(rpacache_t *cache, rlong disable)
 }
 
 
-void rpa_cache_set(rpacache_t *cache, rlong top, rlong ruleid, rlong ret, rlong startrec, rlong endrec)
+void rpa_cache_set(rpacache_t *cache, rlong top, rlong ruleid, rlong ret, rarray_t *records, rlong startrec, rlong size)
 {
+       rlong i;
        rulong bucket = RPA_MCACHE_BUCKET(top, ruleid);
+       rarray_t *cacherecords;
 
        if (cache->disalbled)
                return;
 
+       if (ret > 0 && size > 128)
+               return;
+       cacherecords = cache->entry[bucket].records;
        cache->entry[bucket].ruleid = ruleid;
        cache->entry[bucket].top = top;
        cache->entry[bucket].ret = ret;
        cache->entry[bucket].startrec = startrec;
-       cache->entry[bucket].endrec = endrec;
+       cache->entry[bucket].recsize = size;
        cache->entry[bucket].serial = cache->serial;
+       r_array_setlength(cacherecords, 0);
+       for (i = 0; i < size; i++) {
+               r_array_add(cacherecords, r_array_slot(records, startrec + i));
+       }
 
 }
 
@@ -56,6 +73,9 @@ rpacachedentry_t *rpa_cache_lookup(rpacache_t *cache, rlong top, rlong ruleid)
        rulong bucket = RPA_MCACHE_BUCKET(top, ruleid);
        rpacachedentry_t *entry = &cache->entry[bucket];
 
+       if (cache->disalbled)
+               return NULL;
+
        if (entry->serial == cache->serial && entry->ruleid == ruleid && entry->top == top) {
 //             rparecord_t *prec = (rparecord_t *)r_array_slot(entry->records, 0);
 //             r_printf("HIT the cache @ %ld,  top = %ld, ret = %ld, rulename = %s\n", bucket, entry->top, entry->ret, prec->rule);
index 88af728..5d8a6ca 100644 (file)
@@ -9,7 +9,7 @@
 extern "C" {
 #endif
 
-#define RPA_MCACHE_BITS 12
+#define RPA_MCACHE_BITS 9
 #define RPA_MCACHE_SIZE (1 << RPA_MCACHE_BITS)
 #define RPA_MCACHE_MASK (RPA_MCACHE_SIZE - 1)
 
@@ -19,9 +19,9 @@ typedef struct rpacachedentry_s {
        rlong top;
        rlong ret;
        rlong startrec;
-       rlong endrec;
+       rlong recsize;
        rulong serial;
-//     rarray_t *records;
+       rarray_t *records;
 } rpacachedentry_t;
 
 typedef struct rpacache_s {
@@ -36,7 +36,7 @@ rpacache_t *rpa_cache_create();
 void rpa_cache_destroy(rpacache_t *cache);
 void rpa_cache_disable(rpacache_t *cache, rlong disable);
 void rpa_cache_invalidate(rpacache_t *cache);
-void rpa_cache_set(rpacache_t *cache, rlong top, rlong ruleid, rlong ret, rlong startrec, rlong endrec);
+void rpa_cache_set(rpacache_t *cache, rlong top, rlong ruleid, rlong ret, rarray_t *records, rlong startrec, rlong size);
 rpacachedentry_t *rpa_cache_lookup(rpacache_t *cache, rlong top, rlong ruleid);
 #ifdef __cplusplus
 }
index 8cab264..f562c39 100644 (file)
@@ -314,7 +314,6 @@ rint rpa_compiler_loop_begin(rpa_compiler_t *co, const rchar *name, ruint namesi
        rvm_codegen_addins(co->cg, rvm_asm(RVM_MOV, R_LOO, R3, XX, 0));
        if (exp.rulepref && (exp.rulepref->flags & RPA_RFLAG_EMITRECORD)) {
                rvm_codegen_index_addrelocins(co->cg, RVM_RELOC_BLOB, exp.dataidx, rvm_asm(RPA_EMITSTART, DA, R_TOP, XX, 0));
-               rvm_codegen_index_addrelocins(co->cg, RVM_RELOC_BLOB, exp.dataidx, rvm_asm(RPA_PUSHSTARTREC, DA, XX, XX, 0));
        }
        r_array_add(co->expressions, &exp);
        return 0;
@@ -338,7 +337,6 @@ rint rpa_compiler_loop_end(rpa_compiler_t *co)
        rvm_codegen_index_addrelocins(co->cg, RVM_RELOC_BRANCH, exp.failidx, rvm_asm(RVM_BEQ, DA, XX, XX, 0));  // ------------- R_TOP is the same
        if (exp.rulepref && (exp.rulepref->flags & RPA_RFLAG_EMITRECORD)) {                                            //       |
                rvm_codegen_index_addrelocins(co->cg, RVM_RELOC_BLOB, exp.dataidx, rvm_asm(RPA_EMITEND, DA, R_OTP, R0, 0)); //      |
-               rvm_codegen_index_addrelocins(co->cg, RVM_RELOC_BLOB, exp.dataidx, rvm_asm(RPA_POPSTARTREC, DA, XX, XX, 0));//      |
        }
        rvm_codegen_addins(co->cg, rvm_asm(RVM_MOV, R3, R0, XX, 0));                                                //          |
        rvm_codegen_addins(co->cg, rvm_asm(RVM_MOV, R_TOP, R_OTP, XX, 0));                                          //          |
@@ -346,18 +344,12 @@ rint rpa_compiler_loop_end(rpa_compiler_t *co)
        rvm_codegen_redefinelabel(co->cg, exp.failidx);                                                             //          |
        rvm_codegen_addins(co->cg, rvm_asm(RVM_MOV, R_REC, R1, XX, 0));          //        <-------------------------------------
        rvm_codegen_addins(co->cg, rvm_asm(RVM_MOVS, R0, DA, XX, -1));
-       if (exp.rulepref && (exp.rulepref->flags & RPA_RFLAG_EMITRECORD)) {
-               rvm_codegen_index_addrelocins(co->cg, RVM_RELOC_BLOB, exp.dataidx, rvm_asm(RPA_POPORPHANREC, DA, XX, XX, 0));
-       }
        rvm_codegen_addins(co->cg, rvm_asm(RVM_BX, LR, XX, XX, 0));
 
        /*
         *  END FAILED:
         */
        rvm_codegen_redefinelabel(co->cg, exp.endidx);
-       if (exp.rulepref && (exp.rulepref->flags & RPA_RFLAG_EMITRECORD)) {
-               rvm_codegen_index_addrelocins(co->cg, RVM_RELOC_BLOB, exp.dataidx, rvm_asm(RPA_POPORPHANREC, DA, XX, XX, 0));
-       }
        rvm_codegen_addins(co->cg, rvm_asm(RVM_MOV, R3, R_LOO, XX, 0));         // Save LOO to R3 before restoring the old one
        rvm_codegen_addins(co->cg, rvm_asm(RVM_POP, R0, XX, XX, 0));            // Pop the accumulated ret, use it to save the status for return
        rvm_codegen_addins(co->cg, rvm_asm(RVM_POPM, DA, XX, XX, BIT(R_REC)|BIT(R_LOO)|BIT(R_TOP)|BIT(LR)));
@@ -393,10 +385,8 @@ rint rpa_compiler_rule_begin(rpa_compiler_t *co, const rchar *name, ruint namesi
        rvm_codegen_addins(co->cg, rvm_asm(RVM_PUSH, R_REC, XX, XX, 0));
        rvm_codegen_addins(co->cg, rvm_asm(RVM_PUSH, R_TOP, XX, XX, 0));
        rvm_codegen_addins(co->cg, rvm_asm(RVM_PUSH, LR, XX, XX, 0));
-
        if (exp.rulepref && (exp.rulepref->flags & RPA_RFLAG_EMITRECORD)) {
                rvm_codegen_index_addrelocins(co->cg, RVM_RELOC_BLOB, exp.dataidx, rvm_asm(RPA_EMITSTART, DA, R_TOP, XX, 0));
-               rvm_codegen_index_addrelocins(co->cg, RVM_RELOC_BLOB, exp.dataidx, rvm_asm(RPA_PUSHSTARTREC, DA, XX, XX, 0));
        }
 
        r_array_add(co->expressions, &exp);
@@ -420,9 +410,8 @@ rint rpa_compiler_rule_end(rpa_compiler_t *co)
        rvm_codegen_addins(co->cg, rvm_asm(RVM_SUBS, R0, R_TOP, R_OTP, 0));
 
        if (exp.rulepref && (exp.rulepref->flags & RPA_RFLAG_EMITRECORD)) {
-               rvm_codegen_addins(co->cg, rvm_asm(RVM_BEQ, DA, XX, XX, 5));
+               rvm_codegen_addins(co->cg, rvm_asm(RVM_BEQ, DA, XX, XX, 4));
                rvm_codegen_index_addrelocins(co->cg, RVM_RELOC_BLOB, exp.dataidx, rvm_asm(RPA_EMITEND, DA, R_OTP, R0, 0));
-               rvm_codegen_index_addrelocins(co->cg, RVM_RELOC_BLOB, exp.dataidx, rvm_asm(RPA_POPSTARTREC, DA, XX, XX, 0));
        } else {
                rvm_codegen_addins(co->cg, rvm_asm(RVM_BEQ, DA, XX, XX, 3));
        }
@@ -430,9 +419,6 @@ rint rpa_compiler_rule_end(rpa_compiler_t *co)
        rvm_codegen_addins(co->cg, rvm_asm(RVM_BX, LR, XX, XX, 0));
        rvm_codegen_addins(co->cg, rvm_asm(RVM_MOV, R_REC, R1, XX, 0));
        rvm_codegen_addins(co->cg, rvm_asm(RVM_MOVS, R0, DA, XX, -1));
-       if (exp.rulepref && (exp.rulepref->flags & RPA_RFLAG_EMITRECORD)) {
-               rvm_codegen_index_addrelocins(co->cg, RVM_RELOC_BLOB, exp.dataidx, rvm_asm(RPA_POPORPHANREC, DA, XX, XX, 0));
-       }
        rvm_codegen_addins(co->cg, rvm_asm(RVM_BX, LR, XX, XX, 0));
        rvm_codegen_redefinelabel(co->cg, exp.endidx);
        rvm_codegen_addins(co->cg, rvm_asm(RVM_POP, LR, XX, XX, 0));
@@ -440,9 +426,6 @@ rint rpa_compiler_rule_end(rpa_compiler_t *co)
        rvm_codegen_addins(co->cg, rvm_asm(RVM_POP, R_REC, XX, XX, 0));
        rvm_codegen_addins(co->cg, rvm_asm(RVM_MOV, R_OTP, R_TOP, XX, 0));
        rvm_codegen_addins(co->cg, rvm_asm(RVM_MOVS, R0, DA, XX, -1));
-       if (exp.rulepref && (exp.rulepref->flags & RPA_RFLAG_EMITRECORD)) {
-               rvm_codegen_index_addrelocins(co->cg, RVM_RELOC_BLOB, exp.dataidx, rvm_asm(RPA_POPORPHANREC, DA, XX, XX, 0));
-       }
        rvm_codegen_addins(co->cg, rvm_asml(RPA_SETCACHE, DA, R_REC, R_REC, exp.start));
        rvm_codegen_addins(co->cg, rvm_asm(RVM_BX, LR, XX, XX, 0));
        return 0;
@@ -473,7 +456,6 @@ rint rpa_compiler_inlinerule_begin(rpa_compiler_t *co, const rchar *name, ruint
        rvm_codegen_addins(co->cg, rvm_asm(RVM_PUSH, LR, XX, XX, 0));
        if (exp.rulepref && (exp.rulepref->flags & RPA_RFLAG_EMITRECORD)) {
                rvm_codegen_index_addrelocins(co->cg, RVM_RELOC_BLOB, exp.dataidx, rvm_asm(RPA_EMITSTART, DA, R_TOP, XX, 0));
-               rvm_codegen_index_addrelocins(co->cg, RVM_RELOC_BLOB, exp.dataidx, rvm_asm(RPA_PUSHSTARTREC, DA, XX, XX, 0));
        }
        r_array_add(co->expressions, &exp);
        return 0;
@@ -495,27 +477,20 @@ rint rpa_compiler_inlinerule_end(rpa_compiler_t *co)
        rvm_codegen_addins(co->cg, rvm_asm(RVM_POP, R1, XX, XX, 0));
        rvm_codegen_addins(co->cg, rvm_asm(RVM_SUBS, R0, R_TOP, R_OTP, 0));
        if (exp.rulepref && (exp.rulepref->flags & RPA_RFLAG_EMITRECORD)) {
-               rvm_codegen_addins(co->cg, rvm_asm(RVM_BEQ, DA, XX, XX, 4));
+               rvm_codegen_addins(co->cg, rvm_asm(RVM_BEQ, DA, XX, XX, 3));
                rvm_codegen_index_addrelocins(co->cg, RVM_RELOC_BLOB, exp.dataidx, rvm_asm(RPA_EMITEND, DA, R_OTP, R0, 0));
-               rvm_codegen_index_addrelocins(co->cg, RVM_RELOC_BLOB, exp.dataidx, rvm_asm(RPA_POPSTARTREC, DA, XX, XX, 0));
        } else {
                rvm_codegen_addins(co->cg, rvm_asm(RVM_BEQ, DA, XX, XX, 2));
        }
        rvm_codegen_addins(co->cg, rvm_asm(RVM_BX, LR, XX, XX, 0));
        rvm_codegen_addins(co->cg, rvm_asm(RVM_MOV, R_REC, R1, XX, 0));
        rvm_codegen_addins(co->cg, rvm_asm(RVM_MOVS, R0, DA, XX, -1));
-       if (exp.rulepref && (exp.rulepref->flags & RPA_RFLAG_EMITRECORD)) {
-               rvm_codegen_index_addrelocins(co->cg, RVM_RELOC_BLOB, exp.dataidx, rvm_asm(RPA_POPORPHANREC, DA, XX, XX, 0));
-       }
        rvm_codegen_addins(co->cg, rvm_asm(RVM_BX, LR, XX, XX, 0));
        rvm_codegen_redefinelabel(co->cg, exp.endidx);
        rvm_codegen_addins(co->cg, rvm_asm(RVM_POP, LR, XX, XX, 0));
        rvm_codegen_addins(co->cg, rvm_asm(RVM_POP, R_TOP, XX, XX, 0));
        rvm_codegen_addins(co->cg, rvm_asm(RVM_POP, R_REC, XX, XX, 0));
        rvm_codegen_addins(co->cg, rvm_asm(RVM_MOVS, R0, DA, XX, -1));
-       if (exp.rulepref && (exp.rulepref->flags & RPA_RFLAG_EMITRECORD)) {
-               rvm_codegen_index_addrelocins(co->cg, RVM_RELOC_BLOB, exp.dataidx, rvm_asm(RPA_POPORPHANREC, DA, XX, XX, 0));
-       }
        rvm_codegen_addins(co->cg, rvm_asm(RVM_BX, LR, XX, XX, 0));
        rvm_codegen_replaceins(co->cg, exp.branch, rvm_asm(RVM_B, DA, XX, XX, rvm_codegen_getcodesize(co->cg) - exp.branch));
        rpa_compiler_index_reference(co, exp.startidx, (exp.flags & RPA_MATCH_MASK));
index 9c65c19..9524022 100644 (file)
@@ -241,7 +241,6 @@ static rint rpa_dbex_rh_namedrule(rpadbex_t *dbex, rlong rec)
                if (!r_array_empty(dbex->inlinestack)) {
                        rpa_compiler_inlinerule_begin(dbex->co, name, namesize, 0);
                } else {
-                       rvm_codegen_addins(dbex->co->cg, rvm_asm(RPA_EMITHEAD, XX, XX, XX, 0));
                        rvm_codegen_addins(dbex->co->cg, rvm_asm(RPA_SHIFT, XX, XX, XX, 0));
                        rvm_codegen_addins(dbex->co->cg, rvm_asm(RVM_BL, DA, XX, XX, 3));
                        rvm_codegen_addins(dbex->co->cg, rvm_asm(RPA_EMITTAIL, XX, XX, XX, 0));
@@ -276,7 +275,6 @@ static rint rpa_dbex_rh_anonymousrule(rpadbex_t *dbex, rlong rec)
        rparecord_t *prec = (rparecord_t *) r_array_slot(dbex->records, rec);
 
        if (prec->type & RPA_RECORD_START) {
-               rvm_codegen_addins(dbex->co->cg, rvm_asm(RPA_EMITHEAD, XX, XX, XX, 0));
                rvm_codegen_addins(dbex->co->cg, rvm_asm(RPA_SHIFT, XX, XX, XX, 0));
                rpa_compiler_exp_begin(dbex->co, RPA_MATCH_NONE);
 
index bbace7d..9e43a8d 100644 (file)
@@ -39,7 +39,7 @@ rlong rpa_parser_load(rpa_parser_t *pa, const rchar *prods, rsize_t size)
        rpastat_t *stat = pa->stat;
 
        rpa_stat_init(stat, prods, prods, prods + size);
-       rpa_stat_cachedisable(stat, 1);
+       rpa_stat_cachedisable(stat, 0);
 
        if (rpa_stat_exec(stat, rvm_codegen_getcode(co->cg, 0), pa->main) < 0)
                return -1;
@@ -1397,7 +1397,6 @@ static rint rpa_parser_init(rpa_parser_t *pa)
        rvm_codegen_addins(co->cg, rvm_asml(RVM_MOV, R_LOO, DA, XX, 0));
        rvm_codegen_addins(co->cg, rvm_asml(RVM_MOV, R_TOP, DA, XX, -1));
 
-       rvm_codegen_addins(co->cg, rvm_asm(RPA_EMITHEAD, XX, XX, XX, 0));
        rvm_codegen_addins(co->cg, rvm_asm(RPA_SHIFT, XX, XX, XX, 0));
        rpa_compiler_reference_nan_s(co, "bnf");
        rvm_codegen_addins(co->cg, rvm_asm(RPA_EMITTAIL, XX, XX, XX, 0));
index 1220478..8d5fc45 100644 (file)
@@ -125,6 +125,7 @@ static void rpa_stat_fixrecords(rpastat_t *stat)
        rarray_t *records;
        rparecord_t *cur = (rparecord_t *)r_array_slot(stat->records, 0);
 
+       return;
        cur = rpa_stat_nextrecord(stat, cur);
        if (!cur) {
                /*
index bfc8ad2..47ca3e3 100644 (file)
@@ -209,32 +209,17 @@ static void rpavm_swi_matchrng_mop(rvmcpu_t *cpu, rvm_asmins_t *ins)
 }
 
 
-
-static void rpavm_swi_emithead(rvmcpu_t *cpu, rvm_asmins_t *ins)
-{
-       rparecord_t *rec;
-       rpastat_t *stat = (rpastat_t *)cpu->userdata1;
-       rlong index = RVM_CPUREG_GETL(cpu, R_REC);
-
-       index = r_array_add(stat->records, NULL);
-       RVM_CPUREG_SETL(cpu, R_REC, index);
-       rec = (rparecord_t *)r_array_slot(stat->records, index);
-       rec->type = RPA_RECORD_HEAD;
-}
-
-
 static void rpavm_swi_emittail(rvmcpu_t *cpu, rvm_asmins_t *ins)
 {
-       rparecord_t *rec, *crec;
+       rparecord_t *rec;
        rpastat_t *stat = (rpastat_t *)cpu->userdata1;
        rlong index = RVM_CPUREG_GETL(cpu, R_REC);
 
-       index = r_array_add(stat->records, NULL);
-       crec = (rparecord_t *)r_array_slot(stat->records, RVM_CPUREG_GETL(cpu, R_REC));
+       index = r_array_replace(stat->records, index + 1, NULL);
        RVM_CPUREG_SETL(cpu, R_REC, index);
        rec = (rparecord_t *)r_array_slot(stat->records, index);
        rec->type = RPA_RECORD_TAIL;
-       crec->next = index;
+       r_array_setlength(stat->records, index);
 }
 
 
@@ -242,7 +227,7 @@ static void rpavm_swi_emitstart(rvmcpu_t *cpu, rvm_asmins_t *ins)
 {
        rpa_ruledata_t *ruledata = RVM_CPUREG_GETP(cpu, ins->op1);
        rpastat_t *stat = (rpastat_t *)cpu->userdata1;
-       rparecord_t *rec, *crec;
+       rparecord_t *rec;
        rlong index = RVM_CPUREG_GETL(cpu, R_REC);
        rword tp = RVM_CPUREG_GETU(cpu, ins->op2);
        rstr_t name = {(rchar*)ruledata + ruledata->name, ruledata->namesize};
@@ -251,21 +236,12 @@ static void rpavm_swi_emitstart(rvmcpu_t *cpu, rvm_asmins_t *ins)
                r_printf("%ld: %s, %s, tp = %ld\n", RVM_CPUREG_GETU(cpu, FP), "START", name.str, tp);
        if (!(ruledata->flags & RPA_RFLAG_EMITRECORD))
                return;
-       R_ASSERT(RVM_CPUREG_GETL(cpu, R_REC) >= 0);
-       if (1 && (r_array_length(stat->orphans) > 0)) {
-               /*
-                * First let see if there is an orphan record that we can adopt.
-                */
-               index = r_array_pop(stat->orphans, rlong);
-       } else {
-               index = r_array_add(stat->records, NULL);
-       }
+       index = r_array_replace(stat->records, index + 1, NULL);
 
        /*
         * Important: get the pointer to crec after modifying the array, because if
         * it gets reallocated the pointer will be invalid.
         */
-       crec = (rparecord_t *)r_array_slot(stat->records, RVM_CPUREG_GETL(cpu, R_REC));
        RVM_CPUREG_SETL(cpu, R_REC, index);
        rec = (rparecord_t *)r_array_slot(stat->records, index);
        rec->rule = name.str;
@@ -275,7 +251,6 @@ static void rpavm_swi_emitstart(rvmcpu_t *cpu, rvm_asmins_t *ins)
        rec->input = stat->instack[tp].input;
        rec->inputsiz = 0;
        rec->next = 0;
-       crec->next = index;
 }
 
 
@@ -284,7 +259,7 @@ static void rpavm_swi_emitend(rvmcpu_t *cpu, rvm_asmins_t *ins)
 {
        rpa_ruledata_t *ruledata = RVM_CPUREG_GETP(cpu, ins->op1);
        rpastat_t *stat = (rpastat_t *)cpu->userdata1;
-       rparecord_t *rec, *crec;
+       rparecord_t *rec;
        rlong index = RVM_CPUREG_GETL(cpu, R_REC);
        rword tp = RVM_CPUREG_GETU(cpu, ins->op2);
        rword tplen = RVM_CPUREG_GETU(cpu, ins->op3);
@@ -295,21 +270,7 @@ static void rpavm_swi_emitend(rvmcpu_t *cpu, rvm_asmins_t *ins)
        if (!(ruledata->flags & RPA_RFLAG_EMITRECORD))
                return;
 
-       R_ASSERT(RVM_CPUREG_GETL(cpu, R_REC) >= 0);
-
-       if (1 && (r_array_length(stat->orphans) > 0)) {
-               /*
-                * First let see if there is an orphan record that we can adopt.
-                */
-               index = r_array_pop(stat->orphans, rlong);
-       } else {
-               index = r_array_add(stat->records, NULL);
-       }
-       /*
-        * Important: get the pointer to crec after modifying the array, because if
-        * it gets reallocated the pointer will be invalid.
-        */
-       crec = (rparecord_t *)r_array_slot(stat->records, RVM_CPUREG_GETL(cpu, R_REC));
+       index = r_array_replace(stat->records, index + 1, NULL);
        RVM_CPUREG_SETL(cpu, R_REC, index);
        rec = (rparecord_t *)r_array_slot(stat->records, index);
        rec->rule = name.str;
@@ -320,7 +281,6 @@ static void rpavm_swi_emitend(rvmcpu_t *cpu, rvm_asmins_t *ins)
        rec->input = stat->instack[tp].input;
        rec->inputsiz = stat->instack[tp + tplen].input - stat->instack[tp].input;
        rec->next = 0;
-       crec->next = index;
 
        if (tplen) {
                rec->type |= RPA_RECORD_MATCH;
@@ -328,50 +288,6 @@ static void rpavm_swi_emitend(rvmcpu_t *cpu, rvm_asmins_t *ins)
 }
 
 
-static void rpavm_swi_pushstart(rvmcpu_t *cpu, rvm_asmins_t *ins)
-{
-       rpastat_t *stat = (rpastat_t *)cpu->userdata1;
-       rpa_ruledata_t *ruledata = RVM_CPUREG_GETP(cpu, ins->op1);
-       rlong index = RVM_CPUREG_GETL(cpu, R_REC);
-       rstr_t name = {(rchar*)ruledata + ruledata->name, ruledata->namesize};
-
-       if (!(ruledata->flags & RPA_RFLAG_EMITRECORD))
-               return;
-       if (stat->debug)
-               r_printf("%s, %s\n", "PUSHSTART ", name.str);
-       r_array_add(stat->emitstack, &index);
-}
-
-
-static void rpavm_swi_popstart(rvmcpu_t *cpu, rvm_asmins_t *ins)
-{
-       rlong index;
-       rpastat_t *stat = (rpastat_t *)cpu->userdata1;
-       rpa_ruledata_t *ruledata = RVM_CPUREG_GETP(cpu, ins->op1);
-       rstr_t name = {(rchar*)ruledata + ruledata->name, ruledata->namesize};
-
-       if (!(ruledata->flags & RPA_RFLAG_EMITRECORD))
-               return;
-       if (stat->debug)
-               r_printf("%s, %s\n", "POPSTART ", name.str);
-       index = r_array_pop(stat->emitstack, rlong);
-}
-
-
-static void rpavm_swi_poporphan(rvmcpu_t *cpu, rvm_asmins_t *ins)
-{
-       rlong index;
-       rpastat_t *stat = (rpastat_t *)cpu->userdata1;
-       rpa_ruledata_t *ruledata = RVM_CPUREG_GETP(cpu, ins->op1);
-       rstr_t name = {(rchar*)ruledata + ruledata->name, ruledata->namesize};
-
-       if (!(ruledata->flags & RPA_RFLAG_EMITRECORD))
-               return;
-       if (stat->debug)
-               r_printf("%s, %s\n", "POPORPHAN ", name.str);
-       index = r_array_pop(stat->emitstack, rlong);
-       r_array_add(stat->orphans, &index);
-}
 
 
 static void rpavm_swi_prninfo(rvmcpu_t *cpu, rvm_asmins_t *ins)
@@ -412,28 +328,12 @@ static void rpavm_swi_setcache(rvmcpu_t *cpu, rvm_asmins_t *ins)
        rlong endrec = RVM_CPUREG_GETL(cpu, ins->op3);
        rlong top = RVM_CPUREG_GETL(cpu, R_OTP);
        rlong startrec = 0;
-       rparecord_t *prec = NULL;
-       rlong i;
-       rparecord_t *dumprec = NULL;
-
-       if (stat->cache->disalbled)
-               return;
 
        if (r0 > 0 && prevrec != endrec) {
-               prec = (rparecord_t *)r_array_slot(stat->records, prevrec);
-               startrec = prec->next;
-               rpa_cache_set(stat->cache, top, ruleid, r0, startrec, endrec);
-//             prec = (rparecord_t *)r_array_slot(stat->records, startrec);
-//             r_printf("Set the cache for: %s (%ld, %ld), top = %ld, ret = %ld, ruleid=%ld\n", prec->rule, startrec, endrec, prec->top, r0, ruleid);
-//             for (i = startrec; i != endrec; i = dumprec->next) {
-//                     dumprec = (rparecord_t *)r_array_slot(stat->records, i);
-//                     rpa_record_dump(stat->records, i);
-//             }
-//             dumprec = (rparecord_t *)r_array_slot(stat->records, i);
-//             rpa_record_dump(stat->records, i);
-//             r_printf("\n");
+               startrec = prevrec + 1;
+               rpa_cache_set(stat->cache, top, ruleid, r0, stat->records, startrec, endrec - prevrec);
        } else {
-               rpa_cache_set(stat->cache, top, ruleid, r0, 0, 0);
+               rpa_cache_set(stat->cache, top, ruleid, r0, stat->records, 0, 0);
        }
 }
 
@@ -445,26 +345,19 @@ static void rpavm_swi_checkcache(rvmcpu_t *cpu, rvm_asmins_t *ins)
        rlong ruleid = RVM_CPUREG_GETL(cpu, ins->op1);
        rlong top = RVM_CPUREG_GETL(cpu, ins->op2);
        rlong r0 = 0;
-       rlong i;
-       rpacache_t *cache = stat->cache;
-       rlong startrec;
        entry = rpa_cache_lookup(stat->cache, top, ruleid);
        if (entry) {
-//             rparecord_t *prec = (rparecord_t *)r_array_slot(stat->records, entry->startrec);
-//             r_printf("Hit the cache for: %s (%ld, %ld), r0 = %ld, ruleid: %ld\n", prec->rule, entry->startrec, entry->endrec, entry->ret, ruleid);
-
                r0 = entry->ret;
-               if (entry->startrec != entry->endrec) {
-                       rparecord_t *crec = (rparecord_t *)r_array_slot(stat->records, RVM_CPUREG_GETL(cpu, R_REC));
-                       crec->next = entry->startrec;
-                       RVM_CPUREG_SETL(cpu, R_REC, entry->endrec);
-                       startrec = entry->startrec;
-                       for (i = 0; i < RPA_MCACHE_SIZE; i++) {
-                               if (cache->entry[i].startrec == startrec)
-                                       cache->entry[i].serial = cache->serial - 1;
-                       }
-               }
+
                if (r0 > 0) {
+                       if (entry->recsize) {
+                               long i;
+                               r_array_setlength(stat->records, RVM_CPUREG_GETL(cpu, R_REC) + 1);
+                               for (i = 0; i < r_array_length(entry->records); i++) {
+                                       r_array_add(stat->records, r_array_slot(entry->records, i));
+                               }
+                               RVM_CPUREG_SETL(cpu, R_REC, r_array_length(stat->records) - 1);
+                       }
                        top += r0;
                        RVM_CPUREG_SETU(cpu, R_TOP, top);
                }
@@ -499,13 +392,9 @@ static rvm_switable_t rpavm_swi_table[] = {
                {"RPA_CHECKCACHE", rpavm_swi_checkcache},
                {"RPA_EMITSTART", rpavm_swi_emitstart},
                {"RPA_EMITEND", rpavm_swi_emitend},
-               {"RPA_EMITHEAD", rpavm_swi_emithead},
                {"RPA_EMITTAIL", rpavm_swi_emittail},
                {"RPA_GETNEXTREC", rpavm_swi_getnextrec},
                {"RPA_PRNINFO", rpavm_swi_prninfo},
-               {"RPA_PUSHSTARTREC", rpavm_swi_pushstart},
-               {"RPA_POPSTARTREC", rpavm_swi_popstart},
-               {"RPA_POPORPHANREC", rpavm_swi_poporphan},
                {NULL, NULL},
 };
 
index 15fb4d8..ea97859 100644 (file)
@@ -49,13 +49,9 @@ extern "C" {
 
 #define RPA_EMITSTART          RVM_OPSWI(RVM_SWI_ID(RPAVM_SWI_TABLEID, 16))
 #define RPA_EMITEND                    RVM_OPSWI(RVM_SWI_ID(RPAVM_SWI_TABLEID, 17))
-#define RPA_EMITHEAD           RVM_OPSWI(RVM_SWI_ID(RPAVM_SWI_TABLEID, 18))
-#define RPA_EMITTAIL           RVM_OPSWI(RVM_SWI_ID(RPAVM_SWI_TABLEID, 19))
-#define RPA_GETNEXTREC         RVM_OPSWI(RVM_SWI_ID(RPAVM_SWI_TABLEID, 20))
-#define RPA_PRNINFO                    RVM_OPSWI(RVM_SWI_ID(RPAVM_SWI_TABLEID, 21))
-#define RPA_PUSHSTARTREC       RVM_OPSWI(RVM_SWI_ID(RPAVM_SWI_TABLEID, 22))
-#define RPA_POPSTARTREC                RVM_OPSWI(RVM_SWI_ID(RPAVM_SWI_TABLEID, 23))
-#define RPA_POPORPHANREC       RVM_OPSWI(RVM_SWI_ID(RPAVM_SWI_TABLEID, 24))
+#define RPA_EMITTAIL           RVM_OPSWI(RVM_SWI_ID(RPAVM_SWI_TABLEID, 18))
+#define RPA_GETNEXTREC         RVM_OPSWI(RVM_SWI_ID(RPAVM_SWI_TABLEID, 19))
+#define RPA_PRNINFO                    RVM_OPSWI(RVM_SWI_ID(RPAVM_SWI_TABLEID, 20))