forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AVR] Fix parsing & emitting relative jumps (llvm#106722)
Ever since 6859685 (or, precisely, 84428da) relative jumps emitted by the AVR codegen are off by two bytes - this pull request fixes it. ## Abstract As compared to absolute jumps, relative jumps - such as rjmp, rcall or brsh - have an implied `pc+2` behavior; that is, `jmp 100` is `pc = 100`, but `rjmp 100` gets understood as `pc = pc + 100 + 2`. This is not reflected in the AVR codegen: https://github.com/llvm/llvm-project/blob/f95026dbf66e353128a3a3d7b55f3e52d5985535/llvm/lib/Target/AVR/MCTargetDesc/AVRAsmBackend.cpp#L89 ... which always emits relative jumps that are two bytes too far - or rather it _would_ emit such jumps if not for this check: https://github.com/llvm/llvm-project/blob/f95026dbf66e353128a3a3d7b55f3e52d5985535/llvm/lib/Target/AVR/MCTargetDesc/AVRAsmBackend.cpp#L517 ... which causes most of the relative jumps to be actually resolved late, by the linker, which applies the offsetting logic on its own, hiding the issue within LLVM. [Some time ago](llvm@697a162) we've had a similar "jumps are off" problem that got solved by touching `shouldForceRelocation()`, but I think that has worked only by accident. It's exploited the fact that absolute vs relative jumps in the parsed assembly can be distinguished through a "side channel" check relying on the existence of labels (i.e. absolute jumps happen to named labels, but relative jumps are anonymous, so to say). This was an alright idea back then, but it got broken by 6859685. I propose a different approach: - when emitting relative jumps, offset them by `-2` (well, `-1`, strictly speaking, because those instructions rely on right-shifted offset), - when parsing relative jumps, treat `.` as `+2` and read `rjmp .+1234` as `rjmp (1234 + 2)`. This approach seems to be sound and now we generate the same assembly as avr-gcc, which can be confirmed with: ```cpp // avr-gcc test.c -O3 && avr-objdump -d a.out int main() { asm( " foo:\n\t" " rjmp .+2\n\t" " rjmp .-2\n\t" " rjmp foo\n\t" " rjmp .+8\n\t" " rjmp end\n\t" " rjmp .+0\n\t" " end:\n\t" " rjmp .-4\n\t" " rjmp .-6\n\t" " x:\n\t" " rjmp x\n\t" " .short 0xc00f\n\t" ); } ``` avr-gcc is also how I got the opcodes for all new tests like `inst-brbc.s`, so we should be good. (cherry picked from commit 86a60e7)
- Loading branch information
Showing
26 changed files
with
567 additions
and
401 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
; RUN: llc -filetype=obj -mtriple=avr < %s | llvm-objdump -dr --no-show-raw-insn - | FileCheck %s | ||
|
||
define i8 @foo(i8 %a) { | ||
bb0: | ||
%0 = tail call i8 @bar(i8 %a) | ||
%1 = icmp eq i8 %0, 123 | ||
br i1 %1, label %bb1, label %bb2 | ||
|
||
bb1: | ||
ret i8 100 | ||
|
||
bb2: | ||
ret i8 200 | ||
} | ||
|
||
declare i8 @bar(i8); | ||
|
||
; CHECK: rcall .-2 | ||
; CHECK-NEXT: 00000000: R_AVR_13_PCREL bar | ||
; CHECK-NEXT: cpi r24, 0x7b | ||
; CHECK-NEXT: brne .+4 | ||
; CHECK-NEXT: ldi r24, 0x64 | ||
; CHECK-NEXT: ret | ||
; CHECK-NEXT: ldi r24, 0xc8 | ||
; CHECK-NEXT: ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
; RUN: llvm-mc -triple avr -show-encoding < %s | FileCheck %s | ||
; | ||
; RUN: llvm-mc -filetype=obj -triple avr < %s \ | ||
; RUN: | llvm-objdump -d - \ | ||
; RUN: | FileCheck --check-prefix=INST %s | ||
|
||
foo: | ||
brcc .+66 | ||
brcc .-22 | ||
brbc 0, .+66 | ||
brbc 0, bar | ||
|
||
bar: | ||
|
||
; CHECK: brcc (.Ltmp0+66)+2 ; encoding: [0bAAAAA000,0b111101AA] | ||
; CHECK-NEXT: ; fixup A - offset: 0, value: (.Ltmp0+66)+2, kind: fixup_7_pcrel | ||
; CHECK: brcc (.Ltmp1-22)+2 ; encoding: [0bAAAAA000,0b111101AA] | ||
; CHECK-NEXT: ; fixup A - offset: 0, value: (.Ltmp1-22)+2, kind: fixup_7_pcrel | ||
; CHECK: brcc (.Ltmp2+66)+2 ; encoding: [0bAAAAA000,0b111101AA] | ||
; CHECK-NEXT: ; fixup A - offset: 0, value: (.Ltmp2+66)+2, kind: fixup_7_pcrel | ||
; CHECK: brcc bar ; encoding: [0bAAAAA000,0b111101AA] | ||
; CHECK-NEXT: ; fixup A - offset: 0, value: bar, kind: fixup_7_pcrel | ||
|
||
; INST-LABEL: <foo>: | ||
; INST-NEXT: 08 f5 brsh .+66 | ||
; INST-NEXT: a8 f7 brsh .-22 | ||
; INST-NEXT: 08 f5 brsh .+66 | ||
; INST-NEXT: 00 f4 brsh .+0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
; RUN: llvm-mc -triple avr -show-encoding < %s | FileCheck %s | ||
; | ||
; RUN: llvm-mc -filetype=obj -triple avr < %s \ | ||
; RUN: | llvm-objdump -d - \ | ||
; RUN: | FileCheck --check-prefix=INST %s | ||
|
||
foo: | ||
brcs .+8 | ||
brcs .+4 | ||
brbs 0, .+8 | ||
brbs 0, bar | ||
|
||
bar: | ||
|
||
; CHECK: brcs (.Ltmp0+8)+2 ; encoding: [0bAAAAA000,0b111100AA] | ||
; CHECK-NEXT: ; fixup A - offset: 0, value: (.Ltmp0+8)+2, kind: fixup_7_pcrel | ||
; CHECK: brcs (.Ltmp1+4)+2 ; encoding: [0bAAAAA000,0b111100AA] | ||
; CHECK-NEXT: ; fixup A - offset: 0, value: (.Ltmp1+4)+2, kind: fixup_7_pcrel | ||
; CHECK: brcs (.Ltmp2+8)+2 ; encoding: [0bAAAAA000,0b111100AA] | ||
; CHECK-NEXT: ; fixup A - offset: 0, value: (.Ltmp2+8)+2, kind: fixup_7_pcrel | ||
; CHECK: brcs bar ; encoding: [0bAAAAA000,0b111100AA] | ||
; CHECK-NEXT: ; fixup A - offset: 0, value: bar, kind: fixup_7_pcrel | ||
|
||
; INST-LABEL: <foo>: | ||
; INST-NEXT: 20 f0 brlo .+8 | ||
; INST-NEXT: 10 f0 brlo .+4 | ||
; INST-NEXT: 20 f0 brlo .+8 | ||
; INST-NEXT: 00 f0 brlo .+0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
; RUN: llvm-mc -triple avr -show-encoding < %s | FileCheck %s | ||
; | ||
; RUN: llvm-mc -filetype=obj -triple avr < %s \ | ||
; RUN: | llvm-objdump -d - \ | ||
; RUN: | FileCheck --check-prefix=INST %s | ||
|
||
foo: | ||
breq .-18 | ||
breq .-12 | ||
brbs 1, .-18 | ||
brbs 1, bar | ||
|
||
bar: | ||
|
||
; CHECK: breq (.Ltmp0-18)+2 ; encoding: [0bAAAAA001,0b111100AA] | ||
; CHECK-NEXT: ; fixup A - offset: 0, value: (.Ltmp0-18)+2, kind: fixup_7_pcrel | ||
; CHECK: breq (.Ltmp1-12)+2 ; encoding: [0bAAAAA001,0b111100AA] | ||
; CHECK-NEXT: ; fixup A - offset: 0, value: (.Ltmp1-12)+2, kind: fixup_7_pcrel | ||
; CHECK: brbs 1, (.Ltmp2-18)+2 ; encoding: [0bAAAAA001,0b111100AA] | ||
; CHECK-NEXT: ; fixup A - offset: 0, value: (.Ltmp2-18)+2, kind: fixup_7_pcrel | ||
; CHECK: brbs 1, bar ; encoding: [0bAAAAA001,0b111100AA] | ||
; CHECK-NEXT: ; fixup A - offset: 0, value: bar, kind: fixup_7_pcrel | ||
|
||
; INST-LABEL: <foo>: | ||
; INST-NEXT: b9 f3 breq .-18 | ||
; INST-NEXT: d1 f3 breq .-12 | ||
; INST-NEXT: b9 f3 breq .-18 | ||
; INST-NEXT: 01 f0 breq .+0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
; RUN: llvm-mc -triple avr -show-encoding < %s | FileCheck %s | ||
; | ||
; RUN: llvm-mc -filetype=obj -triple avr < %s \ | ||
; RUN: | llvm-objdump -d - \ | ||
; RUN: | FileCheck --check-prefix=INST %s | ||
|
||
foo: | ||
brge .+50 | ||
brge .+42 | ||
brge bar | ||
|
||
bar: | ||
|
||
; CHECK: brge (.Ltmp0+50)+2 ; encoding: [0bAAAAA100,0b111101AA] | ||
; CHECK-NEXT: ; fixup A - offset: 0, value: (.Ltmp0+50)+2, kind: fixup_7_pcrel | ||
; CHECK: brge (.Ltmp1+42)+2 ; encoding: [0bAAAAA100,0b111101AA] | ||
; CHECK-NEXT: ; fixup A - offset: 0, value: (.Ltmp1+42)+2, kind: fixup_7_pcrel | ||
; CHECK: brge bar ; encoding: [0bAAAAA100,0b111101AA] | ||
; CHECK-NEXT: ; fixup A - offset: 0, value: bar, kind: fixup_7_pcrel | ||
|
||
; INST-LABEL: <foo>: | ||
; INST-NEXT: cc f4 brge .+50 | ||
; INST-NEXT: ac f4 brge .+42 | ||
; INST-NEXT: 04 f4 brge .+0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
; RUN: llvm-mc -triple avr -show-encoding < %s | FileCheck %s | ||
; | ||
; RUN: llvm-mc -filetype=obj -triple avr < %s \ | ||
; RUN: | llvm-objdump -d - \ | ||
; RUN: | FileCheck --check-prefix=INST %s | ||
|
||
foo: | ||
brhc .+12 | ||
brhc .+14 | ||
brhc bar | ||
|
||
bar: | ||
|
||
; CHECK: brhc (.Ltmp0+12)+2 ; encoding: [0bAAAAA101,0b111101AA] | ||
; CHECK-NEXT: ; fixup A - offset: 0, value: (.Ltmp0+12)+2, kind: fixup_7_pcrel | ||
; CHECK: brhc (.Ltmp1+14)+2 ; encoding: [0bAAAAA101,0b111101AA] | ||
; CHECK-NEXT: ; fixup A - offset: 0, value: (.Ltmp1+14)+2, kind: fixup_7_pcrel | ||
; CHECK: brhc bar ; encoding: [0bAAAAA101,0b111101AA] | ||
; CHECK-NEXT: ; fixup A - offset: 0, value: bar, kind: fixup_7_pcrel | ||
|
||
; INST-LABEL: <foo>: | ||
; INST-NEXT: 35 f4 brhc .+12 | ||
; INST-NEXT: 3d f4 brhc .+14 | ||
; INST-NEXT: 05 f4 brhc .+0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
; RUN: llvm-mc -triple avr -show-encoding < %s | FileCheck %s | ||
; | ||
; RUN: llvm-mc -filetype=obj -triple avr < %s \ | ||
; RUN: | llvm-objdump -d - \ | ||
; RUN: | FileCheck --check-prefix=INST %s | ||
|
||
foo: | ||
brhs .-66 | ||
brhs .+14 | ||
brhs bar | ||
|
||
bar: | ||
|
||
; CHECK: brhs (.Ltmp0-66)+2 ; encoding: [0bAAAAA101,0b111100AA] | ||
; CHECK-NEXT: ; fixup A - offset: 0, value: (.Ltmp0-66)+2, kind: fixup_7_pcrel | ||
; CHECK: brhs (.Ltmp1+14)+2 ; encoding: [0bAAAAA101,0b111100AA] | ||
; CHECK-NEXT: ; fixup A - offset: 0, value: (.Ltmp1+14)+2, kind: fixup_7_pcrel | ||
; CHECK: brhs bar ; encoding: [0bAAAAA101,0b111100AA] | ||
; CHECK-NEXT: ; fixup A - offset: 0, value: bar, kind: fixup_7_pcrel | ||
|
||
; INST-LABEL: <foo>: | ||
; INST-NEXT: fd f2 brhs .-66 | ||
; INST-NEXT: 3d f0 brhs .+14 | ||
; INST-NEXT: 05 f0 brhs .+0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
; RUN: llvm-mc -triple avr -show-encoding < %s | FileCheck %s | ||
; | ||
; RUN: llvm-mc -filetype=obj -triple avr < %s \ | ||
; RUN: | llvm-objdump -d - \ | ||
; RUN: | FileCheck --check-prefix=INST %s | ||
|
||
foo: | ||
brid .+42 | ||
brid .+62 | ||
brid bar | ||
|
||
bar: | ||
|
||
; CHECK: brid (.Ltmp0+42)+2 ; encoding: [0bAAAAA111,0b111101AA] | ||
; CHECK-NEXT: ; fixup A - offset: 0, value: (.Ltmp0+42)+2, kind: fixup_7_pcrel | ||
; CHECK: brid (.Ltmp1+62)+2 ; encoding: [0bAAAAA111,0b111101AA] | ||
; CHECK-NEXT: ; fixup A - offset: 0, value: (.Ltmp1+62)+2, kind: fixup_7_pcrel | ||
; CHECK: brid bar ; encoding: [0bAAAAA111,0b111101AA] | ||
; CHECK-NEXT: ; fixup A - offset: 0, value: bar, kind: fixup_7_pcrel | ||
|
||
; INST-LABEL: <foo>: | ||
; INST-NEXT: af f4 brid .+42 | ||
; INST-NEXT: ff f4 brid .+62 | ||
; INST-NEXT: 07 f4 brid .+0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
; RUN: llvm-mc -triple avr -show-encoding < %s | FileCheck %s | ||
; | ||
; RUN: llvm-mc -filetype=obj -triple avr < %s \ | ||
; RUN: | llvm-objdump -d - \ | ||
; RUN: | FileCheck --check-prefix=INST %s | ||
|
||
foo: | ||
brie .+20 | ||
brie .+40 | ||
brie bar | ||
|
||
bar: | ||
|
||
; CHECK: brie (.Ltmp0+20)+2 ; encoding: [0bAAAAA111,0b111100AA] | ||
; CHECK-NEXT: ; fixup A - offset: 0, value: (.Ltmp0+20)+2, kind: fixup_7_pcrel | ||
; CHECK: brie (.Ltmp1+40)+2 ; encoding: [0bAAAAA111,0b111100AA] | ||
; CHECK-NEXT: ; fixup A - offset: 0, value: (.Ltmp1+40)+2, kind: fixup_7_pcrel | ||
; CHECK: brie bar ; encoding: [0bAAAAA111,0b111100AA] | ||
; CHECK-NEXT: ; fixup A - offset: 0, value: bar, kind: fixup_7_pcrel | ||
|
||
; INST-LABEL: <foo>: | ||
; INST-NEXT: 57 f0 brie .+20 | ||
; INST-NEXT: a7 f0 brie .+40 | ||
; INST-NEXT: 07 f0 brie .+0 |
Oops, something went wrong.