use of org.apache.bcel.generic.INVOKEVIRTUAL in project jop by jop-devel.
the class CallStringReceiverTypes method transfer.
public ContextMap<CallString, Set<TypeMapping>> transfer(InstructionHandle stmt, FlowEdge edge, ContextMap<CallString, Set<TypeMapping>> input, Interpreter<CallString, Set<TypeMapping>> interpreter, Map<InstructionHandle, ContextMap<CallString, Set<TypeMapping>>> state) {
Context context = new Context(input.getContext());
Set<TypeMapping> in = input.get(context.callString);
ContextMap<CallString, Set<TypeMapping>> retval = new ContextMap<CallString, Set<TypeMapping>>(context, new LinkedHashMap<CallString, Set<TypeMapping>>());
Set<TypeMapping> result = new LinkedHashSet<TypeMapping>();
retval.put(context.callString, result);
Instruction instruction = stmt.getInstruction();
switch(instruction.getOpcode()) {
case Constants.NOP:
result = in;
retval.put(context.callString, result);
break;
case Constants.ACONST_NULL:
case Constants.ICONST_M1:
case Constants.ICONST_0:
case Constants.ICONST_1:
case Constants.ICONST_2:
case Constants.ICONST_3:
case Constants.ICONST_4:
case Constants.ICONST_5:
case Constants.BIPUSH:
case Constants.SIPUSH:
case Constants.FCONST_0:
case Constants.FCONST_1:
case Constants.FCONST_2:
case Constants.LCONST_0:
case Constants.LCONST_1:
case Constants.DCONST_0:
case Constants.DCONST_1:
case Constants.LDC2_W:
result = in;
retval.put(context.callString, result);
break;
case Constants.LDC:
case Constants.LDC_W:
{
LDC instr = (LDC) instruction;
result = new LinkedHashSet<TypeMapping>(in);
retval.put(context.callString, result);
Type type = instr.getType(context.constPool());
if (type.equals(Type.STRING)) {
result.add(new TypeMapping(context.stackPtr, type.toString()));
String value = type.toString() + ".value";
String name = "char[]";
name += "@" + context.method() + ":" + stmt.getPosition();
result.add(new TypeMapping(value, name));
}
}
break;
case Constants.DUP:
{
for (TypeMapping m : in) {
result.add(m);
if (m.stackLoc == context.stackPtr - 1) {
result.add(new TypeMapping(context.stackPtr, m.type));
}
}
}
break;
case Constants.DUP_X1:
{
for (TypeMapping m : in) {
if (m.stackLoc < context.stackPtr - 2) {
result.add(m);
}
if (m.stackLoc == context.stackPtr - 1) {
result.add(new TypeMapping(context.stackPtr - 2, m.type));
result.add(new TypeMapping(context.stackPtr, m.type));
}
if (m.stackLoc == context.stackPtr - 2) {
result.add(new TypeMapping(context.stackPtr - 1, m.type));
}
}
}
break;
case Constants.DUP_X2:
{
for (TypeMapping m : in) {
if (m.stackLoc < context.stackPtr - 3) {
result.add(m);
}
if (m.stackLoc == context.stackPtr - 1) {
result.add(new TypeMapping(context.stackPtr - 3, m.type));
result.add(new TypeMapping(context.stackPtr, m.type));
}
if (m.stackLoc == context.stackPtr - 2) {
result.add(new TypeMapping(context.stackPtr - 1, m.type));
}
if (m.stackLoc == context.stackPtr - 3) {
result.add(new TypeMapping(context.stackPtr - 2, m.type));
}
}
}
break;
case Constants.DUP2:
{
for (TypeMapping m : in) {
result.add(m);
if (m.stackLoc == context.stackPtr - 2) {
result.add(new TypeMapping(context.stackPtr, m.type));
}
if (m.stackLoc == context.stackPtr - 1) {
result.add(new TypeMapping(context.stackPtr + 1, m.type));
}
}
}
break;
case Constants.DUP2_X1:
{
for (TypeMapping m : in) {
if (m.stackLoc < context.stackPtr - 3) {
result.add(m);
}
if (m.stackLoc == context.stackPtr - 2) {
result.add(new TypeMapping(context.stackPtr - 3, m.type));
result.add(new TypeMapping(context.stackPtr, m.type));
}
if (m.stackLoc == context.stackPtr - 1) {
result.add(new TypeMapping(context.stackPtr - 2, m.type));
result.add(new TypeMapping(context.stackPtr + 1, m.type));
}
if (m.stackLoc == context.stackPtr - 3) {
result.add(new TypeMapping(context.stackPtr - 1, m.type));
}
}
}
break;
case Constants.DUP2_X2:
{
for (TypeMapping m : in) {
if (m.stackLoc < context.stackPtr - 3) {
result.add(m);
}
if (m.stackLoc == context.stackPtr - 2) {
result.add(new TypeMapping(context.stackPtr - 4, m.type));
result.add(new TypeMapping(context.stackPtr, m.type));
}
if (m.stackLoc == context.stackPtr - 1) {
result.add(new TypeMapping(context.stackPtr - 3, m.type));
result.add(new TypeMapping(context.stackPtr + 1, m.type));
}
if (m.stackLoc == context.stackPtr - 4) {
result.add(new TypeMapping(context.stackPtr - 2, m.type));
}
if (m.stackLoc == context.stackPtr - 3) {
result.add(new TypeMapping(context.stackPtr - 1, m.type));
}
}
}
break;
case Constants.SWAP:
{
for (TypeMapping m : in) {
if (m.stackLoc < context.stackPtr - 2) {
result.add(m);
}
if (m.stackLoc == context.stackPtr - 2) {
result.add(new TypeMapping(context.stackPtr - 1, m.type));
}
if (m.stackLoc == context.stackPtr - 1) {
result.add(new TypeMapping(context.stackPtr - 2, m.type));
}
}
}
break;
case Constants.POP:
filterSet(in, result, context.stackPtr - 1);
break;
case Constants.POP2:
filterSet(in, result, context.stackPtr - 2);
break;
case Constants.GETFIELD:
{
GETFIELD instr = (GETFIELD) instruction;
List<String> receivers = new LinkedList<String>();
for (TypeMapping m : in) {
if (m.stackLoc < context.stackPtr - 1) {
result.add(m);
} else if (m.stackLoc == context.stackPtr - 1) {
receivers.add(m.type);
}
}
// if (receivers.isEmpty()
// && instr.getFieldType(context.constPool) != Type.INT) {
// System.out.println("GETFIELD not found: "+context.method+"@"+stmt+": "+instr.getFieldName(context.constPool));
//// System.exit(1);
// }
DFATool p = interpreter.getDFATool();
String fieldName = instr.getFieldName(context.constPool());
for (String receiver : receivers) {
//String heapLoc = receiver + "." + instr.getFieldName(context.constPool);
//String namedLoc = receiver.split("@")[0] + "." + instr.getFieldName(context.constPool);
String className = receiver.split("@")[0];
ClassInfo classInfo = p.classForField(className, fieldName);
if (classInfo != null) {
String heapLoc = classInfo.getClassName() + "." + fieldName;
recordReceiver(stmt, context, heapLoc);
for (TypeMapping m : in) {
if (heapLoc.equals(m.heapLoc)) {
result.add(new TypeMapping(context.stackPtr - 1, m.type));
}
}
}
}
}
break;
case Constants.PUTFIELD:
{
PUTFIELD instr = (PUTFIELD) instruction;
List<String> receivers = new LinkedList<String>();
int fieldSize = instr.getFieldType(context.constPool()).getSize();
for (TypeMapping m : in) {
if (m.stackLoc < context.stackPtr - 1 - fieldSize) {
result.add(m);
} else if (m.stackLoc == context.stackPtr - 1 - fieldSize) {
receivers.add(m.type);
}
}
// if (receivers.isEmpty()) {
// System.out.println("PUTFIELD not found: "+context.method+"@"+stmt+": "+instr.getFieldName(context.constPool));
//// System.exit(-1);
// }
DFATool p = interpreter.getDFATool();
String fieldName = instr.getFieldName(context.constPool());
for (String receiver : receivers) {
//String heapLoc = receiver + "." + instr.getFieldName(context.constPool);
//String namedLoc = receiver.split("@")[0] + "." + instr.getFieldName(context.constPool);
String className = receiver.split("@")[0];
ClassInfo classInfo = p.classForField(className, fieldName);
if (classInfo != null) {
String heapLoc = classInfo.getClassName() + "." + fieldName;
recordReceiver(stmt, context, heapLoc);
for (TypeMapping m : in) {
if (!heapLoc.equals(m.heapLoc) || context.threaded) {
result.add(m);
}
if (m.stackLoc == context.stackPtr - 1) {
result.add(new TypeMapping(heapLoc, m.type));
}
}
}
}
if (instr.getFieldType(context.constPool()) instanceof ReferenceType) {
doInvokeStatic("com.jopdesign.sys.JVM.f_putfield_ref(III)V", stmt, context, input, interpreter, state, retval);
}
}
break;
case Constants.GETSTATIC:
{
GETSTATIC instr = (GETSTATIC) instruction;
DFATool p = interpreter.getDFATool();
FieldRef ref = context.getMethodInfo().getCode().getFieldRef(instr);
String fieldName = ref.getName();
String className = ref.getClassName();
ClassInfo classInfo = p.classForField(className, fieldName);
if (classInfo != null) {
String heapLoc = classInfo.getClassName() + "." + fieldName;
recordReceiver(stmt, context, heapLoc);
for (TypeMapping m : in) {
if (m.stackLoc < context.stackPtr) {
result.add(m);
}
if (heapLoc.equals(m.heapLoc)) {
result.add(new TypeMapping(context.stackPtr, m.type));
}
}
} else {
// System.out.println("GETSTATIC not found: "+heapLoc);
// System.exit(1);
}
}
break;
case Constants.PUTSTATIC:
{
PUTSTATIC instr = (PUTSTATIC) instruction;
DFATool p = interpreter.getDFATool();
FieldRef ref = context.getMethodInfo().getCode().getFieldRef(instr);
String fieldName = ref.getName();
String className = ref.getClassName();
ClassInfo classInfo = p.classForField(className, fieldName);
if (classInfo != null) {
String heapLoc = classInfo.getClassName() + "." + fieldName;
recordReceiver(stmt, context, heapLoc);
for (TypeMapping m : in) {
if (m.stackLoc >= 0 && m.stackLoc < context.stackPtr - 1) {
result.add(m);
} else if (m.stackLoc == -1 && (!heapLoc.equals(m.heapLoc) || context.threaded)) {
result.add(m);
}
if (m.stackLoc == context.stackPtr - 1) {
result.add(new TypeMapping(heapLoc, m.type));
}
}
} else {
// System.out.println("PUTSTATIC not found: "+heapLoc);
// System.exit(1);
}
if (instr.getFieldType(context.constPool()) instanceof ReferenceType) {
doInvokeStatic("com.jopdesign.sys.JVM.f_putstatic_ref(II)V", stmt, context, input, interpreter, state, retval);
}
}
break;
case Constants.ARRAYLENGTH:
for (TypeMapping m : in) {
if (m.stackLoc < context.stackPtr - 1) {
result.add(m);
}
if (m.stackLoc == context.stackPtr - 1) {
recordReceiver(stmt, context, m.type);
}
}
break;
case Constants.IASTORE:
case Constants.BASTORE:
case Constants.CASTORE:
case Constants.SASTORE:
case Constants.FASTORE:
{
for (TypeMapping m : in) {
if (m.stackLoc < context.stackPtr - 3) {
result.add(m);
}
if (m.stackLoc == context.stackPtr - 3) {
recordReceiver(stmt, context, m.type);
}
}
}
break;
case Constants.LASTORE:
case Constants.DASTORE:
{
for (TypeMapping m : in) {
if (m.stackLoc < context.stackPtr - 4) {
result.add(m);
}
if (m.stackLoc == context.stackPtr - 4) {
recordReceiver(stmt, context, m.type);
}
}
}
break;
case Constants.AASTORE:
{
List<String> receivers = new LinkedList<String>();
for (TypeMapping m : in) {
if (m.stackLoc < context.stackPtr - 3) {
result.add(m);
} else if (m.stackLoc == context.stackPtr - 3) {
receivers.add(m.type);
recordReceiver(stmt, context, m.type);
}
}
for (String healLoc : receivers) {
for (TypeMapping m : in) {
if (m.stackLoc == context.stackPtr - 1) {
result.add(new TypeMapping(healLoc, m.type));
}
}
}
doInvokeStatic("com.jopdesign.sys.JVM.f_aastore(III)V", stmt, context, input, interpreter, state, retval);
}
break;
case Constants.IALOAD:
case Constants.BALOAD:
case Constants.CALOAD:
case Constants.SALOAD:
case Constants.FALOAD:
{
for (TypeMapping m : in) {
if (m.stackLoc < context.stackPtr - 2) {
result.add(m);
}
if (m.stackLoc == context.stackPtr - 2) {
recordReceiver(stmt, context, m.type);
}
}
}
break;
case Constants.LALOAD:
case Constants.DALOAD:
{
for (TypeMapping m : in) {
if (m.stackLoc < context.stackPtr - 2) {
result.add(m);
}
if (m.stackLoc == context.stackPtr - 2) {
recordReceiver(stmt, context, m.type);
}
}
}
break;
case Constants.AALOAD:
{
List<String> receivers = new LinkedList<String>();
for (TypeMapping m : in) {
if (m.stackLoc < context.stackPtr - 2) {
result.add(m);
} else if (m.stackLoc == context.stackPtr - 2) {
receivers.add(m.type);
recordReceiver(stmt, context, m.type);
}
}
for (String heapLoc : receivers) {
for (TypeMapping m : in) {
if (heapLoc.equals(m.heapLoc)) {
result.add(new TypeMapping(context.stackPtr - 2, m.type));
}
}
}
}
break;
case Constants.NEW:
{
NEW instr = (NEW) instruction;
filterSet(in, result, context.stackPtr);
String name = instr.getType(context.constPool()).toString();
name += "@" + context.method() + ":" + stmt.getPosition();
result.add(new TypeMapping(context.stackPtr, name));
doInvokeStatic("com.jopdesign.sys.JVM.f_" + stmt.getInstruction().getName() + "(I)I", stmt, context, input, interpreter, state, retval);
}
break;
case Constants.ANEWARRAY:
{
ANEWARRAY instr = (ANEWARRAY) instruction;
filterSet(in, result, context.stackPtr - 1);
String name = instr.getType(context.constPool()).toString() + "[]";
name += "@" + context.method() + ":" + stmt.getPosition();
result.add(new TypeMapping(context.stackPtr - 1, name));
doInvokeStatic("com.jopdesign.sys.JVM.f_" + stmt.getInstruction().getName() + "(II)I", stmt, context, input, interpreter, state, retval);
}
break;
case Constants.NEWARRAY:
{
NEWARRAY instr = (NEWARRAY) instruction;
filterSet(in, result, context.stackPtr - 1);
String name = instr.getType().toString();
name += "@" + context.method() + ":" + stmt.getPosition();
result.add(new TypeMapping(context.stackPtr - 1, name));
doInvokeStatic("com.jopdesign.sys.JVM.f_" + stmt.getInstruction().getName() + "(II)I", stmt, context, input, interpreter, state, retval);
}
break;
case Constants.MULTIANEWARRAY:
{
MULTIANEWARRAY instr = (MULTIANEWARRAY) instruction;
int dim = instr.getDimensions();
filterSet(in, result, context.stackPtr - dim);
String type = instr.getType(context.constPool()).toString();
type = type.substring(0, type.indexOf("["));
for (int i = 1; i <= dim; i++) {
String type1 = type;
String type2 = type;
for (int k = 0; k < i; k++) {
type1 += "[]";
}
for (int k = 0; k < i - 1; k++) {
type2 += "[]";
}
type1 += "@" + context.method() + ":" + stmt.getPosition();
type2 += "@" + context.method() + ":" + stmt.getPosition();
result.add(new TypeMapping(type1, type2));
}
String name = instr.getType(context.constPool()).toString();
name += "@" + context.method() + ":" + stmt.getPosition();
result.add(new TypeMapping(context.stackPtr - dim, name));
doInvokeStatic("com.jopdesign.sys.JVM.f_" + stmt.getInstruction().getName() + "()I", stmt, context, input, interpreter, state, retval);
}
break;
case Constants.ILOAD_0:
case Constants.ILOAD_1:
case Constants.ILOAD_2:
case Constants.ILOAD_3:
case Constants.ILOAD:
case Constants.FLOAD_0:
case Constants.FLOAD_1:
case Constants.FLOAD_2:
case Constants.FLOAD_3:
case Constants.FLOAD:
case Constants.LLOAD_0:
case Constants.LLOAD_1:
case Constants.LLOAD_2:
case Constants.LLOAD_3:
case Constants.LLOAD:
case Constants.DLOAD_0:
case Constants.DLOAD_1:
case Constants.DLOAD_2:
case Constants.DLOAD_3:
case Constants.DLOAD:
result = in;
retval.put(context.callString, result);
break;
case Constants.ALOAD_0:
case Constants.ALOAD_1:
case Constants.ALOAD_2:
case Constants.ALOAD_3:
case Constants.ALOAD:
{
LoadInstruction instr = (LoadInstruction) instruction;
int index = instr.getIndex();
for (TypeMapping m : in) {
if (m.stackLoc < context.stackPtr) {
result.add(m);
}
if (m.stackLoc == index) {
result.add(new TypeMapping(context.stackPtr, m.type));
}
}
}
break;
case Constants.ISTORE_0:
case Constants.ISTORE_1:
case Constants.ISTORE_2:
case Constants.ISTORE_3:
case Constants.ISTORE:
case Constants.FSTORE_0:
case Constants.FSTORE_1:
case Constants.FSTORE_2:
case Constants.FSTORE_3:
case Constants.FSTORE:
case Constants.LSTORE_0:
case Constants.LSTORE_1:
case Constants.LSTORE_2:
case Constants.LSTORE_3:
case Constants.LSTORE:
case Constants.DSTORE_0:
case Constants.DSTORE_1:
case Constants.DSTORE_2:
case Constants.DSTORE_3:
case Constants.DSTORE:
result = in;
retval.put(context.callString, result);
break;
case Constants.ASTORE_0:
case Constants.ASTORE_1:
case Constants.ASTORE_2:
case Constants.ASTORE_3:
case Constants.ASTORE:
{
StoreInstruction instr = (StoreInstruction) instruction;
int index = instr.getIndex();
for (TypeMapping m : in) {
if (m.stackLoc < context.stackPtr - 1 && m.stackLoc != index) {
result.add(m);
}
if (m.stackLoc == context.stackPtr - 1) {
result.add(new TypeMapping(index, m.type));
}
}
}
break;
case Constants.FCMPL:
case Constants.FCMPG:
case Constants.LCMP:
case Constants.DCMPL:
case Constants.DCMPG:
result = in;
retval.put(context.callString, result);
break;
case Constants.IFEQ:
case Constants.IFNE:
case Constants.IFLT:
case Constants.IFGE:
case Constants.IFGT:
case Constants.IFLE:
case Constants.IFNULL:
case Constants.IFNONNULL:
filterSet(in, result, context.stackPtr - 1);
break;
case Constants.IF_ICMPEQ:
case Constants.IF_ICMPNE:
case Constants.IF_ICMPLT:
case Constants.IF_ICMPGE:
case Constants.IF_ICMPGT:
case Constants.IF_ICMPLE:
case Constants.IF_ACMPEQ:
case Constants.IF_ACMPNE:
filterSet(in, result, context.stackPtr - 2);
break;
case Constants.TABLESWITCH:
case Constants.LOOKUPSWITCH:
filterSet(in, result, context.stackPtr - 1);
break;
case Constants.GOTO:
result = in;
retval.put(context.callString, result);
break;
case Constants.IADD:
case Constants.ISUB:
case Constants.IMUL:
case Constants.IDIV:
case Constants.IREM:
case Constants.ISHL:
case Constants.ISHR:
case Constants.IUSHR:
case Constants.IAND:
case Constants.IOR:
case Constants.IXOR:
case Constants.FADD:
case Constants.FSUB:
case Constants.FMUL:
case Constants.FDIV:
case Constants.FREM:
case Constants.DADD:
case Constants.DSUB:
case Constants.DMUL:
case Constants.DDIV:
case Constants.DREM:
case Constants.IINC:
case Constants.INEG:
case Constants.FNEG:
case Constants.LNEG:
case Constants.DNEG:
case Constants.LADD:
case Constants.LAND:
case Constants.LOR:
case Constants.LXOR:
case Constants.LSUB:
case Constants.LSHL:
case Constants.LSHR:
case Constants.LUSHR:
result = in;
retval.put(context.callString, result);
break;
case Constants.LMUL:
case Constants.LDIV:
case Constants.LREM:
result = new LinkedHashSet<TypeMapping>(in);
retval.put(context.callString, result);
doInvokeStatic("com.jopdesign.sys.JVM.f_" + stmt.getInstruction().getName() + "(JJ)J", stmt, context, input, interpreter, state, retval);
break;
case Constants.I2B:
case Constants.I2C:
case Constants.I2S:
case Constants.I2F:
case Constants.F2I:
case Constants.I2L:
case Constants.I2D:
case Constants.F2L:
case Constants.F2D:
case Constants.L2I:
case Constants.D2I:
case Constants.L2F:
case Constants.D2F:
case Constants.L2D:
case Constants.D2L:
result = in;
retval.put(context.callString, result);
break;
case Constants.INSTANCEOF:
filterSet(in, result, context.stackPtr - 1);
break;
case Constants.CHECKCAST:
{
DFATool p = interpreter.getDFATool();
CHECKCAST instr = (CHECKCAST) instruction;
for (TypeMapping m : in) {
if (m.stackLoc < context.stackPtr - 1) {
result.add(m);
}
if (m.stackLoc == context.stackPtr - 1) {
// check whether this class can possibly be cast
String constClassName = instr.getType(context.constPool()).toString();
ClassInfo staticClass = p.getAppInfo().getClassInfo(constClassName);
ClassInfo dynamicClass = p.getAppInfo().getClassInfo(m.type.split("@")[0]);
// System.out.println("CHECKCAST: "+context.callString.asList()+"/"+context.method+": "+stmt+": "+constClassName+" vs "+m.type);
if (dynamicClass.isSubclassOf(staticClass)) {
result.add(m);
// System.out.println("yay!");
}
}
}
}
break;
case Constants.MONITORENTER:
filterSet(in, result, context.stackPtr - 1);
context.syncLevel++;
break;
case Constants.MONITOREXIT:
filterSet(in, result, context.stackPtr - 1);
context.syncLevel--;
if (context.syncLevel < 0) {
System.err.println("Synchronization level mismatch.");
System.exit(-1);
}
break;
case Constants.INVOKEVIRTUAL:
case Constants.INVOKEINTERFACE:
{
InvokeInstruction instr = (InvokeInstruction) instruction;
int argSize = MethodHelper.getArgSize(instr, context.constPool());
DFATool p = interpreter.getDFATool();
MethodRef ref = context.getMethodInfo().getCode().getInvokeSite(stmt).getInvokeeRef();
ClassInfo constClass = ref.getClassInfo();
// find possible receiver types
List<String> receivers = new LinkedList<String>();
for (TypeMapping m : in) {
if (m.stackLoc == context.stackPtr - argSize) {
String clName = m.type.split("@")[0];
ClassInfo dynamicClass;
// check whether this class can possibly be a receiver
try {
dynamicClass = p.getAppInfo().getClassInfo(clName);
} catch (MissingClassError ex) {
logger.error("TRANSFER/" + instr + ": " + ex);
continue;
}
if ((instr instanceof INVOKEVIRTUAL && dynamicClass.isSubclassOf(constClass)) || (instr instanceof INVOKEINTERFACE && dynamicClass.isImplementationOf(constClass))) {
receivers.add(clName);
} else {
logger.debug(context.method() + ": class " + constClass + " is not a superclass of " + clName);
}
}
}
/* Get the actual set of invoked methods */
//ArrayList<MethodInfo> invokeCandidates = new ArrayList<MethodInfo>();
String signature = ref.getMethodSignature();
for (String receiver : receivers) {
// find receiving method
MethodInfo impl = p.getAppInfo().getMethodInfoInherited(receiver, signature);
if (impl == null) {
throw new AppInfoError("Could not find implementation for: " + receiver + "#" + signature);
}
doInvokeVirtual(receiver, impl, stmt, context, input, interpreter, state, retval);
}
// add relevant information to result
filterSet(in, result, context.stackPtr - argSize);
}
break;
case Constants.INVOKESTATIC:
case Constants.INVOKESPECIAL:
{
InvokeInstruction instr = (InvokeInstruction) instruction;
int argSize = MethodHelper.getArgSize(instr, context.constPool());
MethodRef ref = context.getMethodInfo().getCode().getInvokeSite(stmt).getInvokeeRef();
MethodInfo impl = ref.getMethodInfo();
if (impl == null) {
throw new AppInfoError("Cannot find implementation of " + ref);
} else if (impl.isPrivate() && !impl.isStatic()) {
doInvokeVirtual(ref.getClassName(), impl, stmt, context, input, interpreter, state, retval);
} else {
doInvokeStatic(impl.getMemberID().toString(), stmt, context, input, interpreter, state, retval);
}
// add relevant information to result
filterSet(in, result, context.stackPtr - argSize);
}
break;
case Constants.RETURN:
case Constants.IRETURN:
case Constants.FRETURN:
case Constants.LRETURN:
case Constants.DRETURN:
filterSet(in, result, 0);
break;
case Constants.ARETURN:
{
for (TypeMapping m : in) {
if (m.stackLoc < 0) {
result.add(m);
}
if (m.stackLoc == context.stackPtr - 1) {
result.add(new TypeMapping(0, m.type));
}
}
}
break;
default:
System.out.println("Unknown opcode: " + instruction.toString(context.constPool().getConstantPool()));
System.exit(-1);
}
// System.out.print(instruction+":\t{ ");
// for (Iterator k = result.iterator(); k.hasNext(); ) {
// ReceiverTypes.TypeMapping m = (ReceiverTypes.TypeMapping) k.next();
// if (m.stackLoc >= 0) {
// System.out.print("<stack[" + m.stackLoc + "], " + m.type +">, ");
// } else {
// System.out.print("<" + m.heapLoc + ", " + m.type +">, ");
// }
// }
// System.out.println("}");
// System.out.println("AFTER "+context.method+": "+stmt+" / "+context.callString.asList());
// System.out.print(stmt.getInstruction()+":\t{ ");
// System.out.print(context.callString.asList()+": "+retval.get(context.callString));
// System.out.println(" }");
context.stackPtr += instruction.produceStack(context.constPool()) - instruction.consumeStack(context.constPool());
return retval;
}
use of org.apache.bcel.generic.INVOKEVIRTUAL in project jop by jop-devel.
the class InlineHelper method needsNullpointerCheck.
/**
* Check if an exception must be generated if the 'this' reference is null.
* This test can return false if
* <ul><li>There is no 'this' reference</li>
* <li>The DFA analysis showed that the reference is never null</li>
* <li>The inlined code will always generate an exception anyway</li>
* <li>Generating checks has been disabled by configuration</li>
* </ul>
* <p>
* The callstring does not need to start or to end at the method to optimize. However since the callstring is
* used to check the DFA results if available, the callstring must match what the DFA expects, i.e. if
* the DFA-results and -callstrings are updated during inlining, this callstring must not include inlined
* invokes. Contrariwise if the DFA results are not updated during inline, the callstring must contain already
* inlined invokes.
* </p>
*
* @param callString The callstring including the invokesite of the invokee. The top invokesite does not need to
* refer to an invoke instruction, and the referenced invoker method does not need to
* be the method containing the invoke to inline (e.g. if the invoke to inline has
* been inlined itself). However the callstring needs to match what the DFA expects.
* @param invokee the devirtualized invokee.
* @param analyzeCode if false, skip checking the code of the invokee.
* @return true if a nullpointer check code should be generated.
*/
public boolean needsNullpointerCheck(CallString callString, MethodInfo invokee, boolean analyzeCode) {
if (inlineConfig.skipNullpointerChecks())
return false;
InvokeSite invokeSite = callString.top();
// check if we have a 'this' reference anyway
if (invokeSite.isInvokeStatic() || invokeSite.isJVMCall()) {
return false;
}
// TODO check the DFA results if available
if (jcopter.useDFA()) {
} else if ("<init>".equals(invokee.getShortName())) {
// the NP check in this case (and hope that compilers for languages other than Java do the same..)
return false;
}
if (!analyzeCode) {
return true;
}
// check if the code will always throw an exception anyway (without producing any side effects before throwing)
ValueMapAnalysis analysis = new ValueMapAnalysis(invokee);
analysis.loadParameters();
InstructionList list = invokee.getCode().getInstructionList(true, false);
for (InstructionHandle ih : list.getInstructionHandles()) {
Instruction instr = ih.getInstruction();
if (instr instanceof ConstantPushInstruction || instr instanceof LocalVariableInstruction) {
analysis.transfer(instr);
} else if (instr instanceof GETFIELD || instr instanceof PUTFIELD || instr instanceof INVOKEVIRTUAL || instr instanceof INVOKEINTERFACE || instr instanceof INVOKESPECIAL) {
int down = instr.consumeStack(invokee.getConstantPoolGen());
ValueInfo value = analysis.getValueTable().top(down);
// the same way as the inlined invoke
if (value.isThisReference()) {
return false;
}
break;
} else {
// we ignore all other instructions (for now..)
break;
}
}
return true;
}
use of org.apache.bcel.generic.INVOKEVIRTUAL in project jop by jop-devel.
the class ObjectCacheAnalysis method getHandleType.
public static String getHandleType(WCETTool project, ControlFlowGraph cfg, InstructionHandle ih) {
ConstantPoolGen constPool = cfg.getMethodInfo().getConstantPoolGen();
Instruction instr = ih.getInstruction();
if (instr instanceof GETFIELD) {
GETFIELD gf = (GETFIELD) instr;
ReferenceType refty = gf.getReferenceType(constPool);
return refty.toString();
}
if (!ALL_HANDLE_ACCESSES)
return null;
if (instr instanceof PUTFIELD) {
PUTFIELD pf = (PUTFIELD) instr;
ReferenceType refty = pf.getReferenceType(constPool);
return refty.toString();
}
if (instr instanceof ArrayInstruction) {
//ArrayInstruction ainstr = (ArrayInstruction) instr;
return "[]";
}
if (instr instanceof ARRAYLENGTH) {
//ARRAYLENGTH ainstr = (ARRAYLENGTH) instr;
return "[]";
}
if (instr instanceof INVOKEINTERFACE || instr instanceof INVOKEVIRTUAL) {
return "$header";
}
return null;
}
use of org.apache.bcel.generic.INVOKEVIRTUAL in project servicemix-bundles by apache.
the class FunctionCall method translate.
/**
* Translate a function call. The compiled code will leave the function's
* return value on the JVM's stack.
*/
public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
final int n = argumentCount();
final ConstantPoolGen cpg = classGen.getConstantPool();
final InstructionList il = methodGen.getInstructionList();
final boolean isSecureProcessing = classGen.getParser().getXSLTC().isSecureProcessing();
int index;
// Translate calls to methods in the BasisLibrary
if (isStandard() || isExtension()) {
for (int i = 0; i < n; i++) {
final Expression exp = argument(i);
exp.translate(classGen, methodGen);
exp.startIterator(classGen, methodGen);
}
// append "F" to the function's name
final String name = _fname.toString().replace('-', '_') + "F";
String args = Constants.EMPTYSTRING;
// Special precautions for some method calls
if (name.equals("sumF")) {
args = DOM_INTF_SIG;
il.append(methodGen.loadDOM());
} else if (name.equals("normalize_spaceF")) {
if (_chosenMethodType.toSignature(args).equals("()Ljava/lang/String;")) {
args = "I" + DOM_INTF_SIG;
il.append(methodGen.loadContextNode());
il.append(methodGen.loadDOM());
}
}
// Invoke the method in the basis library
index = cpg.addMethodref(BASIS_LIBRARY_CLASS, name, _chosenMethodType.toSignature(args));
il.append(new INVOKESTATIC(index));
} else // run-time error message for unsupported external functions
if (unresolvedExternal) {
index = cpg.addMethodref(BASIS_LIBRARY_CLASS, "unresolved_externalF", "(Ljava/lang/String;)V");
il.append(new PUSH(cpg, _fname.toString()));
il.append(new INVOKESTATIC(index));
} else if (_isExtConstructor) {
if (isSecureProcessing)
translateUnallowedExtension(cpg, il);
final String clazz = _chosenConstructor.getDeclaringClass().getName();
Class[] paramTypes = _chosenConstructor.getParameterTypes();
LocalVariableGen[] paramTemp = new LocalVariableGen[n];
for (int i = 0; i < n; i++) {
final Expression exp = argument(i);
Type expType = exp.getType();
exp.translate(classGen, methodGen);
// Convert the argument to its Java type
exp.startIterator(classGen, methodGen);
expType.translateTo(classGen, methodGen, paramTypes[i]);
paramTemp[i] = methodGen.addLocalVariable("function_call_tmp" + i, expType.toJCType(), null, null);
paramTemp[i].setStart(il.append(expType.STORE(paramTemp[i].getIndex())));
}
il.append(new NEW(cpg.addClass(_className)));
il.append(InstructionConstants.DUP);
for (int i = 0; i < n; i++) {
final Expression arg = argument(i);
paramTemp[i].setEnd(il.append(arg.getType().LOAD(paramTemp[i].getIndex())));
}
final StringBuffer buffer = new StringBuffer();
buffer.append('(');
for (int i = 0; i < paramTypes.length; i++) {
buffer.append(getSignature(paramTypes[i]));
}
buffer.append(')');
buffer.append("V");
index = cpg.addMethodref(clazz, "<init>", buffer.toString());
il.append(new INVOKESPECIAL(index));
// Convert the return type back to our internal type
(Type.Object).translateFrom(classGen, methodGen, _chosenConstructor.getDeclaringClass());
} else // Invoke function calls that are handled in separate classes
{
if (isSecureProcessing)
translateUnallowedExtension(cpg, il);
final String clazz = _chosenMethod.getDeclaringClass().getName();
Class[] paramTypes = _chosenMethod.getParameterTypes();
// Push "this" if it is an instance method
if (_thisArgument != null) {
_thisArgument.translate(classGen, methodGen);
}
for (int i = 0; i < n; i++) {
final Expression exp = argument(i);
exp.translate(classGen, methodGen);
// Convert the argument to its Java type
exp.startIterator(classGen, methodGen);
exp.getType().translateTo(classGen, methodGen, paramTypes[i]);
}
final StringBuffer buffer = new StringBuffer();
buffer.append('(');
for (int i = 0; i < paramTypes.length; i++) {
buffer.append(getSignature(paramTypes[i]));
}
buffer.append(')');
buffer.append(getSignature(_chosenMethod.getReturnType()));
if (_thisArgument != null && _clazz.isInterface()) {
index = cpg.addInterfaceMethodref(clazz, _fname.getLocalPart(), buffer.toString());
il.append(new INVOKEINTERFACE(index, n + 1));
} else {
index = cpg.addMethodref(clazz, _fname.getLocalPart(), buffer.toString());
il.append(_thisArgument != null ? (InvokeInstruction) new INVOKEVIRTUAL(index) : (InvokeInstruction) new INVOKESTATIC(index));
}
// Convert the return type back to our internal type
_type.translateFrom(classGen, methodGen, _chosenMethod.getReturnType());
}
}
use of org.apache.bcel.generic.INVOKEVIRTUAL in project fb-contrib by mebigfatguy.
the class FieldCouldBeLocal method checkBlock.
/**
* looks in this basic block for the first access to the fields in uncheckedFields. Once found the item is removed from uncheckedFields, and removed from
* localizableFields if the access is a GETFIELD. If any unchecked fields remain, this method is recursively called on all outgoing edges of this basic
* block.
*
* @param startBB
* this basic block
* @param uncheckedFields
* the list of fields to look for
*/
private void checkBlock(BasicBlock startBB, Set<String> uncheckedFields) {
Deque<BlockState> toBeProcessed = new ArrayDeque<>();
toBeProcessed.addLast(new BlockState(startBB, uncheckedFields));
visitedBlocks.set(startBB.getLabel());
while (!toBeProcessed.isEmpty()) {
if (localizableFields.isEmpty()) {
return;
}
BlockState bState = toBeProcessed.removeFirst();
BasicBlock bb = bState.getBasicBlock();
InstructionIterator ii = bb.instructionIterator();
while ((bState.getUncheckedFieldSize() > 0) && ii.hasNext()) {
InstructionHandle ih = ii.next();
Instruction ins = ih.getInstruction();
if (ins instanceof FieldInstruction) {
FieldInstruction fi = (FieldInstruction) ins;
if (fi.getReferenceType(cpg).getSignature().equals(clsSig)) {
String fieldName = fi.getFieldName(cpg);
FieldInfo finfo = localizableFields.get(fieldName);
if ((finfo != null) && localizableFields.get(fieldName).hasAnnotation()) {
localizableFields.remove(fieldName);
} else {
boolean justRemoved = bState.removeUncheckedField(fieldName);
if (ins instanceof GETFIELD) {
if (justRemoved) {
localizableFields.remove(fieldName);
if (localizableFields.isEmpty()) {
return;
}
}
} else if (finfo != null) {
finfo.setSrcLineAnnotation(SourceLineAnnotation.fromVisitedInstruction(clsContext, this, ih.getPosition()));
}
}
}
} else if (ins instanceof INVOKESPECIAL) {
INVOKESPECIAL is = (INVOKESPECIAL) ins;
ReferenceType rt = is.getReferenceType(cpg);
if (Values.CONSTRUCTOR.equals(is.getMethodName(cpg))) {
if ((rt instanceof ObjectType) && ((ObjectType) rt).getClassName().startsWith(clsContext.getJavaClass().getClassName() + Values.INNER_CLASS_SEPARATOR)) {
localizableFields.clear();
}
} else {
localizableFields.clear();
}
} else if (ins instanceof INVOKEVIRTUAL) {
INVOKEVIRTUAL is = (INVOKEVIRTUAL) ins;
ReferenceType rt = is.getReferenceType(cpg);
if ((rt instanceof ObjectType) && ((ObjectType) rt).getClassName().equals(clsName)) {
String methodDesc = is.getName(cpg) + is.getSignature(cpg);
Set<String> fields = methodFieldModifiers.get(methodDesc);
if (fields != null) {
localizableFields.keySet().removeAll(fields);
}
}
}
}
if (bState.getUncheckedFieldSize() > 0) {
Iterator<Edge> oei = cfg.outgoingEdgeIterator(bb);
while (oei.hasNext()) {
Edge e = oei.next();
BasicBlock cb = e.getTarget();
int label = cb.getLabel();
if (!visitedBlocks.get(label)) {
toBeProcessed.addLast(new BlockState(cb, bState));
visitedBlocks.set(label);
}
}
}
}
}
Aggregations