Welcome to mirror list, hosted at ThFree Co, Russian Federation.

zapinfo.h « zap « coreclr « src - github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2731bdc8954d1083ba66711c7f57f869a7b6ae60 (plain)
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
//
// ZapInfo.h
//

//
// JIT-EE interface for zapping
//
// ======================================================================================

#ifndef __ZAPINFO_H__
#define __ZAPINFO_H__

#include "zapcode.h"

class ZapInfo;
struct InlineContext;

// The compiled code often implicitly needs fixups for various subtle reasons.
// We only emit explict fixups while compiling the method, while collecting
// implicit fixups in the LoadTable. At the end of compiling, we expect
// many of the LoadTable entries to be subsumed by the explicit entries
// and will not need to be emitted.
// This is also used to detect duplicate explicit fixups for the same type.

template <typename HandleType>
class LoadTable
{
private:
    ZapImage            *m_pModule;

    struct LoadEntry
    {
        HandleType              handle;
        int                     order;      // -1 = fixed
    };

    static int __cdecl LoadEntryCmp(const void* a_, const void* b_)
    {
        return ((LoadEntry*)a_)->order - ((LoadEntry*)b_)->order;
    }

    class LoadEntryTraits : public NoRemoveSHashTraits< DefaultSHashTraits<LoadEntry> >
    {
    public:
        typedef typename NoRemoveSHashTraits<DefaultSHashTraits<LoadEntry> >::count_t count_t;
        typedef typename NoRemoveSHashTraits<DefaultSHashTraits<LoadEntry> >::element_t element_t;
        typedef HandleType key_t;

        static key_t GetKey(element_t e)
        {
            LIMITED_METHOD_CONTRACT;
            return e.handle;
        }
        static BOOL Equals(key_t k1, key_t k2)
        {
            LIMITED_METHOD_CONTRACT;
            return k1 == k2;
        }
        static count_t Hash(key_t k)
        {
            LIMITED_METHOD_CONTRACT;
            return (count_t)(size_t)k;
        }

        static const element_t Null() { LIMITED_METHOD_CONTRACT; LoadEntry e; e.handle = NULL; e.order = 0; return e; }
        static bool IsNull(const element_t &e) { LIMITED_METHOD_CONTRACT; return (e.handle == NULL); }
    };

    typedef SHash<LoadEntryTraits> LoadEntryHashTable;

    LoadEntryHashTable      m_entries;

public:
    LoadTable(ZapImage *pModule)
      : m_pModule(pModule)
    {
    }

    // fixed=TRUE if the caller can guarantee that type will be fixed up because
    // of some implicit fixup. In this case, we track 'handle' only to avoid
    // duplicates and will not actually emit an explicit fixup for 'handle'
    //
    // fixed=FALSE if the caller needs an explicit fixup. We will emit an
    // explicit fixup for 'handle' if there are no other implicit fixups.

    void Load(HandleType handle, BOOL fixed)
    {
        CONTRACTL
        {
            THROWS;
            GC_NOTRIGGER;
        }
        CONTRACTL_END;

        const LoadEntry *result = m_entries.LookupPtr(handle);

        if (result != NULL)
        {
            if (fixed)
                ((LoadEntry*)result)->order = -1;
            return;
        }

        LoadEntry   newEntry;

        newEntry.handle = handle;
        newEntry.order = fixed ? -1 : m_entries.GetCount();

        m_entries.Add(newEntry);
    }

    void EmitLoadFixups(CORINFO_METHOD_HANDLE currentMethodHandle, ZapInfo * pZapInfo);
};

// Declare some specializations of EmitLoadFixups().
template<> void LoadTable<CORINFO_CLASS_HANDLE>::EmitLoadFixups(CORINFO_METHOD_HANDLE currentMethodHandle, ZapInfo * pZapInfo);
template<> void LoadTable<CORINFO_METHOD_HANDLE>::EmitLoadFixups(CORINFO_METHOD_HANDLE currentMethodHandle, ZapInfo * pZapInfo);


class ZapInfo
    : public ICorJitInfo
{
    friend class ZapImage;

    // Owning ZapImage
    ZapImage * m_pImage;

    Zapper * m_zapper;
    ICorDynamicInfo * m_pEEJitInfo;
    ICorCompileInfo * m_pEECompileInfo;

    // Current method being compiled; it is non-nil only for
    // method defs whose IL is in this module and (for generic code)
    // have <object> instantiation. It is also nil for IL_STUBs.
    mdMethodDef                 m_currentMethodToken;
    CORINFO_METHOD_HANDLE       m_currentMethodHandle;
    CORINFO_METHOD_INFO         m_currentMethodInfo;

    // m_currentMethodModule==m_hModule except for generic types/methods
    // defined in another assembly but instantiated in the current assembly.
    CORINFO_MODULE_HANDLE       m_currentMethodModule;

    unsigned                    m_currentMethodProfilingDataFlags;

    // Debug information reported by the JIT compiler for the current method
    ICorDebugInfo::NativeVarInfo *m_pNativeVarInfo;
    ULONG32                     m_iNativeVarInfo;
    ICorDebugInfo::OffsetMapping *m_pOffsetMapping;
    ULONG32                     m_iOffsetMapping;

    BYTE *                      m_pGCInfo;
    SIZE_T                      m_cbGCInfo;


    ZapBlobWithRelocs *         m_pCode;
    ZapBlobWithRelocs *         m_pColdCode;
    ZapBlobWithRelocs *         m_pROData;

#ifdef FEATURE_EH_FUNCLETS
    // Unwind info of the main method body. It will get merged with GC info.
    BYTE *                      m_pMainUnwindInfo;
    ULONG                       m_cbMainUnwindInfo;

    ZapUnwindInfo *             m_pUnwindInfo;
    ZapUnwindInfo *             m_pUnwindInfoFragments;
#if defined(TARGET_AMD64)
    ZapUnwindInfo *             m_pChainedColdUnwindInfo;
#endif
#endif // FEATURE_EH_FUNCLETS

    ZapExceptionInfo *          m_pExceptionInfo;

    ZapBlobWithRelocs *         m_pProfileData;

    ZapImport *                 m_pProfilingHandle;

    struct CodeRelocation : ZapReloc
    {
        ZapBlobWithRelocs * m_pNode;
    };

    SArray<CodeRelocation>   m_CodeRelocations;

    static int __cdecl CompareCodeRelocation(const void * a, const void * b);

    struct ImportEntry
    {
        ZapImport * pImport;
        bool fConditional; // Conditional imports are emitted only if they are actually referenced by the code.
    };

    class ImportTraits : public NoRemoveSHashTraits< DefaultSHashTraits<ImportEntry> >
    {
    public:
        typedef ZapImport * key_t;

        static key_t GetKey(element_t e)
        {
            LIMITED_METHOD_CONTRACT;
            return e.pImport;
        }
        static BOOL Equals(key_t k1, key_t k2)
        {
            LIMITED_METHOD_CONTRACT;
            return k1 == k2;
        }
        static count_t Hash(key_t k)
        {
            LIMITED_METHOD_CONTRACT;
            return (count_t)(size_t)k;
        }

        static const element_t Null() { LIMITED_METHOD_CONTRACT; ImportEntry e; e.pImport = NULL; return e; }
        static bool IsNull(const element_t &e) { LIMITED_METHOD_CONTRACT; return e.pImport == NULL; }
    };

    SHash<ImportTraits>         m_ImportSet;
    SArray<ZapImport *>         m_Imports;

    InlineSString<128>          m_currentMethodName;

    // Cache to reduce the number of entries in CORCOMPILE_LOAD_TABLE if it
    // is implied by some other fixup type
    LoadTable<CORINFO_CLASS_HANDLE>  m_ClassLoadTable;
    LoadTable<CORINFO_METHOD_HANDLE> m_MethodLoadTable;

    CORJIT_FLAGS m_jitFlags;

    void InitMethodName();

    CORJIT_FLAGS ComputeJitFlags(CORINFO_METHOD_HANDLE handle);

    ZapDebugInfo * EmitDebugInfo();
    ZapGCInfo * EmitGCInfo();
    ZapImport ** EmitFixupList();

    void PublishCompiledMethod();

    void EmitCodeRelocations();

    void ProcessReferences();

    BOOL CurrentMethodHasProfileData();

    void embedGenericSignature(CORINFO_LOOKUP * pLookup);

    PVOID embedDirectCall(CORINFO_METHOD_HANDLE ftn,
                          CORINFO_ACCESS_FLAGS accessFlags,
                          BOOL fAllowThunk);

    struct ProfileDataResults
    {
        ProfileDataResults(CORINFO_METHOD_HANDLE ftn) : m_ftn(ftn) {}
        ProfileDataResults* m_next = nullptr;
        CORINFO_METHOD_HANDLE m_ftn;
        SArray<PgoInstrumentationSchema> m_schema;
        BYTE *pInstrumentationData = nullptr;
        HRESULT m_hr = E_FAIL;
    };
    ProfileDataResults *m_pgoResults = nullptr;

public:
    ZapInfo(ZapImage * pImage, mdMethodDef md, CORINFO_METHOD_HANDLE handle, CORINFO_MODULE_HANDLE module, unsigned methodProfilingDataFlags);
    ~ZapInfo();

#ifdef ALLOW_SXS_JIT_NGEN
    void ResetForJitRetry();
#endif // ALLOW_SXS_JIT_NGEN

    void CompileMethod();

    void AppendImport(ZapImport * pImport);
    void AppendConditionalImport(ZapImport * pImport);

    ULONG GetNumFixups();

    // ICorJitInfo
#include "icorjitinfoimpl_generated.h"

    int  canHandleException(struct _EXCEPTION_POINTERS *pExceptionPointers);
    void * getAddressOfPInvokeFixup(CORINFO_METHOD_HANDLE method,
                                    void **ppIndirection);
    ZapImport * GetProfilingHandleImport();
};

#endif // __ZAPINFO_H__