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.algorithmic.forward.instructions;
018    
019    import org.apache.commons.nabla.algorithmic.forward.analysis.InstructionsTransformer;
020    import org.apache.commons.nabla.algorithmic.forward.analysis.MethodDifferentiator;
021    import org.apache.commons.nabla.core.DifferentiationException;
022    import org.objectweb.asm.Opcodes;
023    import org.objectweb.asm.tree.AbstractInsnNode;
024    import org.objectweb.asm.tree.InsnList;
025    import org.objectweb.asm.tree.InsnNode;
026    
027    /** Differentiation transformer for promoting a double on the stack
028     * to a differential pair.
029     * <p>Some instructions pushing a double on the stack simply need to
030     * be completed by pushing a 0 afterwards to form a differential pair
031     * representing a constant value. These instructions are replaced by
032     * a copy of themselves followed by a DCONST_0 instruction.</p>
033     */
034    public class WideningTransformer implements InstructionsTransformer {
035    
036        // CHECKSTYLE: stop HideUtilityClassConstructor
037        /** Holder for the singleton instance.*/
038        private static class LazyHolder  {
039            /** The singleton instance. */
040            private static final InstructionsTransformer INSTANCE = new WideningTransformer();
041        }
042        // CHECKSTYLE: resume HideUtilityClassConstructor
043    
044        /** Hidden constructor.
045         */
046        private WideningTransformer() {
047        }
048    
049        /** Get the singleton instance.
050         * <p>We use here the Initialization on Demand Holder idiom.</p>
051         * @return the singleton instance
052         */
053        public static InstructionsTransformer getInstance() {
054            return LazyHolder.INSTANCE;
055        }
056    
057        /** {@inheritDoc} */
058        public InsnList getReplacement(final AbstractInsnNode insn,
059                                       final MethodDifferentiator methodDifferentiator)
060            throws DifferentiationException {
061            final InsnList list = new InsnList();
062            list.add(methodDifferentiator.clone(insn));
063            list.add(new InsnNode(Opcodes.DCONST_0));
064            return list;
065        }
066    
067    }