/src/capstonenext/MCRegisterInfo.c
Line | Count | Source |
1 | | //=== MC/MCRegisterInfo.cpp - Target Register Description -------*- C++ -*-===// |
2 | | // |
3 | | // The LLVM Compiler Infrastructure |
4 | | // |
5 | | // This file is distributed under the University of Illinois Open Source |
6 | | // License. See LICENSE.TXT for details. |
7 | | // |
8 | | //===----------------------------------------------------------------------===// |
9 | | // |
10 | | // This file implements MCRegisterInfo functions. |
11 | | // |
12 | | //===----------------------------------------------------------------------===// |
13 | | |
14 | | /* Capstone Disassembly Engine */ |
15 | | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2019 */ |
16 | | |
17 | | #include "MCRegisterInfo.h" |
18 | | |
19 | | /// DiffListIterator - Base iterator class that can traverse the |
20 | | /// differentially encoded register and regunit lists in DiffLists. |
21 | | /// Don't use this class directly, use one of the specialized sub-classes |
22 | | /// defined below. |
23 | | typedef struct DiffListIterator { |
24 | | uint16_t Val; |
25 | | const MCPhysReg *List; |
26 | | } DiffListIterator; |
27 | | |
28 | | void MCRegisterInfo_InitMCRegisterInfo(MCRegisterInfo *RI, |
29 | | const MCRegisterDesc *D, unsigned NR, |
30 | | unsigned RA, unsigned PC, |
31 | | const MCRegisterClass *C, unsigned NC, |
32 | | uint16_t (*RURoots)[2], unsigned NRU, |
33 | | const MCPhysReg *DL, const char *Strings, |
34 | | const uint16_t *SubIndices, |
35 | | unsigned NumIndices, const uint16_t *RET) |
36 | 55.8k | { |
37 | 55.8k | RI->Desc = D; |
38 | 55.8k | RI->NumRegs = NR; |
39 | 55.8k | RI->RAReg = RA; |
40 | 55.8k | RI->PCReg = PC; |
41 | 55.8k | RI->Classes = C; |
42 | 55.8k | RI->DiffLists = DL; |
43 | 55.8k | RI->RegStrings = Strings; |
44 | 55.8k | RI->NumClasses = NC; |
45 | 55.8k | RI->RegUnitRoots = RURoots; |
46 | 55.8k | RI->NumRegUnits = NRU; |
47 | 55.8k | RI->SubRegIndices = SubIndices; |
48 | 55.8k | RI->NumSubRegIndices = NumIndices; |
49 | 55.8k | RI->RegEncodingTable = RET; |
50 | 55.8k | } |
51 | | |
52 | | static void DiffListIterator_init(DiffListIterator *d, MCPhysReg InitVal, |
53 | | const MCPhysReg *DiffList) |
54 | 682k | { |
55 | 682k | d->Val = InitVal; |
56 | 682k | d->List = DiffList; |
57 | 682k | } |
58 | | |
59 | | static uint16_t DiffListIterator_getVal(DiffListIterator *d) |
60 | 340k | { |
61 | 340k | return d->Val; |
62 | 340k | } |
63 | | |
64 | | static bool DiffListIterator_next(DiffListIterator *d) |
65 | 5.35M | { |
66 | 5.35M | MCPhysReg D; |
67 | | |
68 | 5.35M | if (d->List == 0) |
69 | 0 | return false; |
70 | | |
71 | 5.35M | D = *d->List; |
72 | 5.35M | d->List++; |
73 | 5.35M | d->Val += D; |
74 | | |
75 | 5.35M | if (!D) |
76 | 341k | d->List = 0; |
77 | | |
78 | 5.35M | return (D != 0); |
79 | 5.35M | } |
80 | | |
81 | | static bool DiffListIterator_isValid(DiffListIterator *d) |
82 | 5.35M | { |
83 | 5.35M | return (d->List != 0); |
84 | 5.35M | } |
85 | | |
86 | | unsigned MCRegisterInfo_getMatchingSuperReg(const MCRegisterInfo *RI, |
87 | | unsigned Reg, unsigned SubIdx, |
88 | | const MCRegisterClass *RC) |
89 | 7.79k | { |
90 | 7.79k | DiffListIterator iter; |
91 | | |
92 | 7.79k | if (Reg >= RI->NumRegs) { |
93 | 0 | return 0; |
94 | 0 | } |
95 | | |
96 | 7.79k | DiffListIterator_init(&iter, (MCPhysReg)Reg, |
97 | 7.79k | RI->DiffLists + RI->Desc[Reg].SuperRegs); |
98 | 7.79k | DiffListIterator_next(&iter); |
99 | | |
100 | 7.79k | while (DiffListIterator_isValid(&iter)) { |
101 | 7.79k | uint16_t val = DiffListIterator_getVal(&iter); |
102 | 7.79k | if (MCRegisterClass_contains(RC, val) && |
103 | 7.79k | Reg == MCRegisterInfo_getSubReg(RI, val, SubIdx)) |
104 | 7.79k | return val; |
105 | | |
106 | 0 | DiffListIterator_next(&iter); |
107 | 0 | } |
108 | | |
109 | 0 | return 0; |
110 | 7.79k | } |
111 | | |
112 | | unsigned MCRegisterInfo_getSubReg(const MCRegisterInfo *RI, unsigned Reg, |
113 | | unsigned Idx) |
114 | 674k | { |
115 | 674k | DiffListIterator iter; |
116 | 674k | const uint16_t *SRI = RI->SubRegIndices + RI->Desc[Reg].SubRegIndices; |
117 | | |
118 | 674k | DiffListIterator_init(&iter, (MCPhysReg)Reg, |
119 | 674k | RI->DiffLists + RI->Desc[Reg].SubRegs); |
120 | 674k | DiffListIterator_next(&iter); |
121 | | |
122 | 5.34M | while (DiffListIterator_isValid(&iter)) { |
123 | 5.00M | if (*SRI == Idx) |
124 | 333k | return DiffListIterator_getVal(&iter); |
125 | 4.66M | DiffListIterator_next(&iter); |
126 | 4.66M | ++SRI; |
127 | 4.66M | } |
128 | | |
129 | 341k | return 0; |
130 | 674k | } |
131 | | |
132 | | const MCRegisterClass *MCRegisterInfo_getRegClass(const MCRegisterInfo *RI, |
133 | | unsigned i) |
134 | 3.17M | { |
135 | | //assert(i < getNumRegClasses() && "Register Class ID out of range"); |
136 | 3.17M | if (i >= RI->NumClasses) |
137 | 0 | return 0; |
138 | 3.17M | return &(RI->Classes[i]); |
139 | 3.17M | } |
140 | | |
141 | | bool MCRegisterClass_contains(const MCRegisterClass *c, unsigned Reg) |
142 | 2.92M | { |
143 | 2.92M | unsigned InByte = 0; |
144 | 2.92M | unsigned Byte = 0; |
145 | | |
146 | | // Make sure that MCRegisterInfo_getRegClass didn't return 0 |
147 | | // (for calls to GETREGCLASS_CONTAIN0) |
148 | 2.92M | if (!c) |
149 | 0 | return false; |
150 | | |
151 | 2.92M | InByte = Reg % 8; |
152 | 2.92M | Byte = Reg / 8; |
153 | | |
154 | 2.92M | if (Byte >= c->RegSetSize) |
155 | 800k | return false; |
156 | | |
157 | 2.12M | return (c->RegSet[Byte] & (1 << InByte)) != 0; |
158 | 2.92M | } |
159 | | |
160 | | unsigned MCRegisterClass_getRegister(const MCRegisterClass *c, unsigned RegNo) |
161 | 183k | { |
162 | 183k | return c->RegsBegin[RegNo]; |
163 | 183k | } |