adapting va_arg parameters of printf

This commit is contained in:
georgemoralis 2023-10-06 13:33:45 +03:00
parent b9c6d9d395
commit cefd3d95ed
2 changed files with 338 additions and 273 deletions

View File

@ -51,11 +51,13 @@
#pragma once #pragma once
#include <cstdarg> #include <cstdarg>
#include <cstddef>
#include <cstdbool> #include <cstdbool>
#include <cstddef>
#include <cstdint> #include <cstdint>
namespace utils { #include "va_ctx.h"
namespace Emulator::HLE::Libraries::LibC {
// ntoa conversion buffer size, this must be big enough to hold // ntoa conversion buffer size, this must be big enough to hold
// one converted numeric number including padded zeros (dynamically created on stack) // one converted numeric number including padded zeros (dynamically created on stack)
// 32 byte is a good default // 32 byte is a good default
@ -76,7 +78,6 @@ namespace utils {
// ptrdiff_t is normally defined in <stddef.h> as long or long long type // ptrdiff_t is normally defined in <stddef.h> as long or long long type
#define PRINTF_SUPPORT_PTRDIFF_T #define PRINTF_SUPPORT_PTRDIFF_T
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// internal flag definitions // internal flag definitions
@ -93,64 +94,53 @@ namespace utils {
#define FLAGS_PRECISION (1U << 10U) #define FLAGS_PRECISION (1U << 10U)
#define FLAGS_WIDTH (1U << 11U) #define FLAGS_WIDTH (1U << 11U)
// output function type // output function type
typedef void (*out_fct_type)(char character, void* buffer, size_t idx, size_t maxlen); typedef void (*out_fct_type)(char character, void* buffer, size_t idx, size_t maxlen);
// wrapper (used as buffer) for output function type // wrapper (used as buffer) for output function type
typedef struct { typedef struct {
void (*fct)(char character, void* arg); void (*fct)(char character, void* arg);
void* arg; void* arg;
} out_fct_wrap_type; } out_fct_wrap_type;
// internal buffer output // internal buffer output
static inline void _out_buffer(char character, void* buffer, size_t idx, size_t maxlen) static inline void _out_buffer(char character, void* buffer, size_t idx, size_t maxlen) {
{
if (idx < maxlen) { if (idx < maxlen) {
((char*)buffer)[idx] = character; ((char*)buffer)[idx] = character;
} }
} }
// internal null output // internal null output
static inline void _out_null(char character, void* buffer, size_t idx, size_t maxlen) static inline void _out_null(char character, void* buffer, size_t idx, size_t maxlen) {
{ (void)character;
(void)character; (void)buffer; (void)idx; (void)maxlen; (void)buffer;
(void)idx;
(void)maxlen;
} }
// internal output function wrapper // internal output function wrapper
static inline void _out_fct(char character, void* buffer, size_t idx, size_t maxlen) static inline void _out_fct(char character, void* buffer, size_t idx, size_t maxlen) {
{ (void)idx;
(void)idx; (void)maxlen; (void)maxlen;
// buffer is the output fct pointer // buffer is the output fct pointer
((out_fct_wrap_type*)buffer)->fct(character, ((out_fct_wrap_type*)buffer)->arg); ((out_fct_wrap_type*)buffer)->fct(character, ((out_fct_wrap_type*)buffer)->arg);
} }
// internal strlen // internal strlen
// \return The length of the string (excluding the terminating 0) // \return The length of the string (excluding the terminating 0)
static inline unsigned int _strlen(const char* str) static inline unsigned int _strlen(const char* str) {
{
const char* s; const char* s;
for (s = str; *s; ++s); for (s = str; *s; ++s)
;
return (unsigned int)(s - str); return (unsigned int)(s - str);
} }
// internal test if char is a digit (0-9) // internal test if char is a digit (0-9)
// \return true if char is a digit // \return true if char is a digit
static inline bool _is_digit(char ch) static inline bool _is_digit(char ch) { return (ch >= '0') && (ch <= '9'); }
{
return (ch >= '0') && (ch <= '9');
}
// internal ASCII string to unsigned int conversion // internal ASCII string to unsigned int conversion
static inline unsigned int _atoi(const char** str) static inline unsigned int _atoi(const char** str) {
{
unsigned int i = 0U; unsigned int i = 0U;
while (_is_digit(**str)) { while (_is_digit(**str)) {
i = i * 10U + (unsigned int)(*((*str)++) - '0'); i = i * 10U + (unsigned int)(*((*str)++) - '0');
@ -158,10 +148,9 @@ static inline unsigned int _atoi(const char** str)
return i; return i;
} }
// internal itoa format // internal itoa format
static inline size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t maxlen, char* buf, size_t len, bool negative, unsigned int base, unsigned int prec, unsigned int width, unsigned int flags) static inline size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t maxlen, char* buf, size_t len, bool negative, unsigned int base,
{ unsigned int prec, unsigned int width, unsigned int flags) {
const size_t start_idx = idx; const size_t start_idx = idx;
// pad leading zeros // pad leading zeros
@ -198,11 +187,9 @@ static inline size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, si
if (len < PRINTF_NTOA_BUFFER_SIZE) { if (len < PRINTF_NTOA_BUFFER_SIZE) {
if (negative) { if (negative) {
buf[len++] = '-'; buf[len++] = '-';
} } else if (flags & FLAGS_PLUS) {
else if (flags & FLAGS_PLUS) {
buf[len++] = '+'; // ignore the space if the '+' exists buf[len++] = '+'; // ignore the space if the '+' exists
} } else if (flags & FLAGS_SPACE) {
else if (flags & FLAGS_SPACE) {
buf[len++] = ' '; buf[len++] = ' ';
} }
} }
@ -229,10 +216,9 @@ static inline size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, si
return idx; return idx;
} }
// internal itoa for 'long' type // internal itoa for 'long' type
static inline size_t _ntoa_long(out_fct_type out, char* buffer, size_t idx, size_t maxlen, unsigned long value, bool negative, unsigned long base, unsigned int prec, unsigned int width, unsigned int flags) static inline size_t _ntoa_long(out_fct_type out, char* buffer, size_t idx, size_t maxlen, unsigned long value, bool negative, unsigned long base,
{ unsigned int prec, unsigned int width, unsigned int flags) {
char buf[PRINTF_NTOA_BUFFER_SIZE]; char buf[PRINTF_NTOA_BUFFER_SIZE];
size_t len = 0U; size_t len = 0U;
@ -248,11 +234,10 @@ static inline size_t _ntoa_long(out_fct_type out, char* buffer, size_t idx, size
return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, (unsigned int)base, prec, width, flags); return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, (unsigned int)base, prec, width, flags);
} }
// internal itoa for 'long long' type // internal itoa for 'long long' type
#if defined(PRINTF_SUPPORT_LONG_LONG) #if defined(PRINTF_SUPPORT_LONG_LONG)
static inline size_t _ntoa_long_long(out_fct_type out, char* buffer, size_t idx, size_t maxlen, unsigned long long value, bool negative, unsigned long long base, unsigned int prec, unsigned int width, unsigned int flags) static inline size_t _ntoa_long_long(out_fct_type out, char* buffer, size_t idx, size_t maxlen, unsigned long long value, bool negative,
{ unsigned long long base, unsigned int prec, unsigned int width, unsigned int flags) {
char buf[PRINTF_NTOA_BUFFER_SIZE]; char buf[PRINTF_NTOA_BUFFER_SIZE];
size_t len = 0U; size_t len = 0U;
@ -269,10 +254,9 @@ static inline size_t _ntoa_long_long(out_fct_type out, char* buffer, size_t idx,
} }
#endif // PRINTF_SUPPORT_LONG_LONG #endif // PRINTF_SUPPORT_LONG_LONG
#if defined(PRINTF_SUPPORT_FLOAT) #if defined(PRINTF_SUPPORT_FLOAT)
static inline size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags) static inline size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width,
{ unsigned int flags) {
char buf[PRINTF_FTOA_BUFFER_SIZE]; char buf[PRINTF_FTOA_BUFFER_SIZE];
size_t len = 0U; size_t len = 0U;
double diff = 0.0; double diff = 0.0;
@ -312,8 +296,7 @@ static inline size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t ma
frac = 0; frac = 0;
++whole; ++whole;
} }
} } else if ((diff == 0.5) && ((frac == 0U) || (frac & 1U))) {
else if ((diff == 0.5) && ((frac == 0U) || (frac & 1U))) {
// if halfway, round up if odd, OR if last digit is 0 // if halfway, round up if odd, OR if last digit is 0
++frac; ++frac;
} }
@ -329,14 +312,12 @@ static inline size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t ma
if (diff > 0.5) { if (diff > 0.5) {
// greater than 0.5, round up, e.g. 1.6 -> 2 // greater than 0.5, round up, e.g. 1.6 -> 2
++whole; ++whole;
} } else if ((diff == 0.5) && (whole & 1)) {
else if ((diff == 0.5) && (whole & 1)) {
// exactly 0.5 and ODD, then round up // exactly 0.5 and ODD, then round up
// 1.5 -> 2, but 2.5 -> 2 // 1.5 -> 2, but 2.5 -> 2
++whole; ++whole;
} }
} } else {
else {
unsigned int count = prec; unsigned int count = prec;
// now do fractional part, as an unsigned number // now do fractional part, as an unsigned number
while (len < PRINTF_FTOA_BUFFER_SIZE) { while (len < PRINTF_FTOA_BUFFER_SIZE) {
@ -376,11 +357,9 @@ static inline size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t ma
if (len < PRINTF_FTOA_BUFFER_SIZE) { if (len < PRINTF_FTOA_BUFFER_SIZE) {
if (negative) { if (negative) {
buf[len++] = '-'; buf[len++] = '-';
} } else if (flags & FLAGS_PLUS) {
else if (flags & FLAGS_PLUS) {
buf[len++] = '+'; // ignore the space if the '+' exists buf[len++] = '+'; // ignore the space if the '+' exists
} } else if (flags & FLAGS_SPACE) {
else if (flags & FLAGS_SPACE) {
buf[len++] = ' '; buf[len++] = ' ';
} }
} }
@ -408,9 +387,9 @@ static inline size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t ma
} }
#endif // PRINTF_SUPPORT_FLOAT #endif // PRINTF_SUPPORT_FLOAT
#if 0
// internal vsnprintf // internal vsnprintf
static inline int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const char* format, CPUState &cpu, MemState &mem, module::vargs &va) static inline int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const char* format, VaList* va_list)
{ {
unsigned int flags, width, precision, n; unsigned int flags, width, precision, n;
size_t idx = 0U; size_t idx = 0U;
@ -453,7 +432,8 @@ static inline int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen
width = _atoi(&format); width = _atoi(&format);
} }
else if (*format == '*') { else if (*format == '*') {
const int w = va.next<int>(cpu, mem); const int w = vaArgInteger(va_list); // const int w = va.next<int>(cpu, mem);
if (w < 0) { if (w < 0) {
flags |= FLAGS_LEFT; // reverse padding flags |= FLAGS_LEFT; // reverse padding
width = (unsigned int)-w; width = (unsigned int)-w;
@ -473,7 +453,7 @@ static inline int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen
precision = _atoi(&format); precision = _atoi(&format);
} }
else if (*format == '*') { else if (*format == '*') {
precision = (unsigned int)va.next<int>(cpu, mem); precision = vaArgInteger(va_list); // precision = (unsigned int)va.next<int>(cpu, mem);
format++; format++;
} }
} }
@ -554,16 +534,19 @@ static inline int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen
// signed // signed
if (flags & FLAGS_LONG_LONG) { if (flags & FLAGS_LONG_LONG) {
#if defined(PRINTF_SUPPORT_LONG_LONG) #if defined(PRINTF_SUPPORT_LONG_LONG)
const long long value = va.next<long long>(cpu, mem); auto value = vaArgLongLong(va_list); // const long long value = va.next<long long>(cpu, mem);
idx = _ntoa_long_long(out, buffer, idx, maxlen, (unsigned long long)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags); idx = _ntoa_long_long(out, buffer, idx, maxlen, (unsigned long long)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags);
#endif #endif
} }
else if (flags & FLAGS_LONG) { else if (flags & FLAGS_LONG) {
const long value = va.next<long>(cpu, mem); auto value = vaArgLong(va_list); // const long value = va.next<long>(cpu, mem);
idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags); idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags);
} }
else { else {
const int value = (flags & FLAGS_CHAR) ? (char)va.next<int>(cpu, mem) : (flags & FLAGS_SHORT) ? (short int)va.next<int>(cpu, mem): va.next<int>(cpu, mem); //const int value = (flags & FLAGS_CHAR) ? (char)va.next<int>(cpu, mem) : (flags & FLAGS_SHORT) ? (short int)va.next<int>(cpu, mem): va.next<int>(cpu, mem);
const int value = (flags & FLAGS_CHAR) ? static_cast<char>(vaArgInteger(va_list))
: (flags & FLAGS_SHORT) ? static_cast<int16_t>(vaArgInteger(va_list))
: vaArgInteger(va_list);
idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned int)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags); idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned int)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags);
} }
} }
@ -571,15 +554,20 @@ static inline int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen
// unsigned // unsigned
if (flags & FLAGS_LONG_LONG) { if (flags & FLAGS_LONG_LONG) {
#if defined(PRINTF_SUPPORT_LONG_LONG) #if defined(PRINTF_SUPPORT_LONG_LONG)
idx = _ntoa_long_long(out, buffer, idx, maxlen, va.next<unsigned long long>(cpu, mem), false, base, precision, width, flags); //idx = _ntoa_long_long(out, buffer, idx, maxlen, va.next<unsigned long long>(cpu, mem), false, base, precision, width, flags);
idx = _ntoa_long_long(out, buffer, idx, maxlen, static_cast<u64>(vaArgLongLong(va_list)), false, base, precision, width, flags);
#endif #endif
} }
else if (flags & FLAGS_LONG) { else if (flags & FLAGS_LONG) {
idx = _ntoa_long(out, buffer, idx, maxlen, va.next<unsigned long>(cpu, mem), false, base, precision, width, flags); // idx = _ntoa_long(out, buffer, idx, maxlen, va.next<unsigned long>(cpu, mem), false, base, precision, width, flags);
idx = _ntoa_long(out, buffer, idx, maxlen, static_cast<u32>(vaArgLong(va_list)), false, base, precision, width, flags);
} }
else { else {
const unsigned int value = (flags & FLAGS_CHAR) ? (unsigned char)va.next<unsigned int>(cpu, mem) : (flags & FLAGS_SHORT) ? //const unsigned int value = (flags & FLAGS_CHAR) ? (unsigned char)va.next<unsigned int>(cpu, mem) : (flags & FLAGS_SHORT) ?
(unsigned short int)va.next<unsigned int>(cpu, mem) : va.next<unsigned int>(cpu, mem); // (unsigned short int)va.next<unsigned int>(cpu, mem) : va.next<unsigned int>(cpu, mem);
const unsigned int value = (flags & FLAGS_CHAR) ? static_cast<u08>(vaArgInteger(va_list))
: (flags & FLAGS_SHORT) ? static_cast<u16>(vaArgInteger(va_list))
: static_cast<u32>(vaArgInteger(va_list));
idx = _ntoa_long(out, buffer, idx, maxlen, value, false, base, precision, width, flags); idx = _ntoa_long(out, buffer, idx, maxlen, value, false, base, precision, width, flags);
} }
} }
@ -589,7 +577,8 @@ static inline int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen
#if defined(PRINTF_SUPPORT_FLOAT) #if defined(PRINTF_SUPPORT_FLOAT)
case 'f' : case 'f' :
case 'F' : case 'F' :
idx = _ftoa(out, buffer, idx, maxlen, va.next<double>(cpu, mem), precision, width, flags); //idx = _ftoa(out, buffer, idx, maxlen, va.next<double>(cpu, mem), precision, width, flags);
idx = _ftoa(out, buffer, idx, maxlen, vaArgDouble(va_list), precision, width, flags);
format++; format++;
break; break;
#endif // PRINTF_SUPPORT_FLOAT #endif // PRINTF_SUPPORT_FLOAT
@ -602,7 +591,8 @@ static inline int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen
} }
} }
// char output // char output
out((char)va.next<int>(cpu, mem), buffer, idx++, maxlen); //out((char)va.next<int>(cpu, mem), buffer, idx++, maxlen);
out(static_cast<char>(vaArgInteger(va_list)), buffer, idx++, maxlen);
// post padding // post padding
if (flags & FLAGS_LEFT) { if (flags & FLAGS_LEFT) {
while (l++ < width) { while (l++ < width) {
@ -614,7 +604,7 @@ static inline int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen
} }
case 's' : { case 's' : {
const char *p = va.next<Ptr<char>>(cpu, mem).get(mem); const char* p = vaArgPtr<const char>(va_list); // const char *p = va.next<Ptr<char>>(cpu, mem).get(mem);
p = p != nullptr ? p : "(null)"; p = p != nullptr ? p : "(null)";
unsigned int l = _strlen(p); unsigned int l = _strlen(p);
// pre padding // pre padding
@ -646,11 +636,15 @@ static inline int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen
#if defined(PRINTF_SUPPORT_LONG_LONG) #if defined(PRINTF_SUPPORT_LONG_LONG)
const bool is_ll = sizeof(uintptr_t) == sizeof(long long); const bool is_ll = sizeof(uintptr_t) == sizeof(long long);
if (is_ll) { if (is_ll) {
idx = _ntoa_long_long(out, buffer, idx, maxlen, (uintptr_t)va.next<Ptr<void>>(cpu, mem).address(), false, 16U, precision, width, flags); //idx = _ntoa_long_long(out, buffer, idx, maxlen, (uintptr_t)va.next<Ptr<void>>(cpu, mem).address(), false, 16U, precision, width, flags);
idx = _ntoa_long_long(out, buffer, idx, maxlen, reinterpret_cast<uintptr_t>(vaArgPtr<void>(va_list)), false, 16U, precision, width, flags);
} }
else { else {
#endif #endif
idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)((uintptr_t)va.next<Ptr<void>>(cpu, mem).address()), false, 16U, precision, width, flags); //idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)((uintptr_t)va.next<Ptr<void>>(cpu, mem).address()), false, 16U, precision, width, flags);
idx = _ntoa_long(out, buffer, idx, maxlen, static_cast<uint32_t>(reinterpret_cast<uintptr_t>(vaArgPtr<void>(va_list))), false, 16U,
precision, width,
flags);
#if defined(PRINTF_SUPPORT_LONG_LONG) #if defined(PRINTF_SUPPORT_LONG_LONG)
} }
#endif #endif
@ -677,6 +671,7 @@ static inline int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen
return (int)idx; return (int)idx;
} }
#if 0
/////////////////////////////////////////////////////// ///////////////////////////////////////////////////////
/** /**
@ -708,4 +703,4 @@ inline int snprintf(char* buffer, size_t count, const char* format, CPUState &cp
#endif #endif
} } // namespace Emulator::HLE::Libraries::LibC

View File

@ -0,0 +1,70 @@
#include <types.h>
namespace Emulator::HLE::Libraries::LibC {
// https://stackoverflow.com/questions/4958384/what-is-the-format-of-the-x86-64-va-list-structure
struct VaList {
u32 gp_offset;
u32 fp_offset;
void* overflow_arg_area;
void* reg_save_area;
};
template <class T, uint32_t Size>
T vaArgRegSaveAreaGp(VaList* l) {
auto* addr = reinterpret_cast<T*>(static_cast<uint8_t*>(l->reg_save_area) + l->gp_offset);
l->gp_offset += Size;
return *addr;
}
template <class T, uint64_t Align, uint64_t Size>
T vaArgOverflowArgArea(VaList* l) {
auto ptr = ((reinterpret_cast<uint64_t>(l->overflow_arg_area) + (Align - 1)) & ~(Align - 1));
auto* addr = reinterpret_cast<T*>(ptr);
l->overflow_arg_area = reinterpret_cast<void*>(ptr + Size);
return *addr;
}
template <class T, uint32_t Size>
T vaArgRegSaveAreaFp(VaList* l) {
auto* addr = reinterpret_cast<T*>(static_cast<uint8_t*>(l->reg_save_area) + l->fp_offset);
l->fp_offset += Size;
return *addr;
}
inline int vaArgInteger(VaList* l) {
if (l->gp_offset <= 40) {
return vaArgRegSaveAreaGp<int, 8>(l);
}
return vaArgOverflowArgArea<int, 1, 8>(l);
}
inline long long vaArgLongLong(VaList* l) {
if (l->gp_offset <= 40) {
return vaArgRegSaveAreaGp<long long, 8>(l);
}
return vaArgOverflowArgArea<long long, 1, 8>(l);
}
inline long vaArgLong(VaList* l) {
if (l->gp_offset <= 40) {
return vaArgRegSaveAreaGp<long, 8>(l);
}
return vaArgOverflowArgArea<long, 1, 8>(l);
}
inline double vaArgDouble(VaList* l) {
if (l->fp_offset <= 160) {
return vaArgRegSaveAreaFp<double, 16>(l);
}
return vaArgOverflowArgArea<double, 1, 8>(l);
}
template <class T>
T* vaArgPtr(VaList* l) {
if (l->gp_offset <= 40) {
return vaArgRegSaveAreaGp<T*, 8>(l);
}
return vaArgOverflowArgArea<T*, 1, 8>(l);
}
} // namespace Emulator::HLE::Libraries::LibC