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 3
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, see http://www.gnu.org/licenses/.
19: */
20:
21: #include <stdlib.h>
22: #include "mini.h"
23:
24: /* the numbers in this struct are primitive indices */
25: typedef struct Combination {
26: int prefix;
27: int lastprim;
28: int combination_prim;
29: } Combination;
30:
31: Combination peephole_table[] = {
32: #include "mini-peephole.i"
33: #ifndef __GNUC__
34: {-1,-1,-1} /* unnecessary; just to shut up lcc if the file is empty */
35: #endif
36: };
37:
38: int use_super = 1; /* turned off by option -p */
39:
40: typedef struct Peeptable_entry {
41: struct Peeptable_entry *next;
42: Inst prefix;
43: Inst lastprim;
44: Inst combination_prim;
45: } Peeptable_entry;
46:
47: #define HASH_SIZE 1024
48: #define hash(_i1,_i2) (((((Cell)(_i1))^((Cell)(_i2)))>>4)&(HASH_SIZE-1))
49:
50: Cell peeptable;
51:
52: Cell prepare_peephole_table(Inst insts[])
53: {
54: Cell i;
55: Peeptable_entry **pt = (Peeptable_entry **)calloc(HASH_SIZE,sizeof(Peeptable_entry *));
56:
57: for (i=0; i<sizeof(peephole_table)/sizeof(peephole_table[0]); i++) {
58: Combination *c = &peephole_table[i];
59: Peeptable_entry *p = (Peeptable_entry *)malloc(sizeof(Peeptable_entry));
60: Cell h;
61: p->prefix = insts[c->prefix];
62: p->lastprim = insts[c->lastprim];
63: p->combination_prim = insts[c->combination_prim];
64: h = hash(p->prefix,p->lastprim);
65: p->next = pt[h];
66: pt[h] = p;
67: }
68: return (Cell)pt;
69: }
70:
71: void init_peeptable(void)
72: {
73: peeptable = prepare_peephole_table(vm_prim);
74: }
75:
76: Inst peephole_opt(Inst inst1, Inst inst2, Cell peeptable)
77: {
78: Peeptable_entry **pt = (Peeptable_entry **)peeptable;
79: Peeptable_entry *p;
80:
81: if (use_super == 0)
82: return 0;
83: for (p = pt[hash(inst1,inst2)]; p != NULL; p = p->next)
84: if (inst1 == p->prefix && inst2 == p->lastprim)
85: return p->combination_prim;
86: return NULL;
87: }
88:
89: Inst *last_compiled = NULL;
90:
91: void gen_inst(Inst **vmcodepp, Inst i)
92: {
93: if (last_compiled != NULL) {
94: Inst combo = peephole_opt(*last_compiled, i, peeptable);
95: if (combo != NULL) {
96: *last_compiled = combo;
97: return;
98: }
99: }
100: last_compiled = *vmcodepp;
101: **vmcodepp = i;
102: (*vmcodepp)++;
103: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>