Skip to content

Commit 9a1611f

Browse files
[libc] Add baremetal putchar (#95182)
In #94685 I discussed my ideas for cleaner baremetal output writing. This patch is not that. This patch just uses `write_to_stderr` for outputting from putchar and printf on baremetal. I'm still planning to create a proper design for writing to stdout and stderr on various platforms, but that will be a followup patch.
1 parent 90a154e commit 9a1611f

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

libc/src/stdio/baremetal/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,16 @@ add_entrypoint_object(
1818
libc.src.stdio.printf_core.printf_main
1919
libc.src.stdio.printf_core.writer
2020
libc.src.__support.arg_list
21+
libc.src.__support.OSUtil.osutil
22+
)
23+
24+
add_entrypoint_object(
25+
putchar
26+
SRCS
27+
putchar.cpp
28+
HDRS
29+
../putchar.h
30+
DEPENDS
31+
libc.src.__support.OSUtil.osutil
32+
libc.src.__support.CPP.string_view
2133
)

libc/src/stdio/baremetal/printf.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,20 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/stdio/printf.h"
10+
#include "src/__support/OSUtil/io.h"
1011
#include "src/__support/arg_list.h"
1112
#include "src/stdio/printf_core/core_structs.h"
1213
#include "src/stdio/printf_core/printf_main.h"
1314
#include "src/stdio/printf_core/writer.h"
1415

1516
#include <stdarg.h>
1617

17-
// TODO(https://github.com/llvm/llvm-project/issues/94685) unify baremetal hooks
18-
19-
// This is intended to be provided by the vendor.
20-
extern "C" size_t __llvm_libc_raw_write(const char *s, size_t size);
21-
2218
namespace LIBC_NAMESPACE {
2319

2420
namespace {
2521

2622
LIBC_INLINE int raw_write_hook(cpp::string_view new_str, void *) {
27-
size_t written = __llvm_libc_raw_write(new_str.data(), new_str.size());
28-
if (written != new_str.size())
29-
return printf_core::FILE_WRITE_ERROR;
23+
write_to_stderr(new_str);
3024
return printf_core::WRITE_OK;
3125
}
3226

libc/src/stdio/baremetal/putchar.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Baremetal Implementation of putchar -------------------------------===//
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/putchar.h"
10+
#include "src/__support/CPP/string_view.h"
11+
#include "src/__support/OSUtil/io.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
LLVM_LIBC_FUNCTION(int, putchar, (int c)) {
16+
char uc = static_cast<char>(c);
17+
18+
write_to_stderr(cpp::string_view(&uc, 1));
19+
20+
return 0;
21+
}
22+
23+
} // namespace LIBC_NAMESPACE

0 commit comments

Comments
 (0)