[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Generalizing the big/little attribute
From: |
Bruno Haible |
Subject: |
Generalizing the big/little attribute |
Date: |
Sun, 07 Jun 2020 12:35:45 +0200 |
User-agent: |
KMail/5.1.3 (Linux/4.4.0-179-generic; KDE/5.18.0; x86_64; ; ) |
Currently, big/little applies only integral struct fields:
deftype test =
struct
{
little uint32 a; // OK
little uint8[3] b; // ERROR
little struct { uint8 x; uint8 y; } c; // ERROR
};
A simple generalization to make it apply to integral types, then
deftype test =
struct
{
little uint32 a; // OK
little uint8[3] b; // would be OK
little struct { uint8 x; uint8 y; } c; // ERROR
};
However, another generalization would be more powerful:
A number of data formats exist in big-endian and little-endian variants.
The sender chooses the variant; the receiver accepts both.
Examples: UTF-16, TIFF [1], PCAP [2], GNU .mo [3].
With the simple generalization, the pickle to define such a format looks
like this:
deftype foo =
struct
{
big uint16 magic : magic in [0xcafe,0xfeca];
struct { little uint8 x; little uint8 y; } body_le if magic == 0xcafe;
struct { big uint8 x; big uint8 y; } body_be if magic == 0xfeca;
};
With a generalization that allows it to be attached to a complex struct
field, and apply to the elements of that field recursively, the pickle
could look like this:
deftype foo =
struct
{
big uint16 magic : magic in [0xcafe,0xfeca];
little struct { uint8 x; uint8 y; } body_le if magic == 0xcafe;
big struct { uint8 x; uint8 y; } body_be if magic == 0xfeca;
};
This is better because it allows to define the body in a single place
(no code duplication):
deftype foo_body = struct { uint8 x; uint8 y; };
deftype foo =
struct
{
big uint16 magic : magic in [0xcafe,0xfeca];
little foo_body body_le if magic == 0xcafe;
big foo_body body_be if magic == 0xfeca;
};
With this generalization, I think, the type system needs to be
extended accordingly. For example you would not only be able to write
foo_body @ 2#B
but also
little foo_body @ 2#B
big foo_body @ 2#B
So, in any place where you can use a type name, you could also add a
little/big attribute, and it has a recursive effect.
Method invocations would not only have an implicit SELF argument, but
maybe also an implicit ENDIANNESS_OVERRIDE argument?
Bruno
[1] https://en.wikipedia.org/wiki/Magic_number_(programming)
[2] https://stackoverflow.com/questions/17928319/
[3] https://www.gnu.org/software/gettext/manual/html_node/MO-Files.html
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- Generalizing the big/little attribute,
Bruno Haible <=