Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell if computed gotos are supported?

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.

like image 652
munificent Avatar asked Sep 20 '25 18:09

munificent


1 Answers

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.

like image 91
Some programmer dude Avatar answered Sep 22 '25 09:09

Some programmer dude