[llvm-dev] Inline ASM ARM syntax to load immediate values with integrated assembler

Friedman, Eli via llvm-dev llvm-dev at lists.llvm.org
Fri Aug 31 10:57:32 PDT 2018


On 8/31/2018 10:27 AM, Peter Smith via llvm-dev wrote:
> Hello Emmanuel,
>
> I've not been able to come up with anything that does exactly what you
> want. I've put some comments inline.
>
> On 31 August 2018 at 16:48, Emmanuel Blot via llvm-dev
> <llvm-dev at lists.llvm.org> wrote:
>> Hi,
>>
>> What would be the proper syntax in a C file, using inline assembly, to
>> load a immediate value into an ARM register using clang & integrated
>> assembler?
>>
> I don't think that there is a constraint that doesn't print the #. The
> source code refers to a modifier 'c' that does not print the # however
> that is rejected by clang. This was added very early on in the ARM
> backend and there weren't any tests added so it is possible that it
> has bitrotted. I'd be very happy to be proved wrong there.

The 'c' modifier works:

     #define CONSTANT 0x12345678

     void __attribute__((naked))
     foo(void)
     {
        asm volatile (
           "ldr r0, =%c0\n"
           "b 1f\n"
           ".ltorg\n"
           "1:\n"
           :
           :
           "i" (CONSTANT)
           :
           "r0"
        );
     }

Note that you must put an ".ltorg" directive into your inline assembly; 
otherwise, the constant might end up somewhere out of range.

If you're targeting armv6t2 or later, you can also just use movw/movt:

     #define CONSTANT 0x12345678

     void __attribute__((naked))
     foo(void)
     {
        asm volatile (
           "movw r0, %0\n"
           "movt r0, %1\n"
           //"b 1f\n"
           //".ltorg\n"
           //"1:\n"
           :
           :
           "i" (CONSTANT&0xFFFF),
           "i" (CONSTANT>>16)
           :
           "r0"
        );
     }

-Eli

-- 
Employee of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project



More information about the llvm-dev mailing list