File:  [gforth] / gforth / engine / peephole.c
Revision 1.6: download - view: text, annotated - select for diffs
Sun Feb 10 14:02:25 2002 UTC (19 years, 9 months ago) by anton
Branches: MAIN
CVS tags: HEAD
lit@ and lit+ are now defined as superinstructions
compile lit @ and lit + instead of lit@ and lit+
extended prims2x to support superinstructions with non-C-names
  (syntax: forth-name /c-name = ...)
support profiling of interpreters with superinstructions
  (with simple instructions in the output).
profile output with prefixes only (enable by editing profile.c).
optional reporting of static superinstruction lengths (compared to
  dynamic superinstructions); enable by compiling with -DPRINT_SUPER_LENGTHS

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

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