/** Optimize a bi-conditional expression */
  protected void genBranch(
      JExpression left,
      JExpression right,
      boolean cond,
      GenerationContext context,
      CodeLabel label) {
    CodeSequence code = context.getCodeSequence();

    if (cond) {
      CodeLabel skip = new CodeLabel();
      left.genBranch(false, context, skip);
      right.genBranch(true, context, label);
      code.plantLabel(skip);
    } else {
      left.genBranch(false, context, label);
      right.genBranch(false, context, label);
    }
  }
예제 #2
0
  /**
   * Generates a bytecode sequence to convert a value of this type to the specified destination
   * type.
   *
   * @param dest the destination type
   * @param code the code sequence
   */
  public void genCastTo(CNumericType dest, GenerationContext context) {
    CodeSequence code = context.getCodeSequence();

    if (dest != this) {
      switch (dest.type) {
        case TID_BYTE:
          code.plantNoArgInstruction(opc_i2b);
          break;

        case TID_SHORT:
          code.plantNoArgInstruction(opc_i2s);
          break;

        case TID_INT:
          break;

        case TID_LONG:
          code.plantNoArgInstruction(opc_i2l);
          break;

        case TID_FLOAT:
          code.plantNoArgInstruction(opc_i2f);
          break;

        case TID_DOUBLE:
          code.plantNoArgInstruction(opc_i2d);
          break;

        default:
          throw new InconsistencyException();
      }
    }
  }
  /**
   * Generates a sequence of bytescodes
   *
   * @param code the code list
   */
  public void genCode(GenerationContext context) {
    CodeSequence code = context.getCodeSequence();

    setLineNumber(code);

    code.pushContext(this);

    if (cond.isConstant() && cond.booleanValue()) {
      // while (true)
      code.plantLabel(getContinueLabel()); // 	body:
      LocalVariableScope scope = new LocalVariableScope();
      code.pushLocalVariableScope(scope);
      body.genCode(context); // 		BODY CODE
      code.popLocalVariableScope(scope);
      code.plantJumpInstruction(opc_goto, getContinueLabel()); // 		GOTO body
    } else {
      CodeLabel bodyLabel = new CodeLabel();

      code.plantJumpInstruction(opc_goto, getContinueLabel()); // 		GOTO cont
      code.plantLabel(bodyLabel); // 	body:
      LocalVariableScope scope = new LocalVariableScope();
      code.pushLocalVariableScope(scope);
      body.genCode(context); // 		BODY CODE
      code.popLocalVariableScope(scope);
      code.plantLabel(getContinueLabel()); // 	cont:
      cond.genBranch(true, context, bodyLabel); // 		EXPR CODE; IFNE body
    }
    code.plantLabel(getBreakLabel()); // 	end:	...

    code.popContext(this);
  }