Page 1 of 1

Varargs Issue

Posted: Wed Feb 02, 2011 10:50 am
by pranjas
Hi
I was coding the printk routine for which i needed the varargs. As mentioned in one of the printing tutorials on this website it uses gcc's in built version.

Code: Select all

#define va_start(v,l) __builtin_va_start(v,l)
#define va_arg(v,l)   __builtin_va_arg(v,l)
#define va_end(v)     __builtin_va_end(v)
#define va_copy(d,s)  __builtin_va_copy(d,s)
typedef __builtin_va_list va_list
The problem is as soon i insert this code i get a weird error

Code: Select all

error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘extern’
Any thoughts on how to get it working. I tried one of the other implementations which is here,

Code: Select all

typedef char* va_list;

#define _INTSIZEOF(n)   ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )

#define va_start(ap,v)  ( ap = (va_list)&v + _INTSIZEOF(v) )

#define va_arg(argp,type) (*(type *)(((argp) +=_INTSIZEOF(type))-_INTSIZEOF(type)))

#define va_end(x) (x=(void*)0)
but this implementation isn't working for me at least with gcc.
I've gcc version 4.4.1 OpenSuse11.2 for developing the kernel.

Re: Varargs Issue

Posted: Wed Feb 02, 2011 12:34 pm
by kriscrump
pranjas wrote:

Code: Select all

typedef __builtin_va_list va_list
The problem is as soon i insert this code i get a weird error

Code: Select all

error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘extern’
Any thoughts on how to get it working. I tried one of the other implementations which is here,
I'm not sure if this might be a problem, but shouldn't the aforementioned line of code end with a ';'? Maybe this will fix it? Unless it was just a mistake in typing it into osdev's editor?

Regards,
~Kris

Re: Varargs Issue

Posted: Wed Feb 02, 2011 1:22 pm
by mariuszp
i think i understand your mistake.

If you use a compiler directive (one that starts with '#'), then the line does not need to end with ';', so:

Code: Select all

#define HI 0x555
is valid syntax. However, 'typedef' is technically not a compiler directive. So this:

Code: Select all

typedef char*  va_list
is invalid. You must add a ';' at the end, like this:

Code: Select all

typedef char * va_list;
I had these kinds of problems all the time. Hope I helped :)

Re: Varargs Issue

Posted: Wed Feb 02, 2011 9:20 pm
by pranjas
:oops: !... yeah there's no ; after the typedef.

thanks... i guess worrying too much about bigger problems i'm facing couldn't notice this small mistake...
really embarassed