Skip to content

Commit 167c2bc

Browse files
committed
Optimized nanoprintf function pointers, and fixed behaviour of boot_sprintf macro aliases
1 parent 8f1ca3f commit 167c2bc

File tree

6 files changed

+147
-21
lines changed

6 files changed

+147
-21
lines changed

src/libc/fprintf.src

+19
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,26 @@ if HAS_PRINTF
1010
_fprintf := __fprintf_c
1111
_vfprintf := __vfprintf_c
1212

13+
public _npf_fputc_std
14+
_npf_fputc_std:
15+
; call __frameset0
16+
; ld hl, (ix + 6)
17+
; ld de, (ix + 9)
18+
; ld (ix + 6), hl
19+
; ld (ix + 9), de
20+
; pop ix
21+
; jp _fputc
22+
ld hl, 9
23+
add hl, sp
24+
ld de, (hl)
25+
dec hl
26+
dec hl
27+
dec hl
28+
ld hl, (hl)
29+
jp _fputc
30+
1331
extern __fprintf_c
1432
extern __vfprintf_c
33+
extern _fputc
1534

1635
end if

src/libc/include/stdio.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <cdefs.h>
55
#include <stdarg.h>
66

7-
#ifndef HAS_PRINTF
7+
#if defined(HAS_PRINTF) && HAS_PRINTF == 0
88
# include <ti/sprintf.h>
99
#endif /* HAS_PRINTF */
1010

@@ -159,7 +159,7 @@ namespace std {
159159
} /* namespace std */
160160
#endif /* __cplusplus */
161161

162-
#ifndef HAS_PRINTF
162+
#if defined(HAS_PRINTF) && HAS_PRINTF == 0
163163
# define snprintf boot_snprintf
164164
# define asprintf boot_asprintf
165165
#endif /* HAS_PRINTF */

src/libc/nanoprintf.c

+32-17
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,40 @@ typedef struct {
240240
size_t cur;
241241
} npf_bufputc_ctx_t;
242242

243-
static int npf_parse_format_spec(char const *format, npf_format_spec_t *out_spec);
243+
#if 0
244+
244245
static void npf_bufputc(int c, void *ctx);
245246
static void npf_bufputc_nop(int c, void *ctx);
247+
248+
static void npf_bufputc(int c, void *ctx) {
249+
npf_bufputc_ctx_t *bpc = (npf_bufputc_ctx_t *)ctx;
250+
if (bpc->cur < bpc->len) { bpc->dst[bpc->cur++] = (char)c; }
251+
}
252+
253+
static void npf_bufputc_nop(int c, void *ctx) { (void)c; (void)ctx; }
254+
255+
static void npf_putc_std(int c, void *ctx) {
256+
(void)ctx;
257+
outchar(c);
258+
}
259+
260+
static void npf_fputc_std(int c, void *ctx) {
261+
fputc(c, (FILE*)ctx);
262+
}
263+
264+
#else
265+
266+
void npf_bufputc(int c, void *ctx);
267+
268+
void npf_bufputc_nop(int c, void *ctx) __attribute__((__const__, __leaf__, __nothrow__));
269+
270+
void npf_putc_std(int c, void *ctx);
271+
272+
void npf_fputc_std(int c, void *ctx);
273+
274+
#endif
275+
276+
static int npf_parse_format_spec(char const *format, npf_format_spec_t *out_spec);
246277
static int npf_itoa_rev(char *buf, npf_int_t i);
247278
static int npf_utoa_rev(char *buf, npf_uint_t i, unsigned base, unsigned case_adjust);
248279

@@ -629,22 +660,6 @@ static int npf_bin_len(npf_uint_t u) {
629660
}
630661
#endif
631662

632-
static void npf_bufputc(int c, void *ctx) {
633-
npf_bufputc_ctx_t *bpc = (npf_bufputc_ctx_t *)ctx;
634-
if (bpc->cur < bpc->len) { bpc->dst[bpc->cur++] = (char)c; }
635-
}
636-
637-
static void npf_bufputc_nop(int c, void *ctx) { (void)c; (void)ctx; }
638-
639-
static void npf_putc_std(int c, void *ctx) {
640-
(void)ctx;
641-
outchar(c);
642-
}
643-
644-
static void npf_fputc_std(int c, void *ctx) {
645-
fputc(c, (FILE*)ctx);
646-
}
647-
648663
typedef struct npf_cnt_putc_ctx {
649664
npf_putc pc;
650665
void *ctx;

src/libc/nanoprintf.src

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
assume adl=1
2+
3+
section .text
4+
5+
public _npf_bufputc_nop
6+
public _npf_bufputc
7+
8+
_npf_bufputc:
9+
; call __frameset0
10+
; ld iy, (ix + 9)
11+
; ld de, (iy + 6)
12+
; ld bc, (iy + 3)
13+
; push de
14+
; pop hl
15+
; or a, a
16+
; sbc hl, bc
17+
; jr nc, .finish
18+
; ld a, (ix + 6)
19+
; ld hl, (iy)
20+
; push de
21+
; pop bc
22+
; inc bc
23+
; ld (iy + 6), bc
24+
; add hl, de
25+
; ld (hl), a
26+
;.finish:
27+
; pop ix
28+
; ret
29+
ld iy, 0
30+
add iy, sp
31+
ld a, (iy + 3)
32+
ld iy, (iy + 6)
33+
ld hl, (iy + 6)
34+
ld bc, (iy + 3)
35+
sbc hl, bc ; carry not set
36+
ret nc
37+
add hl, bc
38+
ex de, hl
39+
ld hl, (iy)
40+
add hl, de
41+
inc de
42+
ld (iy + 6), de
43+
ld (hl), a
44+
_npf_bufputc_nop:
45+
ret

src/libc/printf.src

+14
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,21 @@ if HAS_PRINTF
1010
_printf := __printf_c
1111
_vprintf := __vprintf_c
1212

13+
public _npf_putc_std
14+
15+
_npf_putc_std:
16+
; call __frameset0
17+
; ld a, (ix + 6)
18+
; ld (ix + 6), a
19+
; pop ix
20+
; jp _outchar
21+
ld hl, 6
22+
add hl, sp
23+
ld a, (hl)
24+
jp _outchar
25+
1326
extern __printf_c
1427
extern __vprintf_c
28+
extern _outchar
1529

1630
end if

test/standalone/asprintf_fprintf/src/main.c

+35-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
* memccpy
2020
*/
2121

22+
#if defined(sprintf) || defined(snprintf) || defined(asprintf)
23+
# error "sprintf snprintf asprintf macro aliases need to be disabled"
24+
#endif
25+
2226
// prevents Clang from replacing function calls with builtins
2327
#if 1
2428

@@ -141,8 +145,9 @@ int nano_tests(void) {
141145
return __LINE__;
142146
}
143147
size_t buf_len = T_strlen(buf);
144-
if (buf_len != T_strlen(test_1) || buf_len != (size_t)len) {
145-
printf("E: %zu != %zu != %d\n", T_strlen(test_1), buf_len, len);
148+
const size_t test_1_len = T_strlen(test_1);
149+
if (buf_len != test_1_len || buf_len != (size_t)len) {
150+
printf("E: %zu != %zu != %d\n", test_1_len, buf_len, len);
146151
return __LINE__;
147152
}
148153
if (pos != pos_1) {
@@ -154,6 +159,34 @@ int nano_tests(void) {
154159
printf("cmp: %d\n", cmp);
155160
return __LINE__;
156161
}
162+
char append[128];
163+
int snprintf_test = snprintf(append, 20, "%s", test_1);
164+
if (snprintf_test != (int)test_1_len) {
165+
printf("sprintf_test: %d != %zu\n", snprintf_test, test_1_len);
166+
return __LINE__;
167+
}
168+
int len_2 = snprintf(append, sizeof(append), "%s", test_1);
169+
if (len_2 != (int)test_1_len) {
170+
printf("E: %d != %zu\n", len_2, test_1_len);
171+
return __LINE__;
172+
}
173+
char str2[128];
174+
char* end;
175+
end = T_stpcpy(str2, append);
176+
end = T_stpcpy(end, "");
177+
end = T_stpcpy(end, "foo");
178+
if (*end != '\0') {
179+
return __LINE__;
180+
}
181+
if (end != &str2[pos_2]) {
182+
printf("diff %p - %p = %td\n", end, str2, (ptrdiff_t)(end - str2));
183+
return __LINE__;
184+
}
185+
int cmp2 = T_strcmp(str2, test_2);
186+
if (cmp2 != 0) {
187+
printf("cmp: %d\n", cmp2);
188+
return __LINE__;
189+
}
157190
return 0;
158191
}
159192

0 commit comments

Comments
 (0)