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