3 ms·
They are very common and useful for emulators. They are indeed trouble for super-portable interfaces. They also tempt some people into trouble with hardware dr
by souprock 7y ago
They are very common and useful for emulators.
They are indeed trouble for super-portable interfaces. They also tempt some people into trouble with hardware drivers, but then that would already be trouble anyway due to instruction reordering in CPU or memory operation reordering beyond the CPU. (volatile is neither sufficient nor necessary)
In a normal emulator, none of that is a problem. Reasonable uses of bitfields are compatible between the x86_64 ELF ABI (Linux, etc.) and the Windows ABI. What you gain is C code that is simultaneously concise and readable.
For example, a CPU instruction can be represented in a header file by a union of anonymous structs, each of which contains a padding bitfield and a bitfield for part of the instruction. Access in the C file is then just as easy as for normal struct members. It works for non-CPU hardware too, like DMA descriptors and motherboard registers.
- mytailorisrich 7y agoMacros make code concise and readable. As I said, C's bit fields are often banned in coding guidelines.
- souprock 7y agoHow do you get a macro to make an L-value out of a bitfield? That is, it goes on the left of an assignment. I'm doing stuff like this: ethdev->phy.foo = val; // foo is a bitfield in struct phy cpu->status.irqlevel = newlevel; // irqlevel is a bitfield in the CPU's status word No, I don't want a 2-argument macro (or worse, with an implied variable) called something like SET_FOO or SET_IRQLEVEL. Crummy coding guidelines are not a useful argument against the value of bitfields in the C programming language. Coding guidelines can also ban floating-point types, recursion, or symbol names longer than 6 characters. The problem is not C. The problem is the coding guidelines.
- mytailorisrich 7y agoIt's not because you do not understand something and its reasons to exist (though I hinted at them in previous comments) that it is 'crummy'...