X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.machine%2Fsrc%2Fnet%2Fmograsim%2Fmachine%2Fmi%2Fparameters%2FMnemonicFamily.java;h=a513b60a530f8f8a6305ed89b17cb9a83d26a9a1;hb=e104b507f81eebed56d923d5b0629f72d68d8b5a;hp=62785be6a9e4f3d78abd9449ff9b066a0025009c;hpb=21f5ad7261fa02502dceafd7dd1aa6f1718ef67b;p=Mograsim.git diff --git a/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/MnemonicFamily.java b/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/MnemonicFamily.java index 62785be6..a513b60a 100644 --- a/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/MnemonicFamily.java +++ b/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/MnemonicFamily.java @@ -4,36 +4,55 @@ import java.util.Arrays; import java.util.Map; import java.util.stream.Collectors; +import net.mograsim.logic.core.types.BitVector; import net.mograsim.machine.mi.parameters.MicroInstructionParameter.ParameterType; public class MnemonicFamily implements ParameterClassification { private final Mnemonic[] values; + private final String[] stringValues; private final Map byText; private final int vectorLength; - public MnemonicFamily(Mnemonic... values) + public MnemonicFamily(MnemonicPair... values) { - this.values = values; + this.values = new Mnemonic[values.length]; + this.stringValues = new String[values.length]; + + for(int i = 0; i < values.length; i++) + { + this.values[i] = createMnemonic(values[i], i); + this.stringValues[i] = values[i].name; + } if(values.length == 0) vectorLength = 0; else { - vectorLength = values[0].getValue().width(); + vectorLength = values[0].value.length(); for(int i = 1; i < values.length; i++) - if(values[i].getValue().width() != vectorLength) + if(values[i].value.length() != vectorLength) throw new IllegalArgumentException("MnemonicFamily is not of uniform vector length!"); } - byText = Arrays.stream(values).collect(Collectors.toMap(m -> m.getText(), m -> m)); + byText = Arrays.stream(this.values).collect(Collectors.toMap(m -> m.getText(), m -> m)); if(values.length != byText.keySet().size()) throw new IllegalArgumentException("MnemonicFamily contains multiple Mnemonics with the same name!"); } - public Mnemonic[] getValues() + private Mnemonic createMnemonic(MnemonicPair mnemonicPair, int ordinal) + { + return new Mnemonic(mnemonicPair.name, mnemonicPair.value, this, ordinal); + } + + public Mnemonic[] values() { return values.clone(); } + public Mnemonic get(int ordinal) + { + return values[ordinal]; + } + public Mnemonic get(String text) { return byText.get(text); @@ -42,7 +61,7 @@ public class MnemonicFamily implements ParameterClassification public boolean contains(Mnemonic m) { if(m != null) - return m.equals(byText.get(m.getText())); + return m.owner == this; else return false; } @@ -82,6 +101,42 @@ public class MnemonicFamily implements ParameterClassification public String[] getStringValues() { - return byText.keySet().toArray(new String[values.length]); + return stringValues.clone(); + } + + @Override + public int hashCode() + { + final int prime = 31; + int result = 1; + result = prime * result + Arrays.hashCode(values); + return result; + } + + @Override + public boolean equals(Object obj) + { + return this == obj; + } + + @Override + public Mnemonic parse(String toParse) + { + Mnemonic parsed = get(toParse); + if(parsed == null) + throw new UnknownMnemonicException(toParse); + return parsed; + } + + public static class MnemonicPair + { + public final String name; + public final BitVector value; + + public MnemonicPair(String name, BitVector value) + { + this.name = name; + this.value = value; + } } }