Skip to content

[libc] Provide baremetal implementation of getchar #98059

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libc/config/baremetal/arm/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions libc/config/baremetal/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions libc/src/__support/OSUtil/baremetal/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,19 @@
#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(reinterpret_cast<void *>(&__llvm_libc_stdin),
buf, size);
}

void write_to_stderr(cpp::string_view msg) {
__llvm_libc_log_write(msg.data(), msg.size());
}
Expand Down
3 changes: 3 additions & 0 deletions libc/src/__support/OSUtil/baremetal/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions libc/src/stdio/baremetal/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
24 changes: 24 additions & 0 deletions libc/src/stdio/baremetal/getchar.cpp
Original file line number Diff line number Diff line change
@@ -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
Loading