Search in sources :

Example 36 with CodeTreeBuilder

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

the class FlatNodeGenFactory method createCallExecuteAndSpecialize.

private CodeTree createCallExecuteAndSpecialize(ExecutableTypeData forType, FrameState frameState) {
    TypeMirror returnType = node.getPolymorphicSpecialization().getReturnType().getType();
    String frame = null;
    if (needsFrameToExecute(reachableSpecializations)) {
        frame = FRAME_VALUE;
    }
    CodeTreeBuilder builder = CodeTreeBuilder.createBuilder();
    builder.startCall("executeAndSpecialize");
    frameState.addReferencesTo(builder, frame);
    builder.end();
    CodeTree call = builder.build();
    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) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Example 37 with CodeTreeBuilder

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

the class FlatNodeGenFactory method createFallbackGuard.

private Element createFallbackGuard() {
    boolean frameUsed = false;
    FrameState frameState = FrameState.load(this);
    List<SpecializationData> specializations = new ArrayList<>(reachableSpecializations);
    for (ListIterator<SpecializationData> iterator = specializations.listIterator(); iterator.hasNext(); ) {
        SpecializationData specialization = iterator.next();
        if (specialization.isFallback()) {
            iterator.remove();
        } else if (!specialization.isReachesFallback()) {
            iterator.remove();
        } else {
            if (specialization.isFrameUsedByGuard()) {
                frameUsed = true;
            }
        }
    }
    SpecializationGroup group = SpecializationGroup.create(specializations);
    ExecutableTypeData executableType = node.findAnyGenericExecutableType(context, -1);
    if (!frameUsed) {
        frameState.removeValue(FRAME_VALUE);
    }
    fallbackNeedsState = false;
    fallbackNeedsFrame = frameUsed;
    state.createLoad(frameState);
    CodeExecutableElement method = frameState.createMethod(modifiers(PRIVATE), getType(boolean.class), METHOD_FALLBACK_GUARD, FRAME_VALUE, STATE_VALUE);
    CodeTree result = visitSpecializationGroup(CodeTreeBuilder.createBuilder(), group, executableType, frameState, null, NodeExecutionMode.FALLBACK_GUARD);
    if (!fallbackNeedsState) {
        VariableElement toRemove = null;
        for (VariableElement v : method.getParameters()) {
            if (v.getSimpleName().toString().equals(STATE_VALUE)) {
                toRemove = v;
                break;
            }
        }
        if (toRemove != null) {
            method.getParameters().remove(toRemove);
        }
    }
    final CodeTreeBuilder builder = method.createBuilder();
    for (SpecializationData implemented : specializations) {
        if (implemented.getMaximumNumberOfInstances() > 1) {
            method.getAnnotationMirrors().add(createExplodeLoop());
            break;
        }
    }
    builder.tree(result);
    builder.returnTrue();
    if (!accessesCachedState(specializations)) {
        method.getModifiers().add(STATIC);
    }
    return method;
}
Also used : ExecutableTypeData(com.oracle.truffle.dsl.processor.model.ExecutableTypeData) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) CodeTree(com.oracle.truffle.dsl.processor.java.model.CodeTree) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) ArrayList(java.util.ArrayList) SpecializationGroup(com.oracle.truffle.dsl.processor.parser.SpecializationGroup) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) VariableElement(javax.lang.model.element.VariableElement) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Example 38 with CodeTreeBuilder

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

the class FlatNodeGenFactory method createExecute.

private CodeTree createExecute(CodeTreeBuilder parent, FrameState frameState, final ExecutableTypeData forType, SpecializationData specialization, NodeExecutionMode mode) {
    CodeTreeBuilder builder = parent.create();
    if (mode.isSlowPath()) {
        builder.statement("lock.unlock()");
        builder.statement("hasLock = false");
    }
    if (specialization.getMethod() == null) {
        builder.tree(createThrowUnsupported(builder, frameState));
    } else {
        if (isVoid(specialization.getMethod().getReturnType())) {
            builder.statement(callTemplateMethod(null, specialization, frameState));
            if (isVoid(forType.getReturnType())) {
                builder.returnStatement();
            } else {
                builder.startReturn().defaultValue(forType.getReturnType()).end();
            }
        } else {
            builder.startReturn();
            builder.tree(expectOrCast(specialization.getReturnType().getType(), forType, callTemplateMethod(null, specialization, frameState)));
            builder.end();
        }
    }
    return createCatchRewriteException(builder, specialization, forType, frameState, builder.build(), mode);
}
Also used : CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Example 39 with CodeTreeBuilder

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

the class FlatNodeGenFactory method createRemoveThis.

private CodeTree createRemoveThis(CodeTreeBuilder parent, FrameState frameState, ExecutableTypeData forType, SpecializationData specialization) {
    CodeExecutableElement method = removeThisMethods.get(specialization);
    String specializationLocalName = createSpecializationLocalName(specialization);
    boolean useSpecializationClass = useSpecializationClass(specialization);
    if (method == null) {
        method = new CodeExecutableElement(context.getType(void.class), "remove" + specialization.getId() + "_");
        if (useSpecializationClass) {
            method.addParameter(new CodeVariableElement(context.getType(Object.class), specializationLocalName));
        }
        CodeTreeBuilder builder = method.createBuilder();
        builder.declaration(context.getType(Lock.class), "lock", "getLock()");
        builder.statement("lock.lock()");
        builder.startTryBlock();
        String fieldName = createSpecializationFieldName(specialization);
        if (!useSpecializationClass || specialization.getMaximumNumberOfInstances() == 1) {
            // single instance remove
            builder.tree((state.createSet(null, new Object[] { specialization }, false, true)));
            if (useSpecializationClass) {
                builder.statement("this." + fieldName + " = null");
            }
        } else {
            // multi instance remove
            String typeName = createSpecializationTypeName(specialization);
            boolean specializedIsNode = specializationClassIsNode(specialization);
            builder.declaration(typeName, "prev", "null");
            builder.declaration(typeName, "cur", "this." + fieldName);
            builder.startWhile();
            builder.string("cur != null");
            builder.end().startBlock();
            builder.startIf().string("cur == ").string(specializationLocalName).end().startBlock();
            builder.startIf().string("prev == null").end().startBlock();
            builder.statement("this." + fieldName + " = cur.next_");
            if (specializedIsNode) {
                builder.statement("this.adoptChildren()");
            }
            builder.end().startElseBlock();
            builder.statement("prev.next_ = cur.next_");
            if (specializedIsNode) {
                builder.statement("prev.adoptChildren()");
            }
            builder.end();
            builder.statement("break");
            // if block
            builder.end();
            builder.statement("prev = cur");
            builder.statement("cur = cur.next_");
            // while block
            builder.end();
            builder.startIf().string("this." + fieldName).string(" == null").end().startBlock();
            builder.tree((state.createSet(null, Arrays.asList(specialization).toArray(new SpecializationData[0]), false, true)));
            builder.end();
        }
        builder.end().startFinallyBlock();
        builder.statement("lock.unlock()");
        builder.end();
        removeThisMethods.put(specialization, method);
    }
    CodeTreeBuilder builder = parent.create();
    builder.startStatement().startCall(method.getSimpleName().toString());
    if (useSpecializationClass) {
        builder.string(specializationLocalName);
    }
    builder.end().end();
    builder.tree(createCallExecuteAndSpecialize(forType, frameState));
    return builder.build();
}
Also used : CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder) Lock(java.util.concurrent.locks.Lock)

Example 40 with CodeTreeBuilder

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

the class FlatNodeGenFactory method createTryExecuteChild.

private static CodeTree createTryExecuteChild(LocalVariable value, CodeTree executeChild, boolean needDeclaration, boolean hasTry) {
    CodeTreeBuilder builder = CodeTreeBuilder.createBuilder();
    boolean hasDeclaration = false;
    if ((hasTry || !executeChild.isSingleLine()) && needDeclaration) {
        builder.tree(value.createDeclaration(null));
        hasDeclaration = true;
    }
    if (hasTry) {
        builder.startTryBlock();
    } else {
        builder.startGroup();
    }
    if (executeChild.isSingleLine()) {
        builder.startStatement();
        if (hasDeclaration || !needDeclaration) {
            builder.tree(executeChild);
        } else {
            builder.type(value.getTypeMirror()).string(" ").tree(executeChild);
        }
        builder.end();
    } else {
        builder.tree(executeChild);
    }
    builder.end();
    return builder.build();
}
Also used : 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