Naked functions

Jack Applegame japplegame at gmail.com
Fri Jul 27 10:11:03 UTC 2018


On Monday, 23 July 2018 at 23:36:01 UTC, David Nadlinger wrote:
> Hi Jack,
>
> On 23 Jul 2018, at 23:06, Jack Applegame via digitalmars-d-ldc 
> wrote:
>>> asm {
>>>    naked;
>>> }
>>
>> doesn't work.
>
> asm {} is the x86-only DMD syntax.
>
> See https://github.com/ldc-developers/ldc/pull/2773 which adds 
> a @naked attribute; it would to great to hear whether this fits 
> your use case.
>
>  — David

There is one strange thing.
Should  the compiler to insert the return command for naked 
functions?
I suppose it shouldn't.

For example:

naked.cpp
> __attribute__ ((naked)) int sum(int a, int b) {
>     return a + b;
> }

> $ arm-none-eabi-gcc -c naked.cpp -O3 -o naked_cpp.o
> $ arm-none-eabi-objdump -d naked_cpp.o

> 00000000 <_Z3sumii>:
>   0:   e0800001        add     r0, r0, r1


naked.d
> @naked int sum(int a, int b) {
>     return a + b;
> }

> $ ldc2 -c -mtriple=thumb-none-linux-eabi -mcpu=cortex-m3 -O3 
> naked.d -of naked_d.o
> $ arm-none-eabi-objdump -d naked_d.o

> 00000000 <_D5naked3sumFiiZi>:
>   0:   4408            add     r0, r1
>   2:   4770            bx      lr     <------- WHY???


Looks like LDC removes the prologue/epilogue, but not completely, 
leaving the return from function.




More information about the digitalmars-d-ldc mailing list