[fpc-pascal] Converting old pascal written for Pascal/MT+ compiler

Bernd Oppolzer bernd.oppolzer at t-online.de
Tue Apr 4 08:54:54 CEST 2023


Am 04.04.2023 um 08:16 schrieb Jacob Kroon via fpc-pascal:
>
> Thanks for the tip above.
>
> I was able to write a couple of perl-scripts that are able to convert 
> my old Pascal sources to something that fpc can parse. Amongst other 
> things, the scripts inject the "public name"/"external name" 
> annotations so that the program can link.
>
> But I suspect I have a new problem: With the old Pascal/MT+ compiler 
> it would appear that local variables declared in functions/procedures 
> have a life-time that spans the whole program, like a "static" 
> declared variable in C. With fpc, it looks like locally declared 
> variables are automatic, put on the stack(?), and so they go out of 
> existence once out of scope ?


IMO, this is not a feature of the old PASCAL compiler,
but instead your Pascal programs "by error" depends on the local variables
appearing at the same location on the stack at subsequent calls as before
(if there are no other calls at the same level in between).

The local variables are not initialized when allocated, and so it is 
possible
that they still have the old value that they had when the same function
was left the last time. I know (from the 1970s and 1980s) that some 
weird programs
used this effect. But as soon as another call was entered between the 
two calls
of the procedure, the stack at this location was overwritten, and the 
"clever" use
of the old value of the variable was not possible any more. And: it 
depends on the
stack being initialized to a known default value in the first time.

This said: the initial value of the local variables is UNDEFINED, and 
this is true
for every Pascal compiler. I cannot imagine a compiler which doesn't 
follow this basic rule.

So IMO: you should find the places in your program, where this weird 
technique is used
and not blame the old compiler for this.

Some compilers have an option which initializes every automatic variable 
on every allocation;
some compilers even allow the bit-pattern to be specified (for example: 
0xff).
While this is a performance nightmare, this is very good to run the 
program once during program test,
because it will show if your program depends on such effects or if it 
produces different values,
depending on initialized or uninitialized local variables.

Kind regards

Bernd


>
> The program depends on this feature in the old compiler. I did some 
> googling and found that putting local variables in a "const" section 
> instead of "var" would make them have a "whole-program" lifetime, but 
> then I need to provide them with an initial value.
>
> Do I have any other option besides changing from "var" to "const" 
> everywhere, and provide initial values in all declarations ?
>
> Regards
> Jacob
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


More information about the fpc-pascal mailing list