public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/112413] New: Wrong switch jump table offset
@ 2023-11-06 19:54 vincent.riviere at freesbee dot fr
  2023-11-06 19:57 ` [Bug target/112413] " pinskia at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: vincent.riviere at freesbee dot fr @ 2023-11-06 19:54 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 112413
           Summary: Wrong switch jump table offset
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vincent.riviere at freesbee dot fr
  Target Milestone: ---

In some circumstances, gcc produces bad code for switch instruction.

Main goal of the testcase is to force gcc to produce a jump table.

$ cat swi.c
int g;

void f(int i)
{
        switch (i)
        {
                case 0: g = 6082; break;
                case 1: g = 9332; break;
                case 2: g = 5642; break;
                case 3: g = 1423; break;
                case 4: g = 2152; break;
                case 5: g = 6779; break;
                case 6: g = 7074; break;
                case 7: g = 8280; break;
        }
}

$ m68k-linux-gcc -Os -S -o - swi.c -mlong-jump-table-offsets -malign-int
#NO_APP
        .file   "swi.c"
        .text
        .align  2
        .globl  f
        .type   f, @function
f:
        move.l 4(%sp),%d0
        moveq #7,%d1
        cmp.l %d0,%d1
        jcs .L1
        lsl.l #2,%d0
        move.l .L4(%pc,%d0.l),%d0
        jmp %pc@(2,%d0:l)
        .balignw 4,0x284c   |Potential bug here
.L4:
        .long .L11-.L4
        .long .L10-.L4
        .long .L9-.L4
        .long .L8-.L4
        .long .L7-.L4
        .long .L6-.L4
        .long .L5-.L4
        .long .L3-.L4
.L11:
        move.l #6082,g
.L1:
        rts
.L10:
        move.l #9332,g
        jra .L1
...

As the jmp may not be aligned on a multiple of 4, the .balignw directive may
introduce a 2-byte filler, causing jmp to use a wrong offset.

Same happens with m68k-elf-gcc.

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2023-12-11 15:45 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-06 19:54 [Bug c/112413] New: Wrong switch jump table offset vincent.riviere at freesbee dot fr
2023-11-06 19:57 ` [Bug target/112413] " pinskia at gcc dot gnu.org
2023-11-06 20:06 ` vincent.riviere at freesbee dot fr
2023-11-06 20:39 ` vincent.riviere at freesbee dot fr
2023-11-09 12:15 ` mikpelinux at gmail dot com
2023-11-11 17:13 ` mikpelinux at gmail dot com
2023-11-12 16:11 ` vincent.riviere at freesbee dot fr
2023-12-11 15:09 ` mikpelinux at gmail dot com
2023-12-11 15:41 ` cvs-commit at gcc dot gnu.org
2023-12-11 15:43 ` law at gcc dot gnu.org
2023-12-11 15:45 ` sjames at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).