using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Analyze
	{
	class InstructionStat
		{
		public long DisplacementLength { get; set; }
		public long ImmediateLength { get; set; }
		public long InstructionIndex { get; set; }
		public string Mnemonic { get; set; }
		public long ModifierLength { get; set; }
		public long SIBLength { get; set; }
		public long OpcodeLength { get; set; }
		public string Prefix { get; set; }
		public long PrefixLength { get; set; }
		public long Count { get; set; }


		public InstructionStat()
			{
			}

		/// <summary>
		/// Create a new stat based on the XML XElement
		/// </summary>
		/// <param name="element"></param>
		public InstructionStat(XElement element)
			{
			DisplacementLength = long.Parse(element.Attribute("DisplacementLength").Value);
			ImmediateLength = long.Parse(element.Attribute("ImmediateLength").Value);
			InstructionIndex = long.Parse(element.Attribute("InstructionIndex").Value);
			ModifierLength = long.Parse(element.Attribute("Modifier").Value);
			if (ModifierLength == 2) // SIB and no ModRM, violates our understanding that SIB implies must have ModRM
				throw new ArgumentOutOfRangeException("SIB found with no ModR/M byte");
			if (ModifierLength == 3)
				{
				ModifierLength = 1; // the MOD and SIB bytes are stored as flags
				SIBLength = 1;
				}
			OpcodeLength = long.Parse(element.Attribute("OpcodeLength").Value);
			long prefixValue = long.Parse(element.Attribute("Prefix").Value);
			if (prefixValue == 0)
				Prefix = "";
			else
				Prefix = String.Format("{0:X}", prefixValue);
			PrefixLength = (Prefix.Length + 1) / 2;
			Count = long.Parse(element.Attribute("Count").Value);
			Mnemonic = element.Attribute("Mnemonic").Value;
			}

		}
	}
