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.values = new Mnemonic[names.length];
20 this.stringValues = new String[names.length];
21 BitVector[] values = new BitVector[names.length];
22 int bits = (int) Math.ceil(Math.log(names.length));
23 for(int i = 0; i < names.length; i++)
25 values[i] = BitVector.from(i, bits);
31 public MnemonicFamily(String[] names, long[] values, int bits)
33 if(names.length != values.length)
34 throw new IllegalArgumentException();
35 this.values = new Mnemonic[values.length];
36 this.stringValues = new String[values.length];
37 BitVector[] vectors = new BitVector[values.length];
39 for(int i = 0; i < vectors.length; i++)
41 vectors[i] = BitVector.from(values[i], bits);
44 setup(names, vectors);
47 public MnemonicFamily(String[] names, BitVector[] values)
49 if(names.length != values.length)
50 throw new IllegalArgumentException();
51 this.values = new Mnemonic[values.length];
52 this.stringValues = new String[values.length];
57 public MnemonicFamily(MnemonicPair... values)
59 this.values = new Mnemonic[values.length];
60 this.stringValues = new String[values.length];
65 private void setup(String[] names, BitVector[] values)
67 MnemonicPair[] mnemonics = new MnemonicPair[values.length];
68 for(int i = 0; i < values.length; i++)
69 mnemonics[i] = new MnemonicPair(names[i], values[i]);
73 private void setup(MnemonicPair[] values)
75 for(int i = 0; i < values.length; i++)
77 this.values[i] = createMnemonic(values[i], i);
78 this.stringValues[i] = values[i].name;
80 if(values.length == 0)
84 vectorLength = values[0].value.length();
85 for(int i = 1; i < values.length; i++)
86 if(values[i].value.length() != vectorLength)
87 throw new IllegalArgumentException("MnemonicFamily is not of uniform vector length!");
89 byText = Arrays.stream(this.values).collect(Collectors.toMap(m -> m.getText(), m -> m));
90 if(values.length != byText.keySet().size())
91 throw new IllegalArgumentException("MnemonicFamily contains multiple Mnemonics with the same name!");
94 private Mnemonic createMnemonic(MnemonicPair mnemonicPair, int ordinal)
96 return new Mnemonic(mnemonicPair.name, mnemonicPair.value, this, ordinal);
99 public Mnemonic[] values()
101 return values.clone();
104 public Mnemonic get(int ordinal)
106 return values[ordinal];
109 public Mnemonic get(String text)
111 return byText.get(text);
114 public boolean contains(Mnemonic m)
117 return m.owner == this;
122 public boolean contains(String value)
124 return byText.keySet().contains(value);
129 return values.length;
132 public int getVectorLength()
138 public boolean conforms(MicroInstructionParameter param)
140 return ParameterClassification.super.conforms(param) && (param instanceof Mnemonic ? contains((Mnemonic) param) : false);
144 public ParameterType getExpectedType()
146 return ParameterType.MNEMONIC;
150 public int getExpectedBits()
155 public String[] getStringValues()
157 return stringValues.clone();
161 public int hashCode()
163 final int prime = 31;
165 result = prime * result + Arrays.hashCode(values);
170 public boolean equals(Object obj)
176 public Mnemonic parse(String toParse)
178 Mnemonic parsed = get(toParse);
180 throw new UnknownMnemonicException(toParse);
184 public static class MnemonicPair
186 public final String name;
187 public final BitVector value;
189 public MnemonicPair(String name, BitVector value)