1 /* Peephole optimization routines and tables
3 Copyright (C) 2001,2002,2003 Free Software Foundation, Inc.
5 This file is part of Gforth.
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.
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.
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.
28 #include "vm/jit/intrp/intrp.h"
30 #include "vmcore/options.h"
33 /* the numbers in this struct are primitive indices */
34 typedef struct Combination {
40 Combination peephole_table[] = {
41 #include <java-peephole.i>
42 {-1,-1,-1} /* unnecessary; just to shut up lcc if the file is empty */
45 int use_super = 1; /* turned off by option -p */
47 typedef struct Peeptable_entry {
48 struct Peeptable_entry *next;
54 #define HASH_SIZE 1024
55 #define hash(_i1,_i2) (((((Cell)(_i1))+((Cell)(_i2))))&(HASH_SIZE-1))
59 Cell prepare_peephole_table(Inst insts[])
62 Peeptable_entry **pt = (Peeptable_entry **)calloc(HASH_SIZE,sizeof(Peeptable_entry *));
63 size_t static_supers = sizeof(peephole_table)/sizeof(peephole_table[0]);
65 if (opt_static_supers < static_supers)
66 static_supers = opt_static_supers;
68 for (i=0; i<static_supers; i++) {
69 Combination *c = &peephole_table[i];
70 Peeptable_entry *p = (Peeptable_entry *)malloc(sizeof(Peeptable_entry));
72 p->prefix = c->prefix;
73 p->lastprim = c->lastprim;
74 p->combination_prim = c->combination_prim;
75 h = hash(p->prefix,p->lastprim);
82 void init_peeptable(void)
84 peeptable = prepare_peephole_table(vm_prim);
87 ptrint peephole_opt(ptrint inst1, ptrint inst2, Cell peeptable)
89 Peeptable_entry **pt = (Peeptable_entry **)peeptable;
94 for (p = pt[hash(inst1,inst2)]; p != NULL; p = p->next)
95 if (inst1 == p->prefix && inst2 == p->lastprim)
96 return p->combination_prim;