<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><br></div><div>But back to the original question: gcc certainly handles recursion properly on ARM, x86 and other "real" CPUs.</div><div><br></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><br></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><br></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><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Apr 3, 2024 at 1:30 AM Bevin Brett <<a href="mailto:bevin_brett@hotmail.com">bevin_brett@hotmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="msg-5866579317276282214">
<div dir="ltr">
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
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:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
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:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
ops : array of operators</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
stack : stack of operands</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
top = 0</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
for op in instructionStream do</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
caseOf nOperands(op.code)</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
alt add:</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
stack[top - 1] += stack[top]</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
top = top - 1</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
alt int:</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
top = top+1</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
stack[top] = <a href="http://op.int" target="_blank">op.int</a></div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
...</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
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:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
/Bevin</div>
<div id="m_-5866579317276282214appendonsend"></div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<br>
</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:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,sans-serif;font-size:11pt;color:rgb(0,0,0)">
...<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">Chchrobotics@lists.ourshack.com</a><br>
<a href="https://lists.ourshack.com/mailman/listinfo/chchrobotics" rel="noreferrer" target="_blank">https://lists.ourshack.com/mailman/listinfo/chchrobotics</a><br>
Mail Archives: <a href="http://lists.ourshack.com/pipermail/chchrobotics/" rel="noreferrer" target="_blank">http://lists.ourshack.com/pipermail/chchrobotics/</a><br>
Meetings usually 3rd Monday each month. See <a href="http://kiwibots.org" rel="noreferrer" target="_blank">http://kiwibots.org</a> for venue, directions and dates.<br>
When replying, please edit your Subject line to reflect new subjects.</div></blockquote></div>