Esempio n. 1
0
        /// <summary>
        /// Optimizes color weights by grouping vertices by color, ordering the groups by count,
        /// and assigning colors in rising order to groups of vertices as ordered,
        /// so that the largest group has the smallest color.
        /// </summary>
        /// <param name="coloring">Coloring to be optimized.</param>
        /// <returns>A new, optimized coloring (new instance).</returns>
        public static GraphColoring OptimizeByWeighting(this GraphColoring coloring)
        {
            var colors = coloring.VertexColors
                         .GroupBy(p => p.Value, p => p.Key)
                         .Select(g => (group: g, count: g.Count()))
                         .OrderByDescending(t => t.count)
                         .SelectMany((t, i) => t.group.Select(v => (vertex: v, color: i + 1)))
                         .ToImmutableSortedDictionary(t => t.vertex, t => t.color);

            return(new GraphColoring(coloring.Graph, colors));
        }
Esempio n. 2
0
        public void Test()
        {
            var instructions = new[]
            {
                new M68kInstruction(M68kOpcode.Move, d2, d12),
                new M68kInstruction(M68kOpcode.Move, d3, d13),
                new M68kInstruction(M68kOpcode.Move, 42, d18),
                new M68kInstruction(M68kOpcode.Move, d18, d0),
                new M68kInstruction(M68kOpcode.Move, d12, d2),
                new M68kInstruction(M68kOpcode.Move, d13, d3),
                new M68kInstruction(M68kOpcode.Rts)
                {
                    FinalRegister1 = M68kRegister.D0
                }
            }.ToList();

            instructions.Insert(0, new M68kInstruction
            {
                Opcode   = M68kOpcode.RegDef,
                DefsUses = Enumerable.Range(0, 8).Select(r => "D" + r).ToList()
            });

            var gc = new GraphColoring(instructions);

            gc.Main();

            Assert.IsTrue(gc.Graph.IsEdgeBetween("D0", "D2"));
            Assert.IsTrue(gc.Graph.IsEdgeBetween("D0", "D3"));
            Assert.IsTrue(gc.Graph.IsEdgeBetween("D0", "D13"));
            Assert.IsTrue(gc.Graph.IsEdgeBetween("D0", "D12"));

            Assert.IsTrue(gc.Graph.IsEdgeBetween("D2", "D3"));
            Assert.IsTrue(gc.Graph.IsEdgeBetween("D2", "D13"));

            Assert.IsTrue(gc.Graph.IsEdgeBetween("D3", "D12"));

            Assert.IsTrue(gc.Graph.IsEdgeBetween("D13", "D12"));
            Assert.IsTrue(gc.Graph.IsEdgeBetween("D13", "D18"));

            Assert.IsTrue(gc.Graph.IsEdgeBetween("D12", "D18"));

            Assert.IsFalse(gc.Graph.IsEdgeBetween("D2", "D12"));
            Assert.IsFalse(gc.Graph.IsEdgeBetween("D3", "D13"));
            Assert.IsFalse(gc.Graph.IsEdgeBetween("D0", "D18"));

            gc.FinalRewrite();

            CodeGenerator.RemoveRedundantMoves(gc.Instructions);

            var code = gc.Instructions;

            Assert.AreEqual(3, gc.Instructions.Count);
        }
Esempio n. 3
0
        public void SelectTest()
        {
            var ig    = new InterferenceGraph();
            var stack = new Stack <string>();

            foreach (var n in "ghkdjefbcm")
            {
                stack.Push(n.ToString());
                ig.Nodes.Add(n.ToString());
            }

            ig.Graph.Add(new Tuple <string, string>("j", "f"));
            ig.Graph.Add(new Tuple <string, string>("j", "e"));
            ig.Graph.Add(new Tuple <string, string>("j", "k"));
            ig.Graph.Add(new Tuple <string, string>("j", "d"));
            ig.Graph.Add(new Tuple <string, string>("j", "h"));
            ig.Graph.Add(new Tuple <string, string>("j", "g"));

            ig.Graph.Add(new Tuple <string, string>("f", "m"));
            ig.Graph.Add(new Tuple <string, string>("f", "e"));

            ig.Graph.Add(new Tuple <string, string>("e", "b"));
            ig.Graph.Add(new Tuple <string, string>("e", "m"));

            ig.Graph.Add(new Tuple <string, string>("k", "b"));
            ig.Graph.Add(new Tuple <string, string>("k", "d"));
            ig.Graph.Add(new Tuple <string, string>("k", "g"));

            ig.Graph.Add(new Tuple <string, string>("b", "c"));
            ig.Graph.Add(new Tuple <string, string>("b", "m"));
            ig.Graph.Add(new Tuple <string, string>("b", "d"));

            ig.Graph.Add(new Tuple <string, string>("m", "c"));
            ig.Graph.Add(new Tuple <string, string>("m", "d"));

            ig.Graph.Add(new Tuple <string, string>("h", "g"));

            var newgraph = GraphColoring.Select(ig, stack);

            Assert.AreEqual(4, newgraph.Allocation.Count);
        }
Esempio n. 4
0
        public void Test2()
        {
            var instructions = new[]
            {
                new M68kInstruction(M68kOpcode.Sub)
                {
                    AddressingMode1 = M68kAddressingMode.Immediate,
                    Immediate       = 100,
                    FinalRegister2  = M68kRegister.SP,
                    AddressingMode2 = M68kAddressingMode.Register,
                },

                new M68kInstruction(M68kOpcode.Move, d2, d32),
                new M68kInstruction(M68kOpcode.Move, d3, d33),
                new M68kInstruction(M68kOpcode.Move, d4, d34),
                new M68kInstruction(M68kOpcode.Move, d5, d35),
                new M68kInstruction(M68kOpcode.Move, d6, d36),
                new M68kInstruction(M68kOpcode.Move, d7, d37),

                //new M68kInstruction(M68kOpcode.RegDef) { DefsUses = new List<string> { "D15"} },
                new M68kInstruction(M68kOpcode.Move, 2, d15),
                new M68kInstruction(M68kOpcode.Move, 3, d16),
                new M68kInstruction(M68kOpcode.Move, 4, d17),
                new M68kInstruction(M68kOpcode.Move, 5, d18),

                new M68kInstruction(M68kOpcode.Move, d15, d20),
                new M68kInstruction(M68kOpcode.Add, d16, d20),
                new M68kInstruction(M68kOpcode.Add, d17, d20),
                new M68kInstruction(M68kOpcode.Add, d18, d20),
                new M68kInstruction(M68kOpcode.Move, d20, d0),

                new M68kInstruction(M68kOpcode.Move, d32, d2),
                new M68kInstruction(M68kOpcode.Move, d33, d3),
                new M68kInstruction(M68kOpcode.Move, d34, d4),
                new M68kInstruction(M68kOpcode.Move, d35, d5),
                new M68kInstruction(M68kOpcode.Move, d36, d6),
                new M68kInstruction(M68kOpcode.Move, d37, d7),

                new M68kInstruction(M68kOpcode.Add)
                {
                    AddressingMode1 = M68kAddressingMode.Immediate,
                    Immediate       = 100,
                    FinalRegister2  = M68kRegister.SP,
                    AddressingMode2 = M68kAddressingMode.Register,
                },
                new M68kInstruction(M68kOpcode.Rts)
                {
                    FinalRegister1 = M68kRegister.D0
                }
            }.ToList();

            instructions.Insert(0, new M68kInstruction
            {
                Opcode   = M68kOpcode.RegDef,
                DefsUses = Enumerable.Range(0, 8).Select(r => "D" + r).ToList()
            });

            var gc = new GraphColoring(instructions);

            gc.Main();

            gc.FinalRewrite();

            CodeGenerator.RemoveRedundantMoves(gc.Instructions);
            CodeGenerator.RemoveInstructions(gc.Instructions, M68kOpcode.RegDef);

            var emul = new Emulator(gc.Instructions, new Dictionary <string, Declaration>(), null);

            emul.RunFunction(null);

            Assert.AreEqual(14, emul.Regs[0]);
        }