Search in sources :

Example 11 with ExecutableTypeData

use of com.oracle.truffle.dsl.processor.model.ExecutableTypeData in project graal by oracle.

the class FlatNodeGenFactory method filterExecutableTypes.

private List<ExecutableTypeData> filterExecutableTypes(List<ExecutableTypeData> executableTypes, List<SpecializationData> specializations) {
    Set<TypeMirror> specializedReturnTypes = new HashSet<>();
    for (SpecializationData specialization : specializations) {
        specializedReturnTypes.add(specialization.getReturnType().getType());
    }
    List<ExecutableTypeData> filteredTypes = new ArrayList<>();
    outer: for (ExecutableTypeData executable : executableTypes) {
        if (executable.getMethod() == null) {
            continue;
        }
        if (executable.isAbstract()) {
            filteredTypes.add(executable);
            continue;
        }
        if (executable.isFinal()) {
            // no way to implement that
            continue;
        }
        if (!executable.hasUnexpectedValue(context)) {
            filteredTypes.add(executable);
            continue;
        } else {
            TypeMirror returnType = executable.getReturnType();
            if (boxingEliminationEnabled && (isVoid(returnType) || ElementUtils.isPrimitive(returnType))) {
                for (TypeMirror specializedReturnType : specializedReturnTypes) {
                    if (isSubtypeBoxed(context, specializedReturnType, returnType)) {
                        filteredTypes.add(executable);
                        continue outer;
                    }
                }
            }
        }
    }
    Collections.sort(filteredTypes);
    return filteredTypes;
}
Also used : ExecutableTypeData(com.oracle.truffle.dsl.processor.model.ExecutableTypeData) 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) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 12 with ExecutableTypeData

use of com.oracle.truffle.dsl.processor.model.ExecutableTypeData in project graal by oracle.

the class FlatNodeGenFactory method createExecuteDelegation.

private ExecuteDelegationResult createExecuteDelegation(CodeTreeBuilder parent, FrameState frameState, ExecutableTypeData type, List<ExecutableTypeData> delegateableTypes, final List<SpecializationData> compatibleSpecializations, List<SpecializationData> implementedSpecializations) {
    CodeTreeBuilder builder = parent.create();
    List<SpecializationData> notImplemented = new ArrayList<>(compatibleSpecializations);
    for (SpecializationData specialization : implementedSpecializations) {
        notImplemented.remove(specialization);
    }
    if (notImplemented.isEmpty()) {
        throw new AssertionError();
    }
    List<ExecutableTypeData> compatibleDelegateTypes = filterCompatibleExecutableTypes(type, delegateableTypes);
    List<ExecutableTypeData> delegatedDelegateTypes = new ArrayList<>();
    CodeTreeBuilder delegateBuilder = builder.create();
    boolean elseIf = false;
    boolean coversAllSpecializations = false;
    if (boxingEliminationEnabled) {
        Set<TypeMirror> optimizeTypes = new HashSet<>();
        for (SpecializationData specialization : reachableSpecializations) {
            TypeMirror returnType = specialization.getReturnType().getType();
            if (ElementUtils.isPrimitive(returnType)) {
                optimizeTypes.add(returnType);
            }
        }
        for (TypeMirror optimizedType : ElementUtils.uniqueSortedTypes(optimizeTypes, true)) {
            ExecutableTypeData delegateType = null;
            for (ExecutableTypeData compatibleType : compatibleDelegateTypes) {
                if (ElementUtils.typeEquals(compatibleType.getReturnType(), optimizedType)) {
                    delegateType = compatibleType;
                    break;
                }
            }
            if (delegateType != null) {
                List<SpecializationData> delegateSpecializations = filterImplementedSpecializations(delegateType, filterCompatibleSpecializations(delegateType, reachableSpecializations));
                coversAllSpecializations = delegateSpecializations.size() == reachableSpecializations.size();
                if (!coversAllSpecializations) {
                    builder.tree(state.createLoad(frameState));
                    elseIf = delegateBuilder.startIf(elseIf);
                    delegateBuilder.startGroup();
                    delegateBuilder.tree(state.createContainsOnly(frameState, 0, -1, delegateSpecializations.toArray(), reachableSpecializationsArray)).end();
                    delegateBuilder.string(" && ");
                    delegateBuilder.tree(state.createIsNotAny(frameState, reachableSpecializationsArray));
                    delegateBuilder.end();
                    delegateBuilder.end();
                    delegateBuilder.startBlock();
                }
                delegatedDelegateTypes.add(delegateType);
                delegateBuilder.tree(createCallExecute(type, delegateType, frameState));
                if (!coversAllSpecializations) {
                    delegateBuilder.end();
                }
                if (coversAllSpecializations) {
                    break;
                }
            }
        }
    }
    if (!compatibleDelegateTypes.isEmpty() && !coversAllSpecializations) {
        ExecutableTypeData delegateType = compatibleDelegateTypes.get(0);
        coversAllSpecializations = notImplemented.size() == reachableSpecializations.size();
        if (!coversAllSpecializations) {
            builder.tree(state.createLoad(frameState));
            elseIf = delegateBuilder.startIf(elseIf);
            delegateBuilder.tree(state.createContains(frameState, notImplemented.toArray())).end();
            delegateBuilder.startBlock();
        }
        delegatedDelegateTypes.add(delegateType);
        delegateBuilder.tree(createCallExecute(type, delegateType, frameState));
        if (!coversAllSpecializations) {
            delegateBuilder.end();
        }
    }
    boolean hasUnexpected = false;
    for (ExecutableTypeData delegateType : delegatedDelegateTypes) {
        if (needsUnexpectedResultException(delegateType)) {
            hasUnexpected = true;
            break;
        }
    }
    if (hasUnexpected) {
        builder.startTryBlock();
        builder.tree(delegateBuilder.build());
        builder.end().startCatchBlock(context.getType(UnexpectedResultException.class), "ex");
        if (isVoid(type.getReturnType())) {
            builder.returnStatement();
        } else {
            builder.startReturn();
            builder.tree(expectOrCast(getType(Object.class), type, CodeTreeBuilder.singleString("ex")));
            builder.end();
        }
        builder.end();
    } else {
        builder.tree(delegateBuilder.build());
    }
    return new ExecuteDelegationResult(builder.build(), !coversAllSpecializations);
}
Also used : ExecutableTypeData(com.oracle.truffle.dsl.processor.model.ExecutableTypeData) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) ArrayList(java.util.ArrayList) UnexpectedResultException(com.oracle.truffle.api.nodes.UnexpectedResultException) 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) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 13 with ExecutableTypeData

use of com.oracle.truffle.dsl.processor.model.ExecutableTypeData in project graal by oracle.

the class FlatNodeGenFactory method resolveTargetExecutable.

private ExecutableTypeData resolveTargetExecutable(NodeExecutionData execution, TypeMirror target) {
    NodeChildData child = execution.getChild();
    if (child == null) {
        return null;
    }
    ExecutableTypeData targetExecutable = child.findExecutableType(target);
    if (targetExecutable == null) {
        targetExecutable = child.findAnyGenericExecutableType(context);
    }
    return targetExecutable;
}
Also used : ExecutableTypeData(com.oracle.truffle.dsl.processor.model.ExecutableTypeData) NodeChildData(com.oracle.truffle.dsl.processor.model.NodeChildData)

Example 14 with ExecutableTypeData

use of com.oracle.truffle.dsl.processor.model.ExecutableTypeData in project graal by oracle.

the class FlatNodeGenFactory method createExecuteAndSpecializeType.

private ExecutableTypeData createExecuteAndSpecializeType() {
    SpecializationData polymorphicSpecialization = node.getPolymorphicSpecialization();
    TypeMirror polymorphicType = polymorphicSpecialization.getReturnType().getType();
    List<TypeMirror> parameters = new ArrayList<>();
    for (Parameter param : polymorphicSpecialization.getSignatureParameters()) {
        parameters.add(param.getType());
    }
    return new ExecutableTypeData(node, polymorphicType, "executeAndSpecialize", node.getFrameType(), parameters);
}
Also used : ExecutableTypeData(com.oracle.truffle.dsl.processor.model.ExecutableTypeData) 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) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) ArrayList(java.util.ArrayList) Parameter(com.oracle.truffle.dsl.processor.model.Parameter)

Example 15 with ExecutableTypeData

use of com.oracle.truffle.dsl.processor.model.ExecutableTypeData in project graal by oracle.

the class FlatNodeGenFactory method createFallbackGuard.

private Element createFallbackGuard() {
    boolean frameUsed = false;
    FrameState frameState = FrameState.load(this);
    List<SpecializationData> specializations = new ArrayList<>(reachableSpecializations);
    for (ListIterator<SpecializationData> iterator = specializations.listIterator(); iterator.hasNext(); ) {
        SpecializationData specialization = iterator.next();
        if (specialization.isFallback()) {
            iterator.remove();
        } else if (!specialization.isReachesFallback()) {
            iterator.remove();
        } else {
            if (specialization.isFrameUsedByGuard()) {
                frameUsed = true;
            }
        }
    }
    SpecializationGroup group = SpecializationGroup.create(specializations);
    ExecutableTypeData executableType = node.findAnyGenericExecutableType(context, -1);
    if (!frameUsed) {
        frameState.removeValue(FRAME_VALUE);
    }
    fallbackNeedsState = false;
    fallbackNeedsFrame = frameUsed;
    state.createLoad(frameState);
    CodeExecutableElement method = frameState.createMethod(modifiers(PRIVATE), getType(boolean.class), METHOD_FALLBACK_GUARD, FRAME_VALUE, STATE_VALUE);
    CodeTree result = visitSpecializationGroup(CodeTreeBuilder.createBuilder(), group, executableType, frameState, null, NodeExecutionMode.FALLBACK_GUARD);
    if (!fallbackNeedsState) {
        VariableElement toRemove = null;
        for (VariableElement v : method.getParameters()) {
            if (v.getSimpleName().toString().equals(STATE_VALUE)) {
                toRemove = v;
                break;
            }
        }
        if (toRemove != null) {
            method.getParameters().remove(toRemove);
        }
    }
    final CodeTreeBuilder builder = method.createBuilder();
    for (SpecializationData implemented : specializations) {
        if (implemented.getMaximumNumberOfInstances() > 1) {
            method.getAnnotationMirrors().add(createExplodeLoop());
            break;
        }
    }
    builder.tree(result);
    builder.returnTrue();
    if (!accessesCachedState(specializations)) {
        method.getModifiers().add(STATIC);
    }
    return method;
}
Also used : ExecutableTypeData(com.oracle.truffle.dsl.processor.model.ExecutableTypeData) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) CodeTree(com.oracle.truffle.dsl.processor.java.model.CodeTree) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) ArrayList(java.util.ArrayList) SpecializationGroup(com.oracle.truffle.dsl.processor.parser.SpecializationGroup) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) VariableElement(javax.lang.model.element.VariableElement) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Aggregations

ExecutableTypeData (com.oracle.truffle.dsl.processor.model.ExecutableTypeData)16 ArrayList (java.util.ArrayList)11 ArrayCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror)10 TypeMirror (javax.lang.model.type.TypeMirror)10 DeclaredCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.DeclaredCodeTypeMirror)6 GeneratedTypeMirror (com.oracle.truffle.dsl.processor.java.model.GeneratedTypeMirror)6 SpecializationData (com.oracle.truffle.dsl.processor.model.SpecializationData)6 CodeTreeBuilder (com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)5 NodeChildData (com.oracle.truffle.dsl.processor.model.NodeChildData)4 NodeExecutionData (com.oracle.truffle.dsl.processor.model.NodeExecutionData)4 CodeExecutableElement (com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement)3 HashSet (java.util.HashSet)3 UnexpectedResultException (com.oracle.truffle.api.nodes.UnexpectedResultException)2 CodeTree (com.oracle.truffle.dsl.processor.java.model.CodeTree)2 CodeVariableElement (com.oracle.truffle.dsl.processor.java.model.CodeVariableElement)2 LinkedHashSet (java.util.LinkedHashSet)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 Specialization (com.oracle.truffle.api.dsl.Specialization)1 Child (com.oracle.truffle.api.nodes.Node.Child)1 NodeInfo (com.oracle.truffle.api.nodes.NodeInfo)1