From 33ab387a79a67585bae356196e1ed9b58e123b3f Mon Sep 17 00:00:00 2001 From: Alexey Guseynov Date: Sun, 12 Aug 2012 23:07:41 +0400 Subject: [PATCH] Correct implementation of gcc specific internal functions --- hardware/arduino/cores/arduino/abi.cpp | 61 ++++++++++++++++++++++++++ hardware/arduino/cores/arduino/abi.h | 19 ++++++++ hardware/arduino/cores/arduino/new.cpp | 7 --- hardware/arduino/cores/arduino/new.h | 8 ---- 4 files changed, 80 insertions(+), 15 deletions(-) create mode 100644 hardware/arduino/cores/arduino/abi.cpp create mode 100644 hardware/arduino/cores/arduino/abi.h diff --git a/hardware/arduino/cores/arduino/abi.cpp b/hardware/arduino/cores/arduino/abi.cpp new file mode 100644 index 00000000000..f9f8b1fc454 --- /dev/null +++ b/hardware/arduino/cores/arduino/abi.cpp @@ -0,0 +1,61 @@ +#include +#include +// TODO Uncomment this once you have libstdc++ +//#include + +#include +#include + +namespace { +// guard is an integer type big enough to hold flag and a mutex. +// By default gcc uses long long int and avr ABI does not change it +// So we have 32 or 64 bits available. Actually, we need 16. + +inline char& flag_part(__guard *g) { + return *(reinterpret_cast(g)); +} + +inline uint8_t& sreg_part(__guard *g) { + return *(reinterpret_cast(g) + sizeof(char)); +} +} + +int __cxa_guard_acquire(__guard *g) { + uint8_t oldSREG = SREG; + cli(); + // Initialization of static variable has to be done with blocked interrupts + // because if this function is called from interrupt and sees that somebody + // else is already doing initialization it MUST wait until initializations + // is complete. That's impossible. + // If you don't want this overhead compile with -fno-threadsafe-statics + if (flag_part(g)) { + SREG = oldSREG; + return false; + } else { + sreg_part(g) = oldSREG; + return true; + } +} + +void __cxa_guard_release (__guard *g) { + flag_part(g) = 1; + SREG = sreg_part(g); +} + +void __cxa_guard_abort (__guard *g) { + SREG = sreg_part(g); +} + +void __cxa_pure_virtual(void) { + // We might want to write some diagnostics to uart in this case + // TODO Uncomment this once you have libstdc++ + //std::terminate(); + abort(); +} + +void __cxa_deleted_virtual(void) { + // We might want to write some diagnostics to uart in this case + // TODO Uncomment this once you have libstdc++ + //std::terminate(); + abort(); +} diff --git a/hardware/arduino/cores/arduino/abi.h b/hardware/arduino/cores/arduino/abi.h new file mode 100644 index 00000000000..370087e8c20 --- /dev/null +++ b/hardware/arduino/cores/arduino/abi.h @@ -0,0 +1,19 @@ +/* Header to define cxx abi parts missing from avrlibc */ + +#ifndef ABI_H_INCLUDED +#define ABI_H_INCLUDED + +#include + +__extension__ typedef int __guard __attribute__((mode (__DI__))); + +extern "C" { +int __cxa_guard_acquire(__guard *); +void __cxa_guard_release (__guard *); +void __cxa_guard_abort (__guard *); + +void __cxa_pure_virtual(void) __attribute__ ((__noreturn__)); +void __cxa_deleted_virtual(void) __attribute__ ((__noreturn__)); +} + +#endif diff --git a/hardware/arduino/cores/arduino/new.cpp b/hardware/arduino/cores/arduino/new.cpp index 0f6d4220ef7..35d66a58c11 100644 --- a/hardware/arduino/cores/arduino/new.cpp +++ b/hardware/arduino/cores/arduino/new.cpp @@ -9,10 +9,3 @@ void operator delete(void * ptr) { free(ptr); } - -int __cxa_guard_acquire(__guard *g) {return !*(char *)(g);}; -void __cxa_guard_release (__guard *g) {*(char *)g = 1;}; -void __cxa_guard_abort (__guard *) {}; - -void __cxa_pure_virtual(void) {}; - diff --git a/hardware/arduino/cores/arduino/new.h b/hardware/arduino/cores/arduino/new.h index cd940ce8b26..bd2a1b0ce38 100644 --- a/hardware/arduino/cores/arduino/new.h +++ b/hardware/arduino/cores/arduino/new.h @@ -10,13 +10,5 @@ void * operator new(size_t size); void operator delete(void * ptr); -__extension__ typedef int __guard __attribute__((mode (__DI__))); - -extern "C" int __cxa_guard_acquire(__guard *); -extern "C" void __cxa_guard_release (__guard *); -extern "C" void __cxa_guard_abort (__guard *); - -extern "C" void __cxa_pure_virtual(void); - #endif