1# stack-direction.m4 serial 7
2dnl Copyright (C) 2002-2021 Free Software Foundation, Inc.
3dnl This file is free software, distributed under the terms of the GNU
4dnl General Public License.  As a special exception to the GNU General
5dnl Public License, this file may be distributed as part of a program
6dnl that contains a configuration script generated by Autoconf, under
7dnl the same distribution terms as the rest of that program.
8
9dnl Written by Bruno Haible.
10
11# Determine the stack direction. Define the C macro STACK_DIRECTION.
12AC_DEFUN([SV_STACK_DIRECTION],
13[
14  AC_REQUIRE([AC_CANONICAL_HOST])
15  AC_CACHE_CHECK([for stack direction], [sv_cv_stack_direction_msg], [
16    case "$host_cpu" in
17      dnl See the #define STACK_GROWS_DOWNWARD in gcc-3.1/gcc/config/*/*.h.
18      a29k | \
19      aarch64* | \
20      alpha* | \
21      arc | \
22      arm* | strongarm* | xscale* | \
23      avr | avr32 | \
24      bfin | \
25      c1 | c2 | c32 | c34 | c38 | \
26      clipper | \
27      cris | \
28      d30v | \
29      elxsi | \
30      fr30 | \
31      h8300 | \
32      i?86 | x86_64 | \
33      i860 | \
34      ia64 | \
35      m32r | \
36      m68* | \
37      m88k | \
38      mcore | \
39      microblaze | \
40      mips* | \
41      mmix | \
42      mn10200 | \
43      mn10300 | \
44      nios2 | \
45      nds32* | \
46      ns32k | \
47      pdp11 | \
48      pj* | \
49      powerpc* | rs6000 | \
50      riscv* | \
51      romp | \
52      s390* | \
53      sh* | \
54      sparc* | \
55      v850 | \
56      vax | \
57      xtensa)
58        sv_cv_stack_direction=-1 ;;
59      c4x | \
60      dsp16xx | \
61      i960 | \
62      hppa* | parisc* | \
63      stormy16 | \
64      we32k)
65        sv_cv_stack_direction=1 ;;
66      *)
67        if test $cross_compiling = no; then
68          cat > conftest.c <<EOF
69#include <stdio.h>
70int
71find_stack_direction (int *addr, int depth)
72{
73  int dir, dummy = 0;
74  if (! addr)
75    addr = &dummy;
76  *addr = addr < &dummy ? 1 : addr == &dummy ? 0 : -1;
77  dir = depth ? find_stack_direction (addr, depth - 1) : 0;
78  return dir + dummy;
79}
80int
81main (int argc, char *argv[])
82{
83  printf ("%d\n", find_stack_direction (NULL, argc + 20));
84  return 0;
85}
86EOF
87          AC_TRY_EVAL([ac_link])
88          sv_cv_stack_direction=`./conftest`
89        else
90          sv_cv_stack_direction=0
91        fi
92        ;;
93    esac
94    case $sv_cv_stack_direction in
95      1)  sv_cv_stack_direction_msg="grows up";;
96      -1) sv_cv_stack_direction_msg="grows down";;
97      *)  sv_cv_stack_direction_msg="unknown";;
98    esac
99  ])
100  AC_DEFINE_UNQUOTED([STACK_DIRECTION], [$sv_cv_stack_direction],
101    [Define as the direction of stack growth for your system.
102     STACK_DIRECTION > 0 => grows toward higher addresses
103     STACK_DIRECTION < 0 => grows toward lower addresses
104     STACK_DIRECTION = 0 => spaghetti stack.])
105])
106