| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| DReturnTransformer |
|
| 1.0;1 | ||||
| DReturnTransformer$1 |
|
| 1.0;1 | ||||
| DReturnTransformer$LazyHolder |
|
| 1.0;1 |
| 1 | /* | |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
| 3 | * contributor license agreements. See the NOTICE file distributed with | |
| 4 | * this work for additional information regarding copyright ownership. | |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
| 6 | * (the "License"); you may not use this file except in compliance with | |
| 7 | * the License. You may obtain a copy of the License at | |
| 8 | * | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 10 | * | |
| 11 | * Unless required by applicable law or agreed to in writing, software | |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 14 | * See the License for the specific language governing permissions and | |
| 15 | * limitations under the License. | |
| 16 | */ | |
| 17 | package org.apache.commons.nabla.automatic.instructions; | |
| 18 | ||
| 19 | import org.apache.commons.nabla.automatic.analysis.InstructionsTransformer; | |
| 20 | import org.apache.commons.nabla.automatic.analysis.MethodDifferentiator; | |
| 21 | import org.apache.commons.nabla.core.DifferentiationException; | |
| 22 | import org.objectweb.asm.Opcodes; | |
| 23 | import org.objectweb.asm.tree.AbstractInsnNode; | |
| 24 | import org.objectweb.asm.tree.InsnList; | |
| 25 | import org.objectweb.asm.tree.InsnNode; | |
| 26 | import org.objectweb.asm.tree.MethodInsnNode; | |
| 27 | import org.objectweb.asm.tree.TypeInsnNode; | |
| 28 | import org.objectweb.asm.tree.VarInsnNode; | |
| 29 | ||
| 30 | /** Differentiation transformer for DRETURN instructions. | |
| 31 | * <p>DRETURN instructions are replaced by instructions | |
| 32 | * that build a {@link org.apache.commons.nabla.core.DifferentialPair | |
| 33 | * DifferentialPair} instance from the expanded differential pair | |
| 34 | * on operand stack head and an ARETURN instruction returning this | |
| 35 | * instance.</p> | |
| 36 | */ | |
| 37 | 1 | public class DReturnTransformer implements InstructionsTransformer { |
| 38 | ||
| 39 | /** Holder for the singleton instance.*/ | |
| 40 | 65 | private static class LazyHolder { |
| 41 | /** The singleton instance. */ | |
| 42 | 1 | private static final InstructionsTransformer INSTANCE = new DReturnTransformer(); |
| 43 | } | |
| 44 | ||
| 45 | /** Hidden constructor. | |
| 46 | */ | |
| 47 | 1 | private DReturnTransformer() { |
| 48 | 1 | } |
| 49 | ||
| 50 | /** Get the singleton instance. | |
| 51 | * <p>We use here the Initialization on Demand Holder idiom.</p> | |
| 52 | * @return the singleton instance | |
| 53 | */ | |
| 54 | public static InstructionsTransformer getInstance() { | |
| 55 | 65 | return LazyHolder.INSTANCE; |
| 56 | } | |
| 57 | ||
| 58 | /** {@inheritDoc} */ | |
| 59 | public InsnList getReplacement(final AbstractInsnNode insn, | |
| 60 | final MethodDifferentiator methodDifferentiator) | |
| 61 | throws DifferentiationException { | |
| 62 | ||
| 63 | // reuse local variables slots 1, 2, 3 and 4 for temporary storage | |
| 64 | // (this may reduce the number of needed local variables) | |
| 65 | 65 | methodDifferentiator.useLocal(1, 4); |
| 66 | ||
| 67 | 65 | final InsnList list = new InsnList(); |
| 68 | // operand stack initial state: a0, a1 | |
| 69 | 65 | list.add(new VarInsnNode(Opcodes.DSTORE, 3)); // => a0 |
| 70 | 65 | list.add(new VarInsnNode(Opcodes.DSTORE, 1)); // => |
| 71 | 65 | list.add(new TypeInsnNode(Opcodes.NEW, |
| 72 | MethodDifferentiator.DP_NAME)); // => o, | |
| 73 | 65 | list.add(new InsnNode(Opcodes.DUP)); // => o, o |
| 74 | 65 | list.add(new VarInsnNode(Opcodes.DLOAD, 1)); // => o, o, a0 |
| 75 | 65 | list.add(new VarInsnNode(Opcodes.DLOAD, 3)); // => o, o, a0, a1 |
| 76 | 65 | list.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, |
| 77 | MethodDifferentiator.DP_NAME, | |
| 78 | "<init>", "(DD)V")); // => dp | |
| 79 | 65 | list.add(new InsnNode(Opcodes.ARETURN)); // => |
| 80 | 65 | return list; |
| 81 | ||
| 82 | } | |
| 83 | ||
| 84 | } |