https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125728

            Bug ID: 125728
           Summary: M68k: m68k-elf-gcc generates code that raises address
                    error exception in certain circumstances
           Product: gcc
           Version: 16.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: std282 at icloud dot com
  Target Milestone: ---

Created attachment 64701
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=64701&action=edit
Source file that reproduces the bug

In the following example, expression "foo & 7" gets compiled as

  moveq #7,%d0
  and.l foo-3,%d0

which triggers address error hardware exception due to unaligned longword
access. My target processor is MC68000, which doesn't support unaligned data
accesses.

The faulty code generation happens only on m68k-elf-gcc 16.1.0 with
optimization level -O2 and higher. Earlier versions of m68k-elf-gcc, such as
15.2.0 compile "foo & 7" with the same compilation options as

  move.b foo,%d0
  and.b  #7,%d0

which is valid and doesn't trigger address error.

I have observed the faulty code generation happens when:
1) you have a global 1-byte variable,
2) compiler cannot know its value,
3) you mask some of its bits, and
4) use the result as an index to some array.

Note 1: this is a heavily reduced minimal example. I have encountered this bug
on a personal project of mine, in similar, but more involved circumstances.

Note 2: m68k-elf-gcc that originally triggered this bug was installed via
homebrew. However, I was also able to reproduce that behaviour on godbolt.org,
and as such I consider compiler installation source irrelevant in this case.


Required information
====================

Version of GCC: 16.1.0

System type: compiler targets bare metal m68k (m68k-elf-gcc), runs on MacOS
26.5.1

GCC configuration options: 
  ../configure --target=m68k-elf
--prefix=/opt/homebrew/Cellar/m68k-elf-gcc/16.1.0
--infodir=/opt/homebrew/Cellar/m68k-elf-gcc/16.1.0/share/info/m68k-elf
--disable-nls --without-headers
--with-as=/opt/homebrew/opt/m68k-elf-binutils/bin/m68k-elf-as
--with-ld=/opt/homebrew/opt/m68k-elf-binutils/bin/m68k-elf-ld
--enable-languages=c,c++,objc,lto --enable-lto --with-system-zlib --with-zstd
--disable-debug --disable-dependency-tracking
--prefix=/opt/homebrew/Cellar/m68k-elf-gcc/16.1.0
--libdir=/opt/homebrew/Cellar/m68k-elf-gcc/16.1.0/lib

compiled by GNU C version Apple LLVM 21.0.0 (clang-2100.0.123.102), GMP version
6.3.0, MPFR version 4.2.2, MPC version 1.4.1, isl version isl-0.27-GMP

Command line that triggers the bug: 
  m68k-elf-gcc -std=c11 -m68000 -ffreestanding -nostdlib -O3 -S bad.c -o bad.s

Compiler output: none, returns code 0

Reply via email to