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_X1 instructions. 029 * <p>DUP2_X1 instructions are replaced by instructions 030 * that duplicate the two parts of a differential pair on stack.</p> 031 */ 032 public class Dup2X1Transformer 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 Dup2X1Transformer(); 039 } 040 // CHECKSTYLE: resume HideUtilityClassConstructor 041 042 /** Hidden constructor. 043 */ 044 private Dup2X1Transformer() { 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 final int tmp2 = methodDifferentiator.getTmp(2); 062 063 final InsnList list = new InsnList(); 064 // operand stack initial state: v, a0, a1 065 list.add(new VarInsnNode(Opcodes.DSTORE, tmp1)); // => v, a0 066 list.add(new InsnNode(Opcodes.DUP2)); // => v, a0, a0 067 list.add(new VarInsnNode(Opcodes.DSTORE, tmp2)); // => v, a0 068 list.add(new InsnNode(Opcodes.DUP2_X1)); // => a0, v, a0 069 list.add(new InsnNode(Opcodes.POP2)); // => a0, v 070 list.add(new VarInsnNode(Opcodes.DLOAD, tmp1)); // => a0, v, a1 071 list.add(new InsnNode(Opcodes.DUP2_X1)); // => a0, a1, v, a1 072 list.add(new InsnNode(Opcodes.POP2)); // => a0, a1, v 073 list.add(new VarInsnNode(Opcodes.DLOAD, tmp2)); // => a0, a1, v, a0 074 list.add(new VarInsnNode(Opcodes.DLOAD, tmp1)); // => a0, a1, v, a0, a1 075 return list; 076 077 } 078 079 }