示例#1
0
        private void CompileOnClick(object sender, RoutedEventArgs e)
        {
            var start    = Environment.TickCount;
            var compiler = new Compiler();

            compiler.Compile(Model.Input.Split(new[] { Environment.NewLine }, StringSplitOptions.None));

            var optimizer = new Optimizer();

            optimizer.Optimize(compiler.Instructions);

            var opcodeMap = new OpcodeMap();

            opcodeMap.AssignOpcodesByFrequency(compiler.Instructions);

            Model.ParameterSlots = string.Join(Environment.NewLine,
                                               compilerSettings.ParameterSlots.Select(
                                                   entry => string.Format("{0}{1}", entry.Key.PadRight(30, '.'), entry.Value)));

            var usedOpcodes = new HashSet <byte>(compiler.Instructions.Select(i => opcodeMap.Map[i.Opcode].Value));

            Model.Opcodes =
                opcodeMap.Map.Values.Select(
                    entry =>
                    new OpcodeModel(entry.Name.PadRight(30, '.'), entry.Value,
                                    usedOpcodes.Contains(entry.Value) ? Brushes.Black : Brushes.Silver)).ToList();

            Model.Output = compiler.Instructions.Select(i => WrapInstruction(i, opcodeMap)).ToList();

            Model.FloatConsts = string.Join(Environment.NewLine,
                                            compiler.FloatConsts.Values.Select(v => v.ToString(CultureInfo.InvariantCulture)));

            Model.IntConsts = string.Join(Environment.NewLine,
                                          compiler.IntConsts.Values.Select(v => v.ToString(CultureInfo.InvariantCulture)));

            Model.TickConsts = string.Join(Environment.NewLine,
                                           compiler.TickConsts.Values.Select(v => v.ToString(CultureInfo.InvariantCulture)));

            Model.UnoptimizedBytes = compiler.Instructions.Sum(i => i.ToBytes().Length);

            Model.OptimizedBytes = compiler.Instructions.Where(i => i.Enabled).Sum(i => i.ToBytes().Length);

            var statistics = new Statistics(compiler);

            var optimizedByteFrequencies   = statistics.OptimizedByteFrequencies.ToFrequencies();
            var unoptimizedByteFrequencies = statistics.UnoptimizedByteFrequencies.ToFrequencies();

            var bytecodeStatistics =
                statistics
                .AllBytes
                .Select(b => new StatisticsModel <byte>(b, optimizedByteFrequencies[b], unoptimizedByteFrequencies[b]))
                .ToList();

            Model.BytecodeStatistics = bytecodeStatistics;

            var optimizedOpcodeFrequencies =
                new Frequencies(
                    compiler.Instructions.Where(i => i.Enabled && !(i is Comment))
                    .Select(i => (byte)i.Opcode)
                    .ToArray());

            var opcodeStatistics =
                optimizedOpcodeFrequencies
                .ToFrequencies()
                .Select(entry => new StatisticsModel <string>(((Opcodes)entry.Key).ToString(), entry.Value, 0))
                .OrderBy(m => - m.OptimizedFrequency)
                .ToList();

            Model.OpcodeStatistics = opcodeStatistics;

            var end = Environment.TickCount;

            Model.CompileTime = end - start;
        }