a513b60a530f8f8a6305ed89b17cb9a83d26a9a1
[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         @Override
108         public int hashCode()
109         {
110                 final int prime = 31;
111                 int result = 1;
112                 result = prime * result + Arrays.hashCode(values);
113                 return result;
114         }
115
116         @Override
117         public boolean equals(Object obj)
118         {
119                 return this == obj;
120         }
121         
122         @Override
123         public Mnemonic parse(String toParse)
124         {
125                 Mnemonic parsed = get(toParse);
126                 if(parsed == null)
127                         throw new UnknownMnemonicException(toParse);
128                 return parsed;
129         }
130
131         public static class MnemonicPair
132         {
133                 public final String name;
134                 public final BitVector value;
135                 
136                 public MnemonicPair(String name, BitVector value)
137                 {
138                         this.name = name;
139                         this.value = value;
140                 }
141         }
142 }