add implementation for instruction aliases

This commit is contained in:
Kevin Ramharak 2019-01-20 13:32:48 +01:00
parent 762bad758b
commit 4958c3d9a0

View File

@ -4,6 +4,7 @@ import net.simon987.server.assembly.instruction.*;
import net.simon987.server.logging.LogManager; import net.simon987.server.logging.LogManager;
import java.util.HashMap; import java.util.HashMap;
import java.util.Set;
/** /**
* Default instruction set for the CPU * Default instruction set for the CPU
@ -15,6 +16,11 @@ public class DefaultInstructionSet implements InstructionSet {
*/ */
private HashMap<Integer, Instruction> instructionMap = new HashMap<>(32); private HashMap<Integer, Instruction> instructionMap = new HashMap<>(32);
/**
* Map of aliasses, stored in mnemonic : Instruction format
*/
private HashMap<String, Instruction> aliasesMap = new HashMap<>(16);
private Instruction defaultInstruction; private Instruction defaultInstruction;
/** /**
@ -32,7 +38,6 @@ public class DefaultInstructionSet implements InstructionSet {
add(new AndInstruction()); add(new AndInstruction());
add(new OrInstruction()); add(new OrInstruction());
add(new ShlInstruction()); add(new ShlInstruction());
add(new SalInstruction()); //Alias is added
add(new ShrInstruction()); add(new ShrInstruction());
add(new XorInstruction()); add(new XorInstruction());
add(new TestInstruction()); add(new TestInstruction());
@ -46,6 +51,9 @@ public class DefaultInstructionSet implements InstructionSet {
add(new SarInstruction()); add(new SarInstruction());
add(new IncInstruction()); add(new IncInstruction());
add(new DecInstruction()); add(new DecInstruction());
// aliases
add(new SalInstruction());
} }
/** /**
@ -86,16 +94,22 @@ public class DefaultInstructionSet implements InstructionSet {
} }
} }
Instruction aliasedInstruction = aliasesMap.get(mnemonic.toLowerCase());
if (aliasedInstruction != null) {
return aliasedInstruction;
}
return null; return null;
} }
@Override @Override
public void add(Instruction instruction) { public void add(Instruction instruction) {
if (instructionMap.containsKey(instruction.getOpCode())) { Instruction aliasedInstruction = instructionMap.get(instruction.getOpCode());
LogManager.LOGGER.fine(instruction.getMnemonic() + " instruction is an alias for " + if (aliasedInstruction != null) {
instructionMap.get(instruction.getOpCode()).getMnemonic()); aliasesMap.put(instruction.getMnemonic(), aliasedInstruction);
} else {
instructionMap.put(instruction.getOpCode(), instruction);
} }
instructionMap.put(instruction.getOpCode(), instruction);
} }
} }