Automatic date update in version.in
[deliverable/binutils-gdb.git] / gdb / gdbarch-selftests.c
CommitLineData
b77b02a5
YQ
1/* Self tests for gdbarch for GDB, the GNU debugger.
2
3666a048 3 Copyright (C) 2017-2021 Free Software Foundation, Inc.
b77b02a5
YQ
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20#include "defs.h"
268a13a5 21#include "gdbsupport/selftest.h"
b77b02a5 22#include "selftest-arch.h"
55b11ddf 23#include "target.h"
c180496d 24#include "test-target.h"
f69fdf9b 25#include "target-float.h"
268a13a5 26#include "gdbsupport/def-vector.h"
0d12e84c 27#include "gdbarch.h"
236ef034 28#include "scoped-mock-context.h"
b77b02a5
YQ
29
30namespace selftests {
31
b77b02a5
YQ
32/* Test gdbarch methods register_to_value and value_to_register. */
33
34static void
35register_to_value_test (struct gdbarch *gdbarch)
36{
37 const struct builtin_type *builtin = builtin_type (gdbarch);
38 struct type *types[] =
39 {
40 builtin->builtin_void,
41 builtin->builtin_char,
42 builtin->builtin_short,
43 builtin->builtin_int,
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,
70 };
71
236ef034 72 scoped_mock_context<test_target_ops> mockctx (gdbarch);
55b11ddf
PA
73
74 struct frame_info *frame = get_current_frame ();
f6efe3f8 75 const int num_regs = gdbarch_num_cooked_regs (gdbarch);
b77b02a5 76
b77b02a5
YQ
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)
80 {
81 for (auto regnum = 0; regnum < num_regs; regnum++)
82 {
83 if (gdbarch_convert_register_p (gdbarch, regnum, type))
84 {
85 std::vector<gdb_byte> expected (TYPE_LENGTH (type), 0);
86
78134374 87 if (type->code () == TYPE_CODE_FLT)
b77b02a5 88 {
b77b02a5 89 /* Generate valid float format. */
f69fdf9b 90 target_float_from_string (expected.data (), type, "1.25");
b77b02a5
YQ
91 }
92 else
93 {
94 for (auto j = 0; j < expected.size (); j++)
95 expected[j] = (regnum + j) % 16;
96 }
97
98 gdbarch_value_to_register (gdbarch, frame, regnum, type,
99 expected.data ());
100
101 /* Allocate two bytes more for overflow check. */
102 std::vector<gdb_byte> buf (TYPE_LENGTH (type) + 2, 0);
103 int optim, unavail, ok;
104
105 /* Set the fingerprint in the last two bytes. */
106 buf [TYPE_LENGTH (type)]= 'w';
107 buf [TYPE_LENGTH (type) + 1]= 'l';
108 ok = gdbarch_register_to_value (gdbarch, frame, regnum, type,
109 buf.data (), &optim, &unavail);
110
111 SELF_CHECK (ok);
112 SELF_CHECK (!optim);
113 SELF_CHECK (!unavail);
114
115 SELF_CHECK (buf[TYPE_LENGTH (type)] == 'w');
116 SELF_CHECK (buf[TYPE_LENGTH (type) + 1] == 'l');
117
118 for (auto k = 0; k < TYPE_LENGTH(type); k++)
119 SELF_CHECK (buf[k] == expected[k]);
120 }
121 }
122 }
123}
124
125} // namespace selftests
b77b02a5 126
6c265988 127void _initialize_gdbarch_selftests ();
b77b02a5 128void
6c265988 129_initialize_gdbarch_selftests ()
b77b02a5 130{
1526853e
SM
131 selftests::register_test_foreach_arch ("register_to_value",
132 selftests::register_to_value_test);
b77b02a5 133}
This page took 0.392195 seconds and 4 git commands to generate.