View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.nabla.forward.instructions;
18  
19  import org.apache.commons.nabla.DifferentiationException;
20  import org.apache.commons.nabla.forward.analysis.InstructionsTransformer;
21  import org.apache.commons.nabla.forward.analysis.MethodDifferentiator;
22  import org.objectweb.asm.Opcodes;
23  import org.objectweb.asm.Type;
24  import org.objectweb.asm.tree.AbstractInsnNode;
25  import org.objectweb.asm.tree.InsnList;
26  import org.objectweb.asm.tree.InsnNode;
27  import org.objectweb.asm.tree.MethodInsnNode;
28  
29  /** Differentiation transformer for DCMPx instructions.
30   * @version $Id$
31   */
32  public class DcmpTransformer implements InstructionsTransformer {
33  
34      /** Name of the {@link org.apache.commons.math3.analysis.differentiation.DerivativeStructure} method corresponding to the value getter. */
35      private static final String VALUE_GETTER_METHOD = "getValue";
36  
37      /** Indicator for top stack element conversion. */
38      private final boolean stack0Converted;
39  
40      /** Indicator for next to top stack element conversion. */
41      private final boolean stack1Converted;
42  
43      /** Simple constructor.
44       * @param stack0Converted if true, the top level stack element has already been converted
45       * @param stack1Converted if true, the next to top level stack element has already been converted
46       */
47      public DcmpTransformer(final boolean stack0Converted, final boolean stack1Converted) {
48          this.stack0Converted = stack0Converted;
49          this.stack1Converted = stack1Converted;
50      }
51  
52      /** {@inheritDoc} */
53      public InsnList getReplacement(final AbstractInsnNode insn,
54                                     final MethodDifferentiator methodDifferentiator)
55          throws DifferentiationException {
56          final InsnList list = new InsnList();
57  
58          if (stack1Converted) {
59              if (stack0Converted) {
60                  // operand stack initial state: ds_a, ds_b
61                  list.add(new InsnNode(Opcodes.SWAP));                                              // => ds_b, ds_a
62                  list.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, DS_TYPE.getInternalName(),
63                                              VALUE_GETTER_METHOD,
64                                              Type.getMethodDescriptor(Type.DOUBLE_TYPE)));          // => ds_b, a0
65                  list.add(new InsnNode(Opcodes.DUP2_X2));                                           // => a0, ds_b, a0
66                  list.add(new InsnNode(Opcodes.POP2));                                              // => a0, ds_b
67                  list.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, DS_TYPE.getInternalName(),
68                                              VALUE_GETTER_METHOD,
69                                              Type.getMethodDescriptor(Type.DOUBLE_TYPE)));          // => a0, b0
70              } else {
71                  // operand stack initial state: ds_a, b0
72                  list.add(new InsnNode(Opcodes.DUP2_X2));                                           // => b0, ds_a, b0
73                  list.add(new InsnNode(Opcodes.POP2));                                              // => b0, ds_a
74                  list.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, DS_TYPE.getInternalName(),
75                                              VALUE_GETTER_METHOD,
76                                              Type.getMethodDescriptor(Type.DOUBLE_TYPE)));          // => b0, a0
77                  list.add(new InsnNode(Opcodes.DUP2_X2));                                           // => a0, b0, a0
78                  list.add(new InsnNode(Opcodes.POP2));                                              // => a0, b0
79              }
80          } else {
81              // operand stack initial state: a, ds_b
82              list.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, DS_TYPE.getInternalName(),
83                                          VALUE_GETTER_METHOD,
84                                          Type.getMethodDescriptor(Type.DOUBLE_TYPE)));              // => a0, b0
85          }
86  
87          list.add(new InsnNode(insn.getOpcode()));                                                  // => bool
88  
89          return list;
90  
91      }
92  
93  }