public Expression transform(UnaryMinusExpression exp, CompilerTransformer compiler) {
    final BytecodeExpr inner = (BytecodeExpr) compiler.transform(exp.getExpression());
    final ClassNode type = ClassHelper.getUnwrapper(inner.getType());
    if (type == ClassHelper.byte_TYPE
        || type == ClassHelper.short_TYPE
        || type == ClassHelper.int_TYPE
        || type == ClassHelper.long_TYPE
        || type == ClassHelper.float_TYPE
        || type == ClassHelper.double_TYPE) {
      return new BytecodeExpr(exp, type) {
        protected void compile(MethodVisitor mv) {
          inner.visit(mv);
          if (!ClassHelper.isPrimitiveType(inner.getType())) unbox(type, mv);

          if (type == ClassHelper.int_TYPE
              || type == ClassHelper.short_TYPE
              || type == ClassHelper.byte_TYPE) mv.visitInsn(INEG);
          else if (type == ClassHelper.long_TYPE) mv.visitInsn(LNEG);
          else if (type == ClassHelper.float_TYPE) mv.visitInsn(FNEG);
          else if (type == ClassHelper.double_TYPE) mv.visitInsn(DNEG);
        }
      };
    } else {
      MethodCallExpression negate =
          new MethodCallExpression(inner, "negate", new ArgumentListExpression());
      negate.setSourcePosition(exp);
      return compiler.transform(negate);
    }
  }
  public Expression transform(ExpressionList exp, CompilerTransformer compiler) {
    final ArrayList<BytecodeExpr> list = new ArrayList<BytecodeExpr>(exp.expressions.size());
    for (Expression e : exp.expressions) {
      list.add((BytecodeExpr) compiler.transform(e));
    }

    return new BytecodeExpr(exp, exp.getType()) {
      protected void compile(MethodVisitor mv) {
        for (BytecodeExpr b : list) {
          b.visit(mv);
        }
      }
    };
  }
Пример #3
0
  public static Expression transformExpression(final Expression exp, CompilerTransformer compiler) {
    if (exp instanceof BytecodeExpression) {
      if (exp instanceof BytecodeExpr) return exp;

      return new BytecodeExpr(exp, exp.getType()) {
        protected void compile(MethodVisitor mv) {
          ((BytecodeExpression) exp).visit(mv);
        }
      };
    }

    ExprTransformer t = transformers.get(exp.getClass());
    if (t == null) return compiler.transformImpl(exp);

    return t.transform(exp, compiler);
  }
Пример #4
0
  public BytecodeExpr transformLogical(
      T exp, CompilerTransformer compiler, final Label label, final boolean onTrue) {
    final BytecodeExpr be;
    be = (BytecodeExpr) transform(exp, compiler);
    final ClassNode type = be.getType();

    if (type == ClassHelper.VOID_TYPE) {
      return be;
    }

    if (ClassHelper.isPrimitiveType(type)) {
      return new BytecodeExpr(exp, ClassHelper.VOID_TYPE) {
        protected void compile(MethodVisitor mv) {
          be.visit(mv);
          if (type == ClassHelper.byte_TYPE
              || type == ClassHelper.short_TYPE
              || type == ClassHelper.char_TYPE
              || type == ClassHelper.boolean_TYPE
              || type == ClassHelper.int_TYPE) {
          } else if (type == ClassHelper.long_TYPE) {
            mv.visitInsn(L2I);
          } else if (type == ClassHelper.float_TYPE) {
            mv.visitInsn(F2I);
          } else if (type == ClassHelper.double_TYPE) {
            mv.visitInsn(D2I);
          }
          mv.visitJumpInsn(onTrue ? IFNE : IFEQ, label);
        }
      };
    } else {
      MethodCallExpression safeCall =
          new MethodCallExpression(
              new BytecodeExpr(exp, be.getType()) {
                protected void compile(MethodVisitor mv) {}
              },
              "asBoolean",
              ArgumentListExpression.EMPTY_ARGUMENTS);
      safeCall.setSourcePosition(exp);

      final ResolvedMethodBytecodeExpr call =
          (ResolvedMethodBytecodeExpr) compiler.transform(safeCall);

      if (!call.getType().equals(ClassHelper.boolean_TYPE))
        compiler.addError("asBoolean should return 'boolean'", exp);

      if (call.getMethodNode().getDeclaringClass().equals(ClassHelper.OBJECT_TYPE)) {
        // fast path
        return new BytecodeExpr(exp, ClassHelper.VOID_TYPE) {
          protected void compile(MethodVisitor mv) {
            be.visit(mv);
            mv.visitJumpInsn(onTrue ? IFNONNULL : IFNULL, label);
          }
        };
      } else {
        return new BytecodeExpr(exp, ClassHelper.VOID_TYPE) {
          protected void compile(MethodVisitor mv) {
            be.visit(mv);
            mv.visitInsn(DUP);
            Label nullLabel = new Label(), endLabel = new Label();

            mv.visitJumpInsn(IFNULL, nullLabel);

            call.visit(mv);

            if (onTrue) {
              mv.visitJumpInsn(IFEQ, endLabel);
              mv.visitJumpInsn(GOTO, label);

              mv.visitLabel(nullLabel);
              mv.visitInsn(POP);
            } else {
              mv.visitJumpInsn(IFNE, endLabel);
              mv.visitJumpInsn(GOTO, label);

              mv.visitLabel(nullLabel);
              mv.visitInsn(POP);
              mv.visitJumpInsn(GOTO, label);
            }

            mv.visitLabel(endLabel);
          }
        };
      }
    }
  }