Search in sources :

Example 46 with CodeTreeBuilder

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

the class FlatNodeGenFactory method storeCache.

private Collection<IfTriple> storeCache(FrameState frameState, SpecializationData specialization, CacheExpression cache, CodeTree value) {
    if (value == null) {
        return Collections.emptyList();
    }
    String name = createFieldName(specialization, cache.getParameter());
    LocalVariable var = frameState.get(name);
    if (var != null) {
        // already initialized
        return Collections.emptyList();
    }
    TypeMirror type = cache.getParameter().getType();
    CodeTreeBuilder builder = new CodeTreeBuilder(null);
    String refName = name + "_";
    builder.declaration(type, refName, value);
    frameState.set(name, new LocalVariable(type, name, CodeTreeBuilder.singleString(refName)));
    return Arrays.asList(new IfTriple(builder.build(), null, null));
}
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) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Example 47 with CodeTreeBuilder

use of com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder 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 48 with CodeTreeBuilder

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

the class FlatNodeGenFactory method createAccessChildMethod.

private ExecutableElement createAccessChildMethod(NodeChildData child) {
    if (child.getAccessElement() != null && child.getAccessElement().getModifiers().contains(Modifier.ABSTRACT)) {
        ExecutableElement getter = (ExecutableElement) child.getAccessElement();
        CodeExecutableElement method = CodeExecutableElement.clone(context.getEnvironment(), getter);
        method.getModifiers().remove(Modifier.ABSTRACT);
        List<NodeExecutionData> executions = new ArrayList<>();
        for (NodeExecutionData execution : node.getChildExecutions()) {
            if (execution.getChild() == child) {
                executions.add(execution);
            }
        }
        CodeTreeBuilder builder = method.createBuilder();
        if (child.getCardinality().isMany()) {
            builder.startReturn().startNewArray((ArrayType) child.getOriginalType(), null);
            for (NodeExecutionData execution : executions) {
                builder.string(accessNodeField(execution));
            }
            builder.end().end();
        } else {
            for (NodeExecutionData execution : executions) {
                builder.startReturn().string(accessNodeField(execution)).end();
                break;
            }
        }
        return method;
    }
    return null;
}
Also used : CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) NodeExecutionData(com.oracle.truffle.dsl.processor.model.NodeExecutionData) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Example 49 with CodeTreeBuilder

use of com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder 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 50 with CodeTreeBuilder

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

the class FlatNodeGenFactory method createNodeConstructor.

// old code
private CodeExecutableElement createNodeConstructor(CodeTypeElement clazz, ExecutableElement superConstructor) {
    CodeExecutableElement constructor = GeneratorUtils.createConstructorUsingFields(modifiers(), clazz, superConstructor);
    ElementUtils.setVisibility(constructor.getModifiers(), ElementUtils.getVisibility(superConstructor.getModifiers()));
    constructor.setVarArgs(superConstructor.isVarArgs());
    List<CodeVariableElement> childParameters = new ArrayList<>();
    for (NodeChildData child : node.getChildren()) {
        if (child.needsGeneratedField()) {
            childParameters.add(new CodeVariableElement(child.getOriginalType(), child.getName()));
        }
    }
    constructor.getParameters().addAll(superConstructor.getParameters().size(), childParameters);
    CodeTreeBuilder builder = constructor.appendBuilder();
    List<String> childValues = new ArrayList<>(node.getChildren().size());
    if (!node.getChildExecutions().isEmpty()) {
        for (NodeChildData child : node.getChildren()) {
            if (child.needsGeneratedField()) {
                String name = child.getName();
                if (child.getCardinality().isMany()) {
                    CreateCastData createCast = node.findCast(child.getName());
                    if (createCast != null) {
                        CodeTree nameTree = CodeTreeBuilder.singleString(name);
                        CodeTreeBuilder callBuilder = builder.create();
                        callBuilder.string(name).string(" != null ? ");
                        callBuilder.tree(callMethod(null, createCast.getMethod(), nameTree));
                        callBuilder.string(" : null");
                        name += "_";
                        builder.declaration(child.getNodeType(), name, callBuilder.build());
                    }
                }
                childValues.add(name);
            }
        }
    }
    for (NodeExecutionData execution : node.getChildExecutions()) {
        if (execution.getChild() == null || !execution.getChild().needsGeneratedField()) {
            continue;
        }
        CreateCastData createCast = node.findCast(execution.getChild().getName());
        builder.startStatement();
        builder.string("this.").string(nodeFieldName(execution)).string(" = ");
        String name = childValues.get(node.getChildren().indexOf(execution.getChild()));
        CodeTreeBuilder accessorBuilder = builder.create();
        accessorBuilder.string(name);
        if (execution.hasChildArrayIndex()) {
            accessorBuilder.string("[").string(String.valueOf(execution.getChildArrayIndex())).string("]");
        }
        CodeTree accessor = accessorBuilder.build();
        if (createCast != null && execution.getChild().getCardinality().isOne()) {
            accessor = callMethod(null, createCast.getMethod(), accessor);
        }
        if (execution.hasChildArrayIndex()) {
            CodeTreeBuilder nullCheck = builder.create();
            nullCheck.string(name).string(" != null && ").string(String.valueOf(execution.getChildArrayIndex())).string(" < ").string(name).string(".length").string(" ? ");
            nullCheck.tree(accessor);
            nullCheck.string(" : null");
            accessor = nullCheck.build();
        }
        builder.tree(accessor);
        builder.end();
    }
    return constructor;
}
Also used : CreateCastData(com.oracle.truffle.dsl.processor.model.CreateCastData) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) NodeChildData(com.oracle.truffle.dsl.processor.model.NodeChildData) NodeExecutionData(com.oracle.truffle.dsl.processor.model.NodeExecutionData) CodeTree(com.oracle.truffle.dsl.processor.java.model.CodeTree) ArrayList(java.util.ArrayList) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) 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