Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
// Copyright © 2024, Julian Scheffers, see LICENSE for more information
#include "print.h"
#include "uart.h"
#include <printf_impl.h>
#include <string.h>
#define ERR_EXIT return -1;
// Output to UART0 callback.
static int uart0_print_cb(char const *restrict msg, size_t len, void *cookie) {
(void)cookie;
for (size_t i = 0; i < len; i++) {
UART0.fifo = msg[i];
}
return len;
}
// String callback cookie type.
typedef struct {
// Output string pointer.
char *output;
// Current length.
size_t cur_len;
// Maximum length.
size_t max_len;
} string_cookie_t;
// Output to string callback.
static int string_print_cb(char const *restrict msg, size_t len, void *__cookie) {
string_cookie_t *cookie = __cookie;
size_t i;
for (i = 0; i < cookie->max_len - cookie->cur_len && i < len; i++) {
cookie->output[i + cookie->cur_len] = msg[i];
}
cookie->cur_len += i;
return len;
}
// Write formatted output to UART0.
int printf(char const *__restrict __format, ...) {
va_list ls;
va_start(ls, __format);
int len = printf_impl(__format, ls, uart0_print_cb, NULL);
va_end(ls);
return len;
}
// Write formatted output to UART0, given va_list.
int vprintf(char const *__restrict __format, __builtin_va_list __arg) {
return printf_impl(__format, __arg, uart0_print_cb, NULL);
}
// Write a number of formatted chars to `__s`.
int snprintf(char *__restrict __s, size_t __maxlen, char const *__restrict __format, ...) {
string_cookie_t cookie = {__s, 0, __maxlen};
va_list ls;
va_start(ls, __format);
int len = printf_impl(__format, ls, string_print_cb, &cookie);
va_end(ls);
return len;
}
// Write a number of formatted chars to `__s`, given va_list.
int vsnprintf(char *__restrict __s, size_t __maxlen, char const *__restrict __format, __builtin_va_list __arg) {
string_cookie_t cookie = {__s, 0, __maxlen};
return printf_impl(__format, __arg, string_print_cb, &cookie);
}
// Type specifier.
typedef enum {
TYPESPEC_INT,
TYPESPEC_CHAR,
TYPESPEC_SHORT,
TYPESPEC_LONG,
TYPESPEC_LLONG,
TYPESPEC_INTMAX,
TYPESPEC_SIZE,
TYPESPEC_PTRDIFF,
} typespec_t;
// Read a type specifier.
static typespec_t printf_typespec(char const *__restrict *fmt) {
if (**fmt == 'l') {
++*fmt;
if (**fmt == 'l') {
++*fmt;
return TYPESPEC_LLONG;
}
return TYPESPEC_LONG;
} else if (**fmt == 'h') {
++*fmt;
if (**fmt == 'h') {
++*fmt;
return TYPESPEC_CHAR;
}
return TYPESPEC_SHORT;
} else if (**fmt == 'j') {
++*fmt;
return TYPESPEC_INTMAX;
} else if (**fmt == 'z') {
++*fmt;
return TYPESPEC_SIZE;
} else if (**fmt == 't') {
++*fmt;
return TYPESPEC_PTRDIFF;
}
return TYPESPEC_INT;
}
#define PRINTF_FLAG_LJUST 1
#define PRINTF_FLAG_SIGN 2
#define PRINTF_FLAG_SPACE_PADD 4
#define PRINTF_FLAG_RADIX 8
#define PRINTF_FLAG_ZERO_PADD 16
#define ITOA_BUF_SIZE (__INTMAX_WIDTH__ * 10 / 33 + 1)
// Handle flags.
static int printf_flags(char const *__restrict *fmt) {
int flags = 0;
while (true) {
switch (**fmt) {
case '-': flags |= PRINTF_FLAG_LJUST; break;
case '+': flags |= PRINTF_FLAG_SIGN; break;
case ' ': flags |= PRINTF_FLAG_SPACE_PADD; break;
case '#': flags |= PRINTF_FLAG_RADIX; break;
case '0': flags |= PRINTF_FLAG_ZERO_PADD; break;
default: return flags;
}
++*fmt;
}
}
// Handle %d, %i or %u.
static int printf_fmt_d(
__builtin_va_list *arg,
int flags,
size_t decimals,
typespec_t type,
bool issigned,
printf_cb_t cb,
void *__restrict cookie
) {
if (decimals < 1) {
decimals = 1;
}
// Read value using type specifier.
intmax_t val;
int len = 0;
if (issigned) {
switch (type) {
default: ERR_EXIT
case TYPESPEC_CHAR: val = va_arg(*arg, int); break;
case TYPESPEC_SHORT: val = va_arg(*arg, int); break;
case TYPESPEC_INT: val = va_arg(*arg, int); break;
case TYPESPEC_LONG: val = va_arg(*arg, long); break;
case TYPESPEC_LLONG: val = va_arg(*arg, long long); break;
case TYPESPEC_SIZE: val = va_arg(*arg, size_t); break;
case TYPESPEC_PTRDIFF: val = va_arg(*arg, ptrdiff_t); break;
case TYPESPEC_INTMAX: val = va_arg(*arg, intmax_t); break;
}
if (val < 0) {
// The negative numbers.
int rv = cb("-", 1, cookie);
if (rv == -1) {
ERR_EXIT
}
len++;
val = -val;
decimals--;
}
} else {
switch (type) {
default: ERR_EXIT
case TYPESPEC_CHAR: val = va_arg(*arg, unsigned int); break;
case TYPESPEC_SHORT: val = va_arg(*arg, unsigned int); break;
case TYPESPEC_INT: val = va_arg(*arg, unsigned int); break;
case TYPESPEC_LONG: val = va_arg(*arg, unsigned long); break;
case TYPESPEC_LLONG: val = va_arg(*arg, unsigned long long); break;
case TYPESPEC_SIZE: val = va_arg(*arg, size_t); break;
case TYPESPEC_PTRDIFF: val = va_arg(*arg, ptrdiff_t); break;
case TYPESPEC_INTMAX: val = va_arg(*arg, uintmax_t); break;
}
if (flags & PRINTF_FLAG_SIGN) {
int rv = cb("+", 2, cookie);
if (rv == -1) {
ERR_EXIT
}
len += rv;
decimals--;
}
}
// Left justification flag; padding happens outside this function.
if (flags & PRINTF_FLAG_LJUST) {
decimals = 1;
}
// Convert binary to decimal.
uintmax_t u_val = val;
char buf[ITOA_BUF_SIZE];
int real_decimals = 1;
for (size_t i = 0; i < sizeof(buf); i++) {
int c = u_val % 10;
buf[sizeof(buf) - 1 - i] = c + '0';
u_val /= 10;
if (c) {
real_decimals = i + 1;
}
}
if (decimals < real_decimals) {
decimals = real_decimals;
}
// Print extra zeroes.
char padd = (flags & PRINTF_FLAG_ZERO_PADD) ? '0' : ' ';
for (int i = 0; i + real_decimals < decimals; i++) {
if (cb(&padd, 1, cookie) == -1) {
ERR_EXIT
}
len++;
}
// Print decimal value.
int ret;
if (real_decimals < sizeof(buf)) {
ret = cb(buf + sizeof(buf) - real_decimals, real_decimals, cookie);
} else {
ret = cb(buf, sizeof(buf), cookie);
}
if (ret == -1) {
ERR_EXIT
}
return len + ret;
}
// Handle %x or %x.
static int printf_fmt_x(
__builtin_va_list *arg,
int flags,
size_t decimals,
typespec_t type,
bool isuppercase,
printf_cb_t cb,
void *__restrict cookie
) {
if (decimals < 1) {
decimals = 1;
}
int len = 0;
uintmax_t val;
switch (type) {
default: ERR_EXIT
case TYPESPEC_CHAR: val = va_arg(*arg, unsigned int); break;
case TYPESPEC_SHORT: val = va_arg(*arg, unsigned int); break;
case TYPESPEC_INT: val = va_arg(*arg, unsigned int); break;
case TYPESPEC_LONG: val = va_arg(*arg, unsigned long); break;
case TYPESPEC_LLONG: val = va_arg(*arg, unsigned long long); break;
case TYPESPEC_SIZE: val = va_arg(*arg, size_t); break;
case TYPESPEC_PTRDIFF: val = va_arg(*arg, ptrdiff_t); break;
case TYPESPEC_INTMAX: val = va_arg(*arg, uintmax_t); break;
}
// Sign flag.
if (flags & PRINTF_FLAG_SIGN) {
int rv = cb("+", 2, cookie);
if (rv == -1) {
ERR_EXIT
}
len += rv;
decimals--;
}
// Radix flag.
if (flags & PRINTF_FLAG_RADIX) {
decimals -= 2;
}
// Left justification flag; padding happens outside this function.
if (flags & PRINTF_FLAG_LJUST) {
decimals = 1;
}
// Convert binary to hexadecimal.
char buf[__INTMAX_WIDTH__ / 4];
char alpha = isuppercase ? 'A' : 'a';
int real_decimals = 1;
for (int i = 0; i < sizeof(buf); i++) {
int c = val & 15;
buf[sizeof(buf) - 1 - i] = (c > 9 ? alpha - 10 : '0') + c;
if (c) {
real_decimals = i + 1;
}
val >>= 4;
}
if (decimals < real_decimals) {
decimals = real_decimals;
}
// Radix flag.
if (flags & PRINTF_FLAG_RADIX) {
int rv = cb(isuppercase ? "0X" : "0x", 2, cookie);
if (rv == -1) {
ERR_EXIT
}
len += rv;
}
// Print extra zeroes.
char padd = (flags & PRINTF_FLAG_ZERO_PADD) ? '0' : ' ';
for (int i = 0; i + real_decimals < decimals; i++) {
if (cb(&padd, 1, cookie) == -1) {
ERR_EXIT
}
len++;
}
// Print hexadecimal value.
int ret;
if (real_decimals < sizeof(buf)) {
ret = cb(buf + sizeof(buf) - real_decimals, real_decimals, cookie);
} else {
ret = cb(buf, sizeof(buf), cookie);
}
if (ret == -1) {
ERR_EXIT
}
return len + ret;
}
// Handle a format specifier.
static int
printf_fmtspec(char const *__restrict *fmt, __builtin_va_list *arg, printf_cb_t cb, void *__restrict cookie) {
if (**fmt == '%') {
// Escaped '%'.
return cb("%", 1, cookie);
}
// Flags.
int flags = printf_flags(fmt);
// Number of decimals.
size_t decimals = 0;
if (**fmt == '0') {
ERR_EXIT
}
while (**fmt >= '0' && **fmt <= '9') {
decimals = decimals * 10 + **fmt - '0';
++*fmt;
}
// Type specifier.
typespec_t type = printf_typespec(fmt);
// Format specifier.
int rv;
if (**fmt == 'i' || **fmt == 'd') {
++*fmt;
rv = printf_fmt_d(arg, flags, decimals, type, true, cb, cookie);
} else if (**fmt == 'u') {
++*fmt;
rv = printf_fmt_d(arg, flags, decimals, type, false, cb, cookie);
} else if (**fmt == 'x') {
++*fmt;
rv = printf_fmt_x(arg, flags, decimals, type, false, cb, cookie);
} else if (**fmt == 'X') {
++*fmt;
rv = printf_fmt_x(arg, flags, decimals, type, true, cb, cookie);
} else if (**fmt == 'c') {
++*fmt;
if (type != TYPESPEC_INT) {
ERR_EXIT
}
char c = va_arg(*arg, int);
rv = cb(&c, 1, cookie);
} else if (**fmt == 's') {
++*fmt;
if (type != TYPESPEC_INT) {
ERR_EXIT
}
char const *cstr = va_arg(*arg, char const *);
rv = cb(cstr, strlen(cstr), cookie);
} else if (**fmt == 'p') {
++*fmt;
flags |= PRINTF_FLAG_RADIX;
if (type != TYPESPEC_INT) {
ERR_EXIT
}
if (!decimals) {
decimals = __SIZE_WIDTH__ / 4;
}
rv = printf_fmt_x(arg, flags, decimals, TYPESPEC_SIZE, false, cb, cookie);
}
if (rv != -1 && rv < decimals) {
for (int i = rv; i < decimals; i++) {
int tmp = cb(" ", 1, cookie);
if (tmp == -1) {
ERR_EXIT
}
}
return decimals;
}
return rv;
}
// Underlying printf implementation.
// Returns the number of successfully printed characters.
int printf_impl(char const *__restrict __format, __builtin_va_list __arg, printf_cb_t __cb, void *__restrict __cookie) {
int len = 0;
while (*__format) {
char const *next = strchr(__format, '%');
if (!next) {
// Print unformatted remainder of the string.
len += __cb(__format, strlen(__format), __cookie);
break;
}
if (next > __format) {
// Print unformatted segment.
len += __cb(__format, next - __format, __cookie);
__format = next;
}
// Handle a format specifier.
__format++;
int i = printf_fmtspec(&__format, &__arg, __cb, __cookie);
if (i < 0) {
return i;
}
len += i;
}
return len;
}
511328c3aThis file is pinned to the selected release. Download the original to open it in your design tool.