/*
*	C function header macro
*	~~~~~~~~~~~~~~~~~~~~~~~
*	This macro generates a C callable function header in front of
*	an ARM function. If interworking is enabled, then it is simply
*	declared as an ARM function and the compiler is responsible for
*	the potential switch between ARM and THUMB. If interworking is not
*	enabled and we compile for THUMB, then the macro inserts the THUMB
*	to ARM switch in front of the function and declares it as a THUMB
*	function. The switch back to THUMB mode is always handled by the
*	assembly function itself.
*/

	.macro	afunc	name
	.text
	.align	2
	.globl	\name
	.type	\name\(),%function
#if defined(__thumb__)
	.code 	16
	.thumb_func
	.func
\name\():
	bx		pc
	nop
	.code	32
#else
	.code	32
	.func
\name\():
#endif
	.endm

/*
*	C function header macro
*	~~~~~~~~~~~~~~~~~~~~~~~
*	This macro generates a C callable function header in front of
*	a THUMB function. If interworking is enabled, then it is simply
*	declared as a THUMB function and the compiler is responsible for
*	the potential switch between ARM and THUMB. If interworking is not
*	enabled and we compile for ARM, then the macro inserts the ARM to
*	THUMB switch in front of the function and declares it as a ARM
*	function. The switch back to ARMB mode is always handled by the
*	assembly function itself. If we have to switch, we need to
*	use R12, a register that can be clobbered by a C function call.
*/

	.macro	tfunc	name
	.text
	.align	2
	.globl	\name
	.type	\name\(),%function
#if !defined(__thumb__)
	.code	32
//	.func
\name\():
	orr		ip,pc,#1
	bx		ip
	.code	16
#else
	.code 	16
	.thumb_func
\name\():
#endif
	.endm

/*
*	Calls a function through a register ptr from ARM mode
*	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*	If we have interworking, then it is a simple indirect call.
*	If we have no interworking and we're compiling for THUMB,
*	then we have to switch to THUMB when calling the function.
*	Since the function doesn't know about ARM mode whatsoever,
*	it returns to us in THUMB mode, so we have to switch back
*	to ARM before we can continue.
*/

	.macro	callreg reg
#if ! defined(__THUMB_INTERWORK__) && defined(__thumb__)
	orr		\reg\(),\reg\(),#1
	mov		lr,pc
	bx		\reg
	.code	16
	bx		pc
	nop
	.code	32
#else
	mov		lr,pc
	bx		\reg
#endif
	.endm

