changeset 19293:21b9b9941775

Truffle-DSL: initialize caches late if possible; fix assumption arrays need a @CompilationFinal to be checked.
author Christian Humer <christian.humer@gmail.com>
date Wed, 11 Feb 2015 12:13:44 +0100
parents 906367e494ca
children b31b2f289e7d
files graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/generator/NodeGenFactory.java graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/model/SpecializationData.java
diffstat 2 files changed, 46 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/generator/NodeGenFactory.java	Wed Feb 11 12:13:44 2015 +0100
+++ b/graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/generator/NodeGenFactory.java	Wed Feb 11 12:13:44 2015 +0100
@@ -1037,8 +1037,15 @@
             builder.tree(createTransferToInterpreterAndInvalidate());
         }
 
+        // caches unbound to guards are invoked after all guards
+        for (CacheExpression cache : specialization.getCaches()) {
+            if (!specialization.isCacheBoundByGuard(cache)) {
+                initializeCache(builder, specialization, cache, currentValues);
+            }
+        }
         boolean hasAssumptions = !specialization.getAssumptionExpressions().isEmpty();
         if (hasAssumptions) {
+
             for (AssumptionExpression assumption : specialization.getAssumptionExpressions()) {
                 CodeTree assumptions = DSLExpressionGenerator.write(assumption.getExpression(), accessParent(null),
                                 castBoundTypes(bindExpressionValues(assumption.getExpression(), specialization, currentValues)));
@@ -1327,7 +1334,8 @@
             for (AssumptionExpression assumption : specialization.getAssumptionExpressions()) {
                 String name = assumptionName(assumption);
                 TypeMirror type = assumption.getExpression().getResolvedType();
-                clazz.add(new CodeVariableElement(modifiers(PRIVATE, FINAL), type, name));
+                CodeVariableElement field = clazz.add(new CodeVariableElement(modifiers(PRIVATE, FINAL), type, name));
+                field.addAnnotationMirror(new CodeAnnotationMirror(context.getDeclaredType(CompilationFinal.class)));
                 constructor.addParameter(new CodeVariableElement(type, name));
                 builder.startStatement().string("this.").string(name).string(" = ").string(name).end();
             }
@@ -2229,20 +2237,25 @@
 
         if (specialization != null && !specializationExecution.isFastPath()) {
             for (CacheExpression cache : specialization.getCaches()) {
-                CodeTree initializer = DSLExpressionGenerator.write(cache.getExpression(), accessParent(null),
-                                castBoundTypes(bindExpressionValues(cache.getExpression(), specialization, currentValues)));
-                String name = cache.getParameter().getLocalName();
-                // multiple specializations might use the same name
-                String varName = name + specialization.getIndex();
-                TypeMirror type = cache.getParameter().getType();
-                localsBuilder.declaration(type, varName, initializer);
-                currentValues.set(name, new LocalVariable(null, type, varName, null));
+                if (specialization.isCacheBoundByGuard(cache)) {
+                    initializeCache(localsBuilder, specialization, cache, currentValues);
+                }
             }
         }
 
         return new CodeTree[]{checksBuilder.build(), localsBuilder.build()};
     }
 
+    private void initializeCache(CodeTreeBuilder builder, SpecializationData specialization, CacheExpression cache, LocalContext currentValues) {
+        CodeTree initializer = DSLExpressionGenerator.write(cache.getExpression(), accessParent(null), castBoundTypes(bindExpressionValues(cache.getExpression(), specialization, currentValues)));
+        String name = cache.getParameter().getLocalName();
+        // multiple specializations might use the same name
+        String varName = name + specialization.getIndex();
+        TypeMirror type = cache.getParameter().getType();
+        builder.declaration(type, varName, initializer);
+        currentValues.set(name, new LocalVariable(null, type, varName, null));
+    }
+
     public static final class LocalContext {
 
         private final NodeGenFactory factory;
--- a/graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/model/SpecializationData.java	Wed Feb 11 12:13:44 2015 +0100
+++ b/graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/model/SpecializationData.java	Wed Feb 11 12:13:44 2015 +0100
@@ -67,6 +67,30 @@
         }
     }
 
+    public boolean isCacheBoundByGuard(CacheExpression cacheExpression) {
+        for (GuardExpression expression : getGuards()) {
+            if (expression.getExpression().findBoundVariableElements().contains(cacheExpression.getParameter().getVariableElement())) {
+                return true;
+            }
+        }
+
+        // check all next binding caches if they are bound by guard
+        Set<VariableElement> boundVariables = cacheExpression.getExpression().findBoundVariableElements();
+        boolean found = false;
+        for (CacheExpression expression : getCaches()) {
+            if (cacheExpression == expression) {
+                found = true;
+            } else if (found) {
+                if (boundVariables.contains(expression.getParameter().getVariableElement())) {
+                    if (isCacheBoundByGuard(expression)) {
+                        return true;
+                    }
+                }
+            }
+        }
+        return false;
+    }
+
     public boolean isDynamicParameterBound(DSLExpression expression) {
         Set<VariableElement> boundVariables = expression.findBoundVariableElements();
         for (Parameter parameter : getDynamicParameters()) {