summaryrefslogtreecommitdiff
path: root/src/zap/zapcode.h
blob: 2fe949ee580ad6fa1abb412e5427abb3f1e0e7e8 (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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//
// ZapCode.h
//

//
// ZapNodes for everything directly related to zapping of native code 
//
// code:ZapMethodHeader
// code:ZapMethodEntryPoint
// code:ZapMethodEntryPointTable
// code:ZapDebugInfo
// code:ZapProfileData
// code:ZapHelperTable
// code:ZapGCInfoTable


// 
// ======================================================================================

#ifndef __ZAPCODE_H__
#define __ZAPCODE_H__

// Forward declarations

class ZapBlobWithRelocs;

#ifdef REDHAWK
typedef ZapNode ZapGCInfo;
#else
#if defined(WIN64EXCEPTIONS)
class ZapGCInfo;
#else
typedef ZapBlob ZapGCInfo;
#endif
#endif // REDHAWK

typedef ZapBlob ZapDebugInfo;
typedef ZapBlob ZapFixupInfo;
#ifdef REDHAWK
typedef ZapBlobWithRelocs ZapExceptionInfo;
#else
typedef ZapBlob ZapExceptionInfo;
#endif

class ZapUnwindInfo;

class ZapImport;

class ZapInfo;

class ZapGCRefMapTable;

//---------------------------------------------------------------------------------------
//
// ZapMethodHeader is the main node that all information about the compiled code is hanging from
//
class ZapMethodHeader : public ZapNode
{
    // All other kinds of method headers are tightly coupled with the main method header
    friend class ZapProfileData;
    friend class ZapCodeMethodDescs;
    friend class ZapColdCodeMap;

    friend class MethodCodeComparer;

    friend class ZapImage;
    friend class ZapInfo;

    CORINFO_METHOD_HANDLE m_handle;
    CORINFO_CLASS_HANDLE  m_classHandle;

    ZapBlobWithRelocs * m_pCode;
    ZapBlobWithRelocs * m_pColdCode;    // May be NULL

    ZapUnwindInfo * m_pUnwindInfo;
    ZapUnwindInfo * m_pColdUnwindInfo;  // May be NULL

#ifdef WIN64EXCEPTIONS
    ZapUnwindInfo * m_pUnwindInfoFragments; // Linked list of all unwind info fragments
#endif

    ZapBlobWithRelocs * m_pROData;      // May be NULL

    ZapBlobWithRelocs * m_pProfileData; // May be NULL

    ZapGCInfo * m_pGCInfo;
    ZapDebugInfo * m_pDebugInfo;

    union                               // May be NULL
    {
        ZapImport ** m_pFixupList;      // Valid before place phase
        ZapFixupInfo * m_pFixupInfo;    // Valid after place phase
    };

    ZapExceptionInfo * m_pExceptionInfo; // May be NULL

    unsigned m_ProfilingDataFlags;

    unsigned m_compilationOrder;
    unsigned m_cachedLayoutOrder;

    DWORD m_methodIndex;

    ZapMethodHeader()
    {
    }

public:
    CORINFO_METHOD_HANDLE GetHandle()
    {
        return m_handle;
    }

    CORINFO_CLASS_HANDLE GetClassHandle()
    {
        return m_classHandle;
    }

    DWORD GetMethodIndex()
    {
        return m_methodIndex;
    }

    ZapBlobWithRelocs * GetCode()
    {
        return m_pCode;
    }

    ZapBlobWithRelocs * GetColdCode()
    {
        return m_pColdCode;
    }

    BOOL HasFixups()
    {
        return m_pFixupList != NULL;
    }

    ZapNode * GetFixupList()
    {
        return m_pFixupInfo;
    }

    ZapDebugInfo * GetDebugInfo()
    {
        return m_pDebugInfo;
    }

    unsigned GetCompilationOrder()
    {
        return m_compilationOrder;
    }

    unsigned GetCachedLayoutOrder()
    {
        return m_cachedLayoutOrder;
    }
    virtual ZapNodeType GetType()
    {
        return ZapNodeType_MethodHeader;
    }

    // Iterate over as many of the methods called by this method
    // as are easy to determine.  Currently this is implemented
    // by walking the Reloc list and so is only as complete as
    // the current state of the Relocs.  Note that the implementation
    // ignores virtual calls and calls in the cold code section.
    class PartialTargetMethodIterator
    {
    public:
        PartialTargetMethodIterator(ZapMethodHeader* pMethod)
            : m_pMethod(pMethod)
        {
            ZapBlobWithRelocs * pCode = pMethod->GetCode();
            m_pCurReloc = pCode ? pCode->GetRelocs() : NULL;
        }

        BOOL GetNext(CORINFO_METHOD_HANDLE *pHnd);

    private:
        ZapMethodHeader* m_pMethod;
        ZapReloc* m_pCurReloc;
    };

};

#if defined(_TARGET_X86_)
class ZapCodeBlob : public ZapBlobWithRelocs
{
protected:
    ZapCodeBlob(SIZE_T cbSize)
        : ZapBlobWithRelocs(cbSize)
    {
    }

public:
    static ZapCodeBlob * NewAlignedBlob(ZapWriter * pWriter, PVOID pData, SIZE_T cbSize, SIZE_T cbAlignment);

    virtual DWORD ComputeRVA(ZapWriter * pZapWriter, DWORD dwPos);
};
#else
typedef ZapBlobWithRelocs ZapCodeBlob;
#endif

class ZapCodeMethodDescs : public ZapNode
{
    COUNT_T m_iStartMethod;
    COUNT_T m_iEndMethod;
    COUNT_T m_nUnwindInfos;

public:
    ZapCodeMethodDescs(COUNT_T startMethod, COUNT_T endMethod, COUNT_T nUnwindInfos)
        : m_iStartMethod(startMethod), m_iEndMethod(endMethod), m_nUnwindInfos(nUnwindInfos)
    {
    }

    virtual UINT GetAligment()
    {
        return sizeof(DWORD);
    }

    virtual DWORD GetSize()
    {
        return m_nUnwindInfos * sizeof(DWORD);
    }

    virtual ZapNodeType GetType()
    {
        return ZapNodeType_CodeManagerMap;
    }

    virtual void Save(ZapWriter * pZapWriter);
};

//---------------------------------------------------------------------------------------
//
// ZapMethodEntryPoint is special type of placeholder. Unlike normal placeholder, it
// carries extra CORINFO_ACCESS_FLAGS that is used to opt into the direct call even
// when it would not be otherwise possible.
//
class ZapMethodEntryPoint : public ZapNode
{
    CORINFO_METHOD_HANDLE   m_handle;       // Target method being called
    BYTE                    m_accessFlags;  // CORINFO_ACCESS_FLAGS
    BYTE                    m_fUsed;        // Entrypoint is used - needs to be resolved

    ZapNode                *m_pEntryPoint;  // only used for abstract methods to remember the precode

public:
    ZapMethodEntryPoint(CORINFO_METHOD_HANDLE handle, CORINFO_ACCESS_FLAGS accessFlags)
        : m_handle(handle), m_accessFlags(static_cast<BYTE>(accessFlags))
    {
    }

    virtual ZapNodeType GetType()
    {
        return ZapNodeType_MethodEntryPoint;
    }

    CORINFO_METHOD_HANDLE GetHandle()
    {
        return m_handle;
    }

    CORINFO_ACCESS_FLAGS GetAccessFlags()
    {
        return (CORINFO_ACCESS_FLAGS)m_accessFlags;
    }

    void SetIsUsed()
    {
        m_fUsed = true;
    }

    BOOL IsUsed()
    {
        return m_fUsed;
    }

    void Resolve(ZapImage * pImage);
};

class ZapMethodEntryPointTable
{
    struct MethodEntryPointKey
    {
        MethodEntryPointKey(CORINFO_METHOD_HANDLE handle, CORINFO_ACCESS_FLAGS accessFlags)
            : m_handle(handle), m_accessFlags(accessFlags)
        {
        }

        CORINFO_METHOD_HANDLE   m_handle;       // Target method being called
        CORINFO_ACCESS_FLAGS    m_accessFlags;
    };

    class MethodEntryPointTraits : public NoRemoveSHashTraits< DefaultSHashTraits<ZapMethodEntryPoint *> >
    {
    public:
        typedef MethodEntryPointKey key_t;

        static key_t GetKey(element_t e)
        { 
            LIMITED_METHOD_CONTRACT;
            return MethodEntryPointKey(e->GetHandle(), e->GetAccessFlags());
        }
        static BOOL Equals(key_t k1, key_t k2) 
        { 
            LIMITED_METHOD_CONTRACT;
            return (k1.m_handle == k2.m_handle) && (k1.m_accessFlags == k2.m_accessFlags);
        }
        static count_t Hash(key_t k) 
        {
            LIMITED_METHOD_CONTRACT;
            return (count_t)(size_t)k.m_handle ^ (count_t)k.m_accessFlags;
        }

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

    typedef SHash< MethodEntryPointTraits > MethodEntryPointTable;

    MethodEntryPointTable m_entries;
    ZapImage * m_pImage;

public:
    ZapMethodEntryPointTable(ZapImage * pImage)
        : m_pImage(pImage)
    {
    }

    void Preallocate(COUNT_T cbILImage)
    {
        PREALLOCATE_HASHTABLE(ZapMethodEntryPointTable::m_entries, 0.0018, cbILImage);
    }

    ZapMethodEntryPoint * GetMethodEntryPoint(CORINFO_METHOD_HANDLE handle, CORINFO_ACCESS_FLAGS accessFlags);

    ZapNode * CanDirectCall(ZapMethodEntryPoint * pMethodEntryPoint, ZapMethodHeader * pCaller);

    void Resolve();
};

//---------------------------------------------------------------------------------------
//
// Zapping of unwind info
//
class ZapUnwindInfo : public ZapNode
{
    ZapNode * m_pCode;

    DWORD m_dwStartOffset;
    DWORD m_dwEndOffset;

    ZapNode * m_pUnwindData;

    ZapUnwindInfo * m_pNextFragment;

public:
    ZapUnwindInfo(ZapNode * pCode, DWORD dwStartOffset, DWORD dwEndOffset, ZapNode * pUnwindData = NULL)
        : m_pCode(pCode),
        m_dwStartOffset(dwStartOffset),
        m_dwEndOffset(dwEndOffset),
        m_pUnwindData(pUnwindData)
    {
    }

    ZapNode * GetCode()
    {
        return m_pCode;
    }

    DWORD GetStartOffset()
    {
        return m_dwStartOffset;
    }

    DWORD GetEndOffset()
    {
        return m_dwEndOffset;
    }

    DWORD GetStartAddress()
    {
        return m_pCode->GetRVA() + GetStartOffset();
    }

    DWORD GetEndAddress()
    {
        return m_pCode->GetRVA() + GetEndOffset();
    }
    // Used to set unwind data lazily
    void SetUnwindData(ZapNode * pUnwindData)
    {
        _ASSERTE(m_pUnwindData == NULL);
        m_pUnwindData = pUnwindData;
    }

    ZapNode * GetUnwindData()
    {
        return m_pUnwindData;
    }

    void SetNextFragment(ZapUnwindInfo * pFragment)
    {
        _ASSERTE(m_pNextFragment == NULL);
        m_pNextFragment = pFragment;
    }

    ZapUnwindInfo * GetNextFragment()
    {
        return m_pNextFragment;
    }

    virtual UINT GetAlignment()
    {
        return sizeof(ULONG);
    }

    virtual DWORD GetSize()
    {
        return sizeof(T_RUNTIME_FUNCTION);
    }

    virtual ZapNodeType GetType()
    {
        return ZapNodeType_UnwindInfo;
    }

    virtual void Save(ZapWriter * pZapWriter);

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

#ifdef WIN64EXCEPTIONS
//---------------------------------------------------------------------------------------
//
// Zapping of unwind data
//
class ZapUnwindData : public ZapBlob
{
public:
    ZapUnwindData(SIZE_T cbSize)
        : ZapBlob(cbSize)
    {
    }

    virtual UINT GetAlignment();

    virtual DWORD GetSize();

    virtual ZapNodeType GetType()
    {
        return ZapNodeType_UnwindData;
    }

    BOOL IsFilterFunclet()
    {
        return GetType() == ZapNodeType_FilterFuncletUnwindData;
    }

    ZapNode * GetPersonalityRoutine(ZapImage * pImage);
    virtual void Save(ZapWriter * pZapWriter);

    static ZapUnwindData * NewUnwindData(ZapWriter * pWriter, PVOID pData, SIZE_T cbSize, BOOL fIsFilterFunclet);
};

class ZapFilterFuncletUnwindData : public ZapUnwindData
{
public:
    ZapFilterFuncletUnwindData(SIZE_T cbSize)
        : ZapUnwindData(cbSize)
    {
    }

    virtual ZapNodeType GetType()
    {
        return ZapNodeType_FilterFuncletUnwindData;
    }
};
class ZapUnwindDataTable
{
    ZapImage * m_pImage;

    struct ZapUnwindDataKey
    {
        ZapUnwindDataKey(PVOID pUnwindData, SIZE_T cbUnwindData, BOOL fIsFilterFunclet)
            : m_unwindData(pUnwindData, cbUnwindData), m_fIsFilterFunclet(fIsFilterFunclet)
        {
        }

        ZapBlob::SHashKey m_unwindData;
        BOOL m_fIsFilterFunclet;
    };

    class ZapUnwindDataTraits : public NoRemoveSHashTraits< DefaultSHashTraits<ZapUnwindData *> >
    {
    public:
        typedef ZapUnwindDataKey key_t;

        static key_t GetKey(element_t e)
        { 
            LIMITED_METHOD_CONTRACT;
            return ZapUnwindDataKey(e->GetData(), e->GetBlobSize(), e->IsFilterFunclet());
        }
        static BOOL Equals(key_t k1, key_t k2) 
        { 
            LIMITED_METHOD_CONTRACT;
            return ZapBlob::SHashTraits::Equals(k1.m_unwindData, k2.m_unwindData) && (k1.m_fIsFilterFunclet == k2.m_fIsFilterFunclet);
        }
        static count_t Hash(key_t k) 
        {
            LIMITED_METHOD_CONTRACT;
            return ZapBlob::SHashTraits::Hash(k.m_unwindData) ^ k.m_fIsFilterFunclet;
        }

        static const element_t Null() { LIMITED_METHOD_CONTRACT; return NULL; }
        static bool IsNull(const element_t &e) { LIMITED_METHOD_CONTRACT; return e == NULL; }
    };
    // Hashtable with all unwind data blobs. If two methods have unwind data
    // we store it just once.
    SHash< ZapUnwindDataTraits >  m_blobs;

public:
    ZapUnwindDataTable(ZapImage * pImage)
        : m_pImage(pImage)
    {
    }

    void Preallocate(COUNT_T cbILImage)
    {
        PREALLOCATE_HASHTABLE(ZapUnwindDataTable::m_blobs, 0.0003, cbILImage);
    }

    ZapUnwindData * GetUnwindData(PVOID pBlob, SIZE_T cbBlob, BOOL fIsFilterFunclet);
};
#endif // WIN64EXCEPTIONS


//---------------------------------------------------------------------------------------
//
// Zapping of GC info
//
#ifdef WIN64EXCEPTIONS
class ZapGCInfo : public ZapUnwindData
{
    DWORD m_cbGCInfo;

public:
    ZapGCInfo(SIZE_T cbGCInfo, SIZE_T cbUnwindInfo)
        : ZapUnwindData(cbUnwindInfo), m_cbGCInfo((DWORD)cbGCInfo)
    {
        if (m_cbGCInfo > ZAPWRITER_MAX_SIZE)
            ThrowHR(COR_E_OVERFLOW);
    }

    virtual PBYTE GetData()
    {
        return (PBYTE)(this + 1);
    }

    PBYTE GetGCInfo()
    {
        return GetData() + GetUnwindInfoSize();
    }

    DWORD GetGCInfoSize()
    {
        return m_cbGCInfo;
    }

    PBYTE GetUnwindInfo()
    {
        return GetData();
    }

    DWORD GetUnwindInfoSize()
    {
        return GetBlobSize();
    }

    virtual ZapNodeType GetType()
    {
        return ZapNodeType_UnwindDataAndGCInfo;
    }

    virtual DWORD GetSize()
    {
        return ZapUnwindData::GetSize() + m_cbGCInfo;
    }

    virtual void Save(ZapWriter * pZapWriter)
    {
        ZapUnwindData::Save(pZapWriter);

        pZapWriter->Write(GetGCInfo(), GetGCInfoSize());
    }

    static ZapGCInfo * NewGCInfo(ZapWriter * pWriter, PVOID pGCInfo, SIZE_T cbGCInfo, PVOID pUnwindInfo, SIZE_T cbUnwindInfo);
};

class ZapGCInfoTable
{
    ZapImage * m_pImage;

    struct GCInfoKey
    {
        GCInfoKey(PVOID pGCInfo, SIZE_T cbGCInfo, PVOID pUnwindInfo, SIZE_T cbUnwindInfo)
            : m_gcInfo(pGCInfo, cbGCInfo), m_unwindInfo(pUnwindInfo, cbUnwindInfo)
        {
        }

        ZapBlob::SHashKey m_gcInfo;
        ZapBlob::SHashKey m_unwindInfo;
    };

    class GCInfoTraits : public NoRemoveSHashTraits< DefaultSHashTraits<ZapGCInfo *> >
    {
    public:
        typedef GCInfoKey key_t;

        static key_t GetKey(element_t e)
        { 
            LIMITED_METHOD_CONTRACT;
            return GCInfoKey(e->GetGCInfo(), e->GetGCInfoSize(), e->GetUnwindInfo(), e->GetUnwindInfoSize());
        }
        static BOOL Equals(key_t k1, key_t k2) 
        { 
            LIMITED_METHOD_CONTRACT;
            return ZapBlob::SHashTraits::Equals(k1.m_gcInfo, k2.m_gcInfo) && ZapBlob::SHashTraits::Equals(k1.m_unwindInfo, k2.m_unwindInfo);
        }
        static count_t Hash(key_t k) 
        {
            LIMITED_METHOD_CONTRACT;
            return ZapBlob::SHashTraits::Hash(k.m_gcInfo) ^ ZapBlob::SHashTraits::Hash(k.m_unwindInfo);
        }

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

    // Hashtable with all GC info blobs. If two methods have same GC info
    // we store it just once.
    SHash< GCInfoTraits >  m_blobs;

public:
    ZapGCInfoTable(ZapImage * pImage)
        : m_pImage(pImage)
    {
    }

    void Preallocate(COUNT_T cbILImage)
    {
        PREALLOCATE_HASHTABLE(ZapGCInfoTable::m_blobs, 0.0021, cbILImage);
    }

    // Returns interned instance of the GC info blob
    ZapGCInfo * GetGCInfo(PVOID pGCInfo, SIZE_T cbGCInfo, PVOID pUnwindInfo, SIZE_T cbUnwindInfo);
};
#else
class ZapGCInfoTable
{
    ZapImage * m_pImage;

    // Hashtable with all GC info blobs. If two methods have same GC info
    // we store it just once.
    SHash< NoRemoveSHashTraits < ZapBlob::SHashTraits > >  m_blobs;

public:
    ZapGCInfoTable(ZapImage * pImage)
        : m_pImage(pImage)
    {
    }

    void Preallocate(COUNT_T cbILImage)
    {
        PREALLOCATE_HASHTABLE(ZapGCInfoTable::m_blobs, 0.0021, cbILImage);
    }

    // Returns interned instance of the GC info blob
    ZapGCInfo * GetGCInfo(PVOID pBlob, SIZE_T cbBlob);
};
#endif

//---------------------------------------------------------------------------------------
//
// Zapping of debug info for native code
//
class ZapDebugInfoTable : public ZapNode
{
    COUNT_T m_nCount;
    ZapNode ** m_pTable;

    ZapImage * m_pImage;

    // Hashtable with all debug info blobs. If two methods have same debug info
    // we store it just once.
    SHash< NoRemoveSHashTraits < ZapBlob::SHashTraits > >  m_blobs;

    class LabelledEntry : public ZapNode
    {
    public:
        LabelledEntry * m_pNext;
        ZapMethodHeader * m_pMethod;

        LabelledEntry(ZapMethodHeader * pMethod)
            : m_pMethod(pMethod)
        {
        }

        virtual DWORD GetSize()
        {
            return sizeof(CORCOMPILE_DEBUG_LABELLED_ENTRY);
        }

        virtual UINT GetAlignment()
        {
            return sizeof(DWORD);
        }

        virtual ZapNodeType GetType()
        {
            return ZapNodeType_DebugInfoLabelledEntry;
        }

        virtual void Save(ZapWriter * pZapWriter);
    };

public:
    ZapDebugInfoTable(ZapImage * pImage)
        : m_pImage(pImage)
    {
    }

    void Preallocate(COUNT_T cbILImage)
    {
        PREALLOCATE_HASHTABLE(ZapDebugInfoTable::m_blobs, 0.0024, cbILImage);
    }

    // Returns interned instance of the debug info blob
    ZapDebugInfo * GetDebugInfo(PVOID pBlob, SIZE_T cbBlob);

    void PrepareLayout();
    void PlaceDebugInfo(ZapMethodHeader * pMethod);
    void FinishLayout();

    virtual DWORD GetSize()
    {
        return m_nCount * sizeof(CORCOMPILE_DEBUG_RID_ENTRY);
    }

    virtual UINT GetAlignment()
    {
        return sizeof(DWORD);
    }

    virtual ZapNodeType GetType()
    {
        return ZapNodeType_DebugInfoTable;
    }

    virtual void Save(ZapWriter * pZapWriter);
};

//---------------------------------------------------------------------------------------
//
// Zapping of IBC profile data collection area
//
class ZapProfileData : public ZapNode
{
    ZapMethodHeader *          m_pMethod;
    ZapProfileData *           m_pNext;

public:
    ZapProfileData(ZapMethodHeader * pMethod)
        : m_pMethod(pMethod)
    {
    }

    void SetNext(ZapProfileData * pNext)
    {
        m_pNext = pNext;
    }

    virtual DWORD GetSize()
    {
        return sizeof(CORCOMPILE_METHOD_PROFILE_LIST);
    }

    virtual UINT GetAlignment()
    {
        return sizeof(TADDR);
    }

    virtual ZapNodeType GetType()
    {
        return ZapNodeType_ProfileData;
    }

    virtual void Save(ZapWriter * pZapWriter);
};

// Zapping of ExceptionInfoTable
// ExceptionInfoTable is a lookup table that has 1 entry for each method with EH information.
// The table is sorted by method start address, so binary search is used during runtime to find 
// the EH info for a given method given a method start address.
class ZapExceptionInfoLookupTable : public ZapNode
{
private:
    typedef struct
    {
        ZapNode* m_pCode;
        ZapExceptionInfo* m_pExceptionInfo;
    } ExceptionInfoEntry;

    SArray<ExceptionInfoEntry> m_exceptionInfoEntries;
    ZapImage* m_pImage;
public:
    ZapExceptionInfoLookupTable(ZapImage *pImage);

    void PlaceExceptionInfoEntry(ZapNode* pCode, ZapExceptionInfo* pExceptionInfo);

    virtual ZapNodeType GetType()
    {
        return ZapNodeType_ExceptionInfoTable;
    }
    virtual UINT GetAlignment()
    {
        return sizeof(TADDR);
    }
    virtual DWORD GetSize();
    virtual void Save(ZapWriter* pZapWriter);
};


class ZapUnwindInfoLookupTable : public ZapNode
{

public:
    ZapUnwindInfoLookupTable(ZapVirtualSection * pRuntimeFunctionSection, ZapNode * pCodeSection, DWORD totalCodeSize):
        m_pRuntimeFunctionSection(pRuntimeFunctionSection), m_pCodeSection(pCodeSection), m_TotalCodeSize(totalCodeSize)
    {
    }

    COUNT_T GetNumEntries()
    {
        return m_TotalCodeSize/RUNTIME_FUNCTION_LOOKUP_STRIDE + 1;
    }
    virtual ZapNodeType GetType()
    {
        return ZapNodeType_UnwindInfoLookupTable;
    }
    virtual UINT GetAlignment()
    {
        return sizeof(DWORD);
    }
    virtual DWORD GetSize();
    virtual void Save(ZapWriter* pZapWriter);

private:
    ZapVirtualSection * m_pRuntimeFunctionSection;
    ZapNode * m_pCodeSection;
    DWORD m_TotalCodeSize;
};

class ZapColdCodeMap : public ZapNode
{
public:
    ZapColdCodeMap(ZapVirtualSection * pRuntimeFunctionSection):
        m_pRuntimeFunctionSection (pRuntimeFunctionSection)
    {
    }

    virtual ZapNodeType GetType()
    {
        return ZapNodeType_ColdCodeMap;
    }
    virtual UINT GetAlignment()
    {
        return sizeof(DWORD);
    }
    virtual DWORD GetSize();
    virtual void Save(ZapWriter* pZapWriter);

private:
    ZapVirtualSection * m_pRuntimeFunctionSection;
};

// 
//---------------------------------------------------------------------------------------
//
// Jump thunk for JIT helper
//
#ifdef _DEBUG
const static PCSTR s_rgHelperNames[] = {
#define JITHELPER(code,pfnHelper,sig) #code ,
#define DYNAMICJITHELPER(code,pfnHelper,sig) "<dynamic> " #code ,
#include <jithelpers.h>
};
#endif // _DEBUG

class ZapHelperThunk : public ZapNode
{
    DWORD m_dwHelper;

public:
    ZapHelperThunk(DWORD dwHelper)
        : m_dwHelper(dwHelper)
    {
#ifdef _DEBUG
        static_assert_no_msg(COUNTOF(s_rgHelperNames) == CORINFO_HELP_COUNT);
        LOG((LF_ZAP, LL_INFO1000000, "Created ZapHelperThunk for helper %3d (%s)\n",
            (USHORT)m_dwHelper, s_rgHelperNames[(USHORT)m_dwHelper]));
#endif // _DEBUG
    }

    virtual DWORD GetSize();

    virtual ZapNodeType GetType()
    {
        return ZapNodeType_HelperThunk;
    }

    virtual void Save(ZapWriter * pZapWriter);
};

class ZapLazyHelperThunk : public ZapNode
{
    CorInfoHelpFunc m_dwHelper;

    ZapNode * m_pArg;
    ZapNode * m_pTarget;

    DWORD SaveWorker(ZapWriter * pZapWriter);

public:
    ZapLazyHelperThunk(CorInfoHelpFunc dwHelper)
        : m_dwHelper(dwHelper)
    {
    }

    void Place(ZapImage * pImage);

    virtual DWORD GetSize();

    virtual ZapNodeType GetType()
    {
        return ZapNodeType_LazyHelperThunk;
    }

    virtual void Save(ZapWriter * pZapWriter);
};

#endif // __ZAPCODE_H__