changeset 2:4aed85a1f558

implemented simple vm2
author cin
date Fri, 25 Oct 2013 15:52:16 +0400
parents 033ebe7432d5
children aced2ae9957f
files Jint.Runtime/Jint.Runtime.csproj Jint.Runtime/Main.cs Jint.Runtime/VM2/Box.cs Jint.Runtime/VM2/Instruction.cs Jint.Runtime/VM2/Machine.cs Jint.Runtime/VM2/OpCodes/Codes.cs Jint.Runtime/VM2/RuntimeContext.cs Jint.userprefs
diffstat 8 files changed, 80 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- 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 @@
     <Compile Include="VM2\Box.cs" />
     <Compile Include="VM2\OpCodes\Operation.cs" />
     <Compile Include="VM2\IntegerBinder.cs" />
-    <Compile Include="VM2\Frame.cs" />
     <Compile Include="VM2\Machine.cs" />
+    <Compile Include="VM2\RuntimeContext.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <ProjectExtensions>
--- 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) {
--- 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 ();
--- 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;
--- 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<object[]> m_frames;
-		object[] m_frame;
+		//Stack<object[]> 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];
 		}
 	}
 }
--- 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
 	}
 }
--- /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<Type, Operation[]> m_bindings;
+		public RuntimeContext ()
+		{
+			m_bindings = new Dictionary<Type, Operation[]>();
+			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()]);
+		}
+	}
+}
+
--- 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 @@
 <Properties>
-  <MonoDevelop.Ide.Workspace ActiveConfiguration="Release|x86" />
-  <MonoDevelop.Ide.Workbench ActiveDocument="Jint.Runtime/VM2/Machine.cs">
+  <MonoDevelop.Ide.Workspace ActiveConfiguration="Debug|x86" />
+  <MonoDevelop.Ide.Workbench ActiveDocument="Jint.Runtime/Main.cs">
     <Files>
-      <File FileName="Jint.Runtime/VM/OpCodes/IBinaryOperation2.cs" Line="3" Column="34" />
-      <File FileName="Jint.Runtime/VM2/OpCodes/Codes.cs" Line="12" Column="1" />
-      <File FileName="Jint.Runtime/VM2/Box.cs" Line="14" Column="34" />
-      <File FileName="Jint.Runtime/VM2/IntegerBinder.cs" Line="10" Column="3" />
-      <File FileName="Jint.Runtime/VM2/Instruction.cs" Line="8" Column="19" />
+      <File FileName="Jint.Runtime/VM/OpCodes/IBinaryOperation2.cs" Line="1" Column="1" />
+      <File FileName="Jint.Runtime/VM2/OpCodes/Codes.cs" Line="13" Column="1" />
+      <File FileName="Jint.Runtime/VM2/Box.cs" Line="15" Column="4" />
+      <File FileName="Jint.Runtime/VM2/IntegerBinder.cs" Line="1" Column="1" />
+      <File FileName="Jint.Runtime/VM2/Instruction.cs" Line="1" Column="1" />
       <File FileName="Jint.Runtime/VM2/OpCodes/Operation.cs" Line="4" Column="50" />
-      <File FileName="Jint.Runtime/VM2/Frame.cs" Line="9" Column="4" />
-      <File FileName="Jint.Runtime/VM2/Machine.cs" Line="11" Column="3" />
+      <File FileName="Jint.Runtime/VM2/Machine.cs" Line="33" Column="16" />
+      <File FileName="Jint.Runtime/VM2/RuntimeContext.cs" Line="20" Column="3" />
+      <File FileName="Jint.Runtime/Main.cs" Line="34" Column="30" />
     </Files>
     <Pads>
       <Pad Id="ProjectPad">
@@ -21,8 +22,8 @@
             </Node>
             <Node name="VM2" expanded="True">
               <Node name="OpCodes" expanded="True" />
-              <Node name="Machine.cs" selected="True" />
             </Node>
+            <Node name="Main.cs" selected="True" />
           </Node>
         </State>
       </Pad>
@@ -32,7 +33,9 @@
     </Pads>
   </MonoDevelop.Ide.Workbench>
   <MonoDevelop.Ide.DebuggingService.Breakpoints>
-    <BreakpointStore />
+    <BreakpointStore>
+      <Breakpoint file="/home/sergey/Projects/Jint/Jint.Runtime/VM2/Machine.cs" line="3" column="1" />
+    </BreakpointStore>
   </MonoDevelop.Ide.DebuggingService.Breakpoints>
   <MonoDevelop.Ide.DebuggingService.PinnedWatches />
 </Properties>
\ No newline at end of file