/*
* Copyright (c) 2006 Thomas Weise
*
* E-Mail : tweise@gmx.de
* Creation Date : 2006-07-03 07:02:36
* Original Filename: org.dgpf.gp.vm.instructions.arith.ExchangeHandler.java
* Version : 1.0.0
* Last modification: 2006-07-03
* by: Thomas Weise
*
* License : GNU LESSER GENERAL PUBLIC LICENSE
* Version 2.1, February 1999
* You should have received a copy of this license along
* with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307, USA or download the license under
* http://www.gnu.org/copyleft/lesser.html.
*
* Warranty : This software is provided "as is" without any
* warranty; without even the implied warranty of
* merchantability or fitness for a particular purpose.
* See the Gnu Lesser General Public License for more
* details.
*/
package org.dgpf.gp.vm.instructions.arith;
import org.dgpf.gp.vm.base.IInstructionHandler;
import org.dgpf.gp.vm.base.Instruction;
import org.dgpf.gp.vm.base.VMContext;
import org.dgpf.gp.vm.instructions.base.MemInstr2Handler;
import org.dgpf.gp.vm.mutation.MutationInfo;
/**
* The instruction handler for the exchange instruction.
*
* @author Thomas Weise
*/
public final class ExchangeHandler extends MemInstr2Handler
{
/**
* The serial version uid.
*/
private static final long serialVersionUID = 1;
/**
* The globally shared instance of the exchange handler.
*/
public static final IInstructionHandler INSTANCE = new ExchangeHandler();
/**
* Create a new exchange handler - you cannot do this, use the shared instance.
* @see #INSTANCE
*/
private ExchangeHandler()
{
super(null);
}
/**
* Create a new randomized instance of this instruction.
* @param p_context The vm context to be used.
* @param p_info The information record holding the infos on the
* instruction to be created.
* @return The new instruction instance.
*/
public final Instruction create (final VMContext p_context,
final MutationInfo p_info)
{
Addresses l_a;
l_a = new Addresses(2, p_context, p_info);
return new Exchange(l_a.m_addr[0], l_a.m_rel[0],
l_a.m_addr[1], l_a.m_rel[1]);
}
/**
* Try to melt two instructions together. The second instruction is of
* the type this instruction handler handles.
* @param p_first The first instruction.
* @param p_second The second instruction.
* @return An instruction able to replace the sequence of these both
* instructions, or null
if no valid replacement
* coult be found.
*/
@Override
public Instruction try_melt (final Instruction p_first,
final Instruction p_second)
{
Exchange l_e1, l_e2;
if((p_first) instanceof Exchange)
{
l_e1 = ((Exchange)p_first);
l_e2 = ((Exchange)p_second);
if( (l_e1.get_addr_1() == l_e2.get_addr_1()) &&
(l_e1.get_addr_2() == l_e2.get_addr_2()) &&
(!(l_e1.is_relative_1())) && (!(l_e2.is_relative_1())) &&
(!(l_e1.is_relative_2())) && (!(l_e2.is_relative_2()) ))
{
return p_second;
}
}
return super.try_melt(p_first, p_second);
}
/**
* Get an instruction that better represents the passed in instruction.
* @param p_i The instruction to replace.
* @return An instruction that better represents p_i
, or
* p_i
if it is already perfect, or null
* if p_i
itself is totally useless.
*/
@Override
/**
* Get an instruction that better represents the passed in instruction.
* @param p_i The instruction to replace.
* @return An instruction that better represents p_i
, or
* p_i
if it is already perfect, or null
* if p_i
itself is totally useless.
*/
public final Instruction get_replacement (final Instruction p_i)
{
Exchange l_e;
l_e = ((Exchange)p_i);
if( (l_e.get_addr_1() == l_e.get_addr_2()) &&
(l_e.is_relative_1() == l_e.is_relative_2()) ) return null;
return p_i;
}
}