Mnemonics now exist only as part of a MnemonicFamily
authorFabian Stemmler <stemmler@in.tum.de>
Wed, 28 Aug 2019 12:35:26 +0000 (14:35 +0200)
committerFabian Stemmler <stemmler@in.tum.de>
Wed, 28 Aug 2019 12:35:26 +0000 (14:35 +0200)
Mnemonics and MnemonicFamilies are now unique.
equals() in Mnemonics and MnemonicFamilies now only tests for equal
references.
Mnemonics have an ordinal to identify them within their family.

net.mograsim.machine/src/net/mograsim/machine/mi/parameters/Mnemonic.java
net.mograsim.machine/src/net/mograsim/machine/mi/parameters/MnemonicFamily.java

index cca106e..4633847 100644 (file)
@@ -6,12 +6,16 @@ public final class Mnemonic implements MicroInstructionParameter
 {
        private final String text;
        private final BitVector vector;
+       final MnemonicFamily owner;
+       private final int ordinal;
        
-       public Mnemonic(String text, BitVector vector)
+       Mnemonic(String text, BitVector vector, MnemonicFamily owner, int ordinal)
        {
                super();
                this.text = text;
                this.vector = vector;
+               this.owner = owner;
+               this.ordinal = ordinal;
        }
 
        public String getText()
@@ -38,26 +42,7 @@ public final class Mnemonic implements MicroInstructionParameter
        @Override
        public boolean equals(Object obj)
        {
-               if (this == obj)
-                       return true;
-               if (obj == null)
-                       return false;
-               if (!(obj instanceof Mnemonic))
-                       return false;
-               Mnemonic other = (Mnemonic) obj;
-               if (text == null)
-               {
-                       if (other.text != null)
-                               return false;
-               } else if (!text.equals(other.text))
-                       return false;
-               if (vector == null)
-               {
-                       if (other.vector != null)
-                               return false;
-               } else if (!vector.equals(other.vector))
-                       return false;
-               return true;
+               return this == obj;
        }
 
        @Override
@@ -66,6 +51,11 @@ public final class Mnemonic implements MicroInstructionParameter
                return ParameterType.MNEMONIC;
        }
        
+       public int ordinal()
+       {
+               return ordinal;
+       }
+       
        @Override
        public String toString()
        {
index 3deccb0..b9c1373 100644 (file)
@@ -4,36 +4,55 @@ import java.util.Arrays;
 import java.util.Map;
 import java.util.stream.Collectors;
 
+import net.mograsim.logic.core.types.BitVector;
 import net.mograsim.machine.mi.parameters.MicroInstructionParameter.ParameterType;
 
 public class MnemonicFamily implements ParameterClassification
 {
        private final Mnemonic[] values;
+       private final String[] stringValues;
        private final Map<String, Mnemonic> byText;
        private final int vectorLength;
        
-       public MnemonicFamily(Mnemonic... values)
+       public MnemonicFamily(MnemonicPair... values)
        {
-               this.values = values;
+               this.values = new Mnemonic[values.length];
+               this.stringValues = new String[values.length];
+               
+               for(int i = 0; i < values.length; i++)
+               {
+                       this.values[i] = createMnemonic(values[i], i);
+                       this.stringValues[i] = values[i].name;
+               }
                if(values.length == 0)
                        vectorLength = 0;
                else
                {
-                       vectorLength = values[0].getValue().length();
+                       vectorLength = values[0].value.length();
                        for(int i = 1; i < values.length; i++)
-                               if(values[i].getValue().length() != vectorLength)
+                               if(values[i].value.length() != vectorLength)
                                        throw new IllegalArgumentException("MnemonicFamily is not of uniform vector length!");
                }
-               byText = Arrays.stream(values).collect(Collectors.toMap(m -> m.getText(), m -> m));
+               byText = Arrays.stream(this.values).collect(Collectors.toMap(m -> m.getText(), m -> m));
                if(values.length != byText.keySet().size())
                        throw new IllegalArgumentException("MnemonicFamily contains multiple Mnemonics with the same name!");
        }
        
-       public Mnemonic[] getValues()
+       private Mnemonic createMnemonic(MnemonicPair mnemonicPair, int ordinal)
+       {
+               return new Mnemonic(mnemonicPair.name, mnemonicPair.value, this, ordinal);
+       }
+
+       public Mnemonic[] values()
        {
                return values.clone();
        }
        
+       public Mnemonic get(int ordinal)
+       {
+               return values[ordinal];
+       }
+       
        public Mnemonic get(String text)
        {
                return byText.get(text);
@@ -42,7 +61,7 @@ public class MnemonicFamily implements ParameterClassification
        public boolean contains(Mnemonic m)
        {
                if(m != null)
-                       return m.equals(byText.get(m.getText()));
+                       return m.owner == this;
                else
                        return false;
        }
@@ -82,6 +101,37 @@ public class MnemonicFamily implements ParameterClassification
 
        public String[] getStringValues()
        {
-               return byText.keySet().toArray(new String[values.length]);
+               return stringValues.clone();
+       }
+       
+       
+       
+       @Override
+       public int hashCode()
+       {
+               final int prime = 31;
+               int result = 1;
+               result = prime * result + Arrays.hashCode(values);
+               return result;
+       }
+
+       @Override
+       public boolean equals(Object obj)
+       {
+               return this == obj;
+       }
+
+
+
+       public static class MnemonicPair
+       {
+               public final String name;
+               public final BitVector value;
+               
+               public MnemonicPair(String name, BitVector value)
+               {
+                       this.name = name;
+                       this.value = value;
+               }
        }
 }