Skip to content
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

Convert the C++ support to use exceptions for "proper" language support. #40

Draft
wants to merge 4 commits into
base: cpp_fixes
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion apps/hello_cpp/hello_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ class HelloBuf : public AbstractHello {
}
};

extern "C" void c_main() {
void cpp_main() {
AbstractHello *hellos[] = {
new HelloSDP(), new HelloSDRAM(), new HelloBuf()
};

for (AbstractHello *hello : hellos) {
hello->hello("Hello world from C++!");
}
Expand Down
40 changes: 24 additions & 16 deletions include/sark_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,46 @@ extern "C"
#include <cstdlib>
#include <new>

inline void* operator new (size_t size) noexcept {
void *p = sark_alloc(1, size);
while (p == nullptr) {
rt_error(RTE_SWERR);
inline void* operator new (size_t size) {
void *ptr = sark_alloc(1, size);
if (ptr == nullptr) {
throw std::bad_alloc();
}
return p;
return ptr;
}

inline void operator delete (void *p) noexcept {
sark_free(p);
inline void operator delete (void *ptr) noexcept {
if (ptr != nullptr) {
sark_free(ptr);
}
}

inline void* operator new [](size_t size) noexcept {
return operator new (size); // Same as regular new
inline void* operator new [](size_t size) {
void *ptr = sark_alloc(1, size);
if (ptr == nullptr) {
throw std::bad_alloc();
}
return ptr;
}

inline void operator delete [](void *p) noexcept {
operator delete (p); // Same as regular delete
inline void operator delete [](void *ptr) noexcept {
operator delete (ptr); // Same as regular delete
}

inline void* operator new (size_t size, std::nothrow_t) noexcept {
return sark_alloc(1, size);
}

inline void operator delete (void *p, std::nothrow_t) noexcept {
operator delete (p); // Same as regular delete
inline void operator delete (void *ptr, std::nothrow_t) noexcept {
operator delete (ptr); // Same as regular delete
}

inline void* operator new [](size_t size, std::nothrow_t) noexcept {
inline void* operator new [](size_t size, std::nothrow_t nothrow) noexcept {
return sark_alloc(1, size);
}

inline void operator delete [](void *p, std::nothrow_t) noexcept {
operator delete (p); // Same as regular delete
inline void operator delete [](void *ptr, std::nothrow_t nothrow) noexcept {
operator delete (ptr); // Same as regular delete
}

extern void cpp_main(void);
4 changes: 2 additions & 2 deletions make/Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ ifeq ($(GNU),1)
# GNU Compiler (gcc) settings
AS := $(GP)-as --defsym GNU=1 -mthumb-interwork -march=armv5te
CC_NO_THUMB := $(GP)-gcc -c -mthumb-interwork -march=armv5te -std=gnu99 -ffreestanding -I $(SPINN_INC_DIR)
CXX_NO_THUMB := $(GP)-gcc -c -mthumb-interwork -march=armv5te -std=c++11 -ffreestanding -I $(SPINN_INC_DIR) -fno-rtti -fno-exceptions
CXX_NO_THUMB := $(GP)-gcc -c -mthumb-interwork -march=armv5te -std=c++11 -ffreestanding -I $(SPINN_INC_DIR) -fno-rtti
CC_THUMB := $(CC_NO_THUMB) -mthumb -DTHUMB
CXX_THUMB := $(CXX_NO_THUMB) -mthumb -DTHUMB

Expand All @@ -78,7 +78,7 @@ else
# ARM Compiler settings
AS := armasm --keep --cpu=5te --apcs interwork
CC_NO_THUMB := armcc -c --c99 --cpu=5te --apcs interwork --min_array_alignment=4 -I $(SPINN_INC_DIR)
CXX_NO_THUMB := armcc -c --cpp11 --cpu=5te --apcs interwork --min_array_alignment=4 -I $(SPINN_INC_DIR) --no_rtti --no_exceptions
CXX_NO_THUMB := armcc -c --cpp11 --cpu=5te --apcs interwork --min_array_alignment=4 -I $(SPINN_INC_DIR) --no_rtti
CC_THUMB := $(CC_NO_THUMB) --thumb -DTHUMB
CXX_THUMB := $(CXX_NO_THUMB) --thumb -DTHUMB

Expand Down
5 changes: 4 additions & 1 deletion sark/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,14 @@ endif

#-------------------------------------------------------------------------------

# SARK C sources
# SARK C and C++ sources

$(BUILD_DIR)/%.o: %.c $(SPINN_INC_DIR)/spinnaker.h $(SPINN_INC_DIR)/sark.h $(SPINN_INC_DIR)/spin1_api.h
$(CC_THUMB) $(CFLAGS) -o $@ $<

$(BUILD_DIR)/%.o: %.cpp $(SPINN_INC_DIR)/spinnaker.h $(SPINN_INC_DIR)/sark.h $(SPINN_INC_DIR)/spin1_api.h
$(CXX_THUMB) $(CFLAGS) -o $@ $<

$(BUILD_DIR)/sark_isr.o: sark_isr.c $(SPINN_INC_DIR)/spinnaker.h $(SPINN_INC_DIR)/sark.h $(SPINN_INC_DIR)/spin1_api.h
$(CC) $(CFLAGS) -o $@ $<

Expand Down
6 changes: 0 additions & 6 deletions sark/sark_cpp.c

This file was deleted.

18 changes: 18 additions & 0 deletions sark/sark_cpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <sark_cpp.h>

extern "C" void __cxa_pure_virtual() {
io_printf(IO_BUF, "Pure Virtual Function called");
rt_error(RTE_SWERR);
}

extern "C" void c_main(void) {
try {
cpp_main();
} catch (const std::exception &e) {
io_printf(IO_BUF, "uncaught exception: %s", e.what());
rt_error(RTE_SWERR);
} catch (...) {
io_printf(IO_BUF, "unexpected non-standard exception");
rt_error(RTE_SWERR);
}
}