1 #ifndef ASMSAFE_H 2 #define ASMSAFE_H 3 /* 4 macros to safely call C functions from most inline ASM 5 these should replace the use of __attribute__((naked)) functions for C code, 6 because C code is not actually legal in naked functions and can break in obscure ways. 7 see http://chdk.wikia.com/wiki/CHDK_Coding_Guidelines#Naked_functions 8 usage 9 asm volatile ( 10 ... 11 ASM_SAFE("BL my_func\n") 12 ... 13 ) 14 15 you can set up arguments for the function inside the ASM_SAFE without worrying about preserving values 16 e.g 17 ASM_SAFE( 18 "MOV R0,#1\n" 19 "BL my_func\n" 20 ) 21 */ 22 23 // push all regs except SP and PC 24 // push CSPR via R0 25 // restore value for R0 from stack 26 #define ASM_SAFE_ENTER \ 27 "STMFD SP!, {R0-R12,LR}\n" \ 28 "MRS R0, CPSR\n" \ 29 "STR R0,[SP,#-4]!\n" \ 30 "LDR R0,[SP,#4]\n" 31 32 // pop CSPR via R0 33 // pop all regs except SP and PC 34 #define ASM_SAFE_LEAVE \ 35 "LDR R0,[SP],#4\n" \ 36 "MSR CPSR_cxsf,R0\n" \ 37 "LDMFD SP!, {R0-R12,LR}\n" 38 39 #define ASM_SAFE(asmcode) \ 40 ASM_SAFE_ENTER \ 41 asmcode \ 42 ASM_SAFE_LEAVE 43 44 #endif // ASMSAFE_H