From 4958c3d9a0a02c9104db6b4d0c1d9598a683757c Mon Sep 17 00:00:00 2001 From: Kevin Ramharak Date: Sun, 20 Jan 2019 13:32:48 +0100 Subject: [PATCH] add implementation for instruction aliases --- .../assembly/DefaultInstructionSet.java | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/Server/src/main/java/net/simon987/server/assembly/DefaultInstructionSet.java b/Server/src/main/java/net/simon987/server/assembly/DefaultInstructionSet.java index a87f8ee..1f4ce55 100755 --- a/Server/src/main/java/net/simon987/server/assembly/DefaultInstructionSet.java +++ b/Server/src/main/java/net/simon987/server/assembly/DefaultInstructionSet.java @@ -4,6 +4,7 @@ import net.simon987.server.assembly.instruction.*; import net.simon987.server.logging.LogManager; import java.util.HashMap; +import java.util.Set; /** * Default instruction set for the CPU @@ -15,6 +16,11 @@ public class DefaultInstructionSet implements InstructionSet { */ private HashMap instructionMap = new HashMap<>(32); + /** + * Map of aliasses, stored in mnemonic : Instruction format + */ + private HashMap aliasesMap = new HashMap<>(16); + private Instruction defaultInstruction; /** @@ -32,7 +38,6 @@ public class DefaultInstructionSet implements InstructionSet { add(new AndInstruction()); add(new OrInstruction()); add(new ShlInstruction()); - add(new SalInstruction()); //Alias is added add(new ShrInstruction()); add(new XorInstruction()); add(new TestInstruction()); @@ -46,6 +51,9 @@ public class DefaultInstructionSet implements InstructionSet { add(new SarInstruction()); add(new IncInstruction()); 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; } @Override public void add(Instruction instruction) { - if (instructionMap.containsKey(instruction.getOpCode())) { - LogManager.LOGGER.fine(instruction.getMnemonic() + " instruction is an alias for " + - instructionMap.get(instruction.getOpCode()).getMnemonic()); + Instruction aliasedInstruction = instructionMap.get(instruction.getOpCode()); + if (aliasedInstruction != null) { + aliasesMap.put(instruction.getMnemonic(), aliasedInstruction); + } else { + instructionMap.put(instruction.getOpCode(), instruction); } - instructionMap.put(instruction.getOpCode(), instruction); } }