Annotation of gforth/vmgen-ex/peephole.c, revision 1.5
1.1 anton 1: /* Peephole optimization routines and tables
2:
1.5 ! anton 3: Copyright (C) 2001,2002,2003 Free Software Foundation, Inc.
1.1 anton 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"
1.2 anton 34: #ifndef __GNUC__
35: {-1,-1,-1} /* unnecessary; just to shut up lcc if the file is empty */
36: #endif
1.1 anton 37: };
38:
1.3 anton 39: int use_super = 1; /* turned off by option -p */
1.1 anton 40:
41: typedef struct Peeptable_entry {
42: struct Peeptable_entry *next;
43: Inst prefix;
44: Inst lastprim;
45: Inst combination_prim;
46: } Peeptable_entry;
47:
48: #define HASH_SIZE 1024
49: #define hash(_i1,_i2) (((((Cell)(_i1))^((Cell)(_i2)))>>4)&(HASH_SIZE-1))
50:
51: Cell peeptable;
52:
53: Cell prepare_peephole_table(Inst insts[])
54: {
55: Cell i;
56: Peeptable_entry **pt = (Peeptable_entry **)calloc(HASH_SIZE,sizeof(Peeptable_entry *));
57:
58: for (i=0; i<sizeof(peephole_table)/sizeof(peephole_table[0]); i++) {
59: Combination *c = &peephole_table[i];
60: Peeptable_entry *p = (Peeptable_entry *)malloc(sizeof(Peeptable_entry));
61: Cell h;
62: p->prefix = insts[c->prefix];
63: p->lastprim = insts[c->lastprim];
64: p->combination_prim = insts[c->combination_prim];
65: h = hash(p->prefix,p->lastprim);
66: p->next = pt[h];
67: pt[h] = p;
68: }
69: return (Cell)pt;
70: }
71:
72: void init_peeptable(void)
73: {
74: peeptable = prepare_peephole_table(vm_prim);
75: }
76:
77: Inst peephole_opt(Inst inst1, Inst inst2, Cell peeptable)
78: {
79: Peeptable_entry **pt = (Peeptable_entry **)peeptable;
80: Peeptable_entry *p;
81:
82: if (use_super == 0)
83: return 0;
84: for (p = pt[hash(inst1,inst2)]; p != NULL; p = p->next)
85: if (inst1 == p->prefix && inst2 == p->lastprim)
86: return p->combination_prim;
87: return NULL;
88: }
89:
90: Inst *last_compiled = NULL;
91:
92: void gen_inst(Inst **vmcodepp, Inst i)
93: {
94: if (last_compiled != NULL) {
95: Inst combo = peephole_opt(*last_compiled, i, peeptable);
96: if (combo != NULL) {
97: *last_compiled = combo;
98: return;
99: }
100: }
101: last_compiled = *vmcodepp;
102: **vmcodepp = i;
103: (*vmcodepp)++;
104: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>