7#ifndef ZEPHYR_INCLUDE_SYS_CBPRINTF_INTERNAL_H_
8#define ZEPHYR_INCLUDE_SYS_CBPRINTF_INTERNAL_H_
24#define VA_STACK_ALIGN(type) 1
25#elif defined(__sparc__)
27#define VA_STACK_ALIGN(type) 1
28#elif defined(__x86_64__)
29#define VA_STACK_MIN_ALIGN 8
30#elif defined(__aarch64__)
31#define VA_STACK_MIN_ALIGN 8
33#ifdef CONFIG_RISCV_ISA_RV32E
34#define VA_STACK_ALIGN(type) 4
36#define VA_STACK_MIN_ALIGN (__riscv_xlen / 8)
44#ifndef VA_STACK_MIN_ALIGN
45#define VA_STACK_MIN_ALIGN 1
49#define VA_STACK_ALIGN(type) MAX(VA_STACK_MIN_ALIGN, __alignof__(type))
52static inline void z_cbprintf_wcpy(
int *dst,
int *src,
size_t len)
54 for (
size_t i = 0; i < len; i++) {
77#define Z_CBPRINTF_VA_STACK_LL_DBL_MEMCPY 1
79#define Z_CBPRINTF_VA_STACK_LL_DBL_MEMCPY 0
89#define Z_CBPRINTF_IS_PCHAR(x, flags) \
90 z_cbprintf_cxx_is_pchar(x, (flags) & CBPRINTF_PACKAGE_CONST_CHAR_RO)
92#define Z_CBPRINTF_IS_PCHAR(x, flags) \
96 const char * : ((flags) & CBPRINTF_PACKAGE_CONST_CHAR_RO) ? 0 : 1, \
97 volatile char * : 1, \
98 const volatile char * : 1, \
100 unsigned char * : 1, \
101 const unsigned char * : ((flags) & CBPRINTF_PACKAGE_CONST_CHAR_RO) ? 0 : 1, \
102 volatile unsigned char * : 1, \
103 const volatile unsigned char * : 1,\
106 const wchar_t * : ((flags) & CBPRINTF_PACKAGE_CONST_CHAR_RO) ? 0 : 1, \
107 volatile wchar_t * : 1, \
108 const volatile wchar_t * : 1, \
123#define Z_CBPRINTF_IS_X_PCHAR(idx, x, flags) \
124 (idx < Z_CBPRINTF_PACKAGE_FIRST_RO_STR_CNT_GET(flags) ? \
125 0 : Z_CBPRINTF_IS_PCHAR(x, flags))
135#define Z_CBPRINTF_HAS_PCHAR_ARGS(flags, fmt, ...) \
136 (FOR_EACH_IDX_FIXED_ARG(Z_CBPRINTF_IS_X_PCHAR, (+), flags, __VA_ARGS__))
138#define Z_CBPRINTF_PCHAR_COUNT(flags, ...) \
139 COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__), \
141 (Z_CBPRINTF_HAS_PCHAR_ARGS(flags, __VA_ARGS__)))
152#define Z_CBPRINTF_MUST_RUNTIME_PACKAGE(flags, ...) ({\
153 _Pragma("GCC diagnostic push") \
154 _Pragma("GCC diagnostic ignored \"-Wpointer-arith\"") \
156 if ((flags) & CBPRINTF_PACKAGE_ADD_RW_STR_POS) { \
159 _rv = Z_CBPRINTF_PCHAR_COUNT(flags, __VA_ARGS__) > 0 ? 1 : 0; \
161 _Pragma("GCC diagnostic pop")\
165#define Z_CBPRINTF_MUST_RUNTIME_PACKAGE(flags, ...) 1
178#define Z_CBPRINTF_ARG_SIZE(v) z_cbprintf_cxx_arg_size(v)
180#define Z_CBPRINTF_ARG_SIZE(v) ({\
181 __auto_type __v = (v) + 0; \
184 size_t __arg_size = _Generic((v), \
185 float : sizeof(double), \
200#define Z_CBPRINTF_STORE_ARG(buf, arg) z_cbprintf_cxx_store_arg(buf, arg)
202#define Z_CBPRINTF_STORE_ARG(buf, arg) do { \
203 if (Z_CBPRINTF_VA_STACK_LL_DBL_MEMCPY) { \
205 __auto_type _v = (arg) + 0; \
206 double _d = _Generic((arg) + 0, \
213 size_t arg_size = Z_CBPRINTF_ARG_SIZE(arg); \
214 size_t _wsize = arg_size / sizeof(int); \
215 z_cbprintf_wcpy((int *)buf, \
216 (int *) _Generic((arg) + 0, float : &_d, default : &_v), \
219 *_Generic((arg) + 0, \
221 unsigned char: (int *)buf, \
222 short : (int *)buf, \
223 unsigned short : (int *)buf, \
225 unsigned int : (unsigned int *)buf, \
226 long : (long *)buf, \
227 unsigned long : (unsigned long *)buf, \
228 long long : (long long *)buf, \
229 unsigned long long : (unsigned long long *)buf, \
230 float : (double *)buf, \
231 double : (double *)buf, \
232 long double : (long double *)buf, \
234 (const void **)buf) = arg; \
246#define Z_CBPRINTF_ALIGNMENT(_arg) z_cbprintf_cxx_alignment(_arg)
248#define Z_CBPRINTF_ALIGNMENT(_arg) \
249 MAX(_Generic((_arg) + 0, \
250 float : VA_STACK_ALIGN(double), \
251 double : VA_STACK_ALIGN(double), \
252 long double : VA_STACK_ALIGN(long double), \
253 long long : VA_STACK_ALIGN(long long), \
254 unsigned long long : VA_STACK_ALIGN(long long), \
256 __alignof__((_arg) + 0)), VA_STACK_MIN_ALIGN)
270#if defined(__x86_64__) || defined(__riscv) || defined(__aarch64__)
271#define Z_CBPRINTF_IS_LONGDOUBLE(x) 0
273#define Z_CBPRINTF_IS_LONGDOUBLE(x) z_cbprintf_cxx_is_longdouble(x)
276#define Z_CBPRINTF_IS_LONGDOUBLE(x) \
277 _Generic((x) + 0, long double : 1, default : 0)
295#define Z_CBPRINTF_PACK_ARG2(arg_idx, _buf, _idx, _align_offset, _max, _arg) \
297 BUILD_ASSERT(!((sizeof(double) < VA_STACK_ALIGN(long double)) && \
298 Z_CBPRINTF_IS_LONGDOUBLE(_arg) && \
299 !IS_ENABLED(CONFIG_CBPRINTF_PACKAGE_LONGDOUBLE)),\
300 "Packaging of long double not enabled in Kconfig."); \
301 while (_align_offset % Z_CBPRINTF_ALIGNMENT(_arg) != 0UL) { \
302 _idx += sizeof(int); \
303 _align_offset += sizeof(int); \
305 uint32_t _arg_size = Z_CBPRINTF_ARG_SIZE(_arg); \
306 uint32_t _loc = _idx / sizeof(int); \
307 if (arg_idx < 1 + _fros_cnt) { \
309 _ros_pos_buf[_ros_pos_idx++] = _loc; \
311 } else if (Z_CBPRINTF_IS_PCHAR(_arg, 0)) { \
313 if (Z_CBPRINTF_IS_X_PCHAR(arg_idx, _arg, _flags)) { \
315 _rws_buffer[_rws_pos_idx++] = arg_idx - 1; \
316 _rws_buffer[_rws_pos_idx++] = _loc; \
320 _ros_pos_buf[_ros_pos_idx++] = _loc; \
323 } else if (_rws_pos_en) { \
324 _rws_buffer[_rws_pos_idx++] = arg_idx - 1; \
325 _rws_buffer[_rws_pos_idx++] = _idx / sizeof(int); \
328 if (_buf && _idx < (int)_max) { \
329 Z_CBPRINTF_STORE_ARG(&_buf[_idx], _arg); \
332 _align_offset += _arg_size; \
341#define Z_CBPRINTF_PACK_ARG(arg_idx, arg) \
342 Z_CBPRINTF_PACK_ARG2(arg_idx, _pbuf, _pkg_len, _pkg_offset, _pmax, arg)
350#define Z_CBPRINTF_SUPPRESS_SIZEOF_ARRAY_DECAY \
351 _Pragma("GCC diagnostic ignored \"-Wsizeof-array-decay\"")
353#define Z_CBPRINTF_SUPPRESS_SIZEOF_ARRAY_DECAY
362#ifdef CONFIG_NO_OPTIMIZATIONS
363#define Z_CBPRINTF_ON_STACK_ALLOC(_name, _len) \
364 __ASSERT(_len <= 32, "Too many string arguments."); \
365 uint8_t _name##_buf32[32]; \
366 _name = _name##_buf32
368#define Z_CBPRINTF_ON_STACK_ALLOC(_name, _len) \
369 __ASSERT(_len <= 32, "Too many string arguments."); \
370 uint8_t _name##_buf4[4]; \
371 uint8_t _name##_buf8[8]; \
372 uint8_t _name##_buf12[12]; \
373 uint8_t _name##_buf16[16]; \
374 uint8_t _name##_buf32[32]; \
375 _name = (_len) <= 4 ? _name##_buf4 : \
376 ((_len) <= 8 ? _name##_buf8 : \
377 ((_len) <= 12 ? _name##_buf12 : \
378 ((_len) <= 16 ? _name##_buf16 : \
411#define Z_CBPRINTF_STATIC_PACKAGE_GENERIC(buf, _inlen, _outlen, _align_offset, \
414 _Pragma("GCC diagnostic push") \
415 _Pragma("GCC diagnostic ignored \"-Wpointer-arith\"") \
416 Z_CBPRINTF_SUPPRESS_SIZEOF_ARRAY_DECAY \
417 BUILD_ASSERT(!IS_ENABLED(CONFIG_XTENSA) || \
418 (IS_ENABLED(CONFIG_XTENSA) && \
419 !(_align_offset % CBPRINTF_PACKAGE_ALIGNMENT)), \
420 "Xtensa requires aligned package."); \
421 BUILD_ASSERT((_align_offset % sizeof(int)) == 0, \
422 "Alignment offset must be multiply of a word."); \
423 IF_ENABLED(CONFIG_CBPRINTF_STATIC_PACKAGE_CHECK_ALIGNMENT, \
424 (__ASSERT(!((uintptr_t)buf & (CBPRINTF_PACKAGE_ALIGNMENT - 1)), \
425 "Buffer must be aligned.");)) \
426 uint32_t _flags = flags; \
427 bool _ros_pos_en = (_flags) & CBPRINTF_PACKAGE_ADD_RO_STR_POS; \
428 bool _rws_pos_en = (_flags) & CBPRINTF_PACKAGE_ADD_RW_STR_POS; \
429 bool _cros_en = (_flags) & CBPRINTF_PACKAGE_CONST_CHAR_RO; \
430 uint8_t *_pbuf = buf; \
431 uint8_t _rws_pos_idx = 0; \
432 uint8_t _ros_pos_idx = 0; \
434 uint8_t _alls_cnt = Z_CBPRINTF_PCHAR_COUNT(0, __VA_ARGS__); \
435 uint8_t _fros_cnt = Z_CBPRINTF_PACKAGE_FIRST_RO_STR_CNT_GET(_flags); \
437 uint8_t _rws_cnt = _cros_en ? \
438 Z_CBPRINTF_PCHAR_COUNT(_flags, __VA_ARGS__) : _alls_cnt - _fros_cnt; \
439 uint8_t _ros_cnt = _ros_pos_en ? (1 + _alls_cnt - _rws_cnt) : 0; \
440 uint8_t *_ros_pos_buf; \
441 Z_CBPRINTF_ON_STACK_ALLOC(_ros_pos_buf, _ros_cnt); \
442 uint8_t *_rws_buffer; \
443 Z_CBPRINTF_ON_STACK_ALLOC(_rws_buffer, 2 * _rws_cnt); \
444 size_t _pmax = !___is_null(buf) ? _inlen : INT32_MAX; \
446 int _total_len = 0; \
447 int _pkg_offset = _align_offset; \
448 union cbprintf_package_hdr *_len_loc; \
450 if (_rws_cnt && !((_flags) & CBPRINTF_PACKAGE_ADD_RW_STR_POS)) { \
455 if (_pmax < sizeof(*_len_loc)) { \
459 _len_loc = (union cbprintf_package_hdr *)_pbuf; \
460 _pkg_len += sizeof(*_len_loc); \
461 _pkg_offset += sizeof(*_len_loc); \
463 FOR_EACH_IDX(Z_CBPRINTF_PACK_ARG, (;), __VA_ARGS__);\
464 _total_len = _pkg_len; \
466 _total_len += _ros_cnt; \
467 _total_len += 2 * _rws_cnt; \
468 if (_pbuf != NULL) { \
470 uint8_t *_pbuf_loc = &_pbuf[_pkg_len]; \
471 for (size_t i = 0; i < _ros_cnt; i++) { \
472 *_pbuf_loc++ = _ros_pos_buf[i]; \
474 for (size_t i = 0; i < (2 * _rws_cnt); i++) { \
475 *_pbuf_loc++ = _rws_buffer[i]; \
479 _outlen = (_total_len > (int)_pmax) ? -ENOSPC : _total_len; \
481 if (_pbuf != NULL) { \
482 union cbprintf_package_hdr hdr = { \
484 .len = (uint8_t)(_pkg_len / sizeof(int)), \
486 .ro_str_cnt = _ros_cnt, \
487 .rw_str_cnt = _rws_cnt, \
490 IF_ENABLED(CONFIG_CBPRINTF_PACKAGE_HEADER_STORE_CREATION_FLAGS, \
491 (hdr.desc.pkg_flags = flags)); \
494 _Pragma("GCC diagnostic pop") \
498#define Z_CBPRINTF_STATIC_PACKAGE(packaged, inlen, outlen, align_offset, flags, \
500 Z_CBPRINTF_STATIC_PACKAGE_GENERIC(packaged, inlen, outlen, \
501 align_offset, flags, __VA_ARGS__)
503#define Z_CBPRINTF_STATIC_PACKAGE(packaged, inlen, outlen, align_offset, flags, \
507 if (((uintptr_t)packaged + 1) != 1) { \
508 outlen = cbprintf_package(packaged, inlen, flags, __VA_ARGS__); \
510 outlen = cbprintf_package(NULL, align_offset, flags, __VA_ARGS__); \
519#ifdef CONFIG_CBPRINTF_PACKAGE_SUPPORT_TAGGED_ARGUMENTS
525#define Z_CBPRINTF_ARG_REMOVE_QUAL(arg) \
526 z_cbprintf_cxx_remove_cv < \
527 z_cbprintf_cxx_remove_reference < decltype(arg) > ::type \
533#define Z_CBPRINTF_CXX_ARG_ARRAY_TYPE(arg) \
534 z_cbprintf_cxx_remove_cv < \
535 z_cbprintf_cxx_remove_extent < decltype(arg) > ::type \
541#define Z_CBPRINTF_CXX_ARG_IS_TYPE_CHAR(type) \
542 (z_cbprintf_cxx_is_same_type < type, \
545 (z_cbprintf_cxx_is_same_type < type, \
546 const char > :: value ? \
548 (z_cbprintf_cxx_is_same_type < type, \
549 volatile char > :: value ? \
551 (z_cbprintf_cxx_is_same_type < type, \
552 const volatile char > :: value ? \
560#define Z_CBPRINTF_CXX_ARG_IS_CHAR_ARRAY(arg) \
561 (z_cbprintf_cxx_is_array < decltype(arg) > :: value ? \
562 (Z_CBPRINTF_CXX_ARG_IS_TYPE_CHAR(Z_CBPRINTF_CXX_ARG_ARRAY_TYPE(arg)) ? \
571#define Z_CBPRINTF_ARG_TYPE(arg) \
572 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
574 CBPRINTF_PACKAGE_ARG_TYPE_CHAR : \
575 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
576 unsigned char > ::value ? \
577 CBPRINTF_PACKAGE_ARG_TYPE_UNSIGNED_CHAR : \
578 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
580 CBPRINTF_PACKAGE_ARG_TYPE_SHORT : \
581 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
582 unsigned short > ::value ? \
583 CBPRINTF_PACKAGE_ARG_TYPE_UNSIGNED_SHORT : \
584 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
586 CBPRINTF_PACKAGE_ARG_TYPE_INT : \
587 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
588 unsigned int > ::value ? \
589 CBPRINTF_PACKAGE_ARG_TYPE_UNSIGNED_INT : \
590 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
592 CBPRINTF_PACKAGE_ARG_TYPE_LONG : \
593 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
594 unsigned long > ::value ? \
595 CBPRINTF_PACKAGE_ARG_TYPE_UNSIGNED_LONG : \
596 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
597 long long > ::value ? \
598 CBPRINTF_PACKAGE_ARG_TYPE_LONG_LONG : \
599 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
600 unsigned long long > ::value ? \
601 CBPRINTF_PACKAGE_ARG_TYPE_UNSIGNED_LONG_LONG : \
602 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
604 CBPRINTF_PACKAGE_ARG_TYPE_FLOAT : \
605 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
607 CBPRINTF_PACKAGE_ARG_TYPE_DOUBLE : \
608 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
609 long double > ::value ? \
610 CBPRINTF_PACKAGE_ARG_TYPE_LONG_DOUBLE : \
611 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
612 char * > :: value ? \
613 CBPRINTF_PACKAGE_ARG_TYPE_PTR_CHAR : \
614 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
615 const char * > :: value ? \
616 CBPRINTF_PACKAGE_ARG_TYPE_PTR_CHAR : \
617 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
618 volatile char * > :: value ? \
619 CBPRINTF_PACKAGE_ARG_TYPE_PTR_CHAR : \
620 (z_cbprintf_cxx_is_same_type < Z_CBPRINTF_ARG_REMOVE_QUAL(arg), \
621 const volatile char * > :: value ? \
622 CBPRINTF_PACKAGE_ARG_TYPE_PTR_CHAR : \
623 (Z_CBPRINTF_CXX_ARG_IS_CHAR_ARRAY(arg) ? \
624 CBPRINTF_PACKAGE_ARG_TYPE_PTR_CHAR : \
625 CBPRINTF_PACKAGE_ARG_TYPE_PTR_VOID))))))))))))))))))
627#define Z_CBPRINTF_ARG_TYPE(arg) \
629 char : CBPRINTF_PACKAGE_ARG_TYPE_CHAR, \
630 unsigned char : CBPRINTF_PACKAGE_ARG_TYPE_UNSIGNED_CHAR, \
631 short : CBPRINTF_PACKAGE_ARG_TYPE_SHORT, \
632 unsigned short : CBPRINTF_PACKAGE_ARG_TYPE_UNSIGNED_SHORT, \
633 int : CBPRINTF_PACKAGE_ARG_TYPE_INT, \
634 unsigned int : CBPRINTF_PACKAGE_ARG_TYPE_UNSIGNED_INT, \
635 long : CBPRINTF_PACKAGE_ARG_TYPE_LONG, \
636 unsigned long : CBPRINTF_PACKAGE_ARG_TYPE_UNSIGNED_LONG, \
637 long long : CBPRINTF_PACKAGE_ARG_TYPE_LONG_LONG, \
638 unsigned long long : CBPRINTF_PACKAGE_ARG_TYPE_UNSIGNED_LONG_LONG, \
639 float : CBPRINTF_PACKAGE_ARG_TYPE_FLOAT, \
640 double : CBPRINTF_PACKAGE_ARG_TYPE_DOUBLE, \
641 long double : CBPRINTF_PACKAGE_ARG_TYPE_LONG_DOUBLE, \
642 char * : CBPRINTF_PACKAGE_ARG_TYPE_PTR_CHAR, \
643 const char * : CBPRINTF_PACKAGE_ARG_TYPE_PTR_CHAR, \
644 void * : CBPRINTF_PACKAGE_ARG_TYPE_PTR_VOID, \
646 CBPRINTF_PACKAGE_ARG_TYPE_PTR_VOID \
650#define Z_CBPRINTF_TAGGED_EMPTY_ARGS(...) \
651 CBPRINTF_PACKAGE_ARG_TYPE_END
653#define Z_CBPRINTF_TAGGED_ARGS_3(arg) \
654 Z_CBPRINTF_ARG_TYPE(arg), arg
656#define Z_CBPRINTF_TAGGED_ARGS_2(...) \
657 FOR_EACH(Z_CBPRINTF_TAGGED_ARGS_3, (,), __VA_ARGS__), \
658 CBPRINTF_PACKAGE_ARG_TYPE_END
660#define Z_CBPRINTF_TAGGED_ARGS(_num_args, ...) \
661 COND_CODE_0(_num_args, \
662 (CBPRINTF_PACKAGE_ARG_TYPE_END), \
663 (Z_CBPRINTF_TAGGED_ARGS_2(__VA_ARGS__)))
#define ALWAYS_INLINE
Definition: common.h:124
struct k_pipe p
Definition: kobject.c:1322