1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.apache.commons.nabla.forward.arithmetic; |
18 | |
|
19 | |
import org.apache.commons.nabla.DifferentiationException; |
20 | |
import org.apache.commons.nabla.forward.analysis.InstructionsTransformer; |
21 | |
import org.apache.commons.nabla.forward.analysis.MethodDifferentiator; |
22 | |
import org.objectweb.asm.Opcodes; |
23 | |
import org.objectweb.asm.Type; |
24 | |
import org.objectweb.asm.tree.AbstractInsnNode; |
25 | |
import org.objectweb.asm.tree.InsnList; |
26 | |
import org.objectweb.asm.tree.InsnNode; |
27 | |
import org.objectweb.asm.tree.MethodInsnNode; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
public class DAddTransformer implements InstructionsTransformer { |
33 | |
|
34 | |
|
35 | |
private static final String ADD_METHOD = "add"; |
36 | |
|
37 | |
|
38 | |
private final boolean stack0Converted; |
39 | |
|
40 | |
|
41 | |
private final boolean stack1Converted; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | 9 | public DAddTransformer(final boolean stack0Converted, final boolean stack1Converted) { |
48 | 9 | this.stack0Converted = stack0Converted; |
49 | 9 | this.stack1Converted = stack1Converted; |
50 | 9 | } |
51 | |
|
52 | |
|
53 | |
public InsnList getReplacement(final AbstractInsnNode insn, |
54 | |
final MethodDifferentiator methodDifferentiator) |
55 | |
throws DifferentiationException { |
56 | |
|
57 | 9 | final InsnList list = new InsnList(); |
58 | |
|
59 | 9 | if (stack1Converted) { |
60 | 8 | if (stack0Converted) { |
61 | 2 | list.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, DS_TYPE.getInternalName(), |
62 | |
ADD_METHOD, |
63 | |
Type.getMethodDescriptor(DS_TYPE, DS_TYPE))); |
64 | |
} else { |
65 | 6 | list.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, DS_TYPE.getInternalName(), |
66 | |
ADD_METHOD, |
67 | |
Type.getMethodDescriptor(DS_TYPE, Type.DOUBLE_TYPE))); |
68 | |
} |
69 | |
} else { |
70 | 1 | list.add(new InsnNode(Opcodes.DUP_X2)); |
71 | 1 | list.add(new InsnNode(Opcodes.POP)); |
72 | 1 | list.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, DS_TYPE.getInternalName(), |
73 | |
ADD_METHOD, |
74 | |
Type.getMethodDescriptor(DS_TYPE, Type.DOUBLE_TYPE))); |
75 | |
} |
76 | |
|
77 | 9 | return list; |
78 | |
|
79 | |
} |
80 | |
|
81 | |
} |