Search in sources :

Example 1 with INVOKESTATIC

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

the class DFATool method buildPrologue.

private MethodInfo buildPrologue(MethodInfo mainMethod, List<InstructionHandle> statements, Flow flow, List<ClassInfo> clinits) {
    // we use a prologue sequence for startup
    InstructionList prologue = new InstructionList();
    ConstantPoolGen prologueCP = mainMethod.getConstantPoolGen();
    Instruction instr;
    int idx;
    // add magic initializers to prologue sequence
    if (!analyzeBootMethod) {
        instr = new ICONST(0);
        prologue.append(instr);
        instr = new ICONST(0);
        prologue.append(instr);
        idx = prologueCP.addMethodref("com.jopdesign.sys.GC", "init", "(II)V");
        instr = new INVOKESTATIC(idx);
        prologue.append(instr);
    }
    // add class initializers
    for (ClassInfo clinit : clinits) {
        MemberID cSig = appInfo.getClinitSignature(clinit.getClassName());
        idx = prologueCP.addMethodref(cSig.getClassName(), cSig.getMemberName(), cSig.getDescriptor().toString());
        instr = new INVOKESTATIC(idx);
        prologue.append(instr);
    }
    if (analyzeBootMethod) {
        // Let's just analyze the full boot method, so that the callgraph-builder is happy.
        // Doing this after clinit, so that we have DFA infos on fields in JVMHelp.init()
        idx = prologueCP.addMethodref("com.jopdesign.sys.Startup", "boot", "()V");
        instr = new INVOKESTATIC(idx);
        prologue.append(instr);
    }
    // add main method
    instr = new ACONST_NULL();
    prologue.append(instr);
    idx = prologueCP.addMethodref(mainMethod.getClassName(), mainMethod.getShortName(), mainMethod.getDescriptor().toString());
    instr = new INVOKESTATIC(idx);
    prologue.append(instr);
    // // invoke startMission() to ensure analysis of threads
    // idx = prologueCP.addMethodref("joprt.RtThread", "startMission", "()V");
    // instr = new INVOKESTATIC(idx);
    // prologue.append(instr);
    instr = new NOP();
    prologue.append(instr);
    prologue.setPositions(true);
    // add prologue to program structure
    for (Iterator l = prologue.iterator(); l.hasNext(); ) {
        InstructionHandle handle = (InstructionHandle) l.next();
        statements.add(handle);
        if (handle.getInstruction() instanceof GOTO) {
            GOTO g = (GOTO) handle.getInstruction();
            flow.addEdge(new FlowEdge(handle, g.getTarget(), FlowEdge.NORMAL_EDGE));
        } else if (handle.getNext() != null) {
            flow.addEdge(new FlowEdge(handle, handle.getNext(), FlowEdge.NORMAL_EDGE));
        }
    }
    MemberID pSig = new MemberID(prologueName, Descriptor.parse(prologueSig));
    MethodInfo mi = mainMethod.getClassInfo().createMethod(pSig, null, prologue);
    mi.setAccessType(AccessType.ACC_PRIVATE);
    return mi;
}
Also used : FlowEdge(com.jopdesign.dfa.framework.FlowEdge) INVOKESTATIC(org.apache.bcel.generic.INVOKESTATIC) GOTO(org.apache.bcel.generic.GOTO) InstructionList(org.apache.bcel.generic.InstructionList) Instruction(org.apache.bcel.generic.Instruction) BranchInstruction(org.apache.bcel.generic.BranchInstruction) ReturnInstruction(org.apache.bcel.generic.ReturnInstruction) NOP(org.apache.bcel.generic.NOP) InstructionHandle(org.apache.bcel.generic.InstructionHandle) ConstantPoolGen(org.apache.bcel.generic.ConstantPoolGen) MemberID(com.jopdesign.common.type.MemberID) Iterator(java.util.Iterator) MethodInfo(com.jopdesign.common.MethodInfo) ACONST_NULL(org.apache.bcel.generic.ACONST_NULL) ICONST(org.apache.bcel.generic.ICONST) ClassInfo(com.jopdesign.common.ClassInfo)

Example 2 with INVOKESTATIC

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

the class JOPModel method getNativeOpCode.

/* performance hot spot */
public int getNativeOpCode(MethodInfo context, Instruction instr) {
    if (isSpecialInvoke(context, instr)) {
        INVOKESTATIC isi = (INVOKESTATIC) instr;
        String methodName = isi.getMethodName(context.getConstantPoolGen());
        return JopInstr.getNative(methodName);
    } else if (instr instanceof FieldInstruction) {
        FieldInstruction finstr = (FieldInstruction) instr;
        Type ftype = finstr.getFieldType(context.getConstantPoolGen());
        boolean isRef = ftype instanceof ReferenceType;
        boolean isLong = ftype == BasicType.LONG || ftype == BasicType.DOUBLE;
        if (finstr instanceof GETSTATIC) {
            if (isRef) {
                return JopInstr.get("getstatic_ref");
            } else if (isLong) {
                return JopInstr.get("getstatic_long");
            }
        } else if (finstr instanceof PUTSTATIC) {
            if (isRef) {
                if (!com.jopdesign.build.JOPizer.USE_RTTM) {
                    /* FIXME: This should not be hardcoded? */
                    return JopInstr.get("putstatic_ref");
                }
            } else if (isLong) {
                return JopInstr.get("putstatic_long");
            }
        } else if (finstr instanceof GETFIELD) {
            if (isRef) {
                return JopInstr.get("getfield_ref");
            } else if (isLong) {
                return JopInstr.get("getfield_long");
            }
        } else if (finstr instanceof PUTFIELD) {
            if (isRef) {
                if (!com.jopdesign.build.JOPizer.USE_RTTM) {
                    /* FIXME: This should not be hardcoded? */
                    return JopInstr.get("putfield_ref");
                }
            } else if (isLong) {
                return JopInstr.get("putfield_long");
            }
        }
        return instr.getOpcode();
    } else {
        return instr.getOpcode();
    }
}
Also used : INVOKESTATIC(org.apache.bcel.generic.INVOKESTATIC) GETFIELD(org.apache.bcel.generic.GETFIELD) ReferenceType(org.apache.bcel.generic.ReferenceType) Type(org.apache.bcel.generic.Type) ObjectType(org.apache.bcel.generic.ObjectType) BasicType(org.apache.bcel.generic.BasicType) PUTFIELD(org.apache.bcel.generic.PUTFIELD) FieldInstruction(org.apache.bcel.generic.FieldInstruction) GETSTATIC(org.apache.bcel.generic.GETSTATIC) ReferenceType(org.apache.bcel.generic.ReferenceType) PUTSTATIC(org.apache.bcel.generic.PUTSTATIC)

Example 3 with INVOKESTATIC

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

the class JOPModel method isSpecialInvoke.

public boolean isSpecialInvoke(MethodInfo context, Instruction i) {
    if (!(i instanceof INVOKESTATIC))
        return false;
    INVOKESTATIC isi = (INVOKESTATIC) i;
    ReferenceType refTy = isi.getReferenceType(context.getConstantPoolGen());
    if (refTy instanceof ObjectType) {
        ObjectType objTy = (ObjectType) refTy;
        String className = objTy.getClassName();
        return (className.equals(JOP_NATIVE));
    } else {
        return false;
    }
}
Also used : INVOKESTATIC(org.apache.bcel.generic.INVOKESTATIC) ObjectType(org.apache.bcel.generic.ObjectType) ReferenceType(org.apache.bcel.generic.ReferenceType)

Example 4 with INVOKESTATIC

use of org.apache.bcel.generic.INVOKESTATIC 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());
    }
}
Also used : INVOKESTATIC(org.apache.bcel.generic.INVOKESTATIC) NEW(org.apache.bcel.generic.NEW) InstructionList(org.apache.bcel.generic.InstructionList) LocalVariableGen(org.apache.bcel.generic.LocalVariableGen) INVOKESPECIAL(org.apache.bcel.generic.INVOKESPECIAL) ConstantPoolGen(org.apache.bcel.generic.ConstantPoolGen) InvokeInstruction(org.apache.bcel.generic.InvokeInstruction) BooleanType(org.apache.xalan.xsltc.compiler.util.BooleanType) Type(org.apache.xalan.xsltc.compiler.util.Type) ObjectType(org.apache.xalan.xsltc.compiler.util.ObjectType) IntType(org.apache.xalan.xsltc.compiler.util.IntType) MethodType(org.apache.xalan.xsltc.compiler.util.MethodType) ReferenceType(org.apache.xalan.xsltc.compiler.util.ReferenceType) INVOKEINTERFACE(org.apache.bcel.generic.INVOKEINTERFACE) INVOKEVIRTUAL(org.apache.bcel.generic.INVOKEVIRTUAL) PUSH(org.apache.bcel.generic.PUSH)

Example 5 with INVOKESTATIC

use of org.apache.bcel.generic.INVOKESTATIC in project servicemix-bundles by apache.

the class FunctionCall method translateUnallowedExtension.

/**
 * Translate code to call the BasisLibrary.unallowed_extensionF(String)
 * method.
 */
private void translateUnallowedExtension(ConstantPoolGen cpg, InstructionList il) {
    int index = cpg.addMethodref(BASIS_LIBRARY_CLASS, "unallowed_extension_functionF", "(Ljava/lang/String;)V");
    il.append(new PUSH(cpg, _fname.toString()));
    il.append(new INVOKESTATIC(index));
}
Also used : INVOKESTATIC(org.apache.bcel.generic.INVOKESTATIC) PUSH(org.apache.bcel.generic.PUSH)

Aggregations

INVOKESTATIC (org.apache.bcel.generic.INVOKESTATIC)6 ConstantPoolGen (org.apache.bcel.generic.ConstantPoolGen)2 InstructionList (org.apache.bcel.generic.InstructionList)2 ObjectType (org.apache.bcel.generic.ObjectType)2 PUSH (org.apache.bcel.generic.PUSH)2 ReferenceType (org.apache.bcel.generic.ReferenceType)2 Type (org.apache.bcel.generic.Type)2 ClassInfo (com.jopdesign.common.ClassInfo)1 MethodInfo (com.jopdesign.common.MethodInfo)1 MemberID (com.jopdesign.common.type.MemberID)1 FlowEdge (com.jopdesign.dfa.framework.FlowEdge)1 Iterator (java.util.Iterator)1 ACONST_NULL (org.apache.bcel.generic.ACONST_NULL)1 BasicType (org.apache.bcel.generic.BasicType)1 BranchInstruction (org.apache.bcel.generic.BranchInstruction)1 FieldInstruction (org.apache.bcel.generic.FieldInstruction)1 GETFIELD (org.apache.bcel.generic.GETFIELD)1 GETSTATIC (org.apache.bcel.generic.GETSTATIC)1 GOTO (org.apache.bcel.generic.GOTO)1 ICONST (org.apache.bcel.generic.ICONST)1