]> git.ipfire.org Git - thirdparty/gcc.git/commit
Implement wrap-around arithmetics in DWARF expressions
authorEric Botcazou <ebotcazou@adacore.com>
Mon, 3 Jun 2024 15:44:13 +0000 (17:44 +0200)
committerEric Botcazou <ebotcazou@adacore.com>
Mon, 3 Jun 2024 16:03:31 +0000 (18:03 +0200)
commitf3d6d60d2ae584a23bb7c681cbd511202953c391
treea4f7adadeb2a812797134d7c9903fa26c713f64c
parent482f97e79fc29ea2d61f1425b32564a668b51e1c
Implement wrap-around arithmetics in DWARF expressions

For the following Ada package declaring a simple variable-sized record type:

package P is

  type Enum is (Zero, One, Two, Three, Four, Five, Six, Seven, Eight, Nine);

  type Rec (Kind : Enum := Zero) is record
      case Kind is
        when Four .. Seven =>
          S : String (1 .. 32);
        when others =>
          null;
      end case;
    end record;

end P;

the compiler builds a "size function" in GENERIC which is at -Og:

sizetype _GLOBAL.SZ5_p (p__enum p0)
{
  return (UNSIGNED_8) p0 + 252 <= 3 ? 32 : 0;
}

The UNSIGNED_8-based trick makes it possible to eliminates one branch but
relies on the wrap-around arithmetics of UNSIGNED_8.  This size function
is then translated into a DWARF procedure, but the wrap-around arithmetics
is dropped on the floor, leading to a wrong size calculation when the DWARF
procedure is executed.

The fix also contains an optimization of unsigned comparisons in DWARF for
the case where the type is smaller than the "generic type" like here.

gcc/
* dwarf2out.cc (loc_list_from_tree_1) <CEIL_DIV_EXPR>; Add const.
<do_comp_binop>: Use a signed comparison for small unsigned types.
Implement wrap-around arithmetics for small integer types.
gcc/dwarf2out.cc