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

Add platform dependent assert macro #158

Open
TheSlowGrowth opened this issue May 12, 2021 · 3 comments
Open

Add platform dependent assert macro #158

TheSlowGrowth opened this issue May 12, 2021 · 3 comments

Comments

@TheSlowGrowth
Copy link
Contributor

TheSlowGrowth commented May 12, 2021

It would be great to have a platform-independent way for assertions (e.g. bkpt 255 on ARM, assert(), etc.).

  1. Create Source/platform.h
  2. Add macro D_SP_ASSERTFALSE that triggers an assertion. The JUCE macro jassertfalse could serve as a template and could be extended for bkpt 255 when compiling for embedded targets.
  3. Add macro D_SP_ASSERT(bool condition) that triggers D_SP_ASSERTFALSE if !condition
  4. Replace all existing assertions with these macros

The same file could also be very useful in libDaisy.

@polyclash
Copy link

JUCE dependancy need to be optional, not an default bet.

@TheSlowGrowth
Copy link
Contributor Author

Im not suggesting to add a dependency on JUCE. Rather I'd use the jassert macro from JUCE as an inspiration for our own implementation. JUCE has a lot of the platforms and compilers covered so it would help to look at their implementation.

@dylan-robins
Copy link
Contributor

dylan-robins commented May 12, 2021

Another possibility may be to define the behaviour we want on embedded platforms, and in the case that DaisySP is being used in the context of a JUCE project we can fall back on their implementations. So something like :

// Define macro D_SP_ASSERTFALSE which triggers an assertion failure using the
// appropriate method for the current platform
#if defined(__arm__)
// On embedded platforms use the bkpt opcode
#define D_SP_ASSERTFALSE(expr) asm("bkpt 255")
#elif defined(JUCE_VERSION)
// In a JUCE project use the JUCE macros
#define D_SP_ASSERTFALSE(expr) jassertfalse
#else
// Otherwise, raise a default assertion failure
#include <assert.h>
#define D_SP_ASSERTFALSE(expr) assert(expr)
#endif

// If expression is false, trigger an assertion failure using the appropriate
// method for the current platform
#define D_SP_ASSERT(expression)            \
    do {                                   \
        if (!expression)                   \
            D_SP_ASSERTFALSE(#expression); \
    } while (0)

I've thrown together a full implementation based on this on my fork of the project. It compiles, but unfortunately I don't really have a way to test it properly because I'm still trying to figure this stuff out (and I don't have a debug probe yet). Help and feedback is very much welcome.

One other point to mention: in order to support this I had to bump the C++ standard up to gnu++20. Inline assembly inside a constexpr seems to have only been added in C++20, and in Source/Utility/dsp.h there's an assert in a constexpr so that now raises a compiler error when using my proposed generic assertion macros.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants