View Javadoc

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 (DLOAD i, DLOAD j, DUP2_X2, POP2) with (DLOAD j, DLOAD i).
25   * @version $Id$
26   */
27  public class SwappedDloadTrimmer extends BytecodeTrimmer {
28  
29      /** Simple constructor.
30       */
31      public SwappedDloadTrimmer() {
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.DLOAD) &&
41              (window[1].getOpcode() == Opcodes.DLOAD) &&
42              (window[2].getOpcode() == Opcodes.DUP2_X2) &&
43              (window[3].getOpcode() == Opcodes.POP2)) {
44  
45              // reverse the DLOAD orders
46              final int tmp = ((VarInsnNode) window[0]).var;
47              ((VarInsnNode) window[0]).var = ((VarInsnNode) window[1]).var;
48              ((VarInsnNode) window[1]).var = tmp;
49  
50              // remove the operand stack swap instructions
51              instructions.remove(window[2]);
52              instructions.remove(window[3]);
53  
54              // slide window two instructions forward
55              window[2] = window[1].getNext();
56              window[3] = (window[2] == null) ? null : window[2].getNext();
57              return true;
58  
59          }
60  
61          // nothing have been done
62          return false;
63  
64      }
65  
66  }