<!doctype html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<div>
Thanks Charles, and everyone.
</div>
<div class="default-style">
<div class="default-style">
</div>
<div class="default-style">
I was only checking that the compiler (x86) generated re-entrant code before I climbed in too far.
</div>
<div class="default-style">
</div>
<div class="default-style">
The issue turned out to be a bug with pointer management.
</div>
<div class="default-style">
</div>
<div class="default-style">
-Mark
</div>
</div>
<div class="default-style">
</div>
<blockquote type="cite">
<div>
On 03/04/2024 09:16 NZDT Charles Manning <cdhmanning@gmail.com> wrote:
</div>
<div>
</div>
<div>
</div>
<div dir="ltr">
<div>
Recursion can always be mapped to looping and using a manual stack. Some languages, such as Fortran, lack recursion and this is how recursive constructs are achieved.
</div>
<div>
</div>
<div>
But back to the original question: gcc certainly handles recursion properly on ARM, x86 and other "real" CPUs.
</div>
<div>
</div>
<div>
But is this gcc generating code for a tiny hobby CPU? If so, it might not be using a proper stack which would then prevent recursion.
</div>
<div>
</div>
<div>
For example on some 8-bitters like 8051, C compilers and stacks do not map well so, instead, the code that is generated ends up using fixed memory locations for a fixed "stack". Such constructs do not support recursion.
</div>
<div>
</div>
<div>
I would suggest trying out making a small recursive structure such as factorial and have a look at the generated code.
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<div>
</div>
</div>
<br>
<div class="gmail_quote">
<div class="gmail_attr" dir="ltr">
On Wed, Apr 3, 2024 at 1:30 AM Bevin Brett <<a href="mailto:bevin_brett@hotmail.com">bevin_brett@hotmail.com</a>> wrote:
</div>
<blockquote>
<div class="msg-5866579317276282214">
<div dir="ltr">
<div style="font-family: Calibri,Helvetica,sans-serif; font-size: 12pt; color: #000000;">
The alternative to recursion is either a linked list, a fixed size array, or a growing array
</div>
<div style="font-family: Calibri,Helvetica,sans-serif; font-size: 12pt; color: #000000;">
</div>
<div style="font-family: Calibri,Helvetica,sans-serif; font-size: 12pt; color: #000000;">
In RPN evaluation, the code often looks like this, because you don't need the full power and cost of recursion
</div>
<div style="font-family: Calibri,Helvetica,sans-serif; font-size: 12pt; color: #000000;">
</div>
<div style="font-family: Calibri,Helvetica,sans-serif; font-size: 12pt; color: #000000;">
ops : array of operators
</div>
<div style="font-family: Calibri,Helvetica,sans-serif; font-size: 12pt; color: #000000;">
stack : stack of operands
</div>
<div style="font-family: Calibri,Helvetica,sans-serif; font-size: 12pt; color: #000000;">
top = 0
</div>
<div style="font-family: Calibri,Helvetica,sans-serif; font-size: 12pt; color: #000000;">
for op in instructionStream do
</div>
<div style="font-family: Calibri,Helvetica,sans-serif; font-size: 12pt; color: #000000;">
caseOf nOperands(op.code)
</div>
<div style="font-family: Calibri,Helvetica,sans-serif; font-size: 12pt; color: #000000;">
alt add:
</div>
<div style="font-family: Calibri,Helvetica,sans-serif; font-size: 12pt; color: #000000;">
stack[top - 1] += stack[top]
</div>
<div style="font-family: Calibri,Helvetica,sans-serif; font-size: 12pt; color: #000000;">
top = top - 1
</div>
<div style="font-family: Calibri,Helvetica,sans-serif; font-size: 12pt; color: #000000;">
alt int:
</div>
<div style="font-family: Calibri,Helvetica,sans-serif; font-size: 12pt; color: #000000;">
top = top+1
</div>
<div style="font-family: Calibri,Helvetica,sans-serif; font-size: 12pt; color: #000000;">
stack[top] = <a href="http://op.int" target="_blank" rel="noopener">op.int</a>
</div>
<div style="font-family: Calibri,Helvetica,sans-serif; font-size: 12pt; color: #000000;">
...
</div>
<div style="font-family: Calibri,Helvetica,sans-serif; font-size: 12pt; color: #000000;">
</div>
<div style="font-family: Calibri,Helvetica,sans-serif; font-size: 12pt; color: #000000;">
Don't forget to check for malformed expressions, which could result in h/w stack corruption due to indexing outside 'stack'
</div>
<div style="font-family: Calibri,Helvetica,sans-serif; font-size: 12pt; color: #000000;">
</div>
<div style="font-family: Calibri,Helvetica,sans-serif; font-size: 12pt; color: #000000;">
/Bevin
</div>
<div id="m_-5866579317276282214appendonsend">
</div>
<div style="font-family: Calibri,Helvetica,sans-serif; font-size: 12pt; color: #000000;">
</div>
<hr style="display: inline-block; width: 98%;">
<div id="m_-5866579317276282214divRplyFwdMsg" dir="ltr">
<div style="font-family: Calibri,sans-serif; font-size: 11pt; color: #000000;">
</div>
<div style="font-family: Calibri,sans-serif; font-size: 11pt; color: #000000;">
...
<br>
<br>I am building an RPN expression evaluator for the uCPU assembler; I
<br>can't see any other way to go other than to use recursion to do the
<br>nested processing that I wish.
<br>
<br>Currently using "gcc version 12.2.0 (MinGW-W64 x86_64-ucrt-posix-seh,
<br>built by Brecht Sanders)"
<br>
<br>The first call of the recursed function worked fine, but crashes upon exit.
<br>
<br>It could be that the compiler is not generating re-entrant code. The
<br>only switch I have used is -O0 to disable compiler optimization.
<br>
<br>Are there any other switches for GCC that I need to use/be aware of ?
<br>
<br>Thanks, Mark
<br>
<br>...
</div>
</div>
</div> _______________________________________________
<br>Chchrobotics mailing list <a href="mailto:Chchrobotics@lists.ourshack.com" target="_blank" rel="noopener">Chchrobotics@lists.ourshack.com</a>
<br><a href="https://lists.ourshack.com/mailman/listinfo/chchrobotics" target="_blank" rel="noopener">https://lists.ourshack.com/mailman/listinfo/chchrobotics</a>
<br>Mail Archives: <a href="http://lists.ourshack.com/pipermail/chchrobotics/" target="_blank" rel="noopener">http://lists.ourshack.com/pipermail/chchrobotics/</a>
<br>Meetings usually 3rd Monday each month. See <a href="http://kiwibots.org" target="_blank" rel="noopener">http://kiwibots.org</a> for venue, directions and dates.
<br>When replying, please edit your Subject line to reflect new subjects.
</div>
</blockquote>
</div> _______________________________________________
<br>Chchrobotics mailing list Chchrobotics@lists.ourshack.com
<br>https://lists.ourshack.com/mailman/listinfo/chchrobotics
<br>Mail Archives: http://lists.ourshack.com/pipermail/chchrobotics/
<br>Meetings usually 3rd Monday each month. See http://kiwibots.org for venue, directions and dates.
<br>When replying, please edit your Subject line to reflect new subjects.
</blockquote>
</body>
</html>