Skip to content

Commit 00895ef

Browse files
authored
[libc] Extend the baremetal I/O vendor ABI (#98683)
This refines and extends the external ABI for I/O, later changes will update the baremetal implementations of I/O functions to use these.
1 parent fc9cd32 commit 00895ef

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

libc/src/__support/OSUtil/baremetal/io.cpp

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,58 @@
1313

1414
namespace LIBC_NAMESPACE_DECL {
1515

16-
// This is intended to be provided by the vendor.
16+
// These are intended to be provided by the vendor.
17+
//
18+
// The signature of these types and functions intentionally match `fopencookie`
19+
// which allows the following:
20+
//
21+
// ```
22+
// struct __llvm_libc_stdio_cookie { ... };
23+
// ...
24+
// struct __llvm_libc_stdio_cookie __llvm_libc_stdin_cookie;
25+
// cookie_io_functions_t stdin_func = { .read = __llvm_libc_stdio_read };
26+
// FILE *stdin = fopencookie(&__llvm_libc_stdin_cookie, "r", stdin_func);
27+
// ...
28+
// struct __llvm_libc_stdio_cookie __llvm_libc_stdout_cookie;
29+
// cookie_io_functions_t stdout_func = { .write = __llvm_libc_stdio_write };
30+
// FILE *stdout = fopencookie(&__llvm_libc_stdout_cookie, "w", stdout_func);
31+
// ...
32+
// struct __llvm_libc_stdio_cookie __llvm_libc_stderr_cookie;
33+
// cookie_io_functions_t stderr_func = { .write = __llvm_libc_stdio_write };
34+
// FILE *stderr = fopencookie(&__llvm_libc_stderr_cookie, "w", stderr_func);
35+
// ```
36+
//
37+
// At the same time, implementation of functions like `printf` and `scanf` can
38+
// use `__llvm_libc_stdio_read` and `__llvm_libc_stdio_write` directly to avoid
39+
// the extra indirection.
40+
//
41+
// All three symbols `__llvm_libc_stdin_cookie`, `__llvm_libc_stdout_cookie`,
42+
// and `__llvm_libc_stderr_cookie` must be provided, even if they don't point
43+
// at anything.
1744

18-
extern struct __llvm_libc_stdin __llvm_libc_stdin;
19-
extern "C" ssize_t __llvm_libc_stdin_read(void *cookie, char *buf, size_t size);
45+
struct __llvm_libc_stdio_cookie;
2046

21-
extern "C" void __llvm_libc_log_write(const char *msg, size_t len);
47+
extern struct __llvm_libc_stdio_cookie __llvm_libc_stdin_cookie;
48+
extern struct __llvm_libc_stdio_cookie __llvm_libc_stdout_cookie;
49+
extern struct __llvm_libc_stdio_cookie __llvm_libc_stderr_cookie;
50+
51+
extern "C" ssize_t __llvm_libc_stdio_read(void *cookie, char *buf, size_t size);
52+
extern "C" ssize_t __llvm_libc_stdio_write(void *cookie, const char *buf,
53+
size_t size);
2254

2355
ssize_t read_from_stdin(char *buf, size_t size) {
24-
return __llvm_libc_stdin_read(reinterpret_cast<void *>(&__llvm_libc_stdin),
56+
return __llvm_libc_stdio_read(static_cast<void *>(&__llvm_libc_stdin_cookie),
2557
buf, size);
2658
}
2759

60+
void write_to_stdout(cpp::string_view msg) {
61+
__llvm_libc_stdio_write(static_cast<void *>(&__llvm_libc_stdout_cookie),
62+
msg.data(), msg.size());
63+
}
64+
2865
void write_to_stderr(cpp::string_view msg) {
29-
__llvm_libc_log_write(msg.data(), msg.size());
66+
__llvm_libc_stdio_write(static_cast<void *>(&__llvm_libc_stderr_cookie),
67+
msg.data(), msg.size());
3068
}
3169

3270
} // namespace LIBC_NAMESPACE_DECL

libc/src/__support/OSUtil/baremetal/io.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace LIBC_NAMESPACE_DECL {
1818

1919
ssize_t read_from_stdin(char *buf, size_t size);
2020
void write_to_stderr(cpp::string_view msg);
21+
void write_to_stdout(cpp::string_view msg);
2122

2223
} // namespace LIBC_NAMESPACE_DECL
2324

0 commit comments

Comments
 (0)