Search in sources :

Example 11 with Value

use of de.mirkosertic.bytecoder.ssa.Value in project Bytecoder by mirkosertic.

the class OpenCLWriter method printInvokeStatic.

private void printInvokeStatic(InvokeStaticMethodExpression aValue) {
    BytecodeLinkedClass theLinkedClass = linkerContext.resolveClass(aValue.getClassName());
    BytecodeResolvedMethods theMethods = theLinkedClass.resolvedMethods();
    AtomicBoolean theFound = new AtomicBoolean(false);
    theMethods.stream().forEach(aMethodMapsEntry -> {
        BytecodeMethod theMethod = aMethodMapsEntry.getValue();
        if (Objects.equals(theMethod.getName().stringValue(), aValue.getMethodName()) && theMethod.getSignature().metchesExactlyTo(aValue.getSignature())) {
            BytecodeAnnotation theAnnotation = theMethod.getAttributes().getAnnotationByType(OpenCLFunction.class.getName());
            if (theAnnotation == null) {
                throw new IllegalArgumentException("Annotation @OpenCLFunction required for static method " + aValue.getMethodName());
            }
            String theMethodName = theAnnotation.getElementValueByName("value").stringValue();
            BytecodeMethodSignature theSignature = aValue.getSignature();
            print(theMethodName);
            print("(");
            List<Value> theArguments = aValue.incomingDataFlows();
            for (int i = 0; i < theArguments.size(); i++) {
                if (i > 0) {
                    print(",");
                }
                if (!theSignature.getArguments()[i].isPrimitive()) {
                    print("*");
                }
                printValue(theArguments.get(i));
            }
            print(")");
            theFound.set(true);
        }
    });
    if (!theFound.get()) {
        throw new IllegalArgumentException("Not supported method : " + aValue.getMethodName());
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BytecodeAnnotation(de.mirkosertic.bytecoder.core.BytecodeAnnotation) BytecodeResolvedMethods(de.mirkosertic.bytecoder.core.BytecodeResolvedMethods) BytecodeMethodSignature(de.mirkosertic.bytecoder.core.BytecodeMethodSignature) BytecodeMethod(de.mirkosertic.bytecoder.core.BytecodeMethod) DoubleValue(de.mirkosertic.bytecoder.ssa.DoubleValue) Value(de.mirkosertic.bytecoder.ssa.Value) IntegerValue(de.mirkosertic.bytecoder.ssa.IntegerValue) LongValue(de.mirkosertic.bytecoder.ssa.LongValue) FloatValue(de.mirkosertic.bytecoder.ssa.FloatValue) OpenCLFunction(de.mirkosertic.bytecoder.api.opencl.OpenCLFunction) BytecodeLinkedClass(de.mirkosertic.bytecoder.core.BytecodeLinkedClass)

Example 12 with Value

use of de.mirkosertic.bytecoder.ssa.Value in project Bytecoder by mirkosertic.

the class OpenCLWriter method printGetFieldValue.

private void printGetFieldValue(GetFieldExpression aValue) {
    BytecodeLinkedClass theLinkedClass = linkerContext.resolveClass(BytecodeObjectTypeRef.fromUtf8Constant(aValue.getField().getClassIndex().getClassConstant().getConstant()));
    if (theLinkedClass == kernelClass) {
        print(aValue.getField().getNameAndTypeIndex().getNameAndType().getNameIndex().getName().stringValue());
    } else {
        Value theValue = aValue.incomingDataFlows().get(0);
        if (theValue instanceof Variable && ((Variable) theValue).isSynthetic()) {
            print(aValue.getField().getNameAndTypeIndex().getNameAndType().getNameIndex().getName().stringValue());
        } else {
            printValue(theValue);
            printInstanceFieldReference(aValue.getField());
        }
    }
}
Also used : Variable(de.mirkosertic.bytecoder.ssa.Variable) DoubleValue(de.mirkosertic.bytecoder.ssa.DoubleValue) Value(de.mirkosertic.bytecoder.ssa.Value) IntegerValue(de.mirkosertic.bytecoder.ssa.IntegerValue) LongValue(de.mirkosertic.bytecoder.ssa.LongValue) FloatValue(de.mirkosertic.bytecoder.ssa.FloatValue) BytecodeLinkedClass(de.mirkosertic.bytecoder.core.BytecodeLinkedClass)

Example 13 with Value

use of de.mirkosertic.bytecoder.ssa.Value in project Bytecoder by mirkosertic.

the class WASMSSAWriter method writeDirectMethodInvokeValue.

private void writeDirectMethodInvokeValue(DirectInvokeMethodExpression aValue) {
    List<Value> theIncomingData = aValue.incomingDataFlows();
    Value theTarget = theIncomingData.get(0);
    List<Value> theValues = theIncomingData.subList(1, theIncomingData.size());
    print("(call $");
    print(WASMWriterUtils.toMethodName(aValue.getClazz(), aValue.getMethodName(), aValue.getSignature()));
    print(" ");
    writeValue(theTarget);
    for (Value theValue : theValues) {
        print(" ");
        writeValue(theValue);
    }
    print(")");
}
Also used : StringValue(de.mirkosertic.bytecoder.ssa.StringValue) ByteValue(de.mirkosertic.bytecoder.ssa.ByteValue) Value(de.mirkosertic.bytecoder.ssa.Value) ClassReferenceValue(de.mirkosertic.bytecoder.ssa.ClassReferenceValue) FloatValue(de.mirkosertic.bytecoder.ssa.FloatValue) NullValue(de.mirkosertic.bytecoder.ssa.NullValue) DoubleValue(de.mirkosertic.bytecoder.ssa.DoubleValue) IntegerValue(de.mirkosertic.bytecoder.ssa.IntegerValue) LongValue(de.mirkosertic.bytecoder.ssa.LongValue) ShortValue(de.mirkosertic.bytecoder.ssa.ShortValue)

Example 14 with Value

use of de.mirkosertic.bytecoder.ssa.Value in project Bytecoder by mirkosertic.

the class WASMSSAWriter method writeTableSwitchExpression.

private void writeTableSwitchExpression(TableSwitchExpression aExpression) {
    println("(block $tableswitch");
    Value theValue = aExpression.incomingDataFlows().get(0);
    WASMSSAWriter theChild1 = withDeeperIndent();
    theChild1.println("(block $label$0");
    WASMSSAWriter theChild2 = theChild1.withDeeperIndent();
    theChild2.println("(block $label$1");
    WASMSSAWriter theChild3 = theChild2.withDeeperIndent();
    theChild3.print("(br_if $label$1 (i32.lt_s ");
    theChild3.writeValue(theValue);
    theChild3.print(" (i32.const ");
    theChild3.print(aExpression.getLowValue());
    theChild3.println(")))");
    theChild3.print("(br_if $label$0 (i32.le_s ");
    theChild3.writeValue(theValue);
    theChild3.print(" (i32.const ");
    theChild3.print(aExpression.getHighValue());
    theChild3.println(")))");
    theChild3.writeExpressionList(aExpression.getDefaultExpressions());
    theChild3.println();
    theChild3.println("(br $tableswitch)");
    theChild2.println(")");
    theChild1.println(")");
    WASMSSAWriter theChild4 = withDeeperIndent();
    // For each statement
    for (Map.Entry<Long, ExpressionList> theEntry : aExpression.getOffsets().entrySet()) {
        theChild4.print("(block $switch_");
        theChild4.print(theEntry.getKey());
        theChild4.println();
        WASMSSAWriter theChild5 = theChild4.withDeeperIndent();
        theChild5.print("(br_if $switch_");
        theChild5.print(theEntry.getKey());
        theChild5.print(" (i32.ne (i32.const ");
        theChild5.print(theEntry.getKey());
        theChild5.print(") (i32.sub ");
        theChild5.writeValue(theValue);
        theChild5.print(" (i32.const ");
        theChild5.print(aExpression.getLowValue());
        theChild5.print("))))");
        theChild5.println();
        theChild5.writeExpressionList(theEntry.getValue());
        theChild5.println();
        theChild4.println(")");
    }
    println(")");
    println("(unreachable)");
}
Also used : StringValue(de.mirkosertic.bytecoder.ssa.StringValue) ByteValue(de.mirkosertic.bytecoder.ssa.ByteValue) Value(de.mirkosertic.bytecoder.ssa.Value) ClassReferenceValue(de.mirkosertic.bytecoder.ssa.ClassReferenceValue) FloatValue(de.mirkosertic.bytecoder.ssa.FloatValue) NullValue(de.mirkosertic.bytecoder.ssa.NullValue) DoubleValue(de.mirkosertic.bytecoder.ssa.DoubleValue) IntegerValue(de.mirkosertic.bytecoder.ssa.IntegerValue) LongValue(de.mirkosertic.bytecoder.ssa.LongValue) ShortValue(de.mirkosertic.bytecoder.ssa.ShortValue) Map(java.util.Map) ExpressionList(de.mirkosertic.bytecoder.ssa.ExpressionList)

Example 15 with Value

use of de.mirkosertic.bytecoder.ssa.Value in project Bytecoder by mirkosertic.

the class WASMSSAWriter method writeTypeConversion.

private void writeTypeConversion(TypeConversionExpression aValue) {
    TypeRef theTargetType = aValue.resolveType();
    Value theSource = aValue.incomingDataFlows().get(0);
    if (Objects.equals(theTargetType.resolve(), theSource.resolveType().resolve())) {
        // No conversion needed!
        writeValue(theSource);
        return;
    }
    switch(theSource.resolveType().resolve()) {
        case DOUBLE:
        case FLOAT:
            {
                // Convert floating point to something else
                switch(aValue.resolveType().resolve()) {
                    case DOUBLE:
                    case FLOAT:
                        {
                            // No conversion needed
                            writeValue(theSource);
                            return;
                        }
                    case INT:
                    case SHORT:
                    case BYTE:
                    case LONG:
                    case CHAR:
                        {
                            // Convert f32 to i32
                            print("(i32.trunc_s/f32 ");
                            writeValue(theSource);
                            print(")");
                            return;
                        }
                    default:
                        throw new IllegalStateException("target type " + aValue.resolveType() + " not supported!");
                }
            }
        case INT:
        case LONG:
        case BYTE:
        case SHORT:
        case CHAR:
            {
                // Convert floating point to something else
                switch(aValue.resolveType().resolve()) {
                    case DOUBLE:
                    case FLOAT:
                        {
                            // Convert i32 to f32
                            print("(f32.convert_s/i32 ");
                            writeValue(theSource);
                            print(")");
                            return;
                        }
                    case INT:
                    case SHORT:
                    case BYTE:
                    case LONG:
                    case CHAR:
                        {
                            // No conversion needed
                            writeValue(theSource);
                            return;
                        }
                    default:
                        throw new IllegalStateException("target type " + aValue.resolveType() + " not supported!");
                }
            }
        default:
            throw new IllegalStateException("Conversion of " + theSource.resolveType() + " not supported!");
    }
}
Also used : BytecodePrimitiveTypeRef(de.mirkosertic.bytecoder.core.BytecodePrimitiveTypeRef) BytecodeTypeRef(de.mirkosertic.bytecoder.core.BytecodeTypeRef) TypeRef(de.mirkosertic.bytecoder.ssa.TypeRef) BytecodeObjectTypeRef(de.mirkosertic.bytecoder.core.BytecodeObjectTypeRef) StringValue(de.mirkosertic.bytecoder.ssa.StringValue) ByteValue(de.mirkosertic.bytecoder.ssa.ByteValue) Value(de.mirkosertic.bytecoder.ssa.Value) ClassReferenceValue(de.mirkosertic.bytecoder.ssa.ClassReferenceValue) FloatValue(de.mirkosertic.bytecoder.ssa.FloatValue) NullValue(de.mirkosertic.bytecoder.ssa.NullValue) DoubleValue(de.mirkosertic.bytecoder.ssa.DoubleValue) IntegerValue(de.mirkosertic.bytecoder.ssa.IntegerValue) LongValue(de.mirkosertic.bytecoder.ssa.LongValue) ShortValue(de.mirkosertic.bytecoder.ssa.ShortValue)

Aggregations

Value (de.mirkosertic.bytecoder.ssa.Value)34 DoubleValue (de.mirkosertic.bytecoder.ssa.DoubleValue)32 FloatValue (de.mirkosertic.bytecoder.ssa.FloatValue)32 IntegerValue (de.mirkosertic.bytecoder.ssa.IntegerValue)32 LongValue (de.mirkosertic.bytecoder.ssa.LongValue)32 ByteValue (de.mirkosertic.bytecoder.ssa.ByteValue)26 ClassReferenceValue (de.mirkosertic.bytecoder.ssa.ClassReferenceValue)26 NullValue (de.mirkosertic.bytecoder.ssa.NullValue)26 ShortValue (de.mirkosertic.bytecoder.ssa.ShortValue)26 StringValue (de.mirkosertic.bytecoder.ssa.StringValue)26 MethodParameterValue (de.mirkosertic.bytecoder.ssa.MethodParameterValue)13 SelfReferenceParameterValue (de.mirkosertic.bytecoder.ssa.SelfReferenceParameterValue)13 BytecodeTypeRef (de.mirkosertic.bytecoder.core.BytecodeTypeRef)6 BytecodeMethodSignature (de.mirkosertic.bytecoder.core.BytecodeMethodSignature)5 Variable (de.mirkosertic.bytecoder.ssa.Variable)5 BytecodeLinkedClass (de.mirkosertic.bytecoder.core.BytecodeLinkedClass)4 ExpressionList (de.mirkosertic.bytecoder.ssa.ExpressionList)4 BytecodeFieldRefConstant (de.mirkosertic.bytecoder.core.BytecodeFieldRefConstant)3 BytecodeObjectTypeRef (de.mirkosertic.bytecoder.core.BytecodeObjectTypeRef)3 DirectInvokeMethodExpression (de.mirkosertic.bytecoder.ssa.DirectInvokeMethodExpression)3