Search in sources :

Example 6 with ReferenceType

use of org.apache.bcel.generic.ReferenceType 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;
}
Also used : ConstantPoolGen(org.apache.bcel.generic.ConstantPoolGen) GETFIELD(org.apache.bcel.generic.GETFIELD) ArrayInstruction(org.apache.bcel.generic.ArrayInstruction) ARRAYLENGTH(org.apache.bcel.generic.ARRAYLENGTH) INVOKEINTERFACE(org.apache.bcel.generic.INVOKEINTERFACE) PUTFIELD(org.apache.bcel.generic.PUTFIELD) Instruction(org.apache.bcel.generic.Instruction) FieldInstruction(org.apache.bcel.generic.FieldInstruction) ArrayInstruction(org.apache.bcel.generic.ArrayInstruction) ReferenceType(org.apache.bcel.generic.ReferenceType) INVOKEVIRTUAL(org.apache.bcel.generic.INVOKEVIRTUAL)

Example 7 with ReferenceType

use of org.apache.bcel.generic.ReferenceType in project jop by jop-devel.

the class ReplaceNativeAndCPIdx method replace.

private Method replace(Method method) {
    MethodGen mg = new MethodGen(method, clazz.getClassName(), cpoolgen);
    InstructionList il = mg.getInstructionList();
    InstructionFinder f = new InstructionFinder(il);
    String methodId = method.getName() + method.getSignature();
    OldMethodInfo mi = getCli().getMethodInfo(methodId);
    // find invokes first and replace call to Native by
    // JOP native instructions.
    String invokeStr = "InvokeInstruction";
    for (Iterator i = f.search(invokeStr); i.hasNext(); ) {
        InstructionHandle[] match = (InstructionHandle[]) i.next();
        InstructionHandle first = match[0];
        InvokeInstruction ii = (InvokeInstruction) first.getInstruction();
        if (ii.getClassName(cpoolgen).equals(JOPizer.nativeClass)) {
            short opid = (short) JopInstr.getNative(ii.getMethodName(cpoolgen));
            if (opid == -1) {
                System.err.println(method.getName() + ": cannot locate " + ii.getMethodName(cpoolgen) + ". Replacing with NOP.");
                first.setInstruction(new NOP());
            } else {
                first.setInstruction(new NativeInstruction(opid, (short) 1));
                ((JOPizer) ai).outTxt.println("\t" + first.getPosition());
                // then we remove pc+2 and pc+1 from the MGCI info
                if (JOPizer.dumpMgci) {
                    il.setPositions();
                    int pc = first.getPosition();
                    // important: take the high one first
                    GCRTMethodInfo.removePC(pc + 2, mi);
                    GCRTMethodInfo.removePC(pc + 1, mi);
                }
            }
        }
        if (ii instanceof INVOKESPECIAL) {
            // not an initializer
            if (!ii.getMethodName(cpoolgen).equals("<init>")) {
                // check if this is a super invoke
                // TODO this is just a hack, use InvokeSite.isInvokeSuper() when this is ported to the new framework!
                boolean isSuper = false;
                String declaredType = ii.getClassName(cpoolgen);
                JopClassInfo cls = getCli();
                OldClassInfo superClass = cls.superClass;
                while (superClass != null) {
                    if (superClass.clazz.getClassName().equals(declaredType)) {
                        isSuper = true;
                        break;
                    }
                    if ("java.lang.Object".equals(superClass.clazz.getClassName())) {
                        break;
                    }
                    superClass = superClass.superClass;
                }
                if (isSuper) {
                    Integer idx = ii.getIndex();
                    int new_index = getCli().cpoolUsed.indexOf(idx) + 1;
                    first.setInstruction(new JOPSYS_INVOKESUPER((short) new_index));
                // System.err.println("invokesuper "+ii.getClassName(cpoolgen)+"."+ii.getMethodName(cpoolgen));
                }
            }
        }
    }
    if (JOPizer.CACHE_INVAL) {
        f = new InstructionFinder(il);
        // find volatile reads and insert cache invalidation bytecode
        String fieldInstr = "GETFIELD|GETSTATIC|PUTFIELD|PUTSTATIC";
        for (Iterator i = f.search(fieldInstr); i.hasNext(); ) {
            InstructionHandle[] match = (InstructionHandle[]) i.next();
            InstructionHandle ih = match[0];
            FieldInstruction fi = (FieldInstruction) ih.getInstruction();
            JavaClass jc = JOPizer.jz.cliMap.get(fi.getClassName(cpoolgen)).clazz;
            Field field = null;
            while (field == null) {
                Field[] fields = jc.getFields();
                for (int k = 0; k < fields.length; k++) {
                    if (fields[k].getName().equals(fi.getFieldName(cpoolgen))) {
                        field = fields[k];
                        break;
                    }
                }
                if (field == null) {
                    try {
                        jc = jc.getSuperClass();
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                        throw new Error();
                    }
                }
            }
            if (field.isVolatile()) {
                if (field.getType().getSize() < 2) {
                    if (fi instanceof GETFIELD || fi instanceof GETSTATIC) {
                        ih.setInstruction(new InvalidateInstruction());
                        ih = il.append(ih, fi);
                    }
                } else {
                    // this only works because we do not throw a
                    // NullPointerException for monitorenter/-exit!
                    ih.setInstruction(new ACONST_NULL());
                    ih = il.append(ih, new MONITORENTER());
                    ih = il.append(ih, fi);
                    ih = il.append(ih, new ACONST_NULL());
                    ih = il.append(ih, new MONITOREXIT());
                }
            }
        }
    }
    f = new InstructionFinder(il);
    // find instructions that access the constant pool
    // and replace the index by the new value from ClassInfo
    String cpInstr = "CPInstruction";
    for (Iterator it = f.search(cpInstr); it.hasNext(); ) {
        InstructionHandle[] match = (InstructionHandle[]) it.next();
        InstructionHandle ih = match[0];
        CPInstruction cpii = (CPInstruction) ih.getInstruction();
        int index = cpii.getIndex();
        // we have to grab the information before we change
        // the CP index.
        FieldInstruction fi = null;
        Type ft = null;
        if (cpii instanceof FieldInstruction) {
            fi = (FieldInstruction) ih.getInstruction();
            ft = fi.getFieldType(cpoolgen);
        }
        Integer idx = new Integer(index);
        // pos is the new position in the reduced constant pool
        // idx is the position in the 'original' unresolved cpool
        int pos = getCli().cpoolUsed.indexOf(idx);
        int new_index = pos + 1;
        // and putfield and by address for getstatic and putstatic
        if (cpii instanceof GETFIELD || cpii instanceof PUTFIELD || cpii instanceof GETSTATIC || cpii instanceof PUTSTATIC) {
            // we use the offset instead of the CP index
            new_index = getFieldOffset(cp, index);
        } else {
            if (pos == -1) {
                System.out.println("Error: constant " + index + " " + cpoolgen.getConstant(index) + " not found");
                System.out.println("new cpool: " + getCli().cpoolUsed);
                System.out.println("original cpool: " + cpoolgen);
                System.exit(-1);
            }
        }
        // set new index, position starts at
        // 1 as cp points to the length of the pool
        cpii.setIndex(new_index);
        if (cpii instanceof FieldInstruction) {
            boolean isRef = ft instanceof ReferenceType;
            boolean isLong = ft == BasicType.LONG || ft == BasicType.DOUBLE;
            if (fi instanceof GETSTATIC) {
                if (isRef) {
                    ih.setInstruction(new GETSTATIC_REF((short) new_index));
                } else if (isLong) {
                    ih.setInstruction(new GETSTATIC_LONG((short) new_index));
                }
            } else if (fi instanceof PUTSTATIC) {
                if (isRef) {
                    if (!com.jopdesign.build.JOPizer.USE_RTTM) {
                        ih.setInstruction(new PUTSTATIC_REF((short) new_index));
                    }
                } else if (isLong) {
                    ih.setInstruction(new PUTSTATIC_LONG((short) new_index));
                }
            } else if (fi instanceof GETFIELD) {
                if (isRef) {
                    ih.setInstruction(new GETFIELD_REF((short) new_index));
                } else if (isLong) {
                    ih.setInstruction(new GETFIELD_LONG((short) new_index));
                }
            } else if (fi instanceof PUTFIELD) {
                if (isRef) {
                    if (!com.jopdesign.build.JOPizer.USE_RTTM) {
                        ih.setInstruction(new PUTFIELD_REF((short) new_index));
                    }
                } else if (isLong) {
                    ih.setInstruction(new PUTFIELD_LONG((short) new_index));
                }
            }
        }
    }
    Method m = mg.getMethod();
    il.dispose();
    return m;
}
Also used : InstructionList(org.apache.bcel.generic.InstructionList) InstructionFinder(org.apache.bcel.util.InstructionFinder) MONITORENTER(org.apache.bcel.generic.MONITORENTER) MethodGen(org.apache.bcel.generic.MethodGen) InstructionHandle(org.apache.bcel.generic.InstructionHandle) ReferenceType(org.apache.bcel.generic.ReferenceType) PUTSTATIC(org.apache.bcel.generic.PUTSTATIC) Field(org.apache.bcel.classfile.Field) CPInstruction(org.apache.bcel.generic.CPInstruction) Iterator(java.util.Iterator) MONITOREXIT(org.apache.bcel.generic.MONITOREXIT) ACONST_NULL(org.apache.bcel.generic.ACONST_NULL) PUTFIELD(org.apache.bcel.generic.PUTFIELD) Method(org.apache.bcel.classfile.Method) INVOKESPECIAL(org.apache.bcel.generic.INVOKESPECIAL) NOP(org.apache.bcel.generic.NOP) InvokeInstruction(org.apache.bcel.generic.InvokeInstruction) GETFIELD(org.apache.bcel.generic.GETFIELD) ReferenceType(org.apache.bcel.generic.ReferenceType) Type(org.apache.bcel.generic.Type) BasicType(org.apache.bcel.generic.BasicType) ConstantNameAndType(org.apache.bcel.classfile.ConstantNameAndType) JavaClass(org.apache.bcel.classfile.JavaClass) FieldInstruction(org.apache.bcel.generic.FieldInstruction) GETSTATIC(org.apache.bcel.generic.GETSTATIC)

Example 8 with ReferenceType

use of org.apache.bcel.generic.ReferenceType in project jop by jop-devel.

the class MethodCode method getReferencedClassName.

/**
     * Get the classname or array-type name referenced by an invoke- or field instruction in this code.
     *
     * @param instr the instruction to check, using this methods constantpool.
     * @return the referenced classname or array-typename, to be used for a ClassRef or AppInfo getter. 
     */
public String getReferencedClassName(FieldOrMethod instr) {
    ConstantPoolGen cpg = getConstantPoolGen();
    ReferenceType refType = instr.getReferenceType(cpg);
    String classname;
    if (refType instanceof ObjectType) {
        classname = ((ObjectType) refType).getClassName();
    } else if (refType instanceof ArrayType) {
        // need to call array.<method>, which class should we use? Let's decide later..
        String msg = "Calling a method of an array: " + refType.getSignature() + "#" + instr.getName(cpg) + " in " + methodInfo;
        logger.debug(msg);
        classname = refType.getSignature();
    } else {
        // Hu??
        throw new JavaClassFormatError("Unknown reference type " + refType);
    }
    return classname;
}
Also used : ArrayType(org.apache.bcel.generic.ArrayType) ConstantPoolGen(org.apache.bcel.generic.ConstantPoolGen) ObjectType(org.apache.bcel.generic.ObjectType) JavaClassFormatError(com.jopdesign.common.misc.JavaClassFormatError) HashedString(com.jopdesign.common.misc.HashedString) CallString(com.jopdesign.common.code.CallString) ReferenceType(org.apache.bcel.generic.ReferenceType)

Example 9 with ReferenceType

use of org.apache.bcel.generic.ReferenceType in project jop by jop-devel.

the class SymbolicPointsTo method initial.

public ContextMap<CallString, SymbolicAddressMap> initial(InstructionHandle stmt) {
    ContextMap<CallString, SymbolicAddressMap> retval = new ContextMap<CallString, SymbolicAddressMap>(new Context(), new HashMap<CallString, SymbolicAddressMap>());
    SymbolicAddressMap init = new SymbolicAddressMap(bsFactory);
    // Add symbolic stack names
    int stackPtr = 0;
    if (!entryMethod.isStatic()) {
        init.putStack(stackPtr++, bsFactory.singleton(SymbolicAddress.rootAddress("$this")));
    }
    String[] args = entryMethod.getArgumentNames();
    for (int i = 0; i < args.length; i++) {
        Type ty = entryMethod.getArgumentType(i);
        if (ty instanceof ReferenceType) {
            init.putStack(stackPtr, bsFactory.singleton(SymbolicAddress.rootAddress("$" + args[i])));
        }
        stackPtr += ty.getSize();
    }
    retval.put(initialCallString, init);
    return retval;
}
Also used : Context(com.jopdesign.dfa.framework.Context) ReferenceType(org.apache.bcel.generic.ReferenceType) Type(org.apache.bcel.generic.Type) CallString(com.jopdesign.common.code.CallString) ContextMap(com.jopdesign.dfa.framework.ContextMap) ReferenceType(org.apache.bcel.generic.ReferenceType) CallString(com.jopdesign.common.code.CallString)

Example 10 with ReferenceType

use of org.apache.bcel.generic.ReferenceType in project jop by jop-devel.

the class ObjectCacheAnalysis method getFieldIndex.

/**
	 * @return the index of the field accessed by the instruction, or 0 if the instruction
	 * does not access a field
	 */
private static int getFieldIndex(WCETTool p, ControlFlowGraph cfg, InstructionHandle ih) {
    ConstantPoolGen constPool = cfg.getMethodInfo().getConstantPoolGen();
    Instruction instr = ih.getInstruction();
    if (instr instanceof FieldInstruction) {
        FieldInstruction fieldInstr = (FieldInstruction) instr;
        ReferenceType refType = fieldInstr.getReferenceType(constPool);
        if (!(refType instanceof ObjectType)) {
            throw new RuntimeException("getFieldIndex(): Unsupported object kind: " + refType.getClass());
        }
        ObjectType objType = (ObjectType) refType;
        String klassName = objType.getClassName();
        String fieldName = fieldInstr.getFieldName(constPool);
        String fieldSig = fieldInstr.getSignature(constPool);
        return p.getLinkerInfo().getFieldIndex(klassName, fieldName + fieldSig);
    } else {
        return 0;
    }
}
Also used : ConstantPoolGen(org.apache.bcel.generic.ConstantPoolGen) ObjectType(org.apache.bcel.generic.ObjectType) FieldInstruction(org.apache.bcel.generic.FieldInstruction) CallString(com.jopdesign.common.code.CallString) Instruction(org.apache.bcel.generic.Instruction) FieldInstruction(org.apache.bcel.generic.FieldInstruction) ArrayInstruction(org.apache.bcel.generic.ArrayInstruction) ReferenceType(org.apache.bcel.generic.ReferenceType)

Aggregations

ReferenceType (org.apache.bcel.generic.ReferenceType)10 CallString (com.jopdesign.common.code.CallString)6 GETFIELD (org.apache.bcel.generic.GETFIELD)6 PUTFIELD (org.apache.bcel.generic.PUTFIELD)6 Type (org.apache.bcel.generic.Type)6 FieldInstruction (org.apache.bcel.generic.FieldInstruction)5 GETSTATIC (org.apache.bcel.generic.GETSTATIC)5 Instruction (org.apache.bcel.generic.Instruction)5 PUTSTATIC (org.apache.bcel.generic.PUTSTATIC)5 Context (com.jopdesign.dfa.framework.Context)4 ContextMap (com.jopdesign.dfa.framework.ContextMap)4 ObjectType (org.apache.bcel.generic.ObjectType)4 DFATool (com.jopdesign.dfa.DFATool)3 Set (java.util.Set)3 ArrayInstruction (org.apache.bcel.generic.ArrayInstruction)3 InvokeInstruction (org.apache.bcel.generic.InvokeInstruction)3 LDC (org.apache.bcel.generic.LDC)3 LoadInstruction (org.apache.bcel.generic.LoadInstruction)3 StoreInstruction (org.apache.bcel.generic.StoreInstruction)3 MethodInfo (com.jopdesign.common.MethodInfo)2