forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclrmodule.pp.il
More file actions
271 lines (233 loc) · 9.39 KB
/
clrmodule.pp.il
File metadata and controls
271 lines (233 loc) · 9.39 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
//============================================================================
// This file is a hand-maintained stub - it implements clr.dll, which can be
// loaded by a standard CPython interpreter as an extension module. When it
// is loaded, it bootstraps the managed runtime integration layer and defers
// to it to do initialization and put the clr module into sys.modules, etc.
// The "USE_PYTHON_RUNTIME_*" defines control what extra evidence is used
// to help the CLR find the appropriate Python.Runtime assembly.
// If defined, the "pythonRuntimeVersionString" variable must be set to
// Python.Runtime's current version.
#define USE_PYTHON_RUNTIME_VERSION
// If defined, the "PythonRuntimePublicKeyTokenData" data array must be
// set to Python.Runtime's public key token.
//#define USE_PYTHON_RUNTIME_PUBLIC_KEY_TOKEN
// If DEBUG_PRINT is defined, a few System.Console.WriteLine calls are made
// to indicate what's going on during the load...
//#define DEBUG_PRINT
//============================================================================
.assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 )
.ver 2:0:0:0
}
.assembly clr
{
.hash algorithm 0x00008004
.ver 2:0:0:2
}
.module clr.dll
.imagebase 0x00400000
.subsystem 0x00000003
.file alignment 512
// This includes the platform-specific IL. The include search path
// is set depending on whether we're compiling 32 or 64 bit.
// This MUST come before any other .data directives!
// Why, oh why, can't ilasm support command line #defines? :(
// Contributed by VIKAS DHIMAN - Handled by /home/barton/Projects/PyDotNet/pythonnet/makefile
// gcc -C -P -x c++ -I $(ARCH) clrmodule.pp.il -o clrmodule.il
// to copy the correct architecture to the clrModule.
// Nice formating, as well - Thanks, Vikas!
#include "clrmodule-platform.il"
#ifdef USE_PYTHON_RUNTIME_PUBLIC_KEY_TOKEN
.data PythonRuntimePublicKeyTokenData = bytearray (64 e1 4e 84 5a bf 2e 60)
#endif
.class public auto ansi beforefieldinit clrModule extends [mscorlib]System.Object
{
#ifdef USE_PYTHON_RUNTIME_PUBLIC_KEY_TOKEN
.field static assembly int64 PythonRuntimePublicKeyToken at PythonRuntimePublicKeyTokenData
#endif
.method public hidebysig specialname rtspecialname instance void
.ctor() cil managed
{
.maxstack 1
ldarg.0
call instance void [mscorlib]System.Object::.ctor()
ret
}
.method public hidebysig static void modopt([mscorlib]System.Runtime.CompilerServices.CallConvCdecl)
initclr() cil managed
{
.vtentry 1:1
.export [1] as initclr
.maxstack 6
.locals init (
class [mscorlib]System.Reflection.Assembly pythonRuntime,
class [mscorlib]System.Reflection.Assembly executingAssembly,
class [mscorlib]System.Reflection.AssemblyName pythonRuntimeName,
class [mscorlib]System.Type pythonEngineType,
int8[] publicKeyToken,
string assemblyDirectory,
string pythonRuntimeVersionString,
string pythonRuntimeDllPath)
// pythonRuntime = null;
ldnull
stloc pythonRuntime
.try
{
#ifdef DEBUG_PRINT
ldstr "Attempting to load Python.Runtime using standard binding rules... "
call void [mscorlib]System.Console::Write(string)
#endif
// Attempt to find and load Python.Runtime using standard assembly binding rules.
// This roughly translates into looking in order:
// - GAC
// - ApplicationBase
// - A PrivateBinPath under ApplicationBase
// With an unsigned assembly, the GAC is skipped.
// System.Reflection.AssemblyName pythonRuntimeName = new System.Reflection.AssemblyName();
newobj instance void [mscorlib]System.Reflection.AssemblyName::.ctor()
stloc pythonRuntimeName
// pythonRuntimeName.Name = "Python.Runtime";
ldloc pythonRuntimeName
ldstr "Python.Runtime"
callvirt instance void [mscorlib]System.Reflection.AssemblyName::set_Name(string)
#ifdef USE_PYTHON_RUNTIME_VERSION
// pythonRuntimeVersionString = "...";
ldstr "2.0.0.2"
stloc pythonRuntimeVersionString
// pythonRuntimeName.Version = new Version(pythonRuntimeVersionString);
ldloc pythonRuntimeName
ldloc pythonRuntimeVersionString
newobj instance void [mscorlib]System.Version::.ctor(string)
callvirt instance void [mscorlib]System.Reflection.AssemblyName::set_Version(class [mscorlib]System.Version)
#endif
#ifdef USE_PYTHON_RUNTIME_PUBLIC_KEY_TOKEN
// publicKeyToken = new byte[] { ... };
ldc.i4.8
newarr [mscorlib]System.Byte
dup
ldtoken field int64 clrModule::PythonRuntimePublicKeyToken
call void [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(class [mscorlib]System.Array, valuetype [mscorlib]System.RuntimeFieldHandle)
stloc publicKeyToken
// pythonRuntimeName.SetPublicKeyToken(publicKeyToken);
ldloc pythonRuntimeName
ldloc publicKeyToken
callvirt instance void [mscorlib]System.Reflection.AssemblyName::SetPublicKeyToken(uint8[])
#endif
// pythonRuntimeName.CultureInfo = System.Globalization.CultureInfo.InvariantCulture;
ldloc pythonRuntimeName
call class [mscorlib]System.Globalization.CultureInfo [mscorlib]System.Globalization.CultureInfo::get_InvariantCulture()
callvirt instance void [mscorlib]System.Reflection.AssemblyName::set_CultureInfo(class [mscorlib]System.Globalization.CultureInfo)
// return System.Reflection.Assembly.Load(pythonRuntimeName);
ldloc pythonRuntimeName
call class [mscorlib]System.Reflection.Assembly [mscorlib]System.Reflection.Assembly::Load(class [mscorlib]System.Reflection.AssemblyName)
stloc pythonRuntime
#ifdef DEBUG_PRINT
ldstr "Success!"
call void [mscorlib]System.Console::WriteLine(string)
#endif
leave.s LOADED_PYTHON_RUNTIME
}
catch [mscorlib]System.Object
{
#ifdef DEBUG_PRINT
ldstr "Failed."
call void [mscorlib]System.Console::WriteLine(string)
#endif
leave.s EXIT_CLR_LOAD
}
EXIT_CLR_LOAD: nop
.try
{
// If the above fails for any reason, we fallback to attempting to load "Python.Runtime.dll"
// from the directory this assembly is running in. "This assembly" is probably "clr.pyd",
// sitting somewhere in PYTHONPATH. This is using Assembly.LoadFrom, and inherits all the
// caveats of that call. See MSDN docs for details.
// Suzanne Cook's blog is also an excellent source of info on this:
// http://blogs.msdn.com/suzcook/
// http://blogs.msdn.com/suzcook/archive/2003/05/29/57143.aspx
// http://blogs.msdn.com/suzcook/archive/2003/06/13/57180.aspx
// executingAssembly = System.Reflection.Assembly.GetExecutingAssembly();
call class [mscorlib]System.Reflection.Assembly [mscorlib]System.Reflection.Assembly::GetExecutingAssembly()
stloc executingAssembly
// assemblyDirectory = System.IO.Path.GetDirectoryName(executingAssembly.Location);
ldloc executingAssembly
callvirt instance string [mscorlib]System.Reflection.Assembly::get_Location()
call string [mscorlib]System.IO.Path::GetDirectoryName(string)
stloc assemblyDirectory
// pythonRuntimeDllPath = System.IO.Path.Combine(assemblyDirectory, "Python.Runtime.dll");
ldloc assemblyDirectory
ldstr "Python.Runtime.dll"
call string [mscorlib]System.IO.Path::Combine(string, string)
stloc pythonRuntimeDllPath
#ifdef DEBUG_PRINT
ldstr "Attempting to load Python.Runtime from: '{0}'... "
ldloc pythonRuntimeDllPath
call void [mscorlib]System.Console::Write(string, object)
#endif
// pythonRuntime = System.Reflection.Assembly.LoadFrom(pythonRuntimeDllPath);
ldloc pythonRuntimeDllPath
call class [mscorlib]System.Reflection.Assembly [mscorlib]System.Reflection.Assembly::LoadFrom(string)
stloc pythonRuntime
#ifdef DEBUG_PRINT
ldstr "Success!"
call void [mscorlib]System.Console::WriteLine(string)
#endif
leave.s LOADED_PYTHON_RUNTIME
}
catch [mscorlib]System.Object
{
#ifdef DEBUG_PRINT
ldstr "Failed."
call void [mscorlib]System.Console::WriteLine(string)
#endif
leave.s EXIT_PYTHONPATH_LOAD
}
EXIT_PYTHONPATH_LOAD: nop
// If we get here, we haven't loaded Python.Runtime, so bail.
#ifdef DEBUG_PRINT
ldstr "Could not load Python.Runtime, so sad."
call void [mscorlib]System.Console::WriteLine(string)
#endif
ret
// Once here, we've successfully loaded SOME version of Python.Runtime
// So now we get the PythonEngine and execute the InitExt method on it.
LOADED_PYTHON_RUNTIME: nop
.try
{
#ifdef DEBUG_PRINT
ldstr "Running Python.Runtime.PythonEngine.InitExt()"
call void [mscorlib]System.Console::WriteLine(string)
#endif
// pythonEngineType = pythonRuntime.GetType("Python.Runtime.PythonEngine");
ldloc pythonRuntime
ldstr "Python.Runtime.PythonEngine"
callvirt instance class [mscorlib]System.Type [mscorlib]System.Reflection.Assembly::GetType(string)
stloc pythonEngineType
// pythonEngineType.InvokeMember("InitExt", System.Reflection.BindingFlags.InvokeMethod, null, null, null);
ldloc pythonEngineType
ldstr "InitExt"
ldc.i4 0x100
ldnull
ldnull
ldnull
callvirt instance object [mscorlib]System.Type::InvokeMember( string,
valuetype [mscorlib]System.Reflection.BindingFlags,
class [mscorlib]System.Reflection.Binder,
object,
object[])
pop
leave.s EXIT_TRY_INVOKE
}
catch [mscorlib]System.Object
{
#ifdef DEBUG_PRINT
ldstr "Error calling Python.Runtime.PythonEngine.InitExt()."
call void [mscorlib]System.Console::WriteLine(string)
#endif
leave.s EXIT_TRY_INVOKE
}
EXIT_TRY_INVOKE: nop
ret
}
}