I'm targeting ARM Cortex-M bare metal, and I need my reset interrupt handler marked with the 'naked' attribute as it will be called before I've initialized my the hardware containing my stack. It can also be used to make a useful hard fault handler for finding elusive bugs as described here. http://www.freertos.org/Debugging-Hard-Faults-On-Cortex-M-Microcontrollers.html I could do what I need in assembly, but then I'd break my "do it all in D" goal. I propose the following syntax: @attribute("naked") void myNakedFunction() { } The GCC 'naked' attribute is documented here: gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Created attachment 64 [details] Patch to add @attribute("naked") Can you test this patch please? (I was kinda surprised it didn't have an effect on x86 till I read that it's only supported on some architectures and not on x86...) @Iain Do you think we should include such system-specific attributes? Naked is also special in that dmd already has a 'generic' syntax for it, but it's tied to DMD-style inline asm. I also had to add a noclone attribute because naked automatically sets noclone. Do you think we should somehow hide 'noclone' from @attribute / user code?
Thank you for addressing this. I had trouble applying this patch... ----------------------------------------------------- patching file gcc/d/d-lang.cc Hunk #2 FAILED at 58. 1 out of 3 hunks FAILED -- saving rejects to file gcc/d/d-lang.cc.rej ----------------------------------------------------- ... so I edited the d-lang.cc directly. GDC built without any problems using the latest 4.8.2 branch and these instructions on the wiki: http://wiki.dlang.org/Bare_Metal_ARM_Cortex-M_GDC_Cross_Compiler. However, when compiling my program, I get the following error: ----------------------------------------------------- <built-in>:0: internal compiler error: in register_scoped_attribute, at attribs.c:291 0x6325b4 register_scoped_attribute ../../gcc-4.8.2/gcc/attribs.c:291 0x632645 register_scoped_attributes(attribute_spec const*, char const*) ../../gcc-4.8.2/gcc/attribs.c:149 0x6329a8 init_attributes() ../../gcc-4.8.2/gcc/attribs.c:254 0x6331bc decl_attributes(tree_node**, tree_node*, int) ../../gcc-4.8.2/gcc/attribs.c:356 0x805dce add_builtin_function_common ../../gcc-4.8.2/gcc/langhooks.c:557 0x8065f3 add_builtin_function(char const*, tree_node*, int, built_in_class, char const*, tree_node*) ../../gcc-4.8.2/gcc/langhooks.c:577 0x650def do_build_builtin_fn ../../gcc-4.8.2/gcc/d/d-builtins.c:1055 0x657138 d_init_builtins() ../../gcc-4.8.2/gcc/builtins.def:195 0x664c66 d_backend_init() ../../gcc-4.8.2/gcc/d/d-builtins.c:1765 0x633ef4 d_init ../../gcc-4.8.2/gcc/d/d-lang.cc:257 0x8ee1d6 lang_dependent_init ../../gcc-4.8.2/gcc/toplev.c:1688 0x8ee1d6 do_compile ../../gcc-4.8.2/gcc/toplev.c:1850 ----------------------------------------------------- This occurs regardless of whether I use any attributes or not.
strange, it applies just fine here. patch -p1 -i naked.patch tested on revision fdf0c614b8aef10b846689bef39030c482692b5f The error could be caused by failed/incorrect patching?
I got the patch applied by downloading it from a different computer (had a line ending problem, I think). Nevertheless, the error is the same. I'm using the same revision (fdf0c614b8aef10b846689bef39030c482692b5f). My program builds fine without this patch. Have you tried this patch on one of the ARM cross-compilers?
If memory serves, there's a target hook which you can test if an attribute is handled by the backend. Use this instead of making naked a frontend attribute.
Created attachment 65 [details] Patch to add @attribute("naked") Updated patch. Naked is a 'target attribute' and needs to be handled in a special way. This should work now, @Iain can you review this? (AFAICS we can't access the list of available target attributes, so @attribute("naked") will always compile but you'll get a warning from the backend if naked is not supported on this architecture)
@Iain there's TARGET_ATTRIBUTE_TABLE but I don't know if/how I could access that?
The latest patch seems to work well. /**** void OnReset() ****/ 8000024: b580 push {r7, lr} 8000026: af00 add r7, sp, #0 8000028: 2003 movs r0, #3 800002a: f000 f885 bl 8000138 800002e: f8df 2004 ldr.w r2, [pc, #4] ; 8000034 8000032: 4710 bx r2 /**** @attribute("naked") void OnReset() ****/ _D5start7OnResetFZv(): 8000024: 2003 movs r0, #3 8000026: f000 f883 bl 8000130 800002a: f8df 2004 ldr.w r2, [pc, #4] ; 8000030 800002e: 4710 bx r2
targetm.attribute_table - when we initialise our string table of attributes, load them in too.
Created attachment 66 [details] Patch to add target attributes into lookup table Added patch which describes what I was talking about. There used to be a TARGET_OPTION_VALID_ATTRIBUTE_P hook, but it seems to have been removed.
https://github.com/D-Programming-GDC/GDC/commit/50f446539a061d34b9f3c01d0efabb6845c5a74d https://github.com/D-Programming-GDC/GDC/commit/dc224619a319b280a8169935b8aa83fd407fff47