arm64: Delete the space separator in __emit_inst
PATCH 1/1 arm64: Delete the space separator in __emit_inst
Many instances of __emit_inst(x) expand to a directive. In a few places it is used as a macro argument, e.g.
arch/arm64/include/asm/sysreg.h #define __emit_inst(x) .inst (x) arch/arm64/include/asm/sysreg.h #define SET_PSTATE_PAN(x) __emit_inst(0xd500401f | PSTATE_PAN | ((!!x) << PSTATE_Imm_shift))
arch/arm64/kvm/hyp/entry.S
ALTERNATIVE(nop, SET_PSTATE_PAN(1), ARM64_HAS_PAN, CONFIG_ARM64_PAN)
Clang integrated assembler parses .inst (x) as two arguments passing to a macro. We delete the space separator so that .inst(x) will be parsed as one argument.
Note, GNU as parsing .inst (x) as one argument is unintentional (for example the x86 backend will parse the construct as two arguments). See https://sourceware.org/bugzilla/show_bug.cgi?id=25750#c10
--- arch/arm64/include/asm/sysreg.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
arch/arm64/include/asm/sysreg.h +3 -1
@@ -49,7 +49,9 @@#ifndef CONFIG_BROKEN_GAS_INST #ifdef __ASSEMBLY__ -#define __emit_inst (x) .inst (x) +// The space separator is omitted so that __emit_inst(x) can be parsed as +// either a directive or a macro argument. +#define __emit_inst (x) .inst(x)#else #define __emit_inst (x) ".inst " __stringify((x)) "\n\t"#endif
Signature · 1 line
2.26.0.110.g2183baf09c-goog
Quoted 13 lines · Fangrui Song' via Clang Built Linux
Many instances of
__emit_inst(x)expand to a directive. In a few places it is used as a macro argument, e.g.arch/arm64/include/asm/sysreg.h #define __emit_inst(x) .inst (x) arch/arm64/include/asm/sysreg.h #define SET_PSTATE_PAN(x) __emit_inst(0xd500401f | PSTATE_PAN | ((!!x) << PSTATE_Imm_shift))
arch/arm64/kvm/hyp/entry.S
ALTERNATIVE(nop, SET_PSTATE_PAN(1), ARM64_HAS_PAN, CONFIG_ARM64_PAN)
Quoted 7 lines
Clang integrated assembler parses
.inst (x)as two arguments passing to a macro. We delete the space separator so that.inst(x)will be parsed as one argument.Note, GNU as parsing
.inst (x)as one argument is unintentional (for example the x86 backend will parse the construct as two arguments). See https://sourceware.org/bugzilla/show_bug.cgi?id=25750#c10
Thanks for the patch and for leading the discussion with the binutils developers on this curious parsing case.
Quoted 4 lines
Shouldn't this have:
Since Ilie sugguested this in
https://github.com/ClangBuiltLinux/linux/issues/939#issuecomment-601776123?
Quoted 9 lines
--- arch/arm64/include/asm/sysreg.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)arch/arm64/include/asm/sysreg.h +0
-0
arch/arm64/include/asm/sysreg.h line 56
Quoted 9 lines
arch/arm64/include/asm/sysreg.h +3
-1@@ -49,7 +49,9 @@#ifndef CONFIG_BROKEN_GAS_INST #ifdef __ASSEMBLY__ -#define __emit_inst (x) .inst (x) +// The space separator is omitted so that __emit_inst(x) can be parsed as +// either a directive or a macro argument. +#define __emit_inst (x) .inst(x)#else #define __emit_inst (x) ".inst " __stringify((x)) "\n\t"
What happens if someone starts using __emit_inst from inline assembly, and passes that subexpression to a macro? There are no users today in arch/arm64/, but I think it's better to change both.
With this applied on -next, and testing via:
$ ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make LLVM=1 LLVM_IAS=1 -j71 defconfig $ qemu-system-aarch64 -kernel arch/arm64/boot/Image.gz -machine virt -cpu cortex-a57 -nographic --append "console=ttyAMA0" -m 2048 -initrd <path to my buildroot.cpio>
I was able to build and boot, modulo:
https://github.com/ClangBuiltLinux/linux/issues/988 (new)
https://github.com/ClangBuiltLinux/linux/issues/716
https://github.com/ClangBuiltLinux/linux/issues/510 (more specific to LLD than AS)
so we're pretty close to being able to assemble an arm64 defconfig with Clang.
Signature · 7 lines
Thanks, ~Nick Desaulniers _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Mark Rutland
reviewerHi Fangrui,
Quoted 9 lines · Fangrui Song
Many instances of
__emit_inst(x)expand to a directive. In a few places it is used as a macro argument, e.g.arch/arm64/include/asm/sysreg.h #define __emit_inst(x) .inst (x) arch/arm64/include/asm/sysreg.h #define SET_PSTATE_PAN(x) __emit_inst(0xd500401f | PSTATE_PAN | ((!!x) << PSTATE_Imm_shift))
Quoted 6 lines
arch/arm64/kvm/hyp/entry.S
ALTERNATIVE(nop, SET_PSTATE_PAN(1), ARM64_HAS_PAN, CONFIG_ARM64_PAN)Clang integrated assembler parses
.inst (x)as two arguments passing to a macro. We delete the space separator so that.inst(x)will be parsed as one argument.
I'm a little confused by the above; sorry if the below sounds stupid or pedantic, but I just want to make sure I've understood the problem correctly.
For the above, ALTERNATIVE() and SET_PSTATE_PAN() are both preprocessor macros, so I would expect those to be expanded before either the integrated assembler or an external assembler consumes any of the assembly (and both would see the same expanded text). Given that, I'm a bit confused as to why the integrated assembly would have an impact on preprocessing.
Does compiling the pre-processed source using the integrated assembler result in the same behaviour? Can we see the expanded text to make that clear?
... at what stage exactly does this go wrong?
Thanks,
Mark.
Quoted 30 lines
Note, GNU as parsing
.inst (x)as one argument is unintentional (for example the x86 backend will parse the construct as two arguments). See https://sourceware.org/bugzilla/show_bug.cgi?id=25750#c10Signed-off-by Fangrui Song <[email protected]>--- arch/arm64/include/asm/sysreg.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)arch/arm64/include/asm/sysreg.h +3
-1@@ -49,7 +49,9 @@#ifndef CONFIG_BROKEN_GAS_INST #ifdef __ASSEMBLY__ -#define __emit_inst (x) .inst (x) +// The space separator is omitted so that __emit_inst(x) can be parsed as +// either a directive or a macro argument. +#define __emit_inst (x) .inst(x)#else #define __emit_inst (x) ".inst " __stringify((x)) "\n\t"#endif Signature · 1 line
2.26.0.110.g2183baf09c-goog
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Quoted 30 lines · Mark Rutland
Hi Fangrui,
Quoted 9 lines · Fangrui Song
Many instances of
__emit_inst(x)expand to a directive. In a few places it is used as a macro argument, e.g.arch/arm64/include/asm/sysreg.h #define __emit_inst(x) .inst (x) arch/arm64/include/asm/sysreg.h #define SET_PSTATE_PAN(x) __emit_inst(0xd500401f | PSTATE_PAN | ((!!x) << PSTATE_Imm_shift))Quoted 6 lines
arch/arm64/kvm/hyp/entry.S
ALTERNATIVE(nop, SET_PSTATE_PAN(1), ARM64_HAS_PAN, CONFIG_ARM64_PAN)Clang integrated assembler parses
.inst (x)as two arguments passing to a macro. We delete the space separator so that.inst(x)will be parsed as one argument.I'm a little confused by the above; sorry if the below sounds stupid or pedantic, but I just want to make sure I've understood the problem correctly.
For the above,
ALTERNATIVE()andSET_PSTATE_PAN()are both preprocessor macros, so I would expect those to be expanded before either the integrated assembler or an external assembler consumes any of the assembly (and both would see the same expanded text). Given that, I'm a bit confused as to why the integrated assembly would have an impact on preprocessing.
Quoted 8 lines
Does compiling the pre-processed source using the integrated assembler result in the same behaviour? Can we see the expanded text to make that clear?
... at what stage exactly does this go wrong?
Thanks,
Mark.
Hi Mark,
The C preprocessor expands arch/arm64/kvm/hyp/entry.S
ALTERNATIVE(nop, SET_PSTATE_PAN(1), ARM64_HAS_PAN, CONFIG_ARM64_PAN)
to:
alternative_insn nop, .inst (0xd500401f | ((0) << 16 | (4) << 5) | ((!!1) << 8)), 4, 1
alternative_insn is an assembler macro, not handled by the C preprocessor.
Both comma and space are separators, with an exception that content inside a pair of parentheses/quotes is not split, so clang -cc1as or GNU as x86 splits arguments this way:
nop, .inst, (0xd500401f | ((0) << 16 | (4) << 5) | ((!!1) << 8)), 4, 1
I actually feel that GNU as arm64's behavior is a little bit buggy. It works just because GNU as has another preprocessing step do_scrub_chars and its arm64 backend deletes the space before '('
alternative_insn nop,.inst(0xd500401f|((0)<<16|(4)<<5)|((!!1)<<8)),4,1
The x86 backend keeps the space before the outmost '('
alternative_insn nop,.inst (0xd500401f|((0)<<16|(4)<<5)|((!!1)<<8)),4,1
By reading its state machine, I think keeping the spaces will be the most reasonable behavior.
If .inst were only used as arguments,
alternative_insn nop, ".inst (0xd500401f | ((0) << 16 | (4) << 5) | ((!!1) << 8))", 4, 1
would be the best to avoid parsing issues.
Quoted 30 lines
Quoted 30 lines
Note, GNU as parsing
.inst (x)as one argument is unintentional (for example the x86 backend will parse the construct as two arguments). See https://sourceware.org/bugzilla/show_bug.cgi?id=25750#c10Signed-off-by Fangrui Song <[email protected]>--- arch/arm64/include/asm/sysreg.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)arch/arm64/include/asm/sysreg.h +3
-2@@ -49,7 +49,9 @@#ifndef CONFIG_BROKEN_GAS_INST #ifdef __ASSEMBLY__ -#define __emit_inst (x) .inst (x) +// The space separator is omitted so that __emit_inst(x) can be parsed as +// either a directive or a macro argument. +#define __emit_inst (x) .inst(x)#else #define __emit_inst (x) ".inst " __stringify((x)) "\n\t"#endif --2.26.0.110.g2183baf09c-goog
Quoted 49 lines · Fangrui Song
Quoted 30 lines · Mark Rutland
Hi Fangrui,
Quoted 9 lines · Fangrui Song
Many instances of
__emit_inst(x)expand to a directive. In a few places it is used as a macro argument, e.g.arch/arm64/include/asm/sysreg.h #define __emit_inst(x) .inst (x) arch/arm64/include/asm/sysreg.h #define SET_PSTATE_PAN(x) __emit_inst(0xd500401f | PSTATE_PAN | ((!!x) << PSTATE_Imm_shift))Quoted 6 lines
arch/arm64/kvm/hyp/entry.S
ALTERNATIVE(nop, SET_PSTATE_PAN(1), ARM64_HAS_PAN, CONFIG_ARM64_PAN)Clang integrated assembler parses
.inst (x)as two arguments passing to a macro. We delete the space separator so that.inst(x)will be parsed as one argument.I'm a little confused by the above; sorry if the below sounds stupid or pedantic, but I just want to make sure I've understood the problem correctly.
For the above,
ALTERNATIVE()andSET_PSTATE_PAN()are both preprocessor macros, so I would expect those to be expanded before either the integrated assembler or an external assembler consumes any of the assembly (and both would see the same expanded text). Given that, I'm a bit confused as to why the integrated assembly would have an impact on preprocessing.Quoted 8 lines
Does compiling the pre-processed source using the integrated assembler result in the same behaviour? Can we see the expanded text to make that clear?
... at what stage exactly does this go wrong?
Thanks,
Mark.Hi Mark,
The C preprocessor expands
arch/arm64/kvm/hyp/entry.S
ALTERNATIVE(nop, SET_PSTATE_PAN(1), ARM64_HAS_PAN, CONFIG_ARM64_PAN)to:
alternative_insn nop, .inst (0xd500401f | ((0) << 16 | (4) << 5) | ((!!1) << 8)), 4, 1
Quoted 7 lines
alternative_insnis an assembler macro, not handled by the C preprocessor.Both comma and space are separators, with an exception that content inside a pair of parentheses/quotes is not split, so clang -cc1as or GNU as x86 splits arguments this way:
nop, .inst, (0xd500401f | ((0) << 16 | (4) << 5) | ((!!1) << 8)), 4, 1
Thanks for this; I understand now.
Could we fold that into the commit message? I think this is much clearer than the current wording. The explicit description of separator behaviour, the pre-expansion of the CPP macros, and the example of how the assembler will split this really help.
Quoted 4 lines
I actually feel that GNU as arm64's behavior is a little bit buggy. It works just because GNU as has another preprocessing step
do_scrub_charsand its arm64 backend deletes the space before '('
Quoted 8 lines
alternative_insn nop,.inst(0xd500401f|((0)<<16|(4)<<5)|((!!1)<<8)),4,1The x86 backend keeps the space before the outmost '('
alternative_insnnop,.inst (0xd500401f|((0)<<16|(4)<<5)|((!!1)<<8)),4,1By reading its state machine, I think keeping the spaces will be the most reasonable behavior.
I think I agree. This deviation across architectures is unfortunate for such a low-level but common tool.
Quoted 25 lines
If .inst were only used as arguments,
alternative_insn nop, ".inst (0xd500401f | ((0) << 16 | (4) << 5) | ((!!1) << 8))", 4, 1would be the best to avoid parsing issues.
Quoted 19 lines
Quoted 19 lines
Note, GNU as parsing
.inst (x)as one argument is unintentional (for example the x86 backend will parse the construct as two arguments). See https://sourceware.org/bugzilla/show_bug.cgi?id=25750#c10Signed-off-by Fangrui Song <[email protected]>--- arch/arm64/include/asm/sysreg.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)arch/arm64/include/asm/sysreg.h +0
-0@@ -49,7 +49,9 @@#ifndef CONFIG_BROKEN_GAS_INST
Quoted 5 lines
Quoted 5 lines
arch/arm64/include/asm/sysreg.h line 54
Quoted 5 lines
arch/arm64/include/asm/sysreg.h +3
-1#ifdef __ASSEMBLY__ -#define __emit_inst (x) .inst (x) +// The space separator is omitted so that __emit_inst(x) can be parsed as +// either a directive or a macro argument. +#define __emit_inst (x) .inst(x)
Can we make this a bit more explicit and say "assembler macro argument"? That way we can avoid any confusion with a CPP macro.
With that (and with the details above folded into the commit message):
Thanks,
Mark.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
arch/arm64/include/asm/sysreg.h +0
-1#else #define __emit_inst (x) ".inst " __stringify((x)) "\n\t"#endif --2.26.0.110.g2183baf09c-goog