Skip to content

Commit 66e8d61

Browse files
committed
[libc] Migrate from baremetal stdio.h to generic stdio.h
This is a follow up to the RFC here: https://discourse.llvm.org/t/rfc-implementation-of-stdio-on-baremetal/86944 Currently, this is very rough and this PR is only for feedback. This provides the stdout/stderr/stdin symbols (which now don't have to provided by the user). This allows the user to have access to all functions, currently I've only tested `fprintf` but in theory everything that works in the generic folder should work in the baremetal configuration. The most important part of the implementation is: - where we want to put `std*`. Here it is a part of `libc.a` - if we want it here, what should we set it to? I have aliased all the streams to a _non-buffered_ stream, which does NOT require flushing. It is based on the CookieFile that already existed
1 parent c39b1ae commit 66e8d61

File tree

12 files changed

+72
-359
lines changed

12 files changed

+72
-359
lines changed

libc/config/baremetal/aarch64/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ set(TARGET_LIBC_ENTRYPOINTS
124124

125125
# stdio.h entrypoints
126126
libc.src.stdio.asprintf
127+
libc.src.stdio.fopencookie
128+
libc.src.stdio.fprintf
127129
libc.src.stdio.getchar
128130
libc.src.stdio.printf
129131
libc.src.stdio.putchar

libc/config/baremetal/arm/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ set(TARGET_LIBC_ENTRYPOINTS
124124

125125
# stdio.h entrypoints
126126
libc.src.stdio.asprintf
127+
libc.src.stdio.fopencookie
128+
libc.src.stdio.fprintf
127129
libc.src.stdio.getchar
128130
libc.src.stdio.printf
129131
libc.src.stdio.putchar

libc/config/baremetal/riscv/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ set(TARGET_LIBC_ENTRYPOINTS
124124

125125
# stdio.h entrypoints
126126
libc.src.stdio.asprintf
127+
libc.src.stdio.fopencookie
128+
libc.src.stdio.fprintf
127129
libc.src.stdio.getchar
128130
libc.src.stdio.printf
129131
libc.src.stdio.putchar

libc/src/__support/File/CMakeLists.txt

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ add_object_library(
2222
libc.src.__support.error_or
2323
)
2424

25+
add_header_library(
26+
cookie_file
27+
HDRS
28+
cookie_file.h
29+
DEPENDS
30+
.file
31+
libc.hdr.errno_macros
32+
libc.hdr.stdio_macros
33+
libc.hdr.types.FILE
34+
libc.hdr.types.cookie_io_functions_t
35+
libc.src.__support.CPP.new
36+
libc.src.__support.File.file
37+
)
38+
2539
add_object_library(
2640
dir
2741
SRCS
@@ -41,10 +55,17 @@ endif()
4155

4256
add_subdirectory(${LIBC_TARGET_OS})
4357

44-
set(target_file libc.src.__support.File.${LIBC_TARGET_OS}.file)
45-
set(target_stdout libc.src.__support.File.${LIBC_TARGET_OS}.stdout)
46-
set(target_stderr libc.src.__support.File.${LIBC_TARGET_OS}.stderr)
47-
set(target_stdin libc.src.__support.File.${LIBC_TARGET_OS}.stdin)
58+
if(LIBC_TARGET_OS_IS_BAREMETAL)
59+
set(target_file libc.src.__support.File.cookie_file)
60+
set(target_stdout libc.src.__support.File.${LIBC_TARGET_OS}.stderr)
61+
set(target_stderr libc.src.__support.File.${LIBC_TARGET_OS}.stderr)
62+
set(target_stdin libc.src.__support.File.${LIBC_TARGET_OS}.stderr)
63+
else()
64+
set(target_file libc.src.__support.File.${LIBC_TARGET_OS}.file)
65+
set(target_stdout libc.src.__support.File.${LIBC_TARGET_OS}.stdout)
66+
set(target_stderr libc.src.__support.File.${LIBC_TARGET_OS}.stderr)
67+
set(target_stdin libc.src.__support.File.${LIBC_TARGET_OS}.stdin)
68+
endif()
4869

4970
set(file_targets "${target_file};${target_stdout};${target_stdin};${target_stderr}")
5071
set(file_aliases "platform_file;platform_stdout;platform_stdin;platform_stderr")

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

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,6 @@
1313

1414
namespace LIBC_NAMESPACE_DECL {
1515

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.
44-
45-
struct __llvm_libc_stdio_cookie;
46-
47-
extern "C" struct __llvm_libc_stdio_cookie __llvm_libc_stdin_cookie;
48-
extern "C" struct __llvm_libc_stdio_cookie __llvm_libc_stdout_cookie;
49-
extern "C" 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);
54-
5516
ssize_t read_from_stdin(char *buf, size_t size) {
5617
return __llvm_libc_stdio_read(static_cast<void *>(&__llvm_libc_stdin_cookie),
5718
buf, size);

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,46 @@
1414
#include "src/__support/CPP/string_view.h"
1515
#include "src/__support/macros/config.h"
1616

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

47+
struct __llvm_libc_stdio_cookie;
48+
49+
extern "C" struct __llvm_libc_stdio_cookie __llvm_libc_stdin_cookie;
50+
extern "C" struct __llvm_libc_stdio_cookie __llvm_libc_stdout_cookie;
51+
extern "C" struct __llvm_libc_stdio_cookie __llvm_libc_stderr_cookie;
52+
53+
extern "C" ssize_t __llvm_libc_stdio_read(void *cookie, char *buf, size_t size);
54+
extern "C" ssize_t __llvm_libc_stdio_write(void *cookie, const char *buf,
55+
size_t size);
56+
1957
ssize_t read_from_stdin(char *buf, size_t size);
2058
void write_to_stderr(cpp::string_view msg);
2159
void write_to_stdout(cpp::string_view msg);
Lines changed: 0 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
add_entrypoint_object(
2-
getchar
3-
SRCS
4-
getchar.cpp
5-
HDRS
6-
../getchar.h
7-
DEPENDS
8-
libc.hdr.stdio_macros
9-
libc.src.__support.OSUtil.osutil
10-
libc.src.__support.CPP.string_view
11-
)
12-
131
add_entrypoint_object(
142
remove
153
SRCS
@@ -19,87 +7,3 @@ add_entrypoint_object(
197
DEPENDS
208
libc.include.stdio
219
)
22-
23-
add_entrypoint_object(
24-
printf
25-
SRCS
26-
printf.cpp
27-
HDRS
28-
../printf.h
29-
DEPENDS
30-
libc.src.stdio.printf_core.printf_main
31-
libc.src.stdio.printf_core.writer
32-
libc.src.__support.arg_list
33-
libc.src.__support.OSUtil.osutil
34-
)
35-
36-
add_entrypoint_object(
37-
putchar
38-
SRCS
39-
putchar.cpp
40-
HDRS
41-
../putchar.h
42-
DEPENDS
43-
libc.src.__support.OSUtil.osutil
44-
libc.src.__support.CPP.string_view
45-
)
46-
47-
add_entrypoint_object(
48-
puts
49-
SRCS
50-
puts.cpp
51-
HDRS
52-
../puts.h
53-
DEPENDS
54-
libc.src.__support.OSUtil.osutil
55-
libc.src.__support.CPP.string_view
56-
)
57-
58-
add_header_library(
59-
scanf_internal
60-
HDRS
61-
scanf_internal.h
62-
DEPENDS
63-
libc.src.stdio.scanf_core.reader
64-
libc.src.__support.OSUtil.osutil
65-
)
66-
67-
add_entrypoint_object(
68-
scanf
69-
SRCS
70-
scanf.cpp
71-
HDRS
72-
../scanf.h
73-
DEPENDS
74-
.scanf_internal
75-
libc.include.inttypes
76-
libc.src.stdio.scanf_core.scanf_main
77-
libc.src.__support.arg_list
78-
libc.src.__support.OSUtil.osutil
79-
)
80-
81-
add_entrypoint_object(
82-
vprintf
83-
SRCS
84-
vprintf.cpp
85-
HDRS
86-
../vprintf.h
87-
DEPENDS
88-
libc.src.stdio.printf_core.printf_main
89-
libc.src.stdio.printf_core.writer
90-
libc.src.__support.arg_list
91-
libc.src.__support.OSUtil.osutil
92-
)
93-
94-
add_entrypoint_object(
95-
vscanf
96-
SRCS
97-
vscanf.cpp
98-
HDRS
99-
../vscanf.h
100-
DEPENDS
101-
.scanf_internal
102-
libc.src.stdio.scanf_core.scanf_main
103-
libc.src.__support.arg_list
104-
libc.src.__support.OSUtil.osutil
105-
)

libc/src/stdio/baremetal/printf.cpp

Lines changed: 0 additions & 54 deletions
This file was deleted.

libc/src/stdio/baremetal/putchar.cpp

Lines changed: 0 additions & 24 deletions
This file was deleted.

libc/src/stdio/baremetal/puts.cpp

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)