2

I am trying to retarget lcc for a custom VM. I am facing a problem when passing structures as arguments (by value). The VM's stack grows from low to high addresses. The offsets for the structure fields are being incorrectly generated for arguments.

For e.g., for the code below:

foo(sample p, sample q); 

struct sample 
{ 
    int a; 
    int b; 
}; 
main() 
{ 
    sample x, y; 
    foo(x, y); 
} 

The structures fields a and b as seen by foo() are located at address (&p and &p-4) and (&q and &q-4). These should be (&p and &p+4) and (&q and &q+4). The addresses for struct x and y in main() are correctly referring to their fields (i.e. addresses being generated are &x, &x+4 and &y, &y+4). I have verified that the code generated for ARG+B nodes is correctly copying the passed struct argument on the stack (the base of the copied struct begins at lower address).

Any help would be appreciated.

6
  • Pank4j, can you phrase your preponderance into an interrogatory? May 5, 2014 at 2:04
  • Are you retargeting lcc or lcc-win32 (the latter is based on the former). And how do you know that the addresses are as you describe them? Are you examining generated code, or do you have a running program whose output indicates the offsets? May 5, 2014 at 2:15
  • @MahonriMoriancumer To put it simply, the addresses of the members of passed structures should be growing from low to high for each successive member. However, I notice otherwise. The question is why?
    – pank4j
    May 5, 2014 at 3:10
  • @KeithThompson I am retargeting lcc (not lcc-win32). I have sample code that shows the addresses.
    – pank4j
    May 5, 2014 at 3:13
  • Please include the sample code and its output in your question. (The code currently in your question is not valid C; there is no type sample, just struct sample.) May 5, 2014 at 4:11

1 Answer 1

2

Your observation of how your compiler lays down the stack can often cause confusion.

The stack may grow in either direction, depending on the system. My experience is that most grow from highest address to lowest address as per your observation on your system (with 'x' and 'y').

The values 'a' and 'b' within the structure are another matter. The compiler has no prerogative to reorder these. The C specification requires these to be ordered, within the structure, as they are defined.

Hence, your observations, while surprising, are correct; and the compiler is operating properly as well.

(Or, perhaps I am mis-understanding the detail provided?)

6
  • Well, what confuses me is that on one hand, there are structures (local var for main()) whose each successive member point to higher addresses, and then there are arguments to foo(), whose each successive member point to lower addresses. Now irrespective of stack direction, shouldn't these behave similarly?
    – pank4j
    May 5, 2014 at 3:42
  • @pank4j, Unfortunately, no. Structure elements will always be placed in memory the same way (first element will have lowest address), even when that structure is on the stack. While individual items placed on the stack (including entire structures) are often addressed in the other direction (first item on the stack has the highest address). May 5, 2014 at 3:48
  • 1
    @pank4j: The relative layout of members within a structure should always be consistent. The addresses of distinct objects, of whatever type, have no defined relationship as far as the C standard is concerned. Your description implies that the compiler is laying out members within the same struct incorrectly, but it's impossible to sure of that without seeing actual output of an actual program, like the one in the codepad link I posted in a comment on the question. May 5, 2014 at 4:25
  • Here is the sample code that I am trying to run, along with its output. codepad.org/w0v1AJxB
    – pank4j
    May 5, 2014 at 6:21
  • @pank4j: That's a lot of code. Did you try the code I suggested at codepad.org/3BiRQUGB ? Your code has undefined behavior; "%x" requires an argument of type unsigned int, but you're passing pointers. To print a pointer value, use "%p" and cast the value to void*. Better yet, if you want to print the offset of a struct member, use offsetof and/or use pointer arithmetic to subtract the address of the structure from the address of the member (as I did in my example). May 5, 2014 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.