Annotation of gforth/vmgen-ex2/peephole.c, revision 1.6

1.1       anton       1: /* Peephole optimization routines and tables
                      2: 
1.5       anton       3:   Copyright (C) 2001,2002,2003 Free Software Foundation, Inc.
1.1       anton       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
1.6     ! anton       9:   as published by the Free Software Foundation, either version 3
1.1       anton      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
1.6     ! anton      18:   along with this program; if not, see http://www.gnu.org/licenses/.
1.1       anton      19: */
                     20: 
                     21: #include <stdlib.h>
                     22: #include "mini.h"
                     23: 
                     24: /* the numbers in this struct are primitive indices */
                     25: typedef struct Combination {
                     26:   int prefix;
                     27:   int lastprim;
                     28:   int combination_prim;
                     29: } Combination;
                     30: 
                     31: Combination peephole_table[] = {
                     32: #include "mini-peephole.i"
1.2       anton      33: #ifndef __GNUC__
                     34:   {-1,-1,-1} /* unnecessary; just to shut up lcc if the file is empty */
                     35: #endif
1.1       anton      36: };
                     37: 
1.3       anton      38: int use_super = 1; /* turned off by option -p */
1.1       anton      39: 
                     40: typedef struct Peeptable_entry {
                     41:   struct Peeptable_entry *next;
                     42:   Label prefix;
                     43:   Label lastprim;
                     44:   Label combination_prim;
                     45: } Peeptable_entry;
                     46: 
                     47: #define HASH_SIZE 1024
                     48: #define hash(_i1,_i2) (((((long)(_i1))^((long)(_i2)))>>4)&(HASH_SIZE-1))
                     49: 
                     50: struct Peeptable_entry **peeptable;
                     51: 
                     52: Peeptable_entry **prepare_peephole_table(Label insts[])
                     53: {
                     54:   long i;
                     55:   Peeptable_entry **pt = (Peeptable_entry **)calloc(HASH_SIZE,sizeof(Peeptable_entry *));
                     56: 
                     57:   for (i=0; i<sizeof(peephole_table)/sizeof(peephole_table[0]); i++) {
                     58:     Combination *c = &peephole_table[i];
                     59:     Peeptable_entry *p = (Peeptable_entry *)malloc(sizeof(Peeptable_entry));
                     60:     long h;
                     61:     p->prefix =           insts[c->prefix];
                     62:     p->lastprim =         insts[c->lastprim];
                     63:     p->combination_prim = insts[c->combination_prim];
                     64:     h = hash((p->prefix),(p->lastprim));
                     65:     p->next = pt[h];
                     66:     pt[h] = p;
                     67:   }
                     68:   return pt;
                     69: }
                     70: 
                     71: void init_peeptable(void)
                     72: {
                     73:   peeptable = prepare_peephole_table(vm_prim);
                     74: }
                     75: 
                     76: Label peephole_opt(Label inst1, Label inst2, Peeptable_entry **peeptable)
                     77: {
                     78:   Peeptable_entry **pt = (Peeptable_entry **)peeptable;
                     79:   Peeptable_entry *p;
                     80: 
                     81:   if (use_super == 0)
                     82:       return NULL;
                     83:   for (p = pt[hash(inst1,inst2)]; p != NULL; p = p->next)
                     84:     if (inst1 == p->prefix && inst2 == p->lastprim)
                     85:       return p->combination_prim;
                     86:   return NULL;
                     87: }
                     88: 
                     89: Inst *last_compiled = NULL;
                     90: 
                     91: void gen_inst(Inst **vmcodepp, Label i)
                     92: {
                     93:   if (last_compiled != NULL) {
                     94:     Label combo = peephole_opt((*last_compiled).inst, i, peeptable);
                     95:     if (combo != NULL) {
                     96:       (*last_compiled).inst = combo;
                     97:       return;
                     98:     }
                     99:   }
                    100:   last_compiled = *vmcodepp;
                    101:   (**vmcodepp).inst = i;
                    102:   (*vmcodepp)++;
                    103: }

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