1# Copyright 2022-2023 Free Software Foundation, Inc.
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 3 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16# Check the formatting of the fcsr, fflags, and frm registers in the
17# output of the 'info registers' command.
18
19if {![istarget "riscv*-*-*"]} {
20    verbose "Skipping ${gdb_test_file_name}."
21    return
22}
23
24if { [gdb_skip_float_test] } {
25    untested "no floating point support"
26    return
27}
28
29standard_testfile
30
31if {[prepare_for_testing "failed to prepare" $testfile $srcfile debug]} {
32    return -1
33}
34
35if {![runto_main]} {
36   return 0
37}
38
39# Merge FFLAGS_VALUE and FRM_VALUE into a single hexadecimal value
40# that can be written to the fcsr register.  The two arguments should
41# be the value of each of the two fields within the fcsr register.
42proc merge_fflags_and_frm { fflags_value frm_value } {
43    set fcsr_value 0x[format %x [expr $fflags_value | ($frm_value << 5)]]
44    return $fcsr_value
45}
46
47# Use 'info registers' to check the current values of the fflags, frm,
48# and fcsr registers.  The value in fcsr should consist of the
49# FFLAGS_VALUE and FRM_VALUE, and the frm field of the fcsr register
50# should have the text FRM_STRING associated with it.
51proc check_fcsr { fflags_value frm_value frm_string } {
52    # Merge fflags and frm values into a single fcsr value.
53    set fcsr_value [merge_fflags_and_frm $fflags_value $frm_value]
54
55    # Build up all the patterns we will need for this test.
56    set frm_str_re [string_to_regexp "$frm_string"]
57    set frm_val_re [format %d ${frm_value}]
58
59    set nv [format %d [expr ($fflags_value >> 4) & 0x1]]
60    set dz [format %d [expr ($fflags_value >> 3) & 0x1]]
61    set of [format %d [expr ($fflags_value >> 2) & 0x1]]
62    set uf [format %d [expr ($fflags_value >> 1) & 0x1]]
63    set nx [format %d [expr ($fflags_value >> 0) & 0x1]]
64
65    set fflags_pattern "NV:${nv} DZ:${dz} OF:${of} UF:${uf} NX:${nx}"
66    set frm_pattern "FRM:${frm_val_re} \\\[${frm_str_re}\\\]"
67    set fcsr_pattern "${fflags_pattern} ${frm_pattern}"
68
69    # Now use 'info registers' to check the register values.
70    array set reg_counts {}
71    gdb_test_multiple "info registers \$fflags \$frm \$fcsr" "" {
72	-re "^info registers\[^\r\n\]+\r\n" {
73	    exp_continue
74	}
75
76	-re "^(frm)\\s+${frm_value}\\s+${frm_pattern}\r\n" {
77	    set reg_name $expect_out(1,string)
78	    incr reg_counts($reg_name)
79	    exp_continue
80	}
81
82	-re "^(fflags)\\s+${fflags_value}\\s+${fflags_pattern}\r\n" {
83	    set reg_name $expect_out(1,string)
84	    incr reg_counts($reg_name)
85	    exp_continue
86	}
87
88	-re "^(fcsr)\\s+${fcsr_value}\\s+${fcsr_pattern}\r\n" {
89	    set reg_name $expect_out(1,string)
90	    incr reg_counts($reg_name)
91	    exp_continue
92	}
93
94	-re "^$::gdb_prompt $" {
95	    pass $gdb_test_name
96	}
97    }
98
99    # Check that each register is seen only once.
100    foreach reg {fflags frm fcsr} {
101	gdb_assert { $reg_counts($reg) == 1 } \
102	    "check we saw $reg just once"
103    }
104}
105
106# Set the fcsr register based on FFLAGS_VALUE and FRM_VALUE, then
107# check that the value is displayed correctly in the 'info registers'
108# output.  FRM_STRING should appear in the 'info registers' output
109# next to the frm field.
110proc test_fcsr { fflags_value frm_value frm_string } {
111    # Merge fflags and frm values into a single fcsr value.
112    set fcsr_value [merge_fflags_and_frm $fflags_value $frm_value]
113
114    with_test_prefix "fcsr=${fcsr_value}" {
115	# Set the fcsr value directly.
116	gdb_test_no_output "set \$fcsr = ${fcsr_value}"
117
118	with_test_prefix "set through fcsr" {
119	    check_fcsr $fflags_value $frm_value $frm_string
120	}
121
122	# Reset fcsr register back to zero.
123	gdb_test_no_output "set \$fcsr = 0x0" \
124	    "reset fcsr back to 0x0"
125	gdb_test "p/x \$fcsr" " = 0x0"
126
127	# Now set fcsr value through fflags and frm.
128	gdb_test_no_output "set \$fflags = ${fflags_value}"
129	gdb_test_no_output "set \$frm = ${frm_value}"
130
131	with_test_prefix "set through fflags and frm" {
132	    check_fcsr $fflags_value $frm_value $frm_string
133	}
134    }
135}
136
137# Check each valid value of the fflags register.
138for { set i 0 } { $i < 32 } { incr i } {
139    test_fcsr 0x[format %x $i] 0x0 "RNE (round to nearest; ties to even)"
140}
141
142# Check each valid value of the frm register.
143test_fcsr 0x0 0x1 "RTZ (Round towards zero)"
144test_fcsr 0x0 0x2 "RDN (Round down towards -INF)"
145test_fcsr 0x0 0x3 "RUP (Round up towards +INF)"
146test_fcsr 0x0 0x4 "RMM (Round to nearest; ties to max magnitude)"
147test_fcsr 0x0 0x5 "INVALID\[5\]"
148test_fcsr 0x0 0x6 "INVALID\[6\]"
149test_fcsr 0x0 0x7 "INVALID\[7\] (Dynamic rounding mode)"
150