Skip to content

Correct implementation of gcc specific internal functions #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions hardware/arduino/cores/arduino/abi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <abi.h>
#include <stdint.h>
// TODO Uncomment this once you have libstdc++
//#include <exception>

#include <avr/io.h>
#include <avr/interrupt.h>

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<char*>(g));
}

inline uint8_t& sreg_part(__guard *g) {
return *(reinterpret_cast<uint8_t*>(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();
}
19 changes: 19 additions & 0 deletions hardware/arduino/cores/arduino/abi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* Header to define cxx abi parts missing from avrlibc */

#ifndef ABI_H_INCLUDED
#define ABI_H_INCLUDED

#include <stdlib.h>

__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
7 changes: 0 additions & 7 deletions hardware/arduino/cores/arduino/new.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {};

8 changes: 0 additions & 8 deletions hardware/arduino/cores/arduino/new.h
Original file line number Diff line number Diff line change
Expand Up @@ -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