@@ -29,10 +29,12 @@ public static Func<IProcessReader, IntPtr> CompileExpression(IExpression express
2929 ) . Compile ( ) ;
3030 }
3131
32- private static Expression GenerateMethodBody ( IExpression expression , Expression parameter )
32+ private static Expression GenerateMethodBody ( IExpression expression , Expression processParameter )
3333 {
3434 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 ) } ) ;
3638
3739 switch ( expression )
3840 {
@@ -44,37 +46,37 @@ private static Expression GenerateMethodBody(IExpression expression, Expression
4446 }
4547 case NegateExpression negateExpression :
4648 {
47- var argument = GenerateMethodBody ( negateExpression . Expression , parameter ) ;
49+ var argument = GenerateMethodBody ( negateExpression . Expression , processParameter ) ;
4850
4951 var negateFn = typeof ( IntPtrExtension ) . GetRuntimeMethod ( nameof ( IntPtrExtension . Negate ) , new [ ] { typeof ( IntPtr ) } ) ;
5052
5153 return Expression . Call ( null , negateFn , argument ) ;
5254 }
5355 case AddExpression addExpression :
5456 {
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 ) ;
5759
5860 return Expression . Call ( null , GetIntPtrExtension ( nameof ( IntPtrExtension . Add ) ) , argument1 , argument2 ) ;
5961 }
6062 case SubtractExpression subtractExpression :
6163 {
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 ) ;
6466
6567 return Expression . Call ( null , GetIntPtrExtension ( nameof ( IntPtrExtension . Sub ) ) , argument1 , argument2 ) ;
6668 }
6769 case MultiplyExpression multiplyExpression :
6870 {
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 ) ;
7173
7274 return Expression . Call ( null , GetIntPtrExtension ( nameof ( IntPtrExtension . Mul ) ) , argument1 , argument2 ) ;
7375 }
7476 case DivideExpression divideExpression :
7577 {
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 ) ;
7880
7981 return Expression . Call ( null , GetIntPtrExtension ( nameof ( IntPtrExtension . Div ) ) , argument1 , argument2 ) ;
8082 }
@@ -84,7 +86,7 @@ private static Expression GenerateMethodBody(IExpression expression, Expression
8486 var moduleNameConstant = Expression . Constant ( moduleExpression . Name ) ;
8587
8688 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 ) ) ;
8890
8991 return Expression . Block (
9092 new [ ] { moduleVariable } ,
@@ -98,28 +100,15 @@ private static Expression GenerateMethodBody(IExpression expression, Expression
98100 }
99101 case ReadMemoryExpression readMemoryExpression :
100102 {
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 ) ;
105104
106- var callExpression = Expression . Call ( parameter , readRemoteIntFn , argument ) ;
105+ var readRemoteIntPtrFn = typeof ( IRemoteMemoryReaderExtension ) . GetRuntimeMethod ( nameof ( IRemoteMemoryReaderExtension . ReadRemoteIntPtr ) , new [ ] { typeof ( IRemoteMemoryReader ) , typeof ( IntPtr ) } ) ;
107106
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 ) ;
112108 }
113109 }
114110
115111 throw new ArgumentException ( $ "Unsupported operation '{ expression . GetType ( ) . FullName } '.") ;
116112 }
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- }
124113 }
125114}
0 commit comments