summaryrefslogtreecommitdiff
path: root/tools/debugserver/source/DNBRegisterInfo.cpp
blob: c224fc7056dcff6954bd541a323a617db5a8a290 (plain)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//===-- DNBRegisterInfo.cpp -------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
//  Created by Greg Clayton on 8/3/07.
//
//===----------------------------------------------------------------------===//

#include "DNBRegisterInfo.h"
#include "DNBLog.h"
#include <string.h>

DNBRegisterValueClass::DNBRegisterValueClass(const DNBRegisterInfo *regInfo) {
  Clear();
  if (regInfo)
    info = *regInfo;
}

void DNBRegisterValueClass::Clear() {
  memset(&info, 0, sizeof(DNBRegisterInfo));
  memset(&value, 0, sizeof(value));
}

bool DNBRegisterValueClass::IsValid() const {
  return info.name != NULL && info.type != InvalidRegType && info.size > 0 &&
         info.size <= sizeof(value);
}

#define PRINT_COMMA_SEPARATOR                                                  \
  do {                                                                         \
    if (pos < end) {                                                           \
      if (i > 0) {                                                             \
        strlcpy(pos, ", ", end - pos);                                         \
        pos += 2;                                                              \
      }                                                                        \
    }                                                                          \
  } while (0)

void DNBRegisterValueClass::Dump(const char *pre, const char *post) const {
  if (info.name != NULL) {
    char str[1024];
    char *pos;
    char *end = str + sizeof(str);
    if (info.format == Hex) {
      switch (info.size) {
      case 0:
        snprintf(str, sizeof(str), "%s",
                 "error: invalid register size of zero.");
        break;
      case 1:
        snprintf(str, sizeof(str), "0x%2.2x", value.uint8);
        break;
      case 2:
        snprintf(str, sizeof(str), "0x%4.4x", value.uint16);
        break;
      case 4:
        snprintf(str, sizeof(str), "0x%8.8x", value.uint32);
        break;
      case 8:
        snprintf(str, sizeof(str), "0x%16.16llx", value.uint64);
        break;
      case 16:
        snprintf(str, sizeof(str), "0x%16.16llx%16.16llx", value.v_uint64[0],
                 value.v_uint64[1]);
        break;
      default:
        strlcpy(str, "0x", 3);
        pos = str + 2;
        for (uint32_t i = 0; i < info.size; ++i) {
          if (pos < end)
            pos +=
                snprintf(pos, end - pos, "%2.2x", (uint32_t)value.v_uint8[i]);
        }
        break;
      }
    } else {
      switch (info.type) {
      case Uint:
        switch (info.size) {
        case 1:
          snprintf(str, sizeof(str), "%u", value.uint8);
          break;
        case 2:
          snprintf(str, sizeof(str), "%u", value.uint16);
          break;
        case 4:
          snprintf(str, sizeof(str), "%u", value.uint32);
          break;
        case 8:
          snprintf(str, sizeof(str), "%llu", value.uint64);
          break;
        default:
          snprintf(str, sizeof(str), "error: unsupported uint byte size %d.",
                   info.size);
          break;
        }
        break;

      case Sint:
        switch (info.size) {
        case 1:
          snprintf(str, sizeof(str), "%d", value.sint8);
          break;
        case 2:
          snprintf(str, sizeof(str), "%d", value.sint16);
          break;
        case 4:
          snprintf(str, sizeof(str), "%d", value.sint32);
          break;
        case 8:
          snprintf(str, sizeof(str), "%lld", value.sint64);
          break;
        default:
          snprintf(str, sizeof(str), "error: unsupported sint byte size %d.",
                   info.size);
          break;
        }
        break;

      case IEEE754:
        switch (info.size) {
        case 4:
          snprintf(str, sizeof(str), "%f", value.float32);
          break;
        case 8:
          snprintf(str, sizeof(str), "%g", value.float64);
          break;
        default:
          snprintf(str, sizeof(str), "error: unsupported float byte size %d.",
                   info.size);
          break;
        }
        break;

      case Vector:
        if (info.size > 0) {
          switch (info.format) {
          case VectorOfSInt8:
            snprintf(str, sizeof(str), "%s", "sint8   { ");
            pos = str + strlen(str);
            for (uint32_t i = 0; i < info.size; ++i) {
              PRINT_COMMA_SEPARATOR;
              if (pos < end)
                pos +=
                    snprintf(pos, end - pos, "%d", (int32_t)value.v_sint8[i]);
            }
            strlcat(str, " }", sizeof(str));
            break;

          default:
            DNBLogError(
                "unsupported vector format %d, defaulting to hex bytes.",
                info.format);
            [[clang::fallthrough]];
          case VectorOfUInt8:
            snprintf(str, sizeof(str), "%s", "uint8   { ");
            pos = str + strlen(str);
            for (uint32_t i = 0; i < info.size; ++i) {
              PRINT_COMMA_SEPARATOR;
              if (pos < end)
                pos +=
                    snprintf(pos, end - pos, "%u", (uint32_t)value.v_uint8[i]);
            }
            break;

          case VectorOfSInt16:
            snprintf(str, sizeof(str), "%s", "sint16  { ");
            pos = str + strlen(str);
            for (uint32_t i = 0; i < info.size / 2; ++i) {
              PRINT_COMMA_SEPARATOR;
              if (pos < end)
                pos +=
                    snprintf(pos, end - pos, "%d", (int32_t)value.v_sint16[i]);
            }
            break;

          case VectorOfUInt16:
            snprintf(str, sizeof(str), "%s", "uint16  { ");
            pos = str + strlen(str);
            for (uint32_t i = 0; i < info.size / 2; ++i) {
              PRINT_COMMA_SEPARATOR;
              if (pos < end)
                pos +=
                    snprintf(pos, end - pos, "%u", (uint32_t)value.v_uint16[i]);
            }
            break;

          case VectorOfSInt32:
            snprintf(str, sizeof(str), "%s", "sint32  { ");
            pos = str + strlen(str);
            for (uint32_t i = 0; i < info.size / 4; ++i) {
              PRINT_COMMA_SEPARATOR;
              if (pos < end)
                pos +=
                    snprintf(pos, end - pos, "%d", (int32_t)value.v_sint32[i]);
            }
            break;

          case VectorOfUInt32:
            snprintf(str, sizeof(str), "%s", "uint32  { ");
            pos = str + strlen(str);
            for (uint32_t i = 0; i < info.size / 4; ++i) {
              PRINT_COMMA_SEPARATOR;
              if (pos < end)
                pos +=
                    snprintf(pos, end - pos, "%u", (uint32_t)value.v_uint32[i]);
            }
            break;

          case VectorOfFloat32:
            snprintf(str, sizeof(str), "%s", "float32 { ");
            pos = str + strlen(str);
            for (uint32_t i = 0; i < info.size / 4; ++i) {
              PRINT_COMMA_SEPARATOR;
              if (pos < end)
                pos += snprintf(pos, end - pos, "%f", value.v_float32[i]);
            }
            break;

          case VectorOfUInt128:
            snprintf(str, sizeof(str), "%s", "uint128 { ");
            pos = str + strlen(str);
            for (uint32_t i = 0; i < info.size / 16; ++i) {
              PRINT_COMMA_SEPARATOR;
              if (pos < end)
                pos += snprintf(pos, end - pos, "0x%16.16llx%16.16llx",
                                value.v_uint64[i], value.v_uint64[i + 1]);
            }
            break;
          }
          strlcat(str, " }", sizeof(str));
        } else {
          snprintf(str, sizeof(str), "error: unsupported vector size %d.",
                   info.size);
        }
        break;

      default:
        snprintf(str, sizeof(str), "error: unsupported register type %d.",
                 info.type);
        break;
      }
    }

    DNBLog("%s%4s = %s%s", pre ? pre : "", info.name, str, post ? post : "");
  }
}