KickJava   Java API By Example, From Geeks To Geeks.

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


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
28 import java.io.PrintStream JavaDoc;
29
30 /**
31  * Special instruction form for the opc_tableswitch instruction
32  */

33
34 public class InsnTableSwitch extends Insn {
35   /* The lowest value in the jump table */
36   private int lowOp;
37
38   /* The default target for the switch */
39   private InsnTarget defaultOp;
40
41   /* The targets for the switch - a switch value of lowOp dispatches
42    * to targetsOp[0], lowOp+1 dispatches to targetsOp[1], etc. */

43   private InsnTarget[] targetsOp;
44
45   /* public accessors */
46
47   public int nStackArgs() {
48     return 1;
49   }
50
51   public int nStackResults() {
52     return 0;
53   }
54
55   public String JavaDoc argTypes() {
56       return "I";//NOI18N
57
}
58
59   public String JavaDoc resultTypes() {
60       return "";//NOI18N
61
}
62
63   public boolean branches() {
64     return true;
65   }
66
67   /**
68    * Mark possible branch targets
69    */

70   public void markTargets() {
71     defaultOp.setBranchTarget();
72     for (int i=0; i<targetsOp.length; i++)
73       targetsOp[i].setBranchTarget();
74   }
75
76
77   /**
78    * Return the lowest case for the switch
79    */

80   public int lowCase() {
81     return lowOp;
82   }
83
84   /**
85    * Return the defaultTarget for the switch
86    */

87   public InsnTarget defaultTarget() {
88     return defaultOp;
89   }
90
91   /**
92    * Return the targets for the cases of the switch.
93    */

94   public InsnTarget[] switchTargets() {
95     return targetsOp;
96   }
97
98   /**
99    * Constructor for opc_tableswitch
100    */

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