boot2

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

111-struct-ret-1word.c (1050B)


      1 /* Struct return — 1 word.
      2  *
      3  * Whole struct fits in a single 8-byte return slot. The function exit
      4  * emits `LD a0, [sp + <return-slot>]`; a single-int struct rides
      5  * through that slot intact. */
      6 
      7 struct OneInt  { int x; };                  /* 4 bytes */
      8 struct TwoInt  { int x; int y; };           /* 8 bytes */
      9 struct OneLong { long v; };                 /* 8 bytes */
     10 
     11 struct OneInt  ret1(int v)              { struct OneInt  s; s.x = v; return s; }
     12 struct TwoInt  ret2(int a, int b)       { struct TwoInt  s; s.x = a; s.y = b; return s; }
     13 struct OneLong retL(long v)             { struct OneLong s; s.v = v; return s; }
     14 
     15 int main(int argc, char **argv) {
     16     struct OneInt  a = ret1(42);
     17     if (a.x != 42) return 1;
     18 
     19     struct TwoInt  b = ret2(7, 9);
     20     if (b.x != 7 || b.y != 9) return 2;
     21 
     22     struct OneLong c = retL(0x1122334455667788L);
     23     if (c.v != 0x1122334455667788L) return 3;
     24 
     25     /* Call result used directly without intermediate. */
     26     if (ret1(99).x != 99) return 4;
     27     if (ret2(11, 22).y != 22) return 5;
     28     return 0;
     29 }