tree-optimization/114485 - neg induction with partial vectors
[official-gcc.git] / gcc / dwarf2ctf.cc
blob77d6bf89689394f22d65bec5ff5e01278b8af820
1 /* Generate CTF types and objects from the GCC DWARF.
2 Copyright (C) 2021-2024 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "target.h"
24 #include "dwarf2out.h"
25 #include "dwarf2out.h"
27 #include "dwarf2ctf.h"
28 #include "ctfc.h"
30 /* Forward declarations for some routines defined in this file. */
32 static ctf_id_t
33 gen_ctf_type (ctf_container_ref, dw_die_ref);
35 /* All the DIE structures we handle come from the DWARF information
36 generated by GCC. However, there are three situations where we need
37 to create our own created DIE structures because GCC doesn't
38 provide them.
40 The DWARF spec suggests using a DIE with DW_TAG_unspecified_type
41 and name "void" in order to denote the void type. But GCC doesn't
42 follow this advice. Still we need a DIE to act as a key for void
43 types, we use ctf_void_die.
45 Also, if a subrange type corresponding to an array index does not
46 specify a type then we assume it is `int'.
48 Finally, for types unrepresentable in CTF, we need a DIE to anchor
49 them to a CTF type of kind unknown.
51 The variables below are initialized in ctf_debug_init and hold
52 references to the proper DIEs. */
54 static GTY (()) dw_die_ref ctf_void_die;
55 static GTY (()) dw_die_ref ctf_array_index_die;
56 static GTY (()) dw_die_ref ctf_unknown_die;
58 /* Some DIEs have a type description attribute, stored in a DW_AT_type
59 attribute. However, GCC generates no attribute to signify a `void'
60 type.
62 This can happen in many contexts (return type of a function,
63 pointed or qualified type, etc) so we use the `ctf_get_AT_type'
64 function below abstracts this. */
66 static dw_die_ref
67 ctf_get_AT_type (dw_die_ref die)
69 dw_die_ref type_die = get_AT_ref (die, DW_AT_type);
70 return (type_die ? type_die : ctf_void_die);
73 /* Some data member DIEs have location specified as a DWARF expression
74 (specifically in DWARF2). Luckily, the expression is a simple
75 DW_OP_plus_uconst with one operand set to zero.
77 Sometimes the data member location may also be negative. In yet some other
78 cases (specifically union data members), the data member location is
79 non-existent. Handle all these scenarios here to abstract this. */
81 static HOST_WIDE_INT
82 ctf_get_AT_data_member_location (dw_die_ref die)
84 HOST_WIDE_INT field_location = 0;
85 dw_attr_node * attr;
87 /* The field location (in bits) can be determined from
88 either a DW_AT_data_member_location attribute or a
89 DW_AT_data_bit_offset attribute. */
90 if (get_AT (die, DW_AT_data_bit_offset))
91 field_location = get_AT_unsigned (die, DW_AT_data_bit_offset);
92 else
94 attr = get_AT (die, DW_AT_data_member_location);
95 if (attr && AT_class (attr) == dw_val_class_loc)
97 dw_loc_descr_ref descr = AT_loc (attr);
98 /* Operand 2 must be zero; the structure is assumed to be on the
99 stack in DWARF2. */
100 gcc_assert (!descr->dw_loc_oprnd2.v.val_unsigned);
101 gcc_assert (descr->dw_loc_oprnd2.val_class
102 == dw_val_class_unsigned_const);
103 field_location = descr->dw_loc_oprnd1.v.val_unsigned * 8;
105 else
107 attr = get_AT (die, DW_AT_data_member_location);
108 if (attr && AT_class (attr) == dw_val_class_const)
109 field_location = AT_int (attr) * 8;
110 else
111 field_location = (get_AT_unsigned (die,
112 DW_AT_data_member_location)
113 * 8);
117 return field_location;
120 /* CTF Types' and CTF Variables' Location Information. CTF section does not
121 emit location information, this is provided for BTF CO-RE use-cases. These
122 functions fetch information from DWARF Die directly, as such the location
123 information is not buffered in the CTF container. */
125 const char *
126 ctf_get_die_loc_file (dw_die_ref die)
128 if (!die)
129 return NULL;
131 struct dwarf_file_data * file;
132 file = get_AT_file (die, DW_AT_decl_file);
133 if (!file)
134 return NULL;
136 return file->filename;
139 unsigned int
140 ctf_get_die_loc_line (dw_die_ref die)
142 if (!die)
143 return 0;
145 return get_AT_unsigned (die, DW_AT_decl_line);
148 unsigned int
149 ctf_get_die_loc_col (dw_die_ref die)
151 if (!die)
152 return 0;
154 return get_AT_unsigned (die, DW_AT_decl_column);
157 /* Generate CTF for the void type. */
159 static ctf_id_t
160 gen_ctf_void_type (ctf_container_ref ctfc)
162 ctf_encoding_t ctf_encoding = {0, 0, 0};
164 /* In CTF the void type is encoded as a 0-byte signed integer
165 type. */
167 ctf_encoding.cte_bits = 0;
168 ctf_encoding.cte_format = CTF_INT_SIGNED;
170 gcc_assert (ctf_void_die != NULL);
171 return ctf_add_integer (ctfc, CTF_ADD_ROOT, "void",
172 &ctf_encoding, ctf_void_die);
175 /* Generate CTF type of unknown kind. */
177 static ctf_id_t
178 gen_ctf_unknown_type (ctf_container_ref ctfc)
180 ctf_id_t unknown_type_id;
182 /* In CTF, the unknown type is encoded as a 0 byte sized type with kind
183 CTF_K_UNKNOWN. Create an encoding object merely to reuse the underlying
184 ctf_add_encoded interface; the CTF encoding object is not 'used' any more
185 than just the generation of size from. */
186 ctf_encoding_t ctf_encoding = {0, 0, 0};
188 gcc_assert (ctf_unknown_die != NULL);
189 /* Type de-duplication. */
190 if (!ctf_type_exists (ctfc, ctf_unknown_die, &unknown_type_id))
191 unknown_type_id = ctf_add_unknown (ctfc, CTF_ADD_ROOT, "unknown",
192 &ctf_encoding, ctf_unknown_die);
194 return unknown_type_id;
197 /* Sizes of entities can be given in bytes or bits. This function
198 abstracts this by returning the size in bits of the given entity.
199 If no DW_AT_byte_size nor DW_AT_bit_size are defined, this function
200 returns 0. */
202 static uint32_t
203 ctf_die_bitsize (dw_die_ref die)
205 dw_attr_node *attr_byte_size = get_AT (die, DW_AT_byte_size);
206 dw_attr_node *attr_bit_size = get_AT (die, DW_AT_bit_size);
208 if (attr_bit_size)
209 return AT_unsigned (attr_bit_size);
210 else if (attr_byte_size)
211 return (AT_unsigned (attr_byte_size) * 8);
212 else
213 return 0;
216 /* Generate CTF for base type (integer, boolean, real, fixed point and complex).
217 Important: the caller of this API must make sure that duplicate types are
218 not added. */
220 static ctf_id_t
221 gen_ctf_base_type (ctf_container_ref ctfc, dw_die_ref type)
223 ctf_id_t type_id = CTF_NULL_TYPEID;
225 ctf_encoding_t ctf_encoding = {0, 0, 0};
227 unsigned int encoding = get_AT_unsigned (type, DW_AT_encoding);
228 unsigned int bit_size = ctf_die_bitsize (type);
229 const char * name_string = get_AT_string (type, DW_AT_name);
231 switch (encoding)
233 case DW_ATE_void:
235 ctf_encoding.cte_format = CTF_INT_SIGNED;
236 ctf_encoding.cte_bits = 0;
238 gcc_assert (name_string);
239 type_id = ctf_add_integer (ctfc, CTF_ADD_ROOT, name_string,
240 &ctf_encoding, type);
242 break;
243 case DW_ATE_boolean:
245 ctf_encoding.cte_format = CTF_INT_BOOL;
246 ctf_encoding.cte_bits = bit_size;
248 gcc_assert (name_string);
249 type_id = ctf_add_integer (ctfc, CTF_ADD_ROOT, name_string,
250 &ctf_encoding, type);
251 break;
252 case DW_ATE_float:
254 unsigned int float_bit_size
255 = tree_to_uhwi (TYPE_SIZE (float_type_node));
256 unsigned int double_bit_size
257 = tree_to_uhwi (TYPE_SIZE (double_type_node));
258 unsigned int long_double_bit_size
259 = tree_to_uhwi (TYPE_SIZE (long_double_type_node));
261 if (bit_size == float_bit_size)
262 ctf_encoding.cte_format = CTF_FP_SINGLE;
263 else if (bit_size == double_bit_size)
264 ctf_encoding.cte_format = CTF_FP_DOUBLE;
265 else if (bit_size == long_double_bit_size)
266 ctf_encoding.cte_format = CTF_FP_LDOUBLE;
267 else
268 /* CTF does not have representation for other types. Skip them. */
269 break;
271 ctf_encoding.cte_bits = bit_size;
272 type_id = ctf_add_float (ctfc, CTF_ADD_ROOT, name_string,
273 &ctf_encoding, type);
275 break;
277 case DW_ATE_signed_char:
278 /* FALLTHROUGH */
279 case DW_ATE_unsigned_char:
280 /* FALLTHROUGH */
281 case DW_ATE_signed:
282 /* FALLTHROUGH */
283 case DW_ATE_unsigned:
285 if (encoding == DW_ATE_signed_char
286 || encoding == DW_ATE_unsigned_char)
287 ctf_encoding.cte_format |= CTF_INT_CHAR;
289 if (encoding == DW_ATE_signed
290 || encoding == DW_ATE_signed_char)
291 ctf_encoding.cte_format |= CTF_INT_SIGNED;
293 ctf_encoding.cte_bits = bit_size;
294 type_id = ctf_add_integer (ctfc, CTF_ADD_ROOT, name_string,
295 &ctf_encoding, type);
296 break;
298 case DW_ATE_complex_float:
300 unsigned int float_bit_size
301 = tree_to_uhwi (TYPE_SIZE (float_type_node));
302 unsigned int double_bit_size
303 = tree_to_uhwi (TYPE_SIZE (double_type_node));
304 unsigned int long_double_bit_size
305 = tree_to_uhwi (TYPE_SIZE (long_double_type_node));
307 if (bit_size == float_bit_size * 2)
308 ctf_encoding.cte_format = CTF_FP_CPLX;
309 else if (bit_size == double_bit_size * 2)
310 ctf_encoding.cte_format = CTF_FP_DCPLX;
311 else if (bit_size == long_double_bit_size * 2)
312 ctf_encoding.cte_format = CTF_FP_LDCPLX;
313 else
314 /* CTF does not have representation for other types. Skip them. */
315 break;
317 ctf_encoding.cte_bits = bit_size;
318 type_id = ctf_add_float (ctfc, CTF_ADD_ROOT, name_string,
319 &ctf_encoding, type);
320 break;
322 default:
323 /* Ignore. */
324 break;
327 return type_id;
330 /* Generate CTF for a pointer type. */
332 static ctf_id_t
333 gen_ctf_pointer_type (ctf_container_ref ctfc, dw_die_ref ptr_type)
335 ctf_id_t type_id = CTF_NULL_TYPEID;
336 ctf_id_t ptr_type_id = CTF_NULL_TYPEID;
337 dw_die_ref pointed_type_die = ctf_get_AT_type (ptr_type);
339 type_id = gen_ctf_type (ctfc, pointed_type_die);
341 /* Type de-duplication.
342 Consult the ctfc_types hash again before adding the CTF pointer type
343 because there can be cases where a pointer type may have been added by
344 the gen_ctf_type call above. */
345 if (ctf_type_exists (ctfc, ptr_type, &ptr_type_id))
346 return ptr_type_id;
348 ptr_type_id = ctf_add_pointer (ctfc, CTF_ADD_ROOT, type_id, ptr_type);
349 return ptr_type_id;
352 /* Recursively generate CTF for array dimensions starting at DIE C (of type
353 DW_TAG_subrange_type) until DIE LAST (of type DW_TAG_subrange_type) is
354 reached. ARRAY_ELEMS_TYPE_ID is base type for the array. */
356 static ctf_id_t
357 gen_ctf_subrange_type (ctf_container_ref ctfc, ctf_id_t array_elems_type_id,
358 dw_die_ref c, dw_die_ref last)
360 ctf_arinfo_t arinfo;
361 ctf_id_t array_node_type_id = CTF_NULL_TYPEID;
363 dw_attr_node *upper_bound_at;
364 dw_die_ref array_index_type;
365 uint32_t array_num_elements;
367 if (dw_get_die_tag (c) == DW_TAG_subrange_type)
369 /* When DW_AT_upper_bound is used to specify the size of an
370 array in DWARF, it is usually an unsigned constant
371 specifying the upper bound index of the array. However,
372 for unsized arrays, such as foo[] or bar[0],
373 DW_AT_upper_bound is a signed integer constant
374 instead. */
376 upper_bound_at = get_AT (c, DW_AT_upper_bound);
377 if (upper_bound_at
378 && AT_class (upper_bound_at) == dw_val_class_unsigned_const)
379 /* This is the upper bound index. */
380 array_num_elements = get_AT_unsigned (c, DW_AT_upper_bound) + 1;
381 else if (get_AT (c, DW_AT_count))
382 array_num_elements = get_AT_unsigned (c, DW_AT_count);
383 else
385 /* This is a VLA of some kind. */
386 array_num_elements = 0;
389 else
390 gcc_unreachable ();
392 /* Ok, mount and register the array type. Note how the array
393 type we register here is the type of the elements in
394 subsequent "dimensions", if there are any. */
395 arinfo.ctr_nelems = array_num_elements;
397 array_index_type = ctf_get_AT_type (c);
398 arinfo.ctr_index = gen_ctf_type (ctfc, array_index_type);
400 if (c == last)
401 arinfo.ctr_contents = array_elems_type_id;
402 else
403 arinfo.ctr_contents = gen_ctf_subrange_type (ctfc, array_elems_type_id,
404 dw_get_die_sib (c), last);
406 if (!ctf_type_exists (ctfc, c, &array_node_type_id))
407 array_node_type_id = ctf_add_array (ctfc, CTF_ADD_ROOT, &arinfo, c);
409 return array_node_type_id;
412 /* Generate CTF for an ARRAY_TYPE. */
414 static ctf_id_t
415 gen_ctf_array_type (ctf_container_ref ctfc,
416 dw_die_ref array_type)
418 dw_die_ref first, last, array_elems_type;
419 ctf_id_t array_elems_type_id = CTF_NULL_TYPEID;
420 ctf_id_t array_type_id = CTF_NULL_TYPEID;
422 int vector_type_p = get_AT_flag (array_type, DW_AT_GNU_vector);
423 if (vector_type_p)
424 return array_elems_type_id;
426 /* Find the first and last array dimension DIEs. */
427 last = dw_get_die_child (array_type);
428 first = dw_get_die_sib (last);
430 /* Type de-duplication.
431 Consult the ctfc_types before adding CTF type for the first dimension. */
432 if (!ctf_type_exists (ctfc, first, &array_type_id))
434 array_elems_type = ctf_get_AT_type (array_type);
435 /* First, register the type of the array elements if needed. */
436 array_elems_type_id = gen_ctf_type (ctfc, array_elems_type);
438 array_type_id = gen_ctf_subrange_type (ctfc, array_elems_type_id, first,
439 last);
442 return array_type_id;
445 /* Generate CTF for a typedef. */
447 static ctf_id_t
448 gen_ctf_typedef (ctf_container_ref ctfc, dw_die_ref tdef)
450 ctf_id_t tdef_type_id, tid;
451 const char *tdef_name = get_AT_string (tdef, DW_AT_name);
452 dw_die_ref tdef_type = ctf_get_AT_type (tdef);
454 tid = gen_ctf_type (ctfc, tdef_type);
456 /* Type de-duplication.
457 This is necessary because the ctf for the typedef may have been already
458 added due to the gen_ctf_type call above. */
459 if (!ctf_type_exists (ctfc, tdef, &tdef_type_id))
461 tdef_type_id = ctf_add_typedef (ctfc, CTF_ADD_ROOT,
462 tdef_name,
463 tid,
464 tdef);
466 return tdef_type_id;
469 /* Generate CTF for a type modifier.
471 If the given DIE contains a valid C modifier (like _Atomic), which is not
472 supported by CTF, then this function skips the modifier die and continues
473 with the underlying type.
475 For all other cases, this function returns a CTF_NULL_TYPEID;
478 static ctf_id_t
479 gen_ctf_modifier_type (ctf_container_ref ctfc, dw_die_ref modifier)
481 uint32_t kind = CTF_K_MAX;
482 ctf_id_t modifier_type_id, qual_type_id;
483 dw_die_ref qual_type = ctf_get_AT_type (modifier);
485 switch (dw_get_die_tag (modifier))
487 case DW_TAG_const_type: kind = CTF_K_CONST; break;
488 case DW_TAG_volatile_type: kind = CTF_K_VOLATILE; break;
489 case DW_TAG_restrict_type: kind = CTF_K_RESTRICT; break;
490 case DW_TAG_atomic_type: break;
491 default:
492 return CTF_NULL_TYPEID;
495 /* Register the type for which this modifier applies. */
496 qual_type_id = gen_ctf_type (ctfc, qual_type);
498 /* Skip generating a CTF modifier record for _Atomic as there is no
499 representation for it. */
500 if (dw_get_die_tag (modifier) == DW_TAG_atomic_type)
501 return qual_type_id;
503 gcc_assert (kind != CTF_K_MAX);
504 /* Now register the modifier itself. */
505 if (!ctf_type_exists (ctfc, modifier, &modifier_type_id))
506 modifier_type_id = ctf_add_reftype (ctfc, CTF_ADD_ROOT,
507 qual_type_id, kind,
508 modifier);
510 return modifier_type_id;
513 /* Generate CTF for a struct type. */
515 static ctf_id_t
516 gen_ctf_sou_type (ctf_container_ref ctfc, dw_die_ref sou, uint32_t kind)
518 uint32_t bit_size = ctf_die_bitsize (sou);
519 int declaration_p = get_AT_flag (sou, DW_AT_declaration);
520 const char *sou_name = get_AT_string (sou, DW_AT_name);
522 ctf_id_t sou_type_id;
524 /* An incomplete structure or union type is represented in DWARF by
525 a structure or union DIE that does not have a size attribute and
526 that has a DW_AT_declaration attribute. This corresponds to a
527 CTF forward type with kind CTF_K_STRUCT. */
528 if (bit_size == 0 && declaration_p)
529 return ctf_add_forward (ctfc, CTF_ADD_ROOT,
530 sou_name, kind, sou);
532 /* This is a complete struct or union type. Generate a CTF type for
533 it if it doesn't exist already. */
534 if (!ctf_type_exists (ctfc, sou, &sou_type_id))
535 sou_type_id = ctf_add_sou (ctfc, CTF_ADD_ROOT,
536 sou_name, kind, bit_size / 8,
537 sou);
539 /* Now process the struct members. */
541 dw_die_ref c;
543 c = dw_get_die_child (sou);
544 if (c)
547 const char *field_name;
548 dw_die_ref field_type;
549 HOST_WIDE_INT field_location;
550 ctf_id_t field_type_id;
552 c = dw_get_die_sib (c);
554 field_name = get_AT_string (c, DW_AT_name);
555 field_type = ctf_get_AT_type (c);
556 field_location = ctf_get_AT_data_member_location (c);
558 /* Generate the field type. */
559 field_type_id = gen_ctf_type (ctfc, field_type);
561 /* If this is a bit-field, then wrap the field type
562 generated above with a CTF slice. */
563 if (get_AT (c, DW_AT_bit_offset)
564 || get_AT (c, DW_AT_data_bit_offset))
566 dw_attr_node *attr;
567 HOST_WIDE_INT bitpos = 0;
568 HOST_WIDE_INT bitsize = ctf_die_bitsize (c);
569 HOST_WIDE_INT bit_offset;
571 /* The bit offset is given in bits and it may be
572 negative. */
573 attr = get_AT (c, DW_AT_bit_offset);
574 if (attr)
576 if (AT_class (attr) == dw_val_class_unsigned_const)
577 bit_offset = AT_unsigned (attr);
578 else
579 bit_offset = AT_int (attr);
581 if (BYTES_BIG_ENDIAN)
582 bitpos = field_location + bit_offset;
583 else
585 HOST_WIDE_INT bit_size;
587 attr = get_AT (c, DW_AT_byte_size);
588 if (attr)
589 /* Explicit size given in bytes. */
590 bit_size = AT_unsigned (attr) * 8;
591 else
592 /* Infer the size from the member type. */
593 bit_size = ctf_die_bitsize (field_type);
595 bitpos = (field_location
596 + bit_size
597 - bit_offset
598 - bitsize);
602 /* In DWARF5 a data_bit_offset attribute is given with
603 the offset of the data from the beginning of the
604 struct. Acknowledge it if present. */
605 attr = get_AT (c, DW_AT_data_bit_offset);
606 if (attr)
607 bitpos += AT_unsigned (attr);
609 field_type_id = ctf_add_slice (ctfc, CTF_ADD_NONROOT,
610 field_type_id,
611 bitpos - field_location,
612 bitsize,
616 /* Add the field type to the struct or union type. */
617 ctf_add_member_offset (ctfc, sou,
618 field_name,
619 field_type_id,
620 field_location);
622 while (c != dw_get_die_child (sou));
625 return sou_type_id;
628 /* Generate CTF for a function type. */
630 static ctf_id_t
631 gen_ctf_function_type (ctf_container_ref ctfc, dw_die_ref function,
632 bool from_global_func)
634 const char *function_name = get_AT_string (function, DW_AT_name);
635 dw_die_ref return_type = ctf_get_AT_type (function);
637 ctf_funcinfo_t func_info;
638 uint32_t num_args = 0;
639 int linkage = get_AT_flag (function, DW_AT_external);
641 ctf_id_t return_type_id;
642 ctf_id_t function_type_id;
644 /* First, add the return type. */
645 return_type_id = gen_ctf_type (ctfc, return_type);
646 func_info.ctc_return = return_type_id;
648 /* Type de-duplication.
649 Consult the ctfc_types hash before adding the CTF function type. */
650 if (ctf_type_exists (ctfc, function, &function_type_id))
651 return function_type_id;
653 /* Do a first pass on the formals to determine the number of
654 arguments, and whether the function type gets a varargs. */
656 dw_die_ref c;
658 c = dw_get_die_child (function);
659 if (c)
662 c = dw_get_die_sib (c);
664 if (dw_get_die_tag (c) == DW_TAG_formal_parameter)
665 num_args += 1;
666 else if (dw_get_die_tag (c) == DW_TAG_unspecified_parameters)
668 func_info.ctc_flags |= CTF_FUNC_VARARG;
669 num_args += 1;
672 while (c != dw_get_die_child (function));
675 /* Note the number of typed arguments _includes_ the vararg. */
676 func_info.ctc_argc = num_args;
678 /* Type de-duplication has already been performed by now. */
679 function_type_id = ctf_add_function (ctfc, CTF_ADD_ROOT,
680 function_name,
681 (const ctf_funcinfo_t *)&func_info,
682 function,
683 from_global_func,
684 linkage);
686 /* Second pass on formals: generate the CTF types corresponding to
687 them and add them as CTF function args. */
689 dw_die_ref c;
690 unsigned int i = 0;
691 const char *arg_name;
692 ctf_id_t arg_type;
694 c = dw_get_die_child (function);
695 if (c)
698 c = dw_get_die_sib (c);
700 if (dw_get_die_tag (c) == DW_TAG_unspecified_parameters)
702 gcc_assert (i == num_args - 1);
703 /* Add an argument with type 0 and no name. */
704 ctf_add_function_arg (ctfc, function, "", 0);
706 else if (dw_get_die_tag (c) == DW_TAG_formal_parameter)
708 i++;
709 arg_name = get_AT_string (c, DW_AT_name);
710 arg_type = gen_ctf_type (ctfc, ctf_get_AT_type (c));
711 /* Add the argument to the existing CTF function type. */
712 ctf_add_function_arg (ctfc, function, arg_name, arg_type);
714 else
715 /* This is a local variable. Ignore. */
716 continue;
718 while (c != dw_get_die_child (function));
721 return function_type_id;
724 /* Generate CTF for an enumeration type. */
726 static ctf_id_t
727 gen_ctf_enumeration_type (ctf_container_ref ctfc, dw_die_ref enumeration)
729 const char *enum_name = get_AT_string (enumeration, DW_AT_name);
730 unsigned int bit_size = ctf_die_bitsize (enumeration);
731 unsigned int signedness = get_AT_unsigned (enumeration, DW_AT_encoding);
732 int declaration_p = get_AT_flag (enumeration, DW_AT_declaration);
734 ctf_id_t enumeration_type_id;
736 /* If this is an incomplete enum, generate a CTF forward for it and
737 be done. */
738 if (declaration_p)
740 gcc_assert (enum_name);
741 return ctf_add_forward (ctfc, CTF_ADD_ROOT, enum_name,
742 CTF_K_ENUM, enumeration);
745 /* If the size the enumerators is not specified then use the size of
746 the underlying type, which must be a base type. */
747 if (bit_size == 0)
749 dw_die_ref type = ctf_get_AT_type (enumeration);
750 bit_size = ctf_die_bitsize (type);
753 /* Generate a CTF type for the enumeration. */
754 enumeration_type_id = ctf_add_enum (ctfc, CTF_ADD_ROOT,
755 enum_name, bit_size / 8,
756 (signedness == DW_ATE_unsigned),
757 enumeration);
759 /* Process the enumerators. */
761 dw_die_ref c;
763 c = dw_get_die_child (enumeration);
764 if (c)
767 const char *enumerator_name;
768 dw_attr_node *enumerator_value;
769 HOST_WIDE_INT value_wide_int;
771 c = dw_get_die_sib (c);
773 enumerator_name = get_AT_string (c, DW_AT_name);
774 enumerator_value = get_AT (c, DW_AT_const_value);
776 /* enumerator_value can be either a signed or an unsigned
777 constant value. */
778 if (AT_class (enumerator_value) == dw_val_class_unsigned_const
779 || (AT_class (enumerator_value)
780 == dw_val_class_unsigned_const_implicit))
781 value_wide_int = AT_unsigned (enumerator_value);
782 else
783 value_wide_int = AT_int (enumerator_value);
785 ctf_add_enumerator (ctfc, enumeration_type_id,
786 enumerator_name, value_wide_int, enumeration);
788 while (c != dw_get_die_child (enumeration));
791 return enumeration_type_id;
794 /* Add a CTF variable record for the given input DWARF DIE. */
796 static void
797 gen_ctf_variable (ctf_container_ref ctfc, dw_die_ref die)
799 const char *var_name = get_AT_string (die, DW_AT_name);
800 dw_die_ref var_type = ctf_get_AT_type (die);
801 unsigned int external_vis = get_AT_flag (die, DW_AT_external);
802 ctf_id_t var_type_id;
804 /* Avoid duplicates. */
805 if (ctf_dvd_lookup (ctfc, die))
806 return;
808 /* Do not generate CTF variable records for non-defining incomplete
809 declarations. Such declarations can be known via the DWARF
810 DW_AT_specification attribute. */
811 if (ctf_dvd_ignore_lookup (ctfc, die))
812 return;
814 /* The value of the DW_AT_specification attribute, if present, is a
815 reference to the debugging information entry representing the
816 non-defining declaration. */
817 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
819 /* Add the type of the variable. */
820 var_type_id = gen_ctf_type (ctfc, var_type);
822 /* Generate the new CTF variable and update global counter. */
823 (void) ctf_add_variable (ctfc, var_name, var_type_id, die, external_vis,
824 decl);
825 /* Skip updating the number of global objects at this time. This is updated
826 later after pre-processing as some CTF variable records although
827 generated now, will not be emitted later. [PR105089]. */
830 /* Add a CTF function record for the given input DWARF DIE. */
832 static void
833 gen_ctf_function (ctf_container_ref ctfc, dw_die_ref die)
835 ctf_id_t function_type_id;
836 /* Type de-duplication.
837 Consult the ctfc_types hash before adding the CTF function type. */
838 if (ctf_type_exists (ctfc, die, &function_type_id))
839 return;
841 /* Add the type of the function and update the global functions
842 counter. Note that DWARF encodes function types in both
843 DW_TAG_subroutine_type and DW_TAG_subprogram in exactly the same
844 way. */
845 (void) gen_ctf_function_type (ctfc, die, true /* from_global_func */);
846 ctfc->ctfc_num_global_funcs += 1;
849 /* Add CTF type record(s) for the given input DWARF DIE and return its type id.
851 If there is already a CTF type corresponding to the given DIE, then
852 this function returns the type id of the existing type.
854 If the given DIE is not recognized as a type, then this function
855 returns CTF_NULL_TYPEID. */
857 static ctf_id_t
858 gen_ctf_type (ctf_container_ref ctfc, dw_die_ref die)
860 ctf_id_t type_id;
861 int unrecog_die = false;
863 if (ctf_type_exists (ctfc, die, &type_id))
864 return type_id;
866 switch (dw_get_die_tag (die))
868 case DW_TAG_base_type:
869 type_id = gen_ctf_base_type (ctfc, die);
870 break;
871 case DW_TAG_pointer_type:
872 type_id = gen_ctf_pointer_type (ctfc, die);
873 break;
874 case DW_TAG_typedef:
875 type_id = gen_ctf_typedef (ctfc, die);
876 break;
877 case DW_TAG_array_type:
878 type_id = gen_ctf_array_type (ctfc, die);
879 break;
880 case DW_TAG_structure_type:
881 type_id = gen_ctf_sou_type (ctfc, die, CTF_K_STRUCT);
882 break;
883 case DW_TAG_union_type:
884 type_id = gen_ctf_sou_type (ctfc, die, CTF_K_UNION);
885 break;
886 case DW_TAG_subroutine_type:
887 type_id = gen_ctf_function_type (ctfc, die,
888 false /* from_global_func */);
889 break;
890 case DW_TAG_enumeration_type:
891 type_id = gen_ctf_enumeration_type (ctfc, die);
892 break;
893 case DW_TAG_atomic_type:
894 /* FALLTHROUGH */
895 case DW_TAG_const_type:
896 /* FALLTHROUGH */
897 case DW_TAG_restrict_type:
898 /* FALLTHROUGH */
899 case DW_TAG_volatile_type:
900 type_id = gen_ctf_modifier_type (ctfc, die);
901 break;
902 case DW_TAG_unspecified_type:
904 const char *name = get_AT_string (die, DW_AT_name);
906 if (name && strcmp (name, "void") == 0)
907 type_id = gen_ctf_void_type (ctfc);
908 else
909 type_id = CTF_NULL_TYPEID;
911 break;
913 case DW_TAG_reference_type:
914 type_id = CTF_NULL_TYPEID;
915 break;
916 default:
917 /* Unrecognized DIE. */
918 unrecog_die = true;
919 type_id = CTF_NULL_TYPEID;
920 break;
923 /* For all types unrepresented in CTF, use an explicit CTF type of kind
924 CTF_K_UNKNOWN. */
925 if ((type_id == CTF_NULL_TYPEID) && (!unrecog_die))
926 type_id = gen_ctf_unknown_type (ctfc);
928 return type_id;
931 /* Prepare for output and write out the CTF debug information. */
933 static void
934 ctf_debug_finalize (const char *filename, bool btf)
936 if (btf)
938 btf_output (filename);
939 /* btf_finalize when compiling BPF applciations gets deallocated by the
940 BPF target in bpf_file_end. */
941 if (btf_debuginfo_p () && !btf_with_core_debuginfo_p ())
942 btf_finalize ();
945 else
947 /* Emit the collected CTF information. */
948 ctf_output (filename);
950 /* Reset the CTF state. */
951 ctf_finalize ();
955 bool
956 ctf_do_die (dw_die_ref die)
958 ctf_container_ref tu_ctfc = ctf_get_tu_ctfc ();
960 /* Note how we tell the caller to continue traversing children DIEs
961 if this DIE didn't result in CTF records being added. */
962 if (dw_get_die_tag (die) == DW_TAG_variable)
964 gen_ctf_variable (tu_ctfc, die);
965 return false;
967 else if (dw_get_die_tag (die) == DW_TAG_subprogram)
969 gen_ctf_function (tu_ctfc, die);
970 return false;
972 else
973 return gen_ctf_type (tu_ctfc, die) == CTF_NULL_TYPEID;
976 /* Initialize CTF subsystem for CTF debug info generation. */
978 void
979 ctf_debug_init (void)
981 /* First, initialize the CTF subsystem. */
982 ctf_init ();
984 /* Create a couple of DIE structures that we may need. */
985 ctf_void_die = new_die_raw (DW_TAG_unspecified_type);
986 add_name_attribute (ctf_void_die, "void");
987 ctf_array_index_die
988 = base_type_die (integer_type_node, 0 /* reverse */);
989 add_name_attribute (ctf_array_index_die, "int");
990 ctf_unknown_die = new_die_raw (DW_TAG_unspecified_type);
991 add_name_attribute (ctf_unknown_die, "unknown");
994 /* Preprocess the CTF debug information after initialization. */
996 void
997 ctf_debug_init_postprocess (bool btf)
999 /* Only BTF requires postprocessing right after init. */
1000 if (btf)
1001 btf_init_postprocess ();
1004 /* Early finish CTF/BTF debug info. */
1006 void
1007 ctf_debug_early_finish (const char * filename)
1009 /* Emit CTF debug info early always. */
1010 if (ctf_debug_info_level > CTFINFO_LEVEL_NONE
1011 /* Emit BTF debug info early if CO-RE relocations are not
1012 required. */
1013 || (btf_debuginfo_p () && !btf_with_core_debuginfo_p ()))
1014 ctf_debug_finalize (filename, btf_debuginfo_p ());
1017 /* Finish CTF/BTF debug info emission. */
1019 void
1020 ctf_debug_finish (const char * filename)
1022 /* Emit BTF debug info here when CO-RE relocations need to be generated.
1023 BTF with CO-RE relocations needs to be generated when CO-RE is in effect
1024 for the BPF target. */
1025 if (btf_debuginfo_p () && btf_with_core_debuginfo_p ())
1026 ctf_debug_finalize (filename, btf_debuginfo_p ());
1029 #include "gt-dwarf2ctf.h"