boot2

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

321-vararg-mixed-many.c (894B)


      1 /* Mixed-boundary variadic: 2 named + several variadic spanning the
      2  * a-register / LDARG boundary. Probes that:
      3  *   - named arg 0 (lives in a0/save-slot 0) is preserved
      4  *   - named arg 1 (lives in a1/save-slot 1) is preserved
      5  *   - variadic args at idx >= 4 (LDARG-loaded) are read correctly
      6  *
      7  * Sum = base + tag + 1+2+3+4+5+6 = 100 + 7 + 21 = 128. */
      8 
      9 #ifndef CCSCM
     10 #include <stdarg.h>
     11 #else
     12 typedef char *va_list;
     13 #define va_start(ap, n) __builtin_va_start(ap, n)
     14 #define va_arg(ap, t)   __builtin_va_arg(ap, t)
     15 #define va_end(ap)      __builtin_va_end(ap)
     16 #endif
     17 
     18 int sum_with(int base, int n, ...) {
     19     va_list ap;
     20     int total;
     21     int i;
     22     va_start(ap, n);
     23     total = base;
     24     i = 0;
     25     while (i < n) {
     26         total = total + va_arg(ap, int);
     27         i = i + 1;
     28     }
     29     va_end(ap);
     30     return total;
     31 }
     32 
     33 int main(void) {
     34     return sum_with(100, 7, 1, 2, 3, 4, 5, 6, 7);
     35 }