public void TestInstructions()
        {
            VirtualMachine     virtualMachine = new VirtualMachine();
            PartialJITCompiler jitCompiler    = new PartialJITCompiler(virtualMachine);

            //Sub
            virtualMachine.SetRegisterValue(Registers.R1, 12);
            virtualMachine.SetRegisterValue(Registers.R2, 5);

            Instruction[] methodBody = new Instruction[]
            {
                new RFormatInstruction(OperationCodes.Sub.Code(), OperationXCodes.Sub, 1, 2, 3).AsInstruction()
            };

            var jittedMethod = jitCompiler.GenerateMethod("", methodBody);

            jitCompiler.InvokeJitedMethod(jittedMethod);
            Assert.AreEqual(virtualMachine.GetRegisterValue(Registers.R3), 7);
        }
        public void TestFunction()
        {
            VirtualMachine     virtualMachine = new VirtualMachine();
            PartialJITCompiler jitCompiler    = new PartialJITCompiler(virtualMachine);

            virtualMachine.SetRegisterValue(Registers.R1, 10);
            virtualMachine.SetRegisterValue(Registers.RA, 1337);

            Instruction[] methodBody = new Instruction[]
            {
                new IFormatInstruction(OperationCodes.Addi.Code(), 1, 1, -1).AsInstruction(),
                new RFormatInstruction(OperationCodes.Ret.Code(), OperationXCodes.Ret, 0, 0, 0).AsInstruction()
            };

            var jittedMethod = jitCompiler.GenerateMethod("", methodBody, true);

            jitCompiler.InvokeJitedMethod(jittedMethod);
            Assert.AreEqual(9, virtualMachine.GetRegisterValue(Registers.R1));
            Assert.AreEqual((uint)1337, virtualMachine.ProgramCounter);
        }
        public void TestBenchmark()
        {
            VirtualMachine     virtualMachine  = new VirtualMachine();
            PartialJITCompiler jitCompiler     = new PartialJITCompiler(virtualMachine);
            FullJITCompiler    fullJitCompiler = new FullJITCompiler(virtualMachine);

            int count = 2000;

            Instruction[] methodBody = new Instruction[]
            {
                new IFormatInstruction(OperationCodes.Addi.Code(), 1, 1, -1).AsInstruction(),
                new IFormatInstruction(OperationCodes.Addi.Code(), 2, 2, 1).AsInstruction(),
                new IFormatInstruction(OperationCodes.Bne.Code(), 0, 1, -3 * 4).AsInstruction()
            };

            var jittedMethod = jitCompiler.GenerateMethod("", methodBody);

            TimeSpan jitTime = new TimeSpan();
            TimeSpan vmTime  = new TimeSpan();

            //Partial jitter
            virtualMachine.SetRegisterValue(Registers.R1, count);
            Stopwatch timer = Stopwatch.StartNew();

            jitCompiler.InvokeJitedMethod(jittedMethod);
            timer.Stop();

            jitTime = timer.Elapsed;

            //VM
            virtualMachine.SetRegisterValue(Registers.R1, count);

            timer = Stopwatch.StartNew();
            for (int i = 0; i < methodBody.Length; i++)
            {
                virtualMachine.ExecuteInstruction(methodBody[i].Data);
            }
            timer.Stop();

            vmTime = timer.Elapsed;
        }