Package org.apache.bcel.generic

Examples of org.apache.bcel.generic.InstructionHandle


        }

        //
        // invoke handler's method
        //
        InstructionHandle tryStart = emitInvoke(il, fac, handlerMethodRef);

        // convert to primitive type
        emitCoerceFromObject(il, fac, methodReturnType);

        // and return

        InstructionHandle tryEnd = emitReturn(il, methodReturnType);

        //
        // catch...
        //
        InstructionHandle rethrowLocation = il.append(new ATHROW());

        Class[] exceptions = method.getExceptionTypes();
        boolean handle_throwable_exception = true;
        boolean handle_runtime_exception = true;
        if (exceptions != null) {
            for (int i = 0; i < exceptions.length; i++) {
                Class ex = exceptions[i];

                if (ex == java.lang.Throwable.class)
                    handle_throwable_exception = false;

                if (ex == java.lang.RuntimeException.class
                        || ex == java.lang.Exception.class)
                    handle_runtime_exception = false;

                mg.addExceptionHandler(tryStart, tryEnd, rethrowLocation,
                        (ObjectType) translate(ex));
            }
        }

        // A RuntimeException should not cause an
        // UndeclaredThrowableException, so we catch and re-throw it
        // that before throwable.
        if (handle_throwable_exception && handle_runtime_exception) {
            mg.addExceptionHandler(tryStart, tryEnd, rethrowLocation,
                    new ObjectType("java.lang.RuntimeException"));
        }

        // If anything else is thrown, it is wrapped in an
        // UndeclaredThrowable
        if (handle_throwable_exception) {
            InstructionHandle handlerStart = il.append(new ASTORE(1));

            il
                    .append(new NEW(
                            cp
                                    .addClass("java.lang.reflect.UndeclaredThrowableException")));
View Full Code Here


              GET_PARENT,
              GET_PARENT_SIG);
  final int getType = cpg.addInterfaceMethodref(DOM_INTF,
                  "getType", "(I)I");

  InstructionHandle begin = il.append(methodGen.loadDOM());
  il.append(SWAP);
  il.append(new INVOKEINTERFACE(getParent, 2));
  if (_left instanceof AncestorPattern) { 
      il.append(methodGen.loadDOM());
      il.append(SWAP);
View Full Code Here

  }
  return _right.typeCheck(stable);
    }

    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
View Full Code Here

  }

  if (_compiled) return;
  _compiled = true;
 
  final InstructionHandle start = il.getEnd();
  translateContents(classGen, methodGen);
  final InstructionHandle end = il.getEnd();
  il.setPositions(true);
    }
View Full Code Here

  // The body of this code can get very large - large than can be handled
  // by a single IFNE(body.getStart()) instruction - need workaround:
        final BranchHandle ifeq = ilLoop.append(new IFEQ(null));
  final BranchHandle loop = ilLoop.append(new GOTO_W(null));
  ifeq.setTarget(ilLoop.append(RETURN));   // applyTemplates() ends here!
  final InstructionHandle ihLoop = ilLoop.getStart();

  // Compile default handling of elements (traverse children)
  InstructionList ilRecurse =
      compileDefaultRecursion(classGen, methodGen, ihLoop);
  InstructionHandle ihRecurse = ilRecurse.getStart();

  // Compile default handling of text/attribute nodes (output text)
  InstructionList ilText =
      compileDefaultText(classGen, methodGen, ihLoop);
  InstructionHandle ihText = ilText.getStart();

  // Distinguish attribute/element/namespace tests for further processing
  final int[] types = new int[DOM.NTYPES + names.size()];
  for (int i = 0; i < types.length; i++) {
      types[i] = i;
  }

  // Initialize isAttribute[] and isNamespace[] arrays
  final boolean[] isAttribute = new boolean[types.length];
  final boolean[] isNamespace = new boolean[types.length];
  for (int i = 0; i < names.size(); i++) {
      final String name = (String)names.elementAt(i);
      isAttribute[i + DOM.NTYPES] = isAttributeName(name);
      isNamespace[i + DOM.NTYPES] = isNamespaceName(name);
  }

  // Compile all templates - regardless of pattern type
  compileTemplates(classGen, methodGen, ihLoop);

  // Handle template with explicit "*" pattern
  final TestSeq elemTest = _testSeq[DOM.ELEMENT];
  InstructionHandle ihElem = ihRecurse;
  if (elemTest != null)
      ihElem = elemTest.compile(classGen, methodGen, ihRecurse);

  // Handle template with explicit "@*" pattern
  final TestSeq attrTest = _testSeq[DOM.ATTRIBUTE];
  InstructionHandle ihAttr = ihText;
  if (attrTest != null)
      ihAttr = attrTest.compile(classGen, methodGen, ihAttr);

  // Do tests for id() and key() patterns first
  InstructionList ilKey = null;
  if (_idxTestSeq != null) {
      loop.setTarget(_idxTestSeq.compile(classGen, methodGen, body.getStart()));
      ilKey = _idxTestSeq.getInstructionList();
  }
  else {
      loop.setTarget(body.getStart());
  }

  // If there is a match on node() we need to replace ihElem
  // and ihText if the priority of node() is higher
  if (_childNodeTestSeq != null) {
      // Compare priorities of node() and "*"
      double nodePrio = _childNodeTestSeq.getPriority();
      int    nodePos  = _childNodeTestSeq.getPosition();
      double elemPrio = (0 - Double.MAX_VALUE);
      int    elemPos  = Integer.MIN_VALUE;

      if (elemTest != null) {
    elemPrio = elemTest.getPriority();
    elemPos  = elemTest.getPosition();
      }
      if (elemPrio == Double.NaN || elemPrio < nodePrio ||
    (elemPrio == nodePrio && elemPos < nodePos))
      {
    ihElem = _childNodeTestSeq.compile(classGen, methodGen, ihLoop);
      }

      // Compare priorities of node() and text()
      final TestSeq textTest = _testSeq[DOM.TEXT];
      double textPrio = (0 - Double.MAX_VALUE);
      int    textPos  = Integer.MIN_VALUE;

      if (textTest != null) {
    textPrio = textTest.getPriority();
    textPos  = textTest.getPosition();
      }
      if (textPrio == Double.NaN || textPrio < nodePrio ||
          (textPrio == nodePrio && textPos < nodePos))
      {
    ihText = _childNodeTestSeq.compile(classGen, methodGen, ihLoop);
    _testSeq[DOM.TEXT] = _childNodeTestSeq;
      }
  }

  // Handle templates with "ns:*" pattern
  InstructionHandle elemNamespaceHandle = ihElem;
  InstructionList nsElem = compileNamespaces(classGen, methodGen,
               isNamespace, isAttribute,
               false, ihElem);
  if (nsElem != null) elemNamespaceHandle = nsElem.getStart();

  // Handle templates with "ns:@*" pattern
  InstructionHandle attrNamespaceHandle = ihAttr;
  InstructionList nsAttr = compileNamespaces(classGen, methodGen,
               isNamespace, isAttribute,
               true, ihAttr);
  if (nsAttr != null) attrNamespaceHandle = nsAttr.getStart();

  // Handle templates with "ns:elem" or "ns:@attr" pattern
  final InstructionHandle[] targets = new InstructionHandle[types.length];
  for (int i = DOM.NTYPES; i < targets.length; i++) {
      final TestSeq testSeq = _testSeq[i];
      // Jump straight to namespace tests ?
      if (isNamespace[i]) {
    if (isAttribute[i])
        targets[i] = attrNamespaceHandle;
    else
        targets[i] = elemNamespaceHandle;
      }
      // Test first, then jump to namespace tests
      else if (testSeq != null) {
    if (isAttribute[i])
        targets[i] = testSeq.compile(classGen, methodGen,
             attrNamespaceHandle);
    else
        targets[i] = testSeq.compile(classGen, methodGen,
             elemNamespaceHandle);
      }
      else {
    targets[i] = ihLoop;
      }
  }


  // Handle pattern with match on root node - default: traverse children
  targets[DOM.ROOT] = _rootPattern != null
      ? getTemplateInstructionHandle(_rootPattern.getTemplate())
      : ihRecurse;
 
  // Handle any pattern with match on text nodes - default: output text
  targets[DOM.TEXT] = _testSeq[DOM.TEXT] != null
      ? _testSeq[DOM.TEXT].compile(classGen, methodGen, ihText)
      : ihText;

  // This DOM-type is not in use - default: process next node
  targets[DOM.NAMESPACE] = ihLoop;

  // Match unknown element in DOM - default: check for namespace match
  targets[DOM.ELEMENT] = elemNamespaceHandle;

  // Match unknown attribute in DOM - default: check for namespace match
  targets[DOM.ATTRIBUTE] = attrNamespaceHandle;

  // Match on processing instruction - default: process next node
  InstructionHandle ihPI = ihLoop;
  if (_childNodeTestSeq != null) ihPI = ihElem;
  if (_testSeq[DOM.PROCESSING_INSTRUCTION] != null)
      targets[DOM.PROCESSING_INSTRUCTION] =
    _testSeq[DOM.PROCESSING_INSTRUCTION].
    compile(classGen, methodGen, ihPI);
  else
      targets[DOM.PROCESSING_INSTRUCTION] = ihPI;
 
  // Match on comments - default: process next node
  InstructionHandle ihComment = ihLoop;
  if (_childNodeTestSeq != null) ihComment = ihElem;
  targets[DOM.COMMENT] = _testSeq[DOM.COMMENT] != null
      ? _testSeq[DOM.COMMENT].compile(classGen, methodGen, ihComment)
      : ihComment;

  // Now compile test sequences for various match patterns:
  for (int i = DOM.NTYPES; i < targets.length; i++) {
      final TestSeq testSeq = _testSeq[i];
      // Jump straight to namespace tests ?
      if ((testSeq == null) || (isNamespace[i])) {
    if (isAttribute[i])
        targets[i] = attrNamespaceHandle;
    else
        targets[i] = elemNamespaceHandle;
      }
      // Match on node type
      else {
    if (isAttribute[i])
        targets[i] = testSeq.compile(classGen, methodGen,
             attrNamespaceHandle);
    else
        targets[i] = testSeq.compile(classGen, methodGen,
             elemNamespaceHandle);
      }
  }

  if (ilKey != null) body.insert(ilKey);

  // Append first code in applyTemplates() - get type of current node
  final int getType = cpg.addInterfaceMethodref(DOM_INTF,
                  "getType", "(I)I");
  body.append(methodGen.loadDOM());
  body.append(new ILOAD(_currentIndex));
  body.append(new INVOKEINTERFACE(getType, 2));

  // Append switch() statement - main dispatch loop in applyTemplates()
  InstructionHandle disp = body.append(new SWITCH(types, targets, ihLoop));

  // Append all the "case:" statements
  appendTestSequences(body);
  // Append the actual template code
  appendTemplateCode(body);
View Full Code Here

  // The body of this code can get very large - large than can be handled
  // by a single IFNE(body.getStart()) instruction - need workaround:
        final BranchHandle ifeq = ilLoop.append(new IFEQ(null));
  final BranchHandle loop = ilLoop.append(new GOTO_W(null));
  ifeq.setTarget(ilLoop.append(RETURN)); // applyTemplates() ends here!
  final InstructionHandle ihLoop = ilLoop.getStart();

  // Compile default handling of elements (traverse children)
  InstructionList ilRecurse =
      compileDefaultRecursion(classGen, methodGen, ihLoop);
  InstructionHandle ihRecurse = ilRecurse.getStart();

  // Compile default handling of text/attribute nodes (output text)
  InstructionList ilText =
      compileDefaultText(classGen, methodGen, ihLoop);
  InstructionHandle ihText = ilText.getStart();

  // Distinguish attribute/element/namespace tests for further processing
  final int[] types = new int[DOM.NTYPES + names.size()];
  for (int i = 0; i < types.length; i++) {
      types[i] = i;
  }

  final boolean[] isAttribute = new boolean[types.length];
  final boolean[] isNamespace = new boolean[types.length];
  for (int i = 0; i < names.size(); i++) {
      final String name = (String)names.elementAt(i);
      isAttribute[i+DOM.NTYPES] = isAttributeName(name);
      isNamespace[i+DOM.NTYPES] = isNamespaceName(name);
  }

  // Compile all templates - regardless of pattern type
  compileTemplateCalls(classGen, methodGen, ihLoop, min, max);

  // Handle template with explicit "*" pattern
  final TestSeq elemTest = _testSeq[DOM.ELEMENT];
  InstructionHandle ihElem = ihRecurse;
  if (elemTest != null) {
      ihElem = elemTest.compile(classGen, methodGen, ihLoop);
  }

  // Handle template with explicit "@*" pattern
  final TestSeq attrTest = _testSeq[DOM.ATTRIBUTE];
  InstructionHandle ihAttr = ihLoop;
  if (attrTest != null) {
      ihAttr = attrTest.compile(classGen, methodGen, ihAttr);
  }

  // Do tests for id() and key() patterns first
  InstructionList ilKey = null;
  if (_idxTestSeq != null) {
      loop.setTarget(_idxTestSeq.compile(classGen, methodGen, body.getStart()));
      ilKey = _idxTestSeq.getInstructionList();
  }
  else {
      loop.setTarget(body.getStart());
  }

  // If there is a match on node() we need to replace ihElem
  // and ihText if the priority of node() is higher
  if (_childNodeTestSeq != null) {
      // Compare priorities of node() and "*"
      double nodePrio = _childNodeTestSeq.getPriority();
      int    nodePos  = _childNodeTestSeq.getPosition();
      double elemPrio = (0 - Double.MAX_VALUE);
      int    elemPos  = Integer.MIN_VALUE;

      if (elemTest != null) {
    elemPrio = elemTest.getPriority();
    elemPos  = elemTest.getPosition();
      }

      if (elemPrio == Double.NaN || elemPrio < nodePrio ||
    (elemPrio == nodePrio && elemPos < nodePos))
      {
    ihElem = _childNodeTestSeq.compile(classGen, methodGen, ihLoop);
      }

      // Compare priorities of node() and text()
      final TestSeq textTest = _testSeq[DOM.TEXT];
      double textPrio = (0 - Double.MAX_VALUE);
      int    textPos  = Integer.MIN_VALUE;

      if (textTest != null) {
    textPrio = textTest.getPriority();
    textPos  = textTest.getPosition();
      }

      if (textPrio == Double.NaN || textPrio < nodePrio ||
          (textPrio == nodePrio && textPos < nodePos))
      {
    ihText = _childNodeTestSeq.compile(classGen, methodGen, ihLoop);
    _testSeq[DOM.TEXT] = _childNodeTestSeq;
      }
  }

  // Handle templates with "ns:*" pattern
  InstructionHandle elemNamespaceHandle = ihElem;
  InstructionList nsElem = compileNamespaces(classGen, methodGen,
               isNamespace, isAttribute,
               false, ihElem);
  if (nsElem != null) elemNamespaceHandle = nsElem.getStart();

  // Handle templates with "ns:@*" pattern
  InstructionList nsAttr = compileNamespaces(classGen, methodGen,
               isNamespace, isAttribute,
               true, ihAttr);
  InstructionHandle attrNamespaceHandle = ihAttr;
  if (nsAttr != null) attrNamespaceHandle = nsAttr.getStart();

  // Handle templates with "ns:elem" or "ns:@attr" pattern
  final InstructionHandle[] targets = new InstructionHandle[types.length];
  for (int i = DOM.NTYPES; i < targets.length; i++) {
      final TestSeq testSeq = _testSeq[i];
      // Jump straight to namespace tests ?
      if (isNamespace[i]) {
    if (isAttribute[i])
        targets[i] = attrNamespaceHandle;
    else
        targets[i] = elemNamespaceHandle;
      }
      // Test first, then jump to namespace tests
      else if (testSeq != null) {
    if (isAttribute[i])
        targets[i] = testSeq.compile(classGen, methodGen,
             attrNamespaceHandle);
    else
        targets[i] = testSeq.compile(classGen, methodGen,
             elemNamespaceHandle);
      }
      else {
    targets[i] = ihLoop;
      }
  }

  // Handle pattern with match on root node - default: traverse children
  targets[DOM.ROOT] = _rootPattern != null
      ? getTemplateInstructionHandle(_rootPattern.getTemplate())
      : ihRecurse;
 
  // Handle any pattern with match on text nodes - default: loop
  targets[DOM.TEXT] = _testSeq[DOM.TEXT] != null
      ? _testSeq[DOM.TEXT].compile(classGen, methodGen, ihText)
      : ihText;

  // This DOM-type is not in use - default: process next node
  targets[DOM.NAMESPACE] = ihLoop;

  // Match unknown element in DOM - default: check for namespace match
  targets[DOM.ELEMENT] = elemNamespaceHandle;

  // Match unknown attribute in DOM - default: check for namespace match
  targets[DOM.ATTRIBUTE] = attrNamespaceHandle;

  // Match on processing instruction - default: loop
  InstructionHandle ihPI = ihLoop;
  if (_childNodeTestSeq != null) ihPI = ihElem;
  if (_testSeq[DOM.PROCESSING_INSTRUCTION] != null) {
      targets[DOM.PROCESSING_INSTRUCTION] =
    _testSeq[DOM.PROCESSING_INSTRUCTION].
    compile(classGen, methodGen, ihPI);
  }
  else {
      targets[DOM.PROCESSING_INSTRUCTION] = ihPI;
  }
 
  // Match on comments - default: process next node
  InstructionHandle ihComment = ihLoop;
  if (_childNodeTestSeq != null) ihComment = ihElem;
  targets[DOM.COMMENT] = _testSeq[DOM.COMMENT] != null
      ? _testSeq[DOM.COMMENT].compile(classGen, methodGen, ihComment)
      : ihComment;

  // Now compile test sequences for various match patterns:
  for (int i = DOM.NTYPES; i < targets.length; i++) {
      final TestSeq testSeq = _testSeq[i];
      // Jump straight to namespace tests ?
      if ((testSeq == null) || (isNamespace[i])) {
    if (isAttribute[i])
        targets[i] = attrNamespaceHandle;
    else
        targets[i] = elemNamespaceHandle;
      }
      // Match on node type
      else {
    if (isAttribute[i])
        targets[i] = testSeq.compile(classGen, methodGen,
             attrNamespaceHandle);
    else
        targets[i] = testSeq.compile(classGen, methodGen,
             elemNamespaceHandle);
      }
  }

  if (ilKey != null) body.insert(ilKey);

  // Append first code in applyTemplates() - get type of current node
  final int getType = cpg.addInterfaceMethodref(DOM_INTF,
                  "getType", "(I)I");
  body.append(methodGen.loadDOM());
  body.append(new ILOAD(_currentIndex));
  body.append(new INVOKEINTERFACE(getType, 2));

  // Append switch() statement - main dispatch loop in applyTemplates()
  InstructionHandle disp = body.append(new SWITCH(types,targets,ihLoop));

  // Append all the "case:" statements
  appendTestSequences(body);
  // Append the actual template code
  appendTemplateCode(body);
View Full Code Here

     * Peephole optimization: Remove sequences of [ALOAD, POP].
     */
    private void peepHoleOptimization(MethodGenerator methodGen) {
  InstructionList il = methodGen.getInstructionList();
  InstructionFinder find = new InstructionFinder(il);
  InstructionHandle ih;
  String pattern;

  // Remove seqences of ALOAD, POP (GTM)
  pattern = "`ALOAD'`POP'`Instruction'";
  for(Iterator iter=find.search(pattern); iter.hasNext();){
View Full Code Here

  // Give local variables (if any) default values before starting loop
  initializeVariables(classGen, methodGen);

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

  translateContents(classGen, methodGen);
       
  nextNode.setTarget(il.append(methodGen.loadIterator()));
  il.append(methodGen.nextNode());
View Full Code Here

         "extractValueFromDOM", className, il, cpg);

  // Values needed for the switch statement
  final int levels = sortObjects.size();
  final int match[] = new int[levels];
  final InstructionHandle target[] = new InstructionHandle[levels];
  InstructionHandle tblswitch = null;

  // Compile switch statement only if the key has multiple levels
  if (levels > 1) {
      // Put the parameter to the swtich statement on the stack
      il.append(new ILOAD(extractMethod.getLocalIndex("level")));
      // Append the switch statement here later on
      tblswitch = il.append(new NOP());
  }

  // Append all the cases for the switch statment
  for (int level = 0; level < levels; level++) {
      match[level] = level;
      final Sort sort = (Sort)sortObjects.elementAt(level);
      target[level] = il.append(NOP);
      sort.translateSelect(sortRecord, extractMethod);
      il.append(ARETURN);
  }
 
  // Compile def. target for switch statement if key has multiple levels
  if (levels > 1) {
      // Append the default target - it will _NEVER_ be reached
      InstructionHandle defaultTarget =
    il.append(new PUSH(cpg, EMPTYSTRING));
      il.insert(tblswitch,new TABLESWITCH(match, target, defaultTarget));
      il.append(ARETURN);
  }
View Full Code Here

  _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())));
View Full Code Here

TOP

Related Classes of org.apache.bcel.generic.InstructionHandle

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.