1 /* Self tests for gdbarch for GDB, the GNU debugger.
3 Copyright (C) 2017-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "gdbsupport/selftest.h"
22 #include "selftest-arch.h"
24 #include "test-target.h"
25 #include "target-float.h"
26 #include "gdbsupport/def-vector.h"
28 #include "scoped-mock-context.h"
32 /* Test gdbarch methods register_to_value and value_to_register. */
35 register_to_value_test (struct gdbarch
*gdbarch
)
37 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
38 struct type
*types
[] =
40 builtin
->builtin_void
,
41 builtin
->builtin_char
,
42 builtin
->builtin_short
,
44 builtin
->builtin_long
,
45 builtin
->builtin_signed_char
,
46 builtin
->builtin_unsigned_short
,
47 builtin
->builtin_unsigned_int
,
48 builtin
->builtin_unsigned_long
,
49 builtin
->builtin_float
,
50 builtin
->builtin_double
,
51 builtin
->builtin_long_double
,
52 builtin
->builtin_complex
,
53 builtin
->builtin_double_complex
,
54 builtin
->builtin_string
,
55 builtin
->builtin_bool
,
56 builtin
->builtin_long_long
,
57 builtin
->builtin_unsigned_long_long
,
58 builtin
->builtin_int8
,
59 builtin
->builtin_uint8
,
60 builtin
->builtin_int16
,
61 builtin
->builtin_uint16
,
62 builtin
->builtin_int32
,
63 builtin
->builtin_uint32
,
64 builtin
->builtin_int64
,
65 builtin
->builtin_uint64
,
66 builtin
->builtin_int128
,
67 builtin
->builtin_uint128
,
68 builtin
->builtin_char16
,
69 builtin
->builtin_char32
,
72 scoped_mock_context
<test_target_ops
> mockctx (gdbarch
);
74 struct frame_info
*frame
= get_current_frame ();
75 const int num_regs
= gdbarch_num_cooked_regs (gdbarch
);
77 /* Test gdbarch methods register_to_value and value_to_register with
78 different combinations of register numbers and types. */
79 for (const auto &type
: types
)
81 for (auto regnum
= 0; regnum
< num_regs
; regnum
++)
83 if (gdbarch_convert_register_p (gdbarch
, regnum
, type
))
85 std::vector
<gdb_byte
> expected (type
->length (), 0);
87 if (type
->code () == TYPE_CODE_FLT
)
89 /* Generate valid float format. */
90 target_float_from_string (expected
.data (), type
, "1.25");
94 for (auto j
= 0; j
< expected
.size (); j
++)
95 expected
[j
] = (regnum
+ j
) % 16;
98 gdbarch_value_to_register (gdbarch
, frame
, regnum
, type
,
101 /* Allocate two bytes more for overflow check. */
102 std::vector
<gdb_byte
> buf (type
->length () + 2, 0);
103 int optim
, unavail
, ok
;
105 /* Set the fingerprint in the last two bytes. */
106 buf
[type
->length ()]= 'w';
107 buf
[type
->length () + 1]= 'l';
108 ok
= gdbarch_register_to_value (gdbarch
, frame
, regnum
, type
,
109 buf
.data (), &optim
, &unavail
);
113 SELF_CHECK (!unavail
);
115 SELF_CHECK (buf
[type
->length ()] == 'w');
116 SELF_CHECK (buf
[type
->length () + 1] == 'l');
118 for (auto k
= 0; k
< type
->length (); k
++)
119 SELF_CHECK (buf
[k
] == expected
[k
]);
125 } // namespace selftests
127 void _initialize_gdbarch_selftests ();
129 _initialize_gdbarch_selftests ()
131 selftests::register_test_foreach_arch ("register_to_value",
132 selftests::register_to_value_test
);