Search in sources :

Example 6 with GuardExpression

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

the class SpecializationGroup method combine.

private static SpecializationGroup combine(List<SpecializationGroup> groups) {
    if (groups.isEmpty()) {
        throw new IllegalArgumentException("empty combinations");
    }
    if (groups.size() == 1) {
        return null;
    }
    List<TypeGuard> typeGuardsMatches = new ArrayList<>();
    List<GuardExpression> guardMatches = new ArrayList<>();
    SpecializationGroup first = groups.get(0);
    List<SpecializationGroup> others = groups.subList(1, groups.size());
    outer: for (TypeGuard typeGuard : first.typeGuards) {
        for (SpecializationGroup other : others) {
            if (!other.typeGuards.contains(typeGuard)) {
                // type guards can be combined unordered
                continue outer;
            }
        }
        typeGuardsMatches.add(typeGuard);
    }
    outer: for (GuardExpression guard : first.guards) {
        for (SpecializationGroup other : others) {
            if (!other.guards.contains(guard)) {
                // we must break here. One guard may depend on the other.
                break outer;
            }
        }
        guardMatches.add(guard);
    }
    // check for guards for required type casts
    for (Iterator<GuardExpression> iterator = guardMatches.iterator(); iterator.hasNext(); ) {
        GuardExpression guardMatch = iterator.next();
        if (!guardMatch.getExpression().findBoundVariables().isEmpty()) {
            iterator.remove();
        }
    // TODO we need to be smarter here with bound parameters.
    }
    if (typeGuardsMatches.isEmpty() && guardMatches.isEmpty()) {
        return null;
    }
    for (SpecializationGroup group : groups) {
        group.typeGuards.removeAll(typeGuardsMatches);
        group.guards.removeAll(guardMatches);
    }
    List<SpecializationGroup> newChildren = new ArrayList<>(groups);
    return new SpecializationGroup(newChildren, typeGuardsMatches, guardMatches);
}
Also used : GuardExpression(com.oracle.truffle.dsl.processor.model.GuardExpression) ArrayList(java.util.ArrayList)

Example 7 with GuardExpression

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

the class NodeParser method initializeGuards.

private void initializeGuards(SpecializationData specialization, DSLExpressionResolver resolver) {
    final TypeMirror booleanType = context.getType(boolean.class);
    List<String> guardDefinitions = ElementUtils.getAnnotationValueList(String.class, specialization.getMarkerAnnotation(), "guards");
    List<GuardExpression> guardExpressions = new ArrayList<>();
    for (String guard : guardDefinitions) {
        GuardExpression guardExpression;
        DSLExpression expression = null;
        try {
            expression = DSLExpression.parse(guard);
            expression.accept(resolver);
            guardExpression = new GuardExpression(specialization, expression);
            if (!ElementUtils.typeEquals(expression.getResolvedType(), booleanType)) {
                guardExpression.addError("Incompatible return type %s. Guards must return %s.", ElementUtils.getSimpleName(expression.getResolvedType()), ElementUtils.getSimpleName(booleanType));
            }
        } catch (InvalidExpressionException e) {
            guardExpression = new GuardExpression(specialization, null);
            guardExpression.addError("Error parsing expression '%s': %s", guard, e.getMessage());
        }
        guardExpressions.add(guardExpression);
    }
    specialization.setGuards(guardExpressions);
}
Also used : ArrayCodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) InvalidExpressionException(com.oracle.truffle.dsl.processor.expression.InvalidExpressionException) GuardExpression(com.oracle.truffle.dsl.processor.model.GuardExpression) ArrayList(java.util.ArrayList) DSLExpression(com.oracle.truffle.dsl.processor.expression.DSLExpression)

Example 8 with GuardExpression

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

the class SpecializationGroup method findNegatedGuardInPrevious.

private GuardExpression findNegatedGuardInPrevious(GuardExpression guard) {
    SpecializationGroup previous = this.getPreviousGroup();
    if (previous == null) {
        return null;
    }
    List<GuardExpression> elseConnectedGuards = previous.findElseConnectableGuards();
    if (previous == null || previous.getGuards().size() != elseConnectedGuards.size() + 1) {
        return null;
    }
    /* Guard is else branch can be connected in previous specialization. */
    if (elseConnectedGuards.contains(guard)) {
        return guard;
    }
    GuardExpression previousGuard = previous.getGuards().get(elseConnectedGuards.size());
    if (guard.equalsNegated(previousGuard)) {
        return guard;
    }
    return null;
}
Also used : GuardExpression(com.oracle.truffle.dsl.processor.model.GuardExpression)

Example 9 with GuardExpression

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

Aggregations

GuardExpression (com.oracle.truffle.dsl.processor.model.GuardExpression)9 DSLExpression (com.oracle.truffle.dsl.processor.expression.DSLExpression)5 ArrayList (java.util.ArrayList)5 CodeTreeBuilder (com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)3 SpecializationData (com.oracle.truffle.dsl.processor.model.SpecializationData)3 InvalidExpressionException (com.oracle.truffle.dsl.processor.expression.InvalidExpressionException)2 ArrayCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror)2 AssumptionExpression (com.oracle.truffle.dsl.processor.model.AssumptionExpression)2 Parameter (com.oracle.truffle.dsl.processor.model.Parameter)2 List (java.util.List)2 TypeMirror (javax.lang.model.type.TypeMirror)2 Assumption (com.oracle.truffle.api.Assumption)1 Cached (com.oracle.truffle.api.dsl.Cached)1 CreateCast (com.oracle.truffle.api.dsl.CreateCast)1 Executed (com.oracle.truffle.api.dsl.Executed)1 Fallback (com.oracle.truffle.api.dsl.Fallback)1 GenerateNodeFactory (com.oracle.truffle.api.dsl.GenerateNodeFactory)1 GeneratedBy (com.oracle.truffle.api.dsl.GeneratedBy)1 ImportStatic (com.oracle.truffle.api.dsl.ImportStatic)1 Introspectable (com.oracle.truffle.api.dsl.Introspectable)1