[libc-commits] [libc] [libc] Provide baremetal implementation of getchar (PR #98059)
Petr Hosek via libc-commits
libc-commits at lists.llvm.org
Tue Jul 9 01:38:45 PDT 2024
https://github.com/petrhosek updated https://github.com/llvm/llvm-project/pull/98059
>From b53c5bb4f99ff5bb48f3740428851f2feb2fb1e0 Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Tue, 2 Jul 2024 00:45:57 -0700
Subject: [PATCH 1/3] [libc] Provide baremetal implementation of getchar
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.
---
libc/config/baremetal/arm/entrypoints.txt | 1 +
libc/config/baremetal/riscv/entrypoints.txt | 1 +
libc/src/__support/OSUtil/baremetal/io.cpp | 8 +++++++
libc/src/__support/OSUtil/baremetal/io.h | 3 +++
libc/src/stdio/baremetal/CMakeLists.txt | 11 ++++++++++
libc/src/stdio/baremetal/getchar.cpp | 24 +++++++++++++++++++++
6 files changed, 48 insertions(+)
create mode 100644 libc/src/stdio/baremetal/getchar.cpp
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index 11f560ed2b90f..e613905fdf3d5 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -83,6 +83,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.inttypes.strtoumax
# stdio.h entrypoints
+ libc.src.stdio.getchar
libc.src.stdio.printf
libc.src.stdio.putchar
libc.src.stdio.remove
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index f203317e2839d..bb25991665212 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -79,6 +79,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.inttypes.strtoumax
# stdio.h entrypoints
+ libc.src.stdio.getchar
libc.src.stdio.printf
libc.src.stdio.putchar
libc.src.stdio.remove
diff --git a/libc/src/__support/OSUtil/baremetal/io.cpp b/libc/src/__support/OSUtil/baremetal/io.cpp
index 347c7d405b0a9..0b81903a99f7e 100644
--- a/libc/src/__support/OSUtil/baremetal/io.cpp
+++ b/libc/src/__support/OSUtil/baremetal/io.cpp
@@ -11,10 +11,18 @@
#include "src/__support/CPP/string_view.h"
// This is intended to be provided by the vendor.
+
+extern struct __llvm_libc_stdin __llvm_libc_stdin;
+extern "C" ssize_t __llvm_libc_stdin_read(void *cookie, char *buf, size_t size);
+
extern "C" void __llvm_libc_log_write(const char *msg, size_t len);
namespace LIBC_NAMESPACE {
+ssize_t read_from_stdin(char *buf, size_t size) {
+ return __llvm_libc_stdin_read(static_cast<void*>(&__llvm_libc_stdin), buf, size);
+}
+
void write_to_stderr(cpp::string_view msg) {
__llvm_libc_log_write(msg.data(), msg.size());
}
diff --git a/libc/src/__support/OSUtil/baremetal/io.h b/libc/src/__support/OSUtil/baremetal/io.h
index 87534641b1fa4..b9ae0bde502be 100644
--- a/libc/src/__support/OSUtil/baremetal/io.h
+++ b/libc/src/__support/OSUtil/baremetal/io.h
@@ -9,10 +9,13 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_BAREMETAL_IO_H
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_BAREMETAL_IO_H
+#include "include/llvm-libc-types/size_t.h"
+#include "include/llvm-libc-types/ssize_t.h"
#include "src/__support/CPP/string_view.h"
namespace LIBC_NAMESPACE {
+ssize_t read_from_stdin(char *buf, size_t size);
void write_to_stderr(cpp::string_view msg);
} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdio/baremetal/CMakeLists.txt b/libc/src/stdio/baremetal/CMakeLists.txt
index 45196ffc9de24..27b8e8444d8ef 100644
--- a/libc/src/stdio/baremetal/CMakeLists.txt
+++ b/libc/src/stdio/baremetal/CMakeLists.txt
@@ -1,3 +1,14 @@
+add_entrypoint_object(
+ getchar
+ SRCS
+ getchar.cpp
+ HDRS
+ ../getchar.h
+ DEPENDS
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.CPP.string_view
+)
+
add_entrypoint_object(
remove
SRCS
diff --git a/libc/src/stdio/baremetal/getchar.cpp b/libc/src/stdio/baremetal/getchar.cpp
new file mode 100644
index 0000000000000..bbd5ba7a954fb
--- /dev/null
+++ b/libc/src/stdio/baremetal/getchar.cpp
@@ -0,0 +1,24 @@
+//===-- Baremetal implementation of getchar -------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/stdio/getchar.h"
+#include "src/__support/OSUtil/io.h"
+
+#include <stdio.h>
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(int, getchar, ()) {
+ char buf[1];
+ auto result = read_from_stdin(buf, sizeof(buf));
+ if (result < 0)
+ return EOF;
+ return buf[0];
+}
+
+} // namespace LIBC_NAMESPACE
>From 9513bfa5c69c583ba5b2e864736612d4b2501b32 Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Mon, 8 Jul 2024 11:24:34 -0700
Subject: [PATCH 2/3] Formatting
---
libc/src/__support/OSUtil/baremetal/io.cpp | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/libc/src/__support/OSUtil/baremetal/io.cpp b/libc/src/__support/OSUtil/baremetal/io.cpp
index 0b81903a99f7e..1c018e587e9bb 100644
--- a/libc/src/__support/OSUtil/baremetal/io.cpp
+++ b/libc/src/__support/OSUtil/baremetal/io.cpp
@@ -20,7 +20,8 @@ extern "C" void __llvm_libc_log_write(const char *msg, size_t len);
namespace LIBC_NAMESPACE {
ssize_t read_from_stdin(char *buf, size_t size) {
- return __llvm_libc_stdin_read(static_cast<void*>(&__llvm_libc_stdin), buf, size);
+ return __llvm_libc_stdin_read(static_cast<void *>(&__llvm_libc_stdin), buf,
+ size);
}
void write_to_stderr(cpp::string_view msg) {
>From 0187aa72f1c83bdd5e9f87efe22c26a418a1cb12 Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Tue, 9 Jul 2024 01:38:17 -0700
Subject: [PATCH 3/3] Use reinterpret_cast
---
libc/src/__support/OSUtil/baremetal/io.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libc/src/__support/OSUtil/baremetal/io.cpp b/libc/src/__support/OSUtil/baremetal/io.cpp
index 1c018e587e9bb..5dd92e4a56ce4 100644
--- a/libc/src/__support/OSUtil/baremetal/io.cpp
+++ b/libc/src/__support/OSUtil/baremetal/io.cpp
@@ -20,8 +20,8 @@ extern "C" void __llvm_libc_log_write(const char *msg, size_t len);
namespace LIBC_NAMESPACE {
ssize_t read_from_stdin(char *buf, size_t size) {
- return __llvm_libc_stdin_read(static_cast<void *>(&__llvm_libc_stdin), buf,
- size);
+ return __llvm_libc_stdin_read(reinterpret_cast<void *>(&__llvm_libc_stdin),
+ buf, size);
}
void write_to_stderr(cpp::string_view msg) {
More information about the libc-commits
mailing list