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.forward.trimming;
18
19 import org.objectweb.asm.Opcodes;
20 import org.objectweb.asm.tree.AbstractInsnNode;
21 import org.objectweb.asm.tree.InsnList;
22 import org.objectweb.asm.tree.VarInsnNode;
23
24 /** Trimmer replacing (DUP2_X2, POP2, DSTORE i, DSTORE j) with (DSTORE j, DSTORE i).
25 * @version $Id$
26 */
27 public class SwappedDstoreTrimmer extends BytecodeTrimmer {
28
29 /** Simple constructor.
30 */
31 public SwappedDstoreTrimmer() {
32 super(4);
33 }
34
35 /** {@inheritDoc} */
36 @Override
37 protected boolean trimWindow(final InsnList instructions,
38 final AbstractInsnNode[] window) {
39
40 if ((window[0].getOpcode() == Opcodes.DUP2_X2) &&
41 (window[1].getOpcode() == Opcodes.POP2) &&
42 (window[2].getOpcode() == Opcodes.DSTORE) &&
43 (window[3].getOpcode() == Opcodes.DSTORE)) {
44
45 // reverse the DSTORE orders
46 final int tmp = ((VarInsnNode) window[2]).var;
47 ((VarInsnNode) window[2]).var = ((VarInsnNode) window[3]).var;
48 ((VarInsnNode) window[3]).var = tmp;
49
50 // remove the operand stack swap instructions
51 instructions.remove(window[0]);
52 instructions.remove(window[1]);
53
54 // update lookahead instructions
55 window[0] = window[2];
56 window[1] = window[3];
57 window[2] = window[1].getNext();
58 window[3] = (window[2] == null) ? null : window[2].getNext();
59 return true;
60
61 }
62
63 // nothing have been done
64 return false;
65
66 }
67
68 }