Skip to content

Commit cfa2d7d

Browse files
authored
[libc] Provide baremetal implementation of getchar (#98059)
This introduces opaque type `struct __llvm_libc_stdin` and a symbol `__llvm_libc_stdin_read` that's intended to be provided by the vendor. `__llvm_libc_stdin_read` intentionally has the same signature as `cookie_read_function_t` so it can be used with `fopencookie` to represent `stdin` as `FILE *` in the future.
1 parent dd2bf3b commit cfa2d7d

File tree

6 files changed

+49
-0
lines changed

6 files changed

+49
-0
lines changed

libc/config/baremetal/arm/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ set(TARGET_LIBC_ENTRYPOINTS
8383
libc.src.inttypes.strtoumax
8484

8585
# stdio.h entrypoints
86+
libc.src.stdio.getchar
8687
libc.src.stdio.printf
8788
libc.src.stdio.putchar
8889
libc.src.stdio.puts

libc/config/baremetal/riscv/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ set(TARGET_LIBC_ENTRYPOINTS
7979
libc.src.inttypes.strtoumax
8080

8181
# stdio.h entrypoints
82+
libc.src.stdio.getchar
8283
libc.src.stdio.printf
8384
libc.src.stdio.putchar
8485
libc.src.stdio.puts

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,19 @@
1111
#include "src/__support/CPP/string_view.h"
1212

1313
// This is intended to be provided by the vendor.
14+
15+
extern struct __llvm_libc_stdin __llvm_libc_stdin;
16+
extern "C" ssize_t __llvm_libc_stdin_read(void *cookie, char *buf, size_t size);
17+
1418
extern "C" void __llvm_libc_log_write(const char *msg, size_t len);
1519

1620
namespace LIBC_NAMESPACE {
1721

22+
ssize_t read_from_stdin(char *buf, size_t size) {
23+
return __llvm_libc_stdin_read(reinterpret_cast<void *>(&__llvm_libc_stdin),
24+
buf, size);
25+
}
26+
1827
void write_to_stderr(cpp::string_view msg) {
1928
__llvm_libc_log_write(msg.data(), msg.size());
2029
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_BAREMETAL_IO_H
1010
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_BAREMETAL_IO_H
1111

12+
#include "include/llvm-libc-types/size_t.h"
13+
#include "include/llvm-libc-types/ssize_t.h"
1214
#include "src/__support/CPP/string_view.h"
1315

1416
namespace LIBC_NAMESPACE {
1517

18+
ssize_t read_from_stdin(char *buf, size_t size);
1619
void write_to_stderr(cpp::string_view msg);
1720

1821
} // namespace LIBC_NAMESPACE

libc/src/stdio/baremetal/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
add_entrypoint_object(
2+
getchar
3+
SRCS
4+
getchar.cpp
5+
HDRS
6+
../getchar.h
7+
DEPENDS
8+
libc.src.__support.OSUtil.osutil
9+
libc.src.__support.CPP.string_view
10+
)
11+
112
add_entrypoint_object(
213
remove
314
SRCS

libc/src/stdio/baremetal/getchar.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===-- Baremetal implementation of getchar -------------------------------===//
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/getchar.h"
10+
#include "src/__support/OSUtil/io.h"
11+
12+
#include <stdio.h>
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
LLVM_LIBC_FUNCTION(int, getchar, ()) {
17+
char buf[1];
18+
auto result = read_from_stdin(buf, sizeof(buf));
19+
if (result < 0)
20+
return EOF;
21+
return buf[0];
22+
}
23+
24+
} // namespace LIBC_NAMESPACE

0 commit comments

Comments
 (0)