Search in sources :

Example 6 with TruffleTypes

use of com.oracle.truffle.dsl.processor.TruffleTypes in project graal by oracle.

the class FlatNodeGenFactory method createContextReferenceConstant.

public static CodeVariableElement createContextReferenceConstant(StaticConstants constants, TypeMirror languageType) {
    TruffleTypes types = ProcessorContext.getInstance().getTypes();
    String constantName = ElementUtils.createConstantName(ElementUtils.getSimpleName(languageType) + "Cref");
    TypeElement contextReference = (TypeElement) types.TruffleLanguage_ContextReference.asElement();
    DeclaredCodeTypeMirror constantType = new DeclaredCodeTypeMirror(contextReference, Arrays.asList(NodeParser.findContextTypeFromLanguage(languageType)));
    return lookupConstant(constants.languageReferences, constantName, (name) -> {
        CodeVariableElement newVar = new CodeVariableElement(modifiers(PRIVATE, STATIC, FINAL), constantType, name);
        newVar.createInitBuilder().startStaticCall(contextReference.asType(), "create").typeLiteral(languageType).end();
        return newVar;
    });
}
Also used : TypeElement(javax.lang.model.element.TypeElement) CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) TruffleTypes(com.oracle.truffle.dsl.processor.TruffleTypes) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) DeclaredCodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.DeclaredCodeTypeMirror)

Example 7 with TruffleTypes

use of com.oracle.truffle.dsl.processor.TruffleTypes in project graal by oracle.

the class NodeParser method computeSharing.

public static Map<CacheExpression, String> computeSharing(Element templateType, Collection<NodeData> nodes, boolean emitSharingWarnings) {
    TruffleTypes types = ProcessorContext.getInstance().getTypes();
    Map<SharableCache, Collection<CacheExpression>> groups = computeSharableCaches(nodes);
    // compute unnecessary sharing.
    Map<String, List<SharableCache>> declaredGroups = new HashMap<>();
    for (NodeData node : nodes) {
        for (SpecializationData specialization : node.getSpecializations()) {
            for (CacheExpression cache : specialization.getCaches()) {
                if (cache.isAlwaysInitialized()) {
                    continue;
                }
                String group = cache.getSharedGroup();
                if (group != null) {
                    declaredGroups.computeIfAbsent(group, (v) -> new ArrayList<>()).add(new SharableCache(specialization, cache));
                }
            }
        }
    }
    Map<CacheExpression, String> sharedExpressions = new LinkedHashMap<>();
    for (NodeData node : nodes) {
        for (SpecializationData specialization : node.getSpecializations()) {
            for (CacheExpression cache : specialization.getCaches()) {
                if (cache.isAlwaysInitialized()) {
                    continue;
                }
                Element declaringElement;
                if (node.getTemplateType() instanceof GeneratedElement) {
                    // generated node
                    declaringElement = node.getTemplateType().getEnclosingElement();
                    if (!declaringElement.getKind().isClass() && !declaringElement.getKind().isInterface()) {
                        throw new AssertionError("Unexpected declared element for generated element: " + declaringElement.toString());
                    }
                } else {
                    declaringElement = node.getTemplateType();
                }
                String group = cache.getSharedGroup();
                SharableCache sharable = new SharableCache(specialization, cache);
                Collection<CacheExpression> expressions = groups.get(sharable);
                List<SharableCache> declaredSharing = declaredGroups.get(group);
                if (group != null) {
                    if (declaredSharing.size() <= 1) {
                        if (!ElementUtils.elementEquals(templateType, declaringElement)) {
                            // modifiable.
                            continue;
                        }
                    }
                    if (declaredSharing.size() <= 1 && (expressions == null || expressions.size() <= 1)) {
                        cache.addError(cache.getSharedGroupMirror(), cache.getSharedGroupValue(), "Could not find any other cached parameter that this parameter could be shared. " + "Cached parameters are only sharable if they declare the same type and initializer expressions and if the specialization only has a single instance. " + "Remove the @%s annotation or make the parameter sharable to resolve this.", types.Cached_Shared.asElement().getSimpleName().toString());
                    } else {
                        if (declaredSharing.size() <= 1) {
                            String error = String.format("No other cached parameters are specified as shared with the group '%s'.", group);
                            Set<String> similarGroups = new LinkedHashSet<>(declaredGroups.keySet());
                            similarGroups.remove(group);
                            List<String> fuzzyMatches = ExportsParser.fuzzyMatch(similarGroups, group, 0.7f);
                            if (!fuzzyMatches.isEmpty()) {
                                StringBuilder appendix = new StringBuilder(" Did you mean ");
                                String sep = "";
                                for (String string : fuzzyMatches) {
                                    appendix.append(sep);
                                    appendix.append('\'').append(string).append('\'');
                                    sep = ", ";
                                }
                                error += appendix.toString() + "?";
                            }
                            cache.addError(cache.getSharedGroupMirror(), cache.getSharedGroupValue(), error);
                        } else {
                            StringBuilder b = new StringBuilder();
                            for (SharableCache otherCache : declaredSharing) {
                                if (cache == otherCache.expression) {
                                    continue;
                                }
                                String reason = sharable.equalsWithReason(otherCache);
                                if (reason == null) {
                                    continue;
                                }
                                String signature = formatCacheExpression(otherCache.expression);
                                b.append(String.format("  - %s : %s%n", signature, reason));
                            }
                            if (b.length() != 0) {
                                cache.addError(cache.getSharedGroupMirror(), cache.getSharedGroupValue(), "Could not share some of the cached parameters in group '%s': %n%sRemove the @%s annotation or resolve the described issues to allow sharing.", group, b.toString(), types.Cached_Shared.asElement().getSimpleName().toString());
                            } else {
                                sharedExpressions.put(sharable.expression, group);
                            }
                        }
                    }
                } else if (expressions != null && expressions.size() > 1) {
                    if (emitSharingWarnings) {
                        /*
                             * We only emit sharing warnings for the same declaring type, because
                             * otherwise sharing warnings might not be resolvable if the base type
                             * is not modifiable.
                             */
                        List<CacheExpression> declaredInExpression = new ArrayList<>();
                        for (CacheExpression expression : expressions) {
                            if (ElementUtils.isDeclaredIn(expression.getParameter().getVariableElement(), declaringElement)) {
                                declaredInExpression.add(expression);
                            }
                        }
                        if (declaredInExpression.size() > 1 && findAnnotationMirror(cache.getParameter().getVariableElement(), types.Cached_Exclusive) == null) {
                            StringBuilder sharedCaches = new StringBuilder();
                            Set<String> recommendedGroups = new LinkedHashSet<>();
                            for (CacheExpression cacheExpression : declaredInExpression) {
                                if (cacheExpression != cache) {
                                    String signature = formatCacheExpression(cacheExpression);
                                    sharedCaches.append(String.format("  - %s%n", signature));
                                    String otherGroup = cacheExpression.getSharedGroup();
                                    if (otherGroup != null) {
                                        recommendedGroups.add(otherGroup);
                                    }
                                }
                            }
                            String recommendedGroup = recommendedGroups.size() == 1 ? recommendedGroups.iterator().next() : "group";
                            cache.addWarning("The cached parameter may be shared with: %n%s Annotate the parameter with @%s(\"%s\") or @%s to allow or deny sharing of the parameter.", sharedCaches, types.Cached_Shared.asElement().getSimpleName().toString(), recommendedGroup, types.Cached_Exclusive.asElement().getSimpleName().toString());
                        }
                    }
                }
            }
        }
    }
    return sharedExpressions;
}
Also used : Arrays(java.util.Arrays) ElementUtils.getQualifiedSuperTypeNames(com.oracle.truffle.dsl.processor.java.ElementUtils.getQualifiedSuperTypeNames) Modifier(javax.lang.model.element.Modifier) Parameter(com.oracle.truffle.dsl.processor.model.Parameter) AssumptionExpression(com.oracle.truffle.dsl.processor.model.AssumptionExpression) NodeCodeGenerator(com.oracle.truffle.dsl.processor.generator.NodeCodeGenerator) TypeElement(javax.lang.model.element.TypeElement) ElementUtils.uniqueSortedTypes(com.oracle.truffle.dsl.processor.java.ElementUtils.uniqueSortedTypes) NodeFieldData(com.oracle.truffle.dsl.processor.model.NodeFieldData) Map(java.util.Map) ElementUtils.firstLetterUpperCase(com.oracle.truffle.dsl.processor.java.ElementUtils.firstLetterUpperCase) Log(com.oracle.truffle.dsl.processor.Log) ArrayType(javax.lang.model.type.ArrayType) ClassLiteral(com.oracle.truffle.dsl.processor.expression.DSLExpression.ClassLiteral) GeneratedElement(com.oracle.truffle.dsl.processor.java.model.GeneratedElement) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) Set(java.util.Set) Element(javax.lang.model.element.Element) ElementUtils.createReferenceName(com.oracle.truffle.dsl.processor.java.ElementUtils.createReferenceName) GuardExpression(com.oracle.truffle.dsl.processor.model.GuardExpression) ElementUtils.collectAnnotations(com.oracle.truffle.dsl.processor.java.ElementUtils.collectAnnotations) ParameterSpec(com.oracle.truffle.dsl.processor.model.ParameterSpec) NodeChildData(com.oracle.truffle.dsl.processor.model.NodeChildData) Cardinality(com.oracle.truffle.dsl.processor.model.NodeChildData.Cardinality) ElementUtils.isAssignable(com.oracle.truffle.dsl.processor.java.ElementUtils.isAssignable) AnnotationValue(javax.lang.model.element.AnnotationValue) DSLExpression(com.oracle.truffle.dsl.processor.expression.DSLExpression) MessageContainer(com.oracle.truffle.dsl.processor.model.MessageContainer) VariableElement(javax.lang.model.element.VariableElement) TruffleTypes(com.oracle.truffle.dsl.processor.TruffleTypes) ElementUtils.fromTypeMirror(com.oracle.truffle.dsl.processor.java.ElementUtils.fromTypeMirror) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ElementUtils.findAnnotationMirror(com.oracle.truffle.dsl.processor.java.ElementUtils.findAnnotationMirror) ElementUtils.getVisibility(com.oracle.truffle.dsl.processor.java.ElementUtils.getVisibility) CacheExpression(com.oracle.truffle.dsl.processor.model.CacheExpression) SpecializationThrowsData(com.oracle.truffle.dsl.processor.model.SpecializationThrowsData) DeclaredType(javax.lang.model.type.DeclaredType) ElementFilter(javax.lang.model.util.ElementFilter) LinkedHashSet(java.util.LinkedHashSet) Variable(com.oracle.truffle.dsl.processor.expression.DSLExpression.Variable) LibraryData(com.oracle.truffle.dsl.processor.library.LibraryData) CompileErrorException(com.oracle.truffle.dsl.processor.CompileErrorException) ProcessorContext(com.oracle.truffle.dsl.processor.ProcessorContext) AnnotationMirror(javax.lang.model.element.AnnotationMirror) ElementUtils.getAnnotationValueList(com.oracle.truffle.dsl.processor.java.ElementUtils.getAnnotationValueList) Negate(com.oracle.truffle.dsl.processor.expression.DSLExpression.Negate) DSLExpressionVisitor(com.oracle.truffle.dsl.processor.expression.DSLExpression.DSLExpressionVisitor) ExecutableTypeData(com.oracle.truffle.dsl.processor.model.ExecutableTypeData) DSLExpressionResolver(com.oracle.truffle.dsl.processor.expression.DSLExpressionResolver) ListIterator(java.util.ListIterator) IntLiteral(com.oracle.truffle.dsl.processor.expression.DSLExpression.IntLiteral) MethodSpec(com.oracle.truffle.dsl.processor.model.MethodSpec) ElementUtils.getReadableSignature(com.oracle.truffle.dsl.processor.java.ElementUtils.getReadableSignature) LibraryParser(com.oracle.truffle.dsl.processor.library.LibraryParser) ElementUtils.getRepeatedAnnotation(com.oracle.truffle.dsl.processor.java.ElementUtils.getRepeatedAnnotation) ElementUtils.getSimpleName(com.oracle.truffle.dsl.processor.java.ElementUtils.getSimpleName) CodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror) NodeExecutionData(com.oracle.truffle.dsl.processor.model.NodeExecutionData) ElementUtils.getQualifiedName(com.oracle.truffle.dsl.processor.java.ElementUtils.getQualifiedName) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) TruffleProcessorOptions(com.oracle.truffle.dsl.processor.TruffleProcessorOptions) ElementUtils.getUniqueIdentifiers(com.oracle.truffle.dsl.processor.java.ElementUtils.getUniqueIdentifiers) NodeData(com.oracle.truffle.dsl.processor.model.NodeData) CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) TypeKind(javax.lang.model.type.TypeKind) Objects(java.util.Objects) List(java.util.List) ElementUtils.getCommonSuperType(com.oracle.truffle.dsl.processor.java.ElementUtils.getCommonSuperType) TemplateMethod(com.oracle.truffle.dsl.processor.model.TemplateMethod) ExportsParser(com.oracle.truffle.dsl.processor.library.ExportsParser) NodeFactoryFactory(com.oracle.truffle.dsl.processor.generator.NodeFactoryFactory) ElementUtils.isSubtypeBoxed(com.oracle.truffle.dsl.processor.java.ElementUtils.isSubtypeBoxed) Binary(com.oracle.truffle.dsl.processor.expression.DSLExpression.Binary) ArrayCodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror) ElementUtils.compareByTypeHierarchy(com.oracle.truffle.dsl.processor.java.ElementUtils.compareByTypeHierarchy) BooleanLiteral(com.oracle.truffle.dsl.processor.expression.DSLExpression.BooleanLiteral) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) TypeSystemData(com.oracle.truffle.dsl.processor.model.TypeSystemData) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) HashSet(java.util.HashSet) Kind(javax.tools.Diagnostic.Kind) CompilerFactory(com.oracle.truffle.dsl.processor.java.compiler.CompilerFactory) ElementUtils.isSubtype(com.oracle.truffle.dsl.processor.java.ElementUtils.isSubtype) ElementUtils.typeEquals(com.oracle.truffle.dsl.processor.java.ElementUtils.typeEquals) InvalidExpressionException(com.oracle.truffle.dsl.processor.expression.InvalidExpressionException) ElementUtils.getAnnotationValue(com.oracle.truffle.dsl.processor.java.ElementUtils.getAnnotationValue) ElementUtils.unboxAnnotationValue(com.oracle.truffle.dsl.processor.java.ElementUtils.unboxAnnotationValue) ElementUtils(com.oracle.truffle.dsl.processor.java.ElementUtils) Iterator(java.util.Iterator) ElementKind(javax.lang.model.element.ElementKind) Call(com.oracle.truffle.dsl.processor.expression.DSLExpression.Call) ExecutableElement(javax.lang.model.element.ExecutableElement) TypeMirror(javax.lang.model.type.TypeMirror) ElementUtils.modifiers(com.oracle.truffle.dsl.processor.java.ElementUtils.modifiers) ElementUtils.resolveAnnotationValue(com.oracle.truffle.dsl.processor.java.ElementUtils.resolveAnnotationValue) ElementUtils.firstLetterLowerCase(com.oracle.truffle.dsl.processor.java.ElementUtils.firstLetterLowerCase) Cast(com.oracle.truffle.dsl.processor.expression.DSLExpression.Cast) Comparator(java.util.Comparator) Collections(java.util.Collections) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) GeneratedElement(com.oracle.truffle.dsl.processor.java.model.GeneratedElement) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) TruffleTypes(com.oracle.truffle.dsl.processor.TruffleTypes) ArrayList(java.util.ArrayList) CacheExpression(com.oracle.truffle.dsl.processor.model.CacheExpression) LinkedHashMap(java.util.LinkedHashMap) GeneratedElement(com.oracle.truffle.dsl.processor.java.model.GeneratedElement) ArrayList(java.util.ArrayList) ElementUtils.getAnnotationValueList(com.oracle.truffle.dsl.processor.java.ElementUtils.getAnnotationValueList) List(java.util.List) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) NodeData(com.oracle.truffle.dsl.processor.model.NodeData) Collection(java.util.Collection)

Example 8 with TruffleTypes

use of com.oracle.truffle.dsl.processor.TruffleTypes in project graal by oracle.

the class VerifyCompilationFinalProcessor method checkDimensions.

private boolean checkDimensions(final VariableElement field) {
    TruffleTypes types = ProcessorContext.getInstance().getTypes();
    final AnnotationMirror compFin = ElementUtils.findAnnotationMirror(field, types.CompilerDirectives_CompilationFinal);
    if (compFin != null) {
        final int compFinDimensions = ElementUtils.getAnnotationValue(Integer.class, compFin, "dimensions");
        final int fieldDimensions = dimension(field.asType());
        if (compFinDimensions < -1) {
            emitError(field, "@CompilationFinal.dimensions cannot be negative.");
            return false;
        }
        if (compFinDimensions == -1 && fieldDimensions > 0) {
            final SuppressWarnings suppressWarnings = field.getAnnotation(SuppressWarnings.class);
            if (suppressWarnings != null) {
                for (String warning : suppressWarnings.value()) {
                    if ("VerifyCompilationFinal".equals(warning)) {
                        return true;
                    }
                }
            }
            emitWarning(field, "@CompilationFinal.dimensions should be given for an array type.");
            return false;
        }
        if (compFinDimensions > fieldDimensions) {
            if (fieldDimensions == 0) {
                emitError(field, String.format("Positive @CompilationFinal.dimensions (%d) not allowed for non array type.", compFinDimensions));
            } else {
                emitError(field, String.format("@CompilationFinal.dimensions (%d) cannot exceed the array's dimensions (%d).", compFinDimensions, fieldDimensions));
            }
            return false;
        }
    }
    return true;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TruffleTypes(com.oracle.truffle.dsl.processor.TruffleTypes)

Aggregations

TruffleTypes (com.oracle.truffle.dsl.processor.TruffleTypes)8 CodeVariableElement (com.oracle.truffle.dsl.processor.java.model.CodeVariableElement)4 TypeElement (javax.lang.model.element.TypeElement)4 ProcessorContext (com.oracle.truffle.dsl.processor.ProcessorContext)3 CodeTypeElement (com.oracle.truffle.dsl.processor.java.model.CodeTypeElement)3 VariableElement (javax.lang.model.element.VariableElement)3 CompileErrorException (com.oracle.truffle.dsl.processor.CompileErrorException)2 Log (com.oracle.truffle.dsl.processor.Log)2 TruffleProcessorOptions (com.oracle.truffle.dsl.processor.TruffleProcessorOptions)2 DSLExpression (com.oracle.truffle.dsl.processor.expression.DSLExpression)2 Binary (com.oracle.truffle.dsl.processor.expression.DSLExpression.Binary)2 BooleanLiteral (com.oracle.truffle.dsl.processor.expression.DSLExpression.BooleanLiteral)2 Call (com.oracle.truffle.dsl.processor.expression.DSLExpression.Call)2 Cast (com.oracle.truffle.dsl.processor.expression.DSLExpression.Cast)2 ClassLiteral (com.oracle.truffle.dsl.processor.expression.DSLExpression.ClassLiteral)2 DSLExpressionVisitor (com.oracle.truffle.dsl.processor.expression.DSLExpression.DSLExpressionVisitor)2 IntLiteral (com.oracle.truffle.dsl.processor.expression.DSLExpression.IntLiteral)2 Negate (com.oracle.truffle.dsl.processor.expression.DSLExpression.Negate)2 Variable (com.oracle.truffle.dsl.processor.expression.DSLExpression.Variable)2 DSLExpressionResolver (com.oracle.truffle.dsl.processor.expression.DSLExpressionResolver)2