Search in sources :

Example 26 with CodeTree

use of com.oracle.truffle.dsl.processor.java.model.CodeTree 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)

Example 27 with CodeTree

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

the class FlatNodeGenFactory method persistAndInitializeCache.

private Collection<IfTriple> persistAndInitializeCache(FrameState frameState, SpecializationData specialization, CacheExpression cache, boolean store, boolean persist) {
    List<IfTriple> triples = new ArrayList<>();
    CodeTree init = initializeCache(frameState, specialization, cache);
    if (store) {
        triples.addAll(storeCache(frameState, specialization, cache, init));
    }
    if (persist) {
        triples.addAll(persistCache(frameState, specialization, cache, init));
    }
    return triples;
}
Also used : CodeTree(com.oracle.truffle.dsl.processor.java.model.CodeTree) ArrayList(java.util.ArrayList)

Example 28 with CodeTree

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

the class FlatNodeGenFactory method createMethodGuardCheck.

private IfTriple createMethodGuardCheck(FrameState frameState, SpecializationData specialization, GuardExpression guard, NodeExecutionMode mode) {
    DSLExpression expression = guard.getExpression();
    Map<Variable, CodeTree> resolvedBindings = castBoundTypes(bindExpressionValues(frameState, expression, specialization));
    CodeTree init = null;
    CodeTree expressionCode = DSLExpressionGenerator.write(expression, null, resolvedBindings);
    if (mode.isGuardFallback()) {
        GuardExpression guardWithBit = getGuardThatNeedsStateBit(specialization, guard);
        if (guardWithBit != null) {
            CodeTreeBuilder builder = new CodeTreeBuilder(null);
            builder.string("(");
            builder.tree(state.createNotContains(frameState, new Object[] { guardWithBit }));
            builder.string(" || ");
            builder.tree(expressionCode);
            builder.string(")");
            expressionCode = builder.build();
            fallbackNeedsState = true;
        }
    }
    // overrule with assertion
    CodeTree assertion = null;
    if (mode.isFastPath() || mode.isGuardFallback()) {
        if (!specialization.isDynamicParameterBound(expression)) {
            assertion = CodeTreeBuilder.createBuilder().startAssert().tree(expressionCode).end().build();
            expressionCode = null;
        }
    } else {
        if (guard.isConstantTrueInSlowPath(context)) {
            assertion = CodeTreeBuilder.createBuilder().startStatement().string("// assert ").tree(expressionCode).end().build();
            expressionCode = null;
        }
    }
    return new IfTriple(init, expressionCode, assertion);
}
Also used : Variable(com.oracle.truffle.dsl.processor.expression.DSLExpression.Variable) CodeTree(com.oracle.truffle.dsl.processor.java.model.CodeTree) GuardExpression(com.oracle.truffle.dsl.processor.model.GuardExpression) ElementUtils.isObject(com.oracle.truffle.dsl.processor.java.ElementUtils.isObject) DSLExpression(com.oracle.truffle.dsl.processor.expression.DSLExpression) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Example 29 with CodeTree

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

the class FlatNodeGenFactory method createCallExecute.

private CodeTree createCallExecute(ExecutableTypeData forType, ExecutableTypeData targetType, FrameState frameState) {
    TypeMirror returnType = targetType.getReturnType();
    List<CodeTree> bindings = new ArrayList<>();
    List<TypeMirror> sourceTypes = forType.getSignatureParameters();
    List<TypeMirror> targetTypes = targetType.getSignatureParameters();
    if (sourceTypes.size() != targetTypes.size()) {
        throw new IllegalArgumentException();
    }
    if (targetType.getFrameParameter() != null) {
        LocalVariable parameterLocal = frameState.get(FRAME_VALUE);
        TypeMirror parameterTargetType = targetType.getFrameParameter();
        if (parameterLocal == null) {
            bindings.add(CodeTreeBuilder.createBuilder().defaultValue(parameterTargetType).build());
        } else {
            bindings.add(parameterLocal.createReference());
        }
    }
    for (int i = 0; i < sourceTypes.size(); i++) {
        LocalVariable parameterLocal = frameState.getValue(i);
        TypeMirror parameterTargetType = targetTypes.get(i);
        if (parameterLocal == null) {
            bindings.add(CodeTreeBuilder.createBuilder().defaultValue(parameterTargetType).build());
        } else {
            bindings.add(parameterLocal.createReference());
        }
    }
    CodeTree call = callMethod(null, targetType.getMethod(), bindings.toArray(new CodeTree[0]));
    CodeTreeBuilder builder = CodeTreeBuilder.createBuilder();
    builder = builder.create();
    if (isVoid(forType.getReturnType())) {
        builder.statement(call);
        builder.returnStatement();
    } else {
        builder.startReturn();
        builder.tree(expectOrCast(returnType, forType, call));
        builder.end();
    }
    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) ArrayList(java.util.ArrayList) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Example 30 with CodeTree

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

the class FlatNodeGenFactory method castBoundTypes.

private static Map<Variable, CodeTree> castBoundTypes(Map<Variable, LocalVariable> bindings) {
    Map<Variable, CodeTree> resolvedBindings = new HashMap<>();
    for (Variable variable : bindings.keySet()) {
        LocalVariable localVariable = bindings.get(variable);
        CodeTree resolved = localVariable.createReference();
        TypeMirror sourceType = localVariable.getTypeMirror();
        TypeMirror targetType = variable.getResolvedTargetType();
        if (targetType == null) {
            targetType = variable.getResolvedType();
        }
        if (!ElementUtils.isAssignable(sourceType, targetType)) {
            resolved = CodeTreeBuilder.createBuilder().startParantheses().cast(targetType, resolved).end().build();
        }
        resolvedBindings.put(variable, resolved);
    }
    return resolvedBindings;
}
Also used : Variable(com.oracle.truffle.dsl.processor.expression.DSLExpression.Variable) CodeTree(com.oracle.truffle.dsl.processor.java.model.CodeTree) HashMap(java.util.HashMap) 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)

Aggregations

CodeTree (com.oracle.truffle.dsl.processor.java.model.CodeTree)34 CodeTreeBuilder (com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)21 ArrayCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror)14 GeneratedTypeMirror (com.oracle.truffle.dsl.processor.java.model.GeneratedTypeMirror)14 TypeMirror (javax.lang.model.type.TypeMirror)14 DeclaredCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.DeclaredCodeTypeMirror)13 ArrayList (java.util.ArrayList)13 CodeExecutableElement (com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement)6 SpecializationData (com.oracle.truffle.dsl.processor.model.SpecializationData)6 CodeVariableElement (com.oracle.truffle.dsl.processor.java.model.CodeVariableElement)5 NodeExecutionData (com.oracle.truffle.dsl.processor.model.NodeExecutionData)5 VariableElement (javax.lang.model.element.VariableElement)5 Parameter (com.oracle.truffle.dsl.processor.model.Parameter)4 Variable (com.oracle.truffle.dsl.processor.expression.DSLExpression.Variable)3 ElementUtils.isObject (com.oracle.truffle.dsl.processor.java.ElementUtils.isObject)3 SpecializationGroup (com.oracle.truffle.dsl.processor.parser.SpecializationGroup)3 TypeGuard (com.oracle.truffle.dsl.processor.parser.SpecializationGroup.TypeGuard)3 ExecutableElement (javax.lang.model.element.ExecutableElement)3 DSLExpression (com.oracle.truffle.dsl.processor.expression.DSLExpression)2 CodeTypeElement (com.oracle.truffle.dsl.processor.java.model.CodeTypeElement)2