Search in sources :

Example 1 with CodeTreeBuilder

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

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

the class FlatNodeGenFactory method generateReflectionInfo.

private void generateReflectionInfo(CodeTypeElement clazz) {
    clazz.getImplements().add(context.getType(Introspection.Provider.class));
    CodeExecutableElement reflection = new CodeExecutableElement(modifiers(PUBLIC), context.getType(Introspection.class), "getIntrospectionData");
    CodeTreeBuilder builder = reflection.createBuilder();
    List<SpecializationData> filteredSpecializations = new ArrayList<>();
    for (SpecializationData s : node.getSpecializations()) {
        if (s.getMethod() == null) {
            continue;
        }
        filteredSpecializations.add(s);
    }
    ArrayCodeTypeMirror objectArray = new ArrayCodeTypeMirror(context.getType(Object.class));
    builder.declaration(objectArray, "data", builder.create().startNewArray(objectArray, CodeTreeBuilder.singleString(String.valueOf(filteredSpecializations.size() + 1))).end().build());
    builder.declaration(objectArray, "s", (CodeTree) null);
    // declare version 0
    builder.statement("data[0] = 0");
    FrameState frameState = FrameState.load(this);
    builder.tree(state.createLoad(frameState));
    if (requiresExclude()) {
        builder.tree(exclude.createLoad(frameState));
    }
    int index = 1;
    for (SpecializationData specialization : filteredSpecializations) {
        builder.startStatement().string("s = ").startNewArray(objectArray, CodeTreeBuilder.singleString("3")).end().end();
        builder.startStatement().string("s[0] = ").doubleQuote(specialization.getMethodName()).end();
        builder.startIf().tree(state.createContains(frameState, new Object[] { specialization })).end().startBlock();
        builder.startStatement().string("s[1] = (byte)0b01 /* active */").end();
        TypeMirror listType = new DeclaredCodeTypeMirror((TypeElement) context.getDeclaredType(ArrayList.class).asElement(), Arrays.asList(context.getType(Object.class)));
        if (!specialization.getCaches().isEmpty()) {
            builder.declaration(listType, "cached", "new ArrayList<>()");
            boolean useSpecializationClass = useSpecializationClass(specialization);
            String name = createSpecializationLocalName(specialization);
            if (useSpecializationClass) {
                builder.tree(loadSpecializationClass(frameState, specialization));
                if (specialization.hasMultipleInstances()) {
                    builder.startWhile();
                } else {
                    builder.startIf();
                }
                builder.string(name, " != null");
                builder.end();
                builder.startBlock();
            }
            builder.startStatement().startCall("cached", "add");
            builder.startStaticCall(context.getType(Arrays.class), "asList");
            for (CacheExpression cache : specialization.getCaches()) {
                builder.startGroup();
                builder.tree(createCacheReference(frameState, specialization, cache.getParameter()));
                builder.end();
            }
            builder.end();
            builder.end().end();
            if (useSpecializationClass) {
                if (specialization.getMaximumNumberOfInstances() > 1) {
                    builder.startStatement().string(name, " = ", name, ".next_").end();
                }
                // cache while or if
                builder.end();
            }
            builder.statement("s[2] = cached");
        }
        builder.end();
        if (mayBeExcluded(specialization)) {
            builder.startElseIf().tree(exclude.createContains(frameState, new Object[] { specialization })).end().startBlock();
            builder.startStatement().string("s[1] = (byte)0b10 /* excluded */").end();
            builder.end();
        }
        builder.startElseBlock();
        builder.startStatement().string("s[1] = (byte)0b00 /* inactive */").end();
        builder.end();
        builder.startStatement().string("data[", String.valueOf(index), "] = s").end();
        index++;
    }
    builder.startReturn().startStaticCall(context.getType(Introspection.Provider.class), "create").string("data").end().end();
    clazz.add(reflection);
}
Also used : CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) ArrayList(java.util.ArrayList) DeclaredCodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.DeclaredCodeTypeMirror) Introspection(com.oracle.truffle.api.dsl.Introspection) CacheExpression(com.oracle.truffle.dsl.processor.model.CacheExpression) 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) ElementUtils.isObject(com.oracle.truffle.dsl.processor.java.ElementUtils.isObject) Arrays(java.util.Arrays) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder) ArrayCodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror)

Example 3 with CodeTreeBuilder

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

the class FlatNodeGenFactory method persistAssumptions.

private List<IfTriple> persistAssumptions(FrameState frameState, SpecializationData specialization) {
    List<IfTriple> triples = new ArrayList<>();
    for (AssumptionExpression assumption : specialization.getAssumptionExpressions()) {
        LocalVariable var = frameState.get(assumption.getId());
        String name = createAssumptionFieldName(specialization, assumption);
        CodeTreeBuilder builder = new CodeTreeBuilder(null);
        builder.startStatement();
        builder.tree(createSpecializationFieldReference(frameState, specialization, name)).string(" = ").tree(var.createReference());
        builder.end();
        triples.add(new IfTriple(builder.build(), null, null));
    }
    return triples;
}
Also used : ArrayList(java.util.ArrayList) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder) AssumptionExpression(com.oracle.truffle.dsl.processor.model.AssumptionExpression)

Example 4 with CodeTreeBuilder

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

the class FlatNodeGenFactory method wrapInAMethod.

private CodeTree wrapInAMethod(CodeTreeBuilder parent, SpecializationGroup group, FrameState frameState, String suffix, CodeTree codeTree) {
    CodeExecutableElement parentMethod = (CodeExecutableElement) parent.findMethod();
    CodeTypeElement parentClass = (CodeTypeElement) parentMethod.getEnclosingElement();
    String name = parentMethod.getSimpleName().toString() + "_" + suffix + (boxingSplitIndex++);
    CodeExecutableElement method = parentClass.add(frameState.createMethod(modifiers(Modifier.PRIVATE), parentMethod.getReturnType(), name, FRAME_VALUE, STATE_VALUE));
    CodeTreeBuilder builder = method.createBuilder();
    builder.tree(codeTree);
    method.getThrownTypes().addAll(parentMethod.getThrownTypes());
    addExplodeLoop(builder, group);
    CodeTreeBuilder parentBuilder = parent.create();
    parentBuilder.startReturn();
    parentBuilder.startCall(method.getSimpleName().toString());
    frameState.addReferencesTo(parentBuilder, FRAME_VALUE, STATE_VALUE);
    parentBuilder.end();
    parentBuilder.end();
    return parentBuilder.build();
}
Also used : CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Example 5 with CodeTreeBuilder

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

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