[fpc-pascal] Candidate for crowd-funding
Karoly Balogh (Charlie/SGR)
charlie at scenergy.dfmk.hu
Wed Dec 19 19:17:55 CET 2018
On Wed, 19 Dec 2018, Ryan Joseph wrote:
> > (After reading that old thread we had...)
> > Actually, it can. The only thing you need to do is to allocate a stack
> > first, then make the "coroutine" functions use it, which is basically
> > boils down to how you pass your arguments and self instance to the
> > coroutine on initializatin, while you change the stack pointer register,
> > and then how you restore it in the end.
> And how is a stack allocated? I assume we’re talking assembly here? The
> link I posted (http://www.festra.com/wwwboard/messages/12899.html) seems
> to do this but I don’t know assembly so I’m just guessing.
No, that assembly function just does swapping of the stack and storing the
caller's stack settings and setting up the coroutine's stack instead. The
actual stack is allocated with the Win32 API function VirtualAlloc(), the
rest is just structure copy to/from the coroutine function and setting
up/restoring the stack. The stack is just a memory area, where the
stackpointer points (plus on some archs it has to respect certain
alignment requirements, but that's easy enough to deal with), and until
your stackpointer register points to that memory area, the rest just
works, due to how the CPU works, the compiled code just uses it... The
compiler doesn't handle it normally on the "allocation" level, as usually
the OS takes care of it.
> Even if you allocated a stack frame doesn’t it need to be populated with
> all local variables in the current scope? In my short time working on
> the compiler I haven’t looked at the actual code generator so I don’t
> know how the stack works. However, in theory you should be able to make
> a compiler intrinsic that wraps this process for you.
Well, this is quite a complex code, so most likely would end up as a
helper in the RTL anyway...
More information about the fpc-pascal