public static Expr binary(BinaryOperation op, OperationType type, Expr first, Expr second) { BinaryExpr expr = new BinaryExpr(); expr.setFirstOperand(first); expr.setSecondOperand(second); expr.setOperation(op); expr.setType(type); return expr; }
@Override public void visit(BinaryExpr expr) { pushLocation(expr.getLocation()); try { switch (expr.getOperation()) { case AND: case OR: Expr b = expr.getSecondOperand(); Expr a = expr.getFirstOperand(); if (b instanceof ConstantExpr && expr.getOperation() == BinaryOperation.SUBTRACT) { if (tryMakePositive((ConstantExpr) b)) { expr.setOperation(BinaryOperation.ADD); switch (expr.getOperation()) { case EQUALS: case NOT_EQUALS: case GREATER_OR_EQUALS: { BinaryExpr comparison = (BinaryExpr) p; Expr result = BinaryExpr.binary(expr.getOperation(), comparison.getType(), comparison.getFirstOperand(), comparison.getSecondOperand()); result.setLocation(comparison.getLocation()); if (invert) { result = ExprOptimizer.invert(result); expr.setFirstOperand(a); expr.setSecondOperand(b); resultExpr = expr;
private Expr extractLongRightShiftedBy32(Expr expr) { if (!(expr instanceof BinaryExpr)) { return null; } BinaryExpr binary = (BinaryExpr) expr; if (binary.getOperation() != BinaryOperation.RIGHT_SHIFT && binary.getOperation() != BinaryOperation.UNSIGNED_RIGHT_SHIFT) { return null; } if (binary.getType() != OperationType.LONG) { return null; } if (!(binary.getSecondOperand() instanceof ConstantExpr)) { return null; } Object rightConstant = ((ConstantExpr) binary.getSecondOperand()).getValue(); if (rightConstant.equals(32) || rightConstant.equals(32L)) { return binary.getFirstOperand(); } return null; }
private Expr optimizeCondition(Expr expr) { if (expr instanceof BinaryExpr) { BinaryExpr binary = (BinaryExpr) expr; if (isZero(((BinaryExpr) expr).getSecondOperand())) { switch (binary.getOperation()) { case EQUALS: return ExprOptimizer.invert(binary.getFirstOperand()); case NOT_EQUALS: return binary.getFirstOperand(); } } } return expr; } }
private void generateOr(BinaryExpr expr) { WasmBlock block = new WasmBlock(false); block.setType(WasmType.INT32); accept(expr.getFirstOperand()); WasmBranch branch = new WasmBranch(result, block); branch.setResult(new WasmInt32Constant(1)); branch.setLocation(expr.getLocation()); branch.getResult().setLocation(expr.getLocation()); block.getBody().add(new WasmDrop(branch)); accept(expr.getSecondOperand()); block.getBody().add(result); block.setLocation(expr.getLocation()); result = block; }
@Override public void visit(BinaryExpr expr) { expr.getFirstOperand().acceptVisitor(this); expr.getSecondOperand().acceptVisitor(this); }
@Override public void visit(BinaryExpr expr) { if (expr.getType() == OperationType.LONG) { switch (expr.getOperation()) { case ADD: visitBinaryFunction(expr, "Long_add"); switch (expr.getOperation()) { case ADD: visitBinary(expr, "+", expr.getType() == OperationType.INT); break; case SUBTRACT: visitBinary(expr, "-", expr.getType() == OperationType.INT); break; case MULTIPLY: visitBinary(expr, "*", expr.getType() == OperationType.INT); break; case DIVIDE: visitBinary(expr, "/", expr.getType() == OperationType.INT); break; case MODULO: visitBinary(expr, "%", expr.getType() == OperationType.INT); break; case EQUALS: if (expr.getType() == OperationType.INT) { visitBinary(expr, "==", false); } else { if (expr.getType() == OperationType.INT) { visitBinary(expr, "!=", false);
private static boolean isComparison(Expr expr) { return expr instanceof BinaryExpr && ((BinaryExpr) expr).getOperation() == BinaryOperation.COMPARE; }
@Override public void visit(BinaryExpr expr) { try { output.writeByte(0); output.writeByte(expr.getOperation().ordinal()); output.writeByte(expr.getType() != null ? expr.getType().ordinal() + 1 : 0); writeExpr(expr.getFirstOperand()); writeExpr(expr.getSecondOperand()); } catch (IOException e) { throw new IOExceptionWrapper(e); } }
private void generateAnd(BinaryExpr expr) { WasmBlock block = new WasmBlock(false); block.setType(WasmType.INT32); accept(expr.getFirstOperand()); WasmBranch branch = new WasmBranch(negate(result), block); branch.setResult(new WasmInt32Constant(0)); branch.setLocation(expr.getLocation()); branch.getResult().setLocation(expr.getLocation()); block.getBody().add(new WasmDrop(branch)); accept(expr.getSecondOperand()); block.getBody().add(result); block.setLocation(expr.getLocation()); result = block; }
private boolean isSideEffectFree(Expr expr) { if (expr == null) { return true; } if (expr instanceof VariableExpr || expr instanceof ConstantExpr) { return true; } if (expr instanceof BinaryExpr) { BinaryExpr binary = (BinaryExpr) expr; return isSideEffectFree(binary.getFirstOperand()) && isSideEffectFree(binary.getSecondOperand()); } if (expr instanceof UnaryExpr) { return isSideEffectFree(((UnaryExpr) expr).getOperand()); } if (expr instanceof InstanceOfExpr) { return isSideEffectFree(((InstanceOfExpr) expr).getExpr()); } if (expr instanceof PrimitiveCastExpr) { return isSideEffectFree(((PrimitiveCastExpr) expr).getValue()); } if (expr instanceof NewExpr) { return true; } return false; }
private static boolean isComparison(Expr expr) { return expr instanceof BinaryExpr && ((BinaryExpr) expr).getOperation() == BinaryOperation.COMPARE; }
@Override public void visit(BinaryExpr expr) { super.visit(expr); switch (expr.getOperation()) { case COMPARE: consumer.consumeFunction("$rt_compare"); break; case MULTIPLY: if (expr.getType() == OperationType.INT && !RenderingUtil.isSmallInteger(expr.getFirstOperand()) && !RenderingUtil.isSmallInteger(expr.getSecondOperand())) { consumer.consumeFunction("$rt_imul"); } break; default: break; } }
@Override public void visit(BinaryExpr expr) { pushLocation(expr.getLocation()); try { switch (expr.getOperation()) { case AND: case OR: expr.getSecondOperand().acceptVisitor(this); Expr b = resultExpr; if (b instanceof ConstantExpr && expr.getOperation() == BinaryOperation.SUBTRACT) { if (tryMakePositive((ConstantExpr) b)) { expr.setOperation(BinaryOperation.ADD); expr.getFirstOperand().acceptVisitor(this); Expr a = resultExpr; Expr p = a; switch (expr.getOperation()) { case EQUALS: case NOT_EQUALS: case GREATER_OR_EQUALS: { BinaryExpr comparison = (BinaryExpr) p; Expr result = BinaryExpr.binary(expr.getOperation(), comparison.getType(), comparison.getFirstOperand(), comparison.getSecondOperand()); result.setLocation(comparison.getLocation()); if (invert) { result = ExprOptimizer.invert(result);
@Override protected Expr clone(Map<Expr, Expr> cache) { Expr known = cache.get(this); if (known != null) { return known; } BinaryExpr copy = new BinaryExpr(); cache.put(this, copy); copy.setFirstOperand(firstOperand != null ? firstOperand.clone(cache) : null); copy.setSecondOperand(secondOperand != null ? secondOperand.clone(cache) : null); copy.setOperation(operation); copy.setType(type); return copy; } }
private void visitBinaryFunction(BinaryExpr expr, String function) { try { if (expr.getLocation() != null) { pushLocation(expr.getLocation()); } writer.append(function); writer.append('('); precedence = Precedence.min(); expr.getFirstOperand().acceptVisitor(this); writer.append(",").ws(); precedence = Precedence.min(); expr.getSecondOperand().acceptVisitor(this); writer.append(')'); if (expr.getLocation() != null) { popLocation(); } } catch (IOException e) { throw new RenderingException("IO error occured", e); } }
@Override public void visit(BinaryExpr expr) { expr.getFirstOperand().acceptVisitor(this); expr.getSecondOperand().acceptVisitor(this); }
@Override public void visit(BinaryExpr expr) { super.visit(expr); switch (expr.getOperation()) { case COMPARE: consumer.consumeFunction("$rt_compare"); break; default: break; } }
private void generateBinary(WasmIntBinaryOperation intOp, BinaryExpr expr) { accept(expr.getFirstOperand()); WasmExpression first = result; accept(expr.getSecondOperand()); WasmExpression second = result; if (expr.getType() == OperationType.LONG) { switch (expr.getOperation()) { case LEFT_SHIFT: case RIGHT_SHIFT: case UNSIGNED_RIGHT_SHIFT: second = new WasmConversion(WasmType.INT32, WasmType.INT64, false, second); break; default: break; } } switch (expr.getType()) { case INT: result = new WasmIntBinary(WasmIntType.INT32, intOp, first, second); break; case LONG: result = new WasmIntBinary(WasmIntType.INT64, intOp, first, second); break; case FLOAT: case DOUBLE: throw new AssertionError("Can't translate operation " + intOp + " for type " + expr.getType()); } result.setLocation(expr.getLocation()); }
switch (type) { case 0: { BinaryExpr expr = new BinaryExpr(); expr.setOperation(binaryOperations[input.readByte()]); byte valueType = input.readByte(); expr.setType(valueType > 0 ? OperationType.values()[valueType] : null); expr.setFirstOperand(readExpr(input)); expr.setSecondOperand(readExpr(input)); return expr;