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#define VA_STACK_MIN_ALIGN (__riscv_xlen / 8)
40#ifndef VA_STACK_MIN_ALIGN
41#define VA_STACK_MIN_ALIGN 1
45#define VA_STACK_ALIGN(type) MAX(VA_STACK_MIN_ALIGN, __alignof__(type))
48static inline void z_cbprintf_wcpy(
int *dst,
int *src,
size_t len)
50 for (
size_t i = 0; i < len; i++) {
73#define Z_CBPRINTF_VA_STACK_LL_DBL_MEMCPY 1
75#define Z_CBPRINTF_VA_STACK_LL_DBL_MEMCPY 0
85#define Z_CBPRINTF_IS_PCHAR(x, flags) \
86 z_cbprintf_cxx_is_pchar(x, (flags) & CBPRINTF_PACKAGE_CONST_CHAR_RO)
88#define Z_CBPRINTF_IS_PCHAR(x, flags) \
91 const char * : ((flags) & CBPRINTF_PACKAGE_CONST_CHAR_RO) ? 0 : 1, \
92 volatile char * : 1, \
93 const volatile char * : 1, \
95 const wchar_t * : ((flags) & CBPRINTF_PACKAGE_CONST_CHAR_RO) ? 0 : 1, \
96 volatile wchar_t * : 1, \
97 const volatile wchar_t * : 1, \
112#define Z_CBPRINTF_IS_X_PCHAR(idx, x, flags) \
113 (idx < Z_CBPRINTF_PACKAGE_FIRST_RO_STR_CNT_GET(flags) ? \
114 0 : Z_CBPRINTF_IS_PCHAR(x, flags))
124#define Z_CBPRINTF_HAS_PCHAR_ARGS(flags, fmt, ...) \
125 (FOR_EACH_IDX_FIXED_ARG(Z_CBPRINTF_IS_X_PCHAR, (+), flags, __VA_ARGS__))
127#define Z_CBPRINTF_PCHAR_COUNT(flags, ...) \
128 COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__), \
130 (Z_CBPRINTF_HAS_PCHAR_ARGS(flags, __VA_ARGS__)))
141#define Z_CBPRINTF_MUST_RUNTIME_PACKAGE(flags, ...) ({\
142 _Pragma("GCC diagnostic push") \
143 _Pragma("GCC diagnostic ignored \"-Wpointer-arith\"") \
145 if ((flags) & CBPRINTF_PACKAGE_ADD_RW_STR_POS) { \
148 _rv = Z_CBPRINTF_PCHAR_COUNT(flags, __VA_ARGS__) > 0 ? 1 : 0; \
150 _Pragma("GCC diagnostic pop")\
154#define Z_CBPRINTF_MUST_RUNTIME_PACKAGE(flags, ...) 1
167#define Z_CBPRINTF_ARG_SIZE(v) z_cbprintf_cxx_arg_size(v)
169#define Z_CBPRINTF_ARG_SIZE(v) ({\
170 __auto_type _v = (v) + 0; \
173 size_t _arg_size = _Generic((v), \
174 float : sizeof(double), \
189#define Z_CBPRINTF_STORE_ARG(buf, arg) z_cbprintf_cxx_store_arg(buf, arg)
191#define Z_CBPRINTF_STORE_ARG(buf, arg) do { \
192 if (Z_CBPRINTF_VA_STACK_LL_DBL_MEMCPY) { \
194 __auto_type _v = (arg) + 0; \
195 double _d = _Generic((arg) + 0, \
202 size_t arg_size = Z_CBPRINTF_ARG_SIZE(arg); \
203 size_t _wsize = arg_size / sizeof(int); \
204 z_cbprintf_wcpy((int *)buf, \
205 (int *) _Generic((arg) + 0, float : &_d, default : &_v), \
208 *_Generic((arg) + 0, \
210 unsigned char: (int *)buf, \
211 short : (int *)buf, \
212 unsigned short : (int *)buf, \
214 unsigned int : (unsigned int *)buf, \
215 long : (long *)buf, \
216 unsigned long : (unsigned long *)buf, \
217 long long : (long long *)buf, \
218 unsigned long long : (unsigned long long *)buf, \
219 float : (double *)buf, \
220 double : (double *)buf, \
221 long double : (long double *)buf, \
223 (const void **)buf) = arg; \
235#define Z_CBPRINTF_ALIGNMENT(_arg) z_cbprintf_cxx_alignment(_arg)
237#define Z_CBPRINTF_ALIGNMENT(_arg) \
238 MAX(_Generic((_arg) + 0, \
239 float : VA_STACK_ALIGN(double), \
240 double : VA_STACK_ALIGN(double), \
241 long double : VA_STACK_ALIGN(long double), \
242 long long : VA_STACK_ALIGN(long long), \
243 unsigned long long : VA_STACK_ALIGN(long long), \
245 __alignof__((_arg) + 0)), VA_STACK_MIN_ALIGN)
259#if defined(__x86_64__) || defined(__riscv) || defined(__aarch64__)
260#define Z_CBPRINTF_IS_LONGDOUBLE(x) 0
262#define Z_CBPRINTF_IS_LONGDOUBLE(x) z_cbprintf_cxx_is_longdouble(x)
265#define Z_CBPRINTF_IS_LONGDOUBLE(x) \
266 _Generic((x) + 0, long double : 1, default : 0)
284#define Z_CBPRINTF_PACK_ARG2(arg_idx, _buf, _idx, _align_offset, _max, _arg) \
286 BUILD_ASSERT(!((sizeof(double) < VA_STACK_ALIGN(long double)) && \
287 Z_CBPRINTF_IS_LONGDOUBLE(_arg) && \
288 !IS_ENABLED(CONFIG_CBPRINTF_PACKAGE_LONGDOUBLE)),\
289 "Packaging of long double not enabled in Kconfig."); \
290 while (_align_offset % Z_CBPRINTF_ALIGNMENT(_arg)) { \
291 _idx += sizeof(int); \
292 _align_offset += sizeof(int); \
294 uint32_t _arg_size = Z_CBPRINTF_ARG_SIZE(_arg); \
295 uint32_t _loc = _idx / sizeof(int); \
296 if (arg_idx < 1 + _fros_cnt) { \
298 _ros_pos_buf[_ros_pos_idx++] = _loc; \
300 } else if (Z_CBPRINTF_IS_PCHAR(_arg, 0)) { \
302 if (Z_CBPRINTF_IS_X_PCHAR(arg_idx, _arg, _flags)) { \
304 _rws_buffer[_rws_pos_idx++] = _loc; \
308 _ros_pos_buf[_ros_pos_idx++] = _loc; \
311 } else if (_rws_pos_en) { \
312 _rws_buffer[_rws_pos_idx++] = _idx / sizeof(int); \
315 if (_buf && _idx < (int)_max) { \
316 Z_CBPRINTF_STORE_ARG(&_buf[_idx], _arg); \
319 _align_offset += _arg_size; \
328#define Z_CBPRINTF_PACK_ARG(arg_idx, arg) \
329 Z_CBPRINTF_PACK_ARG2(arg_idx, _pbuf, _pkg_len, _pkg_offset, _pmax, arg)
337struct z_cbprintf_desc {
345union z_cbprintf_hdr {
346 struct z_cbprintf_desc desc;
356#define Z_CBPRINTF_SUPPRESS_SIZEOF_ARRAY_DECAY \
357 _Pragma("GCC diagnostic ignored \"-Wsizeof-array-decay\"")
359#define Z_CBPRINTF_SUPPRESS_SIZEOF_ARRAY_DECAY
368#ifdef CONFIG_NO_OPTIMIZATIONS
369#define Z_CBPRINTF_ON_STACK_ALLOC(_name, _len) \
370 __ASSERT(_len <= 32, "Too many string arguments."); \
371 uint8_t _name##_buf32[32]; \
372 _name = _name##_buf32
374#define Z_CBPRINTF_ON_STACK_ALLOC(_name, _len) \
375 __ASSERT(_len <= 32, "Too many string arguments."); \
376 uint8_t _name##_buf4[4]; \
377 uint8_t _name##_buf8[8]; \
378 uint8_t _name##_buf12[12]; \
379 uint8_t _name##_buf16[16]; \
380 uint8_t _name##_buf32[32]; \
381 _name = (_len) <= 4 ? _name##_buf4 : \
382 ((_len) <= 8 ? _name##_buf8 : \
383 ((_len) <= 12 ? _name##_buf12 : \
384 ((_len) <= 16 ? _name##_buf16 : \
403#define Z_CBPRINTF_STATIC_PACKAGE_GENERIC(buf, _inlen, _outlen, _align_offset, \
406 _Pragma("GCC diagnostic push") \
407 _Pragma("GCC diagnostic ignored \"-Wpointer-arith\"") \
408 Z_CBPRINTF_SUPPRESS_SIZEOF_ARRAY_DECAY \
409 BUILD_ASSERT(!IS_ENABLED(CONFIG_XTENSA) || \
410 (IS_ENABLED(CONFIG_XTENSA) && \
411 !(_align_offset % CBPRINTF_PACKAGE_ALIGNMENT)), \
412 "Xtensa requires aligned package."); \
413 BUILD_ASSERT((_align_offset % sizeof(int)) == 0, \
414 "Alignment offset must be multiply of a word."); \
415 IF_ENABLED(CONFIG_CBPRINTF_STATIC_PACKAGE_CHECK_ALIGNMENT, \
416 (__ASSERT(!((uintptr_t)buf & (CBPRINTF_PACKAGE_ALIGNMENT - 1)), \
417 "Buffer must be aligned.");)) \
418 uint32_t _flags = flags; \
419 bool _ros_pos_en = (_flags) & CBPRINTF_PACKAGE_ADD_RO_STR_POS; \
420 bool _rws_pos_en = (_flags) & CBPRINTF_PACKAGE_ADD_RW_STR_POS; \
421 bool _cros_en = (_flags) & CBPRINTF_PACKAGE_CONST_CHAR_RO; \
422 uint8_t *_pbuf = buf; \
423 uint8_t _rws_pos_idx = 0; \
424 uint8_t _ros_pos_idx = 0; \
426 uint8_t _alls_cnt = Z_CBPRINTF_PCHAR_COUNT(0, __VA_ARGS__); \
427 uint8_t _fros_cnt = Z_CBPRINTF_PACKAGE_FIRST_RO_STR_CNT_GET(_flags); \
429 uint8_t _rws_cnt = _cros_en ? \
430 Z_CBPRINTF_PCHAR_COUNT(_flags, __VA_ARGS__) : _alls_cnt - _fros_cnt; \
431 uint8_t _ros_cnt = _ros_pos_en ? (1 + _alls_cnt - _rws_cnt) : 0; \
432 uint8_t *_ros_pos_buf; \
433 Z_CBPRINTF_ON_STACK_ALLOC(_ros_pos_buf, _ros_cnt); \
434 uint8_t *_rws_buffer; \
435 Z_CBPRINTF_ON_STACK_ALLOC(_rws_buffer, _rws_cnt); \
436 size_t _pmax = (buf != NULL) ? _inlen : INT32_MAX; \
438 int _total_len = 0; \
439 int _pkg_offset = _align_offset; \
440 union z_cbprintf_hdr *_len_loc; \
442 if (_rws_cnt && !((_flags) & CBPRINTF_PACKAGE_ADD_RW_STR_POS)) { \
447 if (_pmax < sizeof(union z_cbprintf_hdr)) { \
451 _len_loc = (union z_cbprintf_hdr *)_pbuf; \
452 _pkg_len += sizeof(union z_cbprintf_hdr); \
453 _pkg_offset += sizeof(union z_cbprintf_hdr); \
455 FOR_EACH_IDX(Z_CBPRINTF_PACK_ARG, (;), __VA_ARGS__);\
456 _total_len = _pkg_len; \
458 _total_len += _ros_cnt; \
459 _total_len += _rws_cnt; \
462 uint8_t *_pbuf_loc = &_pbuf[_pkg_len]; \
463 for (size_t i = 0; i < _ros_cnt; i++) { \
464 *_pbuf_loc++ = _ros_pos_buf[i]; \
466 for (size_t i = 0; i < _rws_cnt; i++) { \
467 *_pbuf_loc++ = _rws_buffer[i]; \
471 _outlen = (_total_len > (int)_pmax) ? -ENOSPC : _total_len; \
474 union z_cbprintf_hdr hdr = { \
476 .len = (uint8_t)(_pkg_len / sizeof(int)), \
478 .ro_str_cnt = _ros_cnt, \
479 .rw_str_cnt = _rws_cnt, \
484 _Pragma("GCC diagnostic pop") \
488#define Z_CBPRINTF_STATIC_PACKAGE(packaged, inlen, outlen, align_offset, flags, \
490 Z_CBPRINTF_STATIC_PACKAGE_GENERIC(packaged, inlen, outlen, \
491 align_offset, flags, __VA_ARGS__)
493#define Z_CBPRINTF_STATIC_PACKAGE(packaged, inlen, outlen, align_offset, flags, \
497 if (((uintptr_t)packaged + 1) != 1) { \
498 outlen = cbprintf_package(packaged, inlen, flags, __VA_ARGS__); \
500 outlen = cbprintf_package(NULL, align_offset, flags, __VA_ARGS__); \
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88