Example #1
0
        public void AssignOpcodesByFrequency(IEnumerable <Instruction> instructions)
        {
            // TODO: fix this
            var optimizedInstructionBytes =
                instructions
                .Where(i => i.Enabled)
                .Select(i => i.ToBytes())
                .ToList();

            var optimizedArgumentsFrequencies = new Frequencies(
                optimizedInstructionBytes
                .SelectMany(i => i.Skip(1))
                .ToArray()
                );

            var optimizedOpcodesFrequencies =
                new Frequencies(
                    optimizedInstructionBytes
                    .Where(b => b.Length > 0)
                    .Select(b => b.First())
                    .ToArray()
                    );

            var byteSet = optimizedArgumentsFrequencies.GetMostFrequentBytes().ToList();

            var opcodeSet = optimizedOpcodesFrequencies.GetMostFrequentBytes().ToList();

            for (byte b = 0; b < 255; b++)
            {
                if (byteSet.Count >= opcodeSet.Count)
                {
                    break;
                }

                if (!byteSet.Contains(b))
                {
                    byteSet.Add(b);
                }
            }

            for (var i = 0; i < opcodeSet.Count; i++)
            {
                var b = opcodeSet[i];
                map[(Opcodes)b].Value = byteSet[i];
            }
        }