少年修仙传客户端基础资源
hch
2024-04-11 4c71d74b77c9eb62a0323698c9a0db3b641a917e
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
#include "BasicBlockSpliter.h"
 
#include "../metadata/Opcodes.h"
#include "../metadata/MetadataUtil.h"
 
using namespace hybridclr::metadata;
 
namespace hybridclr
{
namespace transform
{
 
    void BasicBlockSpliter::SplitNormal(const byte* ilcodeStart, uint32_t codeSize, Uin32Set& ilOffsets)
    {
        const byte* codeEnd = ilcodeStart + codeSize;
        const byte* ip = ilcodeStart;
 
        while (ip < codeEnd)
        {
            ilOffsets.insert((uint32_t)(ip - ilcodeStart));
            const OpCodeInfo* oc = DecodeOpCodeInfo(ip, codeEnd);
            IL2CPP_ASSERT(oc);
            int32_t opCodeSize = GetOpCodeSize(ip, oc);
            const byte* nextIp = ip + opCodeSize;
            int32_t nextOffset = (int32_t)(nextIp - ilcodeStart);
            IL2CPP_ASSERT(nextOffset >= 0 && nextOffset <= (int32_t)codeSize);
 
            switch (oc->inlineType)
            {
            case ArgType::None:
            case ArgType::Data:
            {
                break;
            }
            case ArgType::StaticBranch:
            {
                _splitOffsets.insert(nextOffset);
                break;
            }
            case ArgType::BranchTarget:
            {
                int32_t offset;
                switch (oc->inlineParam)
                {
                case 1:
                {
                    offset = GetI1(ip + 1);
                    break;
                }
                case 4:
                {
                    offset = GetI4LittleEndian(ip + 1);
                    break;
                }
                default:
                {
                    RaiseExecutionEngineException("invalid BranchTarget param");
                    offset = -1;
                }
                }
                // don't split 0 offset br
                if (offset != 0 || (oc->baseOpValue == OpcodeValue::LEAVE || oc->baseOpValue == OpcodeValue::LEAVE_S))
                {
                    _splitOffsets.insert(nextOffset);
                    _splitOffsets.insert(nextOffset + offset);
                }
                break;
            }
            case ArgType::Switch:
            {
                uint32_t caseNum = GetI4LittleEndian(ip + 1);
                bool splitAny = false;
                for (uint32_t caseIdx = 0; caseIdx < caseNum; caseIdx++)
                {
                    int32_t caseOffset = GetI4LittleEndian(ip + 5 + caseIdx * 4);
                    if (caseOffset != 0)
                    {
                        _splitOffsets.insert(nextOffset + caseOffset);
                        splitAny = true;
                    }
                }
                if (splitAny)
                {
                    _splitOffsets.insert(nextOffset);
                }
                break;
            }
            default:
            {
                RaiseExecutionEngineException("unknown inline type");
                nextOffset = -1;
            }
            }
            ip = nextIp;
        }
        IL2CPP_ASSERT(ip == codeEnd);
    }
 
    void BasicBlockSpliter::SplitExceptionHandles(const byte* ilcodeStart, uint32_t codeSize, const std::vector<metadata::ExceptionClause>& exceptionClauses)
    {
        for (auto& eh : exceptionClauses)
        {
            _splitOffsets.insert(eh.tryOffset);
            _splitOffsets.insert(eh.tryOffset + eh.tryLength);
            _splitOffsets.insert(eh.handlerOffsets);
            _splitOffsets.insert(eh.handlerOffsets + eh.handlerLength);
            if (eh.flags == CorILExceptionClauseType::Filter)
            {
                _splitOffsets.insert(eh.classTokenOrFilterOffset);
            }
        }
    }
 
    void BasicBlockSpliter::SplitBasicBlocks()
    {
        const byte* ilcodeStart = _body.ilcodes;
 
        uint32_t codeSize = _body.codeSize;
 
        Uin32Set ilOffsets(codeSize);
        ilOffsets.insert(codeSize);
 
        SplitNormal(ilcodeStart, codeSize, ilOffsets);
        SplitExceptionHandles(ilcodeStart, codeSize, _body.exceptionClauses);
 
        /*if (_splitOffsets.find(0) != _splitOffsets.end())
        {
            _splitOffsets.erase(0);
        }*/
#if DEBUG
        for (uint32_t offset : _splitOffsets)
        {
            IL2CPP_ASSERT(ilOffsets.find(offset) != ilOffsets.end());
        }
        IL2CPP_ASSERT(_splitOffsets.find(codeSize) != _splitOffsets.end());
#endif
    }
}
}