/src/capstonenext/MCRegisterInfo.c
Line | Count | Source (jump to first uncovered line) |
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, |
34 | | const char *Strings, |
35 | | const uint16_t *SubIndices, unsigned NumIndices, |
36 | | const uint16_t *RET) |
37 | 72.9k | { |
38 | 72.9k | RI->Desc = D; |
39 | 72.9k | RI->NumRegs = NR; |
40 | 72.9k | RI->RAReg = RA; |
41 | 72.9k | RI->PCReg = PC; |
42 | 72.9k | RI->Classes = C; |
43 | 72.9k | RI->DiffLists = DL; |
44 | 72.9k | RI->RegStrings = Strings; |
45 | 72.9k | RI->NumClasses = NC; |
46 | 72.9k | RI->RegUnitRoots = RURoots; |
47 | 72.9k | RI->NumRegUnits = NRU; |
48 | 72.9k | RI->SubRegIndices = SubIndices; |
49 | 72.9k | RI->NumSubRegIndices = NumIndices; |
50 | 72.9k | RI->RegEncodingTable = RET; |
51 | 72.9k | } |
52 | | |
53 | | static void DiffListIterator_init(DiffListIterator *d, MCPhysReg InitVal, const MCPhysReg *DiffList) |
54 | 656k | { |
55 | 656k | d->Val = InitVal; |
56 | 656k | d->List = DiffList; |
57 | 656k | } |
58 | | |
59 | | static uint16_t DiffListIterator_getVal(DiffListIterator *d) |
60 | 332k | { |
61 | 332k | return d->Val; |
62 | 332k | } |
63 | | |
64 | | static bool DiffListIterator_next(DiffListIterator *d) |
65 | 5.05M | { |
66 | 5.05M | MCPhysReg D; |
67 | | |
68 | 5.05M | if (d->List == 0) |
69 | 0 | return false; |
70 | | |
71 | 5.05M | D = *d->List; |
72 | 5.05M | d->List++; |
73 | 5.05M | d->Val += D; |
74 | | |
75 | 5.05M | if (!D) |
76 | 323k | d->List = 0; |
77 | | |
78 | 5.05M | return (D != 0); |
79 | 5.05M | } |
80 | | |
81 | | static bool DiffListIterator_isValid(DiffListIterator *d) |
82 | 5.05M | { |
83 | 5.05M | return (d->List != 0); |
84 | 5.05M | } |
85 | | |
86 | | unsigned MCRegisterInfo_getMatchingSuperReg(const MCRegisterInfo *RI, unsigned Reg, unsigned SubIdx, const MCRegisterClass *RC) |
87 | 9.69k | { |
88 | 9.69k | DiffListIterator iter; |
89 | | |
90 | 9.69k | if (Reg >= RI->NumRegs) { |
91 | 0 | return 0; |
92 | 0 | } |
93 | | |
94 | 9.69k | DiffListIterator_init(&iter, (MCPhysReg)Reg, RI->DiffLists + RI->Desc[Reg].SuperRegs); |
95 | 9.69k | DiffListIterator_next(&iter); |
96 | | |
97 | 9.69k | while(DiffListIterator_isValid(&iter)) { |
98 | 9.69k | uint16_t val = DiffListIterator_getVal(&iter); |
99 | 9.69k | if (MCRegisterClass_contains(RC, val) && Reg == MCRegisterInfo_getSubReg(RI, val, SubIdx)) |
100 | 9.69k | return val; |
101 | | |
102 | 0 | DiffListIterator_next(&iter); |
103 | 0 | } |
104 | | |
105 | 0 | return 0; |
106 | 9.69k | } |
107 | | |
108 | | unsigned MCRegisterInfo_getSubReg(const MCRegisterInfo *RI, unsigned Reg, unsigned Idx) |
109 | 647k | { |
110 | 647k | DiffListIterator iter; |
111 | 647k | const uint16_t *SRI = RI->SubRegIndices + RI->Desc[Reg].SubRegIndices; |
112 | | |
113 | 647k | DiffListIterator_init(&iter, (MCPhysReg)Reg, RI->DiffLists + RI->Desc[Reg].SubRegs); |
114 | 647k | DiffListIterator_next(&iter); |
115 | | |
116 | 5.04M | while(DiffListIterator_isValid(&iter)) { |
117 | 4.71M | if (*SRI == Idx) |
118 | 323k | return DiffListIterator_getVal(&iter); |
119 | 4.39M | DiffListIterator_next(&iter); |
120 | 4.39M | ++SRI; |
121 | 4.39M | } |
122 | | |
123 | 323k | return 0; |
124 | 647k | } |
125 | | |
126 | | const MCRegisterClass* MCRegisterInfo_getRegClass(const MCRegisterInfo *RI, unsigned i) |
127 | 3.45M | { |
128 | | //assert(i < getNumRegClasses() && "Register Class ID out of range"); |
129 | 3.45M | if (i >= RI->NumClasses) |
130 | 0 | return 0; |
131 | 3.45M | return &(RI->Classes[i]); |
132 | 3.45M | } |
133 | | |
134 | | bool MCRegisterClass_contains(const MCRegisterClass *c, unsigned Reg) |
135 | 3.02M | { |
136 | 3.02M | unsigned InByte = 0; |
137 | 3.02M | unsigned Byte = 0; |
138 | | |
139 | | // Make sure that MCRegisterInfo_getRegClass didn't return 0 |
140 | | // (for calls to GETREGCLASS_CONTAIN0) |
141 | 3.02M | if(!c) |
142 | 0 | return false; |
143 | | |
144 | 3.02M | InByte = Reg % 8; |
145 | 3.02M | Byte = Reg / 8; |
146 | | |
147 | 3.02M | if (Byte >= c->RegSetSize) |
148 | 656k | return false; |
149 | | |
150 | 2.36M | return (c->RegSet[Byte] & (1 << InByte)) != 0; |
151 | 3.02M | } |