Esempio n. 1
0
        private void ParseData(List <SectionEntry> sectionEntries, OperandSize size, string operandsString)
        {
            // TODO: handle strings with commas
            var result = DatasParser(size).Run(operandsString);

            switch (result)
            {
            case Parser <char, IEnumerable <Data> > .Success success:
                foreach (var value in success.Value)
                {
                    sectionEntries.Add(value);
                }

                break;

            case Parser <char, IEnumerable <Data> > .Failure failure:
                throw new InvalidOperationException(failure.FailureText);
            }

            //var operands = operandsString.Split(',').Select(x => x.Trim());

            //foreach (var operand in operands)
            //{
            //    sectionEntries.Add(ParseDataOperand(size, operand));
            //}
        }
Esempio n. 2
0
        private Parser <char, IEnumerable <Data> > DatasParser(OperandSize size)
        {
            var doubleQuotedString = Satisfy(c => c != '"')
                                     .Many()
                                     .ToStringParser()
                                     .Between(Char('"'), Char('"'))
                                     .Select(str => new Data(str));

            var singleQuotedString = Satisfy(c => c != '\'')
                                     .Many()
                                     .ToStringParser()
                                     .Between(Char('\''), Char('\''))
                                     .Select(str => new Data(str));

            var hexChars =
                from _ in String("0x")
                from hex in AnyOf("0123456789abcdefABCDEF").OneOrMore().ToStringParser()
                select ParseHexAsData(size, hex);

            var intChars =
                from intValue in IntParser
                select ParseIntAsData(size, intValue.ToString()); // HACK: this could be improved

            var dataParser = doubleQuotedString.OrElse(singleQuotedString).OrElse(hexChars).OrElse(intChars);

            var sep = Char(',').TakeLeft(SkipWhitespaceChar.ZeroOrMore());

            return(dataParser.SepBy1(sep));
        }
Esempio n. 3
0
 protected EncoderOpCodeHandler(Code code, uint opCode, int groupIndex, Encodable encodable, OperandSize opSize, AddressSize addrSize, TryConvertToDisp8N tryConvertToDisp8N, Op[] operands)
 {
     TEST_Code          = code;
     OpCode             = opCode;
     GroupIndex         = groupIndex;
     Encodable          = encodable;
     OpSize             = opSize;
     AddrSize           = addrSize;
     TryConvertToDisp8N = tryConvertToDisp8N;
     Operands           = operands;
 }
Esempio n. 4
0
 protected OpCodeHandler(uint opCode, int groupIndex, OpCodeHandlerFlags flags, Encodable encodable, OperandSize opSize, AddressSize addrSize, TryConvertToDisp8N?tryConvertToDisp8N, Op[] operands)
 {
     OpCode             = opCode;
     GroupIndex         = groupIndex;
     Flags              = flags;
     Encodable          = encodable;
     OpSize             = opSize;
     AddrSize           = addrSize;
     TryConvertToDisp8N = tryConvertToDisp8N;
     Operands           = operands;
 }
Esempio n. 5
0
 public LegacyOpCodeInfo(EnumValue code, MandatoryPrefix mandatoryPrefix, OpCodeTableKind table, uint opCode, int groupIndex, OperandSize operandSize, AddressSize addressSize, OpCodeFlags flags, LegacyOpKind[] opKinds)
 {
     Code            = code;
     MandatoryPrefix = mandatoryPrefix;
     Table           = table;
     OpCode          = opCode;
     GroupIndex      = groupIndex;
     Flags           = flags;
     OperandSize     = operandSize;
     AddressSize     = addressSize;
     OpKinds         = opKinds;
 }
Esempio n. 6
0
		private static int GetOperandSizeMatchLevel(InstructionEncoding encoding, OperandSize size)
		{
			switch (encoding & InstructionEncoding.OperandSize_Mask)
			{
				case InstructionEncoding.OperandSize_Ignored: return 0;
				case InstructionEncoding.OperandSize_16Or32Or64: return 1;
				case InstructionEncoding.OperandSize_16Or32: return 2;
				case InstructionEncoding.OperandSize_32Or64: return 2;
				case InstructionEncoding.OperandSize_Fixed8: return 3;
				case InstructionEncoding.OperandSize_Fixed16: return size == OperandSize.Word ? 3 : -1;
				case InstructionEncoding.OperandSize_Fixed32: return size == OperandSize.Dword ? 3 : -1;
				case InstructionEncoding.OperandSize_Fixed64: return size == OperandSize.Qword ? 3 : -1;
				default: throw new ArgumentException();
			}
		}
Esempio n. 7
0
		public static int InBytes(this ImmediateSize size, OperandSize operandSize, AddressSize addressSize)
		{
			Contract.Requires(operandSize >= OperandSize.Word && operandSize <= OperandSize.Qword);
			switch (size)
			{
				case ImmediateSize.Zero: return 0;
				case ImmediateSize.Fixed8: return 1;
				case ImmediateSize.Fixed16: return 2;
				case ImmediateSize.Fixed32: return 4;
				case ImmediateSize.Fixed64: return 8;
				case ImmediateSize.Operand16Or32: return operandSize == OperandSize.Word ? 2 : 4;
				case ImmediateSize.Operand16Or32Or64: return operandSize == OperandSize.Qword ? 8 : 4;
				case ImmediateSize.Address16Or32: return addressSize == AddressSize._16 ? 2 : 4;
				default: throw new ArgumentException(nameof(size));
			}
		}
Esempio n. 8
0
        private static Data ParseHexAsData(OperandSize size, string hexValue)
        {
            switch (size)
            {
            case OperandSize.Size8 when byte.TryParse(hexValue, NumberStyles.HexNumber, null, out var result):
                return(new Data(result));

            case OperandSize.Size16 when short.TryParse(hexValue, NumberStyles.HexNumber, null, out var result):
                return(new Data(result));

            case OperandSize.Size32 when int.TryParse(hexValue, NumberStyles.HexNumber, null, out var result):
                return(new Data(result));

            case OperandSize.Size64 when long.TryParse(hexValue, NumberStyles.HexNumber, null, out var result):
                return(new Data(result));

            case OperandSize.None:
            default:
                throw new InvalidOperationException("Unknown operand data value: " + hexValue);
            }
        }
Esempio n. 9
0
        //private static Data ParseDataOperand(OperandSize size, string operand)
        //{
        //    if ((operand.StartsWith("'") && operand.EndsWith("'")) ||
        //        (operand.StartsWith("\"") && operand.EndsWith("\""))) // string
        //    {
        //        return new Data(operand.Substring(1, operand.Length - 2));
        //    }

        //    if (operand.StartsWith("0x")) //hex
        //    {
        //        var hexValue = operand.Substring(2);

        //        return ParseHexAsData(size, hexValue);
        //    }

        //    // assume int
        //    return ParseIntAsData(size, operand);
        //}

        private static Data ParseIntAsData(OperandSize size, string operand)
        {
            switch (size)
            {
            case OperandSize.Size8 when byte.TryParse(operand, out var result):
                return(new Data(result));

            case OperandSize.Size16 when short.TryParse(operand, out var result):
                return(new Data(result));

            case OperandSize.Size32 when int.TryParse(operand, out var result):
                return(new Data(result));

            case OperandSize.Size64 when long.TryParse(operand, out var result):
                return(new Data(result));

            case OperandSize.None:
            default:
                throw new InvalidOperationException("Unknown operand data value: " + operand);
            }
        }
Esempio n. 10
0
 public RegisterOperand(OperandType type, OperandSize size, int registerIndex = 0)
     : base(type, size)
 {
     Index = registerIndex;
 }
Esempio n. 11
0
		public static Opcode WithVectorSize(this Opcode opcode, OperandSize size)
		{
			Contract.Requires(size >= OperandSize._128 && size <= OperandSize._512);
			opcode &= ~Opcode.VexL_Mask;
			opcode |= (Opcode)((size - OperandSize._128) << (int)Opcode.VexL_0);
			return opcode;
		}
Esempio n. 12
0
 public Operand(OperandType type, OperandSize size, EncodingPosition eposition)
 {
     Type = type;
     Size = size;
     EncodingPosition = eposition;
 }
Esempio n. 13
0
 private Operand(OperandType type, uint value, OperandSize size)
 {
     _type  = type;
     _value = value;
     _size  = size;
 }
Esempio n. 14
0
        private Operand GetOperand(string operandString, OperandSize expectedSize = OperandSize.Unknown)
        {
            var operandHasParens = operandString.StartsWith("(") && operandString.EndsWith(")");

            if (!operandHasParens)
            {
                if (expectedSize != OperandSize.SixteenBit)
                {
                    if (Enum.GetNames(typeof(Register)).Contains(operandString.ToUpper()) && Enum.TryParse <Register>(operandString, true, out var register))
                    {
                        return(new RegisterOperand(register));
                    }
                    if (_hexParser.TryByteParse(operandString, out var immediateNumber))
                    {
                        return(new ImmediateOperand(immediateNumber));
                    }
                }
                if (expectedSize != OperandSize.EightBit)
                {
                    if (Enum.GetNames(typeof(ExtendedRegister)).Contains(operandString.ToUpper()) && Enum.TryParse <ExtendedRegister>(operandString, true, out var extendedRegister))
                    {
                        return(new RegisterExtendedOperand(extendedRegister));
                    }
                    if (_hexParser.TryUShortParse(operandString, out var immediate16BitNumber))
                    {
                        return(new ImmediateExtendedOperand(immediate16BitNumber));
                    }
                }
                if (IsValidLabel(operandString))
                {
                    return(new LabeledImmediateOperand(operandString));
                }
                if (new Regex(@"[\+\-\*\/]", RegexOptions.IgnoreCase).IsMatch(operandString))
                {
                    return(new CalculatedImmediateOperand(operandString, _hexParser));
                }
            }
            else
            {
                var operandWithoutParens = operandString.TrimStart('(').TrimEnd(')');
                if (Enum.GetNames(typeof(ExtendedRegister)).Contains(operandWithoutParens.ToUpper()) && Enum.TryParse <ExtendedRegister>(operandWithoutParens, true, out var extendedRegister))
                {
                    return(new IndirectRegisterOperand(extendedRegister));
                }
                if (Enum.GetNames(typeof(Register)).Contains(operandWithoutParens.ToUpper()) && Enum.TryParse <Register>(operandWithoutParens, true, out var register))
                {
                    return(new IndirectShortRegOperand(register));
                }
                if (_hexParser.TryUShortParse(operandWithoutParens, out var memoryAddress))
                {
                    return(new ExtendedAddressOperand(memoryAddress));
                }
                if (TryDisplacementParse(operandWithoutParens, out var displacementRegister, out var displacement))
                {
                    return(new DisplacementOperand(displacementRegister, displacement));
                }
                if (IsValidLabel(operandWithoutParens))
                {
                    return(new LabeledAddressOperand(operandWithoutParens));
                }
                if (_hexParser.TryByteParse(operandWithoutParens, out var immediateValue))
                {
                    throw new Exception("need to start parsing in and out commands");
                }
            }
            throw new Exception($"Invalid operand: {operandString}");
        }
Esempio n. 15
0
 protected Operand(OperandType type = OperandType.GeneralRegister, OperandSize size = OperandSize.DWord)
 {
     Type = type;
     Size = size;
 }
Esempio n. 16
0
 public MemoryOperand(OperandSize size, Operand address)
     : base(OperandType.Memory, size)
 {
     Address = address;
 }
Esempio n. 17
0
 public static string GetOpSizeStr(OperandSize Size)
 {
     return System.Enum.GetName(typeof(OperandSize), Size).ToLower();
 }
Esempio n. 18
0
 public OperandInfo(string operand)
 {
     operand=operand.ToLower();
     string[] ss=operand.Split(' ');
     if(ss.Length>2) throw new OptimizationException("Operand: More than two parts");
     if(ss.Length==2) {
         Qualifier=ss[0];
         operand=ss[1];
     } else { Qualifier=null; }
     if(operand.IndexOf('*')!=-1) throw new OptimizationException("Operand: Haven't programmed '*' yet");
     Operand=operand;
     bool p=false;
     if(operand.StartsWith("[")) {
         p=true;
         operand=operand.Replace("[","").Replace("]","");
     }
     if(operand.StartsWith("+0x")||operand.StartsWith("-0x")||operand.StartsWith("0x")) {
         if(p) { OpType=OperandType.dMemory; } else { OpType=OperandType.Immediate; }
         Offset=Program.HexParse(operand);
         Register=null;
         MultiPart=false;
     } else {
         if(p) { OpType=OperandType.rMemory; } else { OpType=OperandType.Register; }
         ss=operand.Split(':');
         if(ss.Length>2) throw new OptimizationException("Operand: Illegal operand");
         if(ss.Length==2) {
             operand=ss[1];
             MultiPart=true;
         } else { MultiPart=false; }
         if(operand.IndexOf("+")!=-1) {
             ss=operand.Split('+');
             operand=ss[0];
             if(!ss[1].StartsWith("0x")) throw new OptimizationException("Operand: Illegal offset");
             Offset=Program.HexParse(ss[1]);
         } else if(operand.IndexOf("-")!=-1) {
             ss=operand.Split('-');
             operand=ss[0];
             if(!ss[1].StartsWith("0x")) throw new OptimizationException("Operand: Illegal offset");
             Offset=-Program.HexParse(ss[1]);
         } else { Offset=0; }
         if(Array.IndexOf(Program.Registers,operand)==-1) throw new OptimizationException("Operand: unknown register");
         Register=operand;
     }
     if(OpType==OperandType.Register) {
         OpSize=OperandSize.dWord;
     } else if(Qualifier!=null) {
         switch(Qualifier) {
             case ("qword"): OpSize=OperandSize.qWord; break;
             case ("dword"): OpSize=OperandSize.dWord; break;
             case ("word"): OpSize=OperandSize.Word; break;
             case ("byte"): OpSize=OperandSize.Byte; break;
             default: throw new OptimizationException("Operand: Unknown prefix");
         }
     } else {
         OpSize=OperandSize.Unknown;
     }
 }
Esempio n. 19
0
 public ImmediateOperand(OperandSize size, long value)
     : base(OperandType.Immediate, size)
 {
     Value = value;
 }
Esempio n. 20
0
 public AddressOperand(OperandSize size, string label)
     : base(OperandType.Address, size)
 {
     Label = label;
 }
Esempio n. 21
0
		public static int GetImmediatesSizeInBytes(this InstructionEncoding encoding,
			OperandSize operandSize, AddressSize addressSize)
		{
			Contract.Requires(operandSize >= OperandSize.Word && operandSize <= OperandSize.Qword);
			return GetFirstImmediateSize(encoding).InBytes(operandSize, addressSize)
				+ GetSecondImmediateSize(encoding).InBytes(operandSize, addressSize);
		}
Esempio n. 22
0
 private OperandTypeDefinition(OperandType type, OperandSize size)
 {
     Type = type;
     Size = size;
 }
Esempio n. 23
0
 public static string GetOpSizeStr(OperandSize Size)
 {
     return(System.Enum.GetName(typeof(OperandSize), Size).ToLower());
 }
Esempio n. 24
0
 public LegacyOpCodeInfo(EnumValue code, MandatoryPrefix mandatoryPrefix, OpCodeTableKind table, uint opCode, int groupIndex, OperandSize operandSize, AddressSize addressSize, OpCodeFlags flags, LegacyOpKind[] opKinds)
     : this(code, mandatoryPrefix, table, opCode, groupIndex, -1, operandSize, addressSize, flags, opKinds)
 {
 }
Esempio n. 25
0
 public MemoryGetNode(AstNode index, OperandSize size)
 {
     Index = index;
     Size  = size;
 }
 private FixedOperandDefinition(string name, OperandSize size)
 {
     Name = name;
     Size = size;
 }
Esempio n. 27
0
        public OperandInfo(string operand)
        {
            operand = operand.ToLower();
            string[] ss = operand.Split(' ');
            if (ss.Length > 2)
            {
                throw new OptimizationException("Operand: More than two parts");
            }
            if (ss.Length == 2)
            {
                Qualifier = ss[0];
                operand   = ss[1];
            }
            else
            {
                Qualifier = null;
            }
            if (operand.IndexOf('*') != -1)
            {
                throw new OptimizationException("Operand: Haven't programmed '*' yet");
            }
            Operand = operand;
            bool p = false;

            if (operand.StartsWith("["))
            {
                p       = true;
                operand = operand.Replace("[", "").Replace("]", "");
            }
            if (operand.StartsWith("+0x") || operand.StartsWith("-0x") || operand.StartsWith("0x"))
            {
                if (p)
                {
                    OpType = OperandType.dMemory;
                }
                else
                {
                    OpType = OperandType.Immediate;
                }
                Offset    = Program.HexParse(operand);
                Register  = null;
                MultiPart = false;
            }
            else
            {
                if (p)
                {
                    OpType = OperandType.rMemory;
                }
                else
                {
                    OpType = OperandType.Register;
                }
                ss = operand.Split(':');
                if (ss.Length > 2)
                {
                    throw new OptimizationException("Operand: Illegal operand");
                }
                if (ss.Length == 2)
                {
                    operand   = ss[1];
                    MultiPart = true;
                }
                else
                {
                    MultiPart = false;
                }
                if (operand.IndexOf("+") != -1)
                {
                    ss      = operand.Split('+');
                    operand = ss[0];
                    if (!ss[1].StartsWith("0x"))
                    {
                        throw new OptimizationException("Operand: Illegal offset");
                    }
                    Offset = Program.HexParse(ss[1]);
                }
                else if (operand.IndexOf("-") != -1)
                {
                    ss      = operand.Split('-');
                    operand = ss[0];
                    if (!ss[1].StartsWith("0x"))
                    {
                        throw new OptimizationException("Operand: Illegal offset");
                    }
                    Offset = -Program.HexParse(ss[1]);
                }
                else
                {
                    Offset = 0;
                }
                if (Array.IndexOf(Program.Registers, operand) == -1)
                {
                    throw new OptimizationException("Operand: unknown register");
                }
                Register = operand;
            }
            if (OpType == OperandType.Register)
            {
                OpSize = OperandSize.dWord;
            }
            else if (Qualifier != null)
            {
                switch (Qualifier)
                {
                case ("qword"): OpSize = OperandSize.qWord; break;

                case ("dword"): OpSize = OperandSize.dWord; break;

                case ("word"): OpSize = OperandSize.Word; break;

                case ("byte"): OpSize = OperandSize.Byte; break;

                default: throw new OptimizationException("Operand: Unknown prefix");
                }
            }
            else
            {
                OpSize = OperandSize.Unknown;
            }
        }