Converting C's bit field struct

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Converting C's bit field struct

leledumbo
Administrator
This post was updated on .
Since no one answers on message board, I'll post it here.

C struct:

typedef unsigned int u32int;

typedef struct page
{
   u32int present    : 1;   // Page present in memory
   u32int rw         : 1;   // Read-only if clear, readwrite if set
   u32int user       : 1;   // Supervisor level only if clear
   u32int accessed   : 1;   // Has the page been accessed since last refresh?
   u32int dirty      : 1;   // Has the page been written to since last refresh?
   u32int unused     : 7;   // Amalgamation of unused and reserved bits
   u32int frame      : 20;  // Frame address (shifted right 12 bits)
} page_t;

Pascal record please...
Reply | Threaded
Open this post in threaded view
|

Re: Converting C's bit field struct

leledumbo
Administrator
Found on the net that Pascal doesn't provide it
What if FPC creates it as an extension ?
Reply | Threaded
Open this post in threaded view
|

Re: Converting C's bit field struct

Vincent Snijders
leledumbo schreef:
> Found on the net that Pascal doesn't provide it :-((
> What if FPC creates it as an extension =)?

FPC supports bitpacked records. I cannot find it in the docs right now.

Vincent
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Converting C's bit field struct

Vinzent Höfler
In reply to this post by leledumbo
leledumbo wrote:

> Since no one answers on message board, I'll post it here.
>
> C struct:
>
> typedef unsigned int u32int;
>
> typedef struct page
> {
>    u32int present    : 1;   // Page present in memory
>    u32int rw         : 1;   // Read-only if clear, readwrite if set
>    u32int user       : 1;   // Supervisor level only if clear
>    u32int accessed   : 1;   // Has the page been accessed since last
> refresh?
>    u32int dirty      : 1;   // Has the page been written to since last
> refresh?
>    u32int unused     : 7;   // Amalgamation of unused and reserved bits
>    u32int frame      : 20;  // Frame address (shifted right 12 bits)
> } page_t;

Hmm. That's nasty. ;)

 > Pascal record please...

Should be something like that:

type
    Unsigned_7  = 0 .. (1 shl 7)  - 1;
    Unsigned_20 = 0 .. (1 shl 20) - 1;

type
    page_t = bitpacked record
       present  : boolean;
       rw       : boolean;
       user     : boolean;
       accessed : boolean;
       dirty    : boolean;
       unused   : Unsigned_7;
       frame    : Unsigned_20;
    end;

I guess, portability is of no concern with such a structure. ;)



Vinzent.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Converting C's bit field struct

leledumbo
Administrator
Vincent Snijders wrote
FPC supports bitpacked records. I cannot find it in the docs right now.
Oh... didn't know if such a thing exists.
Vinzent Höfler wrote
type
    Unsigned_7  = 0 .. (1 shl 7)  - 1;
    Unsigned_20 = 0 .. (1 shl 20) - 1;

type
    page_t = bitpacked record
       present  : boolean;
       rw       : boolean;
       user     : boolean;
       accessed : boolean;
       dirty    : boolean;
       unused   : Unsigned_7;
       frame    : Unsigned_20;
    end;
Weird... never seen anything like this before , but I'll try. Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: Converting C's bit field struct

Michael Van Canneyt
In reply to this post by leledumbo


On Wed, 13 Aug 2008, leledumbo wrote:

>
> Found on the net that Pascal doesn't provide it :-((
> What if FPC creates it as an extension =)?

FPC has it, but only in MacPas mode.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Converting C's bit field struct

Vincent Snijders
Michael Van Canneyt schreef:
>
> On Wed, 13 Aug 2008, leledumbo wrote:
>
>> Found on the net that Pascal doesn't provide it :-((
>> What if FPC creates it as an extension =)?
>
> FPC has it, but only in MacPas mode.
>

I copied the example program from: http://qc.codegear.com/wc/qcmain.aspx?d=56480

Added {$mode delphi} or {$mode objfpc} and the program compiled without errors.

Vincent
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Converting C's bit field struct

Thomas Schatzl
Hello,

Am 13.08.2008 um 13:23 schrieb Vincent Snijders:

> Michael Van Canneyt schreef:
>> On Wed, 13 Aug 2008, leledumbo wrote:
>>> Found on the net that Pascal doesn't provide it :-((
>>> What if FPC creates it as an extension =)?
>> FPC has it, but only in MacPas mode.
>>
>
> I copied the example program from: http://qc.codegear.com/wc/ 
> qcmain.aspx?d=56480
>
> Added {$mode delphi} or {$mode objfpc} and the program compiled  
> without errors.
>

Bitpacked records have been implemented and/or greatly improved for  
all platforms some time ago by Jonas; there were some problems with  
the "old" implementation iirc. Bitpacked records are also supported  
in other modes than macpas mode, however I do not know whether it has  
been intentional to enable them in Delphi mode.

Regards,
   Thomas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Converting C's bit field struct

Jonas Maebe-2
In reply to this post by Vinzent Höfler

On 13 Aug 2008, at 11:59, Vinzent Höfler wrote:

> leledumbo wrote:
>> Since no one answers on message board, I'll post it here.
>> C struct:
>> typedef unsigned int u32int;
>> typedef struct page
>> {
>>  u32int present    : 1;   // Page present in memory
>>  u32int rw         : 1;   // Read-only if clear, readwrite if set
>>  u32int user       : 1;   // Supervisor level only if clear
>>  u32int accessed   : 1;   // Has the page been accessed since last
>> refresh?
>>  u32int dirty      : 1;   // Has the page been written to since last
>> refresh?
>>  u32int unused     : 7;   // Amalgamation of unused and reserved bits
>>  u32int frame      : 20;  // Frame address (shifted right 12 bits)
>> } page_t;
>
> Hmm. That's nasty. ;)w
>
> > Pascal record please...
>
> Should be something like that:
>
> type
>  Unsigned_7  = 0 .. (1 shl 7)  - 1;
>  Unsigned_20 = 0 .. (1 shl 20) - 1;
>
> type
>  page_t = bitpacked record

Yes and no: the layout of bitpacked records is by no means guaranteed  
to be the same as that of a C record, regardless of the definition of  
the C record. The internal format of bitpacked records is opaque and  
must not be used in any case where data has to be interchanged or  
where a particular layout is desired.


Jonas_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Converting C's bit field struct

Vinzent Höfler
-------- Original-Nachricht --------

> Datum: Wed, 13 Aug 2008 22:03:23 +0100
> Von: Jonas Maebe <[hidden email]>
> An: FPC-Pascal users discussions <[hidden email]>
> Betreff: Re: [fpc-pascal] Converting C\'s bit field struct
>
> On 13 Aug 2008, at 11:59, Vinzent Höfler wrote:
>
> > Should be something like that:
> >
> > type
> >  Unsigned_7  = 0 .. (1 shl 7)  - 1;
> >  Unsigned_20 = 0 .. (1 shl 20) - 1;
> >
> > type
> >  page_t = bitpacked record
>
> Yes and no: the layout of bitpacked records is by no means guaranteed  
> to be the same as that of a C record, regardless of the definition of  
> the C record.

Well, this is obviously a hardware structure and the actual layout should be checked regardless of the definition in C (IIRC, the C-standard doesn't guarantee any particular layout on such records neither).

> The internal format of bitpacked records is opaque and  
> must not be used in any case where data has to be interchanged or  
> where a particular layout is desired.

There was a reason why I brought up the portability issue. Honestly, what do you propose to mimic such stuff? I remember, we already had a discussion about that a couple of months ago. ;)

I don't want to bring up Ada again, but ... some years ago I implemented such nasty stuff for an embedded system. You might take a look for the implementation of system structures in that language http://www.gmx.de/mc/h9DZhQD3l4X4hfKafRIagg6e3PI6TW . Well, apart from Endianess issues, successful compilation virtually guarantees the layout.

I hope the link works, currently I don't own any web-space... Just click on that "GMX MediaCenter starten" button...


Vinzent.

--
GMX startet ShortView.de. Hier findest Du Leute mit Deinen Interessen!
Jetzt dabei sein: http://www.shortview.de/wasistshortview.php?mc=sv_ext_mf@gmx
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Converting C's bit field struct

leledumbo
Administrator
Tried Vinzent solution and it works !
The layout is I think the same as in C. The difference is that FPC generates it as

resb 4

while C uses

.comm 16

I don't know what .comm for. But since everything runs fine, who cares?
I just have some issues where assignment to the fields using pointer to that type (^page_t) doesn't work. For instance:

var
  P: PPage; // the type is named TPage
...
with P^ do begin
  Present:=true;
  ReadWrite:=false;
  UserMode:=true;
  Frame:=FrameStack[FrameStackTop];
end;

When I check the value of each field, it's all ZERO! But the pointer isn't nil. I guess the problem lies in my OS, but I'll do a re-check.
Reply | Threaded
Open this post in threaded view
|

Re: Converting C's bit field struct

Jonas Maebe-2

On 15 Aug 2008, at 13:23, leledumbo wrote:

> Tried Vinzent solution and it works :clap:!
> The layout is I think the same as in C.

It may be the same as in C in certain cases on certain platforms when  
compiled with a particular version of FPC. Do not depend on it always  
being or remaining the same, because it *will* not work at some point.


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal