# HG changeset patch # User cin # Date 1382701936 -14400 # Node ID 4aed85a1f558da528b5d4939e534a60495297e2e # Parent 033ebe7432d5dfb0a8eae9e22b0551e77b846111 implemented simple vm2 diff -r 033ebe7432d5 -r 4aed85a1f558 Jint.Runtime/Jint.Runtime.csproj --- a/Jint.Runtime/Jint.Runtime.csproj Thu Oct 24 19:45:57 2013 +0400 +++ b/Jint.Runtime/Jint.Runtime.csproj Fri Oct 25 15:52:16 2013 +0400 @@ -55,8 +55,8 @@ - + diff -r 033ebe7432d5 -r 4aed85a1f558 Jint.Runtime/Main.cs --- a/Jint.Runtime/Main.cs Thu Oct 24 19:45:57 2013 +0400 +++ b/Jint.Runtime/Main.cs Fri Oct 25 15:52:16 2013 +0400 @@ -34,6 +34,19 @@ 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 OpAdd(object arg1, object arg2) { diff -r 033ebe7432d5 -r 4aed85a1f558 Jint.Runtime/VM2/Box.cs --- a/Jint.Runtime/VM2/Box.cs Thu Oct 24 19:45:57 2013 +0400 +++ b/Jint.Runtime/VM2/Box.cs Fri Oct 25 15:52:16 2013 +0400 @@ -9,6 +9,11 @@ public Operation[] impl; public object value; + public Box(object boxValue,Operation[] opImpl) { + value = boxValue; + impl = opImpl; + } + public Type HoldingType { get { return value == null ? null : value.GetType (); diff -r 033ebe7432d5 -r 4aed85a1f558 Jint.Runtime/VM2/Instruction.cs --- a/Jint.Runtime/VM2/Instruction.cs Thu Oct 24 19:45:57 2013 +0400 +++ b/Jint.Runtime/VM2/Instruction.cs Fri Oct 25 15:52:16 2013 +0400 @@ -2,6 +2,7 @@ namespace Jint.Runtime.VM2 { + using OpCodes; public struct Instruction { public Codes code; diff -r 033ebe7432d5 -r 4aed85a1f558 Jint.Runtime/VM2/Machine.cs --- a/Jint.Runtime/VM2/Machine.cs Thu Oct 24 19:45:57 2013 +0400 +++ b/Jint.Runtime/VM2/Machine.cs Fri Oct 25 15:52:16 2013 +0400 @@ -6,24 +6,36 @@ public class Machine { RuntimeContext m_context; - Stack m_frames; - object[] m_frame; + //Stack m_frames; + Box[] m_frame; public Machine () { + m_context = new RuntimeContext (); + } + + public void InitFrame(object[] values) { + m_frame = new Box[values.Length]; + for(int i = 0; i< values.Length; i++) + m_frame[i] = m_context.PackValue(values[i]); } public void Execute(Instruction[] instructions) { foreach (var op in instructions) { - + m_frame [op.dest].value = m_frame [op.args [0]].impl [(int)op.code](MakeArgs(op.args)); } } private object[] MakeArgs(int[] regs) { object[] args = new object[regs.Length]; for (int i=0; i< regs.Length; i++) - args = m_frame [regs [i]]; + args[i] = m_frame [regs [i]].value; + return args; + } + + public object Get(int index) { + return m_frame [index]; } } } diff -r 033ebe7432d5 -r 4aed85a1f558 Jint.Runtime/VM2/OpCodes/Codes.cs --- a/Jint.Runtime/VM2/OpCodes/Codes.cs Thu Oct 24 19:45:57 2013 +0400 +++ b/Jint.Runtime/VM2/OpCodes/Codes.cs Fri Oct 25 15:52:16 2013 +0400 @@ -1,10 +1,11 @@ using System; -namespace Jint.Runtime +namespace Jint.Runtime.VM2.OpCodes { public enum Codes: int { - Add = 1, + Noop = 0, + Add, MaxCode } } diff -r 033ebe7432d5 -r 4aed85a1f558 Jint.Runtime/VM2/RuntimeContext.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Jint.Runtime/VM2/RuntimeContext.cs Fri Oct 25 15:52:16 2013 +0400 @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; + +namespace Jint.Runtime.VM2 +{ + using OpCodes; + + public class RuntimeContext + { + Dictionary m_bindings; + public RuntimeContext () + { + m_bindings = new Dictionary(); + m_bindings[typeof(int)] = new Operation[] { + (args) => {return null;}, + (args) => { + return (int)args[0]+(int)args[1]; + } + }; + } + + public Box PackValue(object value) { + return new Box (value,m_bindings[value.GetType()]); + } + } +} + diff -r 033ebe7432d5 -r 4aed85a1f558 Jint.userprefs --- a/Jint.userprefs Thu Oct 24 19:45:57 2013 +0400 +++ b/Jint.userprefs Fri Oct 25 15:52:16 2013 +0400 @@ -1,15 +1,16 @@  - - + + - - - - - + + + + + - - + + + @@ -21,8 +22,8 @@ - + @@ -32,7 +33,9 @@ - + + + \ No newline at end of file