public static boolean isSmallInteger(Expr expr) { if (!(expr instanceof ConstantExpr)) { return false; } Object constant = ((ConstantExpr) expr).getValue(); if (!(constant instanceof Integer)) { return false; } int value = (Integer) constant; return Math.abs(value) < (1 << 18); } }
private static boolean isZero(Expr expr) { return expr instanceof ConstantExpr && Integer.valueOf(0).equals(((ConstantExpr) expr).getValue()); }
@Override public void visit(ConstantExpr expr) { CodeGeneratorUtil.writeValue(writer, context, expr.getValue()); }
@Override public void visit(ConstantExpr expr) { if (expr.getValue() == null) { result = new WasmInt32Constant(0); } else if (expr.getValue() instanceof Integer) { result = new WasmInt32Constant((Integer) expr.getValue()); } else if (expr.getValue() instanceof Long) { result = new WasmInt64Constant((Long) expr.getValue()); } else if (expr.getValue() instanceof Float) { result = new WasmFloat32Constant((Float) expr.getValue()); } else if (expr.getValue() instanceof Double) { result = new WasmFloat64Constant((Double) expr.getValue()); } else if (expr.getValue() instanceof String) { String str = (String) expr.getValue(); result = new WasmInt32Constant(context.getStringPool().getStringPointer(str)); } else if (expr.getValue() instanceof ValueType) { int pointer = classGenerator.getClassPointer((ValueType) expr.getValue()); result = new WasmInt32Constant(pointer); } else { throw new IllegalArgumentException("Constant unsupported: " + expr.getValue()); } }
@Override public void visit(ConstantExpr expr) { try { Object value = expr.getValue(); if (value == null) { output.writeByte(3); } else if (value instanceof Integer) { output.writeByte(4); output.writeInt((Integer) value); } else if (value instanceof Long) { output.writeByte(5); output.writeLong((Long) value); } else if (value instanceof Float) { output.writeByte(6); output.writeFloat((Float) value); } else if (value instanceof Double) { output.writeByte(7); output.writeDouble((Double) value); } else if (value instanceof String) { output.writeByte(8); output.writeUTF((String) value); } else if (value instanceof ValueType) { output.writeByte(9); output.writeInt(symbolTable.lookup(value.toString())); } } catch (IOException e) { throw new IOExceptionWrapper(e); } }
private String extractPropertyName(Expr propertyName) { if (!(propertyName instanceof InvocationExpr)) { return null; } InvocationExpr invoke = (InvocationExpr) propertyName; if (!invoke.getMethod().getClassName().equals(JS.class.getName())) { return null; } if (!invoke.getMethod().getName().equals("wrap") || !invoke.getMethod().getDescriptor().parameterType(0).isObject("java.lang.String")) { return null; } Expr arg = invoke.getArguments().get(0); if (!(arg instanceof ConstantExpr)) { return null; } ConstantExpr constant = (ConstantExpr) arg; return constant.getValue() instanceof String ? (String) constant.getValue() : null; }
private void generateGetFunction(IntrinsicContext context, InvocationExpr invocation) { if (!(invocation.getArguments().get(0) instanceof ConstantExpr) || !(invocation.getArguments().get(1) instanceof ConstantExpr) || !(invocation.getArguments().get(2) instanceof ConstantExpr)) { return; } Object functionClassValue = ((ConstantExpr) invocation.getArguments().get(0)).getValue(); Object classValue = ((ConstantExpr) invocation.getArguments().get(1)).getValue(); Object methodValue = ((ConstantExpr) invocation.getArguments().get(2)).getValue(); if (!(functionClassValue instanceof ValueType.Object) || !(classValue instanceof ValueType.Object) || !(methodValue instanceof String)) { return; } String functionClassName = ((ValueType.Object) functionClassValue).getClassName(); String className = ((ValueType.Object) classValue).getClassName(); String methodName = (String) methodValue; ExportedMethodKey key = new ExportedMethodKey(functionClassName, className, methodName); MethodReference method = resolvedMethods.get(key); if (method == null) { return; } context.writer().print("&").print(context.names().forMethod(method)); } }
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; }
public static String getStringLiteral(IntrinsicContext context, InvocationExpr invocation, Expr expr) { if (expr instanceof ConstantExpr) { Object cst = ((ConstantExpr) expr).getValue(); if (cst instanceof String) { return (String) cst; } } context.getDiagnotics().error( new CallLocation(context.getCallingMethod(), invocation.getLocation()), "This method should take string literal"); return ""; } }
public static String getClassLiteral(IntrinsicContext context, InvocationExpr invocation, Expr expr) { if (expr instanceof ConstantExpr) { Object cst = ((ConstantExpr) expr).getValue(); if (cst instanceof ValueType.Object) { return ((ValueType.Object) cst).getClassName(); } } context.getDiagnotics().error( new CallLocation(context.getCallingMethod(), invocation.getLocation()), "This method should take class literal"); return "java.lang.Object"; }
@Override public WasmExpression apply(InvocationExpr invocation, WasmIntrinsicManager manager) { switch (invocation.getMethod().getName()) { case "toAddress": case "cast": return manager.generate(invocation.getArguments().get(0)); case "sizeOf": { ValueType.Object type = (ValueType.Object) ((ConstantExpr) invocation.getArguments().get(0)).getValue(); return new WasmInt32Constant(classGenerator.getClassSize(type.getClassName())); } case "add": { WasmExpression base = manager.generate(invocation.getArguments().get(1)); WasmExpression offset = manager.generate(invocation.getArguments().get(2)); Object type = ((ConstantExpr) invocation.getArguments().get(0)).getValue(); String className = ((ValueType.Object) type).getClassName(); int size = classGenerator.getClassSize(className); int alignment = classGenerator.getClassAlignment(className); size = WasmClassGenerator.align(size, alignment); offset = new WasmIntBinary(WasmIntType.INT32, WasmIntBinaryOperation.MUL, offset, new WasmInt32Constant(size)); return new WasmIntBinary(WasmIntType.INT32, WasmIntBinaryOperation.ADD, base, offset); } default: throw new IllegalArgumentException(invocation.getMethod().toString()); } } }
@Override public void visit(ConstantExpr expr) { try { if (expr.getLocation() != null) { pushLocation(expr.getLocation()); } String str = context.constantToString(expr.getValue()); if (str.startsWith("-")) { writer.append(' '); } writer.append(str); if (expr.getLocation() != null) { popLocation(); } } catch (IOException e) { throw new RenderingException("IO error occured", e); } }
private boolean tryMakePositive(ConstantExpr constantExpr) { Object value = constantExpr.getValue(); if (value instanceof Integer && (Integer) value < 0) { constantExpr.setValue(-(Integer) value); return true; } else if (value instanceof Float && (Float) value < 0) { constantExpr.setValue(-(Float) value); return true; } else if (value instanceof Byte && (Byte) value < 0) { constantExpr.setValue(-(Byte) value); return true; } else if (value instanceof Short && (Short) value < 0) { constantExpr.setValue(-(Short) value); return true; } else if (value instanceof Long && (Long) value < 0) { constantExpr.setValue(-(Long) value); return true; } else if (value instanceof Double && (Double) value < 0) { constantExpr.setValue(-(Double) value); return true; } return false; }
private void writePropertyAccessor(InjectorContext context, Expr property) throws IOException { if (property instanceof ConstantExpr) { String str = (String) ((ConstantExpr) property).getValue(); if (str.isEmpty()) { context.getWriter().append("[\"\"]"); return; } if (isValidIndentifier(str)) { context.getWriter().append(".").append(str); return; } } context.getWriter().append("[$rt_ustr("); context.writeExpr(property); context.getWriter().append(")]"); }
private void writeStringExpr(InjectorContext context, Expr expr) throws IOException { if (expr instanceof ConstantExpr) { String str = (String) ((ConstantExpr) expr).getValue(); context.getWriter().append('"'); context.writeEscaped(str); context.getWriter().append('"'); return; } context.getWriter().append("$rt_ustr("); context.writeExpr(expr); context.getWriter().append(")"); }
} else { WasmExpression offset = manager.generate(invocation.getArguments().get(2)); Object type = ((ConstantExpr) invocation.getArguments().get(1)).getValue(); String className = ((ValueType.Object) type).getClassName(); int size = classGenerator.getClassSize(className);
private void writeStringExpr(InjectorContext context, Expr expr) throws IOException { if (expr instanceof ConstantExpr) { String str = (String) ((ConstantExpr) expr).getValue(); context.getWriter().append('"'); context.writeEscaped(str); context.getWriter().append('"'); return; } context.getWriter().append("$rt_ustr("); context.writeExpr(expr); context.getWriter().append(")"); }