Recent

Author Topic: Is it possible for the compiler not remove unused variables from the code?  (Read 2016 times)

PascalDragon

  • Hero Member
  • *****
  • Posts: 2262
  • Compiler Developer
Re: Is it possible for the compiler not remove unused variables from the code?
« Reply #15 on: January 18, 2020, 01:19:43 pm »
In subject sources, unstead of calling hundred+ times of RegisterType, these records (like RDialog before) all are collected in a separate unit and are simply called in a loop:
Code: Pascal  [Select][+][-]
  1.  
  2. A: TStreamRec = (...
  3. B: TStreamRec = (...
  4. ...more than hundred...
  5. Z: TStreamRec = (...
  6.  
  7. var
  8.   P: PStreamRec;
  9.   I: Integer;
  10. begin
  11.   P := @A;
  12.   I :=Ofs(B) - Ofs(A);
  13.   repeat
  14.     RegisterType(P^);
  15.     Inc(PtrRec(P).Ofs, I);
  16.   until PtrRec(P).Ofs > Ofs(Z);
  17. end;

And because here explicitly used just three records, all the rest are missing in the binary obtained by FPC. That my current problem.

You could try the following (untested):

Code: Pascal  [Select][+][-]
  1. const
  2.   MyTypes: array[0..42 { whatever your count is }] of TStreamRec = (
  3.     ( ... ),
  4.     ( ... ),
  5.     ...
  6.     ( ... )
  7.   );
  8.  
  9. var
  10.   I: Integer;
  11. begin
  12.   for I := Low(MyTypes) to High(MyTypes) do
  13.     RegisterType(MyTypes[I]);
  14. end.

PascalDragon

  • Hero Member
  • *****
  • Posts: 2262
  • Compiler Developer
Re: Is it possible for the compiler not remove unused variables from the code?
« Reply #16 on: January 18, 2020, 01:25:33 pm »
Again: please do not try to hack this in. In FPC, this assumption is completely unreliable and can break at any time for various reasons (not just the linker).

Jonas, do Seg() and Ofs() generate portability warnings from the compiler? IMO they should, and that should have been sufficient to warn OP that he was on very thin ice.
Ofs and Seg do not require any portability warnings, cause they work correctly in flat memory (segment 0 and address in that segment encompassing the whole address space).

The problem here is not that Ofs is not working, but that the layout of the binary is different from what the code expected in TP.

This will still not guarantee that the variables appear in the same order as in the source, even if that will usually be the case. Additionally, the OP is targeting Win32, which does not use the GNU Linker (I'm not sure we even support the GNU linker for that platform).
Yes, the GNU linker still works, I last tested it when implementing the BigObj COFF format. To enable it the -Xe option needs to be used.

Avinash

  • New Member
  • *
  • Posts: 25
Re: Is it possible for the compiler not remove unused variables from the code?
« Reply #17 on: January 18, 2020, 03:18:01 pm »
Yes, the GNU linker still works, I last tested it when implementing the BigObj COFF format. To enable it the -Xe option needs to be used.
It seems that -Xe helps (alone, without public names). Executable size increased by ~40% but this is not a price.
I got compilation with -Aas/wasm/nasm, but without the ld.exe there really is no desired result.

Again: please do not try to hack this in. In FPC, this assumption is completely unreliable and can break at any time for various reasons (not just the linker).
Maybe, but for now I’ll try with the ld.exe. If it will ultimately works, then why not...
Thanks for help.

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 777
Re: Is it possible for the compiler not remove unused variables from the code?
« Reply #18 on: January 18, 2020, 03:21:44 pm »
Again: please do not try to hack this in. In FPC, this assumption is completely unreliable and can break at any time for various reasons (not just the linker).
Maybe, but for now I’ll try with the ld.exe. If it will ultimately works, then why not...
[/quote]
Because, as I mentioned, it could stop working at any time. This is not how you develop reliable software. It's like keeping an uninitialised variable in your code because "it worked when I tested it".

MarkMLl

  • Hero Member
  • *****
  • Posts: 1347
Re: Is it possible for the compiler not remove unused variables from the code?
« Reply #19 on: January 18, 2020, 04:48:05 pm »
Maybe, but for now I’ll try with the ld.exe. If it will ultimately works, then why not...
Thanks for help.

Because Jonas is one of the core developers and has told you that's it's unwise. That is sufficient reason to look for an alternative solution.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

jamie

  • Hero Member
  • *****
  • Posts: 3660
Re: Is it possible for the compiler not remove unused variables from the code?
« Reply #20 on: January 18, 2020, 11:51:45 pm »
how about all of the needed fields inside a Record and then make a simple reference to that record so the compiler will not remove it..

 Actually the reference could be the start of the record..
The only true wisdom is knowing you know nothing

PascalDragon

  • Hero Member
  • *****
  • Posts: 2262
  • Compiler Developer
Re: Is it possible for the compiler not remove unused variables from the code?
« Reply #21 on: January 19, 2020, 10:10:14 am »
We're talking about record constants of a preexisting type here. The approach I showed would be simpler in that case.

jamie

  • Hero Member
  • *****
  • Posts: 3660
Re: Is it possible for the compiler not remove unused variables from the code?
« Reply #22 on: January 19, 2020, 03:43:08 pm »
I remember writing DOS drivers using TP because I could remove the System unit and it would allow me to keep data at the head end for me to define to header of the driver  :D
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018