Skip to content

[libc] Add baremetal putchar #95182

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 1 commit into from
Jun 12, 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
12 changes: 12 additions & 0 deletions libc/src/stdio/baremetal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,16 @@ add_entrypoint_object(
libc.src.stdio.printf_core.printf_main
libc.src.stdio.printf_core.writer
libc.src.__support.arg_list
libc.src.__support.OSUtil.osutil
)

add_entrypoint_object(
putchar
SRCS
putchar.cpp
HDRS
../putchar.h
DEPENDS
libc.src.__support.OSUtil.osutil
libc.src.__support.CPP.string_view
)
10 changes: 2 additions & 8 deletions libc/src/stdio/baremetal/printf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,20 @@
//===----------------------------------------------------------------------===//

#include "src/stdio/printf.h"
#include "src/__support/OSUtil/io.h"
#include "src/__support/arg_list.h"
#include "src/stdio/printf_core/core_structs.h"
#include "src/stdio/printf_core/printf_main.h"
#include "src/stdio/printf_core/writer.h"

#include <stdarg.h>

// TODO(https://github.com/llvm/llvm-project/issues/94685) unify baremetal hooks

// This is intended to be provided by the vendor.
extern "C" size_t __llvm_libc_raw_write(const char *s, size_t size);

namespace LIBC_NAMESPACE {

namespace {

LIBC_INLINE int raw_write_hook(cpp::string_view new_str, void *) {
size_t written = __llvm_libc_raw_write(new_str.data(), new_str.size());
if (written != new_str.size())
return printf_core::FILE_WRITE_ERROR;
write_to_stderr(new_str);
return printf_core::WRITE_OK;
}

Expand Down
23 changes: 23 additions & 0 deletions libc/src/stdio/baremetal/putchar.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===-- Baremetal Implementation of putchar -------------------------------===//
//
// 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/putchar.h"
#include "src/__support/CPP/string_view.h"
#include "src/__support/OSUtil/io.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(int, putchar, (int c)) {
char uc = static_cast<char>(c);

write_to_stderr(cpp::string_view(&uc, 1));

return 0;
}

} // namespace LIBC_NAMESPACE
Loading