View Javadoc

1   /*
2    * @(#) LOOKUPSWITCH.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.HashMap;
15  import java.util.Map;
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 LOOKUPSWITCH operation as defined in <a
23   * href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/Instructions2.doc8.html">
24   * http://java.sun.com/docs/books/vmspec/2nd-edition/html/Instructions2.doc8.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 LOOKUPSWITCH extends JumpOperation
42  {
43  	/*** OP refers to the operand code */
44  	public static final int OP = 171;
45  
46  	/*** the byteLength of this statement */
47  	private int byteLength = 0;
48  
49  	/*** the offset table */
50  	private Map offsets = new HashMap();
51  
52  	/***
53  	 * constructs a new LOOKUPSWITCH
54  	 * 
55  	 * @param dataInput the dataInput
56  	 * @param padding the amount of bytes to pad
57  	 * @throws IOException on IOfailure
58  	 */
59  	public LOOKUPSWITCH(final DataInput dataInput, final int padding)
60  			throws IOException
61  	{
62  		super();
63  		//If we pad, we pad!
64  		if (padding > 0)
65  		{
66  			dataInput.skipBytes(padding);
67  			this.byteLength = this.byteLength + padding;
68  		}
69  
70  		this.offsets.put("default", new Integer(dataInput.readInt()));
71  		int entries = dataInput.readInt();
72  		this.byteLength = this.byteLength + 8;
73  		for (int i = 0; i < entries; i++)
74  		{
75  			Integer match = new Integer(dataInput.readInt());
76  			Integer offset = new Integer(dataInput.readInt());
77  			this.offsets.put(match, offset);
78  			this.byteLength = this.byteLength + 8;
79  		}
80  	}
81  
82  	/***
83  	 * @see nl.tudelft.simulation.dsol.interpreter.operations.JumpOperation
84  	 *      #execute(nl.tudelft.simulation.dsol.interpreter.OperandStack,
85  	 *      nl.tudelft.simulation.dsol.interpreter.classfile.Constant[],
86  	 *      nl.tudelft.simulation.dsol.interpreter.LocalVariable[])
87  	 */
88  	public int execute(final OperandStack stack, final Constant[] constantPool,
89  			final LocalVariable[] localVariables)
90  	{
91  		Integer key = (Integer) stack.pop();
92  		Integer offset = (Integer) this.offsets.get(key);
93  		if (offset == null)
94  		{
95  			offset = (Integer) this.offsets.get("default");
96  		}
97  		return offset.intValue();
98  	}
99  
100 	/***
101 	 * @see nl.tudelft.simulation.dsol.interpreter.Operation#getByteLength()
102 	 */
103 	public int getByteLength()
104 	{
105 		return OPCODE_BYTE_LENGTH + this.byteLength;
106 	}
107 
108 	/***
109 	 * @see nl.tudelft.simulation.dsol.interpreter.Operation#getOpcode()
110 	 */
111 	public int getOpcode()
112 	{
113 		return LOOKUPSWITCH.OP;
114 	}
115 }