static MnemonicOptionsTestCase ParseLine(string line) { var elems = line.Split(commaSeparator); if (elems.Length != 5) { throw new Exception($"Invalid number of commas: {elems.Length - 1}"); } var hexBytes = elems[0].Trim(); var code = ToEnumConverter.GetCode(elems[1].Trim()); var bitness = NumberConverter.ToInt32(elems[2].Trim()); var formattedString = elems[3].Trim().Replace('|', ','); var flags = FormatMnemonicOptions.None; foreach (var value in elems[4].Split(spaceSeparator, StringSplitOptions.RemoveEmptyEntries)) { if (!Dicts.ToFormatMnemonicOptions.TryGetValue(value, out var f)) { throw new InvalidOperationException($"Invalid flags value: {value}"); } flags |= f; } return(new MnemonicOptionsTestCase(hexBytes, code, bitness, formattedString, flags)); }
static DecoderMemoryTestCase ReadTestCase(int bitness, string line, int lineNumber) { var parts = line.Split(colSep, StringSplitOptions.None); if (parts.Length != 11 && parts.Length != 12) { throw new InvalidOperationException(); } var tc = new DecoderMemoryTestCase(); tc.LineNumber = lineNumber; tc.Bitness = bitness; tc.HexBytes = parts[0].Trim(); tc.Code = ToEnumConverter.GetCode(parts[1].Trim()); tc.Register = ToEnumConverter.GetRegister(parts[2].Trim()); tc.SegmentPrefix = ToEnumConverter.GetRegister(parts[3].Trim()); tc.SegmentRegister = ToEnumConverter.GetRegister(parts[4].Trim()); tc.BaseRegister = ToEnumConverter.GetRegister(parts[5].Trim()); tc.IndexRegister = ToEnumConverter.GetRegister(parts[6].Trim()); tc.Scale = (int)ParseUInt32(parts[7].Trim()); tc.Displacement = ParseUInt32(parts[8].Trim()); tc.DisplacementSize = (int)ParseUInt32(parts[9].Trim()); var coStr = parts[10].Trim(); if (!DecoderTestParser.TryParseConstantOffsets(coStr, out tc.ConstantOffsets)) { throw new InvalidOperationException($"Invalid ConstantOffsets: '{coStr}'"); } tc.EncodedHexBytes = parts.Length > 11 ? parts[11].Trim() : tc.HexBytes; tc.DecoderOptions = DecoderOptions.None; tc.CanEncode = true; return(tc); }
static SymbolResolverTestCase ParseLine(string line) { var elems = line.Split(commaSeparator); const int SYM_RES_INDEX = 4; if (elems.Length < SYM_RES_INDEX) { throw new Exception($"Invalid number of commas: {elems.Length - 1}"); } var bitness = NumberConverter.ToInt32(elems[0].Trim()); var hexBytes = elems[1].Trim(); var code = ToEnumConverter.GetCode(elems[2].Trim()); var options = new List <(OptionsProps property, object value)>(); foreach (var part in elems[3].Split(spaceSeparator, StringSplitOptions.RemoveEmptyEntries)) { options.Add(OptionsParser.ParseOption(part)); } var symbolResults = new SymbolResultTestCase[elems.Length - SYM_RES_INDEX]; for (int i = 0; i < symbolResults.Length; i++) { var symParts = elems[SYM_RES_INDEX + i].Split(semicolonSeparator); if (symParts.Length != 5) { throw new Exception($"Invalid number of semicolons: {symParts.Length - 1}"); } var address = NumberConverter.ToUInt64(symParts[0].Trim()); var symbolAddress = NumberConverter.ToUInt64(symParts[1].Trim()); var addressSize = NumberConverter.ToInt32(symParts[2].Trim()); var symbolParts = symParts[3].Split(barSeparator); MemorySize?memorySize = null; var flags = SymbolFlags.None; foreach (var value in symParts[4].Split(spaceSeparator, StringSplitOptions.RemoveEmptyEntries)) { if (Dicts.ToSymbolFlags.TryGetValue(value, out var f)) { flags |= f; } else { if (!ToEnumConverter.TryMemorySize(value, out var memSize)) { throw new Exception($"Invalid value: {value}"); } memorySize = memSize; } } symbolResults[i] = new SymbolResultTestCase(address, symbolAddress, addressSize, flags, memorySize, symbolParts); } return(new SymbolResolverTestCase(bitness, hexBytes, code, options.ToArray(), symbolResults)); }
static DecoderMemoryTestCase ReadTestCase(int bitness, string line, int lineNumber) { var parts = line.Split(colSep, StringSplitOptions.None); if (parts.Length != 11 && parts.Length != 12) { throw new InvalidOperationException(); } var tc = new DecoderMemoryTestCase(); tc.LineNumber = lineNumber; tc.Bitness = bitness; tc.IP = bitness switch { 16 => DecoderConstants.DEFAULT_IP16, 32 => DecoderConstants.DEFAULT_IP32, 64 => DecoderConstants.DEFAULT_IP64, _ => throw new InvalidOperationException(), }; tc.HexBytes = parts[0].Trim(); var code = parts[1].Trim(); if (CodeUtils.IsIgnored(code)) { return(null); } tc.Code = ToEnumConverter.GetCode(code); tc.Register = ToEnumConverter.GetRegister(parts[2].Trim()); tc.SegmentPrefix = ToEnumConverter.GetRegister(parts[3].Trim()); tc.SegmentRegister = ToEnumConverter.GetRegister(parts[4].Trim()); tc.BaseRegister = ToEnumConverter.GetRegister(parts[5].Trim()); tc.IndexRegister = ToEnumConverter.GetRegister(parts[6].Trim()); tc.Scale = (int)NumberConverter.ToUInt32(parts[7].Trim()); tc.Displacement = NumberConverter.ToUInt64(parts[8].Trim()); tc.DisplacementSize = (int)NumberConverter.ToUInt32(parts[9].Trim()); var coStr = parts[10].Trim(); if (!DecoderTestParser.TryParseConstantOffsets(coStr, out tc.ConstantOffsets)) { throw new InvalidOperationException($"Invalid ConstantOffsets: '{coStr}'"); } tc.EncodedHexBytes = parts.Length > 11 ? parts[11].Trim() : tc.HexBytes; tc.DecoderOptions = DecoderOptions.None; tc.TestOptions = DecoderTestOptions.None; return(tc); }
static MnemonicOptionsTestCase?ParseLine(string line) { var elems = line.Split(commaSeparator); if (elems.Length != 5) { throw new Exception($"Invalid number of commas: {elems.Length - 1}"); } var hexBytes = elems[0].Trim(); var codeStr = elems[1].Trim(); if (CodeUtils.IsIgnored(codeStr)) { return(null); } var code = ToEnumConverter.GetCode(codeStr); var bitness = NumberConverter.ToInt32(elems[2].Trim()); var ip = bitness switch { 16 => DecoderConstants.DEFAULT_IP16, 32 => DecoderConstants.DEFAULT_IP32, 64 => DecoderConstants.DEFAULT_IP64, _ => throw new InvalidOperationException(), }; var formattedString = elems[3].Trim().Replace('|', ','); var flags = FormatMnemonicOptions.None; foreach (var value in elems[4].Split(spaceSeparator, StringSplitOptions.RemoveEmptyEntries)) { if (!Dicts.ToFormatMnemonicOptions.TryGetValue(value, out var f)) { throw new InvalidOperationException($"Invalid flags value: {value}"); } flags |= f; } return(new MnemonicOptionsTestCase(hexBytes, code, bitness, ip, formattedString, flags)); }
static (string hexBytes, Code code, DecoderOptions options, InstructionInfoTestCase testCase) ParseLine(string line, int bitness, Dictionary <string, Register> toRegister) { Static.Assert(MiscInstrInfoTestConstants.InstrInfoElemsPerLine == 5 ? 0 : -1); var elems = line.Split(commaSeparator, MiscInstrInfoTestConstants.InstrInfoElemsPerLine); if (elems.Length != MiscInstrInfoTestConstants.InstrInfoElemsPerLine) { throw new Exception($"Expected {MiscInstrInfoTestConstants.InstrInfoElemsPerLine - 1} commas"); } var testCase = new InstructionInfoTestCase(); var hexBytes = ToHexBytes(elems[0].Trim()); var code = ToEnumConverter.GetCode(elems[1].Trim()); testCase.Encoding = ToEnumConverter.GetEncodingKind(elems[2].Trim()); var cpuidFeatureStrings = elems[3].Trim().Split(new[] { ';' }); var cpuidFeatures = new CpuidFeature[cpuidFeatureStrings.Length]; testCase.CpuidFeatures = cpuidFeatures; for (int i = 0; i < cpuidFeatures.Length; i++) { cpuidFeatures[i] = ToEnumConverter.GetCpuidFeature(cpuidFeatureStrings[i]); } var options = DecoderOptions.None; foreach (var keyValue in elems[4].Split(spaceSeparator, StringSplitOptions.RemoveEmptyEntries)) { string key, value; int index = keyValue.IndexOf('='); if (index >= 0) { key = keyValue.Substring(0, index); value = keyValue.Substring(index + 1); } else { key = keyValue; value = string.Empty; } switch (key) { case InstructionInfoKeys.IsProtectedMode: if (value != string.Empty) { throw new Exception($"Invalid key-value value, '{keyValue}'"); } testCase.IsProtectedMode = true; break; case InstructionInfoKeys.IsPrivileged: if (value != string.Empty) { throw new Exception($"Invalid key-value value, '{keyValue}'"); } testCase.IsPrivileged = true; break; case InstructionInfoKeys.IsSaveRestoreInstruction: if (value != string.Empty) { throw new Exception($"Invalid key-value value, '{keyValue}'"); } testCase.IsSaveRestoreInstruction = true; break; case InstructionInfoKeys.IsStackInstruction: if (!int.TryParse(value, out testCase.StackPointerIncrement)) { throw new Exception($"Invalid key-value value, '{keyValue}'"); } testCase.IsStackInstruction = true; break; case InstructionInfoKeys.IsSpecial: if (value != string.Empty) { throw new Exception($"Invalid key-value value, '{keyValue}'"); } testCase.IsSpecial = true; break; case InstructionInfoKeys.RflagsRead: if (!ParseRflags(value, ref testCase.RflagsRead)) { throw new Exception($"Invalid key-value value, '{keyValue}'"); } break; case InstructionInfoKeys.RflagsUndefined: if (!ParseRflags(value, ref testCase.RflagsUndefined)) { throw new Exception($"Invalid key-value value, '{keyValue}'"); } break; case InstructionInfoKeys.RflagsWritten: if (!ParseRflags(value, ref testCase.RflagsWritten)) { throw new Exception($"Invalid key-value value, '{keyValue}'"); } break; case InstructionInfoKeys.RflagsCleared: if (!ParseRflags(value, ref testCase.RflagsCleared)) { throw new Exception($"Invalid key-value value, '{keyValue}'"); } break; case InstructionInfoKeys.RflagsSet: if (!ParseRflags(value, ref testCase.RflagsSet)) { throw new Exception($"Invalid key-value value, '{keyValue}'"); } break; case InstructionInfoKeys.FlowControl: if (!ToEnumConverter.TryFlowControl(value, out testCase.FlowControl)) { throw new Exception($"Invalid key-value value, '{keyValue}'"); } break; case InstructionInfoKeys.Op0Access: if (!InstructionInfoDicts.ToAccess.TryGetValue(value, out testCase.Op0Access)) { throw new Exception($"Invalid key-value value, '{keyValue}'"); } break; case InstructionInfoKeys.Op1Access: if (!InstructionInfoDicts.ToAccess.TryGetValue(value, out testCase.Op1Access)) { throw new Exception($"Invalid key-value value, '{keyValue}'"); } break; case InstructionInfoKeys.Op2Access: if (!InstructionInfoDicts.ToAccess.TryGetValue(value, out testCase.Op2Access)) { throw new Exception($"Invalid key-value value, '{keyValue}'"); } break; case InstructionInfoKeys.Op3Access: if (!InstructionInfoDicts.ToAccess.TryGetValue(value, out testCase.Op3Access)) { throw new Exception($"Invalid key-value value, '{keyValue}'"); } break; case InstructionInfoKeys.Op4Access: if (!InstructionInfoDicts.ToAccess.TryGetValue(value, out testCase.Op4Access)) { throw new Exception($"Invalid key-value value, '{keyValue}'"); } break; case InstructionInfoKeys.ReadRegister: if (!AddRegisters(toRegister, value, OpAccess.Read, testCase)) { throw new Exception($"Invalid key-value value, '{keyValue}'"); } break; case InstructionInfoKeys.CondReadRegister: if (!AddRegisters(toRegister, value, OpAccess.CondRead, testCase)) { throw new Exception($"Invalid key-value value, '{keyValue}'"); } break; case InstructionInfoKeys.WriteRegister: if (!AddRegisters(toRegister, value, OpAccess.Write, testCase)) { throw new Exception($"Invalid key-value value, '{keyValue}'"); } break; case InstructionInfoKeys.CondWriteRegister: if (!AddRegisters(toRegister, value, OpAccess.CondWrite, testCase)) { throw new Exception($"Invalid key-value value, '{keyValue}'"); } break; case InstructionInfoKeys.ReadWriteRegister: if (!AddRegisters(toRegister, value, OpAccess.ReadWrite, testCase)) { throw new Exception($"Invalid key-value value, '{keyValue}'"); } break; case InstructionInfoKeys.ReadCondWriteRegister: if (!AddRegisters(toRegister, value, OpAccess.ReadCondWrite, testCase)) { throw new Exception($"Invalid key-value value, '{keyValue}'"); } break; case InstructionInfoKeys.ReadMemory: if (!AddMemory(bitness, toRegister, value, OpAccess.Read, testCase)) { throw new Exception($"Invalid key-value value, '{keyValue}'"); } break; case InstructionInfoKeys.CondReadMemory: if (!AddMemory(bitness, toRegister, value, OpAccess.CondRead, testCase)) { throw new Exception($"Invalid key-value value, '{keyValue}'"); } break; case InstructionInfoKeys.ReadWriteMemory: if (!AddMemory(bitness, toRegister, value, OpAccess.ReadWrite, testCase)) { throw new Exception($"Invalid key-value value, '{keyValue}'"); } break; case InstructionInfoKeys.ReadCondWriteMemory: if (!AddMemory(bitness, toRegister, value, OpAccess.ReadCondWrite, testCase)) { throw new Exception($"Invalid key-value value, '{keyValue}'"); } break; case InstructionInfoKeys.WriteMemory: if (!AddMemory(bitness, toRegister, value, OpAccess.Write, testCase)) { throw new Exception($"Invalid key-value value, '{keyValue}'"); } break; case InstructionInfoKeys.CondWriteMemory: if (!AddMemory(bitness, toRegister, value, OpAccess.CondWrite, testCase)) { throw new Exception($"Invalid key-value value, '{keyValue}'"); } break; case InstructionInfoKeys.DecoderOptions: if (!TryParseDecoderOptions(value.Split(semicolonSeparator), ref options)) { throw new Exception($"Invalid key-value value, '{keyValue}'"); } break; default: throw new Exception($"Invalid key-value value, '{keyValue}'"); } } return(hexBytes, code, options, testCase); }