Annotation of gforth/engine/peephole.c, revision 1.5

1.1       anton       1: /* Peephole optimization routines and tables
                      2: 
                      3:   Copyright (C) 2001 Free Software Foundation, Inc.
                      4: 
                      5:   This file is part of Gforth.
                      6: 
                      7:   Gforth is free software; you can redistribute it and/or
                      8:   modify it under the terms of the GNU General Public License
                      9:   as published by the Free Software Foundation; either version 2
                     10:   of the License, or (at your option) any later version.
                     11: 
                     12:   This program is distributed in the hope that it will be useful,
                     13:   but WITHOUT ANY WARRANTY; without even the implied warranty of
                     14:   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     15:   GNU General Public License for more details.
                     16: 
                     17:   You should have received a copy of the GNU General Public License
                     18:   along with this program; if not, write to the Free Software
                     19:   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
                     20: */
                     21: 
                     22: #include "config.h"
                     23: #include "forth.h"
1.2       anton      24: #include <stdlib.h>
1.4       ak042      25: #include <string.h>
                     26: #include <assert.h>
1.1       anton      27: 
                     28: /* the numbers in this struct are primitive indices */
                     29: typedef struct Combination {
                     30:   int prefix;
                     31:   int lastprim;
                     32:   int combination_prim;
                     33: } Combination;
                     34: 
                     35: Combination peephole_table[] = {
                     36: #include "peephole.i"
                     37: };
                     38: 
1.3       anton      39: int use_super = 1;
                     40: 
                     41: typedef Xt Inst;
1.1       anton      42: 
1.3       anton      43: typedef struct Peeptable_entry {
                     44:   struct Peeptable_entry *next;
                     45:   Inst prefix;
                     46:   Inst lastprim;
                     47:   Inst combination_prim;
                     48: } Peeptable_entry;
1.1       anton      49: 
1.3       anton      50: #define HASH_SIZE 1024
                     51: #define hash(_i1,_i2) (((((Cell)(_i1))^((Cell)(_i2)))>>4)&(HASH_SIZE-1))
1.1       anton      52: 
1.3       anton      53: Cell peeptable;
1.1       anton      54: 
1.3       anton      55: Cell prepare_peephole_table(Inst insts[])
1.1       anton      56: {
                     57:   Cell i;
1.3       anton      58:   Peeptable_entry **pt = (Peeptable_entry **)calloc(HASH_SIZE,sizeof(Peeptable_entry *));
1.1       anton      59: 
1.3       anton      60:   for (i=0; i<sizeof(peephole_table)/sizeof(peephole_table[0]); i++) {
1.1       anton      61:     Combination *c = &peephole_table[i];
1.3       anton      62:     Peeptable_entry *p = (Peeptable_entry *)malloc(sizeof(Peeptable_entry));
                     63:     Cell h;
                     64:     p->prefix =           insts[c->prefix];
                     65:     p->lastprim =         insts[c->lastprim];
                     66:     p->combination_prim = insts[c->combination_prim];
                     67:     h = hash(p->prefix,p->lastprim);
                     68:     p->next = pt[h];
                     69:     pt[h] = p;
1.1       anton      70:   }
1.3       anton      71:   return (Cell)pt;
                     72: }
                     73: 
                     74: Inst peephole_opt(Inst inst1, Inst inst2, Cell peeptable)
                     75: {
                     76:   Peeptable_entry **pt = (Peeptable_entry **)peeptable;
                     77:   Peeptable_entry *p;
                     78: 
                     79:   if (use_super == 0)
                     80:       return 0;
                     81:   for (p = pt[hash(inst1,inst2)]; p != NULL; p = p->next)
                     82:     if (inst1 == p->prefix && inst2 == p->lastprim)
                     83:       return p->combination_prim;
                     84:   return NULL;
1.1       anton      85: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>