Search in sources :

Example 1 with CodeTree

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

the class DSLExpressionGenerator method visitCall.

public void visitCall(Call call) {
    ExecutableElement method = call.getResolvedMethod();
    CodeTree[] parameters = new CodeTree[method.getParameters().size()];
    for (int i = 0; i < parameters.length; i++) {
        parameters[parameters.length - i - 1] = pop();
    }
    CodeTreeBuilder builder = CodeTreeBuilder.createBuilder();
    if (call.getResolvedMethod().getKind() == ElementKind.CONSTRUCTOR) {
        builder.startNew(call.getResolvedType());
    } else if (call.getReceiver() == null) {
        if (isStatic(method)) {
            builder.startStaticCall(method);
        } else {
            if (root != null) {
                builder.tree(root).string(".");
            }
            builder.startCall(method.getSimpleName().toString());
        }
    } else {
        if (isStatic(method)) {
            throw new AssertionError("Static calls must not have receivers.");
        }
        builder.startCall(pop(), method.getSimpleName().toString());
    }
    for (CodeTree parameter : parameters) {
        builder.tree(parameter);
    }
    builder.end();
    push(builder.build());
}
Also used : CodeTree(com.oracle.truffle.dsl.processor.java.model.CodeTree) ExecutableElement(javax.lang.model.element.ExecutableElement) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Example 2 with CodeTree

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

the class FlatNodeGenFactory method createThrowUnsupported.

private CodeTree createThrowUnsupported(final CodeTreeBuilder parent, final FrameState frameState) {
    CodeTreeBuilder builder = parent.create();
    builder.startThrow().startNew(context.getType(UnsupportedSpecializationException.class));
    builder.string("this");
    builder.startNewArray(new ArrayCodeTypeMirror(context.getType(Node.class)), null);
    List<CodeTree> values = new ArrayList<>();
    for (NodeExecutionData execution : node.getChildExecutions()) {
        NodeChildData child = execution.getChild();
        LocalVariable var = frameState.getValue(execution);
        if (child != null) {
            builder.string(accessNodeField(execution));
        } else {
            builder.string("null");
        }
        if (var != null) {
            values.add(var.createReference());
        }
    }
    builder.end();
    builder.trees(values.toArray(new CodeTree[0]));
    builder.end().end();
    return builder.build();
}
Also used : NodeExecutionData(com.oracle.truffle.dsl.processor.model.NodeExecutionData) NodeChildData(com.oracle.truffle.dsl.processor.model.NodeChildData) CodeTree(com.oracle.truffle.dsl.processor.java.model.CodeTree) UnsupportedSpecializationException(com.oracle.truffle.api.dsl.UnsupportedSpecializationException) ArrayList(java.util.ArrayList) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder) ArrayCodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror)

Example 3 with CodeTree

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

the class FlatNodeGenFactory method createParameterReference.

private CodeTree createParameterReference(LocalVariable sourceVariable, ExecutableElement targetMethod, int targetIndex) {
    CodeTree valueReference = sourceVariable.createReference();
    TypeMirror sourceType = sourceVariable.getTypeMirror();
    VariableElement targetParameter = targetMethod.getParameters().get(targetIndex);
    TypeMirror targetType = targetParameter.asType();
    if (targetType == null || sourceType == null) {
        return valueReference;
    }
    boolean hasCast = false;
    if (needsCastTo(sourceType, targetType)) {
        CodeTree castValue = TypeSystemCodeGenerator.cast(typeSystem, targetType, valueReference);
        hasCast = valueReference != castValue;
        valueReference = castValue;
    }
    // check for overloads that might conflict in the call and therefore needs a cast
    if (!ElementUtils.typeEquals(sourceType, targetType) && !hasCast) {
        Element element = targetMethod.getEnclosingElement();
        boolean needsOverloadCast = false;
        if (element != null) {
            for (ExecutableElement executable : ElementFilter.methodsIn(element.getEnclosedElements())) {
                if (ElementUtils.executableEquals(executable, targetMethod)) {
                    continue;
                }
                if (!executable.getSimpleName().toString().equals(targetMethod.getSimpleName().toString())) {
                    continue;
                }
                if (executable.getParameters().size() != targetMethod.getParameters().size()) {
                    continue;
                }
                TypeMirror overloadedTarget = executable.getParameters().get(targetIndex).asType();
                if (!needsCastTo(sourceType, overloadedTarget)) {
                    needsOverloadCast = true;
                    break;
                }
            }
        }
        if (needsOverloadCast) {
            valueReference = TypeSystemCodeGenerator.cast(typeSystem, targetType, valueReference);
        }
    }
    return valueReference;
}
Also used : CodeTree(com.oracle.truffle.dsl.processor.java.model.CodeTree) 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) TypeElement(javax.lang.model.element.TypeElement) CodeTypeParameterElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeParameterElement) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) VariableElement(javax.lang.model.element.VariableElement)

Example 4 with CodeTree

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

the class FlatNodeGenFactory method createAssumptionCheckTriples.

private List<IfTriple> createAssumptionCheckTriples(FrameState frameState, SpecializationData specialization) {
    if (specialization == null || specialization.getAssumptionExpressions().isEmpty()) {
        return Collections.emptyList();
    }
    List<IfTriple> triples = new ArrayList<>();
    List<AssumptionExpression> assumptions = specialization.getAssumptionExpressions();
    for (AssumptionExpression assumption : assumptions) {
        CodeTree assumptionReference = createAssumptionReference(frameState, specialization, assumption);
        CodeTree assumptionGuard = createAssumptionGuard(assumption, assumptionReference);
        CodeTreeBuilder builder = new CodeTreeBuilder(null);
        builder.string("(");
        builder.tree(assumptionReference).string(" == null || ");
        builder.tree(assumptionGuard);
        builder.string(")");
        triples.add(new IfTriple(null, builder.build(), null));
    }
    return triples;
}
Also used : CodeTree(com.oracle.truffle.dsl.processor.java.model.CodeTree) ArrayList(java.util.ArrayList) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder) AssumptionExpression(com.oracle.truffle.dsl.processor.model.AssumptionExpression)

Example 5 with CodeTree

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

the class FlatNodeGenFactory method initializeSpecializationClass.

private Collection<? extends IfTriple> initializeSpecializationClass(FrameState frameState, SpecializationData specialization) {
    boolean useSpecializationClass = useSpecializationClass(specialization);
    if (useSpecializationClass) {
        String localName = createSpecializationLocalName(specialization);
        String typeName = createSpecializationTypeName(specialization);
        // we cannot use local name, because its used track reads not init writes
        String classInitialized = createSpecializationClassInitialized(specialization);
        if (!frameState.getBoolean(classInitialized, false)) {
            GeneratedTypeMirror type = new GeneratedTypeMirror("", typeName);
            CodeTreeBuilder initBuilder = new CodeTreeBuilder(null);
            initBuilder.startNew(typeName);
            if (specialization.getMaximumNumberOfInstances() > 1) {
                initBuilder.string(createSpecializationFieldName(specialization));
            }
            // new
            initBuilder.end();
            CodeTree init = initBuilder.build();
            CodeTreeBuilder builder = new CodeTreeBuilder(null);
            builder.startStatement();
            if (frameState.get(localName) == null) {
                // not yet declared
                builder.string(typeName);
                builder.string(" ");
            }
            builder.string(localName);
            builder.string(" = ");
            builder.tree(init);
            builder.end();
            frameState.setBoolean(classInitialized, true);
            frameState.set(localName, new LocalVariable(type, localName, CodeTreeBuilder.singleString(localName)));
            return Arrays.asList(new IfTriple(builder.build(), null, null));
        }
    }
    return Collections.emptyList();
}
Also used : CodeTree(com.oracle.truffle.dsl.processor.java.model.CodeTree) GeneratedTypeMirror(com.oracle.truffle.dsl.processor.java.model.GeneratedTypeMirror) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

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