Search in sources :

Example 1 with Variable

use of com.oracle.truffle.dsl.processor.expression.DSLExpression.Variable in project graal by oracle.

the class FlatNodeGenFactory method bindExpressionValues.

private Map<Variable, LocalVariable> bindExpressionValues(FrameState frameState, DSLExpression expression, SpecializationData specialization) throws AssertionError {
    Map<Variable, LocalVariable> bindings = new HashMap<>();
    Set<Variable> boundVariables = expression.findBoundVariables();
    if (specialization == null && !boundVariables.isEmpty()) {
        throw new AssertionError("Cannot bind guard variable in non-specialization group. yet.");
    }
    // resolve bindings for local context
    for (Variable variable : boundVariables) {
        Parameter resolvedParameter = specialization.findByVariable(variable.getResolvedVariable());
        if (resolvedParameter != null) {
            LocalVariable localVariable;
            if (resolvedParameter.getSpecification().isCached()) {
                // bind cached variable
                String cachedMemberName = createFieldName(specialization, resolvedParameter);
                localVariable = frameState.get(cachedMemberName);
                CodeTree ref;
                if (localVariable == null) {
                    ref = createCacheReference(frameState, specialization, resolvedParameter);
                } else {
                    ref = localVariable.createReference();
                }
                bindings.put(variable, new LocalVariable(resolvedParameter.getType(), cachedMemberName, ref));
            } else {
                // bind local variable
                if (resolvedParameter.getSpecification().isSignature()) {
                    NodeExecutionData execution = resolvedParameter.getSpecification().getExecution();
                    localVariable = frameState.getValue(execution);
                } else {
                    localVariable = frameState.get(resolvedParameter.getLocalName());
                }
                if (localVariable != null) {
                    bindings.put(variable, localVariable);
                }
            }
        }
    }
    return bindings;
}
Also used : Variable(com.oracle.truffle.dsl.processor.expression.DSLExpression.Variable) NodeExecutionData(com.oracle.truffle.dsl.processor.model.NodeExecutionData) HashMap(java.util.HashMap) CodeTree(com.oracle.truffle.dsl.processor.java.model.CodeTree) Parameter(com.oracle.truffle.dsl.processor.model.Parameter)

Example 2 with Variable

use of com.oracle.truffle.dsl.processor.expression.DSLExpression.Variable in project graal by oracle.

the class FlatNodeGenFactory method accessesCachedState.

private static boolean accessesCachedState(List<SpecializationData> specializations) {
    final AtomicBoolean needsState = new AtomicBoolean(false);
    for (final SpecializationData specialization : specializations) {
        if (!specialization.getAssumptionExpressions().isEmpty()) {
            needsState.set(true);
            break;
        }
        for (GuardExpression expression : specialization.getGuards()) {
            expression.getExpression().accept(new DSLExpressionVisitor() {

                public void visitVariable(Variable binary) {
                    if (!needsState.get() && isVariableAccessMember(binary)) {
                        needsState.set(true);
                    }
                }

                private boolean isVariableAccessMember(Variable variable) {
                    if (variable.getName().equals("null") && variable.getReceiver() == null) {
                        return false;
                    }
                    Parameter p = specialization.findByVariable(variable.getResolvedVariable());
                    if (p == null && !variable.getResolvedVariable().getModifiers().contains(STATIC)) {
                        DSLExpression receiver = variable.getReceiver();
                        if (receiver instanceof Variable) {
                            return isVariableAccessMember((Variable) receiver);
                        } else if (receiver instanceof Call) {
                            return isMethodAccessMember((Call) receiver);
                        }
                        return true;
                    } else if (p != null && p.getSpecification().isCached()) {
                        return true;
                    }
                    return false;
                }

                public void visitBooleanLiteral(BooleanLiteral binary) {
                }

                public void visitNegate(Negate negate) {
                }

                public void visitIntLiteral(IntLiteral binary) {
                }

                private boolean isMethodAccessMember(Call call) {
                    if (!call.getResolvedMethod().getModifiers().contains(STATIC)) {
                        DSLExpression receiver = call.getReceiver();
                        if (receiver instanceof Variable) {
                            return isVariableAccessMember((Variable) receiver);
                        } else if (receiver instanceof Call) {
                            return isMethodAccessMember((Call) receiver);
                        }
                        return true;
                    }
                    return false;
                }

                public void visitCall(Call call) {
                    if (!needsState.get() && isMethodAccessMember(call)) {
                        needsState.set(true);
                    }
                }

                public void visitBinary(Binary binary) {
                }
            });
        }
    }
    boolean needsStat = needsState.get();
    return needsStat;
}
Also used : Call(com.oracle.truffle.dsl.processor.expression.DSLExpression.Call) Variable(com.oracle.truffle.dsl.processor.expression.DSLExpression.Variable) GuardExpression(com.oracle.truffle.dsl.processor.model.GuardExpression) BooleanLiteral(com.oracle.truffle.dsl.processor.expression.DSLExpression.BooleanLiteral) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) DSLExpression(com.oracle.truffle.dsl.processor.expression.DSLExpression) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Parameter(com.oracle.truffle.dsl.processor.model.Parameter) IntLiteral(com.oracle.truffle.dsl.processor.expression.DSLExpression.IntLiteral) Negate(com.oracle.truffle.dsl.processor.expression.DSLExpression.Negate) Binary(com.oracle.truffle.dsl.processor.expression.DSLExpression.Binary) DSLExpressionVisitor(com.oracle.truffle.dsl.processor.expression.DSLExpression.DSLExpressionVisitor)

Example 3 with Variable

use of com.oracle.truffle.dsl.processor.expression.DSLExpression.Variable in project graal by oracle.

the class FatalError method MemberExpression.

DSLExpression MemberExpression(DSLExpression receiver) {
    DSLExpression result;
    result = null;
    Expect(1);
    Variable variable = new Variable(receiver, t.val);
    result = variable;
    if (la.kind == 11) {
        Get();
        List<DSLExpression> parameters = new ArrayList<>();
        DSLExpression parameter;
        if (StartOf(2)) {
            parameter = Expression();
            parameters.add(parameter);
            while (la.kind == 13) {
                Get();
                parameter = Expression();
                parameters.add(parameter);
            }
        }
        Expect(12);
        result = new Call(variable.getReceiver(), variable.getName(), parameters);
    }
    if (la.kind == 14) {
        Get();
        result = MemberExpression(result);
    }
    return result;
}
Also used : Call(com.oracle.truffle.dsl.processor.expression.DSLExpression.Call) Variable(com.oracle.truffle.dsl.processor.expression.DSLExpression.Variable) ArrayList(java.util.ArrayList)

Example 4 with Variable

use of com.oracle.truffle.dsl.processor.expression.DSLExpression.Variable in project graal by oracle.

the class GuardExpression method isConstantTrueInSlowPath.

public boolean isConstantTrueInSlowPath(ProcessorContext context) {
    DSLExpression reducedExpression = getExpression().reduce(new AbstractDSLExpressionReducer() {

        @Override
        public DSLExpression visitVariable(Variable binary) {
            // on the slow path we can assume all cache expressions inlined.
            for (CacheExpression cache : source.getCaches()) {
                if (ElementUtils.variableEquals(cache.getParameter().getVariableElement(), binary.getResolvedVariable())) {
                    return cache.getExpression();
                }
            }
            return super.visitVariable(binary);
        }

        @Override
        public DSLExpression visitCall(Call binary) {
            ExecutableElement method = binary.getResolvedMethod();
            if (!method.getSimpleName().toString().equals("equals")) {
                return binary;
            }
            if (method.getModifiers().contains(Modifier.STATIC)) {
                return binary;
            }
            if (!ElementUtils.typeEquals(method.getReturnType(), context.getType(boolean.class))) {
                return binary;
            }
            if (method.getParameters().size() != 1) {
                return binary;
            }
            // signature: receiver.equals(receiver) can be folded to true
            DSLExpression receiver = binary.getReceiver();
            DSLExpression firstArg = binary.getParameters().get(0);
            if (receiver instanceof Variable && firstArg instanceof Variable) {
                if (receiver.equals(firstArg)) {
                    return new BooleanLiteral(true);
                }
            }
            return super.visitCall(binary);
        }

        @Override
        public DSLExpression visitBinary(Binary binary) {
            // signature: value == value can be folded to true
            if (IDENTITY_FOLD_OPERATORS.contains(binary.getOperator())) {
                if (binary.getLeft() instanceof Variable && binary.getRight() instanceof Variable) {
                    Variable leftVar = ((Variable) binary.getLeft());
                    Variable rightVar = ((Variable) binary.getRight());
                    if (leftVar.equals(rightVar)) {
                        // double and float cannot be folded as NaN is never identity equal
                        if (!ElementUtils.typeEquals(leftVar.getResolvedType(), context.getType(float.class)) && !ElementUtils.typeEquals(leftVar.getResolvedType(), context.getType(double.class))) {
                            return new BooleanLiteral(true);
                        }
                    }
                }
            }
            return super.visitBinary(binary);
        }
    });
    Object o = reducedExpression.resolveConstant();
    if (o instanceof Boolean) {
        if (((Boolean) o).booleanValue()) {
            return true;
        }
    }
    return false;
}
Also used : Call(com.oracle.truffle.dsl.processor.expression.DSLExpression.Call) Variable(com.oracle.truffle.dsl.processor.expression.DSLExpression.Variable) BooleanLiteral(com.oracle.truffle.dsl.processor.expression.DSLExpression.BooleanLiteral) AbstractDSLExpressionReducer(com.oracle.truffle.dsl.processor.expression.DSLExpression.AbstractDSLExpressionReducer) ExecutableElement(javax.lang.model.element.ExecutableElement) Binary(com.oracle.truffle.dsl.processor.expression.DSLExpression.Binary) DSLExpression(com.oracle.truffle.dsl.processor.expression.DSLExpression)

Example 5 with Variable

use of com.oracle.truffle.dsl.processor.expression.DSLExpression.Variable 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

Variable (com.oracle.truffle.dsl.processor.expression.DSLExpression.Variable)6 DSLExpression (com.oracle.truffle.dsl.processor.expression.DSLExpression)3 Call (com.oracle.truffle.dsl.processor.expression.DSLExpression.Call)3 CodeTree (com.oracle.truffle.dsl.processor.java.model.CodeTree)3 Binary (com.oracle.truffle.dsl.processor.expression.DSLExpression.Binary)2 BooleanLiteral (com.oracle.truffle.dsl.processor.expression.DSLExpression.BooleanLiteral)2 GuardExpression (com.oracle.truffle.dsl.processor.model.GuardExpression)2 Parameter (com.oracle.truffle.dsl.processor.model.Parameter)2 HashMap (java.util.HashMap)2 AbstractDSLExpressionReducer (com.oracle.truffle.dsl.processor.expression.DSLExpression.AbstractDSLExpressionReducer)1 DSLExpressionVisitor (com.oracle.truffle.dsl.processor.expression.DSLExpression.DSLExpressionVisitor)1 IntLiteral (com.oracle.truffle.dsl.processor.expression.DSLExpression.IntLiteral)1 Negate (com.oracle.truffle.dsl.processor.expression.DSLExpression.Negate)1 ElementUtils.isObject (com.oracle.truffle.dsl.processor.java.ElementUtils.isObject)1 CodeTreeBuilder (com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)1 ArrayCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror)1 DeclaredCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.DeclaredCodeTypeMirror)1 GeneratedTypeMirror (com.oracle.truffle.dsl.processor.java.model.GeneratedTypeMirror)1 NodeExecutionData (com.oracle.truffle.dsl.processor.model.NodeExecutionData)1 SpecializationData (com.oracle.truffle.dsl.processor.model.SpecializationData)1