annotate Jint.Runtime/Main.cs @ 2:4aed85a1f558

implemented simple vm2
author cin
date Fri, 25 Oct 2013 15:52:16 +0400
parents e113095f1de0
children aced2ae9957f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
1 using System;
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
2 using Jint.Runtime.VM.OpCodes;
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
3
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
4 namespace Jint.Runtime
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
5 {
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
6 using VM;
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
7 using System.IO;
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
8 class MainClass
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
9 {
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
10 public static void Main(string[] args)
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
11 {
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
12 var runtime = new RuntimeContext ();
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
13
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
14 var frame = new Frame (3,runtime);
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
15
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
16 frame.Set (0, 0);
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
17 frame.Set (1, 1);
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
18
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
19 var op = new Add (0,1,0);
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
20
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
21 var t = Environment.TickCount;
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
22
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
23 for(int i=0; i < 10000000; i++)
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
24 op.Invoke (frame);
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
25
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
26 var res = frame.Get<int> (0);
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
27
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
28 Console.WriteLine ("got: {0}, int {1} ms", res, Environment.TickCount - t );
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
29
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
30 t = Environment.TickCount;
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
31
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
32 object count = 0, inc = 1;
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
33 for (int i=0; i< 10000000; i++)
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
34 count = OpAdd(count,inc);
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
35
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
36 Console.WriteLine ("reference results: {0}, int {1} ms", count, Environment.TickCount - t );
2
4aed85a1f558 implemented simple vm2
cin
parents: 0
diff changeset
37
4aed85a1f558 implemented simple vm2
cin
parents: 0
diff changeset
38 var code = new VM2.Instruction[10000000];
4aed85a1f558 implemented simple vm2
cin
parents: 0
diff changeset
39
4aed85a1f558 implemented simple vm2
cin
parents: 0
diff changeset
40 for (int i = 0; i < code.Length; i++)
4aed85a1f558 implemented simple vm2
cin
parents: 0
diff changeset
41 code[i] = new VM2.Instruction(){ code = VM2.OpCodes.Codes.Add, dest = 0, args = new int[] {0,1} };
4aed85a1f558 implemented simple vm2
cin
parents: 0
diff changeset
42 var machine = new VM2.Machine ();
4aed85a1f558 implemented simple vm2
cin
parents: 0
diff changeset
43
4aed85a1f558 implemented simple vm2
cin
parents: 0
diff changeset
44 t = Environment.TickCount;
4aed85a1f558 implemented simple vm2
cin
parents: 0
diff changeset
45
4aed85a1f558 implemented simple vm2
cin
parents: 0
diff changeset
46 machine.InitFrame (new object[] { 0, 1 });
4aed85a1f558 implemented simple vm2
cin
parents: 0
diff changeset
47 machine.Execute (code);
4aed85a1f558 implemented simple vm2
cin
parents: 0
diff changeset
48
4aed85a1f558 implemented simple vm2
cin
parents: 0
diff changeset
49 Console.WriteLine ("vm2: {0}, int {1} ms", machine.Get(0), Environment.TickCount - t );
0
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
50 }
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
51
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
52 public static object OpAdd(object arg1, object arg2) {
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
53 if (arg1.GetType () == arg2.GetType ()) {
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
54 return OpAddIntegers(arg1,arg2);
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
55 }
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
56 throw new Exception ();
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
57 }
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
58
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
59 public static object OpAddIntegers(object arg1, object arg2) {
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
60 return (int)arg1 + (int)arg2;
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
61 }
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
62 }
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
63 }