@@ -29,10 +29,12 @@ public static Func<IProcessReader, IntPtr> CompileExpression(IExpression express
29
29
) . Compile ( ) ;
30
30
}
31
31
32
- private static Expression GenerateMethodBody ( IExpression expression , Expression parameter )
32
+ private static Expression GenerateMethodBody ( IExpression expression , Expression processParameter )
33
33
{
34
34
Contract . Requires ( expression != null ) ;
35
- Contract . Requires ( parameter != null ) ;
35
+ Contract . Requires ( processParameter != null ) ;
36
+
37
+ static MethodInfo GetIntPtrExtension ( string name ) => typeof ( IntPtrExtension ) . GetRuntimeMethod ( name , new [ ] { typeof ( IntPtr ) , typeof ( IntPtr ) } ) ;
36
38
37
39
switch ( expression )
38
40
{
@@ -44,37 +46,37 @@ private static Expression GenerateMethodBody(IExpression expression, Expression
44
46
}
45
47
case NegateExpression negateExpression :
46
48
{
47
- var argument = GenerateMethodBody ( negateExpression . Expression , parameter ) ;
49
+ var argument = GenerateMethodBody ( negateExpression . Expression , processParameter ) ;
48
50
49
51
var negateFn = typeof ( IntPtrExtension ) . GetRuntimeMethod ( nameof ( IntPtrExtension . Negate ) , new [ ] { typeof ( IntPtr ) } ) ;
50
52
51
53
return Expression . Call ( null , negateFn , argument ) ;
52
54
}
53
55
case AddExpression addExpression :
54
56
{
55
- var argument1 = GenerateMethodBody ( addExpression . Lhs , parameter ) ;
56
- var argument2 = GenerateMethodBody ( addExpression . Rhs , parameter ) ;
57
+ var argument1 = GenerateMethodBody ( addExpression . Lhs , processParameter ) ;
58
+ var argument2 = GenerateMethodBody ( addExpression . Rhs , processParameter ) ;
57
59
58
60
return Expression . Call ( null , GetIntPtrExtension ( nameof ( IntPtrExtension . Add ) ) , argument1 , argument2 ) ;
59
61
}
60
62
case SubtractExpression subtractExpression :
61
63
{
62
- var argument1 = GenerateMethodBody ( subtractExpression . Lhs , parameter ) ;
63
- var argument2 = GenerateMethodBody ( subtractExpression . Rhs , parameter ) ;
64
+ var argument1 = GenerateMethodBody ( subtractExpression . Lhs , processParameter ) ;
65
+ var argument2 = GenerateMethodBody ( subtractExpression . Rhs , processParameter ) ;
64
66
65
67
return Expression . Call ( null , GetIntPtrExtension ( nameof ( IntPtrExtension . Sub ) ) , argument1 , argument2 ) ;
66
68
}
67
69
case MultiplyExpression multiplyExpression :
68
70
{
69
- var argument1 = GenerateMethodBody ( multiplyExpression . Lhs , parameter ) ;
70
- var argument2 = GenerateMethodBody ( multiplyExpression . Rhs , parameter ) ;
71
+ var argument1 = GenerateMethodBody ( multiplyExpression . Lhs , processParameter ) ;
72
+ var argument2 = GenerateMethodBody ( multiplyExpression . Rhs , processParameter ) ;
71
73
72
74
return Expression . Call ( null , GetIntPtrExtension ( nameof ( IntPtrExtension . Mul ) ) , argument1 , argument2 ) ;
73
75
}
74
76
case DivideExpression divideExpression :
75
77
{
76
- var argument1 = GenerateMethodBody ( divideExpression . Lhs , parameter ) ;
77
- var argument2 = GenerateMethodBody ( divideExpression . Rhs , parameter ) ;
78
+ var argument1 = GenerateMethodBody ( divideExpression . Lhs , processParameter ) ;
79
+ var argument2 = GenerateMethodBody ( divideExpression . Rhs , processParameter ) ;
78
80
79
81
return Expression . Call ( null , GetIntPtrExtension ( nameof ( IntPtrExtension . Div ) ) , argument1 , argument2 ) ;
80
82
}
@@ -84,7 +86,7 @@ private static Expression GenerateMethodBody(IExpression expression, Expression
84
86
var moduleNameConstant = Expression . Constant ( moduleExpression . Name ) ;
85
87
86
88
var moduleVariable = Expression . Variable ( typeof ( Memory . Module ) ) ;
87
- var assignExpression = Expression . Assign ( moduleVariable , Expression . Call ( parameter , getModuleByNameFunc , moduleNameConstant ) ) ;
89
+ var assignExpression = Expression . Assign ( moduleVariable , Expression . Call ( processParameter , getModuleByNameFunc , moduleNameConstant ) ) ;
88
90
89
91
return Expression . Block (
90
92
new [ ] { moduleVariable } ,
@@ -98,28 +100,15 @@ private static Expression GenerateMethodBody(IExpression expression, Expression
98
100
}
99
101
case ReadMemoryExpression readMemoryExpression :
100
102
{
101
- var argument = GenerateMethodBody ( readMemoryExpression . Expression , parameter ) ;
102
-
103
- var functionName = readMemoryExpression . ByteCount == 4 ? nameof ( IRemoteMemoryReader . ReadRemoteInt32 ) : nameof ( IRemoteMemoryReader . ReadRemoteInt64 ) ;
104
- var readRemoteIntFn = typeof ( IRemoteMemoryReader ) . GetRuntimeMethod ( functionName , new [ ] { typeof ( IntPtr ) } ) ;
103
+ var addressParameter = GenerateMethodBody ( readMemoryExpression . Expression , processParameter ) ;
105
104
106
- var callExpression = Expression . Call ( parameter , readRemoteIntFn , argument ) ;
105
+ var readRemoteIntPtrFn = typeof ( IRemoteMemoryReaderExtension ) . GetRuntimeMethod ( nameof ( IRemoteMemoryReaderExtension . ReadRemoteIntPtr ) , new [ ] { typeof ( IRemoteMemoryReader ) , typeof ( IntPtr ) } ) ;
107
106
108
- var paramType = readMemoryExpression . ByteCount == 4 ? typeof ( int ) : typeof ( long ) ;
109
- var convertFn = typeof ( IntPtrExtension ) . GetRuntimeMethod ( nameof ( IntPtrExtension . From ) , new [ ] { paramType } ) ;
110
-
111
- return Expression . Call ( null , convertFn , callExpression ) ;
107
+ return Expression . Call ( null , readRemoteIntPtrFn , processParameter , addressParameter ) ;
112
108
}
113
109
}
114
110
115
111
throw new ArgumentException ( $ "Unsupported operation '{ expression . GetType ( ) . FullName } '.") ;
116
112
}
117
-
118
- private static MethodInfo GetIntPtrExtension ( string name )
119
- {
120
- Contract . Requires ( name != null ) ;
121
-
122
- return typeof ( IntPtrExtension ) . GetRuntimeMethod ( name , new [ ] { typeof ( IntPtr ) , typeof ( IntPtr ) } ) ;
123
- }
124
113
}
125
114
}
0 commit comments