예제 #1
0
파일: NameBase.java 프로젝트: srnsw/xena
  /**
   * Translate the code required for getting the node for which the QName, local-name or namespace
   * URI should be extracted.
   */
  public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();

    il.append(methodGen.loadDOM());

    // Function was called with no parameters
    if (argumentCount() == 0) {
      il.append(methodGen.loadContextNode());
    }
    // Function was called with node parameter
    else if (_paramType == Type.Node) {
      _param.translate(classGen, methodGen);
    } else if (_paramType == Type.Reference) {
      _param.translate(classGen, methodGen);
      il.append(
          new INVOKESTATIC(
              cpg.addMethodref(
                  BASIS_LIBRARY_CLASS,
                  "referenceToNodeSet",
                  "(" + OBJECT_SIG + ")" + NODE_ITERATOR_SIG)));
      il.append(methodGen.nextNode());
    }
    // Function was called with node-set parameter
    else {
      _param.translate(classGen, methodGen);
      _param.startIterator(classGen, methodGen);
      il.append(methodGen.nextNode());
    }
  }
 public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
   final ConstantPoolGen cpg = classGen.getConstantPool();
   final InstructionList il = methodGen.getInstructionList();
   // Push the this pointer on the stack...
   il.append(methodGen.loadDOM());
   // ...then the entity name...
   _entity.translate(classGen, methodGen);
   // ...to get the URI from the DOM object.
   il.append(
       new INVOKEINTERFACE(
           cpg.addInterfaceMethodref(
               DOM_INTF, GET_UNPARSED_ENTITY_URI, GET_UNPARSED_ENTITY_URI_SIG),
           2));
 }
  /**
   * Compiles code that instantiates a SortingIterator object. This object's constructor needs
   * referencdes to the current iterator and a node sort record producing objects as its parameters.
   */
  public static void translateSortIterator(
      ClassGenerator classGen, MethodGenerator methodGen, Expression nodeSet, Vector sortObjects) {
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();

    // SortingIterator.SortingIterator(NodeIterator,NodeSortRecordFactory);
    final int init =
        cpg.addMethodref(
            SORT_ITERATOR, "<init>", "(" + NODE_ITERATOR_SIG + NODE_SORT_FACTORY_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.

    LocalVariableGen nodesTemp =
        methodGen.addLocalVariable("sort_tmp1", Util.getJCRefType(NODE_ITERATOR_SIG), null, null);

    LocalVariableGen sortRecordFactoryTemp =
        methodGen.addLocalVariable(
            "sort_tmp2", Util.getJCRefType(NODE_SORT_FACTORY_SIG), null, null);

    // Get the current node iterator
    if (nodeSet == null) { // apply-templates default
      final int children =
          cpg.addInterfaceMethodref(DOM_INTF, "getAxisIterator", "(I)" + NODE_ITERATOR_SIG);
      il.append(methodGen.loadDOM());
      il.append(new PUSH(cpg, Axis.CHILD));
      il.append(new INVOKEINTERFACE(children, 2));
    } else {
      nodeSet.translate(classGen, methodGen);
    }

    nodesTemp.setStart(il.append(new ASTORE(nodesTemp.getIndex())));

    // Compile the code for the NodeSortRecord producing class and pass
    // that as the last argument to the SortingIterator constructor.
    compileSortRecordFactory(sortObjects, classGen, methodGen);
    sortRecordFactoryTemp.setStart(il.append(new ASTORE(sortRecordFactoryTemp.getIndex())));

    il.append(new NEW(cpg.addClass(SORT_ITERATOR)));
    il.append(DUP);
    nodesTemp.setEnd(il.append(new ALOAD(nodesTemp.getIndex())));
    sortRecordFactoryTemp.setEnd(il.append(new ALOAD(sortRecordFactoryTemp.getIndex())));
    il.append(new INVOKESPECIAL(init));
  }
예제 #4
0
파일: LangCall.java 프로젝트: enenuki/phd
 /* 43:   */
 /* 44:   */ public void translate(ClassGenerator classGen, MethodGenerator methodGen)
       /* 45:   */ {
   /* 46:77 */ ConstantPoolGen cpg = classGen.getConstantPool();
   /* 47:78 */ InstructionList il = methodGen.getInstructionList();
   /* 48:   */
   /* 49:80 */ int tst =
       cpg.addMethodref(
           "org.apache.xalan.xsltc.runtime.BasisLibrary",
           "testLanguage",
           "(Ljava/lang/String;Lorg/apache/xalan/xsltc/DOM;I)Z");
   /* 50:   */
   /* 51:   */
   /* 52:83 */ this._lang.translate(classGen, methodGen);
   /* 53:84 */ il.append(methodGen.loadDOM());
   /* 54:85 */ if ((classGen instanceof FilterGenerator)) {
     /* 55:86 */ il.append(new ILOAD(1));
     /* 56:   */ } else {
     /* 57:88 */ il.append(methodGen.loadContextNode());
     /* 58:   */ }
   /* 59:89 */ il.append(new INVOKESTATIC(tst));
   /* 60:   */ }
  /**
   * Compiles code that instantiates a NodeSortRecordFactory object which will produce
   * NodeSortRecord objects of a specific type.
   */
  public static void compileSortRecordFactory(
      Vector sortObjects, ClassGenerator classGen, MethodGenerator methodGen) {
    String sortRecordClass = compileSortRecord(sortObjects, classGen, methodGen);

    boolean needsSortRecordFactory = false;
    final int nsorts = sortObjects.size();
    for (int i = 0; i < nsorts; i++) {
      final Sort sort = (Sort) sortObjects.elementAt(i);
      needsSortRecordFactory |= sort._needsSortRecordFactory;
    }

    String sortRecordFactoryClass = NODE_SORT_FACTORY;
    if (needsSortRecordFactory) {
      sortRecordFactoryClass =
          compileSortRecordFactory(sortObjects, classGen, methodGen, sortRecordClass);
    }

    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.

    // Compile code that initializes the static _sortOrder
    LocalVariableGen sortOrderTemp =
        methodGen.addLocalVariable(
            "sort_order_tmp", Util.getJCRefType("[" + STRING_SIG), null, null);
    il.append(new PUSH(cpg, nsorts));
    il.append(new ANEWARRAY(cpg.addClass(STRING)));
    for (int level = 0; level < nsorts; level++) {
      final Sort sort = (Sort) sortObjects.elementAt(level);
      il.append(DUP);
      il.append(new PUSH(cpg, level));
      sort.translateSortOrder(classGen, methodGen);
      il.append(AASTORE);
    }
    sortOrderTemp.setStart(il.append(new ASTORE(sortOrderTemp.getIndex())));

    LocalVariableGen sortTypeTemp =
        methodGen.addLocalVariable(
            "sort_type_tmp", Util.getJCRefType("[" + STRING_SIG), null, null);
    il.append(new PUSH(cpg, nsorts));
    il.append(new ANEWARRAY(cpg.addClass(STRING)));
    for (int level = 0; level < nsorts; level++) {
      final Sort sort = (Sort) sortObjects.elementAt(level);
      il.append(DUP);
      il.append(new PUSH(cpg, level));
      sort.translateSortType(classGen, methodGen);
      il.append(AASTORE);
    }
    sortTypeTemp.setStart(il.append(new ASTORE(sortTypeTemp.getIndex())));

    LocalVariableGen sortLangTemp =
        methodGen.addLocalVariable(
            "sort_lang_tmp", Util.getJCRefType("[" + STRING_SIG), null, null);
    il.append(new PUSH(cpg, nsorts));
    il.append(new ANEWARRAY(cpg.addClass(STRING)));
    for (int level = 0; level < nsorts; level++) {
      final Sort sort = (Sort) sortObjects.elementAt(level);
      il.append(DUP);
      il.append(new PUSH(cpg, level));
      sort.translateLang(classGen, methodGen);
      il.append(AASTORE);
    }
    sortLangTemp.setStart(il.append(new ASTORE(sortLangTemp.getIndex())));

    LocalVariableGen sortCaseOrderTemp =
        methodGen.addLocalVariable(
            "sort_case_order_tmp", Util.getJCRefType("[" + STRING_SIG), null, null);
    il.append(new PUSH(cpg, nsorts));
    il.append(new ANEWARRAY(cpg.addClass(STRING)));
    for (int level = 0; level < nsorts; level++) {
      final Sort sort = (Sort) sortObjects.elementAt(level);
      il.append(DUP);
      il.append(new PUSH(cpg, level));
      sort.translateCaseOrder(classGen, methodGen);
      il.append(AASTORE);
    }
    sortCaseOrderTemp.setStart(il.append(new ASTORE(sortCaseOrderTemp.getIndex())));

    il.append(new NEW(cpg.addClass(sortRecordFactoryClass)));
    il.append(DUP);
    il.append(methodGen.loadDOM());
    il.append(new PUSH(cpg, sortRecordClass));
    il.append(classGen.loadTranslet());

    sortOrderTemp.setEnd(il.append(new ALOAD(sortOrderTemp.getIndex())));
    sortTypeTemp.setEnd(il.append(new ALOAD(sortTypeTemp.getIndex())));
    sortLangTemp.setEnd(il.append(new ALOAD(sortLangTemp.getIndex())));
    sortCaseOrderTemp.setEnd(il.append(new ALOAD(sortCaseOrderTemp.getIndex())));

    il.append(
        new INVOKESPECIAL(
            cpg.addMethodref(
                sortRecordFactoryClass,
                "<init>",
                "("
                    + DOM_INTF_SIG
                    + STRING_SIG
                    + TRANSLET_INTF_SIG
                    + "["
                    + STRING_SIG
                    + "["
                    + STRING_SIG
                    + "["
                    + STRING_SIG
                    + "["
                    + STRING_SIG
                    + ")V")));

    // Initialize closure variables in sortRecordFactory
    final ArrayList dups = new ArrayList();

    for (int j = 0; j < nsorts; j++) {
      final Sort sort = (Sort) sortObjects.get(j);
      final int length = (sort._closureVars == null) ? 0 : sort._closureVars.size();

      for (int i = 0; i < length; i++) {
        VariableRefBase varRef = (VariableRefBase) sort._closureVars.get(i);

        // Discard duplicate variable references
        if (dups.contains(varRef)) continue;

        final VariableBase var = varRef.getVariable();

        // Store variable in new closure
        il.append(DUP);
        il.append(var.loadInstruction());
        il.append(
            new PUTFIELD(
                cpg.addFieldref(
                    sortRecordFactoryClass, var.getEscapedName(), var.getType().toSignature())));
        dups.add(varRef);
      }
    }
  }
  /**
   * Translate a function call. The compiled code will leave the function's return value on the
   * JVM's stack.
   */
  public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
    final int n = argumentCount();
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();
    final boolean isSecureProcessing = classGen.getParser().getXSLTC().isSecureProcessing();
    int index;

    // Translate calls to methods in the BasisLibrary
    if (isStandard() || isExtension()) {
      for (int i = 0; i < n; i++) {
        final Expression exp = argument(i);
        exp.translate(classGen, methodGen);
        exp.startIterator(classGen, methodGen);
      }

      // append "F" to the function's name
      final String name = _fname.toString().replace('-', '_') + "F";
      String args = Constants.EMPTYSTRING;

      // Special precautions for some method calls
      if (name.equals("sumF")) {
        args = DOM_INTF_SIG;
        il.append(methodGen.loadDOM());
      } else if (name.equals("normalize_spaceF")) {
        if (_chosenMethodType.toSignature(args).equals("()Ljava/lang/String;")) {
          args = "I" + DOM_INTF_SIG;
          il.append(methodGen.loadContextNode());
          il.append(methodGen.loadDOM());
        }
      }

      // Invoke the method in the basis library
      index = cpg.addMethodref(BASIS_LIBRARY_CLASS, name, _chosenMethodType.toSignature(args));
      il.append(new INVOKESTATIC(index));
    }
    // Add call to BasisLibrary.unresolved_externalF() to generate
    // run-time error message for unsupported external functions
    else if (unresolvedExternal) {
      index =
          cpg.addMethodref(BASIS_LIBRARY_CLASS, "unresolved_externalF", "(Ljava/lang/String;)V");
      il.append(new PUSH(cpg, _fname.toString()));
      il.append(new INVOKESTATIC(index));
    } else if (_isExtConstructor) {
      if (isSecureProcessing) translateUnallowedExtension(cpg, il);

      final String clazz = _chosenConstructor.getDeclaringClass().getName();
      Class[] paramTypes = _chosenConstructor.getParameterTypes();
      LocalVariableGen[] paramTemp = new LocalVariableGen[n];

      // 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.

      for (int i = 0; i < n; i++) {
        final Expression exp = argument(i);
        Type expType = exp.getType();
        exp.translate(classGen, methodGen);
        // Convert the argument to its Java type
        exp.startIterator(classGen, methodGen);
        expType.translateTo(classGen, methodGen, paramTypes[i]);
        paramTemp[i] =
            methodGen.addLocalVariable(
                "function_call_tmp" + i, expType.toJCType(), il.getEnd(), null);
        il.append(expType.STORE(paramTemp[i].getIndex()));
      }

      il.append(new NEW(cpg.addClass(_className)));
      il.append(InstructionConstants.DUP);

      for (int i = 0; i < n; i++) {
        final Expression arg = argument(i);
        il.append(arg.getType().LOAD(paramTemp[i].getIndex()));
      }

      final StringBuffer buffer = new StringBuffer();
      buffer.append('(');
      for (int i = 0; i < paramTypes.length; i++) {
        buffer.append(getSignature(paramTypes[i]));
      }
      buffer.append(')');
      buffer.append("V");

      index = cpg.addMethodref(clazz, "<init>", buffer.toString());
      il.append(new INVOKESPECIAL(index));

      // Convert the return type back to our internal type
      (Type.Object).translateFrom(classGen, methodGen, _chosenConstructor.getDeclaringClass());

    }
    // Invoke function calls that are handled in separate classes
    else {
      if (isSecureProcessing) translateUnallowedExtension(cpg, il);

      final String clazz = _chosenMethod.getDeclaringClass().getName();
      Class[] paramTypes = _chosenMethod.getParameterTypes();

      // Push "this" if it is an instance method
      if (_thisArgument != null) {
        _thisArgument.translate(classGen, methodGen);
      }

      for (int i = 0; i < n; i++) {
        final Expression exp = argument(i);
        exp.translate(classGen, methodGen);
        // Convert the argument to its Java type
        exp.startIterator(classGen, methodGen);
        exp.getType().translateTo(classGen, methodGen, paramTypes[i]);
      }

      final StringBuffer buffer = new StringBuffer();
      buffer.append('(');
      for (int i = 0; i < paramTypes.length; i++) {
        buffer.append(getSignature(paramTypes[i]));
      }
      buffer.append(')');
      buffer.append(getSignature(_chosenMethod.getReturnType()));

      if (_thisArgument != null && _clazz.isInterface()) {
        index = cpg.addInterfaceMethodref(clazz, _fname.getLocalPart(), buffer.toString());
        il.append(new INVOKEINTERFACE(index, n + 1));
      } else {
        index = cpg.addMethodref(clazz, _fname.getLocalPart(), buffer.toString());
        il.append(
            _thisArgument != null
                ? (InvokeInstruction) new INVOKEVIRTUAL(index)
                : (InvokeInstruction) new INVOKESTATIC(index));
      }

      // Convert the return type back to our internal type
      _type.translateFrom(classGen, methodGen, _chosenMethod.getReturnType());
    }
  }