File:  [gforth] / gforth / vmgen-ex2 / peephole.c
Revision 1.1: download - view: text, annotated - select for diffs
Sun Jun 2 15:46:18 2002 UTC (19 years, 6 months ago) by anton
Branches: MAIN
CVS tags: HEAD
vmgen-related changes:
in prims2x:
  Conversion macros for single items now take 2 arguments
  Converting from two items to a type has changed order
  argument printing for disassembler disabled (for now)
  disassembler now also uses VM_IS_INST
in Gforth and vmgen-ex: adapted to work with changed prims2x
new: vmgen-ex2: uses union for Cell instead of casting (lots of
   changes compared to vmgen-ex)

    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 <stdlib.h>
   23: #include "mini.h"
   24: 
   25: /* the numbers in this struct are primitive indices */
   26: typedef struct Combination {
   27:   int prefix;
   28:   int lastprim;
   29:   int combination_prim;
   30: } Combination;
   31: 
   32: Combination peephole_table[] = {
   33: #include "mini-peephole.i"
   34: };
   35: 
   36: int use_super = 1;
   37: 
   38: typedef struct Peeptable_entry {
   39:   struct Peeptable_entry *next;
   40:   Label prefix;
   41:   Label lastprim;
   42:   Label combination_prim;
   43: } Peeptable_entry;
   44: 
   45: #define HASH_SIZE 1024
   46: #define hash(_i1,_i2) (((((long)(_i1))^((long)(_i2)))>>4)&(HASH_SIZE-1))
   47: 
   48: struct Peeptable_entry **peeptable;
   49: 
   50: Peeptable_entry **prepare_peephole_table(Label insts[])
   51: {
   52:   long i;
   53:   Peeptable_entry **pt = (Peeptable_entry **)calloc(HASH_SIZE,sizeof(Peeptable_entry *));
   54: 
   55:   for (i=0; i<sizeof(peephole_table)/sizeof(peephole_table[0]); i++) {
   56:     Combination *c = &peephole_table[i];
   57:     Peeptable_entry *p = (Peeptable_entry *)malloc(sizeof(Peeptable_entry));
   58:     long h;
   59:     p->prefix =           insts[c->prefix];
   60:     p->lastprim =         insts[c->lastprim];
   61:     p->combination_prim = insts[c->combination_prim];
   62:     h = hash((p->prefix),(p->lastprim));
   63:     p->next = pt[h];
   64:     pt[h] = p;
   65:   }
   66:   return pt;
   67: }
   68: 
   69: void init_peeptable(void)
   70: {
   71:   peeptable = prepare_peephole_table(vm_prim);
   72: }
   73: 
   74: Label peephole_opt(Label inst1, Label inst2, Peeptable_entry **peeptable)
   75: {
   76:   Peeptable_entry **pt = (Peeptable_entry **)peeptable;
   77:   Peeptable_entry *p;
   78: 
   79:   if (use_super == 0)
   80:       return NULL;
   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;
   85: }
   86: 
   87: Inst *last_compiled = NULL;
   88: 
   89: void gen_inst(Inst **vmcodepp, Label i)
   90: {
   91:   if (last_compiled != NULL) {
   92:     Label combo = peephole_opt((*last_compiled).inst, i, peeptable);
   93:     if (combo != NULL) {
   94:       (*last_compiled).inst = combo;
   95:       return;
   96:     }
   97:   }
   98:   last_compiled = *vmcodepp;
   99:   (**vmcodepp).inst = i;
  100:   (*vmcodepp)++;
  101: }

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