Date: Mon, 14 Aug 2023 12:44:51 +0100
To my knowledge, there has never been a C++ compiler for a processor
that doesn't have a stack.
You can find some C compilers for some very small microcontrollers
that don't have a stack, but not C++.
So how about we have a function to get the stack pointer, which would
look something like:
__asm__ (
".text\n"
"stack_ptr:\n"
#if defined(__i386__) // x86 (32-bit)
"mo4v %esp, %eax\n"
"ret"
#elif defined(__x86_64__) // x86_64 (64-bit)
"mov %rsp, %rax\n"
"ret"
#elif defined(__aarch64__) || defined(__aarch64__) // AArch64 (ARM 64-bit)
"mov x0, sp\n"
"ret"
#elif defined(__arm__) // ARM (32-bit)
"mov r0, sp\n"
"bx lr"
#elif defined(__mips__) // MIPS
"move $v0, $sp\n"
"jr $ra"
#endif
);
And also how about we standardise the function "alloca" which allows
us to allocate bytes on the stack? For those not familiar with
'alloca', here's the Linux manual entry:
https://man7.org/linux/man-pages/man3/alloca.3.html
And here's a quick excerpt from that manual page
The alloca() function allocates size bytes of space in the stack
frame of the caller. This temporary space is automatically freed
when the function that called alloca() returns to its caller.
Because it allocates from the stack, it's faster than malloc
and free. In certain cases, it can also simplify memory
deallocation in applications that use longjmp or siglongjmp.
that doesn't have a stack.
You can find some C compilers for some very small microcontrollers
that don't have a stack, but not C++.
So how about we have a function to get the stack pointer, which would
look something like:
__asm__ (
".text\n"
"stack_ptr:\n"
#if defined(__i386__) // x86 (32-bit)
"mo4v %esp, %eax\n"
"ret"
#elif defined(__x86_64__) // x86_64 (64-bit)
"mov %rsp, %rax\n"
"ret"
#elif defined(__aarch64__) || defined(__aarch64__) // AArch64 (ARM 64-bit)
"mov x0, sp\n"
"ret"
#elif defined(__arm__) // ARM (32-bit)
"mov r0, sp\n"
"bx lr"
#elif defined(__mips__) // MIPS
"move $v0, $sp\n"
"jr $ra"
#endif
);
And also how about we standardise the function "alloca" which allows
us to allocate bytes on the stack? For those not familiar with
'alloca', here's the Linux manual entry:
https://man7.org/linux/man-pages/man3/alloca.3.html
And here's a quick excerpt from that manual page
The alloca() function allocates size bytes of space in the stack
frame of the caller. This temporary space is automatically freed
when the function that called alloca() returns to its caller.
Because it allocates from the stack, it's faster than malloc
and free. In certain cases, it can also simplify memory
deallocation in applications that use longjmp or siglongjmp.
Received on 2023-08-14 11:45:04