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 DCMPx instructions.
028 * <p>Each DCMPx instruction is replaced by a sequence that
029 * first remove the differential parts from the top stack cells
030 * followed by the comparison instruction.
031 */
032 public class DcmpTransformer1 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 DcmpTransformer1();
039 }
040 // CHECKSTYLE: resume HideUtilityClassConstructor
041
042 /** Hidden constructor.
043 */
044 private DcmpTransformer1() {
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 final InsnList list = new InsnList();
060 // operand stack initial state: a0, a1, b
061 list.add(new InsnNode(Opcodes.DUP2_X2)); // => a0, b, a1, b
062 list.add(new InsnNode(Opcodes.POP2)); // => a0, b, a1
063 list.add(new InsnNode(Opcodes.POP2)); // => a0, b
064 list.add(methodDifferentiator.clone(insn));
065 return list;
066 }
067
068 }