From 042b128f58a952b2cd04bd5b7401bd54c67a687e Mon Sep 17 00:00:00 2001
From: Antoine Pitrou <solipsis@pitrou.net>
Date: Fri, 13 Aug 2010 21:15:58 +0000
Subject: [PATCH] Issue #9203: Computed gotos are now enabled by default on
 supported compilers (which are detected by the configure script).  They can
 still be disable selectively by specifying --without-computed-gotos.

---
 Misc/NEWS      |  4 +++
 Python/ceval.c | 21 +++++++++++---
 configure      | 79 +++++++++++++++++++++++++++++++++++++++++---------
 configure.in   | 42 +++++++++++++++++++++++----
 pyconfig.h.in  |  5 +++-
 5 files changed, 126 insertions(+), 25 deletions(-)

diff --git a/Misc/NEWS b/Misc/NEWS
index d7580fc79c..28e03140df 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,10 @@ What's New in Python 3.2 Alpha 2?
 Core and Builtins
 -----------------
 
+- Issue #9203: Computed gotos are now enabled by default on supported
+  compilers (which are detected by the configure script).  They can still
+  be disable selectively by specifying --without-computed-gotos.
+
 - Issue #9425: Create PyErr_WarnFormat() function, similar to PyErr_WarnEx()
   but use PyUnicode_FromFormatV() to format the warning message.
 
diff --git a/Python/ceval.c b/Python/ceval.c
index 2d4b16a39a..c2c4e78f19 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -840,11 +840,24 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
    -fno-crossjumping).
 */
 
-#if defined(USE_COMPUTED_GOTOS) && defined(DYNAMIC_EXECUTION_PROFILE)
+#ifdef DYNAMIC_EXECUTION_PROFILE
 #undef USE_COMPUTED_GOTOS
+#define USE_COMPUTED_GOTOS 0
+#endif
+
+#ifdef HAVE_COMPUTED_GOTOS
+    #ifndef USE_COMPUTED_GOTOS
+    #define USE_COMPUTED_GOTOS 1
+    #endif
+#else
+    #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
+    #error "Computed gotos are not supported on this compiler."
+    #endif
+    #undef USE_COMPUTED_GOTOS
+    #define USE_COMPUTED_GOTOS 0
 #endif
 
-#ifdef USE_COMPUTED_GOTOS
+#if USE_COMPUTED_GOTOS
 /* Import the static jump table */
 #include "opcode_targets.h"
 
@@ -990,7 +1003,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
 
 */
 
-#if defined(DYNAMIC_EXECUTION_PROFILE) || defined(USE_COMPUTED_GOTOS)
+#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
 #define PREDICT(op)             if (0) goto PRED_##op
 #define PREDICTED(op)           PRED_##op:
 #define PREDICTED_WITH_ARG(op)  PRED_##op:
@@ -2838,7 +2851,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
             oparg = oparg<<16 | NEXTARG();
             goto dispatch_opcode;
 
-#ifdef USE_COMPUTED_GOTOS
+#if USE_COMPUTED_GOTOS
         _unknown_opcode:
 #endif
         default:
diff --git a/configure b/configure
index d48e8dcb64..9c9b974762 100755
--- a/configure
+++ b/configure
@@ -1,5 +1,5 @@
 #! /bin/sh
-# From configure.in Revision: 82090 .
+# From configure.in Revision: 83580 .
 # Guess values for system-dependent variables and create Makefiles.
 # Generated by GNU Autoconf 2.65 for python 3.2.
 #
@@ -1421,8 +1421,9 @@ Optional Packages:
   --with-libm=STRING      math library
   --with-libc=STRING      C library
   --with-wide-unicode     Use 4-byte Unicode characters (default is 2 bytes)
-  --with-computed-gotos   Use computed gotos / threaded dispatch in evaluation
-                          loop (not available on all compilers)
+  --with(out)-computed-gotos
+                          Use computed gotos in evaluation loop (enabled by
+                          default on supported compilers)
 
 Some influential environment variables:
   CC          C compiler command
@@ -1929,11 +1930,11 @@ else
        cat confdefs.h - <<_ACEOF >conftest.$ac_ext
 /* end confdefs.h.  */
 $ac_includes_default
-	     enum { N = $2 / 2 - 1 };
 int
 main ()
 {
-static int test_array [1 - 2 * !(0 < ($ac_type) ((((($ac_type) 1 << N) << N) - 1) * 2 + 1))];
+static int test_array [1 - 2 * !(enum { N = $2 / 2 - 1 };
+	     0 < ($ac_type) ((((($ac_type) 1 << N) << N) - 1) * 2 + 1))];
 test_array [0] = 0
 
   ;
@@ -1944,11 +1945,11 @@ if ac_fn_c_try_compile "$LINENO"; then :
   cat confdefs.h - <<_ACEOF >conftest.$ac_ext
 /* end confdefs.h.  */
 $ac_includes_default
-	        enum { N = $2 / 2 - 1 };
 int
 main ()
 {
-static int test_array [1 - 2 * !(($ac_type) ((((($ac_type) 1 << N) << N) - 1) * 2 + 1)
+static int test_array [1 - 2 * !(enum { N = $2 / 2 - 1 };
+		($ac_type) ((((($ac_type) 1 << N) << N) - 1) * 2 + 1)
 		 < ($ac_type) ((((($ac_type) 1 << N) << N) - 1) * 2 + 2))];
 test_array [0] = 0
 
@@ -9121,7 +9122,7 @@ fi
 { $as_echo "$as_me:${as_lineno-$LINENO}: result: $with_doc_strings" >&5
 $as_echo "$with_doc_strings" >&6; }
 
-# Check for Python-specific malloc support
+# Check if eval loop should use timestamp counter profiling
 { $as_echo "$as_me:${as_lineno-$LINENO}: checking for --with-tsc" >&5
 $as_echo_n "checking for --with-tsc... " >&6; }
 
@@ -13502,6 +13503,49 @@ $as_echo "#define HAVE_BROKEN_MBSTOWCS 1" >>confdefs.h
 
 fi
 
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether $CC supports computed gotos" >&5
+$as_echo_n "checking whether $CC supports computed gotos... " >&6; }
+if test "${ac_cv_computed_gotos+set}" = set; then :
+  $as_echo_n "(cached) " >&6
+else
+  if test "$cross_compiling" = yes; then :
+  ac_cv_computed_gotos=no
+else
+  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+int main(int argc, char **argv)
+{
+    static void *targets[1] = { &&LABEL1 };
+    goto LABEL2;
+LABEL1:
+    return 0;
+LABEL2:
+    goto *targets[0];
+    return 1;
+}
+
+_ACEOF
+if ac_fn_c_try_run "$LINENO"; then :
+  ac_cv_computed_gotos=yes
+else
+  ac_cv_computed_gotos=no
+fi
+rm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext \
+  conftest.$ac_objext conftest.beam conftest.$ac_ext
+fi
+
+fi
+
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_computed_gotos" >&5
+$as_echo "$ac_cv_computed_gotos" >&6; }
+if test "$ac_cv_computed_gotos" = yes
+then
+
+$as_echo "#define HAVE_COMPUTED_GOTOS 1" >>confdefs.h
+
+fi
+
 # Check for --with-computed-gotos
 { $as_echo "$as_me:${as_lineno-$LINENO}: checking for --with-computed-gotos" >&5
 $as_echo_n "checking for --with-computed-gotos... " >&6; }
@@ -13509,21 +13553,28 @@ $as_echo_n "checking for --with-computed-gotos... " >&6; }
 # Check whether --with-computed-gotos was given.
 if test "${with_computed_gotos+set}" = set; then :
   withval=$with_computed_gotos;
-if test "$withval" != no
+if test "$withval" = yes
 then
 
 $as_echo "#define USE_COMPUTED_GOTOS 1" >>confdefs.h
 
   { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
 $as_echo "yes" >&6; }
-else { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
-$as_echo "no" >&6; }
 fi
-else
+if test "$withval" = no
+then
+
+$as_echo "#define USE_COMPUTED_GOTOS 0" >>confdefs.h
+
   { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
 $as_echo "no" >&6; }
 fi
 
+else
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: no value specified" >&5
+$as_echo "no value specified" >&6; }
+fi
+
 
 
 
@@ -14086,8 +14137,8 @@ esac
 
 cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
 # Files that config.status was made for.
-config_files="$ac_config_files"
-config_headers="$ac_config_headers"
+config_files="`echo $ac_config_files`"
+config_headers="`echo $ac_config_headers`"
 
 _ACEOF
 
diff --git a/configure.in b/configure.in
index 7d10e69e50..eb753bb93f 100644
--- a/configure.in
+++ b/configure.in
@@ -4124,20 +4124,50 @@ then
    wide chars that would be converted.])
 fi
 
+AC_MSG_CHECKING(whether $CC supports computed gotos)
+AC_CACHE_VAL(ac_cv_computed_gotos,
+AC_RUN_IFELSE([AC_LANG_SOURCE([[[
+int main(int argc, char **argv)
+{
+    static void *targets[1] = { &&LABEL1 };
+    goto LABEL2;
+LABEL1:
+    return 0;
+LABEL2:
+    goto *targets[0];
+    return 1;
+}
+]]])],
+[ac_cv_computed_gotos=yes],
+[ac_cv_computed_gotos=no],
+[ac_cv_computed_gotos=no]))
+AC_MSG_RESULT($ac_cv_computed_gotos)
+if test "$ac_cv_computed_gotos" = yes
+then
+  AC_DEFINE(HAVE_COMPUTED_GOTOS, 1,
+  [Define if the C compiler supports computed gotos.])
+fi
+
 # Check for --with-computed-gotos
 AC_MSG_CHECKING(for --with-computed-gotos)
 AC_ARG_WITH(computed-gotos,
-            AS_HELP_STRING([--with-computed-gotos],
-                           [Use computed gotos / threaded dispatch in evaluation loop (not available on all compilers)]),
+            AS_HELP_STRING([--with(out)-computed-gotos],
+                           [Use computed gotos in evaluation loop (enabled by default on supported compilers)]),
 [
-if test "$withval" != no
+if test "$withval" = yes
 then 
   AC_DEFINE(USE_COMPUTED_GOTOS, 1,
   [Define if you want to use computed gotos in ceval.c.]) 
   AC_MSG_RESULT(yes)
-else AC_MSG_RESULT(no)
-fi],
-[AC_MSG_RESULT(no)])
+fi
+if test "$withval" = no
+then 
+  AC_DEFINE(USE_COMPUTED_GOTOS, 0,
+  [Define if you want to use computed gotos in ceval.c.]) 
+  AC_MSG_RESULT(no)
+fi
+],
+[AC_MSG_RESULT(no value specified)])
 
 
 
diff --git a/pyconfig.h.in b/pyconfig.h.in
index d3a87861e1..ae3f6e4e49 100644
--- a/pyconfig.h.in
+++ b/pyconfig.h.in
@@ -104,6 +104,9 @@
 /* Define to 1 if you have the `clock' function. */
 #undef HAVE_CLOCK
 
+/* Define if the C compiler supports computed gotos. */
+#undef HAVE_COMPUTED_GOTOS
+
 /* Define to 1 if you have the `confstr' function. */
 #undef HAVE_CONFSTR
 
@@ -1037,7 +1040,7 @@
 /* Define to 1 if your <sys/time.h> declares `struct tm'. */
 #undef TM_IN_SYS_TIME
 
-/* Define if you want to use computed gotos in ceval.c. */
+/* Define to 0 if you don't want to use computed gotos in ceval.c. */
 #undef USE_COMPUTED_GOTOS
 
 /* Enable extensions on AIX 3, Interix.  */
-- 
2.40.0