b9c137381ec05734bafabb1608cb29c6ffeb1695
[Mograsim.git] / net.mograsim.machine / src / net / mograsim / machine / mi / parameters / MnemonicFamily.java
1 package net.mograsim.machine.mi.parameters;
2
3 import java.util.Arrays;
4 import java.util.Map;
5 import java.util.stream.Collectors;
6
7 import net.mograsim.logic.core.types.BitVector;
8 import net.mograsim.machine.mi.parameters.MicroInstructionParameter.ParameterType;
9
10 public class MnemonicFamily implements ParameterClassification
11 {
12         private final Mnemonic[] values;
13         private final String[] stringValues;
14         private final Map<String, Mnemonic> byText;
15         private final int vectorLength;
16         
17         public MnemonicFamily(MnemonicPair... values)
18         {
19                 this.values = new Mnemonic[values.length];
20                 this.stringValues = new String[values.length];
21                 
22                 for(int i = 0; i < values.length; i++)
23                 {
24                         this.values[i] = createMnemonic(values[i], i);
25                         this.stringValues[i] = values[i].name;
26                 }
27                 if(values.length == 0)
28                         vectorLength = 0;
29                 else
30                 {
31                         vectorLength = values[0].value.length();
32                         for(int i = 1; i < values.length; i++)
33                                 if(values[i].value.length() != vectorLength)
34                                         throw new IllegalArgumentException("MnemonicFamily is not of uniform vector length!");
35                 }
36                 byText = Arrays.stream(this.values).collect(Collectors.toMap(m -> m.getText(), m -> m));
37                 if(values.length != byText.keySet().size())
38                         throw new IllegalArgumentException("MnemonicFamily contains multiple Mnemonics with the same name!");
39         }
40         
41         private Mnemonic createMnemonic(MnemonicPair mnemonicPair, int ordinal)
42         {
43                 return new Mnemonic(mnemonicPair.name, mnemonicPair.value, this, ordinal);
44         }
45
46         public Mnemonic[] values()
47         {
48                 return values.clone();
49         }
50         
51         public Mnemonic get(int ordinal)
52         {
53                 return values[ordinal];
54         }
55         
56         public Mnemonic get(String text)
57         {
58                 return byText.get(text);
59         }
60         
61         public boolean contains(Mnemonic m)
62         {
63                 if(m != null)
64                         return m.owner == this;
65                 else
66                         return false;
67         }
68         
69         public boolean contains(String value)
70         {
71                 return byText.keySet().contains(value);
72         }
73         
74         public int size()
75         {
76                 return values.length;
77         }
78         
79         public int getVectorLength()
80         {
81                 return vectorLength;
82         }
83
84         @Override
85         public boolean conforms(MicroInstructionParameter param)
86         {
87                 return ParameterClassification.super.conforms(param) && (param instanceof Mnemonic ? contains((Mnemonic) param) : false);
88         }
89
90         @Override
91         public ParameterType getExpectedType()
92         {
93                 return ParameterType.MNEMONIC;
94         }
95
96         @Override
97         public int getExpectedBits()
98         {
99                 return vectorLength;
100         }
101
102         public String[] getStringValues()
103         {
104                 return stringValues.clone();
105         }
106         
107         
108         
109         @Override
110         public int hashCode()
111         {
112                 final int prime = 31;
113                 int result = 1;
114                 result = prime * result + Arrays.hashCode(values);
115                 return result;
116         }
117
118         @Override
119         public boolean equals(Object obj)
120         {
121                 return this == obj;
122         }
123
124
125
126         public static class MnemonicPair
127         {
128                 public final String name;
129                 public final BitVector value;
130                 
131                 public MnemonicPair(String name, BitVector value)
132                 {
133                         this.name = name;
134                         this.value = value;
135                 }
136         }
137 }