commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l..@apache.org
Subject svn commit: r1392844 - in /commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/forward: analysis/MethodDifferentiator.java instructions/GetFieldTransformer.java
Date Tue, 02 Oct 2012 11:20:03 GMT
Author: luc
Date: Tue Oct  2 11:20:03 2012
New Revision: 1392844

URL: http://svn.apache.org/viewvc?rev=1392844&view=rev
Log:
Set up a dedicated transformer for GETFIELD instructions.

Added:
    commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/forward/instructions/GetFieldTransformer.java
  (with props)
Modified:
    commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/forward/analysis/MethodDifferentiator.java

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=1392844&r1=1392843&r2=1392844&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 11:20:03 2012
@@ -41,6 +41,7 @@ import org.apache.commons.nabla.forward.
 import org.apache.commons.nabla.forward.instructions.Dup2Transformer;
 import org.apache.commons.nabla.forward.instructions.Dup2X1Transformer;
 import org.apache.commons.nabla.forward.instructions.Dup2X2Transformer;
+import org.apache.commons.nabla.forward.instructions.GetFieldTransformer;
 import org.apache.commons.nabla.forward.instructions.InvokeStaticTransformer;
 import org.apache.commons.nabla.forward.instructions.NarrowingTransformer;
 import org.apache.commons.nabla.forward.instructions.Pop2Transformer;
@@ -51,10 +52,8 @@ import org.apache.commons.nabla.forward.
 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.InsnList;
 import org.objectweb.asm.tree.InsnNode;
-import org.objectweb.asm.tree.LdcInsnNode;
 import org.objectweb.asm.tree.MethodInsnNode;
 import org.objectweb.asm.tree.MethodNode;
 import org.objectweb.asm.tree.TypeInsnNode;
@@ -106,6 +105,13 @@ public class MethodDifferentiator {
         return 1;
     }
 
+    /** Get the name of the derived class.
+     * @return name of the derived class
+     */
+    public String getDerivedName() {
+        return derivedName;
+    }
+
     /**
      * Differentiate a method.
      * @param primitiveName primitive class name
@@ -380,7 +386,7 @@ public class MethodDifferentiator {
                 // TODO: add support for PUTSTATIC differentiation
                 throw new RuntimeException("PUTSTATIC not handled yet");
             case Opcodes.GETFIELD :
-                return replaceGetField((FieldInsnNode) insn);
+                return new GetFieldTransformer().getReplacement(insn, this);
             case Opcodes.PUTFIELD :
                 // TODO: add support for PUTFIELD differentiation
                 throw new RuntimeException("PUTFIELD not handled yet");
@@ -413,79 +419,6 @@ public class MethodDifferentiator {
 
     }
 
-    /** Replace a GETFIELD instruction.
-     * @param fieldInsn field instruction
-     * @return replacement instructions list
-     * @exception DifferentiationException if instruction cannot be replaced
-     */
-    private InsnList replaceGetField(final FieldInsnNode fieldInsn) throws DifferentiationException
{
-
-        final InsnList list = new InsnList();
-
-        // get the field as an object
-        list.add(new LdcInsnNode(fieldInsn.name));
-        list.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, derivedName, "getPrimitiveField",
-                                    Type.getMethodDescriptor(Type.getType(Object.class),
-                                                             Type.getType(String.class))));
-
-        // convert it to the expected type
-        final Type type = Type.getType(fieldInsn.desc);
-        final Type boxedType;
-        final String valueMethodName;
-        switch (type.getSort()) {
-            case Type.VOID:
-                throw new DifferentiationException(NablaMessages.CANNOT_GET_VOID_FIELD, fieldInsn.name);
-            case Type.BOOLEAN:
-                valueMethodName = "booleanValue";
-                boxedType       = Type.getType(Boolean.class);
-                break;
-            case Type.CHAR:
-                valueMethodName = "charValue";
-                boxedType       = Type.getType(Character.class);
-                break;
-            case Type.BYTE:
-                valueMethodName = "byteValue";
-                boxedType       = Type.getType(Byte.class);
-                break;
-            case Type.SHORT:
-                valueMethodName = "shortValue";
-                boxedType       = Type.getType(Short.class);
-                break;
-            case Type.INT:
-                valueMethodName = "intValue";
-                boxedType       = Type.getType(Integer.class);
-                break;
-            case Type.FLOAT:
-                valueMethodName = "floatValue";
-                boxedType       = Type.getType(Float.class);
-                break;
-            case Type.LONG:
-                valueMethodName = "longValue";
-                boxedType       = Type.getType(Long.class);
-                break;
-            case Type.DOUBLE:
-                valueMethodName = "doubleValue";
-                boxedType       = Type.getType(Double.class);
-                break;
-            default :
-                // do nothing for Type.ARRAY and Type.OBJECT
-                valueMethodName = null;
-                boxedType       = null;
-
-        }
-        if (boxedType != null) {
-            list.add(new TypeInsnNode(Opcodes.CHECKCAST, boxedType.getInternalName()));
-        }
-        if (valueMethodName != null) {
-            list.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, boxedType.getInternalName(),
-                                        valueMethodName,
-                                        Type.getMethodDescriptor(type, new Type[0])));
-        }
-
-        return list;
-
-    }
-
     /** Test if a class is a math implementation class.
      * @param name name of the class to test
      * @return true if the named class is a math implementation class

Added: commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/forward/instructions/GetFieldTransformer.java
URL: http://svn.apache.org/viewvc/commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/forward/instructions/GetFieldTransformer.java?rev=1392844&view=auto
==============================================================================
--- commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/forward/instructions/GetFieldTransformer.java
(added)
+++ commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/forward/instructions/GetFieldTransformer.java
Tue Oct  2 11:20:03 2012
@@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.nabla.forward.instructions;
+
+import org.apache.commons.nabla.DifferentiationException;
+import org.apache.commons.nabla.NablaMessages;
+import org.apache.commons.nabla.forward.analysis.InstructionsTransformer;
+import org.apache.commons.nabla.forward.analysis.MethodDifferentiator;
+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.InsnList;
+import org.objectweb.asm.tree.LdcInsnNode;
+import org.objectweb.asm.tree.MethodInsnNode;
+import org.objectweb.asm.tree.TypeInsnNode;
+
+/** Differentiation transformer for GETFIELD instructions.
+ * <p>Each GETFIELD instruction is replaced by an instruction
+ * list getting the field from the primitive class using reflection.
+ * </p>
+ * @version $Id$
+ */
+public class GetFieldTransformer implements InstructionsTransformer {
+
+    /** Simple constructor.
+     */
+    public GetFieldTransformer() {
+    }
+
+    /** {@inheritDoc} */
+    public InsnList getReplacement(final AbstractInsnNode insn,
+                                   final MethodDifferentiator methodDifferentiator)
+        throws DifferentiationException {
+
+        final FieldInsnNode fieldInsn = (FieldInsnNode) insn;
+        final InsnList list = new InsnList();
+
+        // get the field as an object
+        list.add(new LdcInsnNode(fieldInsn.name));
+        list.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, methodDifferentiator.getDerivedName(),
+                                    "getPrimitiveField",
+                                    Type.getMethodDescriptor(Type.getType(Object.class),
+                                                             Type.getType(String.class))));
+
+        // convert it to the expected type
+        final Type type = Type.getType(fieldInsn.desc);
+        final Type boxedType;
+        final String valueMethodName;
+        switch (type.getSort()) {
+            case Type.VOID:
+                throw new DifferentiationException(NablaMessages.CANNOT_GET_VOID_FIELD, fieldInsn.name);
+            case Type.BOOLEAN:
+                valueMethodName = "booleanValue";
+                boxedType       = Type.getType(Boolean.class);
+                break;
+            case Type.CHAR:
+                valueMethodName = "charValue";
+                boxedType       = Type.getType(Character.class);
+                break;
+            case Type.BYTE:
+                valueMethodName = "byteValue";
+                boxedType       = Type.getType(Byte.class);
+                break;
+            case Type.SHORT:
+                valueMethodName = "shortValue";
+                boxedType       = Type.getType(Short.class);
+                break;
+            case Type.INT:
+                valueMethodName = "intValue";
+                boxedType       = Type.getType(Integer.class);
+                break;
+            case Type.FLOAT:
+                valueMethodName = "floatValue";
+                boxedType       = Type.getType(Float.class);
+                break;
+            case Type.LONG:
+                valueMethodName = "longValue";
+                boxedType       = Type.getType(Long.class);
+                break;
+            case Type.DOUBLE:
+                valueMethodName = "doubleValue";
+                boxedType       = Type.getType(Double.class);
+                break;
+            default :
+                // do nothing for Type.ARRAY and Type.OBJECT
+                valueMethodName = null;
+                boxedType       = null;
+
+        }
+        if (boxedType != null) {
+            list.add(new TypeInsnNode(Opcodes.CHECKCAST, boxedType.getInternalName()));
+        }
+        if (valueMethodName != null) {
+            list.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, boxedType.getInternalName(),
+                                        valueMethodName,
+                                        Type.getMethodDescriptor(type, new Type[0])));
+        }
+
+        return list;
+
+    }
+
+}

Propchange: commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/forward/instructions/GetFieldTransformer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/forward/instructions/GetFieldTransformer.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"



Mime
View raw message