[fpc-devel] [Suggestion] Enumeration range-check intrinsic

J. Gareth Moreton gareth at moreton-family.com
Sat Jul 6 01:48:05 CEST 2019


On 05/07/2019 23:50, Sven Barth via fpc-devel wrote:
> Am 05.07.2019 um 22:34 schrieb J. Gareth Moreton:
>>
>> It feels like a very convoluted way to implement a safe means of 
>> reading an enumeration from a file, but given that that reading 
>> incorrect data files is one of the main causes of enums taking on 
>> invalid values, I have to say that this is the best solution should 
>> we not be allowed the "is" operator or an intrinsic.  If that is the 
>> case, then I propose this gets added to TStream!
>>
> At least it hides the gruesome details, cause you only need to call 
> ReadEnum<> (or ReadRange<>)... And maybe the code the compiler 
> generates can even be optimized further. :)
>
> Regards,
> Sven

On x86, when it's a standard enum with a 0 lower bound (most enums are 
like this) the check can be made as simple as this:

CMP Value, HighValue{ HighValue is calculated at compile-time, while 
Value is the memory or register that the enum is stored in }
JBE @InRange

I'm not sure how well the compiler can generate that from "If (Value >= 
Ord(Low(TEnum))) and (Value <= Ord(High(TEnum))) then" because it abuses 
the signed/unsigned convention to check both sides of the domain (i.e. 
if the value is negative, it is instead treated as a very large positive 
number that fails the conditions on the jump).

Gareth aka. Kit


---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus



More information about the fpc-devel mailing list