Recent

Author Topic: The compiler doesn't {$pop} local directive value  (Read 944 times)

alpine

  • Hero Member
  • *****
  • Posts: 1063
The compiler doesn't {$pop} local directive value
« on: September 20, 2022, 01:30:02 pm »
Here are two functions, one after another. The first one is enclosed in $push/$pop directives and have a register variables optimization. I'd expect second one not to have such, but apparently it does.

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$optimization off}
  4.  
  5.   {$push} {$optimization regvar}
  6.   function IntLog2Up1(A: PtrUInt): PtrUInt;
  7.   var
  8.    P: PtrUInt;
  9.   begin
  10.    P := 0;
  11.    Dec(A);
  12.    if (A and $FFFF0000) <> 0 then begin A := A shr 16; P := P or 16; end;
  13.    if (A and $FF00) <> 0 then begin A := A shr 8; P := P or 8; end;
  14.    if (A and $F0) <> 0 then begin A := A shr 4; P := P or 4; end;
  15.    if (A and $0C) <> 0 then begin A := A shr 2; P := P or 2; end;
  16.    if (A and 2) <> 0 then begin A := A shr 1; P := P or 1; end;
  17.    Result := P + 1;
  18.   end;
  19.   {$pop}
  20.  
  21.   function IntLog2Up2(A: PtrUInt): PtrUInt;
  22.   var
  23.    P: PtrUInt;
  24.   begin
  25.    Result := 0;
  26.    P := 1;
  27.    while (P < A) do
  28.    begin
  29.      P := P * 2;
  30.      Inc(Result);
  31.    end;
  32.   end;
  33.  
  34. begin
  35.   WriteLn(IntLog2Up1(7), IntLog2Up2(7));
  36. end.

Here is the beginning of the first one:
Code: Text  [Select][+][-]
  1. C:\Users\alpi\AppData\Local\Temp\project1.lpr:10  P := 0;
  2. 004015A3 B900000000               mov ecx,$00000000
  3. C:\Users\alpi\AppData\Local\Temp\project1.lpr:11  Dec(A);
  4. 004015A8 83E801                   sub eax,$01
  5. C:\Users\alpi\AppData\Local\Temp\project1.lpr:12  if (A and $FFFF0000) <> 0 then begin A := A shr 16; P := P or 16; end;
  6. 004015AB 89C2                     mov edx,eax
  7. 004015AD 81E20000FFFF             and edx,$FFFF0000
  8. 004015B3 F7C2FFFFFFFF             test edx,$FFFFFFFF
  9. 004015B9 7502                     jnz +$02
  10. 004015BB EB0F                     jmp +$0F
  11. ...
And here is the beginning of the second:
Code: Text  [Select][+][-]
  1. C:\Users\alpi\AppData\Local\Temp\project1.lpr:25  Result := 0;
  2. 00401656 B800000000               mov eax,$00000000
  3. C:\Users\alpi\AppData\Local\Temp\project1.lpr:26  P := 1;
  4. 0040165B BB01000000               mov ebx,$00000001
  5. C:\Users\alpi\AppData\Local\Temp\project1.lpr:27  while (P < A) do
  6. 00401660 EB0F                     jmp +$0F
  7. 00401662 8DB600000000             lea esi,[esi+$00000000]
  8. C:\Users\alpi\AppData\Local\Temp\project1.lpr:29  P := P * 2;
  9. 00401668 89D9                     mov ecx,ebx
  10. 0040166A D1E1                     shl ecx,1
  11. 0040166C 89CB                     mov ebx,ecx
  12. ...

Lazarus 2.2.2 (rev lazarus_2_2_2) FPC 3.2.2 i386-win32-win32/win64
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

MarkMLl

  • Hero Member
  • *****
  • Posts: 6685
Re: The compiler doesn't {$pop} local directive value
« Reply #1 on: September 20, 2022, 01:36:23 pm »
I think you need a test case: what happens if you delete the $ in the {$optimization regvar} leaving everything else the same?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

PascalDragon

  • Hero Member
  • *****
  • Posts: 5469
  • Compiler Developer
Re: The compiler doesn't {$pop} local directive value
« Reply #2 on: September 20, 2022, 01:44:15 pm »
Optimization switches are not covered by $Push and $Pop. :-[

But you can use {$Optimization Default} to return to the default settings that were active at the start of reading the unit.

Kays

  • Hero Member
  • *****
  • Posts: 575
  • Whasup!?
    • KaiBurghardt.de
Re: The compiler doesn’t {$pop} local directive value
« Reply #3 on: September 20, 2022, 01:48:21 pm »
Optimization switches are not covered by $Push and $Pop. :-[
I just wanted to say that. The documentation, the Programmer’s Guide, says {$push} saves the state of local compiler directives, but with New features 3.2.0 I learned that not everything is indeed saved.
Yours Sincerely
Kai Burghardt

alpine

  • Hero Member
  • *****
  • Posts: 1063
Re: The compiler doesn't {$pop} local directive value
« Reply #4 on: September 20, 2022, 01:55:27 pm »
I think you need a test case: what happens if you delete the $ in the {$optimization regvar} leaving everything else the same?

MarkMLl

Here it is without the $
Code: Text  [Select][+][-]
  1. C:\Users\alpi\AppData\Local\Temp\project1.lpr:10  P := 0;
  2. 004015AA C745F400000000           mov [ebp-$0C],$00000000
  3. C:\Users\alpi\AppData\Local\Temp\project1.lpr:11  Dec(A);
  4. 004015B1 836DFC01                 sub dword ptr [ebp-$04],$01
  5. C:\Users\alpi\AppData\Local\Temp\project1.lpr:12  if (A and $FFFF0000) <> 0 then begin A := A shr 16; P := P or 16; end;
  6. 004015B5 8B45FC                   mov eax,[ebp-$04]
  7. 004015B8 250000FFFF               and eax,$FFFF0000
  8. 004015BD A9FFFFFFFF               test eax,$FFFFFFFF
  9. 004015C2 7502                     jnz +$02
  10. 004015C4 EB12                     jmp +$12
  11.  

I have spotted the issue in another source, my code here is just a small sample.

Optimization switches are not covered by $Push and $Pop. :-[

But you can use {$Optimization Default} to return to the default settings that were active at the start of reading the unit.
I see. Maybe it is a docs issue then.


But it was quite confusing, since after I did that my watches on local variables went haywire.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

PascalDragon

  • Hero Member
  • *****
  • Posts: 5469
  • Compiler Developer
Re: The compiler doesn’t {$pop} local directive value
« Reply #5 on: September 21, 2022, 09:33:00 am »
Optimization switches are not covered by $Push and $Pop. :-[
I just wanted to say that. The documentation, the Programmer’s Guide, says {$push} saves the state of local compiler directives, but with New features 3.2.0 I learned that not everything is indeed saved.

Because originally it was indeed only the local switches. Later on it turned out that other settings might be nice to save as well. So that should probably be updated...

Optimization switches are not covered by $Push and $Pop. :-[

But you can use {$Optimization Default} to return to the default settings that were active at the start of reading the unit.
I see. Maybe it is a docs issue then.


But it was quite confusing, since after I did that my watches on local variables went haywire.

You mean after using {$Optimization Default}?

alpine

  • Hero Member
  • *****
  • Posts: 1063
Re: The compiler doesn’t {$pop} local directive value
« Reply #6 on: September 28, 2022, 12:04:24 am »

Optimization switches are not covered by $Push and $Pop. :-[

But you can use {$Optimization Default} to return to the default settings that were active at the start of reading the unit.
I see. Maybe it is a docs issue then.


But it was quite confusing, since after I did that my watches on local variables went haywire.

You mean after using {$Optimization Default}?
No, no. I was confused in the beginning, when I enclosed one of the procedures with:
Code: Pascal  [Select][+][-]
  1. {$push} {$optimization regvar}
  2. procedure ...
  3. {$pop}
Then on the following procedures, where I had a breakpoint and some watches to monitor, all locals got trashed and I needed a little time to realize that they simply went into registers because $pop doesn't work on $optimization.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

PascalDragon

  • Hero Member
  • *****
  • Posts: 5469
  • Compiler Developer
Re: The compiler doesn’t {$pop} local directive value
« Reply #7 on: September 28, 2022, 09:37:42 am »
No, no. I was confused in the beginning, when I enclosed one of the procedures with:
Code: Pascal  [Select][+][-]
  1. {$push} {$optimization regvar}
  2. procedure ...
  3. {$pop}
Then on the following procedures, where I had a breakpoint and some watches to monitor, all locals got trashed and I needed a little time to realize that they simply went into registers because $pop doesn't work on $optimization.

Ah, okay. Thank you for clarifying.

 

TinyPortal © 2005-2018