boot2

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

320-vararg-cap.c (991B)


      1 /* Variadic save area is capped at 16 incoming-arg slots (named +
      2  * variadic). cg should reject calls that exceed this cap with a clear
      3  * compile-time error rather than silently miscompile by writing past
      4  * the save area in the callee's prologue.
      5  *
      6  * This test exercises the boundary: 1 named + 15 variadic = 16 args
      7  * total, which is the maximum supported. Sum 1..15 = 120. */
      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(int n, ...) {
     19     va_list ap;
     20     int total;
     21     int i;
     22     va_start(ap, n);
     23     total = 0;
     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     /* 15 variadic ints; n itself is named arg 0; total 16 args. */
     35     return sum(15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
     36 }