diff Jint.Runtime/Main.cs @ 3:aced2ae9957f

temp commit, new virtual machine concept (strongly typed version of VM2).
author cin
date Sun, 27 Oct 2013 17:23:25 +0400
parents 4aed85a1f558
children cb13da6e3349
line wrap: on
line diff
--- a/Jint.Runtime/Main.cs	Fri Oct 25 15:52:16 2013 +0400
+++ b/Jint.Runtime/Main.cs	Sun Oct 27 17:23:25 2013 +0400
@@ -1,63 +1,66 @@
 using System;
-using Jint.Runtime.VM.OpCodes;
+using Jint.Runtime.VM;
 
 namespace Jint.Runtime
 {
     using VM;
-    using System.IO;
+    using System.IO;
+    using Jint.Runtime.VM.OpCodes;
     class MainClass
     {
         public static void Main(string[] args)
-        {
-			var runtime = new RuntimeContext ();
-
-			var frame = new Frame (3,runtime);
-
-			frame.Set (0, 0);
-			frame.Set (1, 1);
-
-			var op = new Add (0,1,0);
+        {
+            RuntimeContext runtime = new RuntimeContext();
+            runtime.DefineBinaryOperation<int>(Codes.Add, (x, y) => x + y);
+
+            var frame = new Frame(4,runtime);
+            frame.SetValue(2,0);
+            frame.SetValue(3,1);
+
+            var op = new BinaryOp(Codes.Add, 2, 3, 2);
 
 			var t = Environment.TickCount;
 
-			for(int i=0; i < 10000000; i++)
-				op.Invoke (frame);
+            /*
+             *          mov r1, 10 000 000
+             *          mov r2, 0
+             *          mov r3, 1
+             *          loop
+             *              condition:
+             *                  gte r2, r1
+             *              body:
+             *                  add r2,r3,r2
+             */
+
+            for (int i = 0; i < 10000000; i++)
+                op.Invoke(frame);
 
-			var res = frame.Get<int> (0);
-
-			Console.WriteLine ("got: {0}, int {1} ms", res, Environment.TickCount - t );
+			Console.WriteLine ("vm: {0}, int {1} ms", frame.GetValue<int>(2), Environment.TickCount - t );
+
+            t = Environment.TickCount;
+
+            /*
+             *          mov r1, 10 000 000
+             *          mov r2, 0
+             *          mov r3, 1
+             *          loop
+             *              condition:
+             *                  gte r2, r1
+             *              body:
+             *                  add r2,r3,r2
+             */
+            object count = 0, inc = 1;
+            
+            for (int i = 0; i < 10000000; i++)
+                count = Add(count,inc);
+
+            Console.WriteLine("native: {0}, int {1} ms", frame.GetValue<int>(2), Environment.TickCount - t);
 
 			t = Environment.TickCount;
-
-			object count = 0, inc = 1;
-			for (int i=0; i< 10000000; i++)
-				count = OpAdd(count,inc);
-
-			Console.WriteLine ("reference results: {0}, int {1} ms", count, Environment.TickCount - t );
-
-			var code = new VM2.Instruction[10000000];
-
-			for (int i = 0; i < code.Length; i++)
-				code[i] = new VM2.Instruction(){ code = VM2.OpCodes.Codes.Add, dest = 0, args = new int[] {0,1} };
-			var machine = new VM2.Machine ();
-
-			t = Environment.TickCount;
-
-			machine.InitFrame (new object[] { 0, 1 });
-			machine.Execute (code);
-
-			Console.WriteLine ("vm2: {0}, int {1} ms", machine.Get(0), Environment.TickCount - t );
+        }
+
+        public static object Add(object arg1, object arg2) {
+            return (int)arg1 + (int)arg2;
         }
-
-		public static object OpAdd(object arg1, object arg2) {
-			if (arg1.GetType () == arg2.GetType ()) {
-				return OpAddIntegers(arg1,arg2);
-			}
-			throw new Exception ();
-		}
-
-		public static object OpAddIntegers(object arg1, object arg2) {
-			return (int)arg1 + (int)arg2;
-		}
     }
 }