stdarg.h (3679B)
1 /* -*-comment-start: "//";comment-end:""-*- 2 * GNU Mes --- Maxwell Equations of Software 3 * Copyright © 2017,2018,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org> 4 * Copyright © 2021 W. J. van der Laan <laanwj@protonmail.com> 5 * Copyright © 2023 Andrius Štikonas <andrius@stikonas.eu> 6 * 7 * This file is part of GNU Mes. 8 * 9 * GNU Mes is free software; you can redistribute it and/or modify it 10 * under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; either version 3 of the License, or (at 12 * your option) any later version. 13 * 14 * GNU Mes is distributed in the hope that it will be useful, but 15 * WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>. 21 */ 22 #ifndef __MES_STDARG_H 23 #define __MES_STDARG_H 1 24 25 #if SYSTEM_LIBC 26 #undef __MES_STDARG_H 27 #include_next <stdarg.h> 28 29 #define va_arg8(ap, type) va_arg (ap, type) 30 31 #elif (__GNUC__ || __TINYC__) && __riscv 32 33 // GCC on RISC-V always passes arguments in registers. Implementing 34 // these macros without the use of built-ins would be very involved. 35 // TINYCC tries to be GCC compatible in this case. 36 37 #if __TINYC__ 38 // TINYCC needs some definitions in RISC-V in order to be built 39 // without it's own code generation tool. 40 typedef char *__builtin_va_list; 41 #define __va_reg_size (__riscv_xlen >> 3) 42 #define _tcc_align(addr, type) \ 43 (((unsigned long)addr + __alignof__(type) - 1) \ 44 & -(__alignof__(type))) 45 #define __builtin_va_arg(ap, type) \ 46 (*(sizeof (type) > (2*__va_reg_size) \ 47 ? *(type **)((ap += __va_reg_size) - __va_reg_size) \ 48 : (ap = (va_list)(_tcc_align (ap, type) \ 49 + (sizeof (type) + __va_reg_size - 1) \ 50 & -__va_reg_size), \ 51 (type *)(ap - ((sizeof (type)+ __va_reg_size - 1) \ 52 & -__va_reg_size))))) 53 54 #define __builtin_va_end(ap) (void)(ap) 55 #if !defined (__builtin_va_copy) 56 #define __builtin_va_copy(dest, src) (dest) = (src) 57 #endif 58 #endif // __TINYC__ 59 60 typedef __builtin_va_list va_list; 61 62 #define va_start(v, l) __builtin_va_start (v, l) 63 #define va_end(v) __builtin_va_end (v) 64 #define va_arg(v, l) __builtin_va_arg (v, l) 65 #define va_arg8(ap, type) va_arg (ap, type) 66 #define va_copy(d, s) __builtin_va_copy (d, s) 67 68 #else // ! SYSTEM_LIBC && ! __riscv 69 70 #include <sys/types.h> 71 72 #if __GNUC__ && __x86_64__ 73 #define __FOO_VARARGS 1 74 #endif 75 76 typedef char *va_list; 77 #define va_start(ap, last) (void)((ap) = (char*)(&(last)) + sizeof (void*)) 78 #define va_arg(ap, type) (type)(((long*)((ap) = ((ap) + sizeof (void*))))[-1]) 79 #define va_align(ap, alignment) ((char*)((((unsigned long) (ap)) + (alignment) - 1) &~ ((alignment) - 1))) 80 #define va_arg8(ap, type) (type)(((double*)((ap) = (va_align((ap), 8) + sizeof(double))))[-1]) 81 #define va_end(ap) (void)((ap) = 0) 82 #define va_copy(dest, src) dest = src 83 84 int vexec (char const *file_name, va_list ap); 85 int vfprintf (FILE * stream, char const *template, va_list ap); 86 int vfscanf (FILE * stream, char const *template, va_list ap); 87 int vprintf (char const *format, va_list ap); 88 int vsprintf (char *str, char const *format, va_list ap); 89 int vsnprintf (char *str, size_t size, char const *format, va_list ap); 90 int vsscanf (char const *s, char const *template, va_list ap); 91 92 #endif // ! SYSTEM_LIBC 93 94 #endif // __MES_STDARG_H