From 45821dee079c78367bb25b7ac8619f34d0183daa Mon Sep 17 00:00:00 2001 From: Keir Fraser Date: Sun, 3 Nov 2024 10:26:15 +0000 Subject: [PATCH] New intrinsic "unreachable()" to indicate unreachable code points This allows better compiler optimisation, while also including an ASSERT() for debug builds. Refs #7 --- inc/intrinsics.h | 2 ++ src/board.c | 3 +-- src/usb/hw_dwc_otg.c | 5 ++--- src/usb/hw_f7.c | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/inc/intrinsics.h b/inc/intrinsics.h index 93519b4..e27cc9d 100644 --- a/inc/intrinsics.h +++ b/inc/intrinsics.h @@ -27,6 +27,8 @@ struct exception_frame { #define likely(x) __builtin_expect(!!(x),1) #define unlikely(x) __builtin_expect(!!(x),0) +#define unreachable() do { ASSERT(0); __builtin_unreachable(); } while (0) + #define illegal() asm volatile (".short 0xde00"); #define barrier() asm volatile ("" ::: "memory") diff --git a/src/board.c b/src/board.c index 9a88f43..fa4ce61 100644 --- a/src/board.c +++ b/src/board.c @@ -26,8 +26,7 @@ GPIO gpio_from_id(uint8_t id) case _I: return gpioi; #endif } - ASSERT(0); - return NULL; + unreachable(); } uint8_t write_mapped_pin( diff --git a/src/usb/hw_dwc_otg.c b/src/usb/hw_dwc_otg.c index 91e3afe..3d316f3 100644 --- a/src/usb/hw_dwc_otg.c +++ b/src/usb/hw_dwc_otg.c @@ -410,9 +410,8 @@ static void handle_iepint(uint8_t epnr) handle_tx_ep0(); } - if (iepint & OTG_DIEPINT_TXFE) { - ASSERT(0); - } + /* We don't set DIEPEMPMSK bits so TXFE notification is impossible. */ + ASSERT(!(iepint & OTG_DIEPINT_TXFE)); } static void dwc_otg_process(void) diff --git a/src/usb/hw_f7.c b/src/usb/hw_f7.c index b1c2ef6..642184d 100644 --- a/src/usb/hw_f7.c +++ b/src/usb/hw_f7.c @@ -65,7 +65,7 @@ void hw_usb_init(void) } break; default: - ASSERT(0); + unreachable(); } peripheral_clock_delay(); @@ -109,7 +109,7 @@ void hw_usb_deinit(void) rcc->ahb1enr &= ~RCC_AHB1ENR_OTGHSEN; break; default: - ASSERT(0); + unreachable(); } }