View Javadoc

1   /*
2    * @(#) IINC.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  
15  import nl.tudelft.simulation.dsol.interpreter.LocalVariable;
16  import nl.tudelft.simulation.dsol.interpreter.OperandStack;
17  import nl.tudelft.simulation.dsol.interpreter.classfile.Constant;
18  import nl.tudelft.simulation.language.primitives.Primitive;
19  
20  /***
21   * The IINC operation as defined in <a
22   * href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/Instructions2.doc6.html">
23   * http://java.sun.com/docs/books/vmspec/2nd-edition/html/Instructions2.doc6.html
24   * </a>.
25   * <p>
26   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
27   * University of Technology </a>, the Netherlands. <br>
28   * See for project information <a
29   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
30   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
31   * License (GPL) </a>, no warranty <br>
32   * 
33   * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter
34   *         Jacobs </a><a
35   *         href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">Alexander
36   *         Verbraeck </a>
37   * @version 1.3 Apr 6, 2004
38   * @since 1.4
39   */
40  public class IINC extends VoidOperation
41  {
42  	/*** OP refers to the operand code */
43  	public static final int OP = 132;
44  
45  	/*** the index to the localVariables */
46  	private int index = -1;
47  
48  	/*** the constant */
49  	private int constant = -1;
50  
51  	/*** see the wide statement */
52  	private boolean widened = false;
53  
54  	/***
55  	 * constructs a new IINC
56  	 * 
57  	 * @param dataInput the dataInput to read
58  	 * @throws IOException on IOException
59  	 */
60  	public IINC(final DataInput dataInput) throws IOException
61  	{
62  		this(dataInput, false);
63  	}
64  
65  	/***
66  	 * constructs a new IINC
67  	 * 
68  	 * @param dataInput the dataInput to read
69  	 * @param widened is the operation widened
70  	 * @throws IOException on IOException
71  	 *  
72  	 */
73  	public IINC(final DataInput dataInput, final boolean widened)
74  			throws IOException
75  	{
76  		super();
77  		this.widened = widened;
78  		if (widened)
79  		{
80  			this.index = dataInput.readUnsignedShort();
81  			this.constant = dataInput.readShort();
82  		} else
83  		{
84  			this.index = dataInput.readUnsignedByte();
85  			this.constant = dataInput.readByte();
86  		}
87  	}
88  
89  	/***
90  	 * @see nl.tudelft.simulation.dsol.interpreter.operations.VoidOperation#execute(
91  	 *      nl.tudelft.simulation.dsol.interpreter.OperandStack,
92  	 *      nl.tudelft.simulation.dsol.interpreter.classfile.Constant[],
93  	 *      nl.tudelft.simulation.dsol.interpreter.LocalVariable[])
94  	 */
95  	public void execute(final OperandStack stack,
96  			final Constant[] constantPool, final LocalVariable[] localVariables)
97  	{
98  		int oldValue = Primitive.toInteger(
99  				localVariables[this.index].getValue()).intValue();
100 		localVariables[this.index].setValue(new Integer(oldValue
101 				+ this.constant));
102 	}
103 
104 	/***
105 	 * @see nl.tudelft.simulation.dsol.interpreter.Operation#getByteLength()
106 	 */
107 	public int getByteLength()
108 	{
109 		int result = OPCODE_BYTE_LENGTH + 2;
110 		if (this.widened)
111 		{
112 			result = result + 2;
113 		}
114 		return result;
115 	}
116 
117 	/***
118 	 * @see nl.tudelft.simulation.dsol.interpreter.Operation#getOpcode()
119 	 */
120 	public int getOpcode()
121 	{
122 		return IINC.OP;
123 	}
124 }