public ValueType getMetaParameterType(int index) { if (!isStatic) { if (index == 0) { return ValueType.object(method.getClassName()); } else { --index; } } return method.parameterType(index); }
private Method getJavaMethod(ClassLoader classLoader, MethodReference ref) throws ReflectiveOperationException { Class<?> cls = Class.forName(ref.getClassName(), true, classLoader); Class<?>[] parameterTypes = new Class<?>[ref.parameterCount()]; for (int i = 0; i < parameterTypes.length; ++i) { parameterTypes[i] = getJavaType(classLoader, ref.parameterType(i)); } return cls.getDeclaredMethod(ref.getName(), parameterTypes); }
private Method asJvmMethod(MethodReference method) { Class<?> cls; try { cls = Class.forName(method.getClassName(), false, classLoader); } catch (ClassNotFoundException e) { throw new RuntimeException("Can't find class " + method.getClassName()); } Class<?>[] jvmParameters = new Class[method.parameterCount()]; for (int i = 0; i < method.parameterCount(); ++i) { jvmParameters[i] = asJvmClass(method.parameterType(i)); } Class<?> jvmReturnType = asJvmClass(method.getReturnType()); for (Method jvmMethod : cls.getDeclaredMethods()) { if (Arrays.equals(jvmMethod.getParameterTypes(), jvmParameters) && jvmReturnType.equals(jvmMethod.getReturnType())) { return jvmMethod; } } throw new RuntimeException("Method not found: " + method); }
@Override public boolean canHandle(MethodReference method) { if (!characteristics.isStructure(method.getClassName())) { return false; } switch (method.getName()) { case "cast": case "toAddress": return method.parameterCount() == 0; case "sizeOf": return method.parameterCount() == 1 && method.parameterType(0).equals(ValueType.parse(Class.class)); case "add": return method.parameterCount() == 3 && method.parameterType(0).equals(ValueType.parse(Class.class)) && method.parameterType(1).equals(ValueType.parse(Structure.class)) && method.parameterType(2).equals(ValueType.INTEGER); default: return false; } }
private static WasmExpression comparison(WasmIntBinaryOperation intOp, WasmFloatBinaryOperation floatOp, InvocationExpr invocation, WasmIntrinsicManager manager) { WasmType type = WasmGeneratorUtil.mapType(invocation.getMethod().parameterType(0)); WasmExpression first = manager.generate(invocation.getArguments().get(0)); WasmExpression second = manager.generate(invocation.getArguments().get(1)); switch (type) { case INT32: return new WasmIntBinary(WasmIntType.INT32, intOp, first, second); case INT64: return new WasmIntBinary(WasmIntType.INT64, intOp, first, second); case FLOAT32: return new WasmFloatBinary(WasmFloatType.FLOAT32, floatOp, first, second); case FLOAT64: return new WasmFloatBinary(WasmFloatType.FLOAT64, floatOp, first, second); default: throw new IllegalArgumentException(type.toString()); } } }
@Override public void apply(IntrinsicContext context, InvocationExpr invocation) { MethodReference method = invocation.getMethod(); if (method.getClassName().equals(Function.class.getName())) { generateGetFunction(context, invocation); return; } context.writer().print("(((").printType(method.getReturnType()).print(" (*)("); if (method.parameterCount() > 0) { context.writer().printType(method.parameterType(0)); for (int i = 1; i < method.parameterCount(); ++i) { context.writer().print(", ").printType(method.parameterType(i)); } } context.writer().print(")) "); context.emit(invocation.getArguments().get(0)); context.writer().print(")("); if (invocation.getArguments().size() > 1) { context.emit(invocation.getArguments().get(1)); for (int i = 2; i < invocation.getArguments().size(); ++i) { context.writer().print(", "); context.emit(invocation.getArguments().get(i)); } } context.writer().print("))"); }
ValueEmitter[] castParamVars = new ValueEmitter[paramVars.length]; for (int i = 0; i < castParamVars.length; ++i) { castParamVars[i] = paramVars[i].cast(implMethod.parameterType(i));
if (invocation.getMethod().parameterCount() == 1) { WasmExpression offset = manager.generate(invocation.getArguments().get(1)); if (invocation.getMethod().parameterType(0) == ValueType.LONG) { offset = new WasmConversion(WasmType.INT64, WasmType.INT32, false, offset); manager.generate(invocation.getArguments().get(1))); case "ofData": { ValueType.Array type = (ValueType.Array) invocation.getMethod().parameterType(0); int alignment = getAlignment(type.getItemType()); int start = WasmClassGenerator.align(classGenerator.getClassSize(RuntimeArray.class.getName()),
call.getParameterTypes().add(WasmType.INT32); for (int i = 0; i < expr.getMethod().parameterCount(); ++i) { call.getParameterTypes().add(WasmGeneratorUtil.mapType(expr.getMethod().parameterType(i)));
private Variable getParameterVar(int index) { Program program = MetaprogrammingImpl.generator.getProgram(); Variable var = program.variableAt(index + 1); ValueType type = model.getMethod().parameterType(index); if (type instanceof ValueType.Primitive) { switch (((ValueType.Primitive) type).getKind()) {
public ValueEmitter invoke(MethodReference method, ValueEmitter... arguments) { for (int i = 0; i < method.parameterCount(); ++i) { if (!hierarchy.isSuperType(method.parameterType(i), arguments[i].getType(), true)) { throw new EmitException("Argument " + i + " of type " + arguments[i].getType() + " is " + "not compatible with method " + method); } } Variable result = null; if (method.getReturnType() != ValueType.VOID) { result = program.createVariable(); } InvokeInstruction insn = new InvokeInstruction(); insn.setType(InvocationType.SPECIAL); insn.setMethod(method); insn.setReceiver(result); Variable[] insnArguments = new Variable[arguments.length]; for (int i = 0; i < insnArguments.length; ++i) { insnArguments[i] = arguments[i].variable; } insn.setArguments(insnArguments); addInstruction(insn); return result != null ? var(result, method.getReturnType()) : null; }
break; case "ofData": { ValueType.Array type = (ValueType.Array) invocation.getMethod().parameterType(0); context.writer().print("((char*) "); context.emit(invocation.getArguments().get(0));
if (!hierarchy.isSuperType(method.parameterType(i), arguments[i].getType(), false)) { throw new EmitException("Argument " + i + " of type " + arguments[i].getType() + " is " + "not compatible with method " + method);
ValueType param = method.parameterType(i); types[i + 1] = convert(param);
public ValueType getMetaParameterType(int index) { if (!isStatic) { if (index == 0) { return ValueType.object(method.getClassName()); } else { --index; } } return method.parameterType(index); }
private Method getJavaMethod(ClassLoader classLoader, MethodReference ref) throws ReflectiveOperationException { Class<?> cls = Class.forName(ref.getClassName(), true, classLoader); Class<?>[] parameterTypes = new Class<?>[ref.parameterCount()]; for (int i = 0; i < parameterTypes.length; ++i) { parameterTypes[i] = getJavaType(classLoader, ref.parameterType(i)); } return cls.getDeclaredMethod(ref.getName(), parameterTypes); }
private Method asJvmMethod(MethodReference method) { Class<?> cls; try { cls = Class.forName(method.getClassName(), false, classLoader); } catch (ClassNotFoundException e) { throw new RuntimeException("Can't find class " + method.getClassName()); } Class<?>[] jvmParameters = new Class[method.parameterCount()]; for (int i = 0; i < method.parameterCount(); ++i) { jvmParameters[i] = asJvmClass(method.parameterType(i)); } Class<?> jvmReturnType = asJvmClass(method.getReturnType()); for (Method jvmMethod : cls.getDeclaredMethods()) { if (Arrays.equals(jvmMethod.getParameterTypes(), jvmParameters) && jvmReturnType.equals(jvmMethod.getReturnType())) { return jvmMethod; } } throw new RuntimeException("Method not found: " + method); }
private static WasmExpression comparison(WasmIntBinaryOperation intOp, WasmFloatBinaryOperation floatOp, InvocationExpr invocation, WasmIntrinsicManager manager) { WasmType type = WasmGeneratorUtil.mapType(invocation.getMethod().parameterType(0)); WasmExpression first = manager.generate(invocation.getArguments().get(0)); WasmExpression second = manager.generate(invocation.getArguments().get(1)); switch (type) { case INT32: return new WasmIntBinary(WasmIntType.INT32, intOp, first, second); case INT64: return new WasmIntBinary(WasmIntType.INT64, intOp, first, second); case FLOAT32: return new WasmFloatBinary(WasmFloatType.FLOAT32, floatOp, first, second); case FLOAT64: return new WasmFloatBinary(WasmFloatType.FLOAT64, floatOp, first, second); default: throw new IllegalArgumentException(type.toString()); } } }
public ValueEmitter invoke(MethodReference method, ValueEmitter... arguments) { for (int i = 0; i < method.parameterCount(); ++i) { if (!classSource.isSuperType(method.parameterType(i), arguments[i].getType()).orElse(true)) { throw new EmitException("Argument " + i + " of type " + arguments[i].getType() + " is " + "not compatible with method " + method); } } Variable result = null; if (method.getReturnType() != ValueType.VOID) { result = program.createVariable(); } InvokeInstruction insn = new InvokeInstruction(); insn.setType(InvocationType.SPECIAL); insn.setMethod(method); insn.setReceiver(result); for (ValueEmitter arg : arguments) { insn.getArguments().add(arg.variable); } addInstruction(insn); return result != null ? var(result, method.getReturnType()) : null; }
ValueEmitter[] castParamVars = new ValueEmitter[paramVars.length]; for (int i = 0; i < castParamVars.length; ++i) { castParamVars[i] = paramVars[i].cast(implMethod.parameterType(i));