[M3devel] a trouble with passing records by value..

hendrik at topoi.pooq.com hendrik at topoi.pooq.com
Thu Sep 2 19:50:33 CEST 2010


On Thu, Sep 02, 2010 at 02:59:13PM +0000, Jay K wrote:
> 
> Short answer:
>  
>  
> Consider:
>   void F1(int a) { printf("%p\n", &a); }
>   void F2(struct { int b,c;} d) { printf("%p\n", &d); }
>  
>  
> Longer anser:
>  
>  
> There is no ABI bug. Really.
>  
>  
> How parameters are passed does not imply where they must be at all times.
>  
>  
> C has the same feature set as Modula-3. In general and here specifically.
>    You can pass structs by value in C and you can take the address of such a struct.
>  
>  
> The ABI is never perfectly efficient for all scenarios.
> If one could predict that all structed passed by value will have their
> address taken, then a different choice might be made.
> However the reality is that sometimes their address is taken, sometimes not.
> One ABI must be formulated (for interop) that strikes an efficiency balance, and always works.
>  
>  
> Consider that I can take the address of integers and floats and pointers passed by value!
>  Yet they are very very often passed in registers. (see the "short answer").
>  
>  
> A record is just a smaller or larger collection of integers/floats/pointers.
>  
>  
> The difference is mainly in the layout of the in-memory location, and
> the possibility of large size.
>  

To avoid going into assembly langauge in my C interpreter, on many platforms I did the 
following hack:

Pack the parameters into a large struct (like struct big{char space[1000];} ), calculating 
parameter positions myself.  The type of the space array was completely irrelevant -- as 
long as it ws big enough.

Then I pass the struct by value to the called program, ignoring the types the called 
program wanted -- you can do this with enough casts.

This worked for a *lot* of systems.
 
To avoid wasting too much stack space, I had a variety of struct types available, of 
different sizes, each with its own code.

-- hendrik



More information about the M3devel mailing list