On Fri, Nov 7, 2008 at 3:46 AM, Lutz Donnerhacke <[EMAIL PROTECTED]> wrote:
> * 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).
>
...
>
> The only portable way to get access to C defined structures is involving
> a C compiler, either using hsc or a C helper module.
I don't think hsc2hs provides that support natively. (I would love to
be proven wrong about that.)
However, I think it would be reasonable to extend hsc2hs. I've
attached a basic draft of an extension that would make hsc2hs support
this. Basically this extra template file adds #{peek_bit
<type>,<field>} and #{poke_bit <type>,<field>} which present basically
the same interface as #{peek} and #{poke} but specialized with some
extra smarts to support bit-fields.
I probably won't have time to refine this code, but if others find
this code to be useful, feel free to take and use code and maybe we
can get some proper support into hsc2hs someday. *nudge nudge*
Michael D. Adams
[EMAIL PROTECTED]
#define hsc_calc_mask(minByte, minBit, mask, t, f) \
{ \
t x; \
char *p = (char*)&x; \
memset(p,0,sizeof(t)); \
\
/* Find byte range */ \
x.f = -1; \
minByte = 0; \
while (p[minByte] == 0) minByte++; \
size_t maxByte = sizeof(t) - 1; \
while (p[maxByte] == 0) maxByte--; \
\
/* Find bit range */ \
x.f = 1; \
size_t maxBit; \
if (p[minByte] != 0) { \
minBit = p[minByte]; \
x.f = -1; \
maxBit = p[maxByte] + 1; \
} else { \
minBit = p[maxByte]; \
x.f = -1; \
maxBit = p[minByte] + 1; \
} \
\
mask = (maxBit * (1 << 8*(maxByte-minByte)) / minBit) - 1; \
}
#define hsc_peek_bit(t, f) \
{ \
size_t minByte, minBit, mask; \
hsc_calc_mask(minByte,minBit,mask,t,f) \
printf("(\\hsc_ptr -> peekByteOff hsc_ptr %lu" \
">>= \\hsc_y -> return $ hsc_y `div` %lu .&. %lu)", \
minByte, minBit, mask); \
}
#define hsc_poke_bit(t, f) \
{ \
size_t minByte, minBit, mask; \
hsc_calc_mask(minByte,minBit,mask,t,f) \
printf("(\\hsc_ptr hsc_value -> peekByteOff hsc_ptr %lu" \
">>= \\hsc_y -> pokeByteOff hsc_ptr %lu" \
" ((hsc_y .&. %lu) .|. ((hsc_value * %lu) .&. %lu)))", \
minByte, minByte, ~mask, minBit, mask); \
}
_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe