I try to call function with variable number of arguments. The compiler pushes arguments in stack and then my function pops them. If I pass "int" all work good. If I pass "long long" or "double" (8-bytes variables) my function pops wrong parameters. As I see in assembler code, the compiler pushes "long long" or "double" in stack, but doesn't shift stack pointer properly.
My code:
int func_var(int first, int second, ...)
{
volatile int value;
volatile long long ll;
va_list ap;
va_start(ap,second);
printf("1: %x\n", first);
printf("2: %x\n", second);
value = va_arg(ap, int);
printf("3: %x\n", value);
ll = va_arg(ap, long long);
value = (int)ll;
printf("4.1: %x\n", value);
value = (int)(ll>>32);
printf("4.2: %x\n", value);
value = va_arg(ap, int);
printf("5: %x\n", value);
value = va_arg(ap, int);
printf("6: %x\n", value);
value = va_arg(ap, int);
printf("7: %x\n", value);
va_end(ap);
return 0;
}
....
int a = 0x10101010;
int b = 0x20202020;
int c = 0x30303030;
long long d = 0x4040404041414141;
int e = 0x50505050;
int f = 0x60606060;
func_var(a, b, c, d, e, f);
...
I have got the following results:
1: 10101010
2: 20202020
3: 30303030
4.1: 40404040
4.2: 41414141
5: 40404040
6: 50505050
7: 60606060
My environment:
- IDE: e2studio v5.4.0.015
- Compiler: KPIT GNUARM-NONE-EABI v16.01
- chip: RZ/A1 (Cortex-A9)
- optimization level: none
- note: stack has 8-bytes alignment
What can be the reason? May be I should to use some options?