1 package net.mograsim.machine.mi.parameters;
3 import java.util.Arrays;
5 import java.util.stream.Collectors;
7 import net.mograsim.logic.core.types.BitVector;
8 import net.mograsim.machine.mi.parameters.MicroInstructionParameter.ParameterType;
10 public class MnemonicFamily implements ParameterClassification
12 private final Mnemonic[] values;
13 private final String[] stringValues;
14 private Map<String, Mnemonic> byText;
15 private int vectorLength;
17 public MnemonicFamily(String... names)
19 this(false, (int) Math.round(Math.ceil(Math.log(names.length) / Math.log(2))), names);
22 public MnemonicFamily(boolean reverse, String... names)
24 this(reverse, (int) Math.round(Math.ceil(Math.log(names.length) / Math.log(2))), names);
27 public MnemonicFamily(int bits, String... names)
29 this(false, bits, names);
32 public MnemonicFamily(boolean reverse, int bits, String... names)
34 this.values = new Mnemonic[names.length];
35 this.stringValues = new String[names.length];
36 BitVector[] values = new BitVector[names.length];
37 for (int i = 0; i < names.length; i++)
39 values[i] = BitVector.from(i, bits);
42 setup(names, values, reverse);
45 public MnemonicFamily(String[] names, long[] values, int bits)
47 this(false, names, values, bits);
50 public MnemonicFamily(boolean reverse, String[] names, long[] values, int bits)
52 if (names.length != values.length)
53 throw new IllegalArgumentException();
54 this.values = new Mnemonic[values.length];
55 this.stringValues = new String[values.length];
56 BitVector[] vectors = new BitVector[values.length];
58 for (int i = 0; i < vectors.length; i++)
60 vectors[i] = BitVector.from(values[i], bits);
63 setup(names, vectors, reverse);
66 public MnemonicFamily(String[] names, BitVector[] values)
68 this(false, names, values);
71 public MnemonicFamily(boolean reverse, String[] names, BitVector[] values)
73 if (names.length != values.length)
74 throw new IllegalArgumentException();
75 this.values = new Mnemonic[values.length];
76 this.stringValues = new String[values.length];
78 setup(names, values, reverse);
81 public MnemonicFamily(MnemonicPair... values)
86 public MnemonicFamily(boolean reverse, MnemonicPair... values)
88 this.values = new Mnemonic[values.length];
89 this.stringValues = new String[values.length];
94 private void setup(String[] names, BitVector[] values, boolean reverse)
96 MnemonicPair[] mnemonics = new MnemonicPair[values.length];
97 for (int i = 0; i < values.length; i++)
98 mnemonics[i] = new MnemonicPair(names[i], reverse ? values[i].reverse() : values[i]);
102 private void setup(MnemonicPair[] values)
104 for (int i = 0; i < values.length; i++)
106 this.values[i] = createMnemonic(values[i], i);
107 this.stringValues[i] = values[i].name;
109 if (values.length == 0)
113 vectorLength = values[0].value.length();
114 for (int i = 1; i < values.length; i++)
115 if (values[i].value.length() != vectorLength)
116 throw new IllegalArgumentException("MnemonicFamily is not of uniform vector length!");
118 byText = Arrays.stream(this.values).collect(Collectors.toMap(m -> m.getText(), m -> m));
119 if (values.length != byText.keySet().size())
120 throw new IllegalArgumentException("MnemonicFamily contains multiple Mnemonics with the same name!");
123 private Mnemonic createMnemonic(MnemonicPair mnemonicPair, int ordinal)
125 return new Mnemonic(mnemonicPair.name, mnemonicPair.value, this, ordinal);
128 public Mnemonic[] values()
130 return values.clone();
133 public Mnemonic get(int ordinal)
135 return values[ordinal];
138 public Mnemonic get(String text)
140 return byText.get(text);
143 public boolean contains(Mnemonic m)
146 return m.owner == this;
151 public boolean contains(String value)
153 return byText.keySet().contains(value);
158 return values.length;
161 public int getVectorLength()
167 public boolean conforms(MicroInstructionParameter param)
169 return ParameterClassification.super.conforms(param) && (param instanceof Mnemonic ? contains((Mnemonic) param) : false);
173 public ParameterType getExpectedType()
175 return ParameterType.MNEMONIC;
179 public int getExpectedBits()
184 public String[] getStringValues()
186 return stringValues.clone();
190 public int hashCode()
192 final int prime = 31;
194 result = prime * result + Arrays.hashCode(values);
199 public boolean equals(Object obj)
205 public Mnemonic parse(String toParse)
207 Mnemonic parsed = get(toParse);
209 throw new UnknownMnemonicException(toParse);
213 public static class MnemonicPair
215 public final String name;
216 public final BitVector value;
218 public MnemonicPair(String name, BitVector value)