boot2

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

79-vararg-deep.c (705B)


      1 /* Variadic receive past index 4 — extends §G.2 across the stack-arg
      2  * boundary. P1 ABI puts args 0..3 in registers and args 4+ in the
      3  * incoming stack-arg area accessed via LDARG.
      4  *
      5  * sum(6, 1, 2, 4, 8, 16, 32): n=6 named + 6 variadic.
      6  *   Register-passed: n=6, v0=1, v1=2, v2=4
      7  *   Stack-passed:    v3=8, v4=16, v5=32
      8  *   Variadic sum   = 63
      9  */
     10 
     11 typedef char *va_list;
     12 
     13 int sum(int n, ...) {
     14     va_list ap;
     15     int total;
     16     int i;
     17     __builtin_va_start(ap, n);
     18     total = 0;
     19     i = 0;
     20     while (i < n) {
     21         total = total + __builtin_va_arg(ap, int);
     22         i = i + 1;
     23     }
     24     __builtin_va_end(ap);
     25     return total;
     26 }
     27 
     28 int main(void) {
     29     return sum(6, 1, 2, 4, 8, 16, 32);
     30 }