Ivan Denisov wrote:
The TRAP never happen in DevCPC486.Convert.
And that is why it was so difficult to find the fix.
It took me almost 2 days of hard work to find it.
I will try to explain it in detail.
A TRAP happens
for example when continuing code generation with a LONGINT register pair
that uses an undefined second register and if that undefined register happens to be
a large value (the trap is an "illegal memory read"). If it happens to be a value within 0..31
the code generator may generate code that delivers random results
or it may fail at a different place in the compiler.
The TRAP occurs when testing the undefined register for membership in the SET of registers.
Such a test takes the set as a bit stream and does not perform a range check by default
(see docu of range checks).
The reason why there is an undefined second register for a LONGINT register pair
is that DevCPC486.Convert is called even after detecting that a conversion to LONGINT is not possible.
When a conversion is not possible due to the architecture of the machine (i486), an error message is generated
right before calling DevCPC486.Convert by means of DevCPM.err(...).
Any error message, be it from the parser or the code generator, stops the code generator
at the next statement. The current statement, however, is continued to be compiled and
that is where the TRAPs occur.
Conversion to LONGINT is not the only source of a TRAP as I have shown in earlier posts.
All traps that I have observed so far are avoided by the fix.
It is easy to proof that there cannot be any unintended side-effect from this fix:
1. the fix changes only the error behavior from trapping to non-trapping.
2. It does not introduce any new error detection logic.
3. It does not change the code generator for successful SYSTEM.VALs.
4. Whenever there is an invalid SYSTEM.VAL detected (with existing unmodified error detection logic),
code generation skips the invalid conversion and continues with the unconverted operand.
The unconverted operand is always defined because it is the source of a conversion.
- Josef