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 import org.objectweb.asm.tree.VarInsnNode;
027
028 /** Differentiation transformer for DUP2_X2 instructions.
029 * <p>DUP2_X2 instructions are replaced by instructions
030 * that duplicate the two parts of a differential pair on stack.</p>
031 */
032 public class Dup2X2Transformer1 implements InstructionsTransformer {
033
034 // CHECKSTYLE: stop HideUtilityClassConstructor
035 /** Holder for the singleton instance.*/
036 private static class LazyHolder {
037 /** The singleton instance. */
038 private static final InstructionsTransformer INSTANCE = new Dup2X2Transformer1();
039 }
040 // CHECKSTYLE: resume HideUtilityClassConstructor
041
042 /** Hidden constructor.
043 */
044 private Dup2X2Transformer1() {
045 }
046
047 /** Get the singleton instance.
048 * <p>We use here the Initialization on Demand Holder idiom.</p>
049 * @return the singleton instance
050 */
051 public static InstructionsTransformer getInstance() {
052 return LazyHolder.INSTANCE;
053 }
054
055 /** {@inheritDoc} */
056 public InsnList getReplacement(final AbstractInsnNode insn,
057 final MethodDifferentiator methodDifferentiator)
058 throws DifferentiationException {
059
060 final int tmp1 = methodDifferentiator.getTmp(1);
061
062 final InsnList list = new InsnList();
063 // operand stack initial state: a0, a1, w
064 list.add(new InsnNode(Opcodes.DUP2_X2)); // => a0, w, a1, w
065 list.add(new InsnNode(Opcodes.POP2)); // => a0, w, a1
066 list.add(new VarInsnNode(Opcodes.DSTORE, tmp1)); // => a0, w
067 list.add(new InsnNode(Opcodes.DUP2_X2)); // => w, a0, w
068 list.add(new VarInsnNode(Opcodes.DLOAD, tmp1)); // => w, a0, w, a1
069 list.add(new InsnNode(Opcodes.DUP2_X2)); // => w, a0, a1, w, a1
070 list.add(new InsnNode(Opcodes.POP2)); // => w, a0, a1, w
071 return list;
072
073 }
074
075 }