View Javadoc

1   /*
2    * @(#) TABLESWITCH.java Jan 8, 2004
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  package nl.tudelft.simulation.dsol.interpreter.operations;
11  
12  import java.io.DataInput;
13  import java.io.IOException;
14  import java.util.ArrayList;
15  import java.util.List;
16  
17  import nl.tudelft.simulation.dsol.interpreter.LocalVariable;
18  import nl.tudelft.simulation.dsol.interpreter.OperandStack;
19  import nl.tudelft.simulation.dsol.interpreter.classfile.Constant;
20  
21  /***
22   * The TABLESWITCH operation as defined in <a
23   * href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/Instructions2.doc14.html">
24   * http://java.sun.com/docs/books/vmspec/2nd-edition/html/Instructions2.doc14.html
25   * </a>.
26   * <p>
27   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
28   * University of Technology </a>, the Netherlands. <br>
29   * See for project information <a
30   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
31   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
32   * License (GPL) </a>, no warranty <br>
33   * 
34   * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter
35   *         Jacobs </a><a
36   *         href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">Alexander
37   *         Verbraeck </a>
38   * @version 1.3 Apr 6, 2004
39   * @since 1.4
40   */
41  public class TABLESWITCH extends JumpOperation
42  {
43  	/*** OP refers to the operand code */
44  	public static final int OP = 170;
45  
46  	/*** the byteLength of this statement */
47  	private int byteLength = 0;
48  
49  	/*** the start position of the table */
50  	private int lowValue = -1;
51  
52  	/*** the end position of the table */
53  	private int highValue = -1;
54  
55  	/*** the offset table */
56  	private List offsets = new ArrayList();
57  
58  	/***
59  	 * constructs a new TABLESWITCH
60  	 * 
61  	 * @param dataInput the dataInput
62  	 * @param padding the number of bytes to pad
63  	 * @throws IOException on IOfailure
64  	 */
65  	public TABLESWITCH(final DataInput dataInput, final int padding)
66  			throws IOException
67  	{
68  		super();
69  
70  		//First we pad
71  		if (padding > 0)
72  		{
73  			dataInput.skipBytes(padding);
74  			this.byteLength = this.byteLength + padding;
75  		}
76  
77  		//Now we place the default value at position 0
78  		this.offsets.add(new Integer(dataInput.readInt()));
79  		this.lowValue = dataInput.readInt();
80  		this.highValue = dataInput.readInt();
81  		int entries = this.highValue - this.lowValue + 1;
82  		this.byteLength = this.byteLength + 12;
83  		for (int i = 0; i < entries; i++)
84  		{
85  			Integer offset = new Integer(dataInput.readInt());
86  			this.offsets.add(offset);
87  			this.byteLength = this.byteLength + 4;
88  		}
89  	}
90  
91  	/***
92  	 * @see nl.tudelft.simulation.dsol.interpreter.operations.JumpOperation
93  	 *      #execute(nl.tudelft.simulation.dsol.interpreter.OperandStack,
94  	 *      nl.tudelft.simulation.dsol.interpreter.classfile.Constant[],
95  	 *      nl.tudelft.simulation.dsol.interpreter.LocalVariable[])
96  	 */
97  	public int execute(final OperandStack stack, final Constant[] constantPool,
98  			final LocalVariable[] localVariables)
99  	{
100 		int index = ((Integer) stack.pop()).intValue();
101 		int offset = -1;
102 		if (index < this.lowValue || index > this.highValue)
103 		{
104 			offset = ((Integer) this.offsets.get(0)).intValue();
105 		} else
106 		{
107 			offset = ((Integer) this.offsets.get(index - this.lowValue + 1))
108 					.intValue();
109 		}
110 		return offset;
111 	}
112 
113 	/***
114 	 * @see nl.tudelft.simulation.dsol.interpreter.Operation#getByteLength()
115 	 */
116 	public int getByteLength()
117 	{
118 		return OPCODE_BYTE_LENGTH + this.byteLength;
119 	}
120 
121 	/***
122 	 * @see nl.tudelft.simulation.dsol.interpreter.Operation#getOpcode()
123 	 */
124 	public int getOpcode()
125 	{
126 		return TABLESWITCH.OP;
127 	}
128 }