Coverage Report

Created: 2025-11-09 07:00

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