001    /*
002     * Copyright 2005 Sun Microsystems, Inc.  All Rights Reserved.
003     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004     *
005     * This code is free software; you can redistribute it and/or modify it
006     * under the terms of the GNU General Public License version 2 only, as
007     * published by the Free Software Foundation.  Sun designates this
008     * particular file as subject to the "Classpath" exception as provided
009     * by Sun in the LICENSE file that accompanied this code.
010     *
011     * This code is distributed in the hope that it will be useful, but WITHOUT
012     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014     * version 2 for more details (a copy is included in the LICENSE file that
015     * accompanied this code).
016     *
017     * You should have received a copy of the GNU General Public License version
018     * 2 along with this work; if not, write to the Free Software Foundation,
019     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020     *
021     * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022     * CA 95054 USA or visit www.sun.com if you need additional information or
023     * have any questions.
024     */
025    
026    
027    package sun.tools.javap;
028    
029    import java.util.*;
030    import java.io.*;
031    
032    import static sun.tools.javap.RuntimeConstants.*;
033    
034    /* represents one entry of StackMapTable attribute
035     */
036    class StackMapTableData {
037        final int frameType;
038        int offsetDelta;
039    
040        StackMapTableData(int frameType) {
041            this.frameType = frameType;
042        }
043    
044        void print(JavapPrinter p) {
045            p.out.print("   frame_type = " + frameType);
046        }
047    
048        static class SameFrame extends StackMapTableData {
049            SameFrame(int frameType, int offsetDelta) {
050                super(frameType);
051                this.offsetDelta = offsetDelta;
052            }
053            void print(JavapPrinter p) {
054                super.print(p);
055                if (frameType < SAME_FRAME_BOUND) {
056                    p.out.println(" /* same */");
057                } else {
058                    p.out.println(" /* same_frame_extended */");
059                    p.out.println("     offset_delta = " + offsetDelta);
060                }
061            }
062        }
063    
064        static class SameLocals1StackItem extends StackMapTableData {
065            final int[] stack;
066            SameLocals1StackItem(int frameType, int offsetDelta, int[] stack) {
067                super(frameType);
068                this.offsetDelta = offsetDelta;
069                this.stack = stack;
070            }
071            void print(JavapPrinter p) {
072                super.print(p);
073                if (frameType == SAME_LOCALS_1_STACK_ITEM_EXTENDED) {
074                    p.out.println(" /* same_locals_1_stack_item_frame_extended */");
075                    p.out.println("     offset_delta = " + offsetDelta);
076                } else {
077                    p.out.println(" /* same_locals_1_stack_item */");
078                }
079                p.printMap("     stack = [", stack);
080            }
081        }
082    
083        static class ChopFrame extends StackMapTableData {
084            ChopFrame(int frameType, int offsetDelta) {
085                super(frameType);
086                this.offsetDelta = offsetDelta;
087            }
088            void print(JavapPrinter p) {
089                super.print(p);
090                p.out.println(" /* chop */");
091                p.out.println("     offset_delta = " + offsetDelta);
092            }
093        }
094    
095        static class AppendFrame extends StackMapTableData {
096            final int[] locals;
097            AppendFrame(int frameType, int offsetDelta, int[] locals) {
098                super(frameType);
099                this.offsetDelta = offsetDelta;
100                this.locals = locals;
101            }
102            void print(JavapPrinter p) {
103                super.print(p);
104                p.out.println(" /* append */");
105                p.out.println("     offset_delta = " + offsetDelta);
106                p.printMap("     locals = [", locals);
107            }
108        }
109    
110        static class FullFrame extends StackMapTableData {
111            final int[] locals;
112            final int[] stack;
113            FullFrame(int offsetDelta, int[] locals, int[] stack) {
114                super(FULL_FRAME);
115                this.offsetDelta = offsetDelta;
116                this.locals = locals;
117                this.stack = stack;
118            }
119            void print(JavapPrinter p) {
120                super.print(p);
121                p.out.println(" /* full_frame */");
122                p.out.println("     offset_delta = " + offsetDelta);
123                p.printMap("     locals = [", locals);
124                p.printMap("     stack = [", stack);
125            }
126        }
127    
128        static StackMapTableData getInstance(DataInputStream in, MethodData method)
129                      throws IOException {
130            int frameType = in.readUnsignedByte();
131    
132            if (frameType < SAME_FRAME_BOUND) {
133                // same_frame
134                return new SameFrame(frameType, frameType);
135            } else if (SAME_FRAME_BOUND <= frameType && frameType < SAME_LOCALS_1_STACK_ITEM_BOUND) {
136                // same_locals_1_stack_item_frame
137                // read additional single stack element
138                return new SameLocals1StackItem(frameType,
139                                                (frameType - SAME_FRAME_BOUND),
140                                                StackMapData.readTypeArray(in, 1, method));
141            } else if (frameType == SAME_LOCALS_1_STACK_ITEM_EXTENDED) {
142                // same_locals_1_stack_item_extended
143                return new SameLocals1StackItem(frameType,
144                                                in.readUnsignedShort(),
145                                                StackMapData.readTypeArray(in, 1, method));
146            } else if (SAME_LOCALS_1_STACK_ITEM_EXTENDED < frameType  && frameType < SAME_FRAME_EXTENDED) {
147                // chop_frame or same_frame_extended
148                return new ChopFrame(frameType, in.readUnsignedShort());
149            } else if (frameType == SAME_FRAME_EXTENDED) {
150                // chop_frame or same_frame_extended
151                return new SameFrame(frameType, in.readUnsignedShort());
152            } else if (SAME_FRAME_EXTENDED < frameType  && frameType < FULL_FRAME) {
153                // append_frame
154                return new AppendFrame(frameType, in.readUnsignedShort(),
155                                       StackMapData.readTypeArray(in, frameType - SAME_FRAME_EXTENDED, method));
156            } else if (frameType == FULL_FRAME) {
157                // full_frame
158                int offsetDelta = in.readUnsignedShort();
159                int locals_size = in.readUnsignedShort();
160                int[] locals = StackMapData.readTypeArray(in, locals_size, method);
161                int stack_size = in.readUnsignedShort();
162                int[] stack = StackMapData.readTypeArray(in, stack_size, method);
163                return new FullFrame(offsetDelta, locals, stack);
164            } else {
165                throw new ClassFormatError("unrecognized frame_type in StackMapTable");
166            }
167        }
168    }