/*****************************************************************************
				Tejas Simulator
------------------------------------------------------------------------------------------------------------

   Copyright [2010] [Indian Institute of Technology, Delhi]
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
------------------------------------------------------------------------------------------------------------

	Contributors:  Prathmesh Kallurkar
*****************************************************************************/

package emulatorinterface.translator.x86.instruction;

import emulatorinterface.translator.InvalidInstructionException;
import generic.InstructionArrayList;
import generic.Instruction;
import generic.Operand;

public class Exchange implements InstructionHandler 
{
	public void handle(long instructionPointer, 
			Operand operand1, Operand operand2, Operand operand3,
			InstructionArrayList instructionArrayList)
					throws InvalidInstructionException
	{
		//operand1 is a register and operand2 is also a register
		if((operand1.isIntegerRegisterOperand() || operand1.isMachineSpecificRegisterOperand()) &&
		   (operand2.isIntegerRegisterOperand() || operand2.isMachineSpecificRegisterOperand())&&
		   operand3==null)
		{
			instructionArrayList.appendInstruction(Instruction.getExchangeInstruction(operand1, operand2));
		}

		//operand1 is memory operand and operand2 is a register
		else if((operand1.isMemoryOperand()) &&
				(operand2.isIntegerRegisterOperand() || operand2.isMachineSpecificRegisterOperand()) &&
				 operand3==null)
		{
			instructionArrayList.appendInstruction(Instruction.getExchangeInstruction(operand1, operand2));
		}

		//operand1 is a register and operand2 is also a memory operand
		else if((operand1.isIntegerRegisterOperand() || operand1.isMachineSpecificRegisterOperand()) &&
		        (operand2.isMemoryOperand()) &&
		         operand3==null)
		{
			instructionArrayList.appendInstruction(Instruction.getExchangeInstruction( operand2, operand1));
		}

		else
		{
			misc.Error.invalidOperation("Exchange", operand1, operand2, operand3);
		}
	}
}
@@ -1155,24 +1155,85 @@
 nException;%0A
+import emulatorinterface.translator.x86.registers.Registers;%0A
 import gener
@@ -2029,298 +2029,485 @@
 nd()
-) &&%0A%09%09%09%09(operand2.isIntegerRegisterOperand() %7C%7C operand2.isMachineSpecificRegisterOperand()) &&%0A%09%09%09%09 operand3==null)%0A%09%09%7B%0A%09%09%09instructionArrayList.appendInstruction(Instruction.getExchangeInstruction(operand1, operand2));%0A%09%09%7D%0A%0A%09%09//operand1 is a register and operand2 is also a memory
+ %7C%7C operand1.isIntegerRegisterOperand() %7C%7C operand1.isMachineSpecificRegisterOperand()) &&%0A%09%09%09%09(operand2.isMemoryOperand() %7C%7C operand2.isIntegerRegisterOperand() %7C%7C operand2.isMachineSpecificRegisterOperand()) &&%0A%09%09%09%09 operand3==null)%0A%09%09%7B%0A%09%09%09Operand memLocation = null, tempRegister = null, register = null;%0A%09%09%09%0A%09%09%09tempRegister = Registers.getTempIntReg();%0A%09%09%09%0A%09%09%09if(operand1.isMemoryOperand() && !operand2.isMemoryOperand()) %7B%0A%09%09%09%09memLocation = operand1;%0A%09%09%09%09register =
  operand
 %0A%09%09e
@@ -2502,27 +2502,32 @@
  operand
+2;
 %0A%09%09
+%09%7D 
 else if(
 (operand
@@ -2522,35 +2522,25 @@
  if(
-(
 operand
-1.isIntegerRegister
+2.isMemory
 Oper
@@ -2537,35 +2537,36 @@
 MemoryOperand() 
-%7C%7C 
+&& !
 operand1.isMachi
@@ -2565,213 +2565,519 @@
 .isM
-achineSpecificR
+emoryOperand()) %7B%0A%09%09%09%09memLocation = operand2;%0A%09%09%09%09r
 egister
-O
+ = o
 perand
-()) &&%0A%09%09        (operand2.isMemoryOperand()) &&%0A%09%09         operand3==null)%0A%09%09%7B%0A%09%09%09instructionArrayList.appendInstruction(Instruction.getExchangeInstruction( operand2, operand1
+1;%0A%09%09%09%7D else %7B%0A%09%09%09%09misc.Error.invalidOperation(%22Exchange%22, operand1, operand2, operand3);%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09//temp = mem%0A%09%09%09instructionArrayList.appendInstruction(Instruction.getLoadInstruction(memLocation, tempRegister));%0A%09%09%09//mem = reg%0A%09%09%09instructionArrayList.appendInstruction(Instruction.getStoreInstruction(memLocation, register));%0A%09%09%09// reg = temp%0A%09%09%09instructionArrayList.appendInstruction(Instruction.getMoveInstruction(register, tempRegister
 ));%0A

support for exchange ( memory, register) added