Search in sources :

Example 21 with CodeTreeBuilder

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

the class FlatNodeGenFactory method createExecuteChildImplicitCast.

private ChildExecutionResult createExecuteChildImplicitCast(CodeTreeBuilder parent, FrameState originalFrameState, FrameState frameState, NodeExecutionData execution, LocalVariable target) {
    CodeTreeBuilder builder = parent.create();
    List<TypeMirror> originalSourceTypes = typeSystem.lookupSourceTypes(target.getTypeMirror());
    List<TypeMirror> sourceTypes = resolveOptimizedImplicitSourceTypes(execution, target.getTypeMirror());
    TypeGuard typeGuard = new TypeGuard(target.getTypeMirror(), execution.getIndex());
    boolean throwsUnexpected = false;
    boolean elseIf = false;
    for (TypeMirror sourceType : sourceTypes) {
        ExecutableTypeData executableType = resolveTargetExecutable(execution, sourceType);
        elseIf = builder.startIf(elseIf);
        throwsUnexpected |= executableType.hasUnexpectedValue(context);
        builder.startGroup();
        builder.tree(state.createContainsOnly(frameState, originalSourceTypes.indexOf(sourceType), 1, new Object[] { typeGuard }, new Object[] { typeGuard }));
        builder.string(" && ");
        builder.tree(state.createIsNotAny(frameState, reachableSpecializationsArray));
        builder.end();
        builder.end();
        builder.startBlock();
        CodeTree value = callChildExecuteMethod(execution, executableType, frameState);
        value = expect(executableType.getReturnType(), sourceType, value);
        throwsUnexpected |= needsCastTo(executableType.getReturnType(), sourceType);
        ImplicitCastData cast = typeSystem.lookupCast(sourceType, target.getTypeMirror());
        if (cast != null) {
            // we need to store the original value to restore it in
            // case of a deopt
            String localName = createSourceTypeLocalName(target, sourceType);
            builder.startStatement().string(localName).string(" = ").tree(value).end();
            value = callMethod(null, cast.getMethod(), CodeTreeBuilder.singleString(localName));
        }
        builder.startStatement().string(target.getName()).string(" = ").tree(value).end();
        builder.end();
    }
    if (elseIf) {
        builder.startElseBlock();
    }
    LocalVariable genericValue = target.makeGeneric(context).nextName();
    builder.tree(createAssignExecuteChild(originalFrameState, frameState, builder, execution, node.getGenericExecutableType(null), genericValue));
    builder.startStatement().string(target.getName()).string(" = ");
    CodeTree implicitState = state.createExtractInteger(frameState, typeGuard);
    builder.tree(TypeSystemCodeGenerator.implicitExpectFlat(typeSystem, target.getTypeMirror(), genericValue.createReference(), implicitState));
    builder.end();
    if (!sourceTypes.isEmpty()) {
        builder.end();
    }
    return new ChildExecutionResult(builder.build(), throwsUnexpected);
}
Also used : ImplicitCastData(com.oracle.truffle.dsl.processor.model.ImplicitCastData) 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) 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)

Example 22 with CodeTreeBuilder

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

the class FlatNodeGenFactory method createFastPath.

private CodeTree createFastPath(CodeTreeBuilder parent, List<SpecializationData> allSpecializations, SpecializationGroup originalGroup, final ExecutableTypeData currentType, FrameState frameState) {
    final CodeTreeBuilder builder = parent.create();
    builder.tree(state.createLoad(frameState));
    int sharedExecutes = 0;
    for (NodeExecutionData execution : node.getChildExecutions()) {
        boolean canExecuteChild = execution.getIndex() < currentType.getEvaluatedCount();
        for (TypeGuard checkedGuard : originalGroup.getTypeGuards()) {
            if (checkedGuard.getSignatureIndex() == execution.getIndex()) {
                canExecuteChild = true;
                break;
            }
        }
        if (!canExecuteChild) {
            break;
        }
        for (TypeGuard checkedGuard : originalGroup.getTypeGuards()) {
            // we cannot pull out guards that use optimized implicit source types
            if (resolveOptimizedImplicitSourceTypes(execution, checkedGuard.getType()).size() > 1) {
                canExecuteChild = false;
                break;
            }
        }
        if (!canExecuteChild) {
            break;
        }
        builder.tree(createFastPathExecuteChild(builder, frameState.copy(), frameState, currentType, originalGroup, execution));
        sharedExecutes++;
    }
    List<BoxingSplit> boxingSplits = parameterBoxingElimination(originalGroup, sharedExecutes);
    if (boxingSplits.isEmpty()) {
        builder.tree(executeFastPathGroup(builder, frameState, currentType, originalGroup, sharedExecutes, null));
        addExplodeLoop(builder, originalGroup);
    } else {
        FrameState originalFrameState = frameState.copy();
        boolean elseIf = false;
        for (BoxingSplit split : boxingSplits) {
            elseIf = builder.startIf(elseIf);
            List<SpecializationData> specializations = split.group.collectSpecializations();
            builder.startGroup();
            builder.tree(state.createContainsOnly(frameState, 0, -1, specializations.toArray(), allSpecializations.toArray())).end();
            builder.string(" && ");
            builder.tree(state.createIsNotAny(frameState, allSpecializations.toArray()));
            builder.end();
            builder.end().startBlock();
            builder.tree(wrapInAMethod(builder, split.group, originalFrameState, split.getName(), executeFastPathGroup(builder, frameState.copy(), currentType, split.group, sharedExecutes, specializations)));
            builder.end();
        }
        builder.startElseBlock();
        builder.tree(wrapInAMethod(builder, originalGroup, originalFrameState, "generic", executeFastPathGroup(builder, frameState, currentType, originalGroup, sharedExecutes, null)));
        builder.end();
    }
    return builder.build();
}
Also used : NodeExecutionData(com.oracle.truffle.dsl.processor.model.NodeExecutionData) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder) TypeGuard(com.oracle.truffle.dsl.processor.parser.SpecializationGroup.TypeGuard)

Example 23 with CodeTreeBuilder

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

the class FlatNodeGenFactory method createDuplicationCheck.

private CodeTree createDuplicationCheck(CodeTreeBuilder parent, FrameState frameState, SpecializationGroup group, List<GuardExpression> guardExpressions, boolean useDuplicate, String countName, String duplicateFoundName, String specializationLocalName) {
    SpecializationData specialization = group.getSpecialization();
    CodeTreeBuilder builder = parent.create();
    if (!useDuplicate) {
        builder.declaration("int", countName, CodeTreeBuilder.singleString("0"));
    }
    if (useSpecializationClass(specialization)) {
        builder.tree(loadSpecializationClass(frameState, specialization));
    }
    if (!specialization.hasMultipleInstances()) {
        builder.declaration("boolean", duplicateFoundName, CodeTreeBuilder.singleString("false"));
    }
    builder.startIf().tree(state.createContains(frameState, new Object[] { specialization })).end().startBlock();
    if (specialization.hasMultipleInstances()) {
        builder.startWhile().string(specializationLocalName, " != null").end().startBlock();
    }
    List<IfTriple> duplicationtriples = new ArrayList<>();
    duplicationtriples.addAll(createMethodGuardCheck(frameState, group, guardExpressions, NodeExecutionMode.FAST_PATH));
    duplicationtriples.addAll(createAssumptionCheckTriples(frameState, specialization));
    int duplicationIfCount = IfTriple.materialize(builder, IfTriple.optimize(duplicationtriples), false);
    if (useDuplicate) {
        builder.startStatement().string(duplicateFoundName, " = true").end();
    }
    if (specialization.hasMultipleInstances()) {
        builder.statement("break");
    }
    builder.end(duplicationIfCount);
    if (useDuplicate) {
    // no counting and next traversal necessary for duplication only check
    } else {
        if (specialization.getMaximumNumberOfInstances() > 1) {
            builder.startStatement().string(specializationLocalName, " = ", specializationLocalName, ".next_").end();
        } else {
            builder.statement(specializationLocalName + " = null");
        }
        builder.statement(countName + "++");
        builder.end();
    }
    builder.end();
    return builder.build();
}
Also used : SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) ArrayList(java.util.ArrayList) ElementUtils.isObject(com.oracle.truffle.dsl.processor.java.ElementUtils.isObject) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Example 24 with CodeTreeBuilder

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

the class OptionProcessor method createBuildOptionDescriptor.

private static CodeTree createBuildOptionDescriptor(ProcessorContext context, OptionInfo info) {
    CodeTreeBuilder builder = CodeTreeBuilder.createBuilder();
    builder.startStaticCall(context.getType(OptionDescriptor.class), "newBuilder");
    VariableElement var = info.field;
    builder.staticReference(var.getEnclosingElement().asType(), var.getSimpleName().toString());
    builder.doubleQuote(info.name);
    // newBuilder call
    builder.end();
    if (info.deprecated) {
        builder.startCall("", "deprecated").string("true").end();
    } else {
        builder.startCall("", "deprecated").string("false").end();
    }
    builder.startCall("", "help").doubleQuote(info.help).end();
    builder.startCall("", "category").staticReference(context.getType(OptionCategory.class), info.category.name()).end();
    builder.startCall("", "build").end();
    return builder.build();
}
Also used : OptionDescriptor(org.graalvm.options.OptionDescriptor) VariableElement(javax.lang.model.element.VariableElement) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Example 25 with CodeTreeBuilder

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

the class NodeCodeGenerator method createGetFactories.

private static ExecutableElement createGetFactories(ProcessorContext context, NodeData node) {
    List<NodeData> factoryList = node.getNodesWithFactories();
    if (node.needsFactory() && node.isGenerateFactory()) {
        factoryList.add(node);
    }
    if (factoryList.isEmpty()) {
        return null;
    }
    List<TypeMirror> nodeTypesList = new ArrayList<>();
    TypeMirror prev = null;
    boolean allSame = true;
    for (NodeData child : factoryList) {
        nodeTypesList.add(child.getNodeType());
        if (prev != null && !ElementUtils.typeEquals(child.getNodeType(), prev)) {
            allSame = false;
        }
        prev = child.getNodeType();
    }
    TypeMirror commonNodeSuperType = ElementUtils.getCommonSuperType(context, nodeTypesList);
    Types types = context.getEnvironment().getTypeUtils();
    TypeMirror factoryType = context.getType(NodeFactory.class);
    TypeMirror baseType;
    if (allSame) {
        baseType = ElementUtils.getDeclaredType(ElementUtils.fromTypeMirror(factoryType), commonNodeSuperType);
    } else {
        baseType = ElementUtils.getDeclaredType(ElementUtils.fromTypeMirror(factoryType), types.getWildcardType(commonNodeSuperType, null));
    }
    TypeMirror listType = ElementUtils.getDeclaredType(ElementUtils.fromTypeMirror(context.getType(List.class)), baseType);
    CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), listType, "getFactories");
    CodeTreeBuilder builder = method.createBuilder();
    builder.startReturn();
    if (factoryList.size() > 1) {
        builder.startStaticCall(context.getType(Arrays.class), "asList");
    } else {
        builder.startStaticCall(context.getType(Collections.class), "singletonList");
    }
    for (NodeData child : factoryList) {
        builder.startGroup();
        NodeData childNode = child;
        List<NodeData> factories = new ArrayList<>();
        while (childNode.getDeclaringNode() != null) {
            factories.add(childNode);
            childNode = childNode.getDeclaringNode();
        }
        Collections.reverse(factories);
        for (NodeData nodeData : factories) {
            builder.string(getAccessorClassName(nodeData)).string(".");
        }
        builder.string("getInstance()");
        builder.end();
    }
    builder.end();
    builder.end();
    return method;
}
Also used : Types(javax.lang.model.util.Types) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) GeneratedTypeMirror(com.oracle.truffle.dsl.processor.java.model.GeneratedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) ArrayList(java.util.ArrayList) Arrays(java.util.Arrays) Collections(java.util.Collections) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder) NodeData(com.oracle.truffle.dsl.processor.model.NodeData)

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