RE: [RFC] tools/nolibc: replace duplicated -ENOSYS return with single -ENOSYS return

From: Zhangjin Wu
Date: Tue Aug 29 2023 - 19:40:29 EST


Hi, David

> From: David Laight
> > Sent: 27 August 2023 22:52
> >
> > ...
> > > Of course, we can also use the __stringify() trick to do so, but it is
> > > expensive (bigger size, worse performance) to unstringify and get the number
> > > again, the expensive atoi() 'works' for the numeric __NR_*, but not work for
> > > (__NR_*_base + offset) like __NR_* definitions (used by ARM and MIPS), a simple
> > > interpreter is required for such cases and it is more expensive than atoi().
> > >
> > > /* not for ARM and MIPS */
> > >
> > > static int atoi(const char *s);
> > > #define __get_nr(name) __nr_atoi(__stringify(__NR_##name))
> > > #define __nr_atoi(str) (str[0] == '_' ? -1L : ___nr_atoi(str))
> > > #define ___nr_atoi(str) (str[0] == '(' ? -1L : atoi(str))
> > >
> > > Welcome more discussion or let's simply throw away this direction ;-)
> >
> > While it will look horrid the it ought to be possible to
> > get the compiler to evaluate the string.
> ...
> > So something that starts:
> > #define dig(c) (c < '0' || c > '9' ? 999999 : c - '0')
> > str[0] == '_' ? -1 :
> > str[0] != '(' ? str[1] == ' ' ? dig(str[0]) :
> > str[2] == '1' ? (dig(str[0]) * 10 + dig(str[1]) :
> > Any unexpected character will expand the 99999 and generate
> > an over-large result.
>
> See https://godbolt.org/z/rear4c1hj
>
> That will convert "1234" or "(1234 + 5678)" (or shorter numbers)
> as a compile-time constant.
>

Thanks very much, it works perfectly.

I tuned it for more complicated cases, including ((0x900000+0x0f0000)+5) used
by ARM+OABI (not used by nolibc), now, it should work for all of the
architectures: https://godbolt.org/z/a7hxWj83E ;-)

To get fast building, we can provide different versions for different
architectures. A simple test shows, only two versions (as you mentioned above,
one is "1234" converting, another is "(1234 + 5678)" calculating) are enough
for current nolibc supported architectures and the building of nolibc-test.c is
not slow.

With the __stringify() based __is_nr_defined() macro and this new __nrtoi()
macro based __get_nr() macro, there is no need to redefine the old NOLIBC__NR_*
macros, as a result, all of the duplicated -ENOSYS return lines and even all of
the #ifdef's from sys.h could be dropped and even no need to add them for new
future syscalls, and also, the old syscall() macro can return -ENOSYS at the
runtime instead of any compiling failures.

For the sys_* definitions, to avoid forgetting passing the arguments, instead
of using __VA_ARGS__, perhaps we should simply passing all of the arguments.

Best Regards,
Zhangjin

> David
>
> -
> Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
> Registration No: 1397386 (Wales)