Home | History | Annotate | Line # | Download | only in gdb.dwarf2
      1 # Copyright 2018-2024 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 # Test DW_TAG_variant_part and DW_TAG_variant.
     17 
     18 load_lib dwarf.exp
     19 
     20 # This test can only be run on targets which support DWARF-2 and use
     21 # gas.
     22 require dwarf2_support
     23 
     24 standard_testfile .c .S
     25 
     26 # Make some DWARF for the test.
     27 set asm_file [standard_output_file $srcfile2]
     28 Dwarf::assemble $asm_file {
     29     upvar cu_lang cu_lang
     30 
     31     declare_labels uinteger_label float_label int8_label
     32     declare_labels discr_1_label discr_2_label discr_3_label
     33     declare_labels one_label two_label
     34 
     35     # Creating a CU with 4-byte addresses lets this test link on
     36     # both 32- and 64-bit machines.
     37     cu { addr_size 4 } {
     38 	compile_unit {
     39 	    {name file1.txt}
     40 	    {language @DW_LANG_Rust}
     41 	} {
     42             uinteger_label: DW_TAG_base_type {
     43                 {DW_AT_byte_size 4 DW_FORM_sdata}
     44                 {DW_AT_encoding  @DW_ATE_unsigned}
     45                 {DW_AT_name      {unsigned integer}}
     46             }
     47 
     48             int8_label: DW_TAG_base_type {
     49                 {DW_AT_byte_size 1 DW_FORM_sdata}
     50                 {DW_AT_encoding  @DW_ATE_signed}
     51                 {DW_AT_name      i8}
     52             }
     53 
     54 	    float_label: base_type {
     55 		{name float}
     56 		{encoding @DW_ATE_float}
     57 		{byte_size 4 DW_FORM_sdata}
     58 	    }
     59 
     60 	    one_label: structure_type {
     61 		{name One}
     62 		{byte_size 4 DW_FORM_sdata}
     63 	    } {
     64 		member {
     65 		    {name __0}
     66 		    {type :$uinteger_label}
     67 		    {data_member_location 0 data1}
     68 		}
     69 	    }
     70 
     71 	    two_label: structure_type {
     72 		{name Two}
     73 		{byte_size 4 DW_FORM_sdata}
     74 	    } {
     75 		member {
     76 		    {name __0}
     77 		    {type :$float_label}
     78 		    {data_member_location 0 data1}
     79 		}
     80 	    }
     81 
     82 	    structure_type {
     83 		{name Simple}
     84 		{byte_size 8 DW_FORM_sdata}
     85 	    } {
     86 		variant_part {
     87 		    {discr :$discr_1_label DW_FORM_ref4}
     88 		} {
     89 		    discr_1_label: member {
     90 			{type :$uinteger_label}
     91 			{data_member_location 0 data1}
     92 			{artificial 1 DW_FORM_flag_present}
     93 		    }
     94 
     95 		    variant {
     96 			{discr_value 23 udata}
     97 		    } {
     98 			member {
     99 			    {type :$one_label}
    100 			    {data_member_location 4 data1}
    101 			}
    102 		    }
    103 
    104 		    variant {
    105 			{discr_value 1 udata}
    106 		    } {
    107 			member {
    108 			    {type :$two_label}
    109 			    {data_member_location 4 data1}
    110 			}
    111 		    }
    112 		}
    113 	    }
    114 
    115 	    structure_type {
    116 		{name Defaulted}
    117 		{byte_size 8 DW_FORM_sdata}
    118 	    } {
    119 		variant_part {
    120 		    {discr :$discr_2_label DW_FORM_ref4}
    121 		} {
    122 		    discr_2_label: member {
    123 			{type :$uinteger_label}
    124 			{data_member_location 0 data1}
    125 			{artificial 1 DW_FORM_flag_present}
    126 		    }
    127 
    128 		    variant {
    129 		    } {
    130 			member {
    131 			    {type :$one_label}
    132 			    {data_member_location 4 data1}
    133 			}
    134 		    }
    135 
    136 		    variant {
    137 			{discr_value 1 udata}
    138 		    } {
    139 			member {
    140 			    {type :$two_label}
    141 			    {data_member_location 4 data1}
    142 			}
    143 		    }
    144 		}
    145 	    }
    146 
    147 	    structure_type {
    148 		{name Univariant}
    149 		{byte_size 8 DW_FORM_sdata}
    150 	    } {
    151 		variant_part {
    152 		} {
    153 		    variant {
    154 		    } {
    155 			member {
    156 			    {type :$one_label}
    157 			    {data_member_location 4 data1}
    158 			}
    159 		    }
    160 		}
    161 	    }
    162 
    163 	    # Rust won't emit a negative discriminant like this, but
    164 	    # we want to test the code path anyway.
    165 	    structure_type {
    166 		{name Negative}
    167 		{byte_size 8 DW_FORM_sdata}
    168 	    } {
    169 		variant_part {
    170 		    {discr :$discr_3_label DW_FORM_ref4}
    171 		} {
    172 		    discr_3_label: member {
    173 			{type :$int8_label}
    174 			{data_member_location 0 data1}
    175 			{artificial 1 DW_FORM_flag_present}
    176 		    }
    177 
    178 		    variant {
    179 			{discr_value -1 sdata}
    180 		    } {
    181 			member {
    182 			    {type :$one_label}
    183 			    {data_member_location 4 data1}
    184 			}
    185 		    }
    186 
    187 		    # Make this the default value so we'll see an
    188 		    # incorrect result if we mishandle signed
    189 		    # discriminants.
    190 		    variant {
    191 		    } {
    192 			member {
    193 			    {type :$two_label}
    194 			    {data_member_location 4 data1}
    195 			}
    196 		    }
    197 		}
    198 	    }
    199 	}
    200     }
    201 }
    202 
    203 if { [prepare_for_testing "failed to prepare" ${testfile} \
    204 	  [list $srcfile $asm_file] debug] } {
    205     return -1
    206 }
    207 
    208 if ![runto func] {
    209     return -1
    210 }
    211 
    212 # Get the values into history so we can use it from Rust.
    213 gdb_test "print (void *) buffer" "\\\$1 = .void .. $hex .buffer."
    214 gdb_test "print (void *) buffer2" "\\\$2 = .void .. $hex .buffer2."
    215 
    216 gdb_test "set language rust"
    217 gdb_test "print *(\$1 as *mut Simple)" " = One\\(23\\)" \
    218     "print as Simple"
    219 gdb_test "print *(\$1 as *mut Defaulted)" " = One\\(23\\)" \
    220     "print as Defaulted"
    221 gdb_test "print *(\$1 as *mut Univariant)" " = One\\(23\\)" \
    222     "print as Univariant"
    223 
    224 gdb_test "print *(\$2 as *mut Negative)" " = One\\(23\\)" \
    225     "print as Negative"
    226