diff --git a/libc/src/stdio/printf_core/core_structs.h b/libc/src/stdio/printf_core/core_structs.h --- a/libc/src/stdio/printf_core/core_structs.h +++ b/libc/src/stdio/printf_core/core_structs.h @@ -77,6 +77,30 @@ } }; +enum PrimaryType : uint8_t { Integer = 0, Float = 1, Pointer = 2 }; + +// TypeDesc stores the information about a type that is relevant to printf in +// a relatively compact manner. +struct TypeDesc { + uint8_t size; + PrimaryType primary_type; + LIBC_INLINE constexpr bool operator==(const TypeDesc &other) const { + return (size == other.size) && (primary_type == other.primary_type); + } +}; + +template LIBC_INLINE constexpr TypeDesc type_desc_from_type() { + if constexpr (cpp::is_same_v) { + return TypeDesc{0, PrimaryType::Integer}; + } else { + constexpr bool isPointer = cpp::is_pointer_v; + constexpr bool isFloat = cpp::is_floating_point_v; + return TypeDesc{sizeof(T), isPointer ? PrimaryType::Pointer + : isFloat ? PrimaryType::Float + : PrimaryType::Integer}; + } +} + // This is the value to be returned by conversions when no error has occurred. constexpr int WRITE_OK = 0; // These are the printf return values for when an error has occurred. They are diff --git a/libc/src/stdio/printf_core/parser.h b/libc/src/stdio/printf_core/parser.h --- a/libc/src/stdio/printf_core/parser.h +++ b/libc/src/stdio/printf_core/parser.h @@ -33,18 +33,6 @@ internal::ArgList args_start; size_t args_index = 1; - enum PrimaryType : uint8_t { Integer = 0, Float = 1, Pointer = 2 }; - - // TypeDesc stores the information about a type that is relevant to printf in - // a relatively compact manner. - struct TypeDesc { - uint8_t size; - PrimaryType primary_type; - LIBC_INLINE constexpr bool operator==(const TypeDesc &other) const { - return (size == other.size) && (primary_type == other.primary_type); - } - }; - // Defined in printf_config.h static constexpr size_t DESC_ARR_LEN = LLVM_LIBC_PRINTF_INDEX_ARR_LEN; @@ -105,19 +93,6 @@ // local_pos. size_t parse_index(size_t *local_pos); - template LIBC_INLINE static constexpr TypeDesc get_type_desc() { - if constexpr (cpp::is_same_v) { - return TypeDesc{0, PrimaryType::Integer}; - } else { - constexpr bool isPointer = cpp::is_same_v; - constexpr bool isFloat = - cpp::is_same_v || cpp::is_same_v; - return TypeDesc{sizeof(T), isPointer ? PrimaryType::Pointer - : isFloat ? PrimaryType::Float - : PrimaryType::Integer}; - } - } - LIBC_INLINE void set_type_desc(size_t index, TypeDesc value) { if (index != 0 && index <= DESC_ARR_LEN) desc_arr[index - 1] = value; @@ -130,7 +105,7 @@ if (!(index == 0 || index == args_index)) args_to_index(index); - set_type_desc(index, get_type_desc()); + set_type_desc(index, type_desc_from_type()); ++args_index; return get_next_arg_value(); diff --git a/libc/src/stdio/printf_core/parser.cpp b/libc/src/stdio/printf_core/parser.cpp --- a/libc/src/stdio/printf_core/parser.cpp +++ b/libc/src/stdio/printf_core/parser.cpp @@ -17,6 +17,7 @@ #include "src/__support/FPUtil/FPBits.h" #include "src/__support/ctype_utils.h" #include "src/__support/str_to_integer.h" +#include "src/stdio/printf_core/core_structs.h" namespace __llvm_libc { namespace printf_core { @@ -246,7 +247,7 @@ return 0; } -Parser::TypeDesc Parser::get_type_desc(size_t index) { +TypeDesc Parser::get_type_desc(size_t index) { // index mode is assumed, and the indicies start at 1, so an index // of 0 is invalid. size_t local_pos = 0; @@ -266,9 +267,9 @@ ++local_pos; size_t width_index = parse_index(&local_pos); - set_type_desc(width_index, get_type_desc()); + set_type_desc(width_index, type_desc_from_type()); if (width_index == index) - return get_type_desc(); + return type_desc_from_type(); } else if (internal::isdigit(str[local_pos])) { while (internal::isdigit(str[local_pos])) @@ -282,9 +283,9 @@ ++local_pos; size_t precision_index = parse_index(&local_pos); - set_type_desc(precision_index, get_type_desc()); + set_type_desc(precision_index, type_desc_from_type()); if (precision_index == index) - return get_type_desc(); + return type_desc_from_type(); } else if (internal::isdigit(str[local_pos])) { while (internal::isdigit(str[local_pos])) @@ -303,13 +304,13 @@ continue; } - TypeDesc conv_size = get_type_desc(); + TypeDesc conv_size = type_desc_from_type(); switch (str[local_pos]) { case ('%'): - conv_size = get_type_desc(); + conv_size = type_desc_from_type(); break; case ('c'): - conv_size = get_type_desc(); + conv_size = type_desc_from_type(); break; case ('d'): case ('i'): @@ -321,24 +322,24 @@ case (LengthModifier::hh): case (LengthModifier::h): case (LengthModifier::none): - conv_size = get_type_desc(); + conv_size = type_desc_from_type(); break; case (LengthModifier::l): - conv_size = get_type_desc(); + conv_size = type_desc_from_type(); break; case (LengthModifier::ll): case (LengthModifier::L): // This isn't in the standard, but is in other // libc implementations. - conv_size = get_type_desc(); + conv_size = type_desc_from_type(); break; case (LengthModifier::j): - conv_size = get_type_desc(); + conv_size = type_desc_from_type(); break; case (LengthModifier::z): - conv_size = get_type_desc(); + conv_size = type_desc_from_type(); break; case (LengthModifier::t): - conv_size = get_type_desc(); + conv_size = type_desc_from_type(); break; } break; @@ -352,9 +353,9 @@ case ('g'): case ('G'): if (lm != LengthModifier::L) - conv_size = get_type_desc(); + conv_size = type_desc_from_type(); else - conv_size = get_type_desc(); + conv_size = type_desc_from_type(); break; #endif // LLVM_LIBC_PRINTF_DISABLE_FLOAT #ifndef LLVM_LIBC_PRINTF_DISABLE_WRITE_INT @@ -362,10 +363,10 @@ #endif // LLVM_LIBC_PRINTF_DISABLE_WRITE_INT case ('p'): case ('s'): - conv_size = get_type_desc(); + conv_size = type_desc_from_type(); break; default: - conv_size = get_type_desc(); + conv_size = type_desc_from_type(); break; } @@ -381,7 +382,7 @@ // If there is no size for the requested index, then just guess that it's an // int. - return get_type_desc(); + return type_desc_from_type(); } void Parser::args_to_index(size_t index) { @@ -391,26 +392,26 @@ } while (args_index < index) { - Parser::TypeDesc cur_type_desc = get_type_desc(); + TypeDesc cur_type_desc = type_desc_from_type(); if (args_index <= DESC_ARR_LEN) cur_type_desc = desc_arr[args_index - 1]; - if (cur_type_desc == get_type_desc()) + if (cur_type_desc == type_desc_from_type()) cur_type_desc = get_type_desc(args_index); - if (cur_type_desc == get_type_desc()) + if (cur_type_desc == type_desc_from_type()) args_cur.next_var(); - else if (cur_type_desc == get_type_desc()) + else if (cur_type_desc == type_desc_from_type()) args_cur.next_var(); #ifndef LLVM_LIBC_PRINTF_DISABLE_FLOAT // Floating point numbers are stored separately from the other arguments. - else if (cur_type_desc == get_type_desc()) + else if (cur_type_desc == type_desc_from_type()) args_cur.next_var(); - else if (cur_type_desc == get_type_desc()) + else if (cur_type_desc == type_desc_from_type()) args_cur.next_var(); #endif // LLVM_LIBC_PRINTF_DISABLE_FLOAT // pointers may be stored separately from normal values. - else if (cur_type_desc == get_type_desc()) + else if (cur_type_desc == type_desc_from_type()) args_cur.next_var(); else args_cur.next_var();