javap/src/main/java/org/apidesign/javap/StackMapIterator.java
author Lubomir Nerad <lubomir.nerad@oracle.com>
Thu, 29 Nov 2012 20:19:00 +0100
branchregisters
changeset 221 3ee23267706c
child 281 f2352e0b713e
permissions -rw-r--r--
Register based VM
     1 /*
     2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    25 
    26 package org.apidesign.javap;
    27 
    28 public final class StackMapIterator {
    29     private static final StackMapTableData INITIAL_FRAME =
    30             new StackMapTableData(-1) {
    31                 @Override
    32                 int getStackItemsCount() {
    33                     return 0;
    34                 }
    35 
    36                 @Override
    37                 public String toString() {
    38                     return "INITIAL(0)";
    39                 }
    40             };
    41 
    42     private final StackMapTableData[] stackMapTable;
    43 
    44     private int nextFrameIndex;
    45     private int lastFrameByteCodeOffset;
    46 
    47     private int byteCodeOffset;
    48 
    49     StackMapIterator(final StackMapTableData[] stackMapTable) {
    50         this.stackMapTable = (stackMapTable != null)
    51                                  ? stackMapTable
    52                                  : new StackMapTableData[0];
    53         this.lastFrameByteCodeOffset = -1;
    54         advanceBy(0);
    55     }
    56 
    57     public String getFrameAsString() {
    58         return getCurrentFrame().toString();
    59     }
    60 
    61     public int getFrameIndex() {
    62         return nextFrameIndex;
    63     }
    64 
    65     public int getFrameStackItemsCount() {
    66         return getCurrentFrame().getStackItemsCount();
    67     }
    68 
    69     public void advanceBy(final int numByteCodes) {
    70         if (numByteCodes < 0) {
    71             throw new IllegalStateException("Forward only iterator");
    72         }
    73 
    74         byteCodeOffset += numByteCodes;
    75         while ((nextFrameIndex < stackMapTable.length)
    76                     && ((byteCodeOffset - lastFrameByteCodeOffset)
    77                             >= (stackMapTable[nextFrameIndex].offsetDelta
    78                                     + 1))) {
    79             lastFrameByteCodeOffset +=
    80                     stackMapTable[nextFrameIndex].offsetDelta + 1;
    81             ++nextFrameIndex;
    82         }
    83     }
    84 
    85     public void advanceTo(final int nextByteCodeOffset) {
    86         advanceBy(nextByteCodeOffset - byteCodeOffset);
    87     }
    88 
    89     private StackMapTableData getCurrentFrame() {
    90         return (nextFrameIndex == 0)
    91                 ? INITIAL_FRAME
    92                 : stackMapTable[nextFrameIndex - 1];
    93     }
    94 }