[libc-commits] [libc] [libc] puts implementation for baremetal (PR #98051)

Petr Hosek via libc-commits libc-commits at lists.llvm.org
Mon Jul 8 10:06:56 PDT 2024


https://github.com/petrhosek created https://github.com/llvm/llvm-project/pull/98051

This is a simple baremetal implementation of puts akin to putchar.

>From a2362d10f05da0134805d8044814f5dee8f3287a Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Mon, 8 Jul 2024 10:01:20 -0700
Subject: [PATCH] [libc] puts implementation for baremetal

This is a simple baremetal implementation of puts akin to putchar.
---
 libc/config/baremetal/arm/entrypoints.txt   |  1 +
 libc/config/baremetal/riscv/entrypoints.txt |  1 +
 libc/src/stdio/baremetal/CMakeLists.txt     | 11 +++++++++
 libc/src/stdio/baremetal/puts.cpp           | 25 +++++++++++++++++++++
 4 files changed, 38 insertions(+)
 create mode 100644 libc/src/stdio/baremetal/puts.cpp

diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index 11f560ed2b90f7..ad41267626664d 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -85,6 +85,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     # stdio.h entrypoints
     libc.src.stdio.printf
     libc.src.stdio.putchar
+    libc.src.stdio.puts
     libc.src.stdio.remove
     libc.src.stdio.snprintf
     libc.src.stdio.sprintf
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index f203317e2839d5..6ed6732918b42a 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -81,6 +81,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     # stdio.h entrypoints
     libc.src.stdio.printf
     libc.src.stdio.putchar
+    libc.src.stdio.puts
     libc.src.stdio.remove
     libc.src.stdio.snprintf
     libc.src.stdio.sprintf
diff --git a/libc/src/stdio/baremetal/CMakeLists.txt b/libc/src/stdio/baremetal/CMakeLists.txt
index 45196ffc9de248..4acd8873ab753f 100644
--- a/libc/src/stdio/baremetal/CMakeLists.txt
+++ b/libc/src/stdio/baremetal/CMakeLists.txt
@@ -32,6 +32,17 @@ add_entrypoint_object(
     libc.src.__support.CPP.string_view
 )
 
+add_entrypoint_object(
+  puts
+  SRCS
+    puts.cpp
+  HDRS
+    ../puts.h
+  DEPENDS
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.CPP.string_view
+)
+
 add_entrypoint_object(
   vprintf
   SRCS
diff --git a/libc/src/stdio/baremetal/puts.cpp b/libc/src/stdio/baremetal/puts.cpp
new file mode 100644
index 00000000000000..136cdb8acf8000
--- /dev/null
+++ b/libc/src/stdio/baremetal/puts.cpp
@@ -0,0 +1,25 @@
+//===-- Implementation of puts for baremetal-------------------------------===//
+//
+// 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/puts.h"
+#include "src/__support/CPP/string_view.h"
+#include "src/__support/OSUtil/io.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(int, puts, (const char *__restrict str)) {
+  cpp::string_view str_view(str);
+
+  // TODO: Can we combine these to avoid needing two writes?
+  write_to_stderr(str_view);
+  write_to_stderr("\n");
+
+  return 0;
+}
+
+} // namespace LIBC_NAMESPACE



More information about the libc-commits mailing list