forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathField.cs
More file actions
286 lines (240 loc) · 11.2 KB
/
Field.cs
File metadata and controls
286 lines (240 loc) · 11.2 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
// 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.Diagnostics;
using System.Numerics;
using Microsoft.Scripting.Runtime;
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>
/// Fields are created when a Structure is defined and provide
/// introspection of the structure.
/// </summary>
[PythonType, PythonHidden]
public sealed class Field : PythonTypeDataSlot, ICodeFormattable {
private readonly INativeType _fieldType;
private readonly int _offset, _index, _bits, _bitsOffset;
private readonly string _fieldName;
internal Field(string fieldName, INativeType fieldType, int offset, int index) {
_offset = offset;
_fieldType = fieldType;
_index = index;
_fieldName = fieldName;
}
internal Field(string fieldName, INativeType fieldType, int offset, int index, int? bits, int? bitOffset) {
_offset = offset;
_fieldType = fieldType;
_index = index;
_fieldName = fieldName;
if (bits != null) {
Debug.Assert(bitOffset != null);
Debug.Assert(_fieldType is SimpleType);
_bits = bits.Value;
_bitsOffset = bitOffset.Value;
if (((SimpleType)_fieldType)._swap) {
_bitsOffset = _fieldType.Size * 8 - _bitsOffset - _bits;
}
}
}
public int offset => _offset;
public int size => _bits == 0 ? _fieldType.Size : (_bits << 16) + _bitsOffset;
#region Internal APIs
internal override bool TryGetValue(CodeContext context, object instance, PythonType owner, out object value) {
if (instance != null) {
CData inst = (CData)instance;
if (!IsProperBitfield) {
value = _fieldType.GetValue(inst.MemHolder, inst, _offset, false);
return true;
} else {
value = ExtractBits(inst);
return true;
}
}
value = this;
return true;
}
internal override bool GetAlwaysSucceeds => true;
internal override bool TrySetValue(CodeContext context, object instance, PythonType owner, object value) {
if (instance != null) {
SetValue(((CData)instance).MemHolder, 0, value);
return true;
}
return base.TrySetValue(context, instance, owner, value);
}
internal void SetValue(MemoryHolder address, int baseOffset, object value) {
if (!IsProperBitfield) {
object keepAlive = _fieldType.SetValue(address, baseOffset + _offset, value);
if (keepAlive != null) {
address.AddObject(_index.ToString(), keepAlive);
}
} else {
SetBitsValue(address, baseOffset, value);
}
}
internal override bool TryDeleteValue(CodeContext context, object instance, PythonType owner) {
throw PythonOps.TypeError("cannot delete fields in ctypes structures/unions");
}
internal INativeType NativeType => _fieldType;
internal int? BitCount => _bits == 0 ? null : _bits;
internal string FieldName => _fieldName;
#endregion
#region ICodeFormattable Members
public string __repr__(CodeContext context) {
if (_bits == 0) {
return string.Format("<Field type={0}, ofs={1}, size={2}>", ((PythonType)_fieldType).Name, offset, size);
}
return string.Format("<Field type={0}, ofs={1}:{2}, bits={3}>", ((PythonType)_fieldType).Name, offset, _bitsOffset, _bits);
}
#endregion
#region Private implementation
/// <summary>
/// Called for fields which have been limited to a range of bits. Given the
/// value for the full type this extracts the individual bits.
/// </summary>
private object ExtractBits(CData instance) {
Debug.Assert(_bits > 0);
Debug.Assert(_bits < sizeof(ulong) * 8); // Avoid unspecified shifts: ECMA 335: III.3.58-59
Debug.Assert(instance != null);
object value;
if (IsBoolType) {
int valueBits = instance.MemHolder.ReadByte(offset);
int ibVal = ExtractBitsFromInt(valueBits);
value = ScriptingRuntimeHelpers.BooleanToObject(ibVal != 0);
} else {
value = _fieldType.GetValue(instance.MemHolder, instance, _offset, false);
if (value is int iVal) {
iVal = ExtractBitsFromInt(iVal);
value = ScriptingRuntimeHelpers.Int32ToObject(iVal);
} else if (value is BigInteger biVal) {
ulong validBits = (1UL << _bits) - 1;
ulong bits;
if (IsSignedType) {
bits = (ulong)(long)biVal;
} else {
bits = (ulong)biVal;
}
bits = (bits >> _bitsOffset) & validBits;
if (IsSignedType) {
// need to sign extend if high bit is set
if ((bits & (1UL << (_bits - 1))) != 0) {
bits |= ulong.MaxValue ^ validBits;
}
value = (BigInteger)(long)bits;
} else {
value = (BigInteger)bits;
}
} else {
throw new InvalidOperationException("we only return int, bigint from GetValue");
}
}
return value;
int ExtractBitsFromInt(int iVal) {
int validBits = ((1 << _bits) - 1);
iVal = (iVal >> _bitsOffset) & validBits;
if (IsSignedType) {
// need to sign extend if high bit is set
if ((iVal & (1 << (_bits - 1))) != 0) {
iVal |= (-1) ^ validBits;
}
}
return iVal;
}
}
/// <summary>
/// Called for fields which have been limited to a range of bits. Sets the
/// specified value into the bits for the field.
/// </summary>
private void SetBitsValue(MemoryHolder address, int baseOffset, object value) {
// get the value in the form of a ulong which can contain the biggest bitfield
ulong newBits;
if (IsBoolType) {
newBits = PythonOps.IsTrue(value) ? 1ul : 0ul;
} else {
value = PythonOps.Index(value); // since 3.8
if (value is int iVal) {
newBits = (ulong)iVal;
} else if (value is BigInteger biVal) {
if (biVal.Sign >= 0) {
if (biVal <= ulong.MaxValue) {
newBits = (ulong)biVal;
} else {
newBits = (ulong)(biVal & ulong.MaxValue);
}
} else {
if (biVal >= long.MinValue) {
newBits = (ulong)(long)biVal;
} else {
newBits = (ulong)(biVal & ulong.MaxValue);
}
}
} else {
throw new InvalidOperationException("unreachable");
}
}
// do the same for the existing value
int offset = checked(_offset + baseOffset);
ulong valueBits;
if (IsBoolType) {
valueBits = address.ReadByte(offset);
} else {
object curValue = _fieldType.GetValue(address, null, offset, false);
if (curValue is int iCurVal) {
valueBits = (ulong)iCurVal;
} else {
valueBits = (ulong)(long)(BigInteger)curValue;
}
}
// get a mask for the bits this field owns
ulong targetBits = ((1UL << _bits) - 1) << _bitsOffset;
// clear the existing bits
valueBits &= ~targetBits;
// or in the new bits provided by the user
valueBits |= (newBits << _bitsOffset) & targetBits;
// and set the value
if (IsBoolType) {
address.WriteByte(offset, (byte)valueBits);
} else if (IsSignedType) {
if (_fieldType.Size <= 4) {
_fieldType.SetValue(address, offset, (int)(long)valueBits);
} else {
_fieldType.SetValue(address, offset, (BigInteger)(long)valueBits);
}
} else {
if (_fieldType.Size < 4) {
_fieldType.SetValue(address, offset, (int)valueBits);
} else {
_fieldType.SetValue(address, offset, (BigInteger)valueBits);
}
}
}
private bool IsSignedType {
get {
switch (((SimpleType)_fieldType)._type) {
case SimpleTypeKind.SignedByte:
case SimpleTypeKind.SignedInt:
case SimpleTypeKind.SignedLong:
case SimpleTypeKind.SignedLongLong:
case SimpleTypeKind.SignedShort:
return true;
}
return false;
}
}
private bool IsBoolType => ((SimpleType)_fieldType)._type is SimpleTypeKind.Boolean;
/// <summary>
/// Returns true if the field is a bitfield that does not span the whole width of the underlying field type.
/// </summary>
private bool IsProperBitfield => 0 < _bits && _bits < _fieldType.Size * 8;
#endregion
}
}
}
#endif