Skip to content

Commit dee8274

Browse files
committed
[libc] Support for scanf on baremetal
This uses the templatized scanf Reader interface introduced in llvm#131037.
1 parent be0215d commit dee8274

File tree

13 files changed

+201
-67
lines changed

13 files changed

+201
-67
lines changed

libc/config/baremetal/aarch64/entrypoints.txt

+4-2
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,15 @@ set(TARGET_LIBC_ENTRYPOINTS
128128
libc.src.stdio.putchar
129129
libc.src.stdio.puts
130130
libc.src.stdio.remove
131+
libc.src.stdio.scanf
131132
libc.src.stdio.snprintf
132133
libc.src.stdio.sprintf
133-
libc.src.stdio.asprintf
134+
libc.src.stdio.sscanf
134135
libc.src.stdio.vprintf
136+
libc.src.stdio.vscanf
135137
libc.src.stdio.vsnprintf
136138
libc.src.stdio.vsprintf
137-
libc.src.stdio.vasprintf
139+
libc.src.stdio.vsscanf
138140

139141
# stdbit.h entrypoints
140142
libc.src.stdbit.stdc_bit_ceil_uc

libc/config/baremetal/arm/entrypoints.txt

+4-2
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,15 @@ set(TARGET_LIBC_ENTRYPOINTS
128128
libc.src.stdio.putchar
129129
libc.src.stdio.puts
130130
libc.src.stdio.remove
131+
libc.src.stdio.scanf
131132
libc.src.stdio.snprintf
132133
libc.src.stdio.sprintf
133-
libc.src.stdio.asprintf
134+
libc.src.stdio.sscanf
134135
libc.src.stdio.vprintf
136+
libc.src.stdio.vscanf
135137
libc.src.stdio.vsnprintf
136138
libc.src.stdio.vsprintf
137-
libc.src.stdio.vasprintf
139+
libc.src.stdio.vsscanf
138140

139141
# stdbit.h entrypoints
140142
libc.src.stdbit.stdc_bit_ceil_uc

libc/config/baremetal/riscv/entrypoints.txt

+4-2
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,15 @@ set(TARGET_LIBC_ENTRYPOINTS
124124
libc.src.stdio.putchar
125125
libc.src.stdio.puts
126126
libc.src.stdio.remove
127+
libc.src.stdio.scanf
127128
libc.src.stdio.snprintf
128129
libc.src.stdio.sprintf
129-
libc.src.stdio.asprintf
130+
libc.src.stdio.sscanf
130131
libc.src.stdio.vprintf
132+
libc.src.stdio.vscanf
131133
libc.src.stdio.vsnprintf
132134
libc.src.stdio.vsprintf
133-
libc.src.stdio.vasprintf
135+
libc.src.stdio.vsscanf
134136

135137
# stdbit.h entrypoints
136138
libc.src.stdbit.stdc_bit_ceil_uc

libc/src/stdio/CMakeLists.txt

+4-54
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,6 @@ add_entrypoint_object(
9595
libc.src.__support.File.platform_file
9696
)
9797

98-
list(APPEND scanf_deps
99-
libc.src.__support.arg_list
100-
libc.src.stdio.scanf_core.vfscanf_internal
101-
libc.hdr.types.FILE
102-
)
103-
104-
if(LLVM_LIBC_FULL_BUILD AND NOT LIBC_TARGET_OS_IS_GPU)
105-
list(APPEND scanf_deps
106-
libc.src.__support.File.file
107-
libc.src.__support.File.platform_file
108-
libc.src.__support.File.platform_stdin
109-
)
110-
endif()
111-
11298
add_entrypoint_object(
11399
sscanf
114100
SRCS
@@ -133,46 +119,6 @@ add_entrypoint_object(
133119
libc.src.stdio.scanf_core.scanf_main
134120
)
135121

136-
add_entrypoint_object(
137-
fscanf
138-
SRCS
139-
fscanf.cpp
140-
HDRS
141-
fscanf.h
142-
DEPENDS
143-
${scanf_deps}
144-
)
145-
146-
add_entrypoint_object(
147-
vfscanf
148-
SRCS
149-
vfscanf.cpp
150-
HDRS
151-
vfscanf.h
152-
DEPENDS
153-
${scanf_deps}
154-
)
155-
156-
add_entrypoint_object(
157-
scanf
158-
SRCS
159-
scanf.cpp
160-
HDRS
161-
scanf.h
162-
DEPENDS
163-
${scanf_deps}
164-
)
165-
166-
add_entrypoint_object(
167-
vscanf
168-
SRCS
169-
vscanf.cpp
170-
HDRS
171-
vscanf.h
172-
DEPENDS
173-
${scanf_deps}
174-
)
175-
176122
add_entrypoint_object(
177123
sprintf
178124
SRCS
@@ -295,8 +241,12 @@ add_stdio_entrypoint_object(getchar)
295241
add_stdio_entrypoint_object(getchar_unlocked)
296242
add_stdio_entrypoint_object(fgets)
297243
add_stdio_entrypoint_object(ungetc)
244+
add_stdio_entrypoint_object(scanf)
245+
add_stdio_entrypoint_object(fscanf)
298246
add_stdio_entrypoint_object(stdin)
299247
add_stdio_entrypoint_object(stdout)
300248
add_stdio_entrypoint_object(stderr)
301249
add_stdio_entrypoint_object(vprintf)
302250
add_stdio_entrypoint_object(vfprintf)
251+
add_stdio_entrypoint_object(vscanf)
252+
add_stdio_entrypoint_object(vfscanf)

libc/src/stdio/baremetal/CMakeLists.txt

+26
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@ add_entrypoint_object(
5555
libc.src.__support.CPP.string_view
5656
)
5757

58+
add_entrypoint_object(
59+
scanf
60+
SRCS
61+
scanf.cpp
62+
HDRS
63+
../scanf.h
64+
DEPENDS
65+
libc.src.stdio.scanf_core.scanf_main
66+
libc.src.stdio.scanf_core.reader
67+
libc.src.__support.arg_list
68+
libc.src.__support.OSUtil.osutil
69+
)
70+
5871
add_entrypoint_object(
5972
vprintf
6073
SRCS
@@ -67,3 +80,16 @@ add_entrypoint_object(
6780
libc.src.__support.arg_list
6881
libc.src.__support.OSUtil.osutil
6982
)
83+
84+
add_entrypoint_object(
85+
vscanf
86+
SRCS
87+
vscanf.cpp
88+
HDRS
89+
../vscanf.h
90+
DEPENDS
91+
libc.src.stdio.scanf_core.scanf_main
92+
libc.src.stdio.scanf_core.reader
93+
libc.src.__support.arg_list
94+
libc.src.__support.OSUtil.osutil
95+
)

libc/src/stdio/baremetal/scanf.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//===-- Implementation of scanf ---------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/stdio/scanf.h"
10+
11+
#include "hdr/stdio_macros.h"
12+
#include "src/__support/OSUtil/io.h"
13+
#include "src/__support/arg_list.h"
14+
#include "src/__support/macros/config.h"
15+
#include "src/stdio/scanf_core/reader.h"
16+
#include "src/stdio/scanf_core/scanf_main.h"
17+
18+
#include <stdarg.h>
19+
20+
namespace LIBC_NAMESPACE_DECL {
21+
22+
namespace {
23+
24+
struct StreamBuffer : scanf_core::ReadBuffer<StreamBuffer> {
25+
LIBC_INLINE char getc() {
26+
char buf[1];
27+
read_from_stdin(buf, sizeof(buf));
28+
return buf[0];
29+
}
30+
LIBC_INLINE void ungetc(int) {}
31+
};
32+
33+
} // namespace
34+
35+
LLVM_LIBC_FUNCTION(int, scanf, (const char *__restrict format, ...)) {
36+
va_list vlist;
37+
va_start(vlist, format);
38+
internal::ArgList args(vlist); // This holder class allows for easier copying
39+
// and pointer semantics, as well as handling
40+
// destruction automatically.
41+
va_end(vlist);
42+
43+
StreamBuffer buffer;
44+
scanf_core::Reader<StreamBuffer> reader(&buffer);
45+
46+
int retval = scanf_core::scanf_main(&reader, format, args);
47+
// This is done to avoid including stdio.h in the internals. On most systems
48+
// EOF is -1, so this will be transformed into just "return retval".
49+
return (retval == -1) ? EOF : retval;
50+
}
51+
52+
} // namespace LIBC_NAMESPACE_DECL

libc/src/stdio/baremetal/vscanf.cpp

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//===-- Implementation of vscanf --------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/stdio/vscanf.h"
10+
11+
#include "hdr/stdio_macros.h"
12+
#include "src/__support/OSUtil/io.h"
13+
#include "src/__support/arg_list.h"
14+
#include "src/__support/macros/config.h"
15+
#include "src/stdio/scanf_core/reader.h"
16+
#include "src/stdio/scanf_core/scanf_main.h"
17+
18+
#include <stdarg.h>
19+
20+
namespace LIBC_NAMESPACE_DECL {
21+
22+
namespace {
23+
24+
struct StreamBuffer : scanf_core::ReadBuffer<StreamBuffer> {
25+
LIBC_INLINE char getc() {
26+
char buf[1];
27+
read_from_stdin(buf, sizeof(buf));
28+
return buf[0];
29+
}
30+
LIBC_INLINE void ungetc(int) {}
31+
};
32+
33+
} // namespace
34+
35+
LLVM_LIBC_FUNCTION(int, vscanf,
36+
(const char *__restrict format, va_list vlist)) {
37+
internal::ArgList args(vlist); // This holder class allows for easier copying
38+
// and pointer semantics, as well as handling
39+
// destruction automatically.
40+
va_end(vlist);
41+
42+
StreamBuffer buffer;
43+
scanf_core::Reader<StreamBuffer> reader(&buffer);
44+
45+
int retval = scanf_core::scanf_main(&reader, format, args);
46+
// This is done to avoid including stdio.h in the internals. On most systems
47+
// EOF is -1, so this will be transformed into just "return retval".
48+
return (retval == -1) ? EOF : retval;
49+
}
50+
51+
} // namespace LIBC_NAMESPACE_DECL

libc/src/stdio/generic/CMakeLists.txt

+54
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,60 @@ add_entrypoint_object(
425425
${fprintf_deps}
426426
)
427427

428+
list(APPEND scanf_deps
429+
libc.src.__support.arg_list
430+
libc.src.stdio.scanf_core.vfscanf_internal
431+
libc.hdr.types.FILE
432+
)
433+
434+
if(LLVM_LIBC_FULL_BUILD AND NOT LIBC_TARGET_OS_IS_GPU)
435+
list(APPEND scanf_deps
436+
libc.src.__support.File.file
437+
libc.src.__support.File.platform_file
438+
libc.src.__support.File.platform_stdin
439+
)
440+
endif()
441+
442+
add_entrypoint_object(
443+
fscanf
444+
SRCS
445+
fscanf.cpp
446+
HDRS
447+
../fscanf.h
448+
DEPENDS
449+
${scanf_deps}
450+
)
451+
452+
add_entrypoint_object(
453+
vfscanf
454+
SRCS
455+
vfscanf.cpp
456+
HDRS
457+
../vfscanf.h
458+
DEPENDS
459+
${scanf_deps}
460+
)
461+
462+
add_entrypoint_object(
463+
scanf
464+
SRCS
465+
scanf.cpp
466+
HDRS
467+
../scanf.h
468+
DEPENDS
469+
${scanf_deps}
470+
)
471+
472+
add_entrypoint_object(
473+
vscanf
474+
SRCS
475+
vscanf.cpp
476+
HDRS
477+
../vscanf.h
478+
DEPENDS
479+
${scanf_deps}
480+
)
481+
428482
add_entrypoint_object(
429483
fgets
430484
SRCS
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

libc/src/stdio/scanf_core/CMakeLists.txt

+2-7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ if(LIBC_TARGET_OS_IS_GPU)
1616
libc.src.stdio.ungetc
1717
libc.src.stdio.ferror
1818
)
19+
elseif(LIBC_TARGET_OS_IS_BAREMETAL)
20+
# There's no FILE* for baremetal.
1921
elseif(LLVM_LIBC_FULL_BUILD)
2022
list(APPEND file_deps
2123
libc.src.__support.File.file
@@ -54,13 +56,6 @@ add_header_library(
5456
libc.src.__support.CPP.string_view
5557
)
5658

57-
if(NOT(TARGET libc.src.__support.File.file) AND LLVM_LIBC_FULL_BUILD AND
58-
(NOT LIBC_TARGET_OS_IS_GPU))
59-
# Not all platforms have a file implementation. If file is unvailable, and a
60-
# full build is requested, then we must skip all file based scanf sections.
61-
return()
62-
endif()
63-
6459
add_object_library(
6560
scanf_main
6661
SRCS

0 commit comments

Comments
 (0)