KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > api > persistence > enhancer > classfile > InsnLookupSwitch


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24
25 package com.sun.jdo.api.persistence.enhancer.classfile;
26
27 import java.io.PrintStream JavaDoc;
28
29 /**
30  * Special instruction form for the opc_lookupswitch instruction
31  */

32
33 public class InsnLookupSwitch extends Insn {
34   /* The target for the default case */
35   private InsnTarget defaultOp;
36
37   /* The int constants against which to perform the lookup */
38   private int[] matchesOp;
39
40   /* The branch targets for the cases corresponding to the entries in
41    * the matchesOp array */

42   private InsnTarget[] targetsOp;
43
44   /* public accessors */
45
46   public int nStackArgs() {
47     return 1;
48   }
49
50   public int nStackResults() {
51     return 0;
52   }
53
54   /**
55    * What are the types of the stack operands ?
56    */

57   public String JavaDoc argTypes() {
58       return "I";//NOI18N
59
}
60
61   /**
62    * What are the types of the stack results?
63    */

64   public String JavaDoc resultTypes() {
65       return "";//NOI18N
66
}
67
68   public boolean branches() {
69     return true;
70   }
71
72   /**
73    * Mark possible branch targets
74    */

75   public void markTargets() {
76     defaultOp.setBranchTarget();
77     for (int i=0; i<targetsOp.length; i++)
78       targetsOp[i].setBranchTarget();
79   }
80
81
82   /**
83    * Return the defaultTarget for the switch
84    */

85   public InsnTarget defaultTarget() {
86     return defaultOp;
87   }
88
89   /**
90    * Return the case values of the switch.
91    */

92   public int[] switchCases() {
93     return matchesOp;
94   }
95
96   /**
97    * Return the targets for the cases of the switch.
98    */

99   public InsnTarget[] switchTargets() {
100     return targetsOp;
101   }
102
103   /**
104    * Constructor for opc_lookupswitch
105    */

106   public InsnLookupSwitch (InsnTarget defaultOp, int[] matchesOp,
107                InsnTarget[] targetsOp) {
108     this(defaultOp, matchesOp, targetsOp, NO_OFFSET);
109   }
110
111
112   /* package local methods */
113
114   InsnLookupSwitch (InsnTarget defaultOp, int[] matchesOp,
115             InsnTarget[] targetsOp, int offset) {
116     super(opc_lookupswitch, offset);
117
118     this.defaultOp = defaultOp;
119     this.matchesOp = matchesOp;
120     this.targetsOp = targetsOp;
121
122     if (defaultOp == null || targetsOp == null || matchesOp == null ||
123     targetsOp.length != matchesOp.length)
124         throw new InsnError ("attempt to create an opc_lookupswitch" +//NOI18N
125
" with invalid operands");//NOI18N
126
}
127
128   void print (PrintStream JavaDoc out, int indent) {
129     ClassPrint.spaces(out, indent);
130     out.println(offset() + " opc_lookupswitch ");//NOI18N
131
for (int i=0; i<matchesOp.length; i++) {
132       ClassPrint.spaces(out, indent+2);
133       out.println(matchesOp[i] + " -> " + targetsOp[i].offset());//NOI18N
134
}
135     ClassPrint.spaces(out, indent+2);
136     out.println("default -> " + defaultOp.offset());//NOI18N
137
}
138
139   int store(byte[] buf, int index) {
140     buf[index++] = (byte) opcode();
141     index = (index + 3) & ~3;
142     index = storeInt(buf, index, defaultOp.offset() - offset());
143     index = storeInt(buf, index, targetsOp.length);
144     for (int i=0; i<targetsOp.length; i++) {
145       index = storeInt(buf, index, matchesOp[i]);
146       index = storeInt(buf, index, targetsOp[i].offset() - offset());
147     }
148     return index;
149   }
150
151   int size() {
152     /* account for the instruction, 0-3 bytes of pad, 2 ints */
153     int basic = ((offset() + 4) & ~3) - offset() + 8;
154     /* Add 8*number of offsets */
155     return basic + targetsOp.length*8;
156   }
157
158   static InsnLookupSwitch read (InsnReadEnv insnEnv, int myPC) {
159     /* eat up any padding */
160     int thisPC = myPC +1;
161     for (int pads = ((thisPC + 3) & ~3) - thisPC; pads > 0; pads--)
162       insnEnv.getByte();
163     InsnTarget defaultTarget = insnEnv.getTarget(insnEnv.getInt() + myPC);
164     int npairs = insnEnv.getInt();
165     int matches[] = new int[npairs];
166     InsnTarget[] offsets = new InsnTarget[npairs];
167     for (int i=0; i<npairs; i++) {
168       matches[i] = insnEnv.getInt();
169       offsets[i] = insnEnv.getTarget(insnEnv.getInt() + myPC);
170     }
171     return new InsnLookupSwitch(defaultTarget, matches, offsets, myPC);
172   }
173 }
174
Popular Tags