KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > asm > test > cases > Wide


1 /***
2  * ASM tests
3  * Copyright (c) 2002-2005 France Telecom
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package org.objectweb.asm.test.cases;
31
32 import java.io.IOException JavaDoc;
33
34 import org.objectweb.asm.ClassWriter;
35 import org.objectweb.asm.Label;
36 import org.objectweb.asm.MethodVisitor;
37
38 /**
39  * Generates a class which uses a lot of locals and constant pool values. Covers
40  * the 'wide' form of instructions that have one (xLOAD and xSTORE, LDC, IINC,
41  * GOTO and IF instructions - these jump instructions are not directly generated
42  * in their 'wide' form, but are transformed to this form by the method resizing
43  * test). Also covers the V1_2 class version.
44  *
45  * @author Eric Bruneton
46  */

47 public class Wide extends Generator {
48
49     public void generate(final String JavaDoc dir) throws IOException JavaDoc {
50         generate(dir, "pkg/Wide.class", dump());
51     }
52
53     public byte[] dump() {
54         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
55         MethodVisitor mv;
56
57         cw.visit(V1_2, ACC_PUBLIC, "pkg/Wide", null, "java/lang/Object", null);
58
59         mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
60         mv.visitCode();
61         mv.visitVarInsn(ALOAD, 0);
62         mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
63         for (int i = 0; i < 256; ++i) {
64             mv.visitLdcInsn(Integer.toString(i)); // wide form
65
mv.visitInsn(POP);
66         }
67         mv.visitInsn(RETURN);
68         mv.visitMaxs(0, 0);
69         mv.visitEnd();
70
71         mv = cw.visitMethod(ACC_PUBLIC, "wideLocals", "(I)I", null, null);
72         mv.visitCode();
73         Label l0 = new Label();
74         Label l1 = new Label();
75         mv.visitJumpInsn(GOTO, l1); // will give GOTO_W
76

77         mv.visitLabel(l0);
78         mv.visitIincInsn(300, 1); // covers 'update maxlocals' in MethodWriter
79
mv.visitVarInsn(ILOAD, 300); // will give wide form
80
mv.visitJumpInsn(IFEQ, l1); // will give long forward jump
81

82         // many NOPs will be introduced here by the method resizing test
83

84         mv.visitVarInsn(ILOAD, 300); // will give wide form
85
mv.visitInsn(IRETURN);
86
87         mv.visitLabel(l1);
88         for (int i = 1; i < 300; ++i) {
89             mv.visitVarInsn(ILOAD, i);
90             if (i <= 5) {
91                 mv.visitInsn(ICONST_0 + i);
92             } else if (i <= Byte.MAX_VALUE) {
93                 mv.visitIntInsn(BIPUSH, i);
94             } else {
95                 mv.visitIntInsn(SIPUSH, i);
96             }
97             mv.visitInsn(IADD);
98             mv.visitVarInsn(ISTORE, i + 1);
99         }
100         mv.visitInsn(ICONST_0);
101         mv.visitJumpInsn(IFEQ, l0); // will give long backward jump
102
mv.visitJumpInsn(GOTO, l0); // will give long backward goto
103

104         mv.visitMaxs(0, 0);
105         mv.visitEnd();
106
107         cw.visitEnd();
108
109         return cw.toByteArray();
110     }
111 }
112
Popular Tags