public void Test_Ret()
        {
            var node = new RetNode();

            var map          = TileFactoryTestUtils.createMapping();
            var instructions = TileFactory.Stack.Ret().Cover(node);

            TileFactoryTestUtils.updateMapping(instructions, map);

            var          got      = TileFactoryTestUtils.getASM(instructions, map);
            const string expected = "ret\n";

            Assert.AreEqual(expected, got);
        }
        public void Test_Const()
        {
            var node = new ConstantNode <long> (123L);

            var map          = TileFactoryTestUtils.createMapping();
            var instructions = TileFactory.ConstTile <long>().Cover(node);

            TileFactoryTestUtils.updateMapping(instructions, map);

            var got      = TileFactoryTestUtils.getASM(instructions, map);
            var expected =
                "mov " + TileFactoryTestUtils.SPECIAL + ", 123\n";

            Assert.AreEqual(expected, got);
        }
Beispiel #3
0
        public void Test_Mov_Same()
        {
            var instructions = new[] { InstructionFactory.Move(reg1, reg2) };

            var map = TileFactoryTestUtils.createMapping();

            map.Add(reg1, TileFactoryTestUtils.RAX);
            map.Add(reg2, TileFactoryTestUtils.RAX);
            TileFactoryTestUtils.updateMapping(instructions, map);

            var          got      = TileFactoryTestUtils.getASM(instructions, map);
            const string expected = "";

            Assert.AreEqual(expected, got);
        }
        public void Test_Call()
        {
            var fun  = new Function("myFun", new[] { false });
            var node = new FunctionCallNode(fun);

            var map          = TileFactoryTestUtils.createMapping();
            var instructions = TileFactory.Stack.Call().Cover(node);

            TileFactoryTestUtils.updateMapping(instructions, map);

            var got      = TileFactoryTestUtils.getASM(instructions, map);
            var expected =
                "call " + node.Function.Label + "\n";

            Assert.AreEqual(expected, got);
        }
        public void TestUnconditionalJump()
        {
            var lbl = new LabelNode("target");
            var jmp = new UnconditionalJumpToLabelNode(lbl);

            var tile = TileFactory.Jump.Unconditional_Label();
            var map  = TileFactoryTestUtils.createMapping();

            var instructions = tile.Cover(jmp);

            TileFactoryTestUtils.updateMapping(instructions, map);

            var got      = TileFactoryTestUtils.getASM(instructions, map);
            var expected = "jmp " + lbl.Label + "\n";

            Assert.AreEqual(expected, got);
        }
        public void Test_MemAccessConst()
        {
            var val = new ConstantNode <long> (123L);
            var mem = new MemoryNode(val);

            var map = TileFactoryTestUtils.createMapping();

            var instructions = TileFactory.MemAccessTile <long>().Cover(mem);

            TileFactoryTestUtils.updateMapping(instructions, map);

            var got      = TileFactoryTestUtils.getASM(instructions, map);
            var expected =
                "mov " + TileFactoryTestUtils.SPECIAL + ", [123]\n";

            Assert.AreEqual(expected, got);
        }
        public void Test_Pop()
        {
            var reg  = new TemporaryNode();
            var node = new PopNode(reg);

            var map          = TileFactoryTestUtils.createMapping();
            var instructions = TileFactory.Stack.Pop().Cover(node);

            map.Add(reg, TileFactoryTestUtils.RAX);
            TileFactoryTestUtils.updateMapping(instructions, map);

            var got      = TileFactoryTestUtils.getASM(instructions, map);
            var expected =
                "pop " + TileFactoryTestUtils.RAX + "\n";

            Assert.AreEqual(expected, got);
        }
        static void testRegConst(long value, string expected)
        {
            var dst    = new TemporaryNode();
            var src    = new ConstantNode <long> (value);
            var assign = new AssignmentNode(dst, src);

            var map = TileFactoryTestUtils.createMapping();

            map.Add(dst, TileFactoryTestUtils.RAX);

            var instructions = TileFactory.Assign.Reg_Const().Cover(assign);

            TileFactoryTestUtils.updateMapping(instructions, map);

            var got = TileFactoryTestUtils.getASM(instructions, map);

            Assert.AreEqual(expected, got);
        }
        public void Test_Plus()
        {
            var l     = new TemporaryNode();
            var unary = new UnaryPlusOperatorNode(l);

            var map = TileFactoryTestUtils.createMapping();

            map.Add(l, TileFactoryTestUtils.RAX);

            var instructions = TileFactory.Unop.Plus_Reg().Cover(unary);

            TileFactoryTestUtils.updateMapping(instructions, map);

            var got      = TileFactoryTestUtils.getASM(instructions, map);
            var expected =
                "mov " + TileFactoryTestUtils.SPECIAL + ", " + TileFactoryTestUtils.RAX + "\n";

            Assert.AreEqual(expected, got);
        }
        public void Test_MemAccess()
        {
            var reg = new TemporaryNode();
            var mem = new MemoryNode(reg);

            var map = TileFactoryTestUtils.createMapping();

            map.Add(reg, TileFactoryTestUtils.RAX);

            var instructions = TileFactory.MemAccessTile().Cover(mem);

            TileFactoryTestUtils.updateMapping(instructions, map);

            var got      = TileFactoryTestUtils.getASM(instructions, map);
            var expected =
                "mov " + TileFactoryTestUtils.SPECIAL + ", [" + TileFactoryTestUtils.RAX + "]\n";

            Assert.AreEqual(expected, got);
        }
        public void Test_MemConst_Const()
        {
            var dst    = new ConstantNode <long> (1111L);
            var src    = new ConstantNode <long> (123L);
            var assign = new AssignmentNode(new MemoryNode(dst), src);

            var map = TileFactoryTestUtils.createMapping();

            var instructions = TileFactory.Assign.MemConst_Const().Cover(assign);

            TileFactoryTestUtils.updateMapping(instructions, map);

            var got      = TileFactoryTestUtils.getASM(instructions, map);
            var expected =
                "mov [1111], 123\n" +
                "mov " + TileFactoryTestUtils.SPECIAL + ", 123\n";

            Assert.AreEqual(expected, got);
        }
        public void Test_LogNot()
        {
            var l     = new TemporaryNode();
            var unary = new LogNotOperatorNode(l);

            var map = TileFactoryTestUtils.createMapping();

            map.Add(l, TileFactoryTestUtils.RAX);

            var instructions = TileFactory.Unop.LogNot_Reg().Cover(unary);

            TileFactoryTestUtils.updateMapping(instructions, map);

            var got      = TileFactoryTestUtils.getASM(instructions, map);
            var expected =
                "cmp " + TileFactoryTestUtils.RAX + ", 0\n" +
                "sete " + TileFactoryTestUtils.SPECIAL + "\n";

            Assert.AreEqual(expected, got);
        }
Beispiel #13
0
        public void Test_LogAnd_ConstReg_False()
        {
            var l     = new ConstantNode <long> (0L);
            var r     = new TemporaryNode();
            var binop = new LogAndOperatorNode(l, r);

            var map = TileFactoryTestUtils.createMapping();

            map.Add(r, TileFactoryTestUtils.R9);

            var instructions = TileFactory.LogAnd.ConstReg().Cover(binop);

            TileFactoryTestUtils.updateMapping(instructions, map);

            var got      = TileFactoryTestUtils.getASM(instructions, map);
            var expected =
                "xor " + TileFactoryTestUtils.SPECIAL + ", " + TileFactoryTestUtils.SPECIAL + "\n";

            Assert.AreEqual(expected, got);
        }
        public void Test_Label()
        {
            var node = new LabelNode("myLabel");

            var map          = TileFactoryTestUtils.createMapping();
            var instructions = TileFactory.LabelTile().Cover(node);

            TileFactoryTestUtils.updateMapping(instructions, map);

            var insn = instructions.ToArray() [0];

            Assert.IsFalse(insn.IsCopyInstruction);
            Assert.IsFalse(insn.IsJumpInstruction);
            Assert.IsTrue(insn.IsLabel);
            Assert.AreEqual(node.Label, insn.Label);

            var got      = TileFactoryTestUtils.getASM(instructions, map);
            var expected = node.Label + ":\n";

            Assert.AreEqual(expected, got);
        }
Beispiel #15
0
        static void testBinopConstReg(
            Func <ConstantNode <long>, RegisterNode, BinaryOperatorNode> condMaker, Tile tile, string mnemonik)
        {
            var l     = new ConstantNode <long> (4L);
            var r     = new TemporaryNode();
            var binop = condMaker(l, r);

            var map = TileFactoryTestUtils.createMapping();

            map.Add(r, TileFactoryTestUtils.RAX);

            var instructions = tile.Cover(binop);

            TileFactoryTestUtils.updateMapping(instructions, map);

            var got      = TileFactoryTestUtils.getASM(instructions, map);
            var expected =
                "mov " + TileFactoryTestUtils.SPECIAL + ", 4\n" +
                mnemonik + " " + TileFactoryTestUtils.SPECIAL + ", " + TileFactoryTestUtils.RAX + "\n";

            Assert.AreEqual(expected, got);
        }
        static void testCompareConstReg(
            Func <ConstantNode <long>, RegisterNode, BinaryOperatorNode> condMaker, Tile tile, string cond_type)
        {
            var l   = new ConstantNode <long> (15L);
            var r   = new TemporaryNode();
            var jmp = condMaker(l, r);

            var map = TileFactoryTestUtils.createMapping();

            map.Add(r, TileFactoryTestUtils.RAX);

            var instructions = tile.Cover(jmp);

            TileFactoryTestUtils.updateMapping(instructions, map);

            var got      = TileFactoryTestUtils.getASM(instructions, map);
            var expected =
                "cmp 15, " + TileFactoryTestUtils.RAX + "\n" +
                "set" + cond_type + " " + TileFactoryTestUtils.SPECIAL + "\n";

            Assert.AreEqual(expected, got);
        }
        static void testCondRegConstJump(
            Func <RegisterNode, ConstantNode <long>, BinaryOperatorNode> condMaker, Tile tile, string cond_type)
        {
            var l   = new TemporaryNode();
            var r   = new ConstantNode <long> (15L);
            var lbl = new LabelNode("target");
            var jmp = new ConditionalJumpToLabelNode(condMaker(l, r), lbl);

            var map = TileFactoryTestUtils.createMapping();

            map.Add(l, TileFactoryTestUtils.RAX);

            var instructions = tile.Cover(jmp);

            TileFactoryTestUtils.updateMapping(instructions, map);

            var got      = TileFactoryTestUtils.getASM(instructions, map);
            var expected = "cmp " + TileFactoryTestUtils.RAX + ", 15\n" +
                           "j" + cond_type + " " + lbl.Label + "\n";

            Assert.AreEqual(expected, got);
        }
        public void Test_MemReg_Reg()
        {
            var dst    = new TemporaryNode();
            var src    = new TemporaryNode();
            var assign = new AssignmentNode(new MemoryNode(dst), src);

            var map = TileFactoryTestUtils.createMapping();

            map.Add(dst, TileFactoryTestUtils.RAX);
            map.Add(src, TileFactoryTestUtils.RBX);

            var instructions = TileFactory.Assign.MemReg_Reg().Cover(assign);

            TileFactoryTestUtils.updateMapping(instructions, map);

            var got      = TileFactoryTestUtils.getASM(instructions, map);
            var expected =
                "mov [" + TileFactoryTestUtils.RAX + "], " + TileFactoryTestUtils.RBX + "\n" +
                "mov " + TileFactoryTestUtils.SPECIAL + ", " + TileFactoryTestUtils.RBX + "\n";

            Assert.AreEqual(expected, got);
        }
Beispiel #19
0
        public void Test_Mul_ConstReg()
        {
            var l     = new ConstantNode <long> (13L);
            var r     = new TemporaryNode();
            var binop = new MulOperatorNode(l, r);

            var map = TileFactoryTestUtils.createMapping();

            map.Add(r, TileFactoryTestUtils.R10);

            var instructions = TileFactory.Mul.ConstReg <long>().Cover(binop);

            TileFactoryTestUtils.updateMapping(instructions, map);

            var got      = TileFactoryTestUtils.getASM(instructions, map);
            var expected =
                "mov " + Target.RAX + ", 13\n" +
                "mul " + TileFactoryTestUtils.R10 + "\n" +
                "mov " + TileFactoryTestUtils.SPECIAL + ", " + Target.RAX + "\n";

            Assert.AreEqual(expected, got);
        }
        static void generalTest(
            Func <RegisterNode, RegisterNode, RegisterNode> target,
            Func <RegisterNode, ConstantNode <long>, BinaryOperatorNode> condMaker,
            Tile tile, long value, string expected)
        {
            var dst    = new TemporaryNode();
            var src    = new TemporaryNode();
            var cons   = new ConstantNode <long> (value);
            var oper   = condMaker(src, cons);
            var assign = new AssignmentNode(target(src, dst), oper);

            var map = TileFactoryTestUtils.createMapping();

            map.Add(dst, TileFactoryTestUtils.RAX);
            map.Add(src, TileFactoryTestUtils.RBX);

            var instructions = tile.Cover(assign);

            TileFactoryTestUtils.updateMapping(instructions, map);

            var got = TileFactoryTestUtils.getASM(instructions, map);

            Assert.AreEqual(expected, got);
        }
Beispiel #21
0
        public void Test_LogAnd_RegReg()
        {
            var l     = new TemporaryNode();
            var r     = new TemporaryNode();
            var binop = new LogAndOperatorNode(l, r);

            var map = TileFactoryTestUtils.createMapping();

            map.Add(l, TileFactoryTestUtils.R9);
            map.Add(r, TileFactoryTestUtils.R10);

            var instructions = TileFactory.LogAnd.RegReg().Cover(binop);

            TileFactoryTestUtils.updateMapping(instructions, map);

            var got      = TileFactoryTestUtils.getASM(instructions, map);
            var expected =
                "mov " + TileFactoryTestUtils.SPECIAL + ", " + TileFactoryTestUtils.R9 + "\n" +
                "xor " + TileFactoryTestUtils.R9 + ", " + TileFactoryTestUtils.R9 + "\n" +
                "cmp " + TileFactoryTestUtils.R10 + ", " + TileFactoryTestUtils.R9 + "\n" +
                "cmove " + TileFactoryTestUtils.SPECIAL + ", " + TileFactoryTestUtils.R9 + "\n";

            Assert.AreEqual(expected, got);
        }
Beispiel #22
0
        public void Test_Mod_RegConst()
        {
            var l     = new TemporaryNode();
            var r     = new ConstantNode <long> (13L);
            var binop = new ModOperatorNode(l, r);

            var map = TileFactoryTestUtils.createMapping();

            map.Add(l, TileFactoryTestUtils.R9);

            var instructions = TileFactory.Mod.RegConst <long> ().Cover(binop);

            TileFactoryTestUtils.updateMapping(instructions, map);

            var got      = TileFactoryTestUtils.getASM(instructions, map);
            var expected =
                "xor " + Target.RDX + ", " + Target.RDX + "\n" +
                "mov " + Target.RAX + ", " + TileFactoryTestUtils.R9 + "\n" +
                "mov " + TileFactoryTestUtils.SPECIAL + ", 13\n" +
                "div " + TileFactoryTestUtils.SPECIAL + "\n" +
                "mov " + TileFactoryTestUtils.SPECIAL + ", " + Target.RDX + "\n";

            Assert.AreEqual(expected, got);
        }