Search in sources :

Example 6 with TypeGuard

use of com.oracle.truffle.dsl.processor.parser.SpecializationGroup.TypeGuard in project graal by oracle.

the class FlatNodeGenFactory method initializeCasts.

private List<IfTriple> initializeCasts(FrameState frameState, SpecializationGroup group, DSLExpression expression, NodeExecutionMode specializationExecution) {
    Set<VariableElement> boundElements = expression.findBoundVariableElements();
    if (boundElements.isEmpty()) {
        return Collections.emptyList();
    }
    List<IfTriple> triples = new ArrayList<>();
    for (VariableElement variable : boundElements) {
        Parameter p = group.getSpecialization().findByVariable(variable);
        if (p != null) {
            NodeExecutionData execution = p.getSpecification().getExecution();
            if (execution != null) {
                LocalVariable var = frameState.getValue(execution);
                if (var == null) {
                    throw new AssertionError();
                }
                IfTriple triple = createTypeCheckOrCast(frameState, group, new TypeGuard(p.getType(), execution.getIndex()), specializationExecution, true, false);
                if (triple != null) {
                    triples.add(triple);
                }
            }
        }
    }
    return triples;
}
Also used : NodeExecutionData(com.oracle.truffle.dsl.processor.model.NodeExecutionData) ArrayList(java.util.ArrayList) Parameter(com.oracle.truffle.dsl.processor.model.Parameter) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) VariableElement(javax.lang.model.element.VariableElement) TypeGuard(com.oracle.truffle.dsl.processor.parser.SpecializationGroup.TypeGuard)

Example 7 with TypeGuard

use of com.oracle.truffle.dsl.processor.parser.SpecializationGroup.TypeGuard in project graal by oracle.

the class FlatNodeGenFactory method createFastPathExecuteChild.

private CodeTree createFastPathExecuteChild(final CodeTreeBuilder parent, FrameState originalFrameState, FrameState frameState, final ExecutableTypeData currentType, SpecializationGroup group, NodeExecutionData execution) {
    CodeTreeBuilder builder = parent.create();
    LocalVariable var = frameState.getValue(execution);
    if (var == null) {
        TypeMirror targetType;
        TypeGuard eliminatedGuard = null;
        if (boxingEliminationEnabled) {
            for (TypeGuard checkedGuard : group.getTypeGuards()) {
                if (!ElementUtils.isPrimitive(checkedGuard.getType())) {
                    // no elimination for non primitive types
                    continue;
                } else if (node.getChildExecutions().get(checkedGuard.getSignatureIndex()).getChild().findExecutableType(checkedGuard.getType()) == null) {
                    // type cannot be executed so it cannot be eliminated
                    continue;
                }
                if (checkedGuard.getSignatureIndex() == execution.getIndex()) {
                    eliminatedGuard = checkedGuard;
                    break;
                }
            }
        }
        if (eliminatedGuard != null) {
            // we can optimize the type guard away by executing it
            group.getTypeGuards().remove(eliminatedGuard);
            targetType = eliminatedGuard.getType();
        } else {
            targetType = execution.getChild().findAnyGenericExecutableType(context).getReturnType();
        }
        var = frameState.createValue(execution, targetType).nextName();
        LocalVariable fallbackVar;
        List<TypeMirror> originalSourceTypes = typeSystem.lookupSourceTypes(targetType);
        List<TypeMirror> sourceTypes = resolveOptimizedImplicitSourceTypes(execution, targetType);
        if (sourceTypes.size() > 1) {
            TypeGuard typeGuard = new TypeGuard(targetType, execution.getIndex());
            TypeMirror generic = node.getPolymorphicSpecialization().findParameterOrDie(execution).getType();
            fallbackVar = originalFrameState.createValue(execution, generic);
            // we want to create the check tree in reverse order
            Collections.reverse(sourceTypes);
            CodeTree access = var.createReference();
            boolean first = true;
            for (TypeMirror sType : sourceTypes) {
                if (ElementUtils.typeEquals(sType, targetType)) {
                    continue;
                }
                String localName = createSourceTypeLocalName(var, sType);
                builder.declaration(sType, localName, CodeTreeBuilder.createBuilder().defaultValue(sType).build());
                CodeTreeBuilder accessBuilder = builder.create();
                accessBuilder.startParantheses();
                accessBuilder.tree(state.createContainsOnly(frameState, originalSourceTypes.indexOf(sType), 1, new Object[] { typeGuard }, new Object[] { typeGuard }));
                accessBuilder.string(" && ");
                accessBuilder.tree(state.createIsNotAny(frameState, reachableSpecializationsArray));
                accessBuilder.string(" ? ");
                if (ElementUtils.isPrimitive(sType)) {
                    accessBuilder.string("(").type(generic).string(") ");
                }
                accessBuilder.string(localName);
                accessBuilder.string(" : ");
                if (first && ElementUtils.isPrimitive(targetType)) {
                    accessBuilder.string("(").type(generic).string(") ");
                }
                accessBuilder.tree(access);
                accessBuilder.end();
                access = accessBuilder.build();
                first = false;
            }
            fallbackVar = fallbackVar.accessWith(access);
        } else {
            fallbackVar = var;
        }
        builder.tree(createAssignExecuteChild(originalFrameState, frameState, builder, execution, currentType, var));
        frameState.setValue(execution, var);
        originalFrameState.setValue(execution, fallbackVar);
    }
    return builder.build();
}
Also used : DeclaredCodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.DeclaredCodeTypeMirror) ArrayCodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror) GeneratedTypeMirror(com.oracle.truffle.dsl.processor.java.model.GeneratedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) CodeTree(com.oracle.truffle.dsl.processor.java.model.CodeTree) ElementUtils.isObject(com.oracle.truffle.dsl.processor.java.ElementUtils.isObject) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder) TypeGuard(com.oracle.truffle.dsl.processor.parser.SpecializationGroup.TypeGuard)

Aggregations

TypeGuard (com.oracle.truffle.dsl.processor.parser.SpecializationGroup.TypeGuard)7 CodeTreeBuilder (com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)5 ArrayCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror)4 DeclaredCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.DeclaredCodeTypeMirror)4 GeneratedTypeMirror (com.oracle.truffle.dsl.processor.java.model.GeneratedTypeMirror)4 TypeMirror (javax.lang.model.type.TypeMirror)4 CodeTree (com.oracle.truffle.dsl.processor.java.model.CodeTree)3 Parameter (com.oracle.truffle.dsl.processor.model.Parameter)3 SpecializationData (com.oracle.truffle.dsl.processor.model.SpecializationData)3 ArrayList (java.util.ArrayList)3 ElementUtils.isObject (com.oracle.truffle.dsl.processor.java.ElementUtils.isObject)2 NodeExecutionData (com.oracle.truffle.dsl.processor.model.NodeExecutionData)2 List (java.util.List)2 DSLExpression (com.oracle.truffle.dsl.processor.expression.DSLExpression)1 CodeVariableElement (com.oracle.truffle.dsl.processor.java.model.CodeVariableElement)1 AssumptionExpression (com.oracle.truffle.dsl.processor.model.AssumptionExpression)1 ExecutableTypeData (com.oracle.truffle.dsl.processor.model.ExecutableTypeData)1 GuardExpression (com.oracle.truffle.dsl.processor.model.GuardExpression)1 ImplicitCastData (com.oracle.truffle.dsl.processor.model.ImplicitCastData)1 NodeChildData (com.oracle.truffle.dsl.processor.model.NodeChildData)1