forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCFuncPtrType.cs
More file actions
363 lines (298 loc) · 14.5 KB
/
CFuncPtrType.cs
File metadata and controls
363 lines (298 loc) · 14.5 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
#if FEATURE_CTYPES
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Microsoft.Scripting;
using Microsoft.Scripting.Actions;
using Microsoft.Scripting.Generation;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;
using IronPython.Runtime;
using IronPython.Runtime.Operations;
using IronPython.Runtime.Types;
namespace IronPython.Modules {
/// <summary>
/// Provides support for interop with native code from Python code.
/// </summary>
public static partial class CTypes {
/// <summary>
/// The meta class for ctypes function pointer instances.
/// </summary>
[PythonType, PythonHidden]
public class CFuncPtrType : PythonType, INativeType {
internal readonly int _flags;
internal readonly PythonType _restype;
internal readonly INativeType[] _argtypes;
private DynamicMethod _reverseDelegate; // reverse delegates are lazily computed the 1st time a callable is turned into a func ptr
private List<object> _reverseDelegateConstants;
private Type _reverseDelegateType;
private static Dictionary<DelegateCacheKey, Type> _reverseDelegates = new Dictionary<DelegateCacheKey, Type>();
//from_buffer_copy, from_param, from_address, from_buffer, __doc__ __mul__ __rmul__ in_dll __new__
public CFuncPtrType(CodeContext/*!*/ context, string name, PythonTuple bases, PythonDictionary members)
: base(context, name, bases, members) {
if (!members.TryGetValue("_flags_", out object flags) || !(flags is int)) {
throw PythonOps.TypeError("class must define _flags_ which must be an integer");
}
_flags = (int)flags;
if (members.TryGetValue("_restype_", out object restype) && (restype is PythonType)) {
_restype = (PythonType)restype;
}
if (members.TryGetValue("_argtypes_", out object argtypes) && (argtypes is PythonTuple)) {
PythonTuple pt = argtypes as PythonTuple;
_argtypes = new INativeType[pt.Count];
for (int i = 0; i < pt.Count; i++) {
_argtypes[i] = (INativeType)pt[i];
}
}
}
private CFuncPtrType(Type underlyingSystemType)
: base(underlyingSystemType) {
}
internal static PythonType MakeSystemType(Type underlyingSystemType) {
return PythonType.SetPythonType(underlyingSystemType, new CFuncPtrType(underlyingSystemType));
}
/// <summary>
/// Converts an object into a function call parameter.
/// </summary>
public object from_param(object obj) {
return null;
}
// TODO: Move to Ops class
public object internal_restype {
get {
return _restype;
}
}
#region INativeType Members
int INativeType.Size {
get {
return IntPtr.Size;
}
}
int INativeType.Alignment {
get {
return IntPtr.Size;
}
}
object INativeType.GetValue(MemoryHolder owner, object readingFrom, int offset, bool raw) {
IntPtr funcAddr = owner.ReadIntPtr(offset);
if (raw) {
return funcAddr.ToPython();
}
return CreateInstance(Context.SharedContext, funcAddr);
}
object INativeType.SetValue(MemoryHolder address, int offset, object value) {
if (value is int) {
address.WriteIntPtr(offset, new IntPtr((int)value));
} else if (value is BigInteger) {
address.WriteIntPtr(offset, new IntPtr((long)(BigInteger)value));
} else if (value is _CFuncPtr) {
address.WriteIntPtr(offset, ((_CFuncPtr)value).addr);
return value;
} else {
throw PythonOps.TypeErrorForTypeMismatch("func pointer", value);
}
return null;
}
Type INativeType.GetNativeType() {
return typeof(IntPtr);
}
MarshalCleanup INativeType.EmitMarshalling(ILGenerator/*!*/ method, LocalOrArg argIndex, List<object>/*!*/ constantPool, int constantPoolArgument) {
Type argumentType = argIndex.Type;
argIndex.Emit(method);
if (argumentType.IsValueType) {
method.Emit(OpCodes.Box, argumentType);
}
constantPool.Add(this);
method.Emit(OpCodes.Ldarg, constantPoolArgument);
method.Emit(OpCodes.Ldc_I4, constantPool.Count - 1);
method.Emit(OpCodes.Ldelem_Ref);
method.Emit(OpCodes.Call, typeof(ModuleOps).GetMethod("GetFunctionPointerValue"));
return null;
}
Type/*!*/ INativeType.GetPythonType() {
return typeof(_CFuncPtr);
}
void INativeType.EmitReverseMarshalling(ILGenerator method, LocalOrArg value, List<object> constantPool, int constantPoolArgument) {
value.Emit(method);
constantPool.Add(this);
method.Emit(OpCodes.Ldarg, constantPoolArgument);
method.Emit(OpCodes.Ldc_I4, constantPool.Count - 1);
method.Emit(OpCodes.Ldelem_Ref);
method.Emit(OpCodes.Call, typeof(ModuleOps).GetMethod("CreateCFunction"));
}
string INativeType.TypeFormat {
get {
return "X{}";
}
}
#endregion
internal CallingConvention CallingConvention {
get {
switch (_flags & 0x07) {
case FUNCFLAG_STDCALL: return CallingConvention.StdCall;
case FUNCFLAG_CDECL: return CallingConvention.Cdecl;
case FUNCFLAG_HRESULT:
case FUNCFLAG_PYTHONAPI:
break;
}
return CallingConvention.Cdecl;
}
}
internal Delegate MakeReverseDelegate(CodeContext/*!*/ context, object target) {
if (_reverseDelegate == null) {
lock (this) {
if (_reverseDelegate == null) {
MakeReverseDelegateWorker(context);
}
}
}
object[] constantPool = _reverseDelegateConstants.ToArray();
constantPool[0] = target;
return _reverseDelegate.CreateDelegate(_reverseDelegateType, constantPool);
}
private void MakeReverseDelegateWorker(CodeContext context) {
Type[] sigTypes;
Type[] callSiteType;
Type retType;
GetSignatureInfo(out sigTypes, out callSiteType, out retType);
DynamicMethod dm = new DynamicMethod("ReverseInteropInvoker", retType, ArrayUtils.RemoveLast(sigTypes), DynamicModule);
ILGenerator ilGen = dm.GetILGenerator();
PythonContext pc = context.LanguageContext;
Type callDelegateSiteType = CompilerHelpers.MakeCallSiteDelegateType(callSiteType);
CallSite site = CallSite.Create(callDelegateSiteType, pc.Invoke(new CallSignature(_argtypes.Length)));
List<object> constantPool = new List<object>();
constantPool.Add(null); // 1st item is the target object, will be put in later.
constantPool.Add(site);
ilGen.BeginExceptionBlock();
//CallSite<Func<CallSite, object, object>> mySite;
//mySite.Target(mySite, target, ...);
LocalBuilder siteLocal = ilGen.DeclareLocal(site.GetType());
ilGen.Emit(OpCodes.Ldarg_0);
ilGen.Emit(OpCodes.Ldc_I4, constantPool.Count - 1);
ilGen.Emit(OpCodes.Ldelem_Ref);
ilGen.Emit(OpCodes.Castclass, site.GetType());
ilGen.Emit(OpCodes.Stloc, siteLocal);
ilGen.Emit(OpCodes.Ldloc, siteLocal);
ilGen.Emit(OpCodes.Ldfld, site.GetType().GetField("Target"));
ilGen.Emit(OpCodes.Ldloc, siteLocal);
// load code context
int contextIndex = constantPool.Count;
Debug.Assert(pc.SharedContext != null);
constantPool.Add(pc.SharedContext);
ilGen.Emit(OpCodes.Ldarg_0);
ilGen.Emit(OpCodes.Ldc_I4, contextIndex);
ilGen.Emit(OpCodes.Ldelem_Ref);
// load function target, in constant pool slot 0
ilGen.Emit(OpCodes.Ldarg_0);
ilGen.Emit(OpCodes.Ldc_I4_0);
ilGen.Emit(OpCodes.Ldelem_Ref);
// load arguments
for (int i = 0; i < _argtypes.Length; i++) {
INativeType nativeType = _argtypes[i];
nativeType.EmitReverseMarshalling(ilGen, new Arg(i + 1, sigTypes[i + 1]), constantPool, 0);
}
ilGen.Emit(OpCodes.Call, callDelegateSiteType.GetMethod("Invoke"));
LocalBuilder finalRes = null;
// emit forward marshaling for return value
if (_restype != null) {
LocalBuilder tmpRes = ilGen.DeclareLocal(typeof(object));
ilGen.Emit(OpCodes.Stloc, tmpRes);
finalRes = ilGen.DeclareLocal(retType);
((INativeType)_restype).EmitMarshalling(ilGen, new Local(tmpRes), constantPool, 0);
ilGen.Emit(OpCodes.Stloc, finalRes);
} else {
ilGen.Emit(OpCodes.Pop);
}
// } catch(Exception e) {
// emit the cleanup code
ilGen.BeginCatchBlock(typeof(Exception));
ilGen.Emit(OpCodes.Ldarg_0);
ilGen.Emit(OpCodes.Ldc_I4, contextIndex);
ilGen.Emit(OpCodes.Ldelem_Ref);
ilGen.Emit(OpCodes.Call, typeof(ModuleOps).GetMethod("CallbackException"));
ilGen.EndExceptionBlock();
if (_restype != null) {
ilGen.Emit(OpCodes.Ldloc, finalRes);
}
ilGen.Emit(OpCodes.Ret);
_reverseDelegateConstants = constantPool;
_reverseDelegateType = GetReverseDelegateType(ArrayUtils.RemoveFirst(sigTypes), CallingConvention);
_reverseDelegate = dm;
}
private void GetSignatureInfo(out Type[] sigTypes, out Type[] callSiteType, out Type retType) {
sigTypes = new Type[_argtypes.Length + 2]; // constant pool, args ..., ret type
callSiteType = new Type[_argtypes.Length + 4]; // CallSite, context, target, args ..., ret type
sigTypes[0] = typeof(object[]);
callSiteType[0] = typeof(CallSite);
callSiteType[1] = typeof(CodeContext);
callSiteType[2] = typeof(object);
callSiteType[callSiteType.Length - 1] = typeof(object);
for (int i = 0; i < _argtypes.Length; i++) {
sigTypes[i + 1] = _argtypes[i].GetNativeType();
Debug.Assert(sigTypes[i + 1] != typeof(object));
callSiteType[i + 3] = _argtypes[i].GetPythonType();
}
if (_restype != null) {
sigTypes[sigTypes.Length - 1] = retType = ((INativeType)_restype).GetNativeType();
} else {
sigTypes[sigTypes.Length - 1] = retType = typeof(void);
}
}
private static Type GetReverseDelegateType(Type[] nativeSig, CallingConvention callingConvention) {
Type res;
lock (_reverseDelegates) {
DelegateCacheKey key = new DelegateCacheKey(nativeSig, callingConvention);
if (!_reverseDelegates.TryGetValue(key, out res)) {
res = _reverseDelegates[key] = PythonOps.MakeNewCustomDelegate(nativeSig, callingConvention);
}
}
return res;
}
private struct DelegateCacheKey : IEquatable<DelegateCacheKey> {
private readonly Type[] _types;
private readonly CallingConvention _callConv;
public DelegateCacheKey(Type[] sig, CallingConvention callingConvention) {
Assert.NotNullItems(sig);
_types = sig;
_callConv = callingConvention;
}
public override int GetHashCode() {
int res = _callConv.GetHashCode();
for (int i = 0; i < _types.Length; i++) {
res ^= _types[i].GetHashCode();
}
return res;
}
public override bool Equals(object obj) {
if (obj is DelegateCacheKey) {
return Equals((DelegateCacheKey)obj);
}
return false;
}
#region IEquatable<DelegateCacheKey> Members
public bool Equals(DelegateCacheKey other) {
if (other._types.Length != _types.Length ||
other._callConv != _callConv) {
return false;
}
for (int i = 0; i < _types.Length; i++) {
if (_types[i] != other._types[i]) {
return false;
}
}
return true;
}
#endregion
}
}
}
}
#endif