1
0
mirror of https://github.com/cc65/cc65.git synced 2024-12-25 02:29:52 +00:00
cc65/test/val/trampoline-varargs.c

49 lines
948 B
C
Raw Normal View History

2017-05-19 11:20:04 +00:00
/*
2017-05-20 06:53:30 +00:00
!!DESCRIPTION!! wrapped-call pragma w/ variadic function
2017-05-19 11:20:04 +00:00
!!ORIGIN!! cc65 regression tests
!!LICENCE!! Public Domain
!!AUTHOR!! Lauri Kasanen
*/
#include <stdarg.h>
static unsigned char flag;
static void trampoline_set(void) {
2019-02-12 21:50:49 +00:00
// The Y register is used for variadics - save and restore
asm("sty tmp3");
2017-05-19 11:20:04 +00:00
2019-02-12 21:50:49 +00:00
asm("ldy tmp4");
asm("sty %v", flag);
2017-05-19 11:20:04 +00:00
2019-02-12 21:50:49 +00:00
asm("ldy tmp3");
asm("jsr callptr4");
2017-05-19 11:20:04 +00:00
}
#pragma wrapped-call(push, trampoline_set, 4)
unsigned adder(unsigned char num, ...);
#pragma wrapped-call(pop)
unsigned adder(unsigned char num, ...) {
2019-02-12 21:50:49 +00:00
unsigned char i;
unsigned sum = 0;
va_list ap;
va_start(ap, num);
2017-05-19 11:20:04 +00:00
2019-02-12 21:50:49 +00:00
for (i = 0; i < num; i++) {
sum += va_arg(ap, unsigned);
}
2017-05-19 11:20:04 +00:00
2019-02-12 21:50:49 +00:00
va_end(ap);
2017-05-19 11:20:04 +00:00
2019-02-12 21:50:49 +00:00
return sum;
2017-05-19 11:20:04 +00:00
}
int main() {
2019-02-12 21:50:49 +00:00
flag = 0;
2017-05-19 11:20:04 +00:00
2019-02-12 21:50:49 +00:00
return adder(3, 0, 5, 500) == 505 && flag == 4 ? 0 : 1;
2017-05-19 11:20:04 +00:00
}