KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > coffi > Instruction_Tableswitch


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1997 Clark Verbrugge
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27
28
29
30
31
32 package soot.coffi;
33 import soot.*;
34 import java.io.*;
35 /** Instruction subclasses are used to represent parsed bytecode; each
36  * bytecode operation has a corresponding subclass of Instruction.
37  * <p>
38  * Each subclass is derived from one of
39  * <ul><li>Instruction</li>
40  * <li>Instruction_noargs (an Instruction with no embedded arguments)</li>
41  * <li>Instruction_byte (an Instruction with a single byte data argument)</li>
42  * <li>Instruction_bytevar (a byte argument specifying a local variable)</li>
43  * <li>Instruction_byteindex (a byte argument specifying a constant pool index)</li>
44  * <li>Instruction_int (an Instruction with a single short data argument)</li>
45  * <li>Instruction_intvar (a short argument specifying a local variable)</li>
46  * <li>Instruction_intindex (a short argument specifying a constant pool index)</li>
47  * <li>Instruction_intbranch (a short argument specifying a code offset)</li>
48  * <li>Instruction_longbranch (an int argument specifying a code offset)</li>
49  * </ul>
50  * @author Clark Verbrugge
51  * @see Instruction
52  * @see Instruction_noargs
53  * @see Instruction_byte
54  * @see Instruction_bytevar
55  * @see Instruction_byteindex
56  * @see Instruction_int
57  * @see Instruction_intvar
58  * @see Instruction_intindex
59  * @see Instruction_intbranch
60  * @see Instruction_longbranch
61  * @see Instruction_Unknown
62  */

63 class Instruction_Tableswitch extends Instruction {
64    public Instruction_Tableswitch() {
65       super((byte)ByteCode.TABLESWITCH);
66       name = "tableswitch";
67       branches = true;
68    }
69    public byte pad; // number of bytes used for padding
70
public int default_offset;
71    public int low;
72    public int high;
73    public int jump_offsets[];
74    public Instruction default_inst;
75    public Instruction jump_insts[];
76    public String JavaDoc toString(cp_info constant_pool[]) {
77       String JavaDoc args;
78       int i;
79       args = super.toString(constant_pool) + argsep + "(" +
80          Integer.toString(pad) + ")";
81       args = args + argsep + "label_" + Integer.toString(default_inst.label);
82       args = args + argsep + Integer.toString(low);
83       args = args + argsep + Integer.toString(high) + ": ";
84       for (i=0;i<high-low+1;i++) {
85          args = args + argsep + "label_" + Integer.toString(jump_insts[i].label);
86       }
87       return args;
88    }
89    public int parse(byte bc[],int index) {
90       // first figure out padding to next 4-byte quantity
91
int i,j,baseindex;
92       baseindex = index;
93       i = index % 4;
94       if (i != 0)
95          pad = (byte)(4 - i);
96       else
97          pad = (byte)0;
98       index += pad;
99       default_offset = getInt(bc,index);
100       index += 4;
101       low = getInt(bc,index);
102       index += 4;
103       high = getInt(bc,index);
104       index += 4;
105       i = high-low+1;
106       if (i>0) {
107          jump_offsets = new int[i];
108          j = 0;
109          do {
110             jump_offsets[j] = getInt(bc,index);
111             index += 4;
112             j++;
113          } while(j<i);
114       }
115       return index;
116    }
117    public int nextOffset(int curr) {
118       int i,j,baseindex,siz=0;
119       baseindex = curr;
120       i = (curr+1) % 4;
121       if (i != 0)
122          siz = (4 - i);
123       return (curr + siz + 13 + (high-low+1)*4);
124    }
125    public int compile(byte bc[],int index) {
126       int i;
127       bc[index++] = code;
128       // insert padding so next instruction is on a 4-byte boundary
129
for (i=0;i<pad;i++)
130          bc[index++] = 0;
131       if (default_inst!=null)
132          index = intToBytes(default_inst.label-label,bc,index);
133       else
134          index = intToBytes(default_offset,bc,index);
135       index = intToBytes(low,bc,index);
136       index = intToBytes(high,bc,index);
137       for (i=0;i<=high-low;i++) {
138          if (jump_insts[i]!=null)
139             index = intToBytes((jump_insts[i]).label-label,bc,index);
140          else
141             index = intToBytes(jump_offsets[i],bc,index);
142       }
143       return index;
144    }
145    public void offsetToPointer(ByteCode bc) {
146       int i;
147       default_inst = bc.locateInst(default_offset+label);
148       if (default_inst==null) {
149          G.v().out.println("Warning: can't locate target of instruction");
150          G.v().out.println(" which should be at byte address " + (label+default_offset));
151       } else
152          default_inst.labelled = true;
153       if (high-low+1>0) {
154          jump_insts = new Instruction[high-low+1];
155          for (i=0;i<high-low+1;i++) {
156             jump_insts[i] = bc.locateInst(jump_offsets[i]+label);
157             if (jump_insts[i]==null) {
158                G.v().out.println("Warning: can't locate target of instruction");
159                G.v().out.println(" which should be at byte address " +
160                                   (label+jump_offsets[i]));
161             } else
162                jump_insts[i].labelled = true;
163          }
164       }
165    }
166    public Instruction[] branchpoints(Instruction next) {
167       Instruction i[] = new Instruction[high-low+2];
168       int j;
169       i[0] = default_inst;
170       for (j=1;j<high-low+2;j++)
171          i[j] = jump_insts[j-1];
172       return i;
173    }
174 }
175
Popular Tags