[fpc-pascal] Implementation of variant records

Sven Barth pascaldragon at googlemail.com
Sun Jul 7 13:55:26 CEST 2019


Am 07.07.2019 um 12:54 schrieb Sven Barth:
> Am 07.07.2019 um 02:55 schrieb Ralf Quint:
>> On 7/6/2019 12:21 PM, Florian Klaempfl wrote:
>>> Am 05.07.2019 um 13:53 schrieb Ralf Quint:
>>>> Shouldn't a PACKED Record guarantee that values are aligned at the 
>>>> byte
>>>> level?
>>> It does in TP, but the ISO says only that it shall be economised.
>>>
>> Well, the ISO is probably what has done most of the damage to Pascal. 
>> AFAIK, the ISO doesn't not actually specify any "packed record" per 
>> se, but there is an explicit PACK and UNPACK procedures to more 
>> economically store sets and arrays (and possibly records, I would 
>> have to look up the ISO text to be certain). For example this would 
>> be something to create bit fields, pretty much the only feature of C 
>> that I sometimes miss in (Free)Pascal.
> You mean like this?
>
> === code begin ===
>
> struct Test
> {
>         uint8_t field1;
>         uint16_t bfield1 : 1;
>         uint16_t bfield2 : 1;
>         uint16_t bfield3 : 3;
>         uint16_t bfield4 : 5;
>         uint16_t bfield5 : 1;
>         uint16_t field2;
> };
>
> === code end ===
>
> === code begin ===
>
> type
>   TTest = bitpacked record
>     field1: UInt8;
>     bfield1: 0..1;
>     bfield2: 0..1;
>     bfield3: 0..7;
>     bfield4: 0..31;
>     bfield5: 0..1;
>     pad: UInt8; // needed due to bitpacked
>     field2: UInt16;
>   end;
>
> === code end ===

Small correction: there needs to be another "bpad: 0..31;" field between 
bfield5 and pad, cause otherwise the pad field will be directly behind 
the bfield5 one. This could of course be avoided by putting the bit 
fields into a subrecord, though then of course there'd be the need for 
an additional field name.

Regards,
Sven


More information about the fpc-pascal mailing list