Sujet : Re: transpiling to low level C
De : bc (at) *nospam* freeuk.com (bart)
Groupes : comp.lang.cDate : 17. Dec 2024, 19:37:47
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vjsgdr$1rrvs$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9
User-Agent : Mozilla Thunderbird
On 17/12/2024 18:16, Thiago Adams wrote:
also remove structs changing by unsigned char [] and cast parts of it to access members.
I think this the lower level possible in c.
This is what I do in my IL, where structs are just fixed blocks of so many bytes.
But there are some things to consider:
* A struct may still need alignment corresponding to the strictest alignment among the members. (Any padding between members and at the end should already be taken care of.)
I use an alignment based on overall size, so a 40-byte struct is assumed to have an 64-bit max alignment, but it may only need 16-bit alignment. That is harmless, but it can be fixed with some extra metadata.
With a C char[], you can choose to use a short[] array for example (obviously of half the length) to signal that it needs 16-bit alignment.
* Some machine ABIs, like SYS V for 64 bits, may need to know the internal layout of structs when they are passed 'by value'.
If reduced down to char[], this info will be missing.
I ignore this because I only target Win64 ABI. It only comes up in SYS V, when calling functions across an FFI, and when the API uses value structs, which is uncommon. And also makes I can't make head or tail of the rules.