I'm writing a bytecode interpreter that can either use computed gotos or a normal switch
for the main instruction dispatching loop. The key bits are wrapped up in a couple of macros that can either be defined to use computed gotos or not.
I'd like to decide which mode to use by default based on whether or not the compiler supports computed gotos. Does anyone know how to determine that? As far as I can tell, they work on GCC and Clang, but I don't want to just hardcode a couple of random compiler names.
If you're using a tool such as autoconf, the following feature test have been useful for me:
AC_MSG_CHECKING([if ${CC-gcc} supports computed gotos])
AC_COMPILE_IFELSE(
[AC_LANG_PROGRAM(
[],
[[
void *my_label_ptr = &&my_label; /* GCC syntax */
goto *my_label_ptr;
return 1;
my_label:
return 0;
]])],
[AC_MSG_RESULT(yes)
AC_DEFINE(HAVE_COMPUTED_GOTOS, 1,
[Define to 1 if the compiler supports computed gotos])],
[AC_MSG_RESULT(no)])
It will define the macro HAVE_COMPUTED_GOTOS
if the compiler supports the GCC syntax.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With