Skip to content

Commit

Permalink
support "zx" for print format
Browse files Browse the repository at this point in the history
  • Loading branch information
tyfkda committed May 30, 2024
1 parent d711c1f commit 77b0dc5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 40 deletions.
66 changes: 28 additions & 38 deletions libsrc/stdio/vfprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ int vfprintf(FILE *fp, const char *fmt_, va_list ap) {
int order = 0, suborder = 0;
bool sign = false;
bool leftalign = false;
bool sizez = false;
int nlong = 0;
c = fmt[++i];

Expand Down Expand Up @@ -265,16 +266,23 @@ int vfprintf(FILE *fp, const char *fmt_, va_list ap) {
nlong = 2;
c = fmt[++i];
}
} else if (c == 'z') {
sizez = true;
c = fmt[++i];
}

switch (c) {
case 'd':
{
long long x;
switch (nlong) {
case 0: x = va_arg(ap, int); break;
case 1: x = va_arg(ap, long); break;
default: x = va_arg(ap, long long); break; // case 2:
if (sizez) {
x = va_arg(ap, ssize_t);
} else {
switch (nlong) {
case 0: x = va_arg(ap, int); break;
case 1: x = va_arg(ap, long); break;
default: x = va_arg(ap, long long); break; // case 2:
}
}
bool negative = x < 0;
unsigned long long ux = negative ? -x : x;
Expand All @@ -287,51 +295,33 @@ int vfprintf(FILE *fp, const char *fmt_, va_list ap) {
case 'u':
{
unsigned long long x;
switch (nlong) {
case 0: x = va_arg(ap, unsigned int); break;
case 1: x = va_arg(ap, unsigned long); break;
default: x = va_arg(ap, unsigned long long); break; // case 2:
if (sizez) {
x = va_arg(ap, size_t);
} else {
switch (nlong) {
case 0: x = va_arg(ap, unsigned int); break;
case 1: x = va_arg(ap, unsigned long); break;
default: x = va_arg(ap, unsigned long long); break; // case 2:
}
}
char *p = snprintullong2(bufend, x, 10, kHexDigits);
if (sign)
*(--p) = '+';
o += snprintstr(fp, p, order, suborder, leftalign, padding);
}
break;
case 'z':
{
switch (fmt[i + 1]) {
case 'u':
{
++i;
size_t x = va_arg(ap, size_t);
o += snprintullong(fp, x, 10, kHexDigits, order, padding);
}
break;
case 'd':
{
++i;
ssize_t x = va_arg(ap, ssize_t);
bool negative = x < 0;
size_t ux = negative ? -x : x;
if (negative || order > 0 || sign)
o += sprintsign(fp, negative, sign, &order);
char *p = snprintullong2(bufend, ux, 10, kHexDigits);
o += snprintstr(fp, p, order, suborder, leftalign, padding);
}
break;
default: break; // TODO: error
}
}
break;
case 'x': case 'X':
{
const char *digits = c == 'x' ? kHexDigits : kUpperHexDigits;
unsigned long long x;
switch (nlong) {
case 0: x = va_arg(ap, unsigned int); break;
case 1: x = va_arg(ap, unsigned long); break;
default: x = va_arg(ap, unsigned long long); break; // case 2:
if (sizez) {
x = va_arg(ap, size_t);
} else {
switch (nlong) {
case 0: x = va_arg(ap, unsigned int); break;
case 1: x = va_arg(ap, unsigned long); break;
default: x = va_arg(ap, unsigned long long); break; // case 2:
}
}
o += snprintullong(fp, x, 16, digits, order, padding);
}
Expand Down
1 change: 1 addition & 0 deletions libsrc/tests/printf_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ TEST(vsnprintf) {
EXPECT("PRIuMAX:18446744073709551615", "PRIuMAX:%" PRIuMAX, UINTMAX_MAX);
}
EXPECT("size_t:54321, ssize_t:-54321", "size_t:%zu, ssize_t:%zd", (size_t)54321, (ssize_t)-54321);
EXPECT("size_t:0x54321", "size_t:0x%zx", (size_t)0x54321);
EXPECT("PRId64:1311768467463790320", "PRId64:%" PRId64, (int64_t)0x123456789abcdef0);
EXPECT("PRIu64:18364758544493064720", "PRIu64:%" PRIu64, (uint64_t)0xfedcba9876543210);

Expand Down
2 changes: 1 addition & 1 deletion src/wcc/emit_wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ static Vector *emit_data_section(EmitWasm *ew) {
data_push(&datasec, 0); // flags
// Init (address).
uint32_t address = segment->gvarinfo->non_prim.address;
VERBOSE("%04x: %.*s (size=%zu)\n", address, NAMES(segment->gvarinfo->varinfo->name),
VERBOSE("%04x: %.*s (size=0x%zx)\n", address, NAMES(segment->gvarinfo->varinfo->name),
type_size(segment->gvarinfo->varinfo->type));
data_push(&datasec, OP_I32_CONST);
data_leb128(&datasec, -1, address);
Expand Down
2 changes: 1 addition & 1 deletion src/wcc/traverse.c
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ void traverse_ast(Vector *decls) {
info->non_prim.address = address;
size_t size = type_size(varinfo->type);
address += size;
VERBOSE("%04x: %.*s (size=0x%lx)\n", info->non_prim.address, NAMES(varinfo->name), size);
VERBOSE("%04x: %.*s (size=0x%zx)\n", info->non_prim.address, NAMES(varinfo->name), size);
}
}
}
Expand Down

0 comments on commit 77b0dc5

Please sign in to comment.