Search in sources :

Example 16 with CodeExecutableElement

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

the class FlatNodeGenFactory method createCheckForPolymorphicSpecialize.

private Element createCheckForPolymorphicSpecialize() {
    final boolean requiresExclude = requiresExclude();
    final boolean requiresCacheCheck = requiresCacheCheck();
    TypeMirror returnType = getType(void.class);
    CodeExecutableElement executable = new CodeExecutableElement(modifiers(PRIVATE), returnType, CHECK_FOR_POLYMORPHIC_SPECIALIZE);
    executable.addParameter(new CodeVariableElement(state.bitSetType, OLD_STATE));
    if (requiresExclude) {
        executable.addParameter(new CodeVariableElement(exclude.bitSetType, OLD_EXCLUDE));
    }
    if (requiresCacheCheck) {
        executable.addParameter(new CodeVariableElement(getType(int.class), OLD_CACHE_COUNT));
    }
    CodeTreeBuilder builder = executable.createBuilder();
    builder.declaration(state.bitSetType, NEW_STATE, state.createMaskedReference(FrameState.load(this), reachableSpecializations.toArray()));
    if (requiresExclude) {
        builder.declaration(exclude.bitSetType, NEW_EXCLUDE, exclude.createReference(FrameState.load(this)));
    }
    builder.startIf().string("(" + OLD_STATE + " ^ " + NEW_STATE + ") != 0");
    if (requiresExclude) {
        builder.string(" || ");
        builder.string("(" + OLD_EXCLUDE + " ^ " + NEW_EXCLUDE + ") != 0");
    }
    if (requiresCacheCheck) {
        builder.string(" || " + OLD_CACHE_COUNT + " < " + COUNT_CACHES + "()");
    }
    // if
    builder.end();
    builder.startBlock().startStatement().startCall("this", REPORT_POLYMORPHIC_SPECIALIZE).end(2);
    // true block
    builder.end();
    return executable;
}
Also used : CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) 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) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Example 17 with CodeExecutableElement

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

the class FlatNodeGenFactory method createIsValid.

private Element createIsValid(TypeMirror assumptionType) {
    CodeExecutableElement isValid = new CodeExecutableElement(modifiers(PRIVATE, STATIC), getType(boolean.class), "isValid_");
    CodeTreeBuilder builder = isValid.createBuilder();
    if (assumptionType.getKind() == TypeKind.ARRAY) {
        isValid.addAnnotationMirror(new CodeAnnotationMirror(context.getDeclaredType(ExplodeLoop.class)));
        isValid.addParameter(new CodeVariableElement(getType(Assumption[].class), "assumptions"));
        builder.startIf().string("assumptions == null").end().startBlock().returnFalse().end();
        builder.startFor().startGroup().type(((ArrayType) assumptionType).getComponentType()).string(" assumption : assumptions").end().end();
        builder.startBlock();
        builder.startIf().string("assumption == null || !assumption.isValid()").end();
        builder.startBlock();
        builder.returnFalse();
        builder.end();
        builder.end();
        builder.returnTrue();
    } else {
        isValid.addParameter(new CodeVariableElement(getType(Assumption.class), "assumption"));
        builder.startReturn().string("assumption != null && assumption.isValid()").end();
    }
    return isValid;
}
Also used : CodeAnnotationMirror(com.oracle.truffle.dsl.processor.java.model.CodeAnnotationMirror) ArrayType(javax.lang.model.type.ArrayType) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder) Assumption(com.oracle.truffle.api.Assumption)

Example 18 with CodeExecutableElement

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

the class FlatNodeGenFactory method createExecuteAndSpecialize.

private CodeExecutableElement createExecuteAndSpecialize() {
    if (!node.needsRewrites(context)) {
        return null;
    }
    final FrameState frameState = FrameState.load(this);
    String frame = null;
    if (needsFrameToExecute(reachableSpecializations)) {
        frame = FRAME_VALUE;
    }
    TypeMirror returnType = executeAndSpecializeType.getReturnType();
    CodeExecutableElement method = frameState.createMethod(modifiers(PRIVATE), returnType, "executeAndSpecialize", frame);
    final CodeTreeBuilder builder = method.createBuilder();
    builder.declaration(context.getType(Lock.class), "lock", "getLock()");
    builder.declaration(context.getType(boolean.class), "hasLock", "true");
    builder.statement("lock.lock()");
    builder.tree(state.createLoad(frameState));
    if (requiresExclude()) {
        builder.tree(exclude.createLoad(frameState));
    }
    if (shouldReportPolymorphism(node, reachableSpecializations)) {
        generateSaveOldPolymorphismState(builder, frameState);
    }
    builder.startTryBlock();
    FrameState originalFrameState = frameState.copy();
    SpecializationGroup group = createSpecializationGroups();
    CodeTree execution = visitSpecializationGroup(builder, group, executeAndSpecializeType, frameState, null, NodeExecutionMode.SLOW_PATH);
    builder.tree(execution);
    if (group.hasFallthrough()) {
        builder.tree(createTransferToInterpreterAndInvalidate());
        builder.tree(createThrowUnsupported(builder, originalFrameState));
    }
    builder.end().startFinallyBlock();
    if (shouldReportPolymorphism(node, reachableSpecializations)) {
        generateCheckNewPolymorphismState(builder);
    }
    builder.startIf().string("hasLock").end().startBlock();
    builder.statement("lock.unlock()");
    builder.end();
    builder.end();
    return method;
}
Also used : CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) 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) SpecializationGroup(com.oracle.truffle.dsl.processor.parser.SpecializationGroup) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder) Lock(java.util.concurrent.locks.Lock)

Example 19 with CodeExecutableElement

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

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

Aggregations

CodeExecutableElement (com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement)31 CodeTreeBuilder (com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)23 CodeVariableElement (com.oracle.truffle.dsl.processor.java.model.CodeVariableElement)18 TypeMirror (javax.lang.model.type.TypeMirror)18 GeneratedTypeMirror (com.oracle.truffle.dsl.processor.java.model.GeneratedTypeMirror)11 ArrayList (java.util.ArrayList)11 ArrayCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror)10 ExecutableElement (javax.lang.model.element.ExecutableElement)10 DeclaredCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.DeclaredCodeTypeMirror)9 CodeTypeElement (com.oracle.truffle.dsl.processor.java.model.CodeTypeElement)8 SpecializationData (com.oracle.truffle.dsl.processor.model.SpecializationData)8 VariableElement (javax.lang.model.element.VariableElement)8 TypeElement (javax.lang.model.element.TypeElement)6 Arrays (java.util.Arrays)5 CodeTree (com.oracle.truffle.dsl.processor.java.model.CodeTree)4 NodeExecutionData (com.oracle.truffle.dsl.processor.model.NodeExecutionData)4 Modifier (javax.lang.model.element.Modifier)4 CodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror)3 NodeChildData (com.oracle.truffle.dsl.processor.model.NodeChildData)3 SpecializationGroup (com.oracle.truffle.dsl.processor.parser.SpecializationGroup)3