Skip to content

Commit bec6058

Browse files
committed
Added Newobj and CallVirtCSharp
Added syntax highlight for Newarr, Newobj, CallVirtCSharp Added opcodes Newobj and CallVirtCSharp
1 parent fa89e79 commit bec6058

File tree

6 files changed

+154
-3
lines changed

6 files changed

+154
-3
lines changed

VM/VM.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<ItemGroup>
4040
<Reference Include="System" />
4141
<Reference Include="Microsoft.CSharp" />
42+
<Reference Include="System.Windows.Forms" />
4243
</ItemGroup>
4344
<ItemGroup>
4445
<Compile Include="Program.cs" />

VM/VirtualMachine.cs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@ public void Run(int Offset)
7979
Offset += 1;
8080
break;
8181
}
82+
case 0x54: //Newobj
83+
{
84+
byte[] temp = new byte[Marshal.ReadInt32(MemoryPointer, CodeSectionOffset + Offset + 4)];
85+
Marshal.Copy(
86+
IntPtr.Add(MemoryPointer,
87+
CSharpTypesSectionOffset + Marshal.ReadInt32(MemoryPointer, Offset)), temp, 0,
88+
temp.Length);
89+
Type objType = Type.GetType(Encoding.UTF8.GetString(temp));
90+
Push(Activator.CreateInstance(objType));
91+
Offset += 8;
92+
break;
93+
}
8294
case 0x70: //Newarr
8395
{
8496
int length = Pop();
@@ -171,8 +183,43 @@ public void Run(int Offset)
171183
Offset += 20 + argcount * 8;
172184
break;
173185
}
174-
case 0xA1: // Ceq
186+
case 0x42: //CallVirt C# Method
187+
{
188+
dynamic obj = Pop();
189+
Type type = obj.GetType();
190+
byte[] temp = new byte[Marshal.ReadInt32(MemoryPointer, CodeSectionOffset + Offset + 4)];
191+
Marshal.Copy(
192+
IntPtr.Add(MemoryPointer,
193+
TextSectionOffset + Marshal.ReadInt32(MemoryPointer, CodeSectionOffset + Offset)), temp, 0, temp.Length);
194+
string voidname = Encoding.UTF8.GetString(temp);
195+
int argcount = Marshal.ReadInt32(MemoryPointer, CodeSectionOffset + Offset + 8);
196+
Type[] types = new Type[argcount];
197+
dynamic[] arguments = new dynamic[argcount];
198+
for (int i = argcount - 1; i > -1; i--)
175199
{
200+
temp = new byte[Marshal.ReadInt32(MemoryPointer, CodeSectionOffset + Offset + 12 + i * 8)];
201+
202+
Marshal.Copy(
203+
IntPtr.Add(MemoryPointer,
204+
CSharpTypesSectionOffset + Marshal.ReadInt32(MemoryPointer,
205+
CodeSectionOffset + Offset + 16 + i * 8)), temp, 0, temp.Length);
206+
types[i] = Type.GetType(Encoding.UTF8.GetString(temp));
207+
arguments[i] = Pop();
208+
}
209+
210+
var method = type.GetMethod(voidname, types);
211+
212+
var rez = method.Invoke(obj, arguments);
213+
if (rez != null)
214+
{
215+
Push(rez);
216+
}
217+
218+
Offset += 12 + argcount * 8;
219+
break;
220+
}
221+
case 0xA1: // Ceq
222+
{
176223
dynamic val2 = Pop();
177224
dynamic val1 = Pop();
178225
if (val1 == null || val2 == null)

VMCompiler/Compiler.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,17 @@ public static void Compile(string codeString, string filename)
140140
CodeSection[CodeSection.Length - 1] = oper;
141141
}
142142
break;
143+
case OpCode.Newobj:
144+
{
145+
UsingType oper = i.operand;
146+
Array.Resize(ref CodeSection, CodeSection.Length + 9);
147+
CodeSection[CodeSection.Length - 9] = 0x54;
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;
143154
case OpCode.Newarr:
144155
{
145156
UsingType oper = i.operand;
@@ -198,6 +209,33 @@ public static void Compile(string codeString, string filename)
198209
CodeSection.Length - 4, 4);
199210
}
200211
break;
212+
213+
case OpCode.CallVirtCSharp:
214+
{
215+
CSharpVoid vd = i.operand;
216+
stringsContainer.Add(vd.Name);
217+
Array.Resize(ref TextSection, TextSection.Length + vd.Name.Length);
218+
Array.Copy(Encoding.UTF8.GetBytes(vd.Name), 0, TextSection,
219+
TextSection.Length - vd.Name.Length, vd.Name.Length);
220+
221+
Array.Resize(ref CodeSection, CodeSection.Length + vd.Length);
222+
CodeSection[CodeSection.Length - vd.Length] = 0x42;
223+
Array.Copy(BitConverter.GetBytes(stringsContainer[vd.Name].Index), 0, CodeSection,
224+
CodeSection.Length - vd.Length + 1, 4);
225+
Array.Copy(BitConverter.GetBytes(stringsContainer[vd.Name].Length), 0, CodeSection,
226+
CodeSection.Length - vd.Length + 5, 4);
227+
228+
Array.Copy(BitConverter.GetBytes(vd.Arguments.Length), 0, CodeSection,
229+
CodeSection.Length - vd.Length + 9, 4);
230+
for (int a = 0; a < vd.Arguments.Length; a++)
231+
{
232+
Array.Copy(BitConverter.GetBytes(vd.Arguments[a].Length), 0, CodeSection,
233+
CodeSection.Length - vd.Length + 13 + a * 8, 4);
234+
Array.Copy(BitConverter.GetBytes(vd.Arguments[a].Start), 0, CodeSection,
235+
CodeSection.Length - vd.Length + 17 + a * 8, 4);
236+
}
237+
}
238+
break;
201239
case OpCode.CallCSharp:
202240
{
203241
CSharpVoid vd = i.operand;

VMCompiler/CompilerClasses.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ internal enum OpCode
5353
LdByte, //52 //Load Byte to Stack
5454
LdInt, //51 //Load Integer to Stack
5555
//LdLong, //53
56-
//Newobj, //54
56+
Newobj, //54
5757

5858
//Arrays
5959
Newarr, //70
@@ -64,7 +64,7 @@ internal enum OpCode
6464
// Voids
6565
Call, //40 //Call Void
6666
CallCSharp, //41 //CallCSharp Void
67-
//CallVirtCSharp, //41 //CallCSharp Void
67+
CallVirtCSharp, //42 //CallVirtCSharp Void
6868

6969
// Compare
7070
Ceq, // A1 // == -> 1 | 0
@@ -167,6 +167,13 @@ public VoidType(string name, string v, int ofs)
167167
Length += 5;
168168
}
169169
break;
170+
case OpCode.Newobj:
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;
170177
case OpCode.Newarr:
171178
{
172179
Instructions[Instructions.Length - 1] = new Instruction(op,
@@ -193,6 +200,30 @@ public VoidType(string name, string v, int ofs)
193200
Length += 5;
194201
}
195202
break;
203+
case OpCode.CallVirtCSharp:
204+
{
205+
string opstr = str[i].Substring(str[i].IndexOf(" ") + 1);
206+
CSharpVoid oper = new CSharpVoid();
207+
208+
Regex vo = new Regex("([a-zA-Z0-9]{1,})\\((.*)\\)");
209+
Match voi = vo.Match(opstr);
210+
211+
oper.Type = null;
212+
oper.Name = voi.Groups[1].Value;
213+
214+
string[] args = voi.Groups[2].Value.Split(',');
215+
args = args.Where(x => x != "").ToArray();
216+
Array.Resize(ref oper.Arguments, args.Length);
217+
for (int a = 0; a < args.Length; a++)
218+
{
219+
oper.Arguments[a] = Compiler.usingsContainer[args[a]];
220+
}
221+
oper.Length = 13 + 8 * args.Length;
222+
Instructions[Instructions.Length - 1] =
223+
new Instruction(op, oper) { Index = Length };
224+
Length += 13 + 8 * args.Length;
225+
}
226+
break;
196227
case OpCode.CallCSharp:
197228
{
198229
string opstr = str[i].Substring(str[i].IndexOf(" ") + 1);

VMCompiler/Form1.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,23 @@ private void Highlight(string text, int offset)
131131
richTextBox1.Select(match.Groups[3].Index + offset, match.Groups[3].Length);
132132
richTextBox1.SelectionColor = ColorTranslator.FromHtml("#2ecc71");
133133
}
134+
135+
Regex callvirtcsharp = new Regex(@"CallVirtCSharp (.*?)\((.*)\)");
136+
foreach (Match match in callvirtcsharp.Matches(text))
137+
{
138+
richTextBox1.Select(match.Groups[1].Index + offset, match.Groups[1].Length);
139+
richTextBox1.SelectionColor = ColorTranslator.FromHtml("#e67e22");
140+
141+
richTextBox1.Select(match.Groups[2].Index + offset, match.Groups[2].Length);
142+
richTextBox1.SelectionColor = ColorTranslator.FromHtml("#2ecc71");
143+
}
144+
145+
Regex arrorobj = new Regex(@"[Newarr|Newobj] (.*)");
146+
foreach (Match match in arrorobj.Matches(text))
147+
{
148+
richTextBox1.Select(match.Groups[1].Index + offset, match.Groups[1].Length);
149+
richTextBox1.SelectionColor = ColorTranslator.FromHtml("#2ecc71");
150+
}
134151
}
135152

136153
private void getTypeToolStripMenuItem_Click(object sender, EventArgs e)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using String=System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;
2+
using Console=System.Console, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;
3+
4+
void Main
5+
{
6+
var arr
7+
LdInt 5
8+
Newarr String
9+
StLv arr
10+
LdLv arr
11+
LdInt 1
12+
LdStr "Hello"
13+
Setarr
14+
LdInt 1
15+
Getarr
16+
Ret
17+
}

0 commit comments

Comments
 (0)