Search in sources :

Example 11 with CodeTreeBuilder

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

the class FlatNodeGenFactory method persistSpecializationClass.

private Collection<IfTriple> persistSpecializationClass(FrameState frameState, SpecializationData specialization) {
    if (!useSpecializationClass(specialization)) {
        return Collections.emptyList();
    }
    String localName = createSpecializationLocalName(specialization);
    LocalVariable var = frameState.get(localName);
    if (var == null) {
        // no specialization class initialized
        return Collections.emptyList();
    }
    String persistFrameState = createSpecializationClassPersisted(specialization);
    if (frameState.getBoolean(persistFrameState, false)) {
        // no specialization class initialized
        return Collections.emptyList();
    } else {
        frameState.setBoolean(persistFrameState, true);
    }
    CodeTree ref = var.createReference();
    CodeTreeBuilder builder = new CodeTreeBuilder(null);
    builder.startStatement();
    builder.string("this.", createSpecializationFieldName(specialization));
    builder.string(" = ");
    if (specializationClassIsNode(specialization)) {
        builder.startCall("super", "insert").tree(ref).end();
    } else {
        builder.tree(ref);
    }
    builder.end();
    return Arrays.asList(new IfTriple(builder.build(), null, null));
}
Also used : CodeTree(com.oracle.truffle.dsl.processor.java.model.CodeTree) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Example 12 with CodeTreeBuilder

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

the class FlatNodeGenFactory method createSpecialize.

private CodeTree createSpecialize(CodeTreeBuilder parent, FrameState frameState, SpecializationGroup group, SpecializationData specialization) {
    CodeTreeBuilder builder = parent.create();
    List<IfTriple> triples = new ArrayList<>();
    triples.addAll(initializeSpecializationClass(frameState, specialization));
    triples.addAll(initializeCaches(frameState, group, specialization.getCaches(), NodeExecutionMode.SLOW_PATH, false, true));
    triples.addAll(persistAssumptions(frameState, specialization));
    triples.addAll(persistSpecializationClass(frameState, specialization));
    builder.end(IfTriple.materialize(builder, triples, true));
    List<SpecializationData> excludesSpecializations = new ArrayList<>();
    for (SpecializationData otherSpeciailzation : reachableSpecializations) {
        if (otherSpeciailzation == specialization) {
            continue;
        }
        if (otherSpeciailzation.getExcludedBy().contains(specialization)) {
            excludesSpecializations.add(otherSpeciailzation);
        }
    }
    if (!excludesSpecializations.isEmpty()) {
        SpecializationData[] excludesArray = excludesSpecializations.toArray(new SpecializationData[0]);
        builder.tree(exclude.createSet(frameState, excludesArray, true, true));
        for (SpecializationData excludes : excludesArray) {
            if (useSpecializationClass(excludes)) {
                builder.statement("this." + createSpecializationFieldName(excludes) + " = null");
            }
        }
        builder.tree((state.createSet(frameState, excludesArray, false, false)));
    }
    CodeTree updateImplicitCast = createUpdateImplicitCastState(builder, frameState, specialization);
    if (updateImplicitCast != null) {
        builder.tree(createUpdateImplicitCastState(builder, frameState, specialization));
    }
    builder.tree(state.createSet(frameState, new SpecializationData[] { specialization }, true, true));
    return builder.build();
}
Also used : CodeTree(com.oracle.truffle.dsl.processor.java.model.CodeTree) ArrayList(java.util.ArrayList) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Example 13 with CodeTreeBuilder

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

the class FlatNodeGenFactory method loadSpecializationClass.

private CodeTree loadSpecializationClass(FrameState frameState, SpecializationData specialization) {
    if (!useSpecializationClass(specialization)) {
        return null;
    }
    String localName = createSpecializationLocalName(specialization);
    String typeName = createSpecializationTypeName(specialization);
    LocalVariable var = frameState.get(localName);
    CodeTreeBuilder builder = new CodeTreeBuilder(null);
    builder.startStatement();
    if (var == null) {
        builder.string(typeName);
        builder.string(" ");
    }
    builder.string(localName);
    builder.string(" = ");
    builder.tree(createSpecializationFieldReference(frameState, specialization, null));
    builder.end();
    if (var == null) {
        frameState.set(localName, new LocalVariable(new GeneratedTypeMirror("", typeName), localName, null));
    }
    return builder.build();
}
Also used : GeneratedTypeMirror(com.oracle.truffle.dsl.processor.java.model.GeneratedTypeMirror) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Example 14 with CodeTreeBuilder

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

the class FlatNodeGenFactory method createExecute.

private CodeExecutableElement createExecute(CodeTypeElement clazz, ExecutableTypeData type, List<ExecutableTypeData> delegateableTypes) {
    final List<SpecializationData> allSpecializations = reachableSpecializations;
    final List<SpecializationData> compatibleSpecializations = filterCompatibleSpecializations(type, allSpecializations);
    List<SpecializationData> implementedSpecializations;
    if (delegateableTypes.isEmpty()) {
        implementedSpecializations = compatibleSpecializations;
    } else {
        implementedSpecializations = filterImplementedSpecializations(type, compatibleSpecializations);
    }
    FrameState frameState = FrameState.load(this, type, Integer.MAX_VALUE);
    CodeExecutableElement method = createExecuteMethod(null, type, frameState, true);
    clazz.add(method);
    CodeTreeBuilder builder = method.createBuilder();
    // do I miss specializations that are reachable from this executable?
    if (compatibleSpecializations.size() != implementedSpecializations.size()) {
        ExecuteDelegationResult delegation = createExecuteDelegation(builder, frameState, type, delegateableTypes, compatibleSpecializations, implementedSpecializations);
        builder.tree(delegation.tree);
        if (!delegation.hasFallthrough) {
            return method;
        }
    }
    if (implementedSpecializations.isEmpty()) {
        implementedSpecializations = compatibleSpecializations;
    }
    if (implementedSpecializations.isEmpty()) {
        builder.tree(createTransferToInterpreterAndInvalidate());
        builder.startThrow().startNew(getType(AssertionError.class)).doubleQuote("Delegation failed.").end().end();
    } else {
        SpecializationGroup group = SpecializationGroup.create(implementedSpecializations);
        builder.tree(createFastPath(builder, implementedSpecializations, group, type, frameState));
    }
    return method;
}
Also used : CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) SpecializationGroup(com.oracle.truffle.dsl.processor.parser.SpecializationGroup) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Example 15 with CodeTreeBuilder

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

the class FlatNodeGenFactory method createAssignExecuteChild.

private CodeTree createAssignExecuteChild(FrameState originalFrameState, FrameState frameState, CodeTreeBuilder parent, NodeExecutionData execution, ExecutableTypeData forType, LocalVariable targetValue) {
    CodeTreeBuilder builder = parent.create();
    ChildExecutionResult executeChild = createExecuteChild(builder, originalFrameState, frameState, execution, targetValue);
    builder.tree(createTryExecuteChild(targetValue, executeChild.code, true, executeChild.throwsUnexpectedResult));
    builder.end();
    if (executeChild.throwsUnexpectedResult) {
        builder.startCatchBlock(getType(UnexpectedResultException.class), "ex");
        FrameState slowPathFrameState = originalFrameState.copy();
        slowPathFrameState.setValue(execution, targetValue.makeGeneric(context).accessWith(CodeTreeBuilder.singleString("ex.getResult()")));
        ExecutableTypeData delegateType = node.getGenericExecutableType(forType);
        boolean found = false;
        for (NodeExecutionData otherExecution : node.getChildExecutions()) {
            if (found) {
                LocalVariable childEvaluatedValue = slowPathFrameState.createValue(otherExecution, genericType);
                builder.tree(createAssignExecuteChild(slowPathFrameState.copy(), slowPathFrameState, builder, otherExecution, delegateType, childEvaluatedValue));
                slowPathFrameState.setValue(otherExecution, childEvaluatedValue);
            } else {
                // skip forward already evaluated
                found = execution == otherExecution;
            }
        }
        builder.tree(createCallExecuteAndSpecialize(forType, slowPathFrameState));
        builder.end();
    }
    return builder.build();
}
Also used : ExecutableTypeData(com.oracle.truffle.dsl.processor.model.ExecutableTypeData) UnexpectedResultException(com.oracle.truffle.api.nodes.UnexpectedResultException) NodeExecutionData(com.oracle.truffle.dsl.processor.model.NodeExecutionData) 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