Fun code with GCC

Everything ARM and LPC

Moderator: phalanx

Fun code with GCC

Postby gdisirio » Fri Jan 04, 2008 4:38 am

I am always trying new ways to improve my pet project. Usually I put most efforts into the scheduler code.

I found a way to perform a context switch into a C function without have to use any assembler instruction.

Look at this example:

Code: Select all
void DoSwitch(Thread *otp, Thread *ntp) {

  register struct intctx *sp asm("sp");
  otp->p_ctx.r13 = sp;
  sp = ntp->p_ctx.r13;
  asm ("" : : "r" (sp) : "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11", "lr");
}


The asm() tells the compiler that the function modifies all the specified registers, the compiler will save/restore all the registers into the function prologue/epilogue.
The register variable declaration enforces the compiler to allocate the *variable* sp into the *register* sp, this way it is possible to manipulate registers like normal C automatic variables.
The central instructions, save the stack pointer value into the context of the exiting thread and then loads the stack pointer of the newly executed thread.
The thing is even portable except for the register names that could be defined into a macro.
It is mostly something fun, I am not sure yet if I will use it into an official release but it is already working in a private build and it improves performance because there is one less call level in the scheduler.

LOL, I love GCC :)
ChibiOS/RT The RTOS supports ARM7, Cortex-M0, Cortex-M3, Cortex-M4, PPC e200z, MegaAVR, MSP430, STM8, Coldfire, H8S, x86.
gdisirio
 
Posts: 160
Joined: Sat Sep 22, 2007 12:34 am
Location: Salerno, Italy


Return to Everything ARM and LPC

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest