Recent

Author Topic: alloca  (Read 4710 times)

Red_prig

  • Full Member
  • ***
  • Posts: 153
alloca
« on: March 01, 2023, 08:13:32 am »
Suddenly, the stack allocator I wrote, is it normal or not?

Code: Pascal  [Select][+][-]
  1. function alloca(size:qword):Pointer; sysv_abi_default; assembler; nostackframe;
  2. asm
  3.  movqq       %rsp,%rax
  4.  subq        %rdi,%rax
  5.  lea     -8(%rax),%rax
  6.  andq        $-32,%rax
  7.  movqq     (%rsp),%rdi
  8.  movqq       %rax,%rsp
  9.  lea    -32(%rsp),%rsp
  10.  jmp    %rdi
  11. end;  
  12.  

Bart

  • Hero Member
  • *****
  • Posts: 5564
    • Bart en Mariska's Webstek
Re: alloca
« Reply #1 on: March 01, 2023, 11:33:04 am »
Well, that depends on your definition of "normal".

Bart

Zvoni

  • Hero Member
  • *****
  • Posts: 2966
Re: alloca
« Reply #2 on: March 01, 2023, 11:39:57 am »
Well, that depends on your definition of "normal".

Bart
"Normal" is way overrated, IMO.... :P :P
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Bart

  • Hero Member
  • *****
  • Posts: 5564
    • Bart en Mariska's Webstek
Re: alloca
« Reply #3 on: March 01, 2023, 12:44:32 pm »
"Normal" is way overrated, IMO.... :P :P
I hope so, because I'm not.

Bart

Red_prig

  • Full Member
  • ***
  • Posts: 153
Re: alloca
« Reply #4 on: March 01, 2023, 01:06:35 pm »
Normal in the sense that it seems to work as intended .... I posted this only because I could not find anything serious on this topic for Pascal

Warfley

  • Hero Member
  • *****
  • Posts: 1872
Re: alloca
« Reply #5 on: March 01, 2023, 02:59:07 pm »
Yeah alloca is something that is missing from freepascal.
The problem with custom written assembly is that it of course is highly target CPU dependent, so a general compiler intrinsic or something similar would be much nicer

Thaddy

  • Hero Member
  • *****
  • Posts: 16945
  • Ceterum censeo Trump esse delendam
Re: alloca
« Reply #6 on: March 01, 2023, 03:17:13 pm »
I also have the suspicion that the parameter must be a multiple of 8.... As is the code does not work.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Red_prig

  • Full Member
  • ***
  • Posts: 153
Re: alloca
« Reply #7 on: March 01, 2023, 04:34:39 pm »
I didn’t understand you about the requirement to align by 8 bytes ... given that in the code both the size of the allocated data and the final stack address are aligned by 32 bytes (this is even unnecessary, but I had to)

Thaddy

  • Hero Member
  • *****
  • Posts: 16945
  • Ceterum censeo Trump esse delendam
Re: alloca
« Reply #8 on: March 01, 2023, 07:49:28 pm »
Yeah alloca is something that is missing from freepascal.
The problem with custom written assembly is that it of course is highly target CPU dependent, so a general compiler intrinsic or something similar would be much nicer
I have alloca for win64. I use it in my stack allocated classes. Code is on this forum.
I am a bit amazed that on linux64  __builtin_alloca does not work, at least I can't get it to work. It should work on any linux.


BTW the function by OP does not work at all.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Red_prig

  • Full Member
  • ***
  • Posts: 153
Re: alloca
« Reply #9 on: March 01, 2023, 09:36:02 pm »
Hmm, it probably only works in simple functions (may be calling convention depened?)... but that's enough for me, for variable-sized local data inside a function.

Red_prig

  • Full Member
  • ***
  • Posts: 153
Re: alloca
« Reply #10 on: March 01, 2023, 09:38:44 pm »
If I'm not mistaken, then __builtin_alloca works in a similar way, only for use in the current function, or in child functions called by the same function

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: alloca
« Reply #11 on: March 01, 2023, 10:01:35 pm »
But your alloca is Linux-only since it passes parameter in rdi, isn't it?
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Red_prig

  • Full Member
  • ***
  • Posts: 153
Re: alloca
« Reply #12 on: March 01, 2023, 10:14:49 pm »
I've specified a sysv_abi_default directive which makes it use parameter passing like in lunix, even under windows. Actually, I wrote and used it only under Windows  :P

PascalDragon

  • Hero Member
  • *****
  • Posts: 5968
  • Compiler Developer
Re: alloca
« Reply #13 on: March 01, 2023, 10:43:13 pm »
I am a bit amazed that on linux64  __builtin_alloca does not work, at least I can't get it to work. It should work on any linux.

__builtin_alloca is provided by GCC or Clang and not the operating system.

Thaddy

  • Hero Member
  • *****
  • Posts: 16945
  • Ceterum censeo Trump esse delendam
Re: alloca
« Reply #14 on: March 02, 2023, 07:48:49 am »
Yes, I know, I rephrase it: Why is it is not accessible through {$linklib c}  on my system? (Debian 64 under wsl1 and Debian 64 on  wsl2)
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

 

TinyPortal © 2005-2018