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; }
@Override public void visit(BinaryExpr expr) { expr.getFirstOperand().acceptVisitor(this); expr.getSecondOperand().acceptVisitor(this); }
@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 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; } }
@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; } }
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; }
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 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()); }
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 void generateBinary(WasmIntBinaryOperation intOp, WasmFloatBinaryOperation floatOp, BinaryExpr expr) { accept(expr.getFirstOperand()); WasmExpression first = result; accept(expr.getSecondOperand()); WasmExpression second = result; if (expr.getType() == null) { result = new WasmIntBinary(WasmIntType.INT32, intOp, first, second); } else { 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: result = new WasmFloatBinary(WasmFloatType.FLOAT32, floatOp, first, second); break; case DOUBLE: result = new WasmFloatBinary(WasmFloatType.FLOAT64, floatOp, first, second); break; } } result.setLocation(expr.getLocation()); }
expr.getFirstOperand().acceptVisitor(this); writer.print(", "); expr.getSecondOperand().acceptVisitor(this); writer.print(")"); return; expr.getSecondOperand().acceptVisitor(this); expr.getFirstOperand().acceptVisitor(this); writer.print(", "); expr.getSecondOperand().acceptVisitor(this); writer.print(")"); return; expr.getFirstOperand().acceptVisitor(this); writer.print(", "); expr.getSecondOperand().acceptVisitor(this); writer.print(")"); return; expr.getSecondOperand().acceptVisitor(this); writer.print(")");
private void visitBinary(BinaryExpr expr, String op, boolean guarded) { if (expr.getLocation() != null) { pushLocation(expr.getLocation()); } if (guarded) { visitBinary(BinaryOperation.OR, "|", () -> visitBinary(expr, op, false), () -> { try { writer.append("0"); } catch (IOException e) { throw new RenderingException("IO error occurred", e); } }); } else { visitBinary(expr.getOperation(), op, () -> expr.getFirstOperand().acceptVisitor(this), () -> expr.getSecondOperand().acceptVisitor(this)); } if (expr.getLocation() != null) { popLocation(); } }
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); } }
call.getArguments().add(result); accept(expr.getSecondOperand()); call.getArguments().add(result); call.getArguments().add(result); accept(expr.getSecondOperand()); call.getArguments().add(result);
break; Expr b = expr.getSecondOperand(); Expr a = expr.getFirstOperand(); BinaryExpr comparison = (BinaryExpr) p; Expr result = BinaryExpr.binary(expr.getOperation(), comparison.getType(), comparison.getFirstOperand(), comparison.getSecondOperand()); result.setLocation(comparison.getLocation()); if (invert) {
public static Expr invert(Expr expr) { if (expr instanceof UnaryExpr) { UnaryExpr unary = (UnaryExpr) expr; if (unary.getOperation() == UnaryOperation.NOT) { return unary.getOperand(); } } else if (expr instanceof BinaryExpr) { BinaryExpr binary = (BinaryExpr) expr; Expr a = binary.getFirstOperand(); Expr b = binary.getSecondOperand(); switch (binary.getOperation()) { case EQUALS: return Expr.binary(BinaryOperation.NOT_EQUALS, binary.getType(), a, b, expr.getLocation()); case NOT_EQUALS: return Expr.binary(BinaryOperation.EQUALS, binary.getType(), a, b, expr.getLocation()); case LESS: return Expr.binary(BinaryOperation.GREATER_OR_EQUALS, binary.getType(), a, b, expr.getLocation()); case LESS_OR_EQUALS: return Expr.binary(BinaryOperation.GREATER, binary.getType(), a, b, expr.getLocation()); case GREATER: return Expr.binary(BinaryOperation.LESS_OR_EQUALS, binary.getType(), a, b, expr.getLocation()); case GREATER_OR_EQUALS: return Expr.binary(BinaryOperation.LESS, binary.getType(), a, b, expr.getLocation()); default: break; } } return Expr.invert(expr); } }
case MULTIPLY: if (expr.getType() != OperationType.INT || RenderingUtil.isSmallInteger(expr.getFirstOperand()) || RenderingUtil.isSmallInteger(expr.getSecondOperand())) { visitBinary(expr, "*", expr.getType() == OperationType.INT); } else {
@Override public void visit(BinaryExpr expr) { expr.getFirstOperand().acceptVisitor(this); expr.getSecondOperand().acceptVisitor(this); }
@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; }