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.forward.trimming;
018    
019    import org.objectweb.asm.Opcodes;
020    import org.objectweb.asm.tree.AbstractInsnNode;
021    import org.objectweb.asm.tree.InsnList;
022    import org.objectweb.asm.tree.VarInsnNode;
023    
024    /** Trimmer replacing (DLOAD i, DLOAD j, DUP2_X2, POP2) with (DLOAD j, DLOAD i).
025     * @version $Id$
026     */
027    public class SwappedDloadTrimmer extends BytecodeTrimmer {
028    
029        /** Simple constructor.
030         */
031        public SwappedDloadTrimmer() {
032            super(4);
033        }
034    
035        /** {@inheritDoc} */
036        @Override
037        protected boolean trimWindow(final InsnList instructions,
038                                     final AbstractInsnNode[] window) {
039    
040            if ((window[0].getOpcode() == Opcodes.DLOAD) &&
041                (window[1].getOpcode() == Opcodes.DLOAD) &&
042                (window[2].getOpcode() == Opcodes.DUP2_X2) &&
043                (window[3].getOpcode() == Opcodes.POP2)) {
044    
045                // reverse the DLOAD orders
046                final int tmp = ((VarInsnNode) window[0]).var;
047                ((VarInsnNode) window[0]).var = ((VarInsnNode) window[1]).var;
048                ((VarInsnNode) window[1]).var = tmp;
049    
050                // remove the operand stack swap instructions
051                instructions.remove(window[2]);
052                instructions.remove(window[3]);
053    
054                // slide window two instructions forward
055                window[2] = window[1].getNext();
056                window[3] = (window[2] == null) ? null : window[2].getNext();
057                return true;
058    
059            }
060    
061            // nothing have been done
062            return false;
063    
064        }
065    
066    }