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 final Map<String, Mnemonic> byText;
15 private final int vectorLength;
17 public MnemonicFamily(MnemonicPair... values)
19 this.values = new Mnemonic[values.length];
20 this.stringValues = new String[values.length];
22 for(int i = 0; i < values.length; i++)
24 this.values[i] = createMnemonic(values[i], i);
25 this.stringValues[i] = values[i].name;
27 if(values.length == 0)
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!");
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!");
41 private Mnemonic createMnemonic(MnemonicPair mnemonicPair, int ordinal)
43 return new Mnemonic(mnemonicPair.name, mnemonicPair.value, this, ordinal);
46 public Mnemonic[] values()
48 return values.clone();
51 public Mnemonic get(int ordinal)
53 return values[ordinal];
56 public Mnemonic get(String text)
58 return byText.get(text);
61 public boolean contains(Mnemonic m)
64 return m.owner == this;
69 public boolean contains(String value)
71 return byText.keySet().contains(value);
79 public int getVectorLength()
85 public boolean conforms(MicroInstructionParameter param)
87 return ParameterClassification.super.conforms(param) && (param instanceof Mnemonic ? contains((Mnemonic) param) : false);
91 public ParameterType getExpectedType()
93 return ParameterType.MNEMONIC;
97 public int getExpectedBits()
102 public String[] getStringValues()
104 return stringValues.clone();
108 public int hashCode()
110 final int prime = 31;
112 result = prime * result + Arrays.hashCode(values);
117 public boolean equals(Object obj)
123 public Mnemonic parse(String toParse)
125 Mnemonic parsed = get(toParse);
127 throw new UnknownMnemonicException(toParse);
131 public static class MnemonicPair
133 public final String name;
134 public final BitVector value;
136 public MnemonicPair(String name, BitVector value)