KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jbet > JvmLocals


1 /*
2  * JBET - Java Binary Enhancement Tool
3  * Copyright (c) 2003 Networks Associates Technology, Inc.
4  *
5  * This software was developed under DARPA/SPAWAR contract
6  * N66001-00-C-8602 "SPMA" as part of the
7  * DARPA OASIS research program.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */

30
31 /* Interface for JVM code generation. A local allocator is provided;
32    two types of allocation constraints are supported: types are only
33    used once (for switchify), and DAG shared inputs use shared JVM
34    locals.
35
36    @author Andrew Reisse */

37
38 package jbet;
39 import java.util.*;
40
41 public class JvmLocals extends LocalAccess
42 {
43     int lostart; // first local to use
44
int localn; // next local to use
45
Hashtable localtypes; // types of allocated locals
46
Hashtable binlocals; // ignore for now
47
Vector localns; // DAG nodes <-> locals
48
Vector loalloc; // currently allocated locals
49
int maxlo; // max temporary allocated
50

51     /* An allocated JVM local. These records are never removed, because of the
52        type constraint. */

53
54     static class LocalInfo
55     {
56     int lo; // JVM local number
57
Type type; // type
58
Node.var use; // used as which input?
59

60     LocalInfo (int l, Type t) { lo = l; type = t; }
61     public int hashCode() { return use.hashCode() * 10000 + lo; }
62     }
63
64     JvmLocals (int startl) {
65     lostart = startl; localn = startl;
66     localtypes = new Hashtable();
67     localns = new Vector();
68     freeall();
69     }
70
71     JvmLocals () { this(0); }
72
73     public void load (Snippit out, int lvt, Type t)
74     {
75     out.push (new Instruction().setLoad (t, lvt));
76     }
77
78     public void store (Snippit out, int lvt, Type t)
79     {
80     out.push (new Instruction().setStore (t, lvt));
81     }
82
83     /* Allocate a JVM local, that is to be used as input USE
84        of type T.
85
86        @param use Input node
87        @param t type of value
88        @return a JVM local number
89     */

90
91     public int alloc (Node.var use)
92     {
93     Type t = use.type();
94
95     if (t.base == 'V')
96         throw new IllegalStateException JavaDoc ("loalloc a void");
97
98     Vector lot = (Vector) localtypes.get (t);
99     if (lot != null)
100         for (int i = 0; i < lot.size(); i++) {
101         LocalInfo li = ((LocalInfo) lot.get (i));
102         
103         if (li.use == null) {
104             li.use = use;
105             loalloc.addElement (li);
106             return li.lo;
107         }
108         else if (li.use.sb == use.sb && li.use.v == use.v)
109             return li.lo;
110         }
111     else if (lot == null) {
112         lot = new Vector();
113         localtypes.put (t, lot);
114     }
115
116     int n = localn;
117
118     localn += t.category();
119
120     LocalInfo li = new LocalInfo (n, t);
121     lot.addElement (li);
122     try {
123         localns.set (n, li);
124     } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
125         localns.setSize (n+1);
126         localns.set (n, li);
127     }
128     li.use = use;
129     loalloc.addElement (li);
130     return n;
131     }
132
133     /* Allocate a JVM local, that is to be used as temporary storage
134        or method arguments.
135
136        @param lo Temporary number
137        @param t type of value
138        @return a JVM local number
139     */

140
141     public int alloc (int lo, Type t)
142     {
143     if (lo >= maxlo)
144         maxlo = lo + t.category();
145
146     return alloc (new Node.var (lo + 66666, t));
147     }
148
149     /* Allocate a one-time temporary local.
150        @param t type of value
151        @return a jvm local number
152     */

153
154     public int alloc (Type t) {
155     return alloc (maxlo < 66660 ? maxlo + 66666 : maxlo+1, t);
156     }
157
158     /* Free the local use allocated by alloc(use). */
159     
160     public void free (Node.var use)
161     {
162     for (int i = 0; i < loalloc.size(); i++) {
163         LocalInfo li = ((LocalInfo) loalloc.get (i));
164
165         if (li.use != null &&
166         li.use.sb == use.sb &&
167         li.use.v == use.v) {
168         li.use = null;
169         loalloc.removeElementAt (i);
170         i--;
171         return;
172         }
173     }
174
175     throw new IllegalStateException JavaDoc ("local &" + use.sb.swval + ":" + use.v + " not allocated");
176     }
177
178     /* Reset all local allocation */
179
180     public void freeall ()
181     {
182     loalloc = new Vector();
183     }
184
185     /* Generate local-initialization code */
186
187     public void initlocals (Snippit out, int min)
188     {
189     Instruction ins;
190
191     for (Iterator i = localtypes.values().iterator(); i.hasNext(); ) {
192         Vector v = (Vector) i.next();
193
194         for (int j = 0; j < v.size(); j++) {
195         LocalInfo li = (LocalInfo) v.get (j);
196
197         if (li.lo >= min) {
198             ins = new Instruction();
199             switch (li.type.vmType()) {
200             case Type.VM_INT: ins.setIpush (0); break;
201             case Type.VM_ADDRESS: ins.setAconst_Null(); break;
202             case Type.VM_LONG: ins.setLpush (0); break;
203             case Type.VM_FLOAT: ins.setFpush (0); break;
204             case Type.VM_DOUBLE: ins.setDpush (0); break;
205             default: throw new IllegalStateException JavaDoc ("unexpected vmtype " + li.type.vmType() + " from " + li.type);
206             }
207             out.push (ins);
208             ins = new Instruction();
209             ins.setStore (li.type, li.lo);
210             out.push (ins);
211         }
212         }
213     }
214     }
215
216     public int numJvmLocals () {
217     return localn;
218     }
219 }
220
Popular Tags