[libc-commits] [libc] [libc] Add baremetal putchar (PR #95182)
Michael Jones via libc-commits
libc-commits at lists.llvm.org
Tue Jun 11 16:34:53 PDT 2024
https://github.com/michaelrj-google created https://github.com/llvm/llvm-project/pull/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.
>From 68b6f1e5cc89802a9247a555c61ea1914290e134 Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Tue, 11 Jun 2024 16:31:29 -0700
Subject: [PATCH] [libc] Add baremetal putchar
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.
---
libc/src/stdio/baremetal/CMakeLists.txt | 12 ++++++++++++
libc/src/stdio/baremetal/printf.cpp | 10 ++--------
libc/src/stdio/baremetal/putchar.cpp | 23 +++++++++++++++++++++++
3 files changed, 37 insertions(+), 8 deletions(-)
create mode 100644 libc/src/stdio/baremetal/putchar.cpp
diff --git a/libc/src/stdio/baremetal/CMakeLists.txt b/libc/src/stdio/baremetal/CMakeLists.txt
index a1a5ba5faaf3d..e43f6bdcfef6f 100644
--- a/libc/src/stdio/baremetal/CMakeLists.txt
+++ b/libc/src/stdio/baremetal/CMakeLists.txt
@@ -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
)
diff --git a/libc/src/stdio/baremetal/printf.cpp b/libc/src/stdio/baremetal/printf.cpp
index 597078b1ede51..b240371a0c20e 100644
--- a/libc/src/stdio/baremetal/printf.cpp
+++ b/libc/src/stdio/baremetal/printf.cpp
@@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#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"
@@ -14,19 +15,12 @@
#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;
}
diff --git a/libc/src/stdio/baremetal/putchar.cpp b/libc/src/stdio/baremetal/putchar.cpp
new file mode 100644
index 0000000000000..23e9745e9cf70
--- /dev/null
+++ b/libc/src/stdio/baremetal/putchar.cpp
@@ -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
More information about the libc-commits
mailing list