Search in sources :

Example 1 with ArrayCodeTypeMirror

use of com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror 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 2 with ArrayCodeTypeMirror

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

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

the class FlatNodeGenFactory method createInsertAccessor.

private CodeExecutableElement createInsertAccessor(boolean array) {
    CodeTypeParameterElement tVar = new CodeTypeParameterElement("T", context.getType(Node.class));
    TypeMirror type = tVar.createMirror(null, null);
    if (array) {
        type = new ArrayCodeTypeMirror(type);
    }
    CodeExecutableElement insertAccessor = new CodeExecutableElement(modifiers(FINAL), type, INSERT_ACCESSOR_NAME);
    insertAccessor.getParameters().add(new CodeVariableElement(type, "node"));
    insertAccessor.getTypeParameters().add(tVar);
    insertAccessor.createBuilder().startReturn().string("super.insert(node)").end();
    return insertAccessor;
}
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) Node(com.oracle.truffle.api.nodes.Node) CodeTypeParameterElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeParameterElement) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) ArrayCodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror)

Example 4 with ArrayCodeTypeMirror

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

the class NodeParser method initializeAssumptions.

private void initializeAssumptions(SpecializationData specialization, DSLExpressionResolver resolver) {
    final DeclaredType assumptionType = context.getDeclaredType(Assumption.class);
    final TypeMirror assumptionArrayType = new ArrayCodeTypeMirror(assumptionType);
    final List<String> assumptionDefinitions = ElementUtils.getAnnotationValueList(String.class, specialization.getMarkerAnnotation(), "assumptions");
    List<AssumptionExpression> assumptionExpressions = new ArrayList<>();
    int assumptionId = 0;
    for (String assumption : assumptionDefinitions) {
        AssumptionExpression assumptionExpression;
        DSLExpression expression = null;
        try {
            expression = DSLExpression.parse(assumption);
            expression.accept(resolver);
            assumptionExpression = new AssumptionExpression(specialization, expression, "assumption" + assumptionId);
            if (!ElementUtils.isAssignable(expression.getResolvedType(), assumptionType) && !ElementUtils.isAssignable(expression.getResolvedType(), assumptionArrayType)) {
                assumptionExpression.addError("Incompatible return type %s. Assumptions must be assignable to %s or %s.", ElementUtils.getSimpleName(expression.getResolvedType()), ElementUtils.getSimpleName(assumptionType), ElementUtils.getSimpleName(assumptionArrayType));
            }
            if (specialization.isDynamicParameterBound(expression)) {
                specialization.addError("Assumption expressions must not bind dynamic parameter values.");
            }
        } catch (InvalidExpressionException e) {
            assumptionExpression = new AssumptionExpression(specialization, null, "assumption" + assumptionId);
            assumptionExpression.addError("Error parsing expression '%s': %s", assumption, e.getMessage());
        }
        assumptionExpressions.add(assumptionExpression);
        assumptionId++;
    }
    specialization.setAssumptionExpressions(assumptionExpressions);
}
Also used : ArrayCodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) InvalidExpressionException(com.oracle.truffle.dsl.processor.expression.InvalidExpressionException) ArrayList(java.util.ArrayList) DSLExpression(com.oracle.truffle.dsl.processor.expression.DSLExpression) DeclaredType(javax.lang.model.type.DeclaredType) ArrayCodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror) AssumptionExpression(com.oracle.truffle.dsl.processor.model.AssumptionExpression)

Aggregations

ArrayCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror)4 ArrayList (java.util.ArrayList)3 TypeMirror (javax.lang.model.type.TypeMirror)3 CodeExecutableElement (com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement)2 CodeTreeBuilder (com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)2 DeclaredCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.DeclaredCodeTypeMirror)2 GeneratedTypeMirror (com.oracle.truffle.dsl.processor.java.model.GeneratedTypeMirror)2 Introspection (com.oracle.truffle.api.dsl.Introspection)1 UnsupportedSpecializationException (com.oracle.truffle.api.dsl.UnsupportedSpecializationException)1 Node (com.oracle.truffle.api.nodes.Node)1 DSLExpression (com.oracle.truffle.dsl.processor.expression.DSLExpression)1 InvalidExpressionException (com.oracle.truffle.dsl.processor.expression.InvalidExpressionException)1 ElementUtils.isObject (com.oracle.truffle.dsl.processor.java.ElementUtils.isObject)1 CodeTree (com.oracle.truffle.dsl.processor.java.model.CodeTree)1 CodeTypeParameterElement (com.oracle.truffle.dsl.processor.java.model.CodeTypeParameterElement)1 CodeVariableElement (com.oracle.truffle.dsl.processor.java.model.CodeVariableElement)1 AssumptionExpression (com.oracle.truffle.dsl.processor.model.AssumptionExpression)1 CacheExpression (com.oracle.truffle.dsl.processor.model.CacheExpression)1 NodeChildData (com.oracle.truffle.dsl.processor.model.NodeChildData)1 NodeExecutionData (com.oracle.truffle.dsl.processor.model.NodeExecutionData)1