From 4651cd2b1b95da77fcf084ac03b4df631b92e01b Mon Sep 17 00:00:00 2001
From: Alberto Nidasio <nidasioalberto@gmail.com>
Date: Mon, 1 Jul 2024 14:38:20 +0200
Subject: [PATCH] Removed `register` keyword from code

For example in interrupts_cortexMx.cpp:
```cpp
static unsigned int getProgramCounter()
{
    unsigned int result;
    // Get program counter when the exception was thrown from stack frame
    asm volatile("mrs   %0,  psp    \n\t"
                 "add   %0, %0, #24 \n\t"
                 "ldr   %0, [%0]    \n\t":"=r"(result));
    return result;
}
```

To check differences in the generated code we disassempled the compiled source with and without the keyword, both for O0 and O2 and both for gcc and clang.
Only in a couple of cases with O0 and gcc there were some differences in the instructions, but non in the semantics

Removed `register` keyword and changed how the stack pointer is copied in `getCurrentFreeStack` function
---
 .../common/interfaces-impl/delays.cpp         |  2 +-
 .../common/interfaces-impl/delays.cpp         |  2 +-
 .../common/interfaces-impl/delays.cpp         |  2 +-
 .../common/interfaces-impl/delays.cpp         | 24 +++++++++----------
 .../common/interfaces-impl/delays.cpp         |  6 ++---
 .../common/interfaces-impl/delays.cpp         |  2 +-
 .../common/interfaces-impl/delays.cpp         | 12 +++++-----
 .../common/interfaces-impl/delays.cpp         |  8 +++----
 .../common/interfaces-impl/delays.cpp         | 18 +++++++-------
 .../common/interfaces-impl/delays.cpp         |  2 +-
 .../common/interfaces-impl/delays.cpp         |  4 ++--
 .../armv4/interfaces-impl/atomic_ops_impl.h   | 16 ++++++-------
 .../cpu/armv4/interfaces-impl/interrupts.cpp  |  6 ++---
 .../armv6m/interfaces-impl/interrupts_impl.h  |  2 +-
 .../armv7m/interfaces-impl/interrupts_impl.h  |  2 +-
 miosix/libsyscalls/memoryprofiling.cpp        |  2 +-
 miosix/util/util.cpp                          |  2 +-
 17 files changed, 56 insertions(+), 56 deletions(-)

diff --git a/miosix/arch/cortexM0_stm32f0/common/interfaces-impl/delays.cpp b/miosix/arch/cortexM0_stm32f0/common/interfaces-impl/delays.cpp
index c0d1247e..c02d8aac 100644
--- a/miosix/arch/cortexM0_stm32f0/common/interfaces-impl/delays.cpp
+++ b/miosix/arch/cortexM0_stm32f0/common/interfaces-impl/delays.cpp
@@ -32,7 +32,7 @@ namespace miosix {
 void delayMs(unsigned int mseconds)
 {
     #ifdef SYSCLK_FREQ_32MHz
-    register const unsigned int count=4000;
+    const unsigned int count=4000;
     #else
     #error "Delays are uncalibrated for this clock frequency"
     #endif
diff --git a/miosix/arch/cortexM3_efm32g/common/interfaces-impl/delays.cpp b/miosix/arch/cortexM3_efm32g/common/interfaces-impl/delays.cpp
index de650221..17c0e0ac 100644
--- a/miosix/arch/cortexM3_efm32g/common/interfaces-impl/delays.cpp
+++ b/miosix/arch/cortexM3_efm32g/common/interfaces-impl/delays.cpp
@@ -32,7 +32,7 @@ namespace miosix {
 
 void delayMs(unsigned int mseconds)
 {
-    register const unsigned int count=cpuFrequency/4000;
+    const unsigned int count=cpuFrequency/4000;
 
     for(unsigned int i=0;i<mseconds;i++)
     {
diff --git a/miosix/arch/cortexM3_efm32gg/common/interfaces-impl/delays.cpp b/miosix/arch/cortexM3_efm32gg/common/interfaces-impl/delays.cpp
index c66d3379..9fbb022c 100644
--- a/miosix/arch/cortexM3_efm32gg/common/interfaces-impl/delays.cpp
+++ b/miosix/arch/cortexM3_efm32gg/common/interfaces-impl/delays.cpp
@@ -32,7 +32,7 @@ namespace miosix {
 
 void delayMs(unsigned int mseconds)
 {
-    register const unsigned int count=cpuFrequency/4000;
+    const unsigned int count=cpuFrequency/4000;
 
     for(unsigned int i=0;i<mseconds;i++)
     {
diff --git a/miosix/arch/cortexM3_stm32f1/common/interfaces-impl/delays.cpp b/miosix/arch/cortexM3_stm32f1/common/interfaces-impl/delays.cpp
index a1039262..2efea4e5 100644
--- a/miosix/arch/cortexM3_stm32f1/common/interfaces-impl/delays.cpp
+++ b/miosix/arch/cortexM3_stm32f1/common/interfaces-impl/delays.cpp
@@ -33,33 +33,33 @@ void delayMs(unsigned int mseconds)
 {
     #ifndef __CODE_IN_XRAM
     #ifdef SYSCLK_FREQ_72MHz
-    register const unsigned int count=12000; //Flash 2 wait state
+    const unsigned int count=12000; //Flash 2 wait state
     #elif SYSCLK_FREQ_56MHz
-    register const unsigned int count=9333;  //Flash 2 wait state
+    const unsigned int count=9333;  //Flash 2 wait state
     #elif SYSCLK_FREQ_48MHz
-    register const unsigned int count=12000; //Flash 1 wait state
+    const unsigned int count=12000; //Flash 1 wait state
     #elif SYSCLK_FREQ_36MHz
-    register const unsigned int count=9000;  //Flash 1 wait state
+    const unsigned int count=9000;  //Flash 1 wait state
     #elif SYSCLK_FREQ_24MHz
-    register const unsigned int count=8000;  //Flash 0 wait state
+    const unsigned int count=8000;  //Flash 0 wait state
     #else
-    register const unsigned int count=2678;  //Flash 0 wait state
+    const unsigned int count=2678;  //Flash 0 wait state
     #endif
     #else //__CODE_IN_XRAM
     //These delays are calibrated on an stm3210e-eval, and are only correct when
     //running from ram memories with similar access timings
     #ifdef SYSCLK_FREQ_72MHz
-    register const unsigned int count=1889; //Linear scaling, factor 26.236
+    const unsigned int count=1889; //Linear scaling, factor 26.236
     #elif SYSCLK_FREQ_56MHz
-    register const unsigned int count=1469;
+    const unsigned int count=1469;
     #elif SYSCLK_FREQ_48MHz
-    register const unsigned int count=1259;
+    const unsigned int count=1259;
     #elif SYSCLK_FREQ_36MHz
-    register const unsigned int count=945;
+    const unsigned int count=945;
     #elif SYSCLK_FREQ_24MHz
-    register const unsigned int count=630;
+    const unsigned int count=630;
     #else
-    register const unsigned int count=210;
+    const unsigned int count=210;
     #endif
     #endif //__CODE_IN_XRAM
     for(unsigned int i=0;i<mseconds;i++)
diff --git a/miosix/arch/cortexM3_stm32f2/common/interfaces-impl/delays.cpp b/miosix/arch/cortexM3_stm32f2/common/interfaces-impl/delays.cpp
index 9f82e318..42179568 100644
--- a/miosix/arch/cortexM3_stm32f2/common/interfaces-impl/delays.cpp
+++ b/miosix/arch/cortexM3_stm32f2/common/interfaces-impl/delays.cpp
@@ -34,7 +34,7 @@ void delayMs(unsigned int mseconds)
     #ifndef __CODE_IN_XRAM
 
     #ifdef SYSCLK_FREQ_120MHz
-    register const unsigned int count=40000; //Flash 3 wait state
+    const unsigned int count=40000; //Flash 3 wait state
     #else //SYSCLK_FREQ_120MHz
     #error "Delays are uncalibrated for this clock frequency"
     #endif //SYSCLK_FREQ_120MHz
@@ -45,9 +45,9 @@ void delayMs(unsigned int mseconds)
 
     //When running code from external RAM delays depend on the RAM timings
     #if defined(_BOARD_STM3220G_EVAL)
-    register const unsigned int count=5000;
+    const unsigned int count=5000;
     #elif defined(_BOARD_ETHBOARDV2)
-    register const unsigned int count=6000;
+    const unsigned int count=6000;
     #else
     #error "Delays are uncalibrated for this clock board"
     #endif
diff --git a/miosix/arch/cortexM4_atsam4l/common/interfaces-impl/delays.cpp b/miosix/arch/cortexM4_atsam4l/common/interfaces-impl/delays.cpp
index 846a19cb..44eeeb70 100644
--- a/miosix/arch/cortexM4_atsam4l/common/interfaces-impl/delays.cpp
+++ b/miosix/arch/cortexM4_atsam4l/common/interfaces-impl/delays.cpp
@@ -32,7 +32,7 @@ namespace miosix {
 
 void delayMs(unsigned int mseconds)
 {
-    register const unsigned int count = bootClock / 4000;
+    const unsigned int count = bootClock / 4000;
 
     for(unsigned int i=0;i<mseconds;i++)
     {
diff --git a/miosix/arch/cortexM4_stm32f3/common/interfaces-impl/delays.cpp b/miosix/arch/cortexM4_stm32f3/common/interfaces-impl/delays.cpp
index fc77be97..50f907b4 100644
--- a/miosix/arch/cortexM4_stm32f3/common/interfaces-impl/delays.cpp
+++ b/miosix/arch/cortexM4_stm32f3/common/interfaces-impl/delays.cpp
@@ -35,17 +35,17 @@ void delayMs(unsigned int mseconds)
 {
     #ifdef SYSCLK_FREQ_72MHz
     #warning delayMs has not been calibrated yet with 72MHz clock
-    register const unsigned int count=5350;
+    const unsigned int count=5350;
     #elif SYSCLK_FREQ_56MHz
-    register const unsigned int count=5350;
+    const unsigned int count=5350;
     #elif SYSCLK_FREQ_48MHz
-    register const unsigned int count=5350;
+    const unsigned int count=5350;
     #elif SYSCLK_FREQ_36MHz
-    register const unsigned int count=4010;
+    const unsigned int count=4010;
     #elif SYSCLK_FREQ_24MHz
-    register const unsigned int count=4010;
+    const unsigned int count=4010;
     #else // 8MHz clock
-    register const unsigned int count=2000;
+    const unsigned int count=2000;
     #endif
 
     for(unsigned int i=0;i<mseconds;i++)
diff --git a/miosix/arch/cortexM4_stm32f4/common/interfaces-impl/delays.cpp b/miosix/arch/cortexM4_stm32f4/common/interfaces-impl/delays.cpp
index 8285dba2..71d1741d 100644
--- a/miosix/arch/cortexM4_stm32f4/common/interfaces-impl/delays.cpp
+++ b/miosix/arch/cortexM4_stm32f4/common/interfaces-impl/delays.cpp
@@ -32,13 +32,13 @@ namespace miosix {
 void delayMs(unsigned int mseconds)
 {
     #ifdef SYSCLK_FREQ_180MHz
-    register const unsigned int count=45000;
+    const unsigned int count=45000;
     #elif defined(SYSCLK_FREQ_168MHz)
-    register const unsigned int count=42000;
+    const unsigned int count=42000;
     #elif defined(SYSCLK_FREQ_100MHz)
-    register const unsigned int count=25000;
+    const unsigned int count=25000;
     #elif SYSCLK_FREQ_84MHz
-    register const unsigned int count=21000;
+    const unsigned int count=21000;
     #else
     #warning "Delays are uncalibrated for this clock frequency"
     #endif
diff --git a/miosix/arch/cortexM4_stm32l4/common/interfaces-impl/delays.cpp b/miosix/arch/cortexM4_stm32l4/common/interfaces-impl/delays.cpp
index 434513e3..02eb335a 100644
--- a/miosix/arch/cortexM4_stm32l4/common/interfaces-impl/delays.cpp
+++ b/miosix/arch/cortexM4_stm32l4/common/interfaces-impl/delays.cpp
@@ -32,23 +32,23 @@ namespace miosix {
 void delayMs(unsigned int mseconds)
 {
     #ifdef SYSCLK_FREQ_80MHz
-    register const unsigned int count=20000;
+    const unsigned int count=20000;
     #elif defined(SYSCLK_FREQ_56MHz)
-    register const unsigned int count=14000;
+    const unsigned int count=14000;
     #elif defined(SYSCLK_FREQ_48MHz)
-    register const unsigned int count=12000;
+    const unsigned int count=12000;
     #elif defined(SYSCLK_FREQ_36MHz)
-    register const unsigned int count=9000;
+    const unsigned int count=9000;
     #elif defined(SYSCLK_FREQ_24MHz)
-    register const unsigned int count=6000;
+    const unsigned int count=6000;
     #elif defined(HSE_VALUE) && HSE_VALUE==16000000
-    register const unsigned int count=4000;
+    const unsigned int count=4000;
     #elif defined(HSE_VALUE) && HSE_VALUE==8000000
-    register const unsigned int count=2000;
+    const unsigned int count=2000;
     #elif defined(RUN_WITH_HSI)
-    register const unsigned int count=4000;
+    const unsigned int count=4000;
     #elif defined(RUN_WITH_MSI)
-    register const unsigned int count=1000;
+    const unsigned int count=1000;
     #else
     #error "Delays are uncalibrated for this clock frequency"
     #endif
diff --git a/miosix/arch/cortexM7_stm32f7/common/interfaces-impl/delays.cpp b/miosix/arch/cortexM7_stm32f7/common/interfaces-impl/delays.cpp
index 4c99619e..8126d560 100644
--- a/miosix/arch/cortexM7_stm32f7/common/interfaces-impl/delays.cpp
+++ b/miosix/arch/cortexM7_stm32f7/common/interfaces-impl/delays.cpp
@@ -33,7 +33,7 @@ void delayMs(unsigned int mseconds)
 {
     //Note: flash wait state don't matter because of icache
     #ifdef SYSCLK_FREQ_216MHz
-    register const unsigned int count=216000;
+    const unsigned int count=216000;
     #else
     #warning "Delays are uncalibrated for this clock frequency"
     #endif
diff --git a/miosix/arch/cortexM7_stm32h7/common/interfaces-impl/delays.cpp b/miosix/arch/cortexM7_stm32h7/common/interfaces-impl/delays.cpp
index 02d76948..8f392931 100644
--- a/miosix/arch/cortexM7_stm32h7/common/interfaces-impl/delays.cpp
+++ b/miosix/arch/cortexM7_stm32h7/common/interfaces-impl/delays.cpp
@@ -33,9 +33,9 @@ void delayMs(unsigned int mseconds)
 {
     //Note: flash wait state don't matter because of icache
     #ifdef SYSCLK_FREQ_550MHz
-    register const unsigned int count=550000;
+    const unsigned int count=550000;
     #elif defined(SYSCLK_FREQ_400MHz)
-    register const unsigned int count=400000;
+    const unsigned int count=400000;
     #else
     #error "Delays are uncalibrated for this clock frequency"
     #endif
diff --git a/miosix/arch/cpu/armv4/interfaces-impl/atomic_ops_impl.h b/miosix/arch/cpu/armv4/interfaces-impl/atomic_ops_impl.h
index 12768aab..327d41c9 100644
--- a/miosix/arch/cpu/armv4/interfaces-impl/atomic_ops_impl.h
+++ b/miosix/arch/cpu/armv4/interfaces-impl/atomic_ops_impl.h
@@ -32,14 +32,14 @@ namespace miosix {
 inline int atomicSwap(volatile int *p, int v)
 {
     //This is the only atomic operation in the ARM7 assembler
-    register int result;
+    int result;
     asm volatile("swp %0, %1, [%2]" : "=&r"(result) : "r"(v),"r"(p) : "memory");
     return result;
 }
 
 inline void atomicAdd(volatile int *p, int incr)
 {
-    register int a,b; //Temporaries used by ASM code
+    int a,b; //Temporaries used by ASM code
     asm volatile("    mrs   %0,     cpsr         \n"
                  "    tst   %0,     #0x80        \n"
                  "    orreq %1,     %0,    #0x80 \n"
@@ -56,8 +56,8 @@ inline void atomicAdd(volatile int *p, int incr)
 
 inline int atomicAddExchange(volatile int *p, int incr)
 {
-    register int a; //Temporaries used by ASM code
-    register int result;
+    int a; //Temporaries used by ASM code
+    int result;
     asm volatile("    mrs   %0,     cpsr         \n"
                  "    tst   %0,     #0x80        \n"
                  "    orreq %1,     %0,    #0x80 \n"
@@ -75,8 +75,8 @@ inline int atomicAddExchange(volatile int *p, int incr)
 
 inline int atomicCompareAndSwap(volatile int *p, int prev, int next)
 {
-    register int a; //Temporaries used by ASM code
-    register int result;
+    int a; //Temporaries used by ASM code
+    int result;
     asm volatile("    mrs   %0,     cpsr         \n"
                  "    tst   %0,     #0x80        \n"
                  "    orreq %1,     %0,    #0x80 \n"
@@ -95,8 +95,8 @@ inline int atomicCompareAndSwap(volatile int *p, int prev, int next)
 inline void *atomicFetchAndIncrement(void * const volatile * p, int offset,
         int incr)
 {
-    register int a,b; //Temporaries used by ASM code
-    register void *result;
+    int a,b; //Temporaries used by ASM code
+    void *result;
     asm volatile("    mrs   %0,     cpsr             \n"
                  "    tst   %0,     #0x80            \n"
                  "    orreq %1,     %0,    #0x80     \n"
diff --git a/miosix/arch/cpu/armv4/interfaces-impl/interrupts.cpp b/miosix/arch/cpu/armv4/interfaces-impl/interrupts.cpp
index 84c2c654..2416fab3 100644
--- a/miosix/arch/cpu/armv4/interfaces-impl/interrupts.cpp
+++ b/miosix/arch/cpu/armv4/interfaces-impl/interrupts.cpp
@@ -95,7 +95,7 @@ extern "C" void UNDEF_Routine()
     //These two instructions MUST be the first two instructions of the interrupt
     //routine. They store in return_address the pc of the instruction that
     //caused the interrupt.
-    register int returnAddress;
+    int returnAddress;
     asm volatile("mov	%0, lr" : "=r"(returnAddress));
 
     IRQerrorLog("\r\n***Unexpected UNDEF @ ");
@@ -119,7 +119,7 @@ extern "C" void DABT_Routine()
     //These two instructions MUST be the first two instructions of the interrupt
     //routine. They store in return_address the pc of the instruction that
     //caused the interrupt. (lr has an offset of 8 during a data abort)
-    register int returnAddress;
+    int returnAddress;
     asm volatile("sub	%0, lr, #8" : "=r"(returnAddress));
 
     IRQerrorLog("\r\n***Unexpected data abort @ ");
@@ -143,7 +143,7 @@ extern "C" void PABT_Routine()
     //These two instructions MUST be the first two instructions of the interrupt
     //routine. They store in return_address the pc of the instruction that
     //caused the interrupt. (lr has an offset of 4 during a data abort)
-    register int returnAddress;
+    int returnAddress;
     asm volatile("sub	%0, lr, #4" : "=r"(returnAddress));
 
     IRQerrorLog("\r\n***Unexpected prefetch abort @ ");
diff --git a/miosix/arch/cpu/armv6m/interfaces-impl/interrupts_impl.h b/miosix/arch/cpu/armv6m/interfaces-impl/interrupts_impl.h
index bc4c0863..ca55b2cb 100644
--- a/miosix/arch/cpu/armv6m/interfaces-impl/interrupts_impl.h
+++ b/miosix/arch/cpu/armv6m/interfaces-impl/interrupts_impl.h
@@ -57,7 +57,7 @@ inline void fastEnableInterrupts() noexcept
 
 inline bool areInterruptsEnabled() noexcept
 {
-    register int i;
+    int i;
     asm volatile("mrs   %0, primask    \n\t":"=r"(i));
     if(i!=0) return false;
     return true;
diff --git a/miosix/arch/cpu/armv7m/interfaces-impl/interrupts_impl.h b/miosix/arch/cpu/armv7m/interfaces-impl/interrupts_impl.h
index bc4c0863..ca55b2cb 100644
--- a/miosix/arch/cpu/armv7m/interfaces-impl/interrupts_impl.h
+++ b/miosix/arch/cpu/armv7m/interfaces-impl/interrupts_impl.h
@@ -57,7 +57,7 @@ inline void fastEnableInterrupts() noexcept
 
 inline bool areInterruptsEnabled() noexcept
 {
-    register int i;
+    int i;
     asm volatile("mrs   %0, primask    \n\t":"=r"(i));
     if(i!=0) return false;
     return true;
diff --git a/miosix/libsyscalls/memoryprofiling.cpp b/miosix/libsyscalls/memoryprofiling.cpp
index db85f55e..7bf144ca 100644
--- a/miosix/libsyscalls/memoryprofiling.cpp
+++ b/miosix/libsyscalls/memoryprofiling.cpp
@@ -107,7 +107,7 @@ unsigned int MemoryProfiling::getAbsoluteFreeStack()
 unsigned int MemoryProfiling::getCurrentFreeStack()
 {
     unsigned int stackOccupiedByCtxsave=sysconf(ctxsaveOnStack);
-    register int *stack_ptr asm("sp");
+    int *stack_ptr asm("sp");
     const unsigned int *walk=getStackBottom();
     unsigned int freeStack=(reinterpret_cast<unsigned int>(stack_ptr)
                           - reinterpret_cast<unsigned int>(walk));
diff --git a/miosix/util/util.cpp b/miosix/util/util.cpp
index 721fbdc3..ac8b9617 100644
--- a/miosix/util/util.cpp
+++ b/miosix/util/util.cpp
@@ -94,7 +94,7 @@ unsigned int MemoryProfiling::getAbsoluteFreeStack()
 
 unsigned int MemoryProfiling::getCurrentFreeStack()
 {
-    register int *stack_ptr asm("sp");
+    int *stack_ptr;
     const unsigned int *walk=Thread::getStackBottom();
     unsigned int freeStack=(reinterpret_cast<unsigned int>(stack_ptr)
                           - reinterpret_cast<unsigned int>(walk));
-- 
GitLab