Search in sources :

Example 1 with TruffleTypes

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

the class GeneratorUtils method addBoundaryOrTransferToInterpreter.

public static void addBoundaryOrTransferToInterpreter(CodeExecutableElement method, CodeTreeBuilder builder) {
    if (method != builder.findMethod()) {
        throw new AssertionError("Expected " + method + " but was " + builder.findMethod());
    }
    TruffleTypes types = ProcessorContext.getInstance().getTypes();
    if (ElementUtils.findAnnotationMirror(method, types.CompilerDirectives_TruffleBoundary) != null) {
        // already a boundary. nothing to do.
        return;
    }
    boolean hasFrame = false;
    for (VariableElement var : method.getParameters()) {
        if (ElementUtils.typeEquals(var.asType(), types.VirtualFrame)) {
            hasFrame = true;
            break;
        }
    }
    if (hasFrame) {
        builder.tree(GeneratorUtils.createTransferToInterpreterAndInvalidate());
    } else {
        method.addAnnotationMirror(new CodeAnnotationMirror(types.CompilerDirectives_TruffleBoundary));
    }
}
Also used : CodeAnnotationMirror(com.oracle.truffle.dsl.processor.java.model.CodeAnnotationMirror) TruffleTypes(com.oracle.truffle.dsl.processor.TruffleTypes) VariableElement(javax.lang.model.element.VariableElement) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement)

Example 2 with TruffleTypes

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

the class GeneratorUtils method pushEncapsulatingNode.

public static void pushEncapsulatingNode(CodeTreeBuilder builder, String nodeRef) {
    TruffleTypes types = ProcessorContext.getInstance().getTypes();
    // 
    builder.startStatement().type(types.EncapsulatingNodeReference).string(" encapsulating_ = ").startStaticCall(types.EncapsulatingNodeReference, "getCurrent").end().end();
    builder.startStatement().type(types.Node).string(" prev_ = encapsulating_.set(" + nodeRef + ")").end();
}
Also used : TruffleTypes(com.oracle.truffle.dsl.processor.TruffleTypes)

Example 3 with TruffleTypes

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

the class NodeParser method initializeFallbackReachability.

private static void initializeFallbackReachability(NodeData node) {
    TruffleTypes types = ProcessorContext.getInstance().getTypes();
    List<SpecializationData> specializations = node.getSpecializations();
    SpecializationData fallback = null;
    for (int i = specializations.size() - 1; i >= 0; i--) {
        SpecializationData specialization = specializations.get(i);
        if (specialization.isFallback() && specialization.getMethod() != null) {
            fallback = specialization;
            break;
        }
    }
    if (fallback == null) {
        // no need to compute reachability
        return;
    }
    for (int index = 0; index < specializations.size(); index++) {
        SpecializationData specialization = specializations.get(index);
        SpecializationData lastReachable = specialization;
        for (int searchIndex = index + 1; searchIndex < specializations.size(); searchIndex++) {
            SpecializationData search = specializations.get(searchIndex);
            if (search == fallback) {
                // reached the end of the specialization
                break;
            }
            assert lastReachable != search;
            if (!lastReachable.isReachableAfter(search)) {
                lastReachable = search;
            } else if (search.getReplaces().contains(specialization)) {
                lastReachable = search;
            }
        }
        specialization.setReachesFallback(lastReachable == specialization);
        List<SpecializationData> failedSpecializations = null;
        if (specialization.isReachesFallback() && !specialization.getCaches().isEmpty() && !specialization.getGuards().isEmpty()) {
            boolean failed = false;
            if (specialization.getMaximumNumberOfInstances() > 1) {
                for (GuardExpression guard : specialization.getGuards()) {
                    if (specialization.isGuardBoundWithCache(guard)) {
                        failed = true;
                        break;
                    }
                }
            }
            for (CacheExpression cache : specialization.getCaches()) {
                if (cache.isWeakReferenceGet()) {
                    failed = true;
                    break;
                }
            }
            if (failed) {
                if (failedSpecializations == null) {
                    failedSpecializations = new ArrayList<>();
                }
                failedSpecializations.add(specialization);
            }
        }
        if (failedSpecializations != null) {
            List<String> specializationIds = failedSpecializations.stream().map((e) -> e.getId()).collect(Collectors.toList());
            fallback.addError("Some guards for the following specializations could not be negated for the @%s specialization: %s. " + "Guards cannot be negated for the @%s when they bind @%s parameters and the specialization may consist of multiple instances or if any of the @%s parameters is configured as weak. " + "To fix this limit the number of instances to '1' or " + "introduce a more generic specialization declared between this specialization and the fallback. " + "Alternatively the use of @%s can be avoided by declaring a @%s with manually specified negated guards.", ElementUtils.getSimpleName(types.Fallback), specializationIds, ElementUtils.getSimpleName(types.Fallback), ElementUtils.getSimpleName(types.Cached), ElementUtils.getSimpleName(types.Cached), ElementUtils.getSimpleName(types.Fallback), ElementUtils.getSimpleName(types.Specialization));
        }
    }
}
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) GuardExpression(com.oracle.truffle.dsl.processor.model.GuardExpression) TruffleTypes(com.oracle.truffle.dsl.processor.TruffleTypes) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) CacheExpression(com.oracle.truffle.dsl.processor.model.CacheExpression)

Example 4 with TruffleTypes

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

the class VerifyTruffleProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    if (roundEnv.processingOver()) {
        return false;
    }
    ProcessorContext context = ProcessorContext.enter(processingEnv);
    try {
        TruffleTypes types = context.getTypes();
        TypeElement virtualFrameType = ElementUtils.castTypeElement(types.VirtualFrame);
        for (Element element : roundEnv.getElementsAnnotatedWith(ElementUtils.castTypeElement(types.CompilerDirectives_TruffleBoundary))) {
            scope = element;
            try {
                if (element.getKind() != ElementKind.CONSTRUCTOR && element.getKind() != ElementKind.METHOD) {
                    continue;
                }
                ExecutableElement method = (ExecutableElement) element;
                for (VariableElement parameter : method.getParameters()) {
                    Element paramType = processingEnv.getTypeUtils().asElement(parameter.asType());
                    if (paramType != null && paramType.equals(virtualFrameType)) {
                        errorMessage(element, "Method %s cannot be annotated with @%s and have a parameter of type %s", method.getSimpleName(), types.CompilerDirectives_TruffleBoundary.asElement().getSimpleName().toString(), paramType.getSimpleName());
                    }
                }
            } catch (Throwable t) {
                reportException(isBug367599(t) ? Kind.NOTE : Kind.ERROR, element, t);
            } finally {
                scope = null;
            }
        }
        TypeElement nodeType = ElementUtils.castTypeElement(types.Node);
        TypeElement nodeInterfaceType = ElementUtils.castTypeElement(types.NodeInterface);
        for (Element e : roundEnv.getElementsAnnotatedWith(ElementUtils.castTypeElement(types.Node_Child))) {
            if (e.getModifiers().contains(Modifier.FINAL)) {
                emitError("@Child field cannot be final", e);
                continue;
            }
            if (!processingEnv.getTypeUtils().isSubtype(e.asType(), nodeInterfaceType.asType())) {
                emitError("@Child field must implement NodeInterface", e);
                continue;
            }
            if (!processingEnv.getTypeUtils().isSubtype(e.getEnclosingElement().asType(), nodeType.asType())) {
                emitError("@Child field is allowed only in Node sub-class", e);
                continue;
            }
            if (ElementUtils.findAnnotationMirror(e, types.Executed) == null) {
                assertNoErrorExpected(e);
            }
        }
        for (Element annotatedField : roundEnv.getElementsAnnotatedWith(ElementUtils.castTypeElement(types.Node_Children))) {
            boolean reportError = false;
            TypeMirror annotatedFieldType = annotatedField.asType();
            if (annotatedFieldType.getKind() == TypeKind.ARRAY) {
                TypeMirror compomentType = ((ArrayType) annotatedFieldType).getComponentType();
                if (!processingEnv.getTypeUtils().isSubtype(compomentType, nodeInterfaceType.asType())) {
                    reportError = true;
                }
            } else {
                reportError = true;
            }
            if (reportError) {
                emitError("@Children field must be an array of NodeInerface sub-types", annotatedField);
                continue;
            }
            if (!processingEnv.getTypeUtils().isSubtype(annotatedField.getEnclosingElement().asType(), nodeType.asType())) {
                emitError("@Children field is allowed only in Node sub-class", annotatedField);
                continue;
            }
            if (ElementUtils.findAnnotationMirror(annotatedField, types.Executed) == null) {
                assertNoErrorExpected(annotatedField);
            }
        }
        return false;
    } finally {
        ProcessorContext.leave();
    }
}
Also used : ArrayType(javax.lang.model.type.ArrayType) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) TruffleTypes(com.oracle.truffle.dsl.processor.TruffleTypes) VariableElement(javax.lang.model.element.VariableElement) ProcessorContext(com.oracle.truffle.dsl.processor.ProcessorContext)

Example 5 with TruffleTypes

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

the class FlatNodeGenFactory method createLanguageReferenceConstant.

public static CodeVariableElement createLanguageReferenceConstant(StaticConstants constants, TypeMirror languageType) {
    TruffleTypes types = ProcessorContext.getInstance().getTypes();
    String constantName = ElementUtils.createConstantName(ElementUtils.getSimpleName(languageType) + "Lref");
    TypeElement languageReference = (TypeElement) types.TruffleLanguage_LanguageReference.asElement();
    DeclaredCodeTypeMirror constantType = new DeclaredCodeTypeMirror(languageReference, Arrays.asList(languageType));
    return lookupConstant(constants.languageReferences, constantName, (name) -> {
        CodeVariableElement newVar = new CodeVariableElement(modifiers(PRIVATE, STATIC, FINAL), constantType, name);
        newVar.createInitBuilder().startStaticCall(languageReference.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)

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