Refined MicroInstructionDefinition and associated classes
authorFabian Stemmler <stemmler@in.tum.de>
Mon, 26 Aug 2019 17:09:04 +0000 (19:09 +0200)
committerFabian Stemmler <stemmler@in.tum.de>
Mon, 26 Aug 2019 17:09:04 +0000 (19:09 +0200)
net.mograsim.machine/src/net/mograsim/machine/MicroInstruction.java
net.mograsim.machine/src/net/mograsim/machine/MicroInstructionDefinition.java
net.mograsim.machine/src/net/mograsim/machine/mi/parameters/BooleanImmediate.java [new file with mode: 0644]
net.mograsim.machine/src/net/mograsim/machine/mi/parameters/IntegerImmediate.java [new file with mode: 0644]
net.mograsim.machine/src/net/mograsim/machine/mi/parameters/MicroInstructionParameter.java [new file with mode: 0644]
net.mograsim.machine/src/net/mograsim/machine/mi/parameters/Mnemonic.java [new file with mode: 0644]
net.mograsim.machine/src/net/mograsim/machine/mi/parameters/MnemonicFamily.java [new file with mode: 0644]
net.mograsim.machine/src/net/mograsim/machine/mi/parameters/ParameterClassification.java [new file with mode: 0644]
net.mograsim.machine/src/net/mograsim/machine/mi/parameters/SimpleTypeClassification.java [new file with mode: 0644]
net.mograsim.machine/src/net/mograsim/machine/mnemonics/Mnemonic.java [deleted file]
net.mograsim.machine/src/net/mograsim/machine/mnemonics/MnemonicFamily.java [deleted file]

index b054c66..9f16320 100644 (file)
@@ -1,10 +1,11 @@
 package net.mograsim.machine;
 
-import net.mograsim.machine.mnemonics.Mnemonic;
+import net.mograsim.machine.mi.parameters.MicroInstructionParameter;
+import net.mograsim.machine.mi.parameters.Mnemonic;
 
 public interface MicroInstruction {
        
-       public Mnemonic getValue(int index);
+       public MicroInstructionParameter getParameter(int index);
        
        /**
         * @return The amount of {@link Mnemonic}s, the instruction is composed of
index 3676b2b..33a970c 100644 (file)
@@ -1,17 +1,17 @@
 package net.mograsim.machine;
 
-import net.mograsim.machine.mnemonics.Mnemonic;
-import net.mograsim.machine.mnemonics.MnemonicFamily;
+import net.mograsim.machine.mi.parameters.MicroInstructionParameter;
+import net.mograsim.machine.mi.parameters.ParameterClassification;
 
 public interface MicroInstructionDefinition
 {
        /**
-        * @return The {@link MnemonicFamily}s of which a MicroInstruction is composed.
+        * @return The {@link ParameterClassification}s of which a MicroInstruction is composed.
         */
-       public MnemonicFamily[] getMnemonicFamilies();
+       public ParameterClassification[] getParameterClassifications();
        
        /**
-        * @return The amount of {@link Mnemonic}s a {@link MicroInstruction} that follows this definition consists of.
+        * @return The amount of {@link MicroInstructionParameter}s in a {@link MicroInstruction} that follows this definition.
         */
        public int size();
        
diff --git a/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/BooleanImmediate.java b/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/BooleanImmediate.java
new file mode 100644 (file)
index 0000000..7ed60fd
--- /dev/null
@@ -0,0 +1,51 @@
+package net.mograsim.machine.mi.parameters;
+
+import net.mograsim.logic.core.types.BitVector;
+
+public class BooleanImmediate implements MicroInstructionParameter
+{
+       private boolean value;
+       
+       public BooleanImmediate(boolean value)
+       {
+               this.value = value;
+       }
+       
+       @Override
+       public BitVector getValue()
+       {
+               return value ? BitVector.SINGLE_1 : BitVector.SINGLE_0;
+       }
+
+       @Override
+       public ParameterType getType()
+       {
+               return ParameterType.BOOLEAN_IMMEDIATE;
+       }
+       
+       @Override
+       public int hashCode()
+       {
+               final int prime = 31;
+               int result = 1;
+               result = prime * result + (value ? 1231 : 1237);
+               return result;
+       }
+
+       @Override
+       public boolean equals(Object obj)
+       {
+               if (this == obj)
+                       return true;
+               if (obj == null)
+                       return false;
+               if (!(obj instanceof BooleanImmediate))
+                       return false;
+               BooleanImmediate other = (BooleanImmediate) obj;
+               if (value != other.value)
+                       return false;
+               return true;
+       }
+       
+       
+}
diff --git a/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/IntegerImmediate.java b/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/IntegerImmediate.java
new file mode 100644 (file)
index 0000000..e6f5f76
--- /dev/null
@@ -0,0 +1,60 @@
+package net.mograsim.machine.mi.parameters;
+
+import java.math.BigInteger;
+
+import net.mograsim.logic.core.types.BitVector;
+
+public final class IntegerImmediate implements MicroInstructionParameter
+{
+       private BitVector value;
+       
+       public IntegerImmediate(BigInteger value, int bits)
+       {
+               this.value = BitVector.from(value, bits);
+       }
+       
+       public IntegerImmediate(BitVector value)
+       {
+               this.value = value;
+       }
+       
+       @Override
+       public BitVector getValue()
+       {
+               return value;
+       }
+
+       @Override
+       public ParameterType getType()
+       {
+               return ParameterType.INTEGER_IMMEDIATE;
+       }
+
+       @Override
+       public int hashCode()
+       {
+               final int prime = 31;
+               int result = 1;
+               result = prime * result + ((value == null) ? 0 : value.hashCode());
+               return result;
+       }
+
+       @Override
+       public boolean equals(Object obj)
+       {
+               if (this == obj)
+                       return true;
+               if (obj == null)
+                       return false;
+               if (!(obj instanceof IntegerImmediate))
+                       return false;
+               IntegerImmediate other = (IntegerImmediate) obj;
+               if (value == null)
+               {
+                       if (other.value != null)
+                               return false;
+               } else if (!value.equals(other.value))
+                       return false;
+               return true;
+       }
+}
diff --git a/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/MicroInstructionParameter.java b/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/MicroInstructionParameter.java
new file mode 100644 (file)
index 0000000..ecff9f0
--- /dev/null
@@ -0,0 +1,15 @@
+package net.mograsim.machine.mi.parameters;
+
+import net.mograsim.logic.core.types.BitVector;
+
+public interface MicroInstructionParameter
+{
+       public BitVector getValue();
+       
+       public ParameterType getType();
+       
+       public static enum ParameterType
+       {
+               INTEGER_IMMEDIATE, BOOLEAN_IMMEDIATE, MNEMONIC
+       }
+}
diff --git a/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/Mnemonic.java b/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/Mnemonic.java
new file mode 100644 (file)
index 0000000..feeb88e
--- /dev/null
@@ -0,0 +1,68 @@
+package net.mograsim.machine.mi.parameters;
+
+import net.mograsim.logic.core.types.BitVector;
+
+public final class Mnemonic implements MicroInstructionParameter
+{
+       private final String text;
+       private final BitVector vector;
+       
+       public Mnemonic(String text, BitVector vector)
+       {
+               super();
+               this.text = text;
+               this.vector = vector;
+       }
+
+       public String getText()
+       {
+               return text;
+       }
+
+       @Override
+       public BitVector getValue()
+       {
+               return vector;
+       }
+
+       @Override
+       public int hashCode()
+       {
+               final int prime = 31;
+               int result = 1;
+               result = prime * result + ((text == null) ? 0 : text.hashCode());
+               result = prime * result + ((vector == null) ? 0 : vector.hashCode());
+               return result;
+       }
+
+       @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;
+       }
+
+       @Override
+       public ParameterType getType()
+       {
+               return ParameterType.MNEMONIC;
+       }
+}
diff --git a/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/MnemonicFamily.java b/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/MnemonicFamily.java
new file mode 100644 (file)
index 0000000..aa07f80
--- /dev/null
@@ -0,0 +1,69 @@
+package net.mograsim.machine.mi.parameters;
+
+import java.util.Arrays;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import net.mograsim.machine.mi.parameters.MicroInstructionParameter.ParameterType;
+
+public class MnemonicFamily implements ParameterClassification
+{
+       private final Mnemonic[] values;
+       private final Map<String, Mnemonic> byText;
+       private final int vectorLenght;
+       
+       public MnemonicFamily(Mnemonic... values)
+       {
+               this.values = values;
+               if(values.length == 0)
+                       vectorLenght = 0;
+               else
+               {
+                       vectorLenght = values[0].getValue().width();
+                       for(int i = 1; i < values.length; i++)
+                               if(values[i].getValue().width() != vectorLenght)
+                                       throw new IllegalArgumentException("MnemonicFamily is not of uniform vector length!");
+               }
+               byText = Arrays.stream(values).collect(Collectors.toMap(m -> m.getText(), m -> m));
+       }
+       
+       public Mnemonic[] getValues()
+       {
+               return values.clone();
+       }
+       
+       public Mnemonic get(String text)
+       {
+               return byText.get(text);
+       }
+       
+       public boolean contains(Mnemonic m)
+       {
+               if(m != null)
+                       return m.equals(byText.get(m.getText()));
+               else
+                       return false;
+       }
+       
+       public int size()
+       {
+               return values.length;
+       }
+       
+       public int getVectorLength()
+       {
+               return vectorLenght;
+       }
+
+       @Override
+       public boolean conforms(MicroInstructionParameter param)
+       {
+               return param instanceof Mnemonic ? contains((Mnemonic) param) : false;
+       }
+
+       @Override
+       public ParameterType getExpectedType()
+       {
+               return ParameterType.MNEMONIC;
+       }
+}
diff --git a/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/ParameterClassification.java b/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/ParameterClassification.java
new file mode 100644 (file)
index 0000000..b9362a0
--- /dev/null
@@ -0,0 +1,17 @@
+package net.mograsim.machine.mi.parameters;
+
+import net.mograsim.machine.mi.parameters.MicroInstructionParameter.ParameterType;
+
+public interface ParameterClassification
+{
+       /**
+        * Determines whether a {@link MicroInstructionParameter} is part of this class of parameters.
+        * @return true if the classification contains the Parameter, false otherwise
+        */
+       public boolean conforms(MicroInstructionParameter param);
+       
+       /**
+        * @return The type of the parameters in this classification.
+        */
+       public ParameterType getExpectedType();
+}
diff --git a/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/SimpleTypeClassification.java b/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/SimpleTypeClassification.java
new file mode 100644 (file)
index 0000000..c6687ae
--- /dev/null
@@ -0,0 +1,25 @@
+package net.mograsim.machine.mi.parameters;
+
+import net.mograsim.machine.mi.parameters.MicroInstructionParameter.ParameterType;
+
+public class SimpleTypeClassification implements ParameterClassification
+{
+       private ParameterType expectedType;
+       
+       public SimpleTypeClassification(ParameterType expectedType)
+       {
+               this.expectedType = expectedType;
+       }
+
+       @Override
+       public boolean conforms(MicroInstructionParameter param)
+       {
+               return expectedType.equals(param.getType());
+       }
+
+       @Override
+       public ParameterType getExpectedType()
+       {
+               return expectedType;
+       }
+}
diff --git a/net.mograsim.machine/src/net/mograsim/machine/mnemonics/Mnemonic.java b/net.mograsim.machine/src/net/mograsim/machine/mnemonics/Mnemonic.java
deleted file mode 100644 (file)
index 822d7ed..0000000
+++ /dev/null
@@ -1,26 +0,0 @@
-package net.mograsim.machine.mnemonics;
-
-import net.mograsim.logic.core.types.BitVector;
-
-public final class Mnemonic
-{
-       private final String text;
-       private final BitVector vector;
-       
-       public Mnemonic(String text, BitVector vector)
-       {
-               super();
-               this.text = text;
-               this.vector = vector;
-       }
-
-       public String getText()
-       {
-               return text;
-       }
-
-       public BitVector getVector()
-       {
-               return vector;
-       }
-}
diff --git a/net.mograsim.machine/src/net/mograsim/machine/mnemonics/MnemonicFamily.java b/net.mograsim.machine/src/net/mograsim/machine/mnemonics/MnemonicFamily.java
deleted file mode 100644 (file)
index a55175f..0000000
+++ /dev/null
@@ -1,55 +0,0 @@
-package net.mograsim.machine.mnemonics;
-
-import java.util.Arrays;
-import java.util.Map;
-import java.util.stream.Collectors;
-
-public class MnemonicFamily
-{
-       private final Mnemonic[] values;
-       private final Map<String, Mnemonic> byText;
-       private final int vectorLenght;
-       
-       public MnemonicFamily(Mnemonic... values)
-       {
-               this.values = values;
-               if(values.length == 0)
-                       vectorLenght = 0;
-               else
-               {
-                       vectorLenght = values[0].getVector().width();
-                       for(int i = 1; i < values.length; i++)
-                               if(values[i].getVector().width() != vectorLenght)
-                                       throw new IllegalArgumentException("MnemonicFamily is not of uniform vector length!");
-               }
-               byText = Arrays.stream(values).collect(Collectors.toMap(m -> m.getText(), m -> m));
-       }
-       
-       public Mnemonic[] getValues()
-       {
-               return values.clone();
-       }
-       
-       public Mnemonic get(String text)
-       {
-               return byText.get(text);
-       }
-       
-       public boolean contains(Mnemonic m)
-       {
-               if(m != null)
-                       return m.equals(byText.get(m.getText()));
-               else
-                       return false;
-       }
-       
-       public int size()
-       {
-               return values.length;
-       }
-       
-       public int getVectorLength()
-       {
-               return vectorLenght;
-       }
-}