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
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
1 change: 1 addition & 0 deletions libc/config/baremetal/arm/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions libc/config/baremetal/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions libc/src/stdio/baremetal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions libc/src/stdio/baremetal/puts.cpp
Original file line number Diff line number Diff line change
@@ -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?
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.

write_to_stderr(str_view);
write_to_stderr("\n");

return 0;
}

} // namespace LIBC_NAMESPACE
Loading