--- gforth/engine/peephole.c 2001/02/24 13:44:39 1.1 +++ gforth/engine/peephole.c 2001/03/28 16:18:51 1.3 @@ -21,7 +21,7 @@ #include "config.h" #include "forth.h" -#include "stdlib.h" +#include /* the numbers in this struct are primitive indices */ typedef struct Combination { @@ -34,37 +34,50 @@ Combination peephole_table[] = { #include "peephole.i" }; -Xt *primtable(Label symbols[], Cell size) -{ -#ifdef DIRECT_THREADED - return symbols; -#else /* !defined(DIRECT_THREADED) */ - Xt *xts = (Xt *)malloc(size*sizeof(Xt)); - Cell i; +int use_super = 1; - for (i=0; i>4)&(HASH_SIZE-1)) -Xt peephole_opt(Xt xt1, Xt xt2, Cell peeptable) +Cell peeptable; + +Cell prepare_peephole_table(Inst insts[]) { - Xt *xts = (Xt *)peeptable; Cell i; + Peeptable_entry **pt = (Peeptable_entry **)calloc(HASH_SIZE,sizeof(Peeptable_entry *)); - for (i=0; i<(sizeof(peephole_table)/sizeof(Combination)); i++) { + for (i=0; iprefix] && xt2 == xts[c->lastprim]) - return xts[c->combination_prim]; + Peeptable_entry *p = (Peeptable_entry *)malloc(sizeof(Peeptable_entry)); + Cell h; + p->prefix = insts[c->prefix]; + p->lastprim = insts[c->lastprim]; + p->combination_prim = insts[c->combination_prim]; + h = hash(p->prefix,p->lastprim); + p->next = pt[h]; + pt[h] = p; } - return 0; + return (Cell)pt; +} + +Inst peephole_opt(Inst inst1, Inst inst2, Cell peeptable) +{ + Peeptable_entry **pt = (Peeptable_entry **)peeptable; + Peeptable_entry *p; + + if (use_super == 0) + return 0; + for (p = pt[hash(inst1,inst2)]; p != NULL; p = p->next) + if (inst1 == p->prefix && inst2 == p->lastprim) + return p->combination_prim; + return NULL; }