001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.nabla.forward.instructions;
018    
019    import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
020    import org.apache.commons.nabla.DifferentiationException;
021    import org.apache.commons.nabla.forward.analysis.InstructionsTransformer;
022    import org.apache.commons.nabla.forward.analysis.MethodDifferentiator;
023    import org.objectweb.asm.Opcodes;
024    import org.objectweb.asm.Type;
025    import org.objectweb.asm.tree.AbstractInsnNode;
026    import org.objectweb.asm.tree.InsnList;
027    import org.objectweb.asm.tree.InsnNode;
028    import org.objectweb.asm.tree.LdcInsnNode;
029    import org.objectweb.asm.tree.MethodInsnNode;
030    import org.objectweb.asm.tree.TypeInsnNode;
031    import org.objectweb.asm.tree.VarInsnNode;
032    
033    /** Differentiation transformer for promoting a double on the stack
034     * to a derivative structure.
035     * @version $Id$
036     */
037    public class WideningTransformer implements InstructionsTransformer {
038    
039        /** Simple constructor.
040         */
041        public WideningTransformer() {
042        }
043    
044        /** {@inheritDoc} */
045        public InsnList getReplacement(final AbstractInsnNode insn,
046                                       final MethodDifferentiator methodDifferentiator)
047            throws DifferentiationException {
048    
049            final InsnList list = new InsnList();
050    
051            // copy the instruction creating the double (DCONST_0, DCONST_1, LDC, I2D, L2D or F2D)
052            if (insn.getOpcode() == Opcodes.LDC) {
053                list.add(new LdcInsnNode(((LdcInsnNode) insn).cst));
054            } else {
055                list.add(new InsnNode(insn.getOpcode()));
056            }
057    
058            // operand stack initial state: d
059            list.add(new TypeInsnNode(Opcodes.NEW,
060                                      Type.getInternalName(DerivativeStructure.class)));      // => d y_ds
061            list.add(new InsnNode(Opcodes.DUP_X2));                                           // => y_ds d y_ds
062            list.add(new InsnNode(Opcodes.DUP_X2));                                           // => y_ds y_ds d y_ds
063            list.add(new InsnNode(Opcodes.POP));                                              // => y_ds y_ds d
064            list.add(new VarInsnNode(Opcodes.ALOAD, methodDifferentiator.getInputDSIndex())); // => y_ds y_ds d x_ds
065            list.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL,
066                                        Type.getInternalName(DerivativeStructure.class),
067                                        "getFreeParameters",
068                                        Type.getMethodDescriptor(Type.INT_TYPE)));            // => y_ds y_ds d params
069            list.add(new VarInsnNode(Opcodes.ALOAD, 1));                                      // => y_ds y_ds d params x_ds
070            list.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL,
071                                        Type.getInternalName(DerivativeStructure.class),
072                                        "getOrder",
073                                        Type.getMethodDescriptor(Type.INT_TYPE)));            // => y_ds y_ds d params order
074            list.add(new InsnNode(Opcodes.DUP2_X2));                                          // => y_ds y_ds params order d params order
075            list.add(new InsnNode(Opcodes.POP2));                                             // => y_ds y_ds params order d
076            list.add(new MethodInsnNode(Opcodes.INVOKESPECIAL,
077                                        Type.getInternalName(DerivativeStructure.class),
078                                        "<init>",
079                                        Type.getMethodDescriptor(Type.VOID_TYPE,
080                                                                 Type.INT_TYPE,
081                                                                 Type.INT_TYPE,
082                                                                 Type.DOUBLE_TYPE)));         // => y_ds
083    
084            return list;
085    
086        }
087    
088    }