Coverage Report

Created: 2026-04-12 06:30

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