gm2
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Gm2] translating C bit fields


From: Gaius Mulley
Subject: Re: [Gm2] translating C bit fields
Date: Wed, 23 Nov 2011 22:25:36 +0000
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)

Dragiša Durić <address@hidden> writes:


>
> On Nov 23, 2011, at 3:29 PM, Gaius Mulley wrote:
>
>> Dragiša Durić <address@hidden> writes:
>> 
>>> Gaius,
>>> 
>>> IMO - we also need a mechanism to fix a sequence of fields to word boundary.
>>> Obviousy, a field without "BY" will fit to its default alignment position, 
>>> but
>>> what if we have
>>> 
>>> type = RECORD
>>>  foo: INTEGER BY 2;
>>>  bar: CARDINAL BY 4;
>>>  butalso: CARDINAL BY 2; (* and we need to have this aligned to some 
>>> boundary…
>>> *)
>>> END;
>>> 
>>> Alignment can be pretty complicated these days when on same architecture 
>>> some
>>> people decide to align 64 bit values to  byte positions divisible by 8… And
>>> other do not. I met such a situation while wrapping Gtk2 to Modula-3… gcc
>>> aligns such values to divisible-by-4 byte positions, while cm3 aligns to
>>> divisible-by-8…
>> 
>> Hi Dragiša,
>> 
>> ok - how about:
>> 
>> type = RECORD
>>   foo: INTEGER BY 2;
>>   bar: CARDINAL BY 4;
>>   butalso: CARDINAL MOD 4 BY 2; (* and we need to have this aligned to some 
>> boundary *)
>> END;
>> 
>> so we extend the ebnf to include:
>> 
>> FieldAlignment := [ ByteAlignment | BitAlignment ] =:
>> 
>> BitAlignment := [ 'MOD' ConstExpression ] 'BY' ConstExpression =:
>> 
>> ByteAlignment := "__ATTRIBUTE__" "(" "(" Ident "(" ConstExpression ")" ")" 
>> ")"
>>               =:
>> 
>> where the MOD indicates the byte alignment and BY indicates the bit
>> length?  MOD 0 indicates packed, MOD 1 the next byte.
>> 
>> regards,
>> Gaius

> Hi Gaius,
>
> It looks good.I suppose we can also use this to put "ordinary" fields
> "out of" standard alignment ?

Hi Dragiša,

>
> RECORD
>   a: BOOLEAN BY 8;

I don't this the above would be legal - as it seems to be requesting an
8 bit BOOLEAN.  I suspect the compiler should insist that the bitlength
be <= TSIZE(type)*8.

Did you perhaps mean:

 RECORD
    x   : CARDINAL MOD 0 BY 7 ;
    a   : BOOLEAN MOD 0 BY 1 ;
    b   : CARDINAL MOD 0 BY 32 ;
 END ;

so the first 7 bits represent x, bit 8 the boolean a, last 32 the
cardinal, b.

>   b: CARDINAL MOD 0 BY 32?
> END;
>
> Or..?
>
> dd

or have I misunderstood your question?

regards,
Gaius



reply via email to

[Prev in Thread] Current Thread [Next in Thread]