예제 #1
0
파일: Frame.java 프로젝트: johangas/moped
  /**
   * Reset the state of the operand stack based on the stack state recorded for a given target.
   *
   * @param target the target whose recorded state will be used to reset the state of this frame's
   *     operand stack
   * @param isForCatch specifies if target corresponds to an exception handler entry
   */
  public void resetStack(Target target, boolean isForCatch) {
    Klass[] recordedTypes = target.getStack();
    if (!isForCatch) {
      sp = 0;
      if (recordedTypes.length != 0) {
        StackProducer[] derivedStack = target.getDerivedStack();
        boolean isBackwardBranchTarget = (derivedStack[0] == null);
        if (isBackwardBranchTarget) {
          for (int i = 0; i < recordedTypes.length; i++) {
            Klass recordedType = recordedTypes[i];
            if (recordedType != Klass.LONG2 && recordedType != Klass.DOUBLE2) {

              StackMerge merge = new StackMerge(recordedType);
              // Sometimes we end up with old derivedStack[0] == a StackMerge with no producers.
              Assert.that(derivedStack[0] == null || !derivedStack[0].isOnStack());
              derivedStack[0] = merge;
              push(merge);
            }
          }
        } else {
          while (sp != derivedStack.length) {
            StackProducer producer = derivedStack[sp];
            stack[sp++] = producer;
          }
        }
      }
    } else {
      // Assert.that(sp == 0);
      sp = 0;
    }
  }
예제 #2
0
파일: Frame.java 프로젝트: johangas/moped
 /**
  * Traces the state of the operand stack at the current verification address.
  *
  * @param target the stack map (if any) at the current verification address
  */
 private void traceStack(Target target) {
   if (Translator.TRACING_ENABLED) {
     Klass[] map = target == null ? Klass.NO_CLASSES : target.getStack();
     int r = 0; // index into recorded stack (i.e. from stack map)
     int d = 0; // index into derived stack
     while (r < map.length || d < sp) {
       Klass derived = (d < sp) ? getStackTypeAt(d) : null;
       Klass recorded = (r < map.length) ? map[r] : null;
       String prefix = "  stack[" + r + "]: ";
       traceType(derived, prefix, true);
       if (recorded != null) {
         traceType(recorded, prefix, false);
       }
       ++r;
       ++d;
     }
   }
 }
예제 #3
0
파일: Frame.java 프로젝트: johangas/moped
  /**
   * Merges the current state of the operand stack into the saved state at a control flow target.
   * This method also verifies that the current state of the operand stack matches the expected
   * state of the operand stack at the target as pecified by a stack map entry.
   *
   * @param target the target encapsulting the merged state of the operand stack at a distinct
   *     address
   * @param replaceWithTarget if true, then the current state of the operand stack is updated to
   *     reflect the state recorded in the stack map entry
   */
  public void mergeStack(Target target, boolean replaceWithTarget) {
    Klass[] recordedTypes = target.getStack();

    /*
     * Fail if the map sp is different
     */
    if (recordedTypes.length != getStackSize()) {
      throw codeParser.verifyError("size of recorded and derived stack differs");
    }

    /*
     * Check the stack items
     */
    for (int r = 0, d = 0; r < recordedTypes.length; ++r, ++d) {
      Klass recordedType = recordedTypes[r];
      Klass derivedType = getStackTypeAt(d);
      if (!recordedType.isAssignableFrom(derivedType)) {

        // Interfaces are treated like java.lang.Object in the verifier according to the CLDC spec.
        if (!recordedType.isInterface() || !Klass.OBJECT.isAssignableFrom(derivedType)) {
          throw codeParser.verifyError(
              "invalid type on operand stack @ "
                  + d
                  + ": expected "
                  + recordedType
                  + ", received "
                  + derivedType);
        }
      }
    }

    /*
     * Merge the instructions on the stack
     */
    target.merge(this);

    if (replaceWithTarget) {
      resetStack(target, false);
    }
  }