Any comment on http://www.freepascal.org/mantis/view.php?id=9053 and Daniël Mantione's note ?
Regards,
Adriaan van Os
On Jun 11, 2007, at 1:13 AM, Adriaan van Os wrote:
Any comment on http://www.freepascal.org/mantis/view.php?id=9053 and Daniël Mantione's note ?
There is no error in GPC's handling of the program UnknownLocalSymbol. For easy reference, the program is:
program UnknownLocalSymbol;
procedure p; label 10; begin if False then begin 10: Exit end; goto 10 end;
begin end.
The program doesn't meet ISO Pascal's requirements for goto statements. GPC correctly detects the error and issues a correct error message.
In regards to Daniël Mantione's note:
"Allthough GPC handles it more nicely, I'd say both compilers are in error; since the exit statement is reachable via the goto, the compiler is not allowed to optimize the if-statement away."
Daniël is mistaken that the exit statement is reachable via the goto in a legal ISO Pascal program. The label prefixing statement "10: Exit" does not satisfy any of the goto statement containing requirements of ISO 10206, paragraph 6.9.1 general requirements for labels and goto statements:
"A label, if any, of a statement S shall be designated as prefixing S. The label shall be permitted to occur in a goto-statement G (see 6.9.2.4) if and only if any of the following three conditions is satisfied.
a) S contains G.
b) S is a statement of a statement-sequence containing G.
c) S is a statement of the statement-sequence of the compound- statement of the statement-part of a block containing G.
statement = [ label ':' ] ( simple-statement j structured- statement ) .
NOTE --- 2 A goto-statement within a block may refer to a label in an enclosing block, provided that the label prefixes a statement at the outermost level of nesting of the block. "
It appears that Daniël is also erroneously assuming the reason for GPC's error message is because the if statement was optimized away. That isn't the reason for GPC's error message. The reason is because the label "10" isn't permitted to occur in the goto statement per ISO requirements.
Since FPC makes no claims for ISO Pascal requirements compliance, the FPC compiler may not enforce ISO label and goto statement requirements so what is or isn't legal with FPC depends upon what the FPC compiler folks have defined the requirements to be. As a general observation I'll note that transferring control into a nested statement can result in ill program behavior which is why ISO PAscal prohibits it.
Gale Paeper gpaeper@empirenet.com
Maurice Lombardi wrote:
Adriaan van Os a écrit:
Any comment on http://www.freepascal.org/mantis/view.php?id=9053 and Daniël Mantione's note ?
I would have guessed that it would compile (maybe with a warning) with the compilation option --no-iso-goto-restrictions
It does -- with a warning, as you say. (The option is called --no-iso-gotos.)
Gale Paeper wrote:
In regards to Daniël Mantione's note:
"Allthough GPC handles it more nicely, I'd say both compilers are in error; since the exit statement is reachable via the goto, the compiler is not allowed to optimize the if-statement away."
Daniël is mistaken that the exit statement is reachable via the goto in a legal ISO Pascal program. The label prefixing statement "10: Exit" does not satisfy any of the goto statement containing requirements of ISO 10206, paragraph 6.9.1 general requirements for labels and goto statements:
It appears that Daniël is also erroneously assuming the reason for GPC's error message is because the if statement was optimized away. That isn't the reason for GPC's error message. The reason is because the label "10" isn't permitted to occur in the goto statement per ISO requirements.
Since FPC makes no claims for ISO Pascal requirements compliance, the FPC compiler may not enforce ISO label and goto statement requirements so what is or isn't legal with FPC depends upon what the FPC compiler folks have defined the requirements to be. As a general observation I'll note that transferring control into a nested statement can result in ill program behavior which is why ISO PAscal prohibits it.
I agree.
In strict ISO, the code is indeed unreachable. Letting the assembler detect the invalid goto, while not strictly incorrect in this case, might be confusing. (And it won't work for invalid gotos in most other cases.)
OTOH, if you do allow such gotos (which are indeed problematic in some cases, and at least on the verge in this case), like GPC does with "--no-iso-gotos", and I think BP does generally, and apparently also FPC does by default, then of course, you cannot optimize away code which, due to a label, is not in fact unreachable.
Frank
Frank Heckenbach a écrit:
Maurice Lombardi wrote:
Adriaan van Os a écrit:
Any comment on http://www.freepascal.org/mantis/view.php?id=9053 and Daniël Mantione's note ?
I would have guessed that it would compile (maybe with a warning) with the compilation option --no-iso-goto-restrictions
It does -- with a warning, as you say. (The option is called --no-iso-gotos.)
This is a recent change then. I my version gpc 20060325 / gcc 3.4.4 (DJGPP) --(no-)iso-gotos is not recognized. --(no-)iso-goto-restrictions is recognized but ineffective in this case.
Maurice
Maurice Lombardi wrote:
I my version gpc 20060325 / gcc 3.4.4 (DJGPP) --(no-)iso-gotos is not recognized. --(no-)iso-goto-restrictions is recognized but ineffective in this case.
This is a bug in 20060325, --no-iso-goto-restrictions missed one case.
On Jun 11, 2007, at 11:18 AM, Micha Nelissen wrote:
Gale Paeper wrote:
c) S is a statement of the statement-sequence of the compound- statement of the statement-part of a block containing G.
This is certainly not a trivial statement, care to explain why this is not the case ? Seems to me this one applies here.
You're correct in that it isn't a trivial statement. Exactly what is legally allowed by that statement requires using the ISO standard's definition of the terms used in the statement.
For the program in question, the "compound-statement of the statement- part of a block containing G" is:
begin if False then begin 10: Exit end; goto 10 end;
In abstract terms, the statements in the statement-sequence of that compound-statement are:
if-statement goto-statement
Since there is no statement in that statement sequence prefixed by the label "10", the label "10" is not permitted to occur in the goto statement by that c) condition.
Note: The exit-statement is contained by the if-statement. Thus, the exit-statement is not a statement in the statement-sequence; it is merely a component of the if-statement. Per definition, it is the if- statement which is the statement in the statement-sequence.
I'll note that the c) condition is also what permits non-local goto's. However, in the example program, the label isn't defined in the containing program block so the only containing block of the goto- statement involved is procedure p's block.
Gale Paeper gpaeper@empirenet.com
Gale Paeper wrote:
In abstract terms, the statements in the statement-sequence of that compound-statement are:
if-statement goto-statement
I see. The following is then also not allowed ? Assume, i, j, l, h, k, m integers, foo a label.
for i := l to h do begin for j := k to m do begin writeln(j, ' '); goto foo; end; foo: writeln('hello'); end;
Micha
On Jun 11, 2007, at 2:14 PM, Micha Nelissen wrote:
Gale Paeper wrote:
In abstract terms, the statements in the statement-sequence of that compound-statement are:
if-statement goto-statement
I see. The following is then also not allowed ? Assume, i, j, l, h, k, m integers, foo a label.
for i := l to h do begin for j := k to m do begin writeln(j, ' '); goto foo; end; foo: writeln('hello'); end;
It is a ISO Pascal legal goto statement. Remember there are three conditions and all that is necessary if one of the conditions is satisfied. In this example, condition b) "S is a statement of a statement-sequence containing G." is satisfied so the foo label is a valid label for the goto statement. (Ignoring for the moment that "foo" isn't an ISO Pascal legal label.)
"foo: writeln('hello');" is a statement in the "for i..." compound- statement statement-sequence. The "for j ..." statement is also in that statement-sequence and the "goto foo;" statement is contained (through the containing compound-statement) by that for-statement. So, with the "goto foo;" statement, the foo label is prefixing a statement of a statement-sequence which contains (through several containing levels) the goto-statement. Thus, condition b) is satisfied and "foo"" is a legal label target for the goto statement.
Gale Paeper gpaeper@empirenet.com
Adriaan van Os a écrit:
Any comment on http://www.freepascal.org/mantis/view.php?id=9053 and Daniël Mantione's note ?
I would have guessed that it would compile (maybe with a warning) with the compilation option --no-iso-goto-restrictions
Maurice
I don't recommend to use labels and goto statements at all. It just messes the code and tells about bad programming manners.
2007/6/11, Adriaan van Os gpc@microbizz.nl:
Any comment on http://www.freepascal.org/mantis/view.php?id=9053 and Daniël Mantione's note ?
Regards,
Adriaan van Os