001 package AST; 002 003 import java.util.HashSet; 004 import java.io.File; 005 import java.util.*; 006 import beaver.*; 007 import java.util.ArrayList; 008 import java.util.zip.*; 009 import java.io.*; 010 import java.io.FileNotFoundException; 011 import java.util.Collection; 012 /** 013 * @ast class 014 * 015 */ 016 public class ConstantPool extends java.lang.Object { 017 018 public TypeDecl typeDecl; 019 020 021 public ConstantPool(TypeDecl typeDecl) { 022 this.typeDecl = typeDecl; 023 //if(!typeDecl.isFinal) 024 // System.out.println("Warning: trying to create constant pool for non final type decl " + typeDecl.fullName()); 025 } 026 027 028 029 public static final byte CONSTANT_Class = 7; 030 031 032 public static final byte CONSTANT_Fieldref = 9; 033 034 035 public static final byte CONSTANT_Methodref = 10; 036 037 038 public static final byte CONSTANT_InterfaceMethodref = 11; 039 040 041 public static final byte CONSTANT_String = 8; 042 043 044 public static final byte CONSTANT_Integer = 3; 045 046 047 public static final byte CONSTANT_Float = 4; 048 049 050 public static final byte CONSTANT_Long = 5; 051 052 053 public static final byte CONSTANT_Double = 6; 054 055 056 public static final byte CONSTANT_NameAndType = 12; 057 058 059 public static final byte CONSTANT_Utf8 = 1; 060 061 062 063 private int posCounter = 1; 064 065 066 067 private ArrayList list = new ArrayList(); 068 069 070 private void addCPInfo(CPInfo info) { 071 list.add(info); 072 } 073 074 075 076 // for debugging purposes 077 public int numElements() { 078 return list.size(); 079 } 080 081 082 public String toString() { 083 StringBuffer s = new StringBuffer(); 084 for(Iterator iter = list.iterator(); iter.hasNext(); ) { 085 s.append(iter.next().toString()); 086 s.append("\n"); 087 } 088 return s.toString(); 089 } 090 091 092 093 public void emit(DataOutputStream out) throws IOException { 094 out.writeChar(posCounter); 095 for(Iterator iter = list.iterator(); iter.hasNext(); ) { 096 CPInfo info = (CPInfo)iter.next(); 097 info.emit(out); 098 } 099 } 100 101 102 103 private int labelCounter = 1; 104 105 106 public int newLabel() { 107 return labelCounter++; 108 } 109 110 111 112 private HashMap classConstants = new HashMap(); 113 114 115 public int addClass(String name) { 116 Map map = classConstants; 117 Object key = name; 118 if(!map.containsKey(key)) { 119 CPInfo info = new ConstantClass(addUtf8(name.replace('.', '/'))); 120 info.pos = posCounter; posCounter += info.size(); 121 addCPInfo(info); 122 map.put(key, info); 123 String s = info.toString(); 124 return info.pos; 125 } 126 CPInfo info = (CPInfo)map.get(key); 127 return info.pos; 128 } 129 130 131 132 private HashMap fieldrefConstants = new HashMap(); 133 134 135 public int addFieldref(String classname, String name, String type) { 136 Map map = fieldrefConstants; 137 Object key = classname + name + type; 138 if(!map.containsKey(key)) { 139 CPInfo info = new ConstantFieldref(addClass(classname), addNameAndType(name, type)); 140 info.pos = posCounter; posCounter += info.size(); 141 addCPInfo(info); 142 map.put(key, info); 143 String s = info.toString(); 144 return info.pos; 145 } 146 CPInfo info = (CPInfo)map.get(key); 147 return info.pos; 148 } 149 150 151 152 private HashMap methodrefConstants = new HashMap(); 153 154 155 public int addMethodref(String classname, String name, String desc) { 156 Map map = methodrefConstants; 157 Object key = classname + name + desc; 158 if(!map.containsKey(key)) { 159 CPInfo info = new ConstantMethodref(addClass(classname), addNameAndType(name, desc)); 160 info.pos = posCounter; posCounter += info.size(); 161 addCPInfo(info); 162 map.put(key, info); 163 String s = info.toString(); 164 return info.pos; 165 } 166 CPInfo info = (CPInfo)map.get(key); 167 return info.pos; 168 } 169 170 171 172 private HashMap interfaceMethodrefConstants = new HashMap(); 173 174 175 public int addInterfaceMethodref(String classname, String name, String desc) { 176 Map map = interfaceMethodrefConstants; 177 Object key = classname + name + desc; 178 if(!map.containsKey(key)) { 179 CPInfo info = new ConstantInterfaceMethodref(addClass(classname), addNameAndType(name, desc)); 180 info.pos = posCounter; posCounter += info.size(); 181 addCPInfo(info); 182 map.put(key, info); 183 String s = info.toString(); 184 return info.pos; 185 } 186 CPInfo info = (CPInfo)map.get(key); 187 return info.pos; 188 } 189 190 191 192 private HashMap nameAndTypeConstants = new HashMap(); 193 194 195 public int addNameAndType(String name, String type) { 196 Map map = nameAndTypeConstants; 197 Object key = name + type; 198 if(!map.containsKey(key)) { 199 CPInfo info = new ConstantNameAndType(addUtf8(name), addUtf8(type)); 200 info.pos = posCounter; posCounter += info.size(); 201 addCPInfo(info); 202 map.put(key, info); 203 String s = info.toString(); 204 return info.pos; 205 } 206 CPInfo info = (CPInfo)map.get(key); 207 return info.pos; 208 } 209 210 211 212 private HashMap utf8Constants = new HashMap(); 213 214 215 public int addUtf8(String name) { 216 Map map = utf8Constants; 217 Object key = name; 218 if(!map.containsKey(key)) { 219 CPInfo info = new ConstantUtf8(name); 220 info.pos = posCounter; posCounter += info.size(); 221 addCPInfo(info); 222 map.put(key, info); 223 String s = info.toString(); 224 return info.pos; 225 } 226 CPInfo info = (CPInfo)map.get(key); 227 return info.pos; 228 } 229 230 231 232 private HashMap stringConstants = new HashMap(); 233 234 235 public int addConstant(String val) { 236 Map map = stringConstants; 237 Object key = val; 238 if(!map.containsKey(key)) { 239 CPInfo info = new ConstantString(addUtf8(val)); 240 info.pos = posCounter; posCounter += info.size(); 241 addCPInfo(info); 242 map.put(key, info); 243 String s = info.toString(); 244 return info.pos; 245 } 246 CPInfo info = (CPInfo)map.get(key); 247 return info.pos; 248 } 249 250 251 252 private HashMap intConstants = new HashMap(); 253 254 255 public int addConstant(int val) { 256 Map map = intConstants; 257 Object key = new Integer(val); 258 if(!map.containsKey(key)) { 259 CPInfo info = new ConstantInteger(val); 260 info.pos = posCounter; posCounter += info.size(); 261 addCPInfo(info); 262 map.put(key, info); 263 return info.pos; 264 } 265 CPInfo info = (CPInfo)map.get(key); 266 return info.pos; 267 } 268 269 270 271 private HashMap floatConstants = new HashMap(); 272 273 274 public int addConstant(float val) { 275 Map map = floatConstants; 276 Object key = new Float(val); 277 if(!map.containsKey(key)) { 278 CPInfo info = new ConstantFloat(val); 279 info.pos = posCounter; posCounter += info.size(); 280 addCPInfo(info); 281 map.put(key, info); 282 return info.pos; 283 } 284 CPInfo info = (CPInfo)map.get(key); 285 return info.pos; 286 } 287 288 289 290 private HashMap longConstants = new HashMap(); 291 292 293 public int addConstant(long val) { 294 Map map = longConstants; 295 Object key = new Long(val); 296 if(!map.containsKey(key)) { 297 CPInfo info = new ConstantLong(val); 298 info.pos = posCounter; posCounter += info.size(); 299 addCPInfo(info); 300 map.put(key, info); 301 return info.pos; 302 } 303 CPInfo info = (CPInfo)map.get(key); 304 return info.pos; 305 } 306 307 308 309 private HashMap doubleConstants = new HashMap(); 310 311 312 public int addConstant(double val) { 313 Map map = doubleConstants; 314 Object key = new Double(val); 315 if(!map.containsKey(key)) { 316 CPInfo info = new ConstantDouble(val); 317 info.pos = posCounter; posCounter += info.size(); 318 addCPInfo(info); 319 map.put(key, info); 320 return info.pos; 321 } 322 CPInfo info = (CPInfo)map.get(key); 323 return info.pos; 324 } 325 326 327 }