forked from CosmosOS/Cosmos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpVar.cs
116 lines (109 loc) · 3.3 KB
/
OpVar.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Reflection;
using System.Reflection.Metadata;
using Cosmos.Debug.Symbols;
using Cosmos.IL2CPU.Extensions;
namespace Cosmos.IL2CPU.ILOpCodes
{
public class OpVar : ILOpCode
{
public readonly UInt16 Value;
public OpVar(Code aOpCode, int aPos, int aNextPos, UInt16 aValue, _ExceptionRegionInfo aCurrentExceptionRegion)
: base(aOpCode, aPos, aNextPos, aCurrentExceptionRegion)
{
Value = aValue;
}
public override int GetNumberOfStackPops(MethodBase aMethod)
{
switch (OpCode)
{
case Code.Ldloc:
case Code.Ldloca:
case Code.Ldarg:
case Code.Ldarga:
return 0;
case Code.Stloc:
case Code.Starg:
return 1;
default:
throw new NotImplementedException("OpCode '" + OpCode + "' not implemented!");
}
}
public override int GetNumberOfStackPushes(MethodBase aMethod)
{
switch (OpCode)
{
case Code.Stloc:
case Code.Starg:
return 0;
case Code.Ldloc:
case Code.Ldloca:
case Code.Ldarg:
case Code.Ldarga:
return 1;
default:
throw new NotImplementedException("OpCode '" + OpCode + "' not implemented!");
}
}
protected override void DoInitStackAnalysis(MethodBase aMethod)
{
base.DoInitStackAnalysis(aMethod);
var xArgIndexCorrection = 0;
var xParams = aMethod.GetParameters();
var xLocals = aMethod.GetLocalVariables();
switch (OpCode)
{
case Code.Ldloc:
StackPushTypes[0] = xLocals[Value].Type;
if (StackPushTypes[0].GetTypeInfo().IsEnum)
{
StackPushTypes[0] = StackPushTypes[0].GetTypeInfo().GetEnumUnderlyingType();
}
return;
case Code.Ldloca:
StackPushTypes[0] = xLocals[Value].Type.MakeByRefType();
return;
case Code.Ldarg:
if (!aMethod.IsStatic)
{
if (Value == 0)
{
StackPushTypes[0] = aMethod.DeclaringType;
if (StackPushTypes[0].GetTypeInfo().IsEnum)
{
StackPushTypes[0] = StackPushTypes[0].GetTypeInfo().GetEnumUnderlyingType();
}
else if (StackPushTypes[0].GetTypeInfo().IsValueType)
{
StackPushTypes[0] = StackPushTypes[0].MakeByRefType();
}
return;
}
xArgIndexCorrection = -1;
}
StackPushTypes[0] = xParams[Value + xArgIndexCorrection].ParameterType;
if (StackPushTypes[0].GetTypeInfo().IsEnum)
{
StackPushTypes[0] = StackPushTypes[0].GetTypeInfo().GetEnumUnderlyingType();
}
return;
case Code.Ldarga:
if (!aMethod.IsStatic)
{
if (Value == 0)
{
if (StackPushTypes[0].GetTypeInfo().IsValueType)
{
StackPushTypes[0] = StackPushTypes[0].MakeByRefType();
}
return;
}
xArgIndexCorrection = -1;
}
StackPushTypes[0] = xParams[Value + xArgIndexCorrection].ParameterType;
StackPushTypes[0] = StackPushTypes[0].MakeByRefType();
return;
}
}
}
}