1 19 20 25 26 27 28 29 30 31 32 package soot.coffi; 33 34 import java.io.*; 35 36 import soot.*; 37 38 42 43 class TypeStack 44 { 45 private Type[] types; 46 47 private TypeStack() 48 { 49 } 51 52 public Object clone() 53 { 54 TypeStack newTypeStack = new TypeStack(); 55 56 newTypeStack.types = (Type[]) types.clone(); 57 58 return newTypeStack; 59 } 60 61 64 65 public static TypeStack v() 66 { 67 TypeStack typeStack = new TypeStack(); 68 69 typeStack.types = new Type[0]; 70 71 return typeStack; 72 } 73 74 public TypeStack pop() 75 { 76 TypeStack newStack = new TypeStack(); 77 78 newStack.types = new Type[types.length - 1]; 79 System.arraycopy(types, 0, newStack.types, 0, types.length - 1); 80 81 return newStack; 82 } 83 84 public TypeStack push(Type type) 85 { 86 TypeStack newStack = new TypeStack(); 87 88 newStack.types = new Type[types.length + 1]; 89 System.arraycopy(types, 0, newStack.types, 0, types.length); 90 91 newStack.types[types.length] = type; 92 93 return newStack; 94 } 95 96 public Type get(int index) 97 { 98 return types[index]; 99 } 100 101 public int topIndex() 102 { 103 return types.length - 1; 104 } 105 106 public Type top() 107 { 108 if(types.length == 0) 109 throw new RuntimeException ("TypeStack is empty"); 110 else 111 return types[types.length - 1]; 112 } 113 114 public boolean equals(Object object) 115 { 116 if(object instanceof TypeStack) 117 { 118 TypeStack otherStack = (TypeStack) object; 119 120 if(otherStack.types.length != types.length) 121 return false; 122 123 for(int i = 0; i < types.length; i++) 124 if(!types[i].equals(otherStack.types[i])) 125 return false; 126 127 return true; 128 } 129 else 130 return false; 131 } 132 133 public TypeStack merge(TypeStack other) 134 { 135 136 if(types.length != other.types.length) 137 throw new RuntimeException ("TypeStack merging failed; unequal " + 138 "stack lengths: " + types.length + " and " + other.types.length); 139 140 TypeStack newStack = new TypeStack(); 141 142 newStack.types = new Type[other.types.length]; 143 144 for(int i = 0; i < types.length; i++) 145 if(types[i].equals(other.types[i])) 146 newStack.types[i] = types[i]; 147 else { 148 if((!(types[i] instanceof ArrayType) && !(types[i] instanceof RefType)) || 149 (!(other.types[i] instanceof RefType) && !(other.types[i] instanceof ArrayType))) 150 { 151 throw new RuntimeException ("TypeStack merging failed; incompatible types " + types[i] + " and " + other.types[i]); 152 } 153 154 156 newStack.types[i] = RefType.v("java.lang.Object"); 157 } 158 159 return newStack; 160 } 161 162 public void print(PrintStream out) 163 { 164 for(int i = types.length - 1; i >= 0; i--) 165 out.println(i + ": " + types[i].toString()); 166 167 if(types.length == 0) 168 out.println("<empty>"); 169 } 170 } 171 | Popular Tags |