Search in sources :

Example 26 with CodeTreeBuilder

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

the class NodeFactoryFactory method createCreateGetNodeSignatures.

private CodeExecutableElement createCreateGetNodeSignatures() {
    TypeMirror returnValue = ElementUtils.getDeclaredType(ElementUtils.fromTypeMirror(context.getType(List.class)));
    CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC), returnValue, "getNodeSignatures");
    CodeTreeBuilder builder = method.createBuilder();
    builder.startReturn();
    builder.startGroup();
    builder.startStaticCall(context.getType(Arrays.class), "asList");
    List<ExecutableElement> constructors = GeneratorUtils.findUserConstructors(createdFactoryElement.asType());
    for (ExecutableElement constructor : constructors) {
        builder.startGroup();
        builder.startStaticCall(context.getType(Arrays.class), "asList");
        for (VariableElement var : constructor.getParameters()) {
            builder.typeLiteral(var.asType());
        }
        builder.end();
        builder.end();
    }
    builder.end();
    builder.end();
    builder.end();
    return method;
}
Also used : CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) TypeMirror(javax.lang.model.type.TypeMirror) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) Arrays(java.util.Arrays) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Example 27 with CodeTreeBuilder

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

the class NodeFactoryFactory method createGetInstanceMethod.

private ExecutableElement createGetInstanceMethod(Modifier visibility) {
    TypeElement nodeFactoryType = ElementUtils.fromTypeMirror(context.getType(NodeFactory.class));
    TypeMirror returnType = ElementUtils.getDeclaredType(nodeFactoryType, node.getNodeType());
    CodeExecutableElement method = new CodeExecutableElement(modifiers(), returnType, "getInstance");
    if (visibility != null) {
        method.getModifiers().add(visibility);
    }
    method.getModifiers().add(Modifier.STATIC);
    String varName = instanceVarName(node);
    CodeTreeBuilder builder = method.createBuilder();
    builder.startIf();
    builder.string(varName).string(" == null");
    builder.end().startBlock();
    builder.startStatement();
    builder.string(varName);
    builder.string(" = ");
    builder.startNew(factoryClassName(node)).end();
    builder.end();
    builder.end();
    builder.startReturn().string(varName).end();
    return method;
}
Also used : CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) NodeFactory(com.oracle.truffle.api.dsl.NodeFactory) TypeMirror(javax.lang.model.type.TypeMirror) CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) TypeElement(javax.lang.model.element.TypeElement) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Example 28 with CodeTreeBuilder

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

the class TypeSystemCodeGenerator method createExpectMethod.

static CodeExecutableElement createExpectMethod(Modifier visibility, TypeSystemData typeSystem, TypeMirror sourceTypeOriginal, TypeMirror expectedTypeOriginal) {
    TypeMirror expectedType = ElementUtils.fillInGenericWildcards(expectedTypeOriginal);
    TypeMirror sourceType = ElementUtils.fillInGenericWildcards(sourceTypeOriginal);
    if (ElementUtils.isObject(expectedType) || ElementUtils.isVoid(expectedType)) {
        return null;
    }
    CodeExecutableElement method = new CodeExecutableElement(modifiers(STATIC), expectedType, TypeSystemCodeGenerator.expectTypeMethodName(typeSystem, expectedType));
    method.setVisibility(visibility);
    method.addParameter(new CodeVariableElement(sourceType, LOCAL_VALUE));
    method.addThrownType(typeSystem.getContext().getTruffleTypes().getUnexpectedValueException());
    CodeTreeBuilder body = method.createBuilder();
    body.startIf().tree(check(typeSystem, expectedType, LOCAL_VALUE)).end().startBlock();
    body.startReturn().tree(cast(typeSystem, expectedType, LOCAL_VALUE)).end();
    body.end();
    body.startThrow().startNew(typeSystem.getContext().getTruffleTypes().getUnexpectedValueException()).string(LOCAL_VALUE).end().end();
    return method;
}
Also used : CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) CodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror) GeneratedTypeMirror(com.oracle.truffle.dsl.processor.java.model.GeneratedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Example 29 with CodeTreeBuilder

use of com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder 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 30 with CodeTreeBuilder

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

the class FlatNodeGenFactory method createFastPathExecute.

private CodeTree createFastPathExecute(CodeTreeBuilder parent, final ExecutableTypeData forType, SpecializationData specialization, FrameState frameState) {
    CodeTreeBuilder builder = parent.create();
    int ifCount = 0;
    if (specialization.isFallback()) {
        builder.startIf().startCall(METHOD_FALLBACK_GUARD);
        if (fallbackNeedsFrame) {
            if (frameState.get(FRAME_VALUE) != null) {
                builder.string(FRAME_VALUE);
            } else {
                builder.nullLiteral();
            }
        }
        if (fallbackNeedsState) {
            builder.string(STATE_VALUE);
        }
        frameState.addReferencesTo(builder);
        builder.end();
        builder.end();
        builder.startBlock();
        ifCount++;
    }
    builder.tree(createExecute(builder, frameState, forType, specialization, NodeExecutionMode.FAST_PATH));
    builder.end(ifCount);
    return builder.build();
}
Also used : CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Aggregations

CodeTreeBuilder (com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)64 CodeExecutableElement (com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement)23 TypeMirror (javax.lang.model.type.TypeMirror)23 CodeTree (com.oracle.truffle.dsl.processor.java.model.CodeTree)21 GeneratedTypeMirror (com.oracle.truffle.dsl.processor.java.model.GeneratedTypeMirror)20 ArrayList (java.util.ArrayList)18 ArrayCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror)17 DeclaredCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.DeclaredCodeTypeMirror)16 CodeVariableElement (com.oracle.truffle.dsl.processor.java.model.CodeVariableElement)13 SpecializationData (com.oracle.truffle.dsl.processor.model.SpecializationData)12 NodeExecutionData (com.oracle.truffle.dsl.processor.model.NodeExecutionData)8 VariableElement (javax.lang.model.element.VariableElement)8 ElementUtils.isObject (com.oracle.truffle.dsl.processor.java.ElementUtils.isObject)7 ExecutableElement (javax.lang.model.element.ExecutableElement)7 CodeTypeElement (com.oracle.truffle.dsl.processor.java.model.CodeTypeElement)5 ExecutableTypeData (com.oracle.truffle.dsl.processor.model.ExecutableTypeData)5 Arrays (java.util.Arrays)5 UnexpectedResultException (com.oracle.truffle.api.nodes.UnexpectedResultException)4 AssumptionExpression (com.oracle.truffle.dsl.processor.model.AssumptionExpression)4 SpecializationGroup (com.oracle.truffle.dsl.processor.parser.SpecializationGroup)4