Merge pull request #9522 from sandreenko/GVM-for-corert
[platform/upstream/coreclr.git] / src / vm / mscorlib.cpp
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 //
5
6
7 // 
8 // This file defines tables for references between VM and mscorlib.
9 //
10 // When compiling crossgen, this file is compiled with the FEATURE_XXX define settings matching the target.
11 // It allows us to strip features (e.g. refection only load) from crossgen without stripping them from the target.
12 //
13
14 #ifdef CROSSGEN_MSCORLIB
15 // Use minimal set of headers for crossgen
16 #include "windows.h"
17 #include "corinfo.h"
18 #else
19 #include "common.h"
20 #include "ecall.h"
21 #endif // CROSSGEN_MSCORLIB
22
23 #ifndef CROSSGEN_COMPILE
24 //
25 // Headers for all ECall entrypoints
26 //
27 #include "arraynative.h"
28 #include "stringnative.h"
29 #include "stringbuffer.h"
30 #include "objectnative.h"
31 #include "comdelegate.h"
32 #include "customattribute.h"
33 #include "comdynamic.h"
34 #include "excep.h"
35 #include "fcall.h"
36 #include "nlsinfo.h"
37 #include "calendardata.h"
38 #include "commodule.h"
39 #include "marshalnative.h"
40 #include "system.h"
41 #include "comutilnative.h"
42 #include "comsynchronizable.h"
43 #include "floatdouble.h"
44 #include "floatsingle.h"
45 #include "decimal.h"
46 #include "currency.h"
47 #include "comdatetime.h"
48 #include "comisolatedstorage.h"
49 #include "number.h"
50 #include "compatibilityswitch.h"
51 #ifdef FEATURE_REMOTING
52 #include "remotingnative.h"
53 #include "message.h"
54 #include "stackbuildersink.h"
55 #endif
56 #include "debugdebugger.h"
57 #include "assemblyname.hpp"
58 #include "assemblynative.hpp"
59 #include "rwlock.h"
60 #include "comthreadpool.h"
61 #include "comwaithandle.h"
62 #include "nativeoverlapped.h"
63
64 #include "proftoeeinterfaceimpl.h"
65
66 #include "appdomainnative.hpp"
67 #include "arrayhelpers.h"
68 #include "runtimehandles.h"
69 #include "reflectioninvocation.h"
70 #include "managedmdimport.hpp"
71 #include "synchronizationcontextnative.h"
72 #include "commemoryfailpoint.h"
73 #include "typestring.h"
74 #include "comdependenthandle.h"
75 #include "weakreferencenative.h"
76 #include "varargsnative.h"
77
78 #ifdef MDA_SUPPORTED 
79 #include "mdaassistants.h"
80 #endif
81
82 #include "coverage.h"
83
84 #ifdef FEATURE_COMINTEROP
85 #include "variant.h"
86 #include "oavariant.h"
87 #include "registration.h"
88 #include "mngstdinterfaces.h"
89 #include "extensibleclassfactory.h"
90 #endif // FEATURE_COMINTEROP
91
92 #include "stubhelpers.h"
93 #include "ilmarshalers.h"
94
95 #ifdef FEATURE_MULTICOREJIT
96 #include "multicorejit.h"
97 #endif
98
99 #if defined(FEATURE_COMINTEROP) && defined(FEATURE_REFLECTION_ONLY_LOAD)
100 #include "clrprivtypecachereflectiononlywinrt.h"
101 #endif
102
103 #ifdef FEATURE_COMINTEROP
104 #include "windowsruntimebufferhelper.h"
105 #endif
106
107 #if defined(FEATURE_EVENTSOURCE_XPLAT)
108 #include "nativeeventsource.h"
109 #endif //defined(FEATURE_EVENTSOURCE_XPLAT)
110
111 #endif // CROSSGEN_MSCORLIB
112
113
114 #ifdef CROSSGEN_MSCORLIB
115
116 ///////////////////////////////////////////////////////////////////////////////
117 //
118 // Duplicate definitions of constants and datastructures required to define the tables
119 //
120
121 #define NumItems(s) (sizeof(s) / sizeof(s[0]))
122
123 #define GetEEFuncEntryPoint(pfn) 0x1001
124
125 enum {
126     FCFuncFlag_EndOfArray   = 0x01,
127     FCFuncFlag_HasSignature = 0x02,
128     FCFuncFlag_Unreferenced = 0x04, // Suppress unused fcall check
129     FCFuncFlag_QCall        = 0x08, // QCall - mscorlib.dll to mscorwks.dll transition implemented as PInvoke
130 };
131
132 struct ECClass
133 {
134     LPCSTR      m_szClassName;
135     LPCSTR      m_szNameSpace;
136     const LPVOID *  m_pECFunc;
137 };
138
139 struct HardCodedMetaSig
140 {
141     const BYTE* m_pMetaSig; // metasig prefixed with INT8 length:
142                             // length > 0 - resolved, lenght < 0 - has unresolved type references
143 };
144
145 enum BinderClassID
146 {
147 #define TYPEINFO(e,ns,c,s,g,ia,ip,if,im,gv)   CLASS__ ## e,
148 #include "cortypeinfo.h"
149 #undef TYPEINFO
150
151 #define DEFINE_CLASS(i,n,s)         CLASS__ ## i,
152 #include "mscorlib.h"
153
154     CLASS__MSCORLIB_COUNT,
155
156     CLASS__VOID     = CLASS__ELEMENT_TYPE_VOID,
157     CLASS__BOOLEAN  = CLASS__ELEMENT_TYPE_BOOLEAN,
158     CLASS__CHAR     = CLASS__ELEMENT_TYPE_CHAR,
159     CLASS__BYTE     = CLASS__ELEMENT_TYPE_U1,
160     CLASS__SBYTE    = CLASS__ELEMENT_TYPE_I1,
161     CLASS__INT16    = CLASS__ELEMENT_TYPE_I2,
162     CLASS__UINT16   = CLASS__ELEMENT_TYPE_U2,
163     CLASS__INT32    = CLASS__ELEMENT_TYPE_I4,
164     CLASS__UINT32   = CLASS__ELEMENT_TYPE_U4,
165     CLASS__INT64    = CLASS__ELEMENT_TYPE_I8,
166     CLASS__UINT64   = CLASS__ELEMENT_TYPE_U8,
167     CLASS__SINGLE   = CLASS__ELEMENT_TYPE_R4,
168     CLASS__DOUBLE   = CLASS__ELEMENT_TYPE_R8,
169     CLASS__STRING   = CLASS__ELEMENT_TYPE_STRING,
170     CLASS__TYPED_REFERENCE = CLASS__ELEMENT_TYPE_TYPEDBYREF,
171     CLASS__INTPTR   = CLASS__ELEMENT_TYPE_I,
172     CLASS__UINTPTR  = CLASS__ELEMENT_TYPE_U,
173     CLASS__OBJECT   = CLASS__ELEMENT_TYPE_OBJECT
174 };
175
176 struct MscorlibClassDescription
177 {
178     LPCSTR  nameSpace;
179     LPCSTR  name;
180 };
181
182 struct MscorlibMethodDescription
183 {
184     BinderClassID classID;
185     LPCSTR  name;
186     const HardCodedMetaSig * sig;
187 };
188
189 struct MscorlibFieldDescription
190 {
191     BinderClassID classID;
192     LPCSTR  name;
193 };
194
195 #endif // CROSSGEN_MSCORLIB
196
197
198 #ifdef CROSSGEN_MSCORLIB
199 // When compiling crossgen this namespace creates the second version of the tables than matches the target
200 namespace CrossGenMscorlib {
201 #endif
202
203
204 ///////////////////////////////////////////////////////////////////////////////
205 //
206 // Hardcoded Meta-Sig
207 //
208
209 //
210 // Helper enum with metasig lengths
211 //
212 // iterate over the metasig recursing into the complex types
213 #define DEFINE_METASIG(body)            body,
214 #define METASIG_ATOM(x)                 + 1
215 #define METASIG_RECURSE                 1
216 #define SM(varname, args, retval)       gsigl_SM_ ## varname = 1 + 1 retval args
217 #define IM(varname, args, retval)       gsigl_IM_ ## varname = 1 + 1 retval args
218 #define GM(varname, conv, n, args, retval) gsigl_GM_ ## varname = 1 + 1 + 1 + retval args
219 #define Fld(varname, val)               gsigl_Fld_ ## varname = 1 val
220 enum _gsigl {
221 #include "metasig.h"
222 };
223
224 //
225 // Helper enum with metasig argcount
226 //
227 // iterate over the metasig without recursing into the complex types
228 #define DEFINE_METASIG(body)            body,
229 #define METASIG_ATOM(x)                 + 1
230 #define METASIG_RECURSE                 0
231 #define SM(varname, args, retval)       gsigc_SM_ ## varname = 0 args
232 #define IM(varname, args, retval)       gsigc_IM_ ## varname = 0 args
233 #define GM(varname, conv, n, args, retval) gsigc_GM_ ## varname = 0 args
234 #define Fld(varname, val)               gsigc_Fld_ ## varname = 0
235 enum _gsigc {
236 #include "metasig.h"
237 };
238
239
240 //
241 // The actual array with the hardcoded metasig:
242 //
243 // There are 3 variations of the macros for Fields, Static Methods and Instance Methods.
244 //
245 // Each of them has 2 flavors: one for the fully baked signatures, and the other 
246 // for the signatures with unresolved type references
247 //
248 // The signatures with unresolved type references are marked with negative size, 
249 // and the pointer to them is non-const because of it will be overwritten with
250 // the pointer to the resolved signature at runtime.
251 //
252
253 #define DEFINE_METASIG(body)            body
254 #define DEFINE_METASIG_T(body)          _##body
255 #define METASIG_ATOM(x)                 x,
256 #define METASIG_RECURSE                 1
257
258 // define gsig_ ## varname before gsige_ ## varname to give a hint to the compiler about the desired layout
259
260 #define SM(varname, args, retval) extern const BYTE gsige_SM_ ## varname[];     \
261     const HardCodedMetaSig gsig_SM_ ## varname = { gsige_SM_ ## varname };      \
262     const BYTE gsige_SM_ ## varname[] = { gsigl_SM_ ## varname,                 \
263         IMAGE_CEE_CS_CALLCONV_DEFAULT, gsigc_SM_ ## varname, retval args };
264
265 #define IM(varname, args, retval) extern const BYTE gsige_IM_ ## varname[];     \
266     const HardCodedMetaSig gsig_IM_ ## varname = { gsige_IM_ ## varname };      \
267     const BYTE gsige_IM_ ## varname[] = { gsigl_IM_ ## varname,                 \
268         IMAGE_CEE_CS_CALLCONV_HASTHIS, gsigc_IM_ ## varname, retval args };
269
270 #define GM(varname, conv, n, args, retval) extern const BYTE gsige_GM_ ## varname[];    \
271     const HardCodedMetaSig gsig_GM_ ## varname = { gsige_GM_ ## varname };              \
272     const BYTE gsige_GM_ ## varname[] = { gsigl_GM_ ## varname,                         \
273         conv | IMAGE_CEE_CS_CALLCONV_GENERIC, n, gsigc_GM_ ## varname, retval args };
274
275 #define Fld(varname, val) extern const BYTE gsige_Fld_ ## varname[];            \
276     const HardCodedMetaSig gsig_Fld_ ## varname = { gsige_Fld_ ## varname };    \
277     const BYTE gsige_Fld_ ## varname[] = { gsigl_Fld_ ## varname,               \
278         IMAGE_CEE_CS_CALLCONV_FIELD, val };
279
280 #define _SM(varname, args, retval) extern const BYTE gsige_SM_ ## varname[];    \
281     HardCodedMetaSig gsig_SM_ ## varname = { gsige_SM_ ## varname };            \
282     const BYTE gsige_SM_ ## varname[] = { (BYTE) -gsigl_SM_ ## varname,         \
283         IMAGE_CEE_CS_CALLCONV_DEFAULT, gsigc_SM_ ## varname, retval args };
284
285 #define _IM(varname, args, retval) extern const BYTE gsige_IM_ ## varname[];    \
286     HardCodedMetaSig gsig_IM_ ## varname = { gsige_IM_ ## varname };            \
287     const BYTE gsige_IM_ ## varname[] = { (BYTE) -gsigl_IM_ ## varname,         \
288         IMAGE_CEE_CS_CALLCONV_HASTHIS, gsigc_IM_ ## varname, retval args };
289
290 #define _Fld(varname, val) extern const BYTE gsige_Fld_ ## varname[];           \
291     HardCodedMetaSig gsig_Fld_ ## varname = { gsige_Fld_ ## varname };          \
292     const BYTE gsige_Fld_ ## varname[] = { (BYTE) -gsigl_Fld_ ## varname,       \
293         IMAGE_CEE_CS_CALLCONV_FIELD, val };
294
295 #include "metasig.h"
296
297 #undef _SM
298 #undef _IM
299 #undef _Fld
300
301
302
303 #ifdef _DEBUG
304
305 //
306 // Make sure DEFINE_METASIG is used for signatures that do not reference other types
307 //
308 // counts number of type references in the signature and C_ASSERTs that 
309 // it is zero. An assertion failure results in error C2118: negative subscript.
310 #define DEFINE_METASIG(body)            body
311 #define DEFINE_METASIG_T(body)
312 #define METASIG_BODY(varname, types)    C_ASSERT(types 0 == 0);
313 #define METASIG_ATOM(x)                 0+
314 #define METASIG_RECURSE                 1
315 #define C(x)                            1+
316 #define g(x)                            1+
317 #include "metasig.h"
318
319 //
320 // Make sure DEFINE_METASIG_T is used only for signatures that reference 
321 // other types.
322 //
323 // counts number of type references in the signature and C_ASSERTs that 
324 // it is non zero. An assertion failure results in error C2118: negative subscript.
325 #define DEFINE_METASIG(body)
326 #define DEFINE_METASIG_T(body)          body
327 #define METASIG_BODY(varname, types)    C_ASSERT(types 0 != 0);
328 #define METASIG_ATOM(x)                 0+
329 #define METASIG_RECURSE                 1
330 #define C(x)                            1+
331 #define g(x)                            1+
332 #include "metasig.h"
333
334 #endif
335
336
337
338
339
340 ///////////////////////////////////////////////////////////////////////////////
341 //
342 // Mscorlib binder
343 //
344
345 // Extern definitions so that binder.cpp can see these tables
346 extern const MscorlibClassDescription c_rgMscorlibClassDescriptions[];
347 extern const USHORT c_nMscorlibClassDescriptions;
348
349 extern const MscorlibMethodDescription c_rgMscorlibMethodDescriptions[];
350 extern const USHORT c_nMscorlibMethodDescriptions;
351
352 extern const MscorlibFieldDescription c_rgMscorlibFieldDescriptions[];
353 extern const USHORT c_nMscorlibFieldDescriptions;
354
355 const MscorlibClassDescription c_rgMscorlibClassDescriptions[] =
356 {
357     #define TYPEINFO(e,ns,c,s,g,ia,ip,if,im,gv)   { ns, c },
358     #include "cortypeinfo.h"
359     #undef TYPEINFO
360     
361     #define DEFINE_CLASS(i,n,s)        { g_ ## n ## NS, # s },
362     #include "namespace.h"
363     #include "mscorlib.h"
364     
365     // Include all exception types here that are defined in mscorlib.  Omit exceptions defined elsewhere.
366     #define DEFINE_EXCEPTION(ns, reKind, bHRformessage, ...) { ns , # reKind },
367     #define DEFINE_EXCEPTION_HR_WINRT_ONLY(ns, reKind, ...)
368     #define DEFINE_EXCEPTION_IN_OTHER_FX_ASSEMBLY(ns, reKind, assemblySimpleName, publicKeyToken, bHRformessage, ...)
369     #include "rexcep.h"
370 };
371 const USHORT c_nMscorlibClassDescriptions = NumItems(c_rgMscorlibClassDescriptions);
372
373 #define gsig_NoSig (*(HardCodedMetaSig *)NULL)
374
375 const MscorlibMethodDescription c_rgMscorlibMethodDescriptions[] =
376 {
377     #define DEFINE_METHOD(c,i,s,g)          { CLASS__ ## c , # s, & gsig_ ## g },
378     #include "mscorlib.h"
379 };
380 const USHORT c_nMscorlibMethodDescriptions = NumItems(c_rgMscorlibMethodDescriptions) + 1;
381
382 const MscorlibFieldDescription c_rgMscorlibFieldDescriptions[] =
383 {
384     #define DEFINE_FIELD(c,i,s)           { CLASS__ ## c , # s },
385     #include "mscorlib.h"
386 };
387 const USHORT c_nMscorlibFieldDescriptions = NumItems(c_rgMscorlibFieldDescriptions) + 1;
388
389
390
391
392
393 ///////////////////////////////////////////////////////////////////////////////
394 //
395 // ECalls
396 //
397
398 // When compiling crossgen, we only need the target version of the ecall tables
399 #if !defined(CROSSGEN_COMPILE) || defined(CROSSGEN_MSCORLIB)
400
401 #ifdef CROSSGEN_COMPILE
402
403 #define QCFuncElement(name,impl) \
404     FCFuncFlag_QCall + FCFuncFlags(CORINFO_INTRINSIC_Illegal, ECall::InvalidDynamicFCallId), NULL, (LPVOID)name,
405
406 #define FCFuncFlags(intrinsicID, dynamicID) \
407     (BYTE*)( (((BYTE)intrinsicID) << 16) )
408
409 #else
410
411 #define QCFuncElement(name,impl) \
412     FCFuncFlag_QCall + FCFuncFlags(CORINFO_INTRINSIC_Illegal, ECall::InvalidDynamicFCallId), (LPVOID)(impl), (LPVOID)name,
413
414 #define FCFuncFlags(intrinsicID, dynamicID) \
415     (BYTE*)( (((BYTE)intrinsicID) << 16) + (((BYTE)dynamicID) << 24) )
416
417 #endif
418
419 #define FCFuncElement(name, impl) FCFuncFlags(CORINFO_INTRINSIC_Illegal, ECall::InvalidDynamicFCallId), \
420     (LPVOID)GetEEFuncEntryPoint(impl), (LPVOID)name,
421
422 #define FCFuncElementSig(name,sig,impl) \
423     FCFuncFlag_HasSignature + FCFuncElement(name, impl) (LPVOID)sig,
424
425 #define FCIntrinsic(name,impl,intrinsicID) FCFuncFlags(intrinsicID, ECall::InvalidDynamicFCallId), \
426     (LPVOID)GetEEFuncEntryPoint(impl), (LPVOID)name,
427
428 #define FCIntrinsicSig(name,sig,impl,intrinsicID) \
429     FCFuncFlag_HasSignature + FCIntrinsic(name,impl,intrinsicID) (LPVOID)sig,
430
431 #define FCDynamic(name,intrinsicID,dynamicID) FCFuncFlags(intrinsicID, dynamicID), \
432     NULL, (LPVOID)name,
433
434 #define FCDynamicSig(name,sig,intrinsicID,dynamicID) \
435     FCFuncFlag_HasSignature + FCDynamic(name,intrinsicID,dynamicID) (LPVOID)sig,
436
437 #define FCUnreferenced FCFuncFlag_Unreferenced +
438
439 #define FCFuncStart(name) static const LPVOID name[] = {
440 #define FCFuncEnd() FCFuncFlag_EndOfArray + FCFuncFlags(CORINFO_INTRINSIC_Illegal, ECall::InvalidDynamicFCallId) };
441
442 #include "ecalllist.h"
443
444
445 // Extern definitions so that ecall.cpp can see these tables
446 extern const ECClass c_rgECClasses[];
447 extern const int c_nECClasses;
448
449 const ECClass c_rgECClasses[] =
450 {
451 #define FCClassElement(name,namespace,funcs) {name, namespace, funcs},
452 #include "ecalllist.h"
453 };  // c_rgECClasses[]
454
455 const int c_nECClasses = NumItems(c_rgECClasses);
456
457 #endif // !CROSSGEN_COMPILE && CROSSGEN_MSCORLIB
458
459
460 #ifdef CROSSGEN_MSCORLIB
461 }; // namespace CrossGenMscorlib
462 #endif