Java Code Examples for com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator#addLocalVariable2()

The following examples show how to use com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator#addLocalVariable2() . 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: Variable.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This method is part of a little trick that is needed to use local
 * variables inside nested for-each loops. See the initializeVariables()
 * method in the ForEach class for an explanation
 */
public void initialize(ClassGenerator classGen, MethodGenerator methodGen) {
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();

    // This is only done for local variables that are actually used
    if (isLocal() && !_refs.isEmpty()) {
        // Create a variable slot if none is allocated
        if (_local == null) {
            _local = methodGen.addLocalVariable2(getEscapedName(),
                                                 _type.toJCType(),
                                                 null);
        }
        // Push the default value on the JVM's stack
        if ((_type instanceof IntType) ||
            (_type instanceof NodeType) ||
            (_type instanceof BooleanType))
            il.append(new ICONST(0)); // 0 for node-id, integer and boolean
        else if (_type instanceof RealType)
            il.append(new DCONST(0)); // 0.0 for floating point numbers
        else
            il.append(new ACONST_NULL()); // and 'null' for anything else

        // Mark the store as the start of the live range of the variable
        _local.setStart(il.append(_type.STORE(_local.getIndex())));

    }
}
 
Example 2
Source File: VariableBase.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * Map this variable to a register
 */
public void mapRegister(MethodGenerator methodGen) {
    if (_local == null) {
        final InstructionList il = methodGen.getInstructionList();
        final String name = getEscapedName(); // TODO: namespace ?
        final com.sun.org.apache.bcel.internal.generic.Type varType = _type.toJCType();
        _local = methodGen.addLocalVariable2(name, varType, il.getEnd());
    }
}
 
Example 3
Source File: VariableBase.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Map this variable to a register
 */
public void mapRegister(MethodGenerator methodGen) {
    if (_local == null) {
        final InstructionList il = methodGen.getInstructionList();
        final String name = getEscapedName(); // TODO: namespace ?
        final com.sun.org.apache.bcel.internal.generic.Type varType = _type.toJCType();
        _local = methodGen.addLocalVariable2(name, varType, il.getEnd());
    }
}
 
Example 4
Source File: VariableBase.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Map this variable to a register
 */
public void mapRegister(MethodGenerator methodGen) {
    if (_local == null) {
        final InstructionList il = methodGen.getInstructionList();
        final String name = getEscapedName(); // TODO: namespace ?
        final com.sun.org.apache.bcel.internal.generic.Type varType = _type.toJCType();
        _local = methodGen.addLocalVariable2(name, varType, il.getEnd());
    }
}
 
Example 5
Source File: Variable.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This method is part of a little trick that is needed to use local
 * variables inside nested for-each loops. See the initializeVariables()
 * method in the ForEach class for an explanation
 */
public void initialize(ClassGenerator classGen, MethodGenerator methodGen) {
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();

    // This is only done for local variables that are actually used
    if (isLocal() && !_refs.isEmpty()) {
        // Create a variable slot if none is allocated
        if (_local == null) {
            _local = methodGen.addLocalVariable2(getEscapedName(),
                                                 _type.toJCType(),
                                                 null);
        }
        // Push the default value on the JVM's stack
        if ((_type instanceof IntType) ||
            (_type instanceof NodeType) ||
            (_type instanceof BooleanType))
            il.append(new ICONST(0)); // 0 for node-id, integer and boolean
        else if (_type instanceof RealType)
            il.append(new DCONST(0)); // 0.0 for floating point numbers
        else
            il.append(new ACONST_NULL()); // and 'null' for anything else

        // Mark the store as the start of the live range of the variable
        _local.setStart(il.append(_type.STORE(_local.getIndex())));

    }
}
 
Example 6
Source File: Copy.java    From openjdk-8-source 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 7
Source File: ParentPattern.java    From JDKSourceCode1.8 with MIT License 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 8
Source File: Param.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();

    if (_ignore) return;
    _ignore = true;

    /*
     * To fix bug 24518 related to setting parameters of the form
     * {namespaceuri}localName which will get mapped to an instance
     * variable in the class.
     */
    final String name = BasisLibrary.mapQNameToJavaName(_name.toString());
    final String signature = _type.toSignature();
    final String className = _type.getClassName();

    if (isLocal()) {
        /*
          * If simple named template then generate a conditional init of the
          * param using its default value:
          *       if (param == null) param = <default-value>
          */
        if (_isInSimpleNamedTemplate) {
            il.append(loadInstruction());
            BranchHandle ifBlock = il.append(new IFNONNULL(null));
            translateValue(classGen, methodGen);
            il.append(storeInstruction());
            ifBlock.setTarget(il.append(NOP));
            return;
        }

        il.append(classGen.loadTranslet());
        il.append(new PUSH(cpg, name));
        translateValue(classGen, methodGen);
        il.append(new PUSH(cpg, true));

        // Call addParameter() from this class
        il.append(new INVOKEVIRTUAL(cpg.addMethodref(TRANSLET_CLASS,
                                                     ADD_PARAMETER,
                                                     ADD_PARAMETER_SIG)));
        if (className != EMPTYSTRING) {
            il.append(new CHECKCAST(cpg.addClass(className)));
        }

        _type.translateUnBox(classGen, methodGen);

        if (_refs.isEmpty()) { // nobody uses the value
            il.append(_type.POP());
            _local = null;
        }
        else {              // normal case
            _local = methodGen.addLocalVariable2(name,
                                                 _type.toJCType(),
                                                 il.getEnd());
            // Cache the result of addParameter() in a local variable
            il.append(_type.STORE(_local.getIndex()));
        }
    }
    else {
        if (classGen.containsField(name) == null) {
            classGen.addField(new Field(ACC_PUBLIC, cpg.addUtf8(name),
                                        cpg.addUtf8(signature),
                                        null, cpg.getConstantPool()));
            il.append(classGen.loadTranslet());
            il.append(DUP);
            il.append(new PUSH(cpg, name));
            translateValue(classGen, methodGen);
            il.append(new PUSH(cpg, true));

            // Call addParameter() from this class
            il.append(new INVOKEVIRTUAL(cpg.addMethodref(TRANSLET_CLASS,
                                                 ADD_PARAMETER,
                                                 ADD_PARAMETER_SIG)));

            _type.translateUnBox(classGen, methodGen);

            // Cache the result of addParameter() in a field
            if (className != EMPTYSTRING) {
                il.append(new CHECKCAST(cpg.addClass(className)));
            }
            il.append(new PUTFIELD(cpg.addFieldref(classGen.getClassName(),
                                                   name, signature)));
        }
    }
}
 
Example 9
Source File: Param.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();

    if (_ignore) return;
    _ignore = true;

    /*
     * To fix bug 24518 related to setting parameters of the form
     * {namespaceuri}localName which will get mapped to an instance
     * variable in the class.
     */
    final String name = BasisLibrary.mapQNameToJavaName(_name.toString());
    final String signature = _type.toSignature();
    final String className = _type.getClassName();

    if (isLocal()) {
        /*
          * If simple named template then generate a conditional init of the
          * param using its default value:
          *       if (param == null) param = <default-value>
          */
        if (_isInSimpleNamedTemplate) {
            il.append(loadInstruction());
            BranchHandle ifBlock = il.append(new IFNONNULL(null));
            translateValue(classGen, methodGen);
            il.append(storeInstruction());
            ifBlock.setTarget(il.append(NOP));
            return;
        }

        il.append(classGen.loadTranslet());
        il.append(new PUSH(cpg, name));
        translateValue(classGen, methodGen);
        il.append(new PUSH(cpg, true));

        // Call addParameter() from this class
        il.append(new INVOKEVIRTUAL(cpg.addMethodref(TRANSLET_CLASS,
                                                     ADD_PARAMETER,
                                                     ADD_PARAMETER_SIG)));
        if (className != EMPTYSTRING) {
            il.append(new CHECKCAST(cpg.addClass(className)));
        }

        _type.translateUnBox(classGen, methodGen);

        if (_refs.isEmpty()) { // nobody uses the value
            il.append(_type.POP());
            _local = null;
        }
        else {              // normal case
            _local = methodGen.addLocalVariable2(name,
                                                 _type.toJCType(),
                                                 il.getEnd());
            // Cache the result of addParameter() in a local variable
            il.append(_type.STORE(_local.getIndex()));
        }
    }
    else {
        if (classGen.containsField(name) == null) {
            classGen.addField(new Field(ACC_PUBLIC, cpg.addUtf8(name),
                                        cpg.addUtf8(signature),
                                        null, cpg.getConstantPool()));
            il.append(classGen.loadTranslet());
            il.append(DUP);
            il.append(new PUSH(cpg, name));
            translateValue(classGen, methodGen);
            il.append(new PUSH(cpg, true));

            // Call addParameter() from this class
            il.append(new INVOKEVIRTUAL(cpg.addMethodref(TRANSLET_CLASS,
                                                 ADD_PARAMETER,
                                                 ADD_PARAMETER_SIG)));

            _type.translateUnBox(classGen, methodGen);

            // Cache the result of addParameter() in a field
            if (className != EMPTYSTRING) {
                il.append(new CHECKCAST(cpg.addClass(className)));
            }
            il.append(new PUTFIELD(cpg.addFieldref(classGen.getClassName(),
                                                   name, signature)));
        }
    }
}
 
Example 10
Source File: ParentPattern.java    From Bytecoder 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();
    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 11
Source File: XslElement.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * At runtime the compilation of xsl:element results in code that: (i)
 * evaluates the avt for the name, (ii) checks for a prefix in the name
 * (iii) generates a new prefix and create a new qname when necessary
 * (iv) calls startElement() on the handler (v) looks up a uri in the XML
 * when the prefix is not known at compile time (vi) calls namespace()
 * on the handler (vii) evaluates the contents (viii) calls endElement().
 */
public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();

    // Optimize translation if element name is a literal
    if (_isLiteralName) {
        translateLiteral(classGen, methodGen);
        return;
    }

    if (!_ignore) {

        // if the qname is an AVT, then the qname has to be checked at runtime if it is a valid qname
        LocalVariableGen nameValue =
                methodGen.addLocalVariable2("nameValue",
                                            Util.getJCRefType(STRING_SIG),
                                            null);

        // store the name into a variable first so _name.translate only needs to be called once
        _name.translate(classGen, methodGen);
        nameValue.setStart(il.append(new ASTORE(nameValue.getIndex())));
        il.append(new ALOAD(nameValue.getIndex()));

        // call checkQName if the name is an AVT
        final int check = cpg.addMethodref(BASIS_LIBRARY_CLASS, "checkQName",
                        "("
                        +STRING_SIG
                        +")V");
        il.append(new INVOKESTATIC(check));

        // Push handler for call to endElement()
        il.append(methodGen.loadHandler());

        // load name value again
        nameValue.setEnd(il.append(new ALOAD(nameValue.getIndex())));

        if (_namespace != null) {
            _namespace.translate(classGen, methodGen);
        }
        else {
            il.append(ACONST_NULL);
        }

        // Push additional arguments
        il.append(methodGen.loadHandler());
        il.append(methodGen.loadDOM());
        il.append(methodGen.loadCurrentNode());

        // Invoke BasisLibrary.startXslElemCheckQName()
        il.append(new INVOKESTATIC(
        cpg.addMethodref(BASIS_LIBRARY_CLASS, "startXslElement",
                "(" + STRING_SIG
                + STRING_SIG
                + TRANSLET_OUTPUT_SIG
                + DOM_INTF_SIG + "I)" + STRING_SIG)));


    }

    translateContents(classGen, methodGen);

    if (!_ignore) {
        il.append(methodGen.endElement());
    }
}
 
Example 12
Source File: ParentPattern.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 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 13
Source File: Copy.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();

    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 14
Source File: ParentPattern.java    From jdk8u60 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 15
Source File: ProcessingInstruction.java    From openjdk-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();

    if (!_isLiteral) {
        // if the ncname is an AVT, then the ncname has to be checked at runtime if it is a valid ncname
        LocalVariableGen nameValue =
                methodGen.addLocalVariable2("nameValue",
        Util.getJCRefType(STRING_SIG),
                                            null);

        // store the name into a variable first so _name.translate only needs to be called once
        _name.translate(classGen, methodGen);
        nameValue.setStart(il.append(new ASTORE(nameValue.getIndex())));
        il.append(new ALOAD(nameValue.getIndex()));

        // call checkNCName if the name is an AVT
        final int check = cpg.addMethodref(BASIS_LIBRARY_CLASS, "checkNCName",
                            "("
                            +STRING_SIG
                            +")V");
                            il.append(new INVOKESTATIC(check));

        // Save the current handler base on the stack
        il.append(methodGen.loadHandler());
        il.append(DUP);     // first arg to "attributes" call

        // load name value again
        nameValue.setEnd(il.append(new ALOAD(nameValue.getIndex())));
    } else {
        // Save the current handler base on the stack
        il.append(methodGen.loadHandler());
        il.append(DUP);     // first arg to "attributes" call

        // Push attribute name
        _name.translate(classGen, methodGen);// 2nd arg

    }

    il.append(classGen.loadTranslet());
    il.append(new GETFIELD(cpg.addFieldref(TRANSLET_CLASS,
                                           "stringValueHandler",
                                           STRING_VALUE_HANDLER_SIG)));
    il.append(DUP);
    il.append(methodGen.storeHandler());

    // translate contents with substituted handler
    translateContents(classGen, methodGen);

    // get String out of the handler
    il.append(new INVOKEVIRTUAL(cpg.addMethodref(STRING_VALUE_HANDLER,
                                                 "getValueOfPI",
                                                 "()" + STRING_SIG)));
    // call "processingInstruction"
    final int processingInstruction =
        cpg.addInterfaceMethodref(TRANSLET_OUTPUT_INTERFACE,
                                  "processingInstruction",
                                  "(" + STRING_SIG + STRING_SIG + ")V");
    il.append(new INVOKEINTERFACE(processingInstruction, 3));
    // Restore old handler base from stack
    il.append(methodGen.storeHandler());
}
 
Example 16
Source File: AncestorPattern.java    From openjdk-jdk9 with GNU General Public License v2.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 17
Source File: AncestorPattern.java    From openjdk-jdk8u-backup with GNU General Public License v2.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 18
Source File: AncestorPattern.java    From openjdk-8 with GNU General Public License v2.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 19
Source File: XslElement.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * At runtime the compilation of xsl:element results in code that: (i)
 * evaluates the avt for the name, (ii) checks for a prefix in the name
 * (iii) generates a new prefix and create a new qname when necessary
 * (iv) calls startElement() on the handler (v) looks up a uri in the XML
 * when the prefix is not known at compile time (vi) calls namespace()
 * on the handler (vii) evaluates the contents (viii) calls endElement().
 */
public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
    LocalVariableGen local = null;
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();

    // Optimize translation if element name is a literal
    if (_isLiteralName) {
        translateLiteral(classGen, methodGen);
        return;
    }

    if (!_ignore) {

        // if the qname is an AVT, then the qname has to be checked at runtime if it is a valid qname
        LocalVariableGen nameValue =
                methodGen.addLocalVariable2("nameValue",
                                            Util.getJCRefType(STRING_SIG),
                                            null);

        // store the name into a variable first so _name.translate only needs to be called once
        _name.translate(classGen, methodGen);
        nameValue.setStart(il.append(new ASTORE(nameValue.getIndex())));
        il.append(new ALOAD(nameValue.getIndex()));

        // call checkQName if the name is an AVT
        final int check = cpg.addMethodref(BASIS_LIBRARY_CLASS, "checkQName",
                        "("
                        +STRING_SIG
                        +")V");
        il.append(new INVOKESTATIC(check));

        // Push handler for call to endElement()
        il.append(methodGen.loadHandler());

        // load name value again
        nameValue.setEnd(il.append(new ALOAD(nameValue.getIndex())));

        if (_namespace != null) {
            _namespace.translate(classGen, methodGen);
        }
        else {
            il.append(ACONST_NULL);
        }

        // Push additional arguments
        il.append(methodGen.loadHandler());
        il.append(methodGen.loadDOM());
        il.append(methodGen.loadCurrentNode());

        // Invoke BasisLibrary.startXslElemCheckQName()
        il.append(new INVOKESTATIC(
        cpg.addMethodref(BASIS_LIBRARY_CLASS, "startXslElement",
                "(" + STRING_SIG
                + STRING_SIG
                + TRANSLET_OUTPUT_SIG
                + DOM_INTF_SIG + "I)" + STRING_SIG)));


    }

    translateContents(classGen, methodGen);

    if (!_ignore) {
        il.append(methodGen.endElement());
    }
}
 
Example 20
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));
}