Skip to content

Commit fa89e79

Browse files
committed
added arrays support
Added opcodes Newarr Setarr Getarr Nop
1 parent c3da007 commit fa89e79

File tree

5 files changed

+108
-24
lines changed

5 files changed

+108
-24
lines changed

VM/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Reflection;
66
using System.Runtime.InteropServices;
77
using System.Text;
8-
using System.Windows.Forms;
98

109
namespace VM
1110
{
@@ -27,6 +26,7 @@ static void Main(string[] args)
2726
}
2827
Console.WriteLine("---STACK END---");
2928
Console.ReadLine();
29+
Console.WriteLine("---STACK END---");
3030
}
3131
}
3232
}

VM/VM.csproj

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
1212
<FileAlignment>512</FileAlignment>
1313
<Deterministic>true</Deterministic>
14+
<TargetFrameworkProfile />
1415
</PropertyGroup>
1516
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1617
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -22,6 +23,7 @@
2223
<ErrorReport>prompt</ErrorReport>
2324
<WarningLevel>4</WarningLevel>
2425
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
26+
<Prefer32Bit>false</Prefer32Bit>
2527
</PropertyGroup>
2628
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
2729
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -31,16 +33,12 @@
3133
<DefineConstants>TRACE</DefineConstants>
3234
<ErrorReport>prompt</ErrorReport>
3335
<WarningLevel>4</WarningLevel>
36+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
37+
<Prefer32Bit>false</Prefer32Bit>
3438
</PropertyGroup>
3539
<ItemGroup>
3640
<Reference Include="System" />
37-
<Reference Include="System.Core" />
38-
<Reference Include="System.Windows.Forms" />
39-
<Reference Include="System.Xml.Linq" />
40-
<Reference Include="System.Data.DataSetExtensions" />
4141
<Reference Include="Microsoft.CSharp" />
42-
<Reference Include="System.Data" />
43-
<Reference Include="System.Xml" />
4442
</ItemGroup>
4543
<ItemGroup>
4644
<Compile Include="Program.cs" />

VM/VirtualMachine.cs

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ namespace VM
88
{
99
internal unsafe class VirtualMachine
1010
{
11-
public object[] Stack = new object[0];
11+
public dynamic[] Stack = new dynamic[0];
1212
public IntPtr MemoryPointer { get; set; }
1313
public int ImageSize { get; set; }
1414
public int CodeSectionOffset { get; set; }
1515
public int TextSectionOffset { get; set; }
1616
public int CSharpTypesSectionOffset { get; set; }
1717
public int ClearSectionOffset { get; set; }
18-
public object[] GlobalVariables = new object[512];
18+
public dynamic[] GlobalVariables = new dynamic[512];
1919

2020
public VirtualMachine(byte[] b)
2121
{
@@ -30,26 +30,26 @@ public VirtualMachine(byte[] b)
3030
MemoryPointer = IntPtr.Add(MemoryPointer, 24);
3131
}
3232

33-
private object Pop()
33+
private dynamic Pop()
3434
{
3535
if (Stack.Length > 0)
3636
{
37-
object rez = Stack[Stack.Length - 1];
37+
dynamic rez = Stack[Stack.Length - 1];
3838
Array.Resize(ref Stack, Stack.Length - 1);
3939
return rez;
4040
}
4141
return null;
4242
}
4343

44-
public void Push(object val)
44+
public void Push(dynamic val)
4545
{
4646
Array.Resize(ref Stack, Stack.Length + 1);
4747
Stack[Stack.Length - 1] = val;
4848
}
4949

5050
public void Run(int Offset)
5151
{
52-
object[] LocalVariables = new object[512];
52+
dynamic[] LocalVariables = new dynamic[512];
5353
while (true)
5454
{
5555
byte curByte = Marshal.ReadByte(IntPtr.Add(MemoryPointer, CodeSectionOffset + Offset++));
@@ -74,11 +74,40 @@ public void Run(int Offset)
7474
break;
7575
}
7676
case 0x52: //LdByte
77-
{
78-
Push(Marshal.ReadByte(MemoryPointer, CodeSectionOffset + Offset));
79-
Offset += 1;
80-
break;
81-
}
77+
{
78+
Push(Marshal.ReadByte(MemoryPointer, CodeSectionOffset + Offset));
79+
Offset += 1;
80+
break;
81+
}
82+
case 0x70: //Newarr
83+
{
84+
int length = Pop();
85+
byte[] temp = new byte[Marshal.ReadInt32(MemoryPointer, CodeSectionOffset + Offset + 4)];
86+
Marshal.Copy(
87+
IntPtr.Add(MemoryPointer,
88+
CSharpTypesSectionOffset + Marshal.ReadInt32(MemoryPointer, Offset)), temp, 0,
89+
temp.Length);
90+
Type arrType = Type.GetType(Encoding.UTF8.GetString(temp)).MakeArrayType();
91+
Push(Activator.CreateInstance(arrType, length));
92+
Offset += 8;
93+
break;
94+
}
95+
case 0x71: //Setarr
96+
{
97+
dynamic value = Pop();
98+
dynamic index = Pop();
99+
dynamic arr = Pop();
100+
arr[index] = value;
101+
Push(arr);
102+
break;
103+
}
104+
case 0x72: //Getarr
105+
{
106+
dynamic index = Pop();
107+
dynamic arr = Pop();
108+
Push(arr[index]);
109+
break;
110+
}
82111
case 0x60: // Br
83112
{
84113
Offset = CodeSectionOffset + Marshal.ReadInt32(MemoryPointer, CodeSectionOffset + Offset);
@@ -118,7 +147,7 @@ public void Run(int Offset)
118147
string voidname = Encoding.UTF8.GetString(temp);
119148
int argcount = Marshal.ReadInt32(MemoryPointer, CodeSectionOffset + Offset + 16);
120149
Type[] types = new Type[argcount];
121-
object[] arguments = new object[argcount];
150+
dynamic[] arguments = new dynamic[argcount];
122151
for (int i = argcount - 1; i > -1; i--)
123152
{
124153
temp = new byte[Marshal.ReadInt32(MemoryPointer, CodeSectionOffset + Offset + 20 + i * 8)];

VMCompiler/Compiler.cs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,35 @@ public static void Compile(string codeString, string filename)
134134
break;
135135
case OpCode.LdByte:
136136
{
137-
int oper = i.operand;
137+
byte oper = i.operand;
138138
Array.Resize(ref CodeSection, CodeSection.Length + 2);
139139
CodeSection[CodeSection.Length - 2] = 0x52;
140-
Array.Copy(BitConverter.GetBytes(oper), 0, CodeSection,
141-
CodeSection.Length - 1, 1);
140+
CodeSection[CodeSection.Length - 1] = oper;
141+
}
142+
break;
143+
case OpCode.Newarr:
144+
{
145+
UsingType oper = i.operand;
146+
Array.Resize(ref CodeSection, CodeSection.Length + 9);
147+
CodeSection[CodeSection.Length - 9] = 0x70;
148+
Array.Copy(BitConverter.GetBytes(oper.Start), 0, CodeSection,
149+
CodeSection.Length - 8, 4);
150+
Array.Copy(BitConverter.GetBytes(oper.Length), 0, CodeSection,
151+
CodeSection.Length - 4, 4);
152+
}
153+
break;
154+
case OpCode.Setarr:
155+
{
156+
Array.Resize(ref CodeSection, CodeSection.Length + 1);
157+
CodeSection[CodeSection.Length - 1] = 0x71;
158+
}
159+
break;
160+
case OpCode.Getarr:
161+
{
162+
Array.Resize(ref CodeSection, CodeSection.Length + 1);
163+
CodeSection[CodeSection.Length - 1] = 0x72;
142164
}
143165
break;
144-
145166
case OpCode.Br:
146167
{
147168
Array.Resize(ref CodeSection, CodeSection.Length + 5);
@@ -304,6 +325,12 @@ public static void Compile(string codeString, string filename)
304325
CodeSection.Length - 4, 4);
305326
}
306327
break;
328+
case OpCode.Nop:
329+
{
330+
Array.Resize(ref CodeSection, CodeSection.Length + 1);
331+
CodeSection[CodeSection.Length - 1] = 0x90;
332+
}
333+
break;
307334
case OpCode.Ret:
308335
{
309336
Array.Resize(ref CodeSection, CodeSection.Length + 1);

VMCompiler/CompilerClasses.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Text;
55
using System.Text.RegularExpressions;
6+
using System.Windows.Forms;
67

78
namespace VMCompiler
89
{
@@ -49,12 +50,21 @@ internal enum OpCode
4950
{
5051
// Types
5152
LdStr, //50 //Load String to Stack
52-
LdInt, //51 //Load Integer to Stack
5353
LdByte, //52 //Load Byte to Stack
54+
LdInt, //51 //Load Integer to Stack
55+
//LdLong, //53
56+
//Newobj, //54
57+
58+
//Arrays
59+
Newarr, //70
60+
Setarr, //71
61+
Getarr, //72
62+
5463

5564
// Voids
5665
Call, //40 //Call Void
5766
CallCSharp, //41 //CallCSharp Void
67+
//CallVirtCSharp, //41 //CallCSharp Void
5868

5969
// Compare
6070
Ceq, // A1 // == -> 1 | 0
@@ -157,6 +167,25 @@ public VoidType(string name, string v, int ofs)
157167
Length += 5;
158168
}
159169
break;
170+
case OpCode.Newarr:
171+
{
172+
Instructions[Instructions.Length - 1] = new Instruction(op,
173+
Compiler.usingsContainer[str[i].Substring(str[i].IndexOf(" ") + 1)]) {Index = Length};
174+
Length += 9;
175+
}
176+
break;
177+
case OpCode.Setarr:
178+
{
179+
Instructions[Instructions.Length - 1] = new Instruction(op, null) {Index = Length};
180+
Length += 1;
181+
}
182+
break;
183+
case OpCode.Getarr:
184+
{
185+
Instructions[Instructions.Length - 1] = new Instruction(op, null) {Index = Length};
186+
Length += 1;
187+
}
188+
break;
160189
case OpCode.Call:
161190
{
162191
Instructions[Instructions.Length - 1] =
@@ -176,6 +205,7 @@ public VoidType(string name, string v, int ofs)
176205
oper.Name = voi.Groups[2].Value;
177206

178207
string[] args = voi.Groups[3].Value.Split(',');
208+
args = args.Where(x => x != "").ToArray();
179209
Array.Resize(ref oper.Arguments, args.Length);
180210
for (int a = 0; a < args.Length; a++)
181211
{

0 commit comments

Comments
 (0)