Skip to content

Commit 17fa052

Browse files
committed
Define _Py_STACK_GROWS_DOWN to declare the stack direction
1 parent 5f1ed94 commit 17fa052

File tree

2 files changed

+37
-31
lines changed

2 files changed

+37
-31
lines changed

Include/internal/pycore_ceval.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ extern "C" {
1616
#include "pycore_stats.h" // EVAL_CALL_STAT_INC()
1717
#include "pycore_typedefs.h" // _PyInterpreterFrame
1818

19+
/* HP PA-RISC has a stack that goes up */
20+
#ifdef __hppa__
21+
# define _Py_STACK_GROWS_DOWN 0
22+
#else
23+
# define _Py_STACK_GROWS_DOWN 1
24+
#endif
1925

2026
/* Forward declarations */
2127
struct _ceval_runtime_state;
@@ -217,10 +223,10 @@ extern void _PyEval_DeactivateOpCache(void);
217223
static inline int _Py_MakeRecCheck(PyThreadState *tstate) {
218224
uintptr_t here_addr = _Py_get_machine_stack_pointer();
219225
_PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
220-
#ifdef __hppa__
221-
return here_addr > _tstate->c_stack_soft_limit;
222-
#else
226+
#if _Py_STACK_GROWS_DOWN
223227
return here_addr < _tstate->c_stack_soft_limit;
228+
#else
229+
return here_addr > _tstate->c_stack_soft_limit;
224230
#endif
225231
}
226232

@@ -253,10 +259,10 @@ static inline int _Py_ReachedRecursionLimit(PyThreadState *tstate) {
253259
uintptr_t here_addr = _Py_get_machine_stack_pointer();
254260
_PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
255261
assert(_tstate->c_stack_hard_limit != 0);
256-
#ifdef __hppa__
257-
return here_addr >= _tstate->c_stack_soft_limit;
258-
#else
262+
#if _Py_STACK_GROWS_DOWN
259263
return here_addr <= _tstate->c_stack_soft_limit;
264+
#else
265+
return here_addr >= _tstate->c_stack_soft_limit;
260266
#endif
261267
}
262268

Python/ceval.c

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -347,20 +347,20 @@ _Py_ReachedRecursionLimitWithMargin(PyThreadState *tstate, int margin_count)
347347
{
348348
uintptr_t here_addr = _Py_get_machine_stack_pointer();
349349
_PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
350-
#ifdef __hppa__
351-
if (here_addr <= _tstate->c_stack_soft_limit - margin_count * _PyOS_STACK_MARGIN_BYTES) {
352-
#else
350+
#if _Py_STACK_GROWS_DOWN
353351
if (here_addr > _tstate->c_stack_soft_limit + margin_count * _PyOS_STACK_MARGIN_BYTES) {
352+
#else
353+
if (here_addr <= _tstate->c_stack_soft_limit - margin_count * _PyOS_STACK_MARGIN_BYTES) {
354354
#endif
355355
return 0;
356356
}
357357
if (_tstate->c_stack_hard_limit == 0) {
358358
_Py_InitializeRecursionLimits(tstate);
359359
}
360-
#ifdef __hppa__
361-
return here_addr > _tstate->c_stack_soft_limit - margin_count * _PyOS_STACK_MARGIN_BYTES;
362-
#else
360+
#if _Py_STACK_GROWS_DOWN
363361
return here_addr <= _tstate->c_stack_soft_limit + margin_count * _PyOS_STACK_MARGIN_BYTES;
362+
#else
363+
return here_addr > _tstate->c_stack_soft_limit - margin_count * _PyOS_STACK_MARGIN_BYTES;
364364
#endif
365365
}
366366

@@ -369,10 +369,10 @@ _Py_EnterRecursiveCallUnchecked(PyThreadState *tstate)
369369
{
370370
uintptr_t here_addr = _Py_get_machine_stack_pointer();
371371
_PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
372-
#ifdef __hppa__
373-
if (here_addr > _tstate->c_stack_hard_limit) {
374-
#else
372+
#if _Py_STACK_GROWS_DOWN
375373
if (here_addr < _tstate->c_stack_hard_limit) {
374+
#else
375+
if (here_addr > _tstate->c_stack_hard_limit) {
376376
#endif
377377
Py_FatalError("Unchecked stack overflow.");
378378
}
@@ -503,21 +503,21 @@ _Py_InitializeRecursionLimits(PyThreadState *tstate)
503503
#ifdef _Py_THREAD_SANITIZER
504504
// Thread sanitizer crashes if we use more than half the stack.
505505
uintptr_t stacksize = top - base;
506-
# ifdef __hppa__
507-
top -= stacksize/2;
508-
# else
506+
# if _Py_STACK_GROWS_DOWN
509507
base += stacksize/2;
508+
# else
509+
top -= stacksize/2;
510510
# endif
511511
#endif
512512
_PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
513-
#ifdef __hppa__
514-
_tstate->c_stack_top = base;
515-
_tstate->c_stack_hard_limit = top - _PyOS_STACK_MARGIN_BYTES;
516-
_tstate->c_stack_soft_limit = top - _PyOS_STACK_MARGIN_BYTES * 2;
517-
#else
513+
#if _Py_STACK_GROWS_DOWN
518514
_tstate->c_stack_top = top;
519515
_tstate->c_stack_hard_limit = base + _PyOS_STACK_MARGIN_BYTES;
520516
_tstate->c_stack_soft_limit = base + _PyOS_STACK_MARGIN_BYTES * 2;
517+
#else
518+
_tstate->c_stack_top = base;
519+
_tstate->c_stack_hard_limit = top - _PyOS_STACK_MARGIN_BYTES;
520+
_tstate->c_stack_soft_limit = top - _PyOS_STACK_MARGIN_BYTES * 2;
521521
#endif
522522
}
523523

@@ -530,14 +530,14 @@ _Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
530530
uintptr_t here_addr = _Py_get_machine_stack_pointer();
531531
assert(_tstate->c_stack_soft_limit != 0);
532532
assert(_tstate->c_stack_hard_limit != 0);
533-
#ifdef __hppa__
534-
if (here_addr > _tstate->c_stack_hard_limit) {
535-
/* Overflowing while handling an overflow. Give up. */
536-
int kbytes_used = (int)(here_addr - _tstate->c_stack_top)/1024;
537-
#else
533+
#if _Py_STACK_GROWS_DOWN
538534
if (here_addr < _tstate->c_stack_hard_limit) {
539535
/* Overflowing while handling an overflow. Give up. */
540536
int kbytes_used = (int)(_tstate->c_stack_top - here_addr)/1024;
537+
#else
538+
if (here_addr > _tstate->c_stack_hard_limit) {
539+
/* Overflowing while handling an overflow. Give up. */
540+
int kbytes_used = (int)(here_addr - _tstate->c_stack_top)/1024;
541541
#endif
542542
char buffer[80];
543543
snprintf(buffer, 80, "Unrecoverable stack overflow (used %d kB)%s", kbytes_used, where);
@@ -547,10 +547,10 @@ _Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
547547
return 0;
548548
}
549549
else {
550-
#ifdef __hppa__
551-
int kbytes_used = (int)(here_addr - _tstate->c_stack_top)/1024;
552-
#else
550+
#if _Py_STACK_GROWS_DOWN
553551
int kbytes_used = (int)(_tstate->c_stack_top - here_addr)/1024;
552+
#else
553+
int kbytes_used = (int)(here_addr - _tstate->c_stack_top)/1024;
554554
#endif
555555
tstate->recursion_headroom++;
556556
_PyErr_Format(tstate, PyExc_RecursionError,

0 commit comments

Comments
 (0)