Skip to content

[libc] puts implementation for baremetal #98051

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
Jul 9, 2024

Conversation

petrhosek
Copy link
Member

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

This is a simple baremetal implementation of puts akin to putchar.
@llvmbot
Copy link
Member

llvmbot commented Jul 8, 2024

@llvm/pr-subscribers-libc

Author: Petr Hosek (petrhosek)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/98051.diff

4 Files Affected:

  • (modified) libc/config/baremetal/arm/entrypoints.txt (+1)
  • (modified) libc/config/baremetal/riscv/entrypoints.txt (+1)
  • (modified) libc/src/stdio/baremetal/CMakeLists.txt (+11)
  • (added) libc/src/stdio/baremetal/puts.cpp (+25)
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

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?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's possible to combine these without mallocing, unfortunately.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alternative I was thinking about would be to have a writev style API where you can provide a vector of strings but I'm not sure if it's worth it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

up to you if you want to implement that

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not now, maybe as a future improvement.

@petrhosek petrhosek merged commit f3fe14f into llvm:main Jul 9, 2024
8 checks passed
aaryanshukla pushed a commit to aaryanshukla/llvm-project that referenced this pull request Jul 14, 2024
This is a simple baremetal implementation of puts akin to putchar.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants