Search in sources :

Example 6 with CodeExecutableElement

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

the class FlatNodeGenFactory method createCountCaches.

private Element createCountCaches() {
    TypeMirror returnType = getType(int.class);
    CodeExecutableElement executable = new CodeExecutableElement(modifiers(PRIVATE), returnType, COUNT_CACHES);
    CodeTreeBuilder builder = executable.createBuilder();
    final String cacheCount = "cache" + COUNT_SUFIX;
    builder.declaration(context.getType(int.class), cacheCount, "0");
    for (SpecializationData specialization : reachableSpecializations) {
        if (useSpecializationClass(specialization) && specialization.getMaximumNumberOfInstances() > 1) {
            String typeName = createSpecializationTypeName(specialization);
            String fieldName = createSpecializationFieldName(specialization);
            String localName = createSpecializationLocalName(specialization);
            builder.declaration(typeName, localName, "this." + fieldName);
            builder.startWhile().string(localName, " != null");
            builder.end();
            builder.startBlock().statement(cacheCount + "++").statement(localName + "= " + localName + ".next_");
            builder.end();
        }
    }
    builder.startReturn().statement(cacheCount);
    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) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Example 7 with CodeExecutableElement

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

the class FlatNodeGenFactory method createFields.

private void createFields(CodeTypeElement clazz) {
    state.declareFields(clazz);
    if (requiresExclude()) {
        exclude.declareFields(clazz);
    }
    for (SpecializationData specialization : reachableSpecializations) {
        List<CodeVariableElement> fields = new ArrayList<>();
        boolean useSpecializationClass = useSpecializationClass(specialization);
        for (CacheExpression cache : specialization.getCaches()) {
            Parameter parameter = cache.getParameter();
            String fieldName = createFieldName(specialization, parameter);
            TypeMirror type = parameter.getType();
            Modifier visibility = useSpecializationClass ? null : Modifier.PRIVATE;
            CodeVariableElement cachedField;
            if (ElementUtils.isAssignable(type, context.getType(NodeInterface.class))) {
                cachedField = createNodeField(visibility, type, fieldName, Child.class);
            } else if (isNodeInterfaceArray(type)) {
                cachedField = createNodeField(visibility, type, fieldName, Children.class);
            } else {
                cachedField = createNodeField(visibility, type, fieldName, null);
                setFieldCompilationFinal(cachedField, parameter.getVariableElement().getAnnotation(Cached.class).dimensions());
            }
            fields.add(cachedField);
        }
        for (AssumptionExpression assumption : specialization.getAssumptionExpressions()) {
            String fieldName = createAssumptionFieldName(specialization, assumption);
            TypeMirror type;
            int compilationFinalDimensions;
            if (assumption.getExpression().getResolvedType().getKind() == TypeKind.ARRAY) {
                type = context.getType(Assumption[].class);
                compilationFinalDimensions = 1;
            } else {
                type = context.getType(Assumption.class);
                compilationFinalDimensions = -1;
            }
            CodeVariableElement assumptionField;
            if (useSpecializationClass) {
                assumptionField = createNodeField(null, type, fieldName, null);
            } else {
                assumptionField = createNodeField(PRIVATE, type, fieldName, null);
            }
            setFieldCompilationFinal(assumptionField, compilationFinalDimensions);
            fields.add(assumptionField);
        }
        if (useSpecializationClass) {
            TypeMirror baseType;
            boolean useNode = specializationClassIsNode(specialization);
            if (useNode) {
                baseType = context.getType(Node.class);
            } else {
                baseType = context.getType(Object.class);
            }
            CodeTypeElement cacheType = GeneratorUtils.createClass(node, null, modifiers(PRIVATE, FINAL, STATIC), createSpecializationTypeName(specialization), baseType);
            Class<?> annotationType;
            if (useNode) {
                annotationType = Child.class;
                if (specialization.getMaximumNumberOfInstances() > 1) {
                    cacheType.add(createNodeField(null, cacheType.asType(), "next_", Child.class));
                }
                CodeExecutableElement getNodeCost = new CodeExecutableElement(modifiers(PUBLIC), context.getType(NodeCost.class), "getCost");
                getNodeCost.createBuilder().startReturn().staticReference(context.getType(NodeCost.class), "NONE").end();
                cacheType.add(getNodeCost);
            } else {
                annotationType = CompilationFinal.class;
                if (specialization.getMaximumNumberOfInstances() > 1) {
                    cacheType.add(createNodeField(null, cacheType.asType(), "next_", annotationType));
                }
            }
            cacheType.add(GeneratorUtils.createConstructorUsingFields(modifiers(), cacheType));
            cacheType.getEnclosedElements().addAll(fields);
            clazz.add(createNodeField(PRIVATE, cacheType.asType(), createSpecializationFieldName(specialization), annotationType));
            clazz.add(cacheType);
            specializationClasses.put(specialization, cacheType);
        } else {
            clazz.getEnclosedElements().addAll(fields);
        }
    }
}
Also used : CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) Node(com.oracle.truffle.api.nodes.Node) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) ArrayList(java.util.ArrayList) NodeCost(com.oracle.truffle.api.nodes.NodeCost) CacheExpression(com.oracle.truffle.dsl.processor.model.CacheExpression) Assumption(com.oracle.truffle.api.Assumption) 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) CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) Cached(com.oracle.truffle.api.dsl.Cached) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) Parameter(com.oracle.truffle.dsl.processor.model.Parameter) ElementUtils.isObject(com.oracle.truffle.dsl.processor.java.ElementUtils.isObject) Modifier(javax.lang.model.element.Modifier) Child(com.oracle.truffle.api.nodes.Node.Child) NodeInterface(com.oracle.truffle.api.nodes.NodeInterface) AssumptionExpression(com.oracle.truffle.dsl.processor.model.AssumptionExpression)

Example 8 with CodeExecutableElement

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

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

the class GeneratorUtils method createConstructorUsingFields.

static CodeExecutableElement createConstructorUsingFields(Set<Modifier> modifiers, CodeTypeElement clazz) {
    TypeElement superClass = fromTypeMirror(clazz.getSuperclass());
    ExecutableElement constructor = findConstructor(superClass);
    return createConstructorUsingFields(modifiers, clazz, constructor);
}
Also used : TypeElement(javax.lang.model.element.TypeElement) CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 10 with CodeExecutableElement

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

the class NodeCodeGenerator method createGetFactories.

private static ExecutableElement createGetFactories(ProcessorContext context, NodeData node) {
    List<NodeData> factoryList = node.getNodesWithFactories();
    if (node.needsFactory() && node.isGenerateFactory()) {
        factoryList.add(node);
    }
    if (factoryList.isEmpty()) {
        return null;
    }
    List<TypeMirror> nodeTypesList = new ArrayList<>();
    TypeMirror prev = null;
    boolean allSame = true;
    for (NodeData child : factoryList) {
        nodeTypesList.add(child.getNodeType());
        if (prev != null && !ElementUtils.typeEquals(child.getNodeType(), prev)) {
            allSame = false;
        }
        prev = child.getNodeType();
    }
    TypeMirror commonNodeSuperType = ElementUtils.getCommonSuperType(context, nodeTypesList);
    Types types = context.getEnvironment().getTypeUtils();
    TypeMirror factoryType = context.getType(NodeFactory.class);
    TypeMirror baseType;
    if (allSame) {
        baseType = ElementUtils.getDeclaredType(ElementUtils.fromTypeMirror(factoryType), commonNodeSuperType);
    } else {
        baseType = ElementUtils.getDeclaredType(ElementUtils.fromTypeMirror(factoryType), types.getWildcardType(commonNodeSuperType, null));
    }
    TypeMirror listType = ElementUtils.getDeclaredType(ElementUtils.fromTypeMirror(context.getType(List.class)), baseType);
    CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), listType, "getFactories");
    CodeTreeBuilder builder = method.createBuilder();
    builder.startReturn();
    if (factoryList.size() > 1) {
        builder.startStaticCall(context.getType(Arrays.class), "asList");
    } else {
        builder.startStaticCall(context.getType(Collections.class), "singletonList");
    }
    for (NodeData child : factoryList) {
        builder.startGroup();
        NodeData childNode = child;
        List<NodeData> factories = new ArrayList<>();
        while (childNode.getDeclaringNode() != null) {
            factories.add(childNode);
            childNode = childNode.getDeclaringNode();
        }
        Collections.reverse(factories);
        for (NodeData nodeData : factories) {
            builder.string(getAccessorClassName(nodeData)).string(".");
        }
        builder.string("getInstance()");
        builder.end();
    }
    builder.end();
    builder.end();
    return method;
}
Also used : Types(javax.lang.model.util.Types) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) GeneratedTypeMirror(com.oracle.truffle.dsl.processor.java.model.GeneratedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) ArrayList(java.util.ArrayList) Arrays(java.util.Arrays) Collections(java.util.Collections) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder) NodeData(com.oracle.truffle.dsl.processor.model.NodeData)

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