Skip to content

Commit 9bba11f

Browse files
committed
ctf: potential refactoring (may be worse than what it replaces)
Split out type DIE computation from type_id_internal() into another function. This is barely any more readable, but it *does* reduce type_id_internal() to nothing but a 'decorate with location and type ID of this DIE in particular' function -- splitting out everything to do with the parent type DIE, which is a bit squirrelly because of the requirement to pass bit_size and bit_offset overrides down. Signed-off-by: Nick Alcock <[email protected]>
1 parent 55c27d4 commit 9bba11f

File tree

1 file changed

+67
-35
lines changed

1 file changed

+67
-35
lines changed

scripts/dwarf2ctf/dwarf2ctf.c

Lines changed: 67 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,21 @@ static char *type_id_internal(Dwarf_Die *die,
300300
void *data,
301301
int flags);
302302

303+
/*
304+
* Internal: generate the type ID for a type DIE.
305+
*
306+
* If there are no overrides, look for a bit_size and bit_offset and pass them
307+
* down as well.
308+
*/
309+
static char *type_id_type_die(Dwarf_Die *die,
310+
Dwarf_Die *type_die,
311+
struct die_override *overrides,
312+
void (*fun)(Dwarf_Die *die,
313+
const char *id,
314+
struct die_override *overrides,
315+
void *data),
316+
void *data);
317+
303318
/*
304319
* Convert 'long unsigned int' to 'sizetype'. Internal use within type_id().
305320
*/
@@ -1474,6 +1489,51 @@ static char *type_id(Dwarf_Die *die,
14741489
return type_id_internal(die, overrides, fun, data, 0);
14751490
}
14761491

1492+
/*
1493+
* Internal: generate the type ID for a type DIE.
1494+
*
1495+
* If there are no overrides, look for a bit_size and bit_offset and pass them
1496+
* down as well.
1497+
*/
1498+
static char *type_id_type_die(Dwarf_Die *die,
1499+
Dwarf_Die *type_die,
1500+
struct die_override *overrides,
1501+
void (*fun)(Dwarf_Die *die,
1502+
const char *id,
1503+
struct die_override *overrides,
1504+
void *data),
1505+
void *data)
1506+
{
1507+
char *id;
1508+
1509+
/*
1510+
* bit_size and bit_offset go together: we can assume that if a member
1511+
* has the one, it has the other.
1512+
*/
1513+
1514+
if (private_dwarf_hasattr(die, DW_AT_bit_size)) {
1515+
Dwarf_Word size;
1516+
Dwarf_Word offset;
1517+
1518+
size = private_dwarf_udata(die, DW_AT_bit_size, NULL);
1519+
offset = private_dwarf_udata(die, DW_AT_bit_offset, NULL);
1520+
struct die_override o[] = {
1521+
{ DW_TAG_base_type,
1522+
DW_AT_bit_size,
1523+
DIE_OVERRIDE_REPLACE,
1524+
size, NULL },
1525+
{ DW_TAG_base_type,
1526+
DW_AT_bit_offset,
1527+
DIE_OVERRIDE_REPLACE,
1528+
offset, overrides },
1529+
{0}
1530+
};
1531+
id = type_id(type_die, o, fun, data);
1532+
} else
1533+
id = type_id(type_die, overrides, fun, data);
1534+
return id;
1535+
}
1536+
14771537
/*
14781538
* Internal: allows flags to be passed to affect one (and only one) type ID
14791539
* recursion, without affecting other type_id()s launched from the 'fun'.
@@ -1490,7 +1550,6 @@ static char *type_id_internal(Dwarf_Die *die,
14901550
char *id = NULL;
14911551
int no_type_id = 0;
14921552
int decorated = 1;
1493-
Dwarf_Die type_die;
14941553

14951554
/*
14961555
* The ID of a null pointer is NULL.
@@ -1511,8 +1570,8 @@ static char *type_id_internal(Dwarf_Die *die,
15111570

15121571
/*
15131572
* If we have a type DIE, generate it first, passing any overrides down.
1514-
* If there are no overrides, look for a bit_size and bit_offset and pass
1515-
* them down as well.
1573+
* (Base types and enumerations don't have a type DIE that CTF can
1574+
* encode the type of in any useful fashion.)
15161575
*
15171576
* Otherwise, note the location of this DIE, providing scoping
15181577
* information for all types based upon this one. Location elements are
@@ -1528,40 +1587,12 @@ static char *type_id_internal(Dwarf_Die *die,
15281587
if (dwarf_tag(die) != DW_TAG_subrange_type) {
15291588
if ((dwarf_tag(die) != DW_TAG_base_type) &&
15301589
(dwarf_tag(die) != DW_TAG_enumeration_type)) {
1590+
Dwarf_Die type_die;
15311591
Dwarf_Die *diep = private_dwarf_type(die, &type_die);
15321592

1533-
/*
1534-
* bit_size and bit_offset go together: we can assume
1535-
* that if a member has the one, it has the other.
1536-
*/
1537-
if (diep) {
1538-
if (private_dwarf_hasattr(die, DW_AT_bit_size)) {
1539-
Dwarf_Word size;
1540-
Dwarf_Word offset;
1541-
1542-
size = private_dwarf_udata(die,
1543-
DW_AT_bit_size,
1544-
NULL);
1545-
offset = private_dwarf_udata(die,
1546-
DW_AT_bit_offset,
1547-
NULL);
1548-
struct die_override o[] = {
1549-
{ DW_TAG_base_type,
1550-
DW_AT_bit_size,
1551-
DIE_OVERRIDE_REPLACE,
1552-
size,
1553-
NULL },
1554-
{ DW_TAG_base_type,
1555-
DW_AT_bit_offset,
1556-
DIE_OVERRIDE_REPLACE,
1557-
offset,
1558-
overrides },
1559-
{0}
1560-
};
1561-
id = type_id(diep, o, fun, data);
1562-
} else
1563-
id = type_id(diep, overrides, fun, data);
1564-
}
1593+
if (diep)
1594+
id = type_id_type_die(die, diep, overrides,
1595+
fun, data);
15651596
}
15661597

15671598
/*
@@ -1768,6 +1799,7 @@ static char *type_id_internal(Dwarf_Die *die,
17681799
id = str_append(id, "[");
17691800

17701801
if (nelems > 0) {
1802+
Dwarf_Die type_die;
17711803
char elems[22]; /* bigger than 2^64's digit count */
17721804
char *sub_id = type_id_internal(private_dwarf_type(die, &type_die),
17731805
overrides, fun, data,

0 commit comments

Comments
 (0)