[M3devel] packing problem… how exactly does modula-3 pack data into records??

Jay K jay.krell at cornell.edu
Fri Jan 20 02:18:26 CET 2012

The cross-wordsize problem of gcc was indeed lame, but it is long since gone.

 Gcc has an almost-great cross-build story. 
 Ld has a good cross-build story. Though it doesn't support as many systems as gcc.  
 The problem is getting headers and libraries. 
 I've been trying to a build ia64-linux toolset from source but I haven't gotten glibc/linux-headers to work out. 
 Producing .S and .o from .c (w/o #include) is easy enough. 

On Windows, cross-compilers are now also common, a lot of the tools are x86-hosted despite targeting AMD64, IA64, and more (Windows CE: PowerPC, MIPS, ARM, SH.)
Though I think NT/MIPS, NT/PowerPC, NT/Alpha had only native compilers released -- presumably there were cross compilers at least for bootstrapping.

Anyway, if you just conserving space, then "BITS FOR" should work fine.
If you need to interface with C, and you can't change the C, you should be able to get something to work, but it will possibly be somewhat non-portable, esp. endian-specific.
I think besides endianness, there is another factor, where cm3 tries to be C-compatible.
Like, how aligned bitfields need to be.

I only recently learned the rules for NT bitfields.
First, aside, in C, bitfields can only be int or unsigned.
In Microsoft C however, they can be any integer type.
The layout rules are affected by the type.
I think the rules are that the type of the bitfield determines the alignment of the loads/stores to access it.

That is:
struct {
 unsigned char a;
 unsigned char b:1;

struct {
  unsigned char a;
  unsigned int b:1

In the first case, b is at byte offset 1.
In the second case, it is at byte offset 4.

I don't remember what order the bits are allocated within bytes.

Anyway, it isn't portable stuff.

I used to like bitfields for compactness.
Lately I disfavor them, because I like to be able to predict the layout of a struct.
Still, they can be useful:
  1) again, compactness
  2) lining up with some external definition, esp. hardware (e.g. page tables) 
  3) ability to do stuff like:

union {
   unsigned AsInt;
   struct {
       unsigned a : 1;
       unsigned b : 1;

       unsigned c : 1;
   } s;

} u;

You can test AsInt for non-zeroness to see if any of a/b/c are set.
That can be useful.

Though, if layout-predictability is important, I'd just do one of:

 unsigned char a, b, c;
 unsigned char any;

and require setters of a/b/c to set any,

  unsigned char a,b,c;

require checking a || b || c.
Problem is -- when d is added, have to update all the uses.

Then again..C++

  unsigned char a,b,c;
  bool Any() { return a || b || c; }

only one place to update.

 - Jay

From: dragisha at m3w.org
Date: Thu, 19 Jan 2012 23:51:34 +0100
To: jay.krell at cornell.edu
CC: m3devel at elegosoft.com
Subject: Re: [M3devel] packing problem… how exactly does modula-3 pack data into records??

back in 1997-8 I did an LINUXALPHA port of pm3.
All portability problems I met were in C. And biggie was with gcc and its inability (at the moment) to cross compile on 32bit platform for 64bit RISC (lots and lots of registers on target machine).
So, speaking about C and portability... No such thing.
Your m3core.h work is great, by the way. I do a lot of low level stuff and it helps a lot. Thanks! :)
On Jan 19, 2012, at 11:42 PM, Jay K wrote:(then again -- endian-knowledge can be used to make ntohl/htonl highly optimized.)

I know it's a little wierd what I say -- C is impelementation-dependent and we try
to be C compatible. And m3core/libm3 used to have some dependency
on this C compatibility, e.g. the waitpid stuff. But I believe I removed all
such dependency.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://m3lists.elegosoft.com/pipermail/m3devel/attachments/20120120/14ac847a/attachment-0002.html>

More information about the M3devel mailing list