1 30 31 37 38 package jbet; 39 import java.util.*; 40 41 public class JvmLocals extends LocalAccess 42 { 43 int lostart; int localn; Hashtable localtypes; Hashtable binlocals; Vector localns; Vector loalloc; int maxlo; 51 53 54 static class LocalInfo 55 { 56 int lo; Type type; Node.var use; 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 90 91 public int alloc (Node.var use) 92 { 93 Type t = use.type(); 94 95 if (t.base == 'V') 96 throw new IllegalStateException ("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 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 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 153 154 public int alloc (Type t) { 155 return alloc (maxlo < 66660 ? maxlo + 66666 : maxlo+1, t); 156 } 157 158 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 ("local &" + use.sb.swval + ":" + use.v + " not allocated"); 176 } 177 178 179 180 public void freeall () 181 { 182 loalloc = new Vector(); 183 } 184 185 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 ("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 |