KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > coffi > Instruction_Lookupswitch


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