File:  [gforth] / gforth / engine / peephole.c
Revision 1.11: download - view: text, annotated - select for diffs
Mon Dec 31 19:02:25 2007 UTC (13 years, 5 months ago) by anton
Branches: MAIN
CVS tags: v0-7-0, HEAD
updated copyright year after changing license notice

    1: /* Peephole optimization routines and tables
    2: 
    3:   Copyright (C) 2001,2002,2003,2007 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 3
   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, see http://www.gnu.org/licenses/.
   19: */
   20: 
   21: #include "config.h"
   22: #include "forth.h"
   23: #include <stdlib.h>
   24: #include <string.h>
   25: #include <assert.h>
   26: 
   27: /* the numbers in this struct are primitive indices */
   28: typedef struct Combination {
   29:   int prefix;
   30:   int lastprim;
   31:   int combination_prim;
   32: } Combination;
   33: 
   34: Combination peephole_table[] = {
   35: #include PEEPHOLE_I
   36: };
   37: 
   38: #ifdef PRINT_SUPER_LENGTHS
   39: char *prim_names[] = {
   40: #include PRIM_NAMES_I
   41: };
   42: 
   43: Combination *find_super(Cell prim)
   44: {
   45:   Cell i;
   46: 
   47:   for (i=0; i<sizeof(peephole_table)/sizeof(peephole_table[0]); i++) {
   48:     if (peephole_table[i].combination_prim == prim)
   49:       return &peephole_table[i];
   50:   }
   51:   return NULL;
   52: }
   53: 
   54: Cell sum_length(Cell prim)
   55: {
   56:   Combination *super=find_super(prim);
   57: 
   58:   if (super)
   59:     return sum_length(super->prefix)+prim_length(super->lastprim);
   60:   else
   61:     return prim_length(prim);
   62: }
   63: 
   64: void print_prim(Cell prim)
   65: {
   66:   fprintf(stderr, "%s", prim_names[prim]);
   67: }  
   68: 
   69: void print_super(Cell prim)
   70: {
   71:   Combination *super=find_super(prim);
   72:   
   73:   if (super) {
   74:     print_super(super->prefix);
   75:     fprintf(stderr, " ");
   76:     print_prim(super->lastprim);
   77:   } else {
   78:     print_prim(prim);
   79:   }
   80: }
   81: 
   82: void print_super_lengths()
   83: {
   84:   Cell i;
   85: 
   86:   for (i=0; i<sizeof(peephole_table)/sizeof(peephole_table[0]); i++) {
   87:     Cell super_length = prim_length(peephole_table[i].combination_prim);
   88:     Cell sum_super_length = sum_length(peephole_table[i].combination_prim);
   89: 
   90:     fprintf(stderr, "%6.4f %3d %3d ", ((double)super_length)/sum_super_length,
   91: 	    super_length, sum_super_length);
   92:     print_super(peephole_table[i].combination_prim);
   93:     fprintf(stderr,"\n");
   94:   }
   95: }
   96: #endif
   97: 
   98: int use_super = 1;
   99: 
  100: typedef Xt Inst;
  101: 
  102: typedef struct Peeptable_entry {
  103:   struct Peeptable_entry *next;
  104:   Inst prefix;
  105:   Inst lastprim;
  106:   Inst combination_prim;
  107: } Peeptable_entry;
  108: 
  109: #define HASH_SIZE 1024
  110: #define hash(_i1,_i2) (((((Cell)(_i1))^((Cell)(_i2)))>>4)&(HASH_SIZE-1))
  111: 
  112: Cell peeptable;
  113: 
  114: Cell prepare_peephole_table(Inst insts[])
  115: {
  116:   Cell i;
  117:   Peeptable_entry **pt = (Peeptable_entry **)calloc(HASH_SIZE,sizeof(Peeptable_entry *));
  118: 
  119:   for (i=0; i<sizeof(peephole_table)/sizeof(peephole_table[0]); i++) {
  120:     Combination *c = &peephole_table[i];
  121:     Peeptable_entry *p = (Peeptable_entry *)malloc(sizeof(Peeptable_entry));
  122:     Cell h;
  123:     p->prefix =           insts[c->prefix];
  124:     p->lastprim =         insts[c->lastprim];
  125:     p->combination_prim = insts[c->combination_prim];
  126:     h = hash(p->prefix,p->lastprim);
  127:     p->next = pt[h];
  128:     pt[h] = p;
  129:   }
  130:   return (Cell)pt;
  131: }
  132: 
  133: Inst peephole_opt(Inst inst1, Inst inst2, Cell peeptable)
  134: {
  135:   Peeptable_entry **pt = (Peeptable_entry **)peeptable;
  136:   Peeptable_entry *p;
  137: 
  138:   if (use_super == 0)
  139:       return 0;
  140:   for (p = pt[hash(inst1,inst2)]; p != NULL; p = p->next)
  141:     if (inst1 == p->prefix && inst2 == p->lastprim)
  142:       return p->combination_prim;
  143:   return NULL;
  144: }

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