boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

337-struct-by-value-arg.c (1145B)


      1 /* Struct-by-value parameter passing. aarch64 AAPCS hands a 9..16-byte
      2  * aggregate in two consecutive arg registers (or two stack slots if
      3  * neither fits); cc.scm has to mirror that on both the call and
      4  * receive sides. Until this works, every callee with a wider-than-8B
      5  * struct param sees the second word truncated, which silently
      6  * miscompiles tcc.flat.c's CType-passing helpers and any user code
      7  * with similar shapes.
      8  */
      9 
     10 struct Pair { long a; long b; };
     11 
     12 static int probe(struct Pair x, struct Pair y, long ea1, long ea2,
     13                  long eb1, long eb2)
     14 {
     15     if (x.a != ea1) return 1;
     16     if (x.b != ea2) return 2;
     17     if (y.a != eb1) return 3;
     18     if (y.b != eb2) return 4;
     19     return 0;
     20 }
     21 
     22 static int probe_after_int(int prefix, struct Pair p, long ea, long eb)
     23 {
     24     if (prefix != 99)  return 5;
     25     if (p.a   != ea)   return 6;
     26     if (p.b   != eb)   return 7;
     27     return 0;
     28 }
     29 
     30 int main(void)
     31 {
     32     struct Pair a; a.a = 10; a.b = 20;
     33     struct Pair b; b.a = 30; b.b = 40;
     34 
     35     int r;
     36     if ((r = probe(a, b, 10, 20, 30, 40))) return 10 + r;
     37     if ((r = probe_after_int(99, a, 10, 20))) return 30 + r;
     38 
     39     return 0;
     40 }