llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[arm] Invalid instruction error when using '$' to mark an immediate in lsls instruction

Open jonathonpenix opened this issue 2 years ago • 2 comments

LLVM seems to inconsistently reject using '$' to mark an immediate operand in arm code.

A reproducer is below:

$ clang --version
clang version 19.0.0git (https://github.com/jonathonpenix/llvm-project.git 377a2767a9951659b5ec7309abb78da719a4f93b)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /local/mnt/workspace/jpenix/toolchains/community/install/bin
Build config: +assertions
$ cat foo.s
foo:
  # We seem happy with the '$' here, no error
  subs r0, r0, $0x02
  # We produce an error on the '$' here though
  lsls  r0, r0, $0x02
$ clang --target=arm-none-eabi -c foo.s
foo.s:5:3: error: invalid instruction, any one of the following would fix this:
  lsls  r0, r0, $0x02
  ^
foo.s:5:16: note: operand must be an immediate in the range [0,31]
  lsls  r0, r0, $0x02
                ^
foo.s:5:16: note: operand must be a register in range [r0, r14]
  lsls  r0, r0, $0x02
                ^
foo.s:5:16: note: too many operands for instruction
  lsls  r0, r0, $0x02
                ^

Compiler explorer link: https://godbolt.org/z/v6x847nhb

GNU binutils seems happy with both instructions (and their docs generally mention that both '#' and '$' are supported IIUC, please see https://sourceware.org/binutils/docs-2.26/as/ARM_002dChars.html#ARM_002dChars).

This issue is easy to work around so probably isn't a huge priority, but might be nice to fix for compatibility with binutils/internal consistency.

Please also note that I'm not sure if this is limited to lsls or if there are other cases where this will happen--I only happened across this in the lsls instance.

jonathonpenix avatar May 30 '24 17:05 jonathonpenix

@llvm/issue-subscribers-backend-arm

Author: Jonathon Penix (jonathonpenix)

LLVM seems to inconsistently reject using '$' to mark an immediate operand in arm code.

A reproducer is below:

$ clang --version
clang version 19.0.0git (https://github.com/jonathonpenix/llvm-project.git 377a2767a9951659b5ec7309abb78da719a4f93b)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /local/mnt/workspace/jpenix/toolchains/community/install/bin
Build config: +assertions
$ cat foo.s
foo:
  # We seem happy with the '$' here, no error
  subs r0, r0, $0x02
  # We produce an error on the '$' here though
  lsls  r0, r0, $0x02
$ clang --target=arm-none-eabi -c foo.s
foo.s:5:3: error: invalid instruction, any one of the following would fix this:
  lsls  r0, r0, $0x02
  ^
foo.s:5:16: note: operand must be an immediate in the range [0,31]
  lsls  r0, r0, $0x02
                ^
foo.s:5:16: note: operand must be a register in range [r0, r14]
  lsls  r0, r0, $0x02
                ^
foo.s:5:16: note: too many operands for instruction
  lsls  r0, r0, $0x02
                ^

Compiler explorer link: https://godbolt.org/z/v6x847nhb

GNU binutils seems happy with both instructions (and their docs generally mention that both '#' and '$' are supported IIUC, please see https://sourceware.org/binutils/docs-2.26/as/ARM_002dChars.html#ARM_002dChars).

This issue is easy to work around so probably isn't a huge priority, but might be nice to fix for compatibility with binutils/internal consistency.

Please also note that I'm not sure if this is limited to lsls or if there are other cases where this will happen--I only happened across this in the lsls instance.

llvmbot avatar May 30 '24 17:05 llvmbot

I'm not sure this is easily fixable, and the status quo might have to stay. I have one idea for fixing this, but I'm not sure it's particularly sustainable.

To start with, the behaviour we're seeing in lsls and lsl is expected, according to this comment: https://github.com/llvm/llvm-project/blob/5a12f2867a167bbe11099150f3cb6b6cb77d767c/llvm/lib/Target/ARM/AsmParser/ARMAsmParser.cpp#L6345-L6348 - in short $1 should be treated as a symbol, but $ 1 should be treated as an immediate containing 1.

This was landed in https://github.com/llvm/llvm-project/commit/af1c2e561e5fbd584093b1200cf364fe1b7ec7c4 / https://reviews.llvm.org/D73176 - and the latter review shows a lot of comments about this functionality by @efriedma-quic and @pratlucas - and clarifies that this is an aim to match what binutils accepts.

So why isn't the same happening for sub{s}? Unlike lsl{s}, sub{s} takes a Modified Immediate, which has a custom parser. This custom parser for accepts $3 as an immediate, rather than as a symbol (unlike the usual asm parser).

As for some of what binutils is doing in these examples: https://godbolt.org/z/fbdbh49KW

  • subs/adds r0, r0, $2 seems to be treated as an immediate, even when the 2 symbol exists in the file. Even if you have a hex label (such as $0x4:, which will drop the dollar prefix), this is treated as an immediate.
  • subs/adds r0, r0, $foo treats $foo as a reference to the symbol foo, but only if foo is defined. If foo is undefined in the whole file, then you get an error.
  • lsls r0, r0, $2 seems to be treated as an immediate.
  • lsls r0, r0, $foo can fail for a variety of issues - this example has a range issue and an issue where there's no relocation for shift immediates (something that seems to be binutils' architecture-generic approach showing into error messages)

I don't think there's a nice way forwards here, to be honest - $ is a binutils extension - the architecture wants developers to use #.

One ugly way forwards would be to make a "Immediate that's definitely never a symbol" custom parser for things like lsl, but I'm not actually sure that helps very much either, and makes the parser a lot harder to maintain. I'm erring towards won't fix.

lenary avatar Jun 07 '24 12:06 lenary