[Haskell-cafe] Bit Field Marshalling

Lutz Donnerhacke lutz at iks-jena.de
Fri Nov 7 03:46:56 EST 2008


* Michael D. Adams wrote:
> But as far as I can tell, hsc2hs doesn't support bit
> fields.  On top of that I'm not sure I can make any safe assumptions
> about what order the bit fields are packed (LSB or MSB first).

C standard allows padding and reorder of struct entries in order to match
alignment requirements. The only exeption are bitfields, which must not
reordered and padded. This way bit fields are the only portable way to
define the binary representation in C. Unfortunly the C standard does not
specify any bit order for bit fields, but almost all implementations use
the machine specific bit order, in order to ease access to multiple bits
wide bit field and fill LSB to MSB. But there is no guarantee.

I run into this problem when writing an low level kernel interface in SPARK.
The ABI (binary representation) of the kernel API depends on the C compiler
flags (i.e. #pragma packed). Especially kernels of commericial unicies might
generate this problem, because the developer uses gcc instead of the native
compiler.

The only portable way to get access to C defined structures is involving
a C compiler, either using hsc or a C helper module.


More information about the Haskell-Cafe mailing list