Java Code Examples for com.sun.org.apache.bcel.internal.generic.ConstantPoolGen#addInterfaceMethodref()

The following examples show how to use com.sun.org.apache.bcel.internal.generic.ConstantPoolGen#addInterfaceMethodref() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PositionCall.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
    final InstructionList il = methodGen.getInstructionList();

    if (methodGen instanceof CompareGenerator) {
        il.append(((CompareGenerator)methodGen).loadCurrentNode());
    }
    else if (methodGen instanceof TestGenerator) {
        il.append(new ILOAD(POSITION_INDEX));
    }
    else {
        final ConstantPoolGen cpg = classGen.getConstantPool();
        final int index = cpg.addInterfaceMethodref(NODE_ITERATOR,
                                                   "getPosition",
                                                   "()I");

        il.append(methodGen.loadIterator());
        il.append(new INVOKEINTERFACE(index,1));
    }
}
 
Example 2
Source File: VariableBase.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
/**
 * Remove the mapping of this variable to a register.
 * Called when we leave the AST scope of the variable's declaration
 */
public void unmapRegister(ClassGenerator classGen, MethodGenerator methodGen) {
    if (_local != null) {
        if (_type instanceof ResultTreeType) {
            final ConstantPoolGen cpg = classGen.getConstantPool();
            final InstructionList il = methodGen.getInstructionList();
            if (classGen.getStylesheet().callsNodeset() && classGen.getDOMClass().equals(MULTI_DOM_CLASS)) {
                final int removeDA = cpg.addMethodref(MULTI_DOM_CLASS, "removeDOMAdapter", "(" + DOM_ADAPTER_SIG + ")V");
                il.append(methodGen.loadDOM());
                il.append(new CHECKCAST(cpg.addClass(MULTI_DOM_CLASS)));
                il.append(loadInstruction());
                il.append(new CHECKCAST(cpg.addClass(DOM_ADAPTER_CLASS)));
                il.append(new INVOKEVIRTUAL(removeDA));
            }
            final int release = cpg.addInterfaceMethodref(DOM_IMPL_CLASS, "release", "()V");
            il.append(loadInstruction());
            il.append(new INVOKEINTERFACE(release, 1));
        }

        _local.setEnd(methodGen.getInstructionList().getEnd());
        methodGen.removeLocalVariable(_local);
        _refs = null;
        _local = null;
    }
}
 
Example 3
Source File: ReferenceType.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
/**
 * Casts a reference into a NodeIterator.
 *
 * @see     com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
 */
public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
                        NodeSetType type) {
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();
    int index = cpg.addMethodref(BASIS_LIBRARY_CLASS, "referenceToNodeSet",
                                 "("
                                 + OBJECT_SIG
                                 + ")"
                                 + NODE_ITERATOR_SIG);
    il.append(new INVOKESTATIC(index));

    // Reset this iterator
    index = cpg.addInterfaceMethodref(NODE_ITERATOR, RESET, RESET_SIG);
    il.append(new INVOKEINTERFACE(index, 1));
}
 
Example 4
Source File: NamespaceUriCall.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translate code that leaves a node's namespace URI (as a String)
 * on the stack
 */
public void translate(ClassGenerator classGen,
                      MethodGenerator methodGen) {
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();

    // Returns the string value for a node in the DOM
    final int getNamespace = cpg.addInterfaceMethodref(DOM_INTF,
                                                       "getNamespaceName",
                                                       "(I)"+STRING_SIG);
    super.translate(classGen, methodGen);
    il.append(new INVOKEINTERFACE(getNamespace, 2));
}
 
Example 5
Source File: Mode.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public static void compileGetChildren(ClassGenerator classGen,
                                      MethodGenerator methodGen,
                                      int node) {
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();
    final int git = cpg.addInterfaceMethodref(DOM_INTF,
                                              GET_CHILDREN,
                                              GET_CHILDREN_SIG);
    il.append(methodGen.loadDOM());
    il.append(new ILOAD(node));
    il.append(new INVOKEINTERFACE(git, 2));
}
 
Example 6
Source File: Mode.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void compileGetChildren(ClassGenerator classGen,
                                      MethodGenerator methodGen,
                                      int node) {
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();
    final int git = cpg.addInterfaceMethodref(DOM_INTF,
                                              GET_CHILDREN,
                                              GET_CHILDREN_SIG);
    il.append(methodGen.loadDOM());
    il.append(new ILOAD(node));
    il.append(new INVOKEINTERFACE(git, 2));
}
 
Example 7
Source File: CastExpr.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public void translateDesynthesized(ClassGenerator classGen,
                                   MethodGenerator methodGen) {
    FlowList fl;
    final Type ltype = _left.getType();

    // This is a special case for the self:: axis. Instead of letting
    // the Step object create and iterator that we cast back to a single
    // node, we simply ask the DOM for the node type.
    if (_typeTest) {
        final ConstantPoolGen cpg = classGen.getConstantPool();
        final InstructionList il = methodGen.getInstructionList();

        final int idx = cpg.addInterfaceMethodref(DOM_INTF,
                                                  "getExpandedTypeID",
                                                  "(I)I");
        il.append(new SIPUSH((short)((Step)_left).getNodeType()));
        il.append(methodGen.loadDOM());
        il.append(methodGen.loadContextNode());
        il.append(new INVOKEINTERFACE(idx, 2));
        _falseList.add(il.append(new IF_ICMPNE(null)));
    }
    else {

        _left.translate(classGen, methodGen);
        if (_type != ltype) {
            _left.startIterator(classGen, methodGen);
            if (_type instanceof BooleanType) {
                fl = ltype.translateToDesynthesized(classGen, methodGen,
                                                    _type);
                if (fl != null) {
                    _falseList.append(fl);
                }
            }
            else {
                ltype.translateTo(classGen, methodGen, _type);
            }
        }
    }
}
 
Example 8
Source File: WithParam.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * Release the compiled result tree.
 */
public void releaseResultTree(ClassGenerator classGen,
                              MethodGenerator methodGen)
{
    if (_domAdapter != null) {
        final ConstantPoolGen cpg = classGen.getConstantPool();
        final InstructionList il = methodGen.getInstructionList();
        if (classGen.getStylesheet().callsNodeset() &&
            classGen.getDOMClass().equals(MULTI_DOM_CLASS))
        {
            final int removeDA =
                cpg.addMethodref(MULTI_DOM_CLASS, "removeDOMAdapter",
                                 "(" + DOM_ADAPTER_SIG + ")V");
            il.append(methodGen.loadDOM());
            il.append(new CHECKCAST(cpg.addClass(MULTI_DOM_CLASS)));
            il.append(new ALOAD(_domAdapter.getIndex()));
            il.append(new CHECKCAST(cpg.addClass(DOM_ADAPTER_CLASS)));
            il.append(new INVOKEVIRTUAL(removeDA));
        }
        final int release =
            cpg.addInterfaceMethodref(DOM_IMPL_CLASS, "release", "()V");
        il.append(new ALOAD(_domAdapter.getIndex()));
        il.append(new INVOKEINTERFACE(release, 1));
        _domAdapter.setEnd(il.getEnd());
        methodGen.removeLocalVariable(_domAdapter);
        _domAdapter = null;
    }
}
 
Example 9
Source File: Mode.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void compileGetChildren(ClassGenerator classGen,
                                      MethodGenerator methodGen,
                                      int node) {
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();
    final int git = cpg.addInterfaceMethodref(DOM_INTF,
                                              GET_CHILDREN,
                                              GET_CHILDREN_SIG);
    il.append(methodGen.loadDOM());
    il.append(new ILOAD(node));
    il.append(new INVOKEINTERFACE(git, 2));
}
 
Example 10
Source File: ParentLocationPath.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
public void translateStep(ClassGenerator classGen, MethodGenerator methodGen) {
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();

    // Backwards branches are prohibited if an uninitialized object is
    // on the stack by section 4.9.4 of the JVM Specification, 2nd Ed.
    // We don't know whether this code might contain backwards branches
    // so we mustn't create the new object until after we've created
    // the suspect arguments to its constructor.  Instead we calculate
    // the values of the arguments to the constructor first, store them
    // in temporary variables, create the object and reload the
    // arguments from the temporaries to avoid the problem.

    LocalVariableGen pathTemp
            = methodGen.addLocalVariable("parent_location_path_tmp1",
                                     Util.getJCRefType(NODE_ITERATOR_SIG),
                                     null, null);
    pathTemp.setStart(il.append(new ASTORE(pathTemp.getIndex())));

    _step.translate(classGen, methodGen);
    LocalVariableGen stepTemp
            = methodGen.addLocalVariable("parent_location_path_tmp2",
                                     Util.getJCRefType(NODE_ITERATOR_SIG),
                                     null, null);
    stepTemp.setStart(il.append(new ASTORE(stepTemp.getIndex())));

    // Create new StepIterator
    final int initSI = cpg.addMethodref(STEP_ITERATOR_CLASS,
                                        "<init>",
                                        "("
                                        +NODE_ITERATOR_SIG
                                        +NODE_ITERATOR_SIG
                                        +")V");
    il.append(new NEW(cpg.addClass(STEP_ITERATOR_CLASS)));
    il.append(DUP);

    pathTemp.setEnd(il.append(new ALOAD(pathTemp.getIndex())));
    stepTemp.setEnd(il.append(new ALOAD(stepTemp.getIndex())));

    // Initialize StepIterator with iterators from the stack
    il.append(new INVOKESPECIAL(initSI));

    // This is a special case for the //* path with or without predicates
    Expression stp = _step;
    if (stp instanceof ParentLocationPath)
        stp = ((ParentLocationPath)stp).getStep();

    if ((_path instanceof Step) && (stp instanceof Step)) {
        final int path = ((Step)_path).getAxis();
        final int step = ((Step)stp).getAxis();
        if ((path == Axis.DESCENDANTORSELF && step == Axis.CHILD) ||
            (path == Axis.PRECEDING        && step == Axis.PARENT)) {
            final int incl = cpg.addMethodref(NODE_ITERATOR_BASE,
                                              "includeSelf",
                                              "()" + NODE_ITERATOR_SIG);
            il.append(new INVOKEVIRTUAL(incl));
        }
    }

    /*
     * If this pattern contains a sequence of descendant iterators we
     * run the risk of returning the same node several times. We put
     * a new iterator on top of the existing one to assure node order
     * and prevent returning a single node multiple times.
     */
    if (_orderNodes) {
        final int order = cpg.addInterfaceMethodref(DOM_INTF,
                                                    ORDER_ITERATOR,
                                                    ORDER_ITERATOR_SIG);
        il.append(methodGen.loadDOM());
        il.append(SWAP);
        il.append(methodGen.loadContextNode());
        il.append(new INVOKEINTERFACE(order, 3));
    }
}
 
Example 11
Source File: AncestorPattern.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
    InstructionHandle parent;
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();

    /*
     * The scope of this local var must be the entire method since
     * a another pattern may decide to jump back into the loop
     */
    final LocalVariableGen local =
        methodGen.addLocalVariable2("app", Util.getJCRefType(NODE_SIG),
                                    il.getEnd());

    final com.sun.org.apache.bcel.internal.generic.Instruction loadLocal =
        new ILOAD(local.getIndex());
    final com.sun.org.apache.bcel.internal.generic.Instruction storeLocal =
        new ISTORE(local.getIndex());

    if (_right instanceof StepPattern) {
        il.append(DUP);
        il.append(storeLocal);
        _right.translate(classGen, methodGen);
        il.append(methodGen.loadDOM());
        il.append(loadLocal);
    }
    else {
        _right.translate(classGen, methodGen);

        if (_right instanceof AncestorPattern) {
            il.append(methodGen.loadDOM());
            il.append(SWAP);
        }
    }

    if (_left != null) {
        final int getParent = cpg.addInterfaceMethodref(DOM_INTF,
                                                        GET_PARENT,
                                                        GET_PARENT_SIG);
        parent = il.append(new INVOKEINTERFACE(getParent, 2));

        il.append(DUP);
        il.append(storeLocal);
        _falseList.add(il.append(new IFLT(null)));
        il.append(loadLocal);

        _left.translate(classGen, methodGen);

        final SyntaxTreeNode p = getParent();
        if (p == null || p instanceof Instruction ||
            p instanceof TopLevelElement)
        {
            // do nothing
        }
        else {
            il.append(loadLocal);
        }

        final BranchHandle exit = il.append(new GOTO(null));
        _loop = il.append(methodGen.loadDOM());
        il.append(loadLocal);
        local.setEnd(_loop);
        il.append(new GOTO(parent));
        exit.setTarget(il.append(NOP));
        _left.backPatchFalseList(_loop);

        _trueList.append(_left._trueList);
    }
    else {
        il.append(POP2);
    }

    /*
     * If _right is an ancestor pattern, backpatch this pattern's false
     * list to the loop that searches for more ancestors.
     */
    if (_right instanceof AncestorPattern) {
        final AncestorPattern ancestor = (AncestorPattern) _right;
        _falseList.backPatch(ancestor.getLoopHandle());    // clears list
    }

    _trueList.append(_right._trueList);
    _falseList.append(_right._falseList);
}
 
Example 12
Source File: Whitespace.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Compiles the predicate method
 */
private static void compilePredicate(Vector rules,
                                     int defaultAction,
                                     ClassGenerator classGen) {
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = new InstructionList();
    final XSLTC xsltc = classGen.getParser().getXSLTC();

    // private boolean Translet.stripSpace(int type) - cannot be static
    final MethodGenerator stripSpace =
        new MethodGenerator(ACC_PUBLIC | ACC_FINAL ,
                    com.sun.org.apache.bcel.internal.generic.Type.BOOLEAN,
                    new com.sun.org.apache.bcel.internal.generic.Type[] {
                        Util.getJCRefType(DOM_INTF_SIG),
                        com.sun.org.apache.bcel.internal.generic.Type.INT,
                        com.sun.org.apache.bcel.internal.generic.Type.INT
                    },
                    new String[] { "dom","node","type" },
                    "stripSpace",classGen.getClassName(),il,cpg);

    classGen.addInterface("com/sun/org/apache/xalan/internal/xsltc/StripFilter");

    final int paramDom = stripSpace.getLocalIndex("dom");
    final int paramCurrent = stripSpace.getLocalIndex("node");
    final int paramType = stripSpace.getLocalIndex("type");

    BranchHandle strip[] = new BranchHandle[rules.size()];
    BranchHandle preserve[] = new BranchHandle[rules.size()];
    int sCount = 0;
    int pCount = 0;

    // Traverse all strip/preserve rules
    for (int i = 0; i<rules.size(); i++) {
        // Get the next rule in the prioritised list
        WhitespaceRule rule = (WhitespaceRule)rules.elementAt(i);

        // Returns the namespace for a node in the DOM
        final int gns = cpg.addInterfaceMethodref(DOM_INTF,
                                                  "getNamespaceName",
                                                  "(I)Ljava/lang/String;");

        final int strcmp = cpg.addMethodref("java/lang/String",
                                            "compareTo",
                                            "(Ljava/lang/String;)I");

        // Handle elements="ns:*" type rule
        if (rule.getStrength() == RULE_NAMESPACE) {
            il.append(new ALOAD(paramDom));
            il.append(new ILOAD(paramCurrent));
            il.append(new INVOKEINTERFACE(gns,2));
            il.append(new PUSH(cpg, rule.getNamespace()));
            il.append(new INVOKEVIRTUAL(strcmp));
            il.append(ICONST_0);

            if (rule.getAction() == STRIP_SPACE) {
                strip[sCount++] = il.append(new IF_ICMPEQ(null));
            }
            else {
                preserve[pCount++] = il.append(new IF_ICMPEQ(null));
            }
        }
        // Handle elements="ns:el" type rule
        else if (rule.getStrength() == RULE_ELEMENT) {
            // Create the QName for the element
            final Parser parser = classGen.getParser();
            QName qname;
            if (rule.getNamespace() != Constants.EMPTYSTRING )
                qname = parser.getQName(rule.getNamespace(), null,
                                        rule.getElement());
            else
                qname = parser.getQName(rule.getElement());

            // Register the element.
            final int elementType = xsltc.registerElement(qname);
            il.append(new ILOAD(paramType));
            il.append(new PUSH(cpg, elementType));

            // Compare current node type with wanted element type
            if (rule.getAction() == STRIP_SPACE)
                strip[sCount++] = il.append(new IF_ICMPEQ(null));
            else
                preserve[pCount++] = il.append(new IF_ICMPEQ(null));
        }
    }

    if (defaultAction == STRIP_SPACE) {
        compileStripSpace(strip, sCount, il);
        compilePreserveSpace(preserve, pCount, il);
    }
    else {
        compilePreserveSpace(preserve, pCount, il);
        compileStripSpace(strip, sCount, il);
    }

    classGen.addMethod(stripSpace);
}
 
Example 13
Source File: FilterParentPath.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();
    // Create new StepIterator
    final int initSI = cpg.addMethodref(STEP_ITERATOR_CLASS,
                                        "<init>",
                                        "("
                                        +NODE_ITERATOR_SIG
                                        +NODE_ITERATOR_SIG
                                        +")V");

    // Backwards branches are prohibited if an uninitialized object is
    // on the stack by section 4.9.4 of the JVM Specification, 2nd Ed.
    // We don't know whether this code might contain backwards branches,
    // so we mustn't create the new object until after we've created
    // the suspect arguments to its constructor.  Instead we calculate
    // the values of the arguments to the constructor first, store them
    // in temporary variables, create the object and reload the
    // arguments from the temporaries to avoid the problem.

    // Recursively compile 2 iterators
    _filterExpr.translate(classGen, methodGen);
    LocalVariableGen filterTemp =
            methodGen.addLocalVariable("filter_parent_path_tmp1",
                                       Util.getJCRefType(NODE_ITERATOR_SIG),
                                       null, null);
    filterTemp.setStart(il.append(new ASTORE(filterTemp.getIndex())));

    _path.translate(classGen, methodGen);
    LocalVariableGen pathTemp =
            methodGen.addLocalVariable("filter_parent_path_tmp2",
                                       Util.getJCRefType(NODE_ITERATOR_SIG),
                                       null, null);
    pathTemp.setStart(il.append(new ASTORE(pathTemp.getIndex())));

    il.append(new NEW(cpg.addClass(STEP_ITERATOR_CLASS)));
    il.append(DUP);
    filterTemp.setEnd(il.append(new ALOAD(filterTemp.getIndex())));
    pathTemp.setEnd(il.append(new ALOAD(pathTemp.getIndex())));

    // Initialize StepIterator with iterators from the stack
    il.append(new INVOKESPECIAL(initSI));

    // This is a special case for the //* path with or without predicates
    if (_hasDescendantAxis) {
        final int incl = cpg.addMethodref(NODE_ITERATOR_BASE,
                                          "includeSelf",
                                          "()" + NODE_ITERATOR_SIG);
        il.append(new INVOKEVIRTUAL(incl));
    }

    SyntaxTreeNode parent = getParent();

    boolean parentAlreadyOrdered =
        (parent instanceof RelativeLocationPath)
            || (parent instanceof FilterParentPath)
            || (parent instanceof KeyCall)
            || (parent instanceof CurrentCall)
            || (parent instanceof DocumentCall);

    if (!parentAlreadyOrdered) {
        final int order = cpg.addInterfaceMethodref(DOM_INTF,
                                                    ORDER_ITERATOR,
                                                    ORDER_ITERATOR_SIG);
        il.append(methodGen.loadDOM());
        il.append(SWAP);
        il.append(methodGen.loadContextNode());
        il.append(new INVOKEINTERFACE(order, 3));
    }
}
 
Example 14
Source File: Key.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * This method is called if the "use" attribute of the key contains a
 * node set. In this case we must traverse all nodes in the set and
 * create one entry in this key's index for each node in the set.
 */
public void traverseNodeSet(ClassGenerator classGen,
                            MethodGenerator methodGen,
                            int buildKeyIndex) {
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();

    // DOM.getStringValueX(nodeIndex) => String
    final int getNodeValue = cpg.addInterfaceMethodref(DOM_INTF,
                                                       GET_NODE_VALUE,
                                                       "(I)"+STRING_SIG);

    final int getNodeIdent = cpg.addInterfaceMethodref(DOM_INTF,
                                                       "getNodeIdent",
                                                       "(I)"+NODE_SIG);

    // AbstractTranslet.SetKeyIndexDom(name, Dom) => void
    final int keyDom = cpg.addMethodref(TRANSLET_CLASS,
                                     "setKeyIndexDom",
                                     "("+STRING_SIG+DOM_INTF_SIG+")V");


    // This variable holds the id of the node we found with the "match"
    // attribute of xsl:key. This is the id we store, with the value we
    // get from the nodes we find here, in the index for this key.
    final LocalVariableGen parentNode =
        methodGen.addLocalVariable("parentNode",
                                   Util.getJCRefType("I"),
                                   null, null);

    // Get the 'parameter' from the stack and store it in a local var.
    parentNode.setStart(il.append(new ISTORE(parentNode.getIndex())));

    // Save current node and current iterator on the stack
    il.append(methodGen.loadCurrentNode());
    il.append(methodGen.loadIterator());

    // Overwrite current iterator with one that gives us only what we want
    _use.translate(classGen, methodGen);
    _use.startIterator(classGen, methodGen);
    il.append(methodGen.storeIterator());

    final BranchHandle nextNode = il.append(new GOTO(null));
    final InstructionHandle loop = il.append(NOP);

    // Prepare to call buildKeyIndex(String name, int node, String value);
    il.append(classGen.loadTranslet());
    il.append(new PUSH(cpg, _name.toString()));
    parentNode.setEnd(il.append(new ILOAD(parentNode.getIndex())));

    // Now get the node value and push it on the parameter stack
    il.append(methodGen.loadDOM());
    il.append(methodGen.loadCurrentNode());
    il.append(new INVOKEINTERFACE(getNodeValue, 2));

    // Finally do the call to add an entry in the index for this key.
    il.append(new INVOKEVIRTUAL(buildKeyIndex));

    il.append(classGen.loadTranslet());
    il.append(new PUSH(cpg, getName()));
    il.append(methodGen.loadDOM());
    il.append(new INVOKEVIRTUAL(keyDom));

    nextNode.setTarget(il.append(methodGen.loadIterator()));
    il.append(methodGen.nextNode());

    il.append(DUP);
    il.append(methodGen.storeCurrentNode());
    il.append(new IFGE(loop)); // Go on to next matching node....

    // Restore current node and current iterator from the stack
    il.append(methodGen.storeIterator());
    il.append(methodGen.storeCurrentNode());
}
 
Example 15
Source File: Copy.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();

    final LocalVariableGen name =
        methodGen.addLocalVariable2("name",
                                    Util.getJCRefType(STRING_SIG),
                                    null);
    final LocalVariableGen length =
        methodGen.addLocalVariable2("length",
                                    Util.getJCRefType("I"),
                                    null);

    // Get the name of the node to copy and save for later
    il.append(methodGen.loadDOM());
    il.append(methodGen.loadCurrentNode());
    il.append(methodGen.loadHandler());
    final int cpy = cpg.addInterfaceMethodref(DOM_INTF,
                                              "shallowCopy",
                                              "("
                                              + NODE_SIG
                                              + TRANSLET_OUTPUT_SIG
                                              + ")" + STRING_SIG);
    il.append(new INVOKEINTERFACE(cpy, 3));
    il.append(DUP);
    name.setStart(il.append(new ASTORE(name.getIndex())));
    final BranchHandle ifBlock1 = il.append(new IFNULL(null));

    // Get the length of the node name and save for later
    il.append(new ALOAD(name.getIndex()));
    final int lengthMethod = cpg.addMethodref(STRING_CLASS,"length","()I");
    il.append(new INVOKEVIRTUAL(lengthMethod));
    il.append(DUP);
    length.setStart(il.append(new ISTORE(length.getIndex())));

    // Ignore attribute sets if current node is ROOT. DOM.shallowCopy()
    // returns "" for ROOT, so skip attribute sets if length == 0
    final BranchHandle ifBlock4 = il.append(new IFEQ(null));

    // Copy in attribute sets if specified
    if (_useSets != null) {
        // If the parent of this element will result in an element being
        // output then we know that it is safe to copy out the attributes
        final SyntaxTreeNode parent = getParent();
        if ((parent instanceof LiteralElement) ||
            (parent instanceof LiteralElement)) {
            _useSets.translate(classGen, methodGen);
        }
        // If not we have to check to see if the copy will result in an
        // element being output.
        else {
            // check if element; if not skip to translate body
            il.append(new ILOAD(length.getIndex()));
            final BranchHandle ifBlock2 = il.append(new IFEQ(null));
            // length != 0 -> element -> do attribute sets
            _useSets.translate(classGen, methodGen);
            // not an element; root
            ifBlock2.setTarget(il.append(NOP));
        }
    }

    // Instantiate body of xsl:copy
    ifBlock4.setTarget(il.append(NOP));
    translateContents(classGen, methodGen);

    // Call the output handler's endElement() if we copied an element
    // (The DOM.shallowCopy() method calls startElement().)
    length.setEnd(il.append(new ILOAD(length.getIndex())));
    final BranchHandle ifBlock3 = il.append(new IFEQ(null));
    il.append(methodGen.loadHandler());
    name.setEnd(il.append(new ALOAD(name.getIndex())));
    il.append(methodGen.endElement());

    final InstructionHandle end = il.append(NOP);
    ifBlock1.setTarget(end);
    ifBlock3.setTarget(end);
    methodGen.removeLocalVariable(name);
    methodGen.removeLocalVariable(length);
}
 
Example 16
Source File: AncestorPattern.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
    InstructionHandle parent;
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();

    /*
     * The scope of this local var must be the entire method since
     * a another pattern may decide to jump back into the loop
     */
    final LocalVariableGen local =
        methodGen.addLocalVariable2("app", Util.getJCRefType(NODE_SIG),
                                    il.getEnd());

    final com.sun.org.apache.bcel.internal.generic.Instruction loadLocal =
        new ILOAD(local.getIndex());
    final com.sun.org.apache.bcel.internal.generic.Instruction storeLocal =
        new ISTORE(local.getIndex());

    if (_right instanceof StepPattern) {
        il.append(DUP);
        il.append(storeLocal);
        _right.translate(classGen, methodGen);
        il.append(methodGen.loadDOM());
        il.append(loadLocal);
    }
    else {
        _right.translate(classGen, methodGen);

        if (_right instanceof AncestorPattern) {
            il.append(methodGen.loadDOM());
            il.append(SWAP);
        }
    }

    if (_left != null) {
        final int getParent = cpg.addInterfaceMethodref(DOM_INTF,
                                                        GET_PARENT,
                                                        GET_PARENT_SIG);
        parent = il.append(new INVOKEINTERFACE(getParent, 2));

        il.append(DUP);
        il.append(storeLocal);
        _falseList.add(il.append(new IFLT(null)));
        il.append(loadLocal);

        _left.translate(classGen, methodGen);

        final SyntaxTreeNode p = getParent();
        if (p == null || p instanceof Instruction ||
            p instanceof TopLevelElement)
        {
            // do nothing
        }
        else {
            il.append(loadLocal);
        }

        final BranchHandle exit = il.append(new GOTO(null));
        _loop = il.append(methodGen.loadDOM());
        il.append(loadLocal);
        local.setEnd(_loop);
        il.append(new GOTO(parent));
        exit.setTarget(il.append(NOP));
        _left.backPatchFalseList(_loop);

        _trueList.append(_left._trueList);
    }
    else {
        il.append(POP2);
    }

    /*
     * If _right is an ancestor pattern, backpatch this pattern's false
     * list to the loop that searches for more ancestors.
     */
    if (_right instanceof AncestorPattern) {
        final AncestorPattern ancestor = (AncestorPattern) _right;
        _falseList.backPatch(ancestor.getLoopHandle());    // clears list
    }

    _trueList.append(_right._trueList);
    _falseList.append(_right._falseList);
}
 
Example 17
Source File: FilteredAbsoluteLocationPath.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();
    if (_path != null) {
        final int initDFI = cpg.addMethodref(DUP_FILTERED_ITERATOR,
                                            "<init>",
                                            "("
                                            + NODE_ITERATOR_SIG
                                            + ")V");

        // Backwards branches are prohibited if an uninitialized object is
        // on the stack by section 4.9.4 of the JVM Specification, 2nd Ed.
        // We don't know whether this code might contain backwards branches,
        // so we mustn't create the new object until after we've created
        // the suspect arguments to its constructor.  Instead we calculate
        // the values of the arguments to the constructor first, store them
        // in temporary variables, create the object and reload the
        // arguments from the temporaries to avoid the problem.

        // Compile relative path iterator(s)
        LocalVariableGen pathTemp =
           methodGen.addLocalVariable("filtered_absolute_location_path_tmp",
                                      Util.getJCRefType(NODE_ITERATOR_SIG),
                                      null, null);
        _path.translate(classGen, methodGen);
        pathTemp.setStart(il.append(new ASTORE(pathTemp.getIndex())));

        // Create new Dup Filter Iterator
        il.append(new NEW(cpg.addClass(DUP_FILTERED_ITERATOR)));
        il.append(DUP);
        pathTemp.setEnd(il.append(new ALOAD(pathTemp.getIndex())));

        // Initialize Dup Filter Iterator with iterator from the stack
        il.append(new INVOKESPECIAL(initDFI));
    }
    else {
        final int git = cpg.addInterfaceMethodref(DOM_INTF,
                                                  "getIterator",
                                                  "()"+NODE_ITERATOR_SIG);
        il.append(methodGen.loadDOM());
        il.append(new INVOKEINTERFACE(git, 1));
    }
}
 
Example 18
Source File: ParentPattern.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();
    final LocalVariableGen local =
        methodGen.addLocalVariable2("ppt",
                                    Util.getJCRefType(NODE_SIG),
                                    null);

    final com.sun.org.apache.bcel.internal.generic.Instruction loadLocal =
        new ILOAD(local.getIndex());
    final com.sun.org.apache.bcel.internal.generic.Instruction storeLocal =
        new ISTORE(local.getIndex());

    if (_right.isWildcard()) {
        il.append(methodGen.loadDOM());
        il.append(SWAP);
    }
    else if (_right instanceof StepPattern) {
        il.append(DUP);
        local.setStart(il.append(storeLocal));

        _right.translate(classGen, methodGen);

        il.append(methodGen.loadDOM());
        local.setEnd(il.append(loadLocal));
    }
    else {
        _right.translate(classGen, methodGen);

        if (_right instanceof AncestorPattern) {
            il.append(methodGen.loadDOM());
            il.append(SWAP);
        }
    }

    final int getParent = cpg.addInterfaceMethodref(DOM_INTF,
                                                    GET_PARENT,
                                                    GET_PARENT_SIG);
    il.append(new INVOKEINTERFACE(getParent, 2));

    final SyntaxTreeNode p = getParent();
    if (p == null || p instanceof Instruction ||
        p instanceof TopLevelElement)
    {
        _left.translate(classGen, methodGen);
    }
    else {
        il.append(DUP);
        InstructionHandle storeInst = il.append(storeLocal);

        if (local.getStart() == null) {
            local.setStart(storeInst);
        }

        _left.translate(classGen, methodGen);

        il.append(methodGen.loadDOM());
        local.setEnd(il.append(loadLocal));
    }

    methodGen.removeLocalVariable(local);

    /*
     * If _right is an ancestor pattern, backpatch _left false
     * list to the loop that searches for more ancestors.
     */
    if (_right instanceof AncestorPattern) {
        final AncestorPattern ancestor = (AncestorPattern) _right;
        _left.backPatchFalseList(ancestor.getLoopHandle());    // clears list
    }

    _trueList.append(_right._trueList.append(_left._trueList));
    _falseList.append(_right._falseList.append(_left._falseList));
}
 
Example 19
Source File: Copy.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();

    final LocalVariableGen name =
        methodGen.addLocalVariable2("name",
                                    Util.getJCRefType(STRING_SIG),
                                    null);
    final LocalVariableGen length =
        methodGen.addLocalVariable2("length",
                                    Util.getJCRefType("I"),
                                    null);

    // Get the name of the node to copy and save for later
    il.append(methodGen.loadDOM());
    il.append(methodGen.loadCurrentNode());
    il.append(methodGen.loadHandler());
    final int cpy = cpg.addInterfaceMethodref(DOM_INTF,
                                              "shallowCopy",
                                              "("
                                              + NODE_SIG
                                              + TRANSLET_OUTPUT_SIG
                                              + ")" + STRING_SIG);
    il.append(new INVOKEINTERFACE(cpy, 3));
    il.append(DUP);
    name.setStart(il.append(new ASTORE(name.getIndex())));
    final BranchHandle ifBlock1 = il.append(new IFNULL(null));

    // Get the length of the node name and save for later
    il.append(new ALOAD(name.getIndex()));
    final int lengthMethod = cpg.addMethodref(STRING_CLASS,"length","()I");
    il.append(new INVOKEVIRTUAL(lengthMethod));
    il.append(DUP);
    length.setStart(il.append(new ISTORE(length.getIndex())));

    // Ignore attribute sets if current node is ROOT. DOM.shallowCopy()
    // returns "" for ROOT, so skip attribute sets if length == 0
    final BranchHandle ifBlock4 = il.append(new IFEQ(null));

    // Copy in attribute sets if specified
    if (_useSets != null) {
        // If the parent of this element will result in an element being
        // output then we know that it is safe to copy out the attributes
        final SyntaxTreeNode parent = getParent();
        if ((parent instanceof LiteralElement) ||
            (parent instanceof LiteralElement)) {
            _useSets.translate(classGen, methodGen);
        }
        // If not we have to check to see if the copy will result in an
        // element being output.
        else {
            // check if element; if not skip to translate body
            il.append(new ILOAD(length.getIndex()));
            final BranchHandle ifBlock2 = il.append(new IFEQ(null));
            // length != 0 -> element -> do attribute sets
            _useSets.translate(classGen, methodGen);
            // not an element; root
            ifBlock2.setTarget(il.append(NOP));
        }
    }

    // Instantiate body of xsl:copy
    ifBlock4.setTarget(il.append(NOP));
    translateContents(classGen, methodGen);

    // Call the output handler's endElement() if we copied an element
    // (The DOM.shallowCopy() method calls startElement().)
    length.setEnd(il.append(new ILOAD(length.getIndex())));
    final BranchHandle ifBlock3 = il.append(new IFEQ(null));
    il.append(methodGen.loadHandler());
    name.setEnd(il.append(new ALOAD(name.getIndex())));
    il.append(methodGen.endElement());

    final InstructionHandle end = il.append(NOP);
    ifBlock1.setTarget(end);
    ifBlock3.setTarget(end);
    methodGen.removeLocalVariable(name);
    methodGen.removeLocalVariable(length);
}
 
Example 20
Source File: ResultTreeType.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Expects a result tree on the stack and pushes a node-set (iterator).
 * Note that the produced iterator is an iterator for the DOM that
 * contains the result tree, and not the DOM that is currently in use.
 * This conversion here will therefore not directly work with elements
 * such as <xsl:apply-templates> and <xsl:for-each> without the DOM
 * parameter/variable being updates as well.
 *
 * @param classGen A BCEL class generator
 * @param methodGen A BCEL method generator
 * @param type An instance of NodeSetType (any)
 * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
 */
public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
                        NodeSetType type) {
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();

    // Put an extra copy of the result tree (DOM) on the stack
    il.append(DUP);

    // DOM adapters containing a result tree are not initialised with
    // translet-type to DOM-type mapping. This must be done now for
    // XPath expressions and patterns to work for the iterator we create.
    il.append(classGen.loadTranslet()); // get names array
    il.append(new GETFIELD(cpg.addFieldref(TRANSLET_CLASS,
                                           NAMES_INDEX,
                                           NAMES_INDEX_SIG)));
    il.append(classGen.loadTranslet()); // get uris array
    il.append(new GETFIELD(cpg.addFieldref(TRANSLET_CLASS,
                                           URIS_INDEX,
                                           URIS_INDEX_SIG)));
    il.append(classGen.loadTranslet()); // get types array
    il.append(new GETFIELD(cpg.addFieldref(TRANSLET_CLASS,
                                           TYPES_INDEX,
                                           TYPES_INDEX_SIG)));
    il.append(classGen.loadTranslet()); // get namespaces array
    il.append(new GETFIELD(cpg.addFieldref(TRANSLET_CLASS,
                                           NAMESPACE_INDEX,
                                           NAMESPACE_INDEX_SIG)));
    // Pass the type mappings to the DOM adapter
    final int mapping = cpg.addInterfaceMethodref(DOM_INTF,
                                                  "setupMapping",
                                                  "(["+STRING_SIG+
                                                  "["+STRING_SIG+
                                                  "[I" +
                                                  "["+STRING_SIG+")V");
    il.append(new INVOKEINTERFACE(mapping, 5));
    il.append(DUP);

    // Create an iterator for the root node of the DOM adapter
    final int iter = cpg.addInterfaceMethodref(DOM_INTF,
                                               "getIterator",
                                               "()"+NODE_ITERATOR_SIG);
    il.append(new INVOKEINTERFACE(iter, 1));
}