boot2

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

320-vararg-cap.c (1032B)


      1 /* Variadic save area is capped at 32 incoming-argument words (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 Kit-motivated range above the old 16-word cap:
      7  * 1 named + 19 variadic = 20 args total. Sum 1..19 = 190. */
      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     /* 19 variadic ints; n itself is named arg 0; total 20 args. */
     35     return sum(19, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
     36                11, 12, 13, 14, 15, 16, 17, 18, 19);
     37 }