Source Code Cross Referenced for Label.java in  » Development » Retrotranslator » net » sf » retrotranslator » runtime » asm » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Development » Retrotranslator » net.sf.retrotranslator.runtime.asm 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /***
002:         * ASM: a very small and fast Java bytecode manipulation framework
003:         * Copyright (c) 2000-2005 INRIA, France Telecom
004:         * All rights reserved.
005:         *
006:         * Redistribution and use in source and binary forms, with or without
007:         * modification, are permitted provided that the following conditions
008:         * are met:
009:         * 1. Redistributions of source code must retain the above copyright
010:         *    notice, this list of conditions and the following disclaimer.
011:         * 2. Redistributions in binary form must reproduce the above copyright
012:         *    notice, this list of conditions and the following disclaimer in the
013:         *    documentation and/or other materials provided with the distribution.
014:         * 3. Neither the name of the copyright holders nor the names of its
015:         *    contributors may be used to endorse or promote products derived from
016:         *    this software without specific prior written permission.
017:         *
018:         * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019:         * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020:         * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021:         * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022:         * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023:         * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024:         * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025:         * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026:         * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027:         * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
028:         * THE POSSIBILITY OF SUCH DAMAGE.
029:         */package net.sf.retrotranslator.runtime.asm;
030:
031:        /**
032:         * A label represents a position in the bytecode of a method. Labels are used
033:         * for jump, goto, and switch instructions, and for try catch blocks.
034:         * 
035:         * @author Eric Bruneton
036:         */
037:        public class Label {
038:
039:            /**
040:             * The line number corresponding to this label, if known.
041:             */
042:            int line;
043:
044:            /**
045:             * Indicates if the position of this label is known.
046:             */
047:            boolean resolved;
048:
049:            /**
050:             * The position of this label in the code, if known.
051:             */
052:            int position;
053:
054:            /**
055:             * If the label position has been updated, after instruction resizing.
056:             */
057:            boolean resized;
058:
059:            /**
060:             * Number of forward references to this label, times two.
061:             */
062:            private int referenceCount;
063:
064:            /**
065:             * Informations about forward references. Each forward reference is
066:             * described by two consecutive integers in this array: the first one is the
067:             * position of the first byte of the bytecode instruction that contains the
068:             * forward reference, while the second is the position of the first byte of
069:             * the forward reference itself. In fact the sign of the first integer
070:             * indicates if this reference uses 2 or 4 bytes, and its absolute value
071:             * gives the position of the bytecode instruction.
072:             */
073:            private int[] srcAndRefPositions;
074:
075:            /*
076:             * Fields for the control flow graph analysis algorithm (used to compute the
077:             * maximum stack size). A control flow graph contains one node per "basic
078:             * block", and one edge per "jump" from one basic block to another. Each
079:             * node (i.e., each basic block) is represented by the Label object that
080:             * corresponds to the first instruction of this basic block. Each node also
081:             * stores the list of it successors in the graph, as a linked list of Edge
082:             * objects.
083:             */
084:
085:            /**
086:             * The stack size at the beginning of this basic block. This size is
087:             * initially unknown. It is computed by the control flow analysis algorithm
088:             * (see {@link MethodWriter#visitMaxs visitMaxs}).
089:             */
090:            int beginStackSize;
091:
092:            /**
093:             * The (relative) maximum stack size corresponding to this basic block. This
094:             * size is relative to the stack size at the beginning of the basic block,
095:             * i.e., the true maximum stack size is equal to {@link #beginStackSize
096:             * beginStackSize} + {@link #maxStackSize maxStackSize}.
097:             */
098:            int maxStackSize;
099:
100:            /**
101:             * The successors of this node in the control flow graph. These successors
102:             * are stored in a linked list of {@link Edge Edge} objects, linked to each
103:             * other by their {@link Edge#next} field.
104:             */
105:            Edge successors;
106:
107:            /**
108:             * The next basic block in the basic block stack. See
109:             * {@link MethodWriter#visitMaxs visitMaxs}.
110:             */
111:            Label next;
112:
113:            /**
114:             * <tt>true</tt> if this basic block has been pushed in the basic block
115:             * stack. See {@link MethodWriter#visitMaxs visitMaxs}.
116:             */
117:            boolean pushed;
118:
119:            // ------------------------------------------------------------------------
120:            // Constructor
121:            // ------------------------------------------------------------------------
122:
123:            /**
124:             * Constructs a new label.
125:             */
126:            public Label() {
127:            }
128:
129:            // ------------------------------------------------------------------------
130:            // Methods to compute offsets and to manage forward references
131:            // ------------------------------------------------------------------------
132:
133:            /**
134:             * Returns the offset corresponding to this label. This offset is computed
135:             * from the start of the method's bytecode. <i>This method is intended for
136:             * {@link Attribute} sub classes, and is normally not needed by class
137:             * generators or adapters.</i>
138:             * 
139:             * @return the offset corresponding to this label.
140:             * @throws IllegalStateException if this label is not resolved yet.
141:             */
142:            public int getOffset() {
143:                if (!resolved) {
144:                    throw new IllegalStateException(
145:                            "Label offset position has not been resolved yet");
146:                }
147:                return position;
148:            }
149:
150:            /**
151:             * Puts a reference to this label in the bytecode of a method. If the
152:             * position of the label is known, the offset is computed and written
153:             * directly. Otherwise, a null offset is written and a new forward reference
154:             * is declared for this label.
155:             * 
156:             * @param owner the code writer that calls this method.
157:             * @param out the bytecode of the method.
158:             * @param source the position of first byte of the bytecode instruction that
159:             *        contains this label.
160:             * @param wideOffset <tt>true</tt> if the reference must be stored in 4
161:             *        bytes, or <tt>false</tt> if it must be stored with 2 bytes.
162:             * @throws IllegalArgumentException if this label has not been created by
163:             *         the given code writer.
164:             */
165:            void put(final MethodWriter owner, final ByteVector out,
166:                    final int source, final boolean wideOffset) {
167:                if (resolved) {
168:                    if (wideOffset) {
169:                        out.putInt(position - source);
170:                    } else {
171:                        out.putShort(position - source);
172:                    }
173:                } else {
174:                    if (wideOffset) {
175:                        addReference(-1 - source, out.length);
176:                        out.putInt(-1);
177:                    } else {
178:                        addReference(source, out.length);
179:                        out.putShort(-1);
180:                    }
181:                }
182:            }
183:
184:            /**
185:             * Adds a forward reference to this label. This method must be called only
186:             * for a true forward reference, i.e. only if this label is not resolved
187:             * yet. For backward references, the offset of the reference can be, and
188:             * must be, computed and stored directly.
189:             * 
190:             * @param sourcePosition the position of the referencing instruction. This
191:             *        position will be used to compute the offset of this forward
192:             *        reference.
193:             * @param referencePosition the position where the offset for this forward
194:             *        reference must be stored.
195:             */
196:            private void addReference(final int sourcePosition,
197:                    final int referencePosition) {
198:                if (srcAndRefPositions == null) {
199:                    srcAndRefPositions = new int[6];
200:                }
201:                if (referenceCount >= srcAndRefPositions.length) {
202:                    int[] a = new int[srcAndRefPositions.length + 6];
203:                    System.arraycopy(srcAndRefPositions, 0, a, 0,
204:                            srcAndRefPositions.length);
205:                    srcAndRefPositions = a;
206:                }
207:                srcAndRefPositions[referenceCount++] = sourcePosition;
208:                srcAndRefPositions[referenceCount++] = referencePosition;
209:            }
210:
211:            /**
212:             * Resolves all forward references to this label. This method must be called
213:             * when this label is added to the bytecode of the method, i.e. when its
214:             * position becomes known. This method fills in the blanks that where left
215:             * in the bytecode by each forward reference previously added to this label.
216:             * 
217:             * @param owner the code writer that calls this method.
218:             * @param position the position of this label in the bytecode.
219:             * @param data the bytecode of the method.
220:             * @return <tt>true</tt> if a blank that was left for this label was to
221:             *         small to store the offset. In such a case the corresponding jump
222:             *         instruction is replaced with a pseudo instruction (using unused
223:             *         opcodes) using an unsigned two bytes offset. These pseudo
224:             *         instructions will need to be replaced with true instructions with
225:             *         wider offsets (4 bytes instead of 2). This is done in
226:             *         {@link MethodWriter#resizeInstructions}.
227:             * @throws IllegalArgumentException if this label has already been resolved,
228:             *         or if it has not been created by the given code writer.
229:             */
230:            boolean resolve(final MethodWriter owner, final int position,
231:                    final byte[] data) {
232:                boolean needUpdate = false;
233:                this .resolved = true;
234:                this .position = position;
235:                int i = 0;
236:                while (i < referenceCount) {
237:                    int source = srcAndRefPositions[i++];
238:                    int reference = srcAndRefPositions[i++];
239:                    int offset;
240:                    if (source >= 0) {
241:                        offset = position - source;
242:                        if (offset < Short.MIN_VALUE
243:                                || offset > Short.MAX_VALUE) {
244:                            /*
245:                             * changes the opcode of the jump instruction, in order to
246:                             * be able to find it later (see resizeInstructions in
247:                             * MethodWriter). These temporary opcodes are similar to
248:                             * jump instruction opcodes, except that the 2 bytes offset
249:                             * is unsigned (and can therefore represent values from 0 to
250:                             * 65535, which is sufficient since the size of a method is
251:                             * limited to 65535 bytes).
252:                             */
253:                            int opcode = data[reference - 1] & 0xFF;
254:                            if (opcode <= Opcodes.JSR) {
255:                                // changes IFEQ ... JSR to opcodes 202 to 217
256:                                data[reference - 1] = (byte) (opcode + 49);
257:                            } else {
258:                                // changes IFNULL and IFNONNULL to opcodes 218 and 219
259:                                data[reference - 1] = (byte) (opcode + 20);
260:                            }
261:                            needUpdate = true;
262:                        }
263:                        data[reference++] = (byte) (offset >>> 8);
264:                        data[reference] = (byte) offset;
265:                    } else {
266:                        offset = position + source + 1;
267:                        data[reference++] = (byte) (offset >>> 24);
268:                        data[reference++] = (byte) (offset >>> 16);
269:                        data[reference++] = (byte) (offset >>> 8);
270:                        data[reference] = (byte) offset;
271:                    }
272:                }
273:                return needUpdate;
274:            }
275:
276:            // ------------------------------------------------------------------------
277:            // Overriden Object methods
278:            // ------------------------------------------------------------------------
279:
280:            /**
281:             * Returns a string representation of this label.
282:             * 
283:             * @return a string representation of this label.
284:             */
285:            public String toString() {
286:                return "L" + System.identityHashCode(this);
287:            }
288:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.