Exemple #1
0
  /**
   * Creates a Frame instance to emulate and verify the execution of a single class file method.
   *
   * @param codeParser the parser used to parse the "Code" attribute of the method being emulated
   * @param extraLocals the number of extra local variables needed
   */
  public Frame(CodeParser codeParser, int extraLocals) {
    this.maxStack = codeParser.getMaxStack();
    this.codeParser = codeParser;

    /*
     * Initialize the operand stack
     */
    stack = new StackProducer[maxStack];

    /*
     * Initialize the types in the local variables
     */
    localTypes = new Klass[codeParser.getMaxLocals() + extraLocals];
    Method method = codeParser.getMethod();
    Klass[] parameterTypes = method.getParameterTypes();
    Local[] parameterLocals = null;
    if (Translator.REVERSE_PARAMETERS && method.isInterpreterInvoked()) {
      parameterLocals = new Local[parameterTypes.length];
    }
    int javacIndex = 0;

    /*
     * Initialize 'this' in non-static methods. The type of 'this' is
     * UNINITIALIZED_THIS if this method is a constructor in any class
     * except java.lang.Object otherwise it is the class in which the
     * method was defined.
     */
    if (!method.isStatic() || method.isConstructor()) {
      Assert.that(parameterLocals == null);
      Klass thisType = method.getDefiningClass();
      if (method.isConstructor() && thisType != Klass.OBJECT) {
        thisType = Klass.UNINITIALIZED_THIS;
      }
      Local thisLocal = allocateParameter(thisType, javacIndex);
      store(javacIndex, thisType, thisLocal);
      javacIndex++;
    }

    /*
     * Initialize locals for the parameters.
     */
    int parameterIndex = javacIndex;
    for (int i = 0; i < parameterTypes.length; i++) {
      Klass parameterType = parameterTypes[i];
      Local parameterLocal = allocateParameter(parameterType, javacIndex);
      if (parameterLocals != null) {
        parameterLocals[i] = parameterLocal;
      }
      if (Klass.SQUAWK_64) {
        if (javacIndex != parameterIndex) {
          Assert.that(parameterIndex < javacIndex);
          parameterLocal.setParameterIndex(parameterIndex);
        }
        parameterIndex++;
      }
      store(javacIndex, parameterType, parameterLocal);
      javacIndex += (parameterType.isDoubleWord() ? 2 : 1);
    }
    parameterLocalsCount = javacIndex;

    /*
     * Adjust the parameter offsets for parameter order reversal.
     */
    if (parameterLocals != null) {
      parameterIndex = 0;
      for (int i = parameterTypes.length - 1; i >= 0; i--) {
        Klass parameterType = parameterTypes[i];
        parameterLocals[i].setParameterIndex(parameterIndex++);
        if (!Klass.SQUAWK_64 && parameterType.isDoubleWord()) {
          parameterIndex++;
        }
      }
    }

    /*
     * Initialize the remaining local variables to the TOP type
     */
    while (javacIndex < localTypes.length) {
      localTypes[javacIndex++] = Klass.TOP;
    }
  }