X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.machine%2Fsrc%2Fnet%2Fmograsim%2Fmachine%2Fmi%2Fparameters%2FMnemonicFamily.java;h=f4343bd0d2a95ea85122daf415e6345b9ed657d0;hb=88e1b4382640ee4e907e06572fe6794bc925f344;hp=b9c137381ec05734bafabb1608cb29c6ffeb1695;hpb=a8c93edad3e4fc0651e2d18734ef189c376539a9;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 b9c13738..f4343bd0 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 @@ -11,14 +11,96 @@ public class MnemonicFamily implements ParameterClassification { private final Mnemonic[] values; private final String[] stringValues; - private final Map byText; - private final int vectorLength; + private Map byText; + private int vectorLength; + + public MnemonicFamily(String... names) + { + this(false, (int) Math.round(Math.ceil(Math.log(names.length) / Math.log(2))), names); + } + + public MnemonicFamily(boolean reverse, String... names) + { + this(reverse, (int) Math.round(Math.ceil(Math.log(names.length) / Math.log(2))), names); + } + + public MnemonicFamily(int bits, String... names) + { + this(false, bits, names); + } + + public MnemonicFamily(boolean reverse, int bits, String... names) + { + this.values = new Mnemonic[names.length]; + this.stringValues = new String[names.length]; + BitVector[] values = new BitVector[names.length]; + for(int i = 0; i < names.length; i++) + { + values[i] = BitVector.from(i, bits); + } + + setup(names, values, reverse); + } + + public MnemonicFamily(String[] names, long[] values, int bits) + { + this(false, names, values, bits); + } + + public MnemonicFamily(boolean reverse, String[] names, long[] values, int bits) + { + if(names.length != values.length) + throw new IllegalArgumentException(); + this.values = new Mnemonic[values.length]; + this.stringValues = new String[values.length]; + BitVector[] vectors = new BitVector[values.length]; + + for(int i = 0; i < vectors.length; i++) + { + vectors[i] = BitVector.from(values[i], bits); + } + + setup(names, vectors, reverse); + } + + public MnemonicFamily(String[] names, BitVector[] values) + { + this(false, names, values); + } + + public MnemonicFamily(boolean reverse, String[] names, BitVector[] values) + { + if(names.length != values.length) + throw new IllegalArgumentException(); + this.values = new Mnemonic[values.length]; + this.stringValues = new String[values.length]; + + setup(names, values, reverse); + } public MnemonicFamily(MnemonicPair... values) + { + this(false, values); + } + + public MnemonicFamily(boolean reverse, MnemonicPair... values) { this.values = new Mnemonic[values.length]; this.stringValues = new String[values.length]; + setup(values); + } + + private void setup(String[] names, BitVector[] values, boolean reverse) + { + MnemonicPair[] mnemonics = new MnemonicPair[values.length]; + for(int i = 0; i < values.length; i++) + mnemonics[i] = new MnemonicPair(names[i], reverse ? values[i].reverse() : values[i]); + setup(mnemonics); + } + + private void setup(MnemonicPair[] values) + { for(int i = 0; i < values.length; i++) { this.values[i] = createMnemonic(values[i], i); @@ -37,7 +119,7 @@ public class MnemonicFamily implements ParameterClassification if(values.length != byText.keySet().size()) throw new IllegalArgumentException("MnemonicFamily contains multiple Mnemonics with the same name!"); } - + private Mnemonic createMnemonic(MnemonicPair mnemonicPair, int ordinal) { return new Mnemonic(mnemonicPair.name, mnemonicPair.value, this, ordinal); @@ -104,8 +186,6 @@ public class MnemonicFamily implements ParameterClassification return stringValues.clone(); } - - @Override public int hashCode() { @@ -120,8 +200,15 @@ public class MnemonicFamily implements ParameterClassification { 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 {