Recent

Author Topic: assembler routines and stack frame generation  (Read 4766 times)

Marladu

  • New Member
  • *
  • Posts: 19
assembler routines and stack frame generation
« on: January 21, 2015, 05:02:37 am »
Well, I had typed up this as a report but remembered being asked to write in forums before filing reports so here's what I was going to report.
---------------
assembler routines won't respect {$OPTIMIZATION STACKFRAME} and {$STACKFRAMES OFF} but respects nostackframe function modifier
---------------

 In the following code with {$OPTIMIZATION STACKFRAME} and {$STACKFRAMES OFF}, if I leave commented out the "nostackframe;" function modifer the following code is generated:

push   %ebp
mov    %esp,%ebp
lea    -0x4(%esp),%esp
add    %edx,%eax
leave 
ret   

 However, if I uncomment the "nostackframe;" function modifer the following code is generated:

add    %edx,%eax
ret   

 From the documentation of {$STACKFRAMES OFF} it says it won't turn off stackframes for assembler routines. Alright then so that's why it doesn't work in this case. But the documentation of {$OPTIMIZATION STACKFRAME} does not mention what rules it follows.

 In the end if {$OPTIMIZATION STACKFRAME} is completely identical to {$STACKFRAMES OFF} then it should be mentioned in the documentation, else if it's supposed to apply the "nostackframe;" to every function then it's not doing it to assembler routines.

--------------------------

program Project1;

{$APPTYPE GUI}
{$STACKFRAMES OFF}
{$INLINE ON}
{$MODE FPC}
{$ASMMODE INTEL}
{$MMX-}
{$FPUTYPE SSE2}
{$OPTIMIZATION LEVEL3}
{$OPTIMIZATION REGVAR}
{$OPTIMIZATION STACKFRAME}
{$OPTIMIZATION PEEPHOLE}
{$OPTIMIZATION ASMCSE}
{$OPTIMIZATION LOOPUNROLL}
{$OPTIMIZATION TAILREC}
{$OPTIMIZATION CSE}
{$OPTIMIZATION DFA}
{$OPTIMIZATION USEEBP}
{$OPTIMIZATION ORDERFIELDS}
{$OPTIMIZATION FASTMATH}
{$OPTIMIZATION REMOVEEMPTYPROCS}
{$OPTIMIZATION CONSTPROP}

function test_it(a,b:longint):longint;assembler;//nostackframe;
asm
 add eax,edx
end;

procedure test;
begin
 if test_it(10,20)<>30 then
  halt(1);
end;

begin
 test;
end.

----------------------------

 So, the question is, should the "nostackframe" function modifier be required after specifying {$OPTIMIZATION STACKFRAME} and {$STACKFRAMES OFF}?

engkin

  • Hero Member
  • *****
  • Posts: 2513
Re: assembler routines and stack frame generation
« Reply #1 on: January 21, 2015, 03:22:14 pm »
From the documentation of {$STACKFRAMES OFF} it says it won't turn off stackframes for assembler routines.
Where did you read that?
According to this, your test_it function should have a stackframe because it has parameters.

Marladu

  • New Member
  • *
  • Posts: 19
Re: assembler routines and stack frame generation
« Reply #2 on: January 21, 2015, 04:15:36 pm »
 Hi and thanks for your reply.

 In the very same page you linked it says: "If the procedure is not an assembler procedure, it must not have a asm …end; block.", i figured that meant that compiler directive doesn't apply to assembler routines.

 Interestingly enough, changing mode to {$MODE DELPHI} without changing anything else in the code in the first post (so without nostackframe function modifier) then no stack frame is generated. Also, by digging up the compiler source code I found an undocumented compiler directive {$OPTIMIZATION FORCENOSTACKFRAME} that did remove that stackframe. I'm guessing it adds the nostackframe function modifier to every function so should be used carefully.

engkin

  • Hero Member
  • *****
  • Posts: 2513
Re: assembler routines and stack frame generation
« Reply #3 on: January 21, 2015, 04:56:27 pm »
In the very same page you linked it says: "If the procedure is not an assembler procedure, it must not have a asm …end; block.", i figured that meant that compiler directive doesn't apply to assembler routines.
My understanding is that it should *not* have an asm block *inside* a pascal procedure, it should be a pure pascal procedure or a pure assembly procedure. So mixing assembly and pascal is going to cause a stackframe, as in:
Code: [Select]
procedure SomeProc;
begin
..
//The following asm block is going to cause a stackframe:
  asm
    //some assembly
    ..
  end;
..
end;

Marladu

  • New Member
  • *
  • Posts: 19
Re: assembler routines and stack frame generation
« Reply #4 on: January 21, 2015, 06:09:24 pm »
 Ah ok thanks for the clarification, that wasn't what I had understood.

 As for {$OPTIMIZATION FORCENOSTACKFRAME} I made a mistake, it doesn't seem to do anything at this time, it's probably a work in progress. No idea why the stack frame in the code presented disappears when using {$mode delphi} but it does, just like it does in {$mode fpc} with "nostackframe;" on function implementation.