# Copyright (C) 2013-2021 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-lib.exp
load_lib dwarf.exp

set asm_file unavailable-dwarf-piece-dw.S

Dwarf::assemble $asm_file {
    declare_labels uchar_label struct_s_label foo_label struct_t_label bar_label

    cu {} {
	compile_unit {{language @DW_LANG_C}} {
	    uchar_label: DW_TAG_base_type {
		{name "unsigned char"}
		{byte_size 1 DW_FORM_sdata}
		{encoding @DW_ATE_unsigned_char}
	    }

	    struct_s_label: DW_TAG_structure_type {
		{name s}
		{byte_size 3 DW_FORM_sdata}
		{decl_file 0 DW_FORM_udata}
		{decl_line 1 DW_FORM_sdata}
	    } {
		DW_TAG_member {
		    {name a}
		    {type :$uchar_label}
		    {data_member_location {
			DW_OP_plus_uconst 0
		    } SPECIAL_expr}
		}
		DW_TAG_member {
		    {name b}
		    {type :$uchar_label}
		    {data_member_location {
			DW_OP_plus_uconst 1
		    } SPECIAL_expr}
		}
		DW_TAG_member {
		    {name c}
		    {type :$uchar_label}
		    {data_member_location {
			DW_OP_plus_uconst 2
		    } SPECIAL_expr}
		}
	    }

	    struct_t_label: DW_TAG_structure_type {
		{name t}
		{byte_size 3 DW_FORM_sdata}
		{decl_file 0 DW_FORM_udata}
		{decl_line 1 DW_FORM_sdata}
	    } {
		DW_TAG_member {
		    {name a}
		    {type :$uchar_label}
		    {data_member_location {
			DW_OP_plus_uconst 0
		    } SPECIAL_expr}
		}
		DW_TAG_member {
		    {name b}
		    {type :$uchar_label}
		    {byte_size 1 DW_FORM_sdata}
		    {bit_size 1 DW_FORM_sdata}
		    {bit_offset 7 DW_FORM_sdata}
		    {data_member_location {
			DW_OP_plus_uconst 1
		    } SPECIAL_expr}
		}
		DW_TAG_member {
		    {name c}
		    {type :$uchar_label}
		    {byte_size 1 DW_FORM_sdata}
		    {bit_size 1 DW_FORM_sdata}
		    {bit_offset 6 DW_FORM_sdata}
		    {data_member_location {
			DW_OP_plus_uconst 1
		    } SPECIAL_expr}
		}
		DW_TAG_member {
		    {name d}
		    {type :$uchar_label}
		    {byte_size 1 DW_FORM_sdata}
		    {bit_size 1 DW_FORM_sdata}
		    {bit_offset 5 DW_FORM_sdata}
		    {data_member_location {
			DW_OP_plus_uconst 1
		    } SPECIAL_expr}
		}
		DW_TAG_member {
		    {name e}
		    {type :$uchar_label}
		    {byte_size 1 DW_FORM_sdata}
		    {bit_size 1 DW_FORM_sdata}
		    {bit_offset 4 DW_FORM_sdata}
		    {data_member_location {
			DW_OP_plus_uconst 1
		    } SPECIAL_expr}
		}
		DW_TAG_member {
		    {name f}
		    {type :$uchar_label}
		    {byte_size 1 DW_FORM_sdata}
		    {bit_size 1 DW_FORM_sdata}
		    {bit_offset 3 DW_FORM_sdata}
		    {data_member_location {
			DW_OP_plus_uconst 1
		    } SPECIAL_expr}
		}
		DW_TAG_member {
		    {name g}
		    {type :$uchar_label}
		    {byte_size 1 DW_FORM_sdata}
		    {bit_size 1 DW_FORM_sdata}
		    {bit_offset 2 DW_FORM_sdata}
		    {data_member_location {
			DW_OP_plus_uconst 1
		    } SPECIAL_expr}
		}
		DW_TAG_member {
		    {name h}
		    {type :$uchar_label}
		    {byte_size 1 DW_FORM_sdata}
		    {bit_size 1 DW_FORM_sdata}
		    {bit_offset 1 DW_FORM_sdata}
		    {data_member_location {
			DW_OP_plus_uconst 1
		    } SPECIAL_expr}
		}
		DW_TAG_member {
		    {name i}
		    {type :$uchar_label}
		    {byte_size 1 DW_FORM_sdata}
		    {bit_size 1 DW_FORM_sdata}
		    {bit_offset 0 DW_FORM_sdata}
		    {data_member_location {
			DW_OP_plus_uconst 1
		    } SPECIAL_expr}
		}
		DW_TAG_member {
		    {name j}
		    {type :$uchar_label}
		    {data_member_location {
			DW_OP_plus_uconst 2
		    } SPECIAL_expr}
		}
	    }

	    DW_TAG_subprogram {
		{name foo}
		{decl_file 0 udata}
		{low_pc foo_start_lbl addr}
		{high_pc foo_end_lbl addr}
	    } {
		DW_TAG_formal_parameter {
		    {type :$struct_s_label}
		    {name x}
		    {location {
			DW_OP_lit0
			DW_OP_stack_value
			DW_OP_piece 2
			DW_OP_reg0
			DW_OP_piece 1
		    } SPECIAL_expr}
		}
		DW_TAG_formal_parameter {
		    {type :$struct_s_label}
		    {name y}
		    {location {
			DW_OP_lit0
			DW_OP_stack_value
			DW_OP_piece 1
			DW_OP_reg0
			DW_OP_piece 1
			DW_OP_lit0
			DW_OP_stack_value
			DW_OP_piece 1
		    } SPECIAL_expr}
		}
		DW_TAG_formal_parameter {
		    {type :$struct_s_label}
		    {name z}
		    {location {
			DW_OP_reg0
			DW_OP_piece 1
			DW_OP_lit0
			DW_OP_stack_value
			DW_OP_piece 2
		    } SPECIAL_expr}
		}
	    }


	    DW_TAG_subprogram {
		{name bar}
		{decl_file 0 udata}
		{low_pc bar_start_lbl addr}
		{high_pc bar_end_lbl addr}
	    } {
		DW_TAG_formal_parameter {
		    {type :$struct_t_label}
		    {name x}
		    {location {
			DW_OP_lit0
			DW_OP_stack_value
			DW_OP_piece 1
			DW_OP_reg0
			DW_OP_bit_piece 1 0
			DW_OP_lit0
			DW_OP_stack_value
			DW_OP_bit_piece 7 0
			DW_OP_lit0
			DW_OP_stack_value
			DW_OP_piece 1
		    } SPECIAL_expr}
		}
		DW_TAG_formal_parameter {
		    {type :$struct_t_label}
		    {name y}
		    {location {
			DW_OP_lit0
			DW_OP_stack_value
			DW_OP_piece 1
			DW_OP_lit0
			DW_OP_stack_value
			DW_OP_bit_piece 3 0
			DW_OP_reg0
			DW_OP_bit_piece 1 0
			DW_OP_lit0
			DW_OP_stack_value
			DW_OP_bit_piece 4 0
			DW_OP_lit0
			DW_OP_stack_value
			DW_OP_piece 1
		    } SPECIAL_expr}
		}
		DW_TAG_formal_parameter {
		    {type :$struct_t_label}
		    {name z}
		    {location {
			DW_OP_lit0
			DW_OP_stack_value
			DW_OP_piece 1
			DW_OP_lit0
			DW_OP_stack_value
			DW_OP_bit_piece 7 0
			DW_OP_reg0
			DW_OP_bit_piece 1 0
			DW_OP_lit0
			DW_OP_stack_value
			DW_OP_piece 1
		    } SPECIAL_expr}
		}
	    }

	}
    }
}
