commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l..@apache.org
Subject svn commit: r1392815 - in /commons/sandbox/nabla/trunk/src: main/java/org/apache/commons/nabla/ main/java/org/apache/commons/nabla/forward/analysis/ main/resources/assets/org/apache/commons/nabla/ test/java/org/apache/commons/nabla/
Date Tue, 02 Oct 2012 09:37:06 GMT
Author: luc
Date: Tue Oct  2 09:37:06 2012
New Revision: 1392815

URL: http://svn.apache.org/viewvc?rev=1392815&view=rev
Log:
Removed all local variables changes.

Since we have switched to DerivativeStructure for computation, we can
store the references in the same slots that were used to store double
values in the original code (they are smaller and fit in the available
memory cell). So we don't need to mess with the local variables anymore.
The differentiated method will always use local variables in the same
way as the original method, just changing their type.

Modified:
    commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/NablaMessages.java
    commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/forward/analysis/MethodDifferentiator.java
    commons/sandbox/nabla/trunk/src/main/resources/assets/org/apache/commons/nabla/NablaMessages_fr.properties
    commons/sandbox/nabla/trunk/src/test/java/org/apache/commons/nabla/NablaMessagesTest.java

Modified: commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/NablaMessages.java
URL: http://svn.apache.org/viewvc/commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/NablaMessages.java?rev=1392815&r1=1392814&r2=1392815&view=diff
==============================================================================
--- commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/NablaMessages.java
(original)
+++ commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/NablaMessages.java
Tue Oct  2 09:37:06 2012
@@ -52,8 +52,6 @@ public enum NablaMessages implements Loc
     CLASS_DOES_NOT_IMPLEMENT_INTERFACE("the {0} class does not implement the {1} interface"),
     UNABLE_TO_ANALYZE_METHOD("unable to analyze the {0}.{1} method ({2})"),
     UNKNOWN_METHOD("unknown method {0}.{1}"),
-    NUMBER_OF_TEMPORARY_VARIABLES_OUT_OF_RANGE("number of temporary variable ({0}) outside
of [{1}, {2}] range"),
-    INDEX_OF_LOCAL_VARIABLE_OUT_OF_RANGE("index of size {0} local variable ({1}) outside
of [{2}, {3}] range"),
     UNEXPECTED_INSTRUCTION("unexpected instruction with opcode {0}"),
     UNABLE_TO_HANDLE_INSTRUCTION("unable to handle instruction with opcode {0}"),
     CANNOT_GET_VOID_FIELD("unable to get value of void type field {0}"),

Modified: commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/forward/analysis/MethodDifferentiator.java
URL: http://svn.apache.org/viewvc/commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/forward/analysis/MethodDifferentiator.java?rev=1392815&r1=1392814&r2=1392815&view=diff
==============================================================================
--- commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/forward/analysis/MethodDifferentiator.java
(original)
+++ commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/forward/analysis/MethodDifferentiator.java
Tue Oct  2 09:37:06 2012
@@ -52,7 +52,6 @@ import org.objectweb.asm.Opcodes;
 import org.objectweb.asm.Type;
 import org.objectweb.asm.tree.AbstractInsnNode;
 import org.objectweb.asm.tree.FieldInsnNode;
-import org.objectweb.asm.tree.IincInsnNode;
 import org.objectweb.asm.tree.InsnList;
 import org.objectweb.asm.tree.InsnNode;
 import org.objectweb.asm.tree.LdcInsnNode;
@@ -71,18 +70,12 @@ import org.objectweb.asm.tree.analysis.I
  */
 public class MethodDifferentiator {
 
-    /** Maximal number of temporary size 2 variables. */
-    private static final int MAX_TEMP = 5;
-
     /** Math implementation classes. */
     private final Set<String> mathClasses;
 
     /** Name of the derived class. */
     private final String derivedName;
 
-    /** Used locals variables array. */
-    private boolean[] usedLocals;
-
     /** Set of converted values. */
     private final Set<TrackingValue> converted;
 
@@ -97,7 +90,6 @@ public class MethodDifferentiator {
      * @param derivedName name of the derived class
      */
     public MethodDifferentiator(final Set<String> mathClasses, final String derivedName)
{
-        this.usedLocals   = null;
         this.mathClasses  = mathClasses;
         this.derivedName  = derivedName;
         this.converted    = new HashSet<TrackingValue>();
@@ -124,17 +116,8 @@ public class MethodDifferentiator {
         throws DifferentiationException {
         try {
 
-            // at start, "this" and one DerivativeStructure are already used
-            method.maxLocals  = 2 * (method.maxLocals + MAX_TEMP) - 1;
-            usedLocals = new boolean[method.maxLocals];
-            useLocal(0, 1);
-            useLocal(1, 4);
-
             final Type dsType = Type.getType(DerivativeStructure.class);
 
-            // add spare cells to hold new variables if needed
-            addSpareLocalVariables(method.instructions);
-
             // analyze the original code, tracing values production/consumption
             final FlowAnalyzer analyzer =
                 new FlowAnalyzer(new TrackingInterpreter(), method.instructions);
@@ -182,13 +165,9 @@ public class MethodDifferentiator {
 
             }
 
-            // remove the local variables added at the beginning and not used
-            removeUnusedSpareLocalVariables(method.instructions);
-
             // set the method properties
-            method.desc       = Type.getMethodDescriptor(dsType, dsType);
-            method.access    |= Opcodes.ACC_SYNTHETIC;
-            method.maxLocals  = maxVariables();
+            method.desc    = Type.getMethodDescriptor(dsType, dsType);
+            method.access |= Opcodes.ACC_SYNTHETIC;
 
         } catch (AnalyzerException ae) {
             ae.printStackTrace(System.err);
@@ -201,60 +180,6 @@ public class MethodDifferentiator {
         }
     }
 
-    /** Add spare cells for new local variables.
-     * <p>In order to ease conversion from double values to derivative structures,
-     * we start by reserving one spare cell between each original local variables.
-     * So we have to modify the indices in all instructions referencing local
-     * variables in the original code, to take into account the renumbering
-     * introduced by these spare cells. The spare cells by themselves will
-     * be referenced by the converted instructions in the following passes.</p>
-     * <p>The spare cells that will not be used will be reclaimed after
-     * conversion, to avoid wasting memory.</p>
-     * @param instructions instructions of the method
-     * @exception DifferentiationException if local variables array has not been
-     * expanded appropriately beforehand
-     * @see #removeUnusedSpareLocalVariables()
-     */
-    private void addSpareLocalVariables(final InsnList instructions)
-        throws DifferentiationException {
-        for (final Iterator<AbstractInsnNode> i = instructions.iterator(); i.hasNext();)
{
-            final AbstractInsnNode insn = i.next();
-            if (insn.getType() == AbstractInsnNode.VAR_INSN) {
-                final VarInsnNode varInsn = (VarInsnNode) insn;
-                if (varInsn.var > 2) {
-                    varInsn.var = 2 * varInsn.var - 1;
-                    final int opcode = varInsn.getOpcode();
-                    if ((opcode == Opcodes.ILOAD)  || (opcode == Opcodes.FLOAD)  ||
-                        (opcode == Opcodes.ALOAD)  || (opcode == Opcodes.ISTORE) ||
-                        (opcode == Opcodes.FSTORE) || (opcode == Opcodes.ASTORE)) {
-                        useLocal(varInsn.var, 1);
-                    } else {
-                        useLocal(varInsn.var, 2);
-                    }
-                }
-            } else if (insn.getOpcode() == Opcodes.IINC) {
-                final IincInsnNode iincInsn = (IincInsnNode) insn;
-                if (iincInsn.var > 2) {
-                    iincInsn.var = 2 * iincInsn.var - 1;
-                    useLocal(iincInsn.var, 1);
-                }
-            }
-        }
-    }
-
-    /** Remove the unused spare cells introduced at conversion start.
-     * @param instructions instructions of the method
-     * @see #addSpareLocalVariables()
-     */
-    private void removeUnusedSpareLocalVariables(final InsnList instructions) {
-        for (final Iterator<AbstractInsnNode> i = instructions.iterator(); i.hasNext();)
{
-            final AbstractInsnNode insn = i.next();
-            if (insn.getType() == AbstractInsnNode.VAR_INSN) {
-                shiftVariable((VarInsnNode) insn);
-            }
-        }
-    }
-
     /** Identify the instructions that must be changed.
      * <p>Identification is based on data flow analysis. We start by changing
      * the local variables in the initial frame to match the parameters of
@@ -314,7 +239,7 @@ public class MethodDifferentiator {
         }
 
         // the various GETFIELD/PUTFIELD instructions must also be changed
-        // to retrieve the field from the outer class
+        // to retrieve the field from the primitive class
         final ListIterator<AbstractInsnNode> iterator = instructions.iterator();
         while (iterator.hasNext()) {
             final AbstractInsnNode ins = iterator.next();
@@ -392,13 +317,11 @@ public class MethodDifferentiator {
 
         switch(insn.getOpcode()) {
             case Opcodes.DLOAD :
-                useLocal(((VarInsnNode) insn).var, 4);
                 return new DLoadTransformer().getReplacement(insn, this);
             case Opcodes.DALOAD :
                 // TODO: add support for DALOAD differentiation
                 throw new RuntimeException("DALOAD not handled yet");
             case Opcodes.DSTORE :
-                useLocal(((VarInsnNode) insn).var, 4);
                 return new DStoreTransformer().getReplacement(insn, this);
             case Opcodes.DASTORE :
                 // TODO: add support for DASTORE differentiation
@@ -598,50 +521,6 @@ public class MethodDifferentiator {
 
     }
 
-    /** Set a local variable as used by the modified code.
-     * @param index index of the variable
-     * @param size size of the variable (1 or 2 for standard variables,
-     * 4 for special expanded derivative structures)
-     * @exception DifferentiationException if the number of the
-     * temporary variable lies outside of the allowed range
-     */
-    public void useLocal(final int index, final int size)
-        throws DifferentiationException {
-        if ((index < 0) || ((index + size) > usedLocals.length)) {
-            throw new DifferentiationException(NablaMessages.INDEX_OF_LOCAL_VARIABLE_OUT_OF_RANGE,
-                                               size, index, 1, MAX_TEMP);
-        }
-        for (int i = index; i < index + size; ++i) {
-            usedLocals[i] = true;
-        }
-    }
-
-    /** Shifted the index of a variable instruction.
-     * @param insn variable instruction
-     */
-    private void shiftVariable(final VarInsnNode insn) {
-        int shifted = 0;
-        for (int i = 0; i < insn.var; ++i) {
-            if (usedLocals[i]) {
-                ++shifted;
-            }
-        }
-        insn.var = shifted;
-    }
-
-    /** Compute the maximal number of used local variables.
-     * @return maximal number of used local variables
-     */
-    private int maxVariables() {
-        int max = 0;
-        for (final boolean isUsed : usedLocals) {
-            if (isUsed) {
-                ++max;
-            }
-        }
-        return max;
-    }
-
     /** Analyzer preserving instructions successors information. */
     private class FlowAnalyzer extends Analyzer<TrackingValue> {
 

Modified: commons/sandbox/nabla/trunk/src/main/resources/assets/org/apache/commons/nabla/NablaMessages_fr.properties
URL: http://svn.apache.org/viewvc/commons/sandbox/nabla/trunk/src/main/resources/assets/org/apache/commons/nabla/NablaMessages_fr.properties?rev=1392815&r1=1392814&r2=1392815&view=diff
==============================================================================
--- commons/sandbox/nabla/trunk/src/main/resources/assets/org/apache/commons/nabla/NablaMessages_fr.properties
(original)
+++ commons/sandbox/nabla/trunk/src/main/resources/assets/org/apache/commons/nabla/NablaMessages_fr.properties
Tue Oct  2 09:37:06 2012
@@ -23,8 +23,6 @@ INTERFACE_NOT_FOUND_WHILE_DIFFERENTIATIN
 CLASS_DOES_NOT_IMPLEMENT_INTERFACE = la classe {0} ne met pas en \u0153uvre l''interface
{1}
 UNABLE_TO_ANALYZE_METHOD = impossible d''analyser la m\u00e9thode {0}.{1} ({2})
 UNKNOWN_METHOD = m\u00e9thode {0}.{1} inconnue
-NUMBER_OF_TEMPORARY_VARIABLES_OUT_OF_RANGE = num\u00e9ro de la variable temporaire ({0})
hors du domaine [{1}, {2}]
-INDEX_OF_LOCAL_VARIABLE_OUT_OF_RANGE = index ({1}) de la variable locale de taille {0} hors
du domaine [{2}, {3}]
 UNEXPECTED_INSTRUCTION = instruction ayant l''opcode {0} inattendue
 UNABLE_TO_HANDLE_INSTRUCTION = incapable de g\u00e9rer une instruction ayant l''opcode {0}
 CANNOT_GET_VOID_FIELD = impossible de r\u00e9cup\u00e9rer la valeur du champ {0} de type
void

Modified: commons/sandbox/nabla/trunk/src/test/java/org/apache/commons/nabla/NablaMessagesTest.java
URL: http://svn.apache.org/viewvc/commons/sandbox/nabla/trunk/src/test/java/org/apache/commons/nabla/NablaMessagesTest.java?rev=1392815&r1=1392814&r2=1392815&view=diff
==============================================================================
--- commons/sandbox/nabla/trunk/src/test/java/org/apache/commons/nabla/NablaMessagesTest.java
(original)
+++ commons/sandbox/nabla/trunk/src/test/java/org/apache/commons/nabla/NablaMessagesTest.java
Tue Oct  2 09:37:06 2012
@@ -36,7 +36,7 @@ public class NablaMessagesTest {
 
     @Test
     public void testMessageNumber() {
-        Assert.assertEquals(17, NablaMessages.values().length);
+        Assert.assertEquals(15, NablaMessages.values().length);
     }
 
     @Test



Mime
View raw message