* src/vmcore/linker.c (build_display): Removed superfluous recursion; return
[cacao.git] / src / vm / jit / intrp / peephole.c
1 /* Peephole optimization routines and tables
2
3   Copyright (C) 2001,2002,2003 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
23 #include "config.h"
24
25 #include <assert.h>
26 #include <stdlib.h>
27
28 #include "vm/jit/intrp/intrp.h"
29
30 #include "vmcore/options.h"
31
32
33 /* the numbers in this struct are primitive indices */
34 typedef struct Combination {
35   int prefix;
36   int lastprim;
37   int combination_prim;
38 } Combination;
39
40 Combination peephole_table[] = {
41 #include <java-peephole.i>
42   {-1,-1,-1} /* unnecessary; just to shut up lcc if the file is empty */
43 };
44
45 int use_super = 1; /* turned off by option -p */
46
47 typedef struct Peeptable_entry {
48   struct Peeptable_entry *next;
49   u4 prefix;
50   u4 lastprim;
51   u4 combination_prim;
52 } Peeptable_entry;
53
54 #define HASH_SIZE 1024
55 #define hash(_i1,_i2) (((((Cell)(_i1))+((Cell)(_i2))))&(HASH_SIZE-1))
56
57 Cell peeptable;
58
59 Cell prepare_peephole_table(Inst insts[])
60 {
61   Cell i;
62   Peeptable_entry **pt = (Peeptable_entry **)calloc(HASH_SIZE,sizeof(Peeptable_entry *));
63   size_t static_supers = sizeof(peephole_table)/sizeof(peephole_table[0]);
64
65   if (opt_static_supers < static_supers)
66     static_supers = opt_static_supers;
67
68   for (i=0; i<static_supers; i++) {
69     Combination *c = &peephole_table[i];
70     Peeptable_entry *p = (Peeptable_entry *)malloc(sizeof(Peeptable_entry));
71     Cell h;
72     p->prefix =           c->prefix;
73     p->lastprim =         c->lastprim;
74     p->combination_prim = c->combination_prim;
75     h = hash(p->prefix,p->lastprim);
76     p->next = pt[h];
77     pt[h] = p;
78   }
79   return (Cell)pt;
80 }
81
82 void init_peeptable(void)
83 {
84   peeptable = prepare_peephole_table(vm_prim);
85 }
86
87 ptrint peephole_opt(ptrint inst1, ptrint inst2, Cell peeptable)
88 {
89   Peeptable_entry **pt = (Peeptable_entry **)peeptable;
90   Peeptable_entry *p;
91
92   if (use_super == 0)
93       return -1;
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;
97   return -1;
98 }