# Copyright 2016-2024 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
load_lib dwarf.exp

# This test can only be run on targets which support DWARF-2 and use gas.
require dwarf2_support

standard_testfile dw2-fixed-point.c dw2-fixed-point-dw.S

# Make some DWARF for the test.
set asm_file [standard_output_file $srcfile2]
Dwarf::assemble $asm_file {
    cu {} {
 	DW_TAG_compile_unit {
                {DW_AT_language @DW_LANG_Ada95}
                {DW_AT_name     pck.ads}
                {DW_AT_comp_dir /tmp}
        } {
            declare_labels fp1_base_type fp2_base_type fp3_small \
                fp3_base_type fp1_range_type

            fp1_base_type: DW_TAG_base_type {
                {DW_AT_byte_size     1 DW_FORM_sdata}
                {DW_AT_encoding      @DW_ATE_signed_fixed}
                {DW_AT_name          pck__fp1_type}
                {DW_AT_binary_scale  -4 DW_FORM_sdata}
            }

            DW_TAG_variable {
                {DW_AT_name pck__fp1_var}
                {DW_AT_type :$fp1_base_type}
                {DW_AT_location {
                    DW_OP_addr [gdb_target_symbol pck__fp1_var]
                } SPECIAL_expr}
                {external 1 flag}
            }

            DW_TAG_variable {
                {DW_AT_name pck__fp1_var2}
                {DW_AT_type :$fp1_base_type}
                {DW_AT_location {
                    DW_OP_addr [gdb_target_symbol pck__fp1_var2]
                } SPECIAL_expr}
                {external 1 flag}
            }

            fp2_base_type: DW_TAG_base_type {
                {DW_AT_byte_size     1 DW_FORM_sdata}
                {DW_AT_encoding      @DW_ATE_signed_fixed}
                {DW_AT_name          pck__fp2_type}
                {DW_AT_decimal_scale -2 DW_FORM_sdata}
            }

            DW_TAG_variable {
                {DW_AT_name pck__fp2_var}
                {DW_AT_type :$fp2_base_type}
                {DW_AT_location {
                    DW_OP_addr [gdb_target_symbol pck__fp2_var]
                } SPECIAL_expr}
                {external 1 flag}
            }

            fp3_small: DW_TAG_constant {
                {DW_AT_GNU_numerator   1 DW_FORM_data1}
                {DW_AT_GNU_denominator 30 DW_FORM_sdata}
            }

            fp3_base_type: DW_TAG_base_type {
                {DW_AT_byte_size     1 DW_FORM_sdata}
                {DW_AT_encoding      @DW_ATE_signed_fixed}
                {DW_AT_name          pck__fp3_type}
                {DW_AT_small         :$fp3_small}
            }

            DW_TAG_variable {
                {DW_AT_name pck__fp3_var}
                {DW_AT_type :$fp3_base_type}
                {DW_AT_location {
                    DW_OP_addr [gdb_target_symbol pck__fp3_var]
                } SPECIAL_expr}
                {external 1 flag}
            }

            fp1_range_type: DW_TAG_subrange_type {
                 {DW_AT_lower_bound 0xf0 DW_FORM_data1}
                 {DW_AT_upper_bound 0x10 DW_FORM_data1}
                 {DW_AT_name foo__fp1_range_type}
                 {DW_AT_type :$fp1_base_type}
             }

             DW_TAG_variable {
                 {DW_AT_name pck__fp1_range_var}
                 {DW_AT_type :$fp1_range_type}
                 {DW_AT_location {
                     DW_OP_addr [gdb_target_symbol pck__fp1_range_var]
                 } SPECIAL_expr}
                 {external 1 flag}
             }
	}
    }
}

if { [prepare_for_testing ${testfile}.exp ${testfile} \
	  [list $srcfile $asm_file] {nodebug}] } {
    return -1
}

if ![runto_main] {
    return -1
}

# Do the testing in Ada mode, since this is the language for which
# this feature has been implemented, and where we know the language
# has the concept of fixed-point types.
gdb_test_no_output "set lang ada"

gdb_test "print pck.fp1_var" \
         " = 0.25"

gdb_test "print /x pck.fp1_var" \
         " = 0x0"

gdb_test "print pck.fp2_var" \
         " = -0.01"

gdb_test "print /x pck.fp2_var" \
         " = 0x0"

gdb_test "print pck.fp3_var" \
         " = 0.1"

gdb_test "print /x pck.fp3_var" \
         " = 0x0"

gdb_test "print pck.fp1_range_var" \
         " = 1"

gdb_test "print /x pck.fp1_range_var" \
         " = 0x1"

gdb_test "print pck.fp1_var + 0.25" \
         " = 0.5"

gdb_test "print pck.fp2_var - pck.fp2_var" \
         " = 0"

gdb_test "print pck.fp3_var * 1" \
         " = 0.1"

gdb_test "print pck.fp3_var / pck.fp3_var" \
         " = 1"

gdb_test "print pck.fp3_var / 0" \
         "Division by zero"

gdb_test "print pck.fp1_range_var - 0.5" \
         " = 0.5"

gdb_test "print -pck.fp1_var" \
         " = -0.25"

gdb_test "print pck.fp1_var = pck.fp1_var" \
         " = true"

gdb_test "print pck.fp1_var = pck.fp1_var2" \
         " = false"

gdb_test "print pck.fp1_var /= pck.fp1_var" \
         " = false"

gdb_test "print pck.fp1_var /= pck.fp1_var2" \
         " = true"

gdb_test "print pck.fp1_var < pck.fp1_var" \
         " = false"

gdb_test "print pck.fp1_var < pck.fp1_var2" \
         " = true"

gdb_test "print pck.fp1_var <= pck.fp1_var2" \
         " = true"

gdb_test "print pck.fp1_var <= pck.fp1_var" \
         " = true"

gdb_test "print pck.fp1_var > pck.fp1_var2" \
         " = false"

gdb_test "print pck.fp1_var2 > pck.fp1_var" \
         " = true"

gdb_test "print pck.fp1_var >= pck.fp1_var" \
         " = true"

gdb_test "print pck.fp1_var >= pck.fp1_var2" \
         " = false"

# Same as above, but with litterals...

gdb_test "print pck.fp1_var = 0.25" \
         " = true"

gdb_test "print pck.fp1_var = 0.5" \
         " = false"

gdb_test "print pck.fp1_var = 1" \
         " = false"

gdb_test "print pck.fp1_var /= 0.25" \
         " = false"

gdb_test "print pck.fp1_var /= 0.5" \
         " = true"

gdb_test "print pck.fp1_var /= 1" \
         " = true"

gdb_test "print pck.fp1_var < 0.25" \
         " = false"

gdb_test "print pck.fp1_var <  0.5" \
         " = true"

gdb_test "print pck.fp1_var <  1" \
         " = true"

gdb_test "print pck.fp1_var <= 0.25" \
         " = true"

gdb_test "print pck.fp1_var <= 0.5" \
         " = true"

gdb_test "print pck.fp1_var <= 1" \
         " = true"

gdb_test "print pck.fp1_var > 0.25" \
         " = false"

gdb_test "print pck.fp1_var > 0.5" \
         " = false"

gdb_test "print pck.fp1_var > 1" \
         " = false"

gdb_test "print pck.fp1_var >= 0.25" \
         " = true"

gdb_test "print pck.fp1_var >= 0.5" \
         " = false"

gdb_test "print pck.fp1_var >= 1" \
         " = false"


# Set the language to LANG and do a ptype test on pck__fp1_var,
# pck__fp2_var and pck__fp3_var, verifying that the output matches
# FP1_RE, FP2_RE, FP2_RE (resp.).

proc do_ptype_test {lang fp1_re fp2_re fp3_re fp1_range_re} {
    with_test_prefix "lang=$lang" {
        gdb_test_no_output "set language $lang" \
            "set language to $lang for ptype test"

        gdb_test "ptype pck__fp1_var" $fp1_re

        gdb_test "ptype pck__fp2_var" $fp2_re

        gdb_test "ptype pck__fp3_var" $fp3_re

        gdb_test "ptype pck__fp1_range_var" $fp1_range_re
    }
}

do_ptype_test "ada" \
              " = <1-byte fixed point \\(small = 1/16\\)>" \
              " = <1-byte fixed point \\(small = 1/100\\)>" \
              " = <1-byte fixed point \\(small = 1/30\\)>" \
              " = <1-byte fixed point \\(small = 1/16\\)>"

foreach lang [list "c" "d" "go" "objective-c" "opencl" ] {
    do_ptype_test $lang \
                  " = 1-byte fixed point \\(small = 1/16\\)" \
                  " = 1-byte fixed point \\(small = 1/100\\)" \
                  " = 1-byte fixed point \\(small = 1/30\\)" \
                  " = <range type>"
}

do_ptype_test "fortran" \
          " = pck__fp1_type" \
          " = pck__fp2_type" \
          " = pck__fp3_type" \
          " = <range type>"

do_ptype_test "modula-2" \
          " = pck__fp1_type" \
          " = pck__fp2_type" \
          " = pck__fp3_type" \
          " = \\\[1-byte fixed point \\(small = 1/16\\)\\.\\.1-byte fixed point \\(small = 1/16\\)\\\]"

do_ptype_test "pascal" \
          " = pck__fp1_type" \
          " = pck__fp2_type" \
          " = pck__fp3_type" \
          " = 1-byte fixed point \\(small = 1/16\\)\\.\\.1-byte fixed point \\(small = 1/16\\)"