Skip to content

[libc] Add putc, fputc, and fprintf to stdio/baremetal #144567

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions libc/config/baremetal/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,11 @@ set(TARGET_LIBC_ENTRYPOINTS

# stdio.h entrypoints
libc.src.stdio.asprintf
libc.src.stdio.fprintf
libc.src.stdio.fputc
libc.src.stdio.getchar
libc.src.stdio.printf
libc.src.stdio.putc
libc.src.stdio.putchar
libc.src.stdio.puts
libc.src.stdio.remove
Expand Down
3 changes: 3 additions & 0 deletions libc/config/baremetal/arm/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,11 @@ set(TARGET_LIBC_ENTRYPOINTS

# stdio.h entrypoints
libc.src.stdio.asprintf
libc.src.stdio.fprintf
libc.src.stdio.fputc
libc.src.stdio.getchar
libc.src.stdio.printf
libc.src.stdio.putc
libc.src.stdio.putchar
libc.src.stdio.puts
libc.src.stdio.remove
Expand Down
3 changes: 3 additions & 0 deletions libc/config/baremetal/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,11 @@ set(TARGET_LIBC_ENTRYPOINTS

# stdio.h entrypoints
libc.src.stdio.asprintf
libc.src.stdio.fprintf
libc.src.stdio.fputc
libc.src.stdio.getchar
libc.src.stdio.printf
libc.src.stdio.putc
libc.src.stdio.putchar
libc.src.stdio.puts
libc.src.stdio.remove
Expand Down
52 changes: 48 additions & 4 deletions libc/src/stdio/baremetal/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
add_entrypoint_object(
fprintf
SRCS
fprintf.cpp
HDRS
../fprintf.h
write_utils.h
DEPENDS
libc.src.__support.CPP.string_view
libc.src.stdio.printf_core.printf_main
)

add_entrypoint_object(
fputc
SRCS
fputc.cpp
HDRS
../fputc.h
write_utils.h
DEPENDS
libc.src.__support.CPP.string_view
)

add_entrypoint_object(
getchar
SRCS
Expand All @@ -20,17 +43,27 @@ add_entrypoint_object(
libc.include.stdio
)

add_entrypoint_object(
putc
SRCS
putc.cpp
HDRS
../putc.h
write_utils.h
DEPENDS
libc.src.__support.CPP.string_view
)

add_entrypoint_object(
printf
SRCS
printf.cpp
HDRS
../printf.h
write_utils.h
Copy link
Member

Choose a reason for hiding this comment

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

This shouldn't be here since it's not part of printf implementation, you should be instead depending on baremetal_write_utils.

DEPENDS
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(
Expand Down Expand Up @@ -85,9 +118,7 @@ add_entrypoint_object(
../vprintf.h
DEPENDS
libc.src.stdio.printf_core.printf_main
libc.src.stdio.printf_core.writer
libc.src.__support.arg_list
libc.src.__support.OSUtil.osutil
Copy link
Member

Choose a reason for hiding this comment

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

This is missing a dependency on baremetal_write_utils.

)

add_entrypoint_object(
Expand All @@ -102,3 +133,16 @@ add_entrypoint_object(
libc.src.__support.arg_list
libc.src.__support.OSUtil.osutil
)

add_header_library(
baremetal_write_utils
Copy link
Member

Choose a reason for hiding this comment

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

This is not being used anywhere (see the comments above).

HDRS
write_utils.h
DEPENDS
libc.hdr.types.FILE
libc.hdr.stdio_macros
libc.src.__support.OSUtil.osutil
libc.src.__support.CPP.string_view
.stdout
.stderr
)
46 changes: 46 additions & 0 deletions libc/src/stdio/baremetal/fprintf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//===-- Implementation of fprintf for baremetal -----------------*- C++ -*-===//
Copy link
Contributor

Choose a reason for hiding this comment

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

nit:

Suggested change
//===-- Implementation of fprintf for baremetal -----------------*- C++ -*-===//
//===-- Implementation of fprintf 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/fprintf.h"

#include "hdr/types/FILE.h"
#include "src/__support/arg_list.h"
#include "src/__support/macros/config.h"
#include "src/stdio/baremetal/write_utils.h"
#include "src/stdio/printf_core/printf_main.h"

#include <stdarg.h>

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, fprintf,
(::FILE *__restrict stream, const char *__restrict format,
...)) {
va_list vlist;
va_start(vlist, format);
internal::ArgList args(vlist); // This holder class allows for easier copying
// and pointer semantics, as well as handling
// destruction automatically.
va_end(vlist);
static constexpr size_t BUFF_SIZE = 1024;
char buffer[BUFF_SIZE];

printf_core::WriteBuffer<printf_core::WriteMode::FLUSH_TO_STREAM> wb(
buffer, BUFF_SIZE, write_utils::get_write_hook(stream), nullptr);
printf_core::Writer<printf_core::WriteMode::FLUSH_TO_STREAM> writer(wb);

int retval = printf_core::printf_main(&writer, format, args);

int flushval = wb.overflow_write("");
if (flushval != printf_core::WRITE_OK)
retval = flushval;

return retval;
}

} // namespace LIBC_NAMESPACE_DECL
25 changes: 25 additions & 0 deletions libc/src/stdio/baremetal/fputc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===-- Implementation of fputc for baremetal -------------------*- C++ -*-===//
//
// 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/fputc.h"

#include "src/__support/CPP/string_view.h"
#include "src/__support/macros/config.h"
#include "src/stdio/baremetal/write_utils.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, fputc, (int c, ::FILE *stream)) {
char uc = static_cast<char>(c);

write_utils::write(stream, cpp::string_view(&uc, 1));

return 0;
}

} // namespace LIBC_NAMESPACE_DECL
2 changes: 1 addition & 1 deletion libc/src/stdio/baremetal/getchar.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===-- Baremetal implementation of getchar -------------------------------===//
//===-- Implementation of getchar for baremetal -----------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
Expand Down
16 changes: 3 additions & 13 deletions libc/src/stdio/baremetal/printf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,17 @@
//===----------------------------------------------------------------------===//

#include "src/stdio/printf.h"
#include "src/__support/OSUtil/io.h"

#include "src/__support/arg_list.h"
#include "src/__support/macros/config.h"
#include "src/stdio/printf_core/core_structs.h"
#include "src/stdio/baremetal/write_utils.h"
#include "src/stdio/printf_core/printf_main.h"
#include "src/stdio/printf_core/writer.h"

#include <stdarg.h>
#include <stddef.h>

namespace LIBC_NAMESPACE_DECL {

namespace {

LIBC_INLINE int stdout_write_hook(cpp::string_view new_str, void *) {
write_to_stdout(new_str);
return printf_core::WRITE_OK;
}

} // namespace

LLVM_LIBC_FUNCTION(int, printf, (const char *__restrict format, ...)) {
va_list vlist;
va_start(vlist, format);
Expand All @@ -39,7 +29,7 @@ LLVM_LIBC_FUNCTION(int, printf, (const char *__restrict format, ...)) {
char buffer[BUFF_SIZE];

printf_core::WriteBuffer<printf_core::WriteMode::FLUSH_TO_STREAM> wb(
buffer, BUFF_SIZE, &stdout_write_hook, nullptr);
buffer, BUFF_SIZE, &write_utils::stdout_write_hook, nullptr);
printf_core::Writer<printf_core::WriteMode::FLUSH_TO_STREAM> writer(wb);

int retval = printf_core::printf_main(&writer, format, args);
Expand Down
25 changes: 25 additions & 0 deletions libc/src/stdio/baremetal/putc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===-- Implementation of putc for baremetal --------------------*- C++ -*-===//
//
// 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/putc.h"

#include "src/__support/CPP/string_view.h"
#include "src/__support/macros/config.h"
#include "src/stdio/baremetal/write_utils.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, putc, (int c, ::FILE *stream)) {
char uc = static_cast<char>(c);

write_utils::write(stream, cpp::string_view(&uc, 1));

return 0;
}

} // namespace LIBC_NAMESPACE_DECL
3 changes: 2 additions & 1 deletion libc/src/stdio/baremetal/putchar.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===-- Baremetal Implementation of putchar -------------------------------===//
//===-- Implementation of putchar for baremetal -----------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
Expand All @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "src/stdio/putchar.h"

#include "src/__support/CPP/string_view.h"
#include "src/__support/OSUtil/io.h"
#include "src/__support/macros/config.h"
Expand Down
2 changes: 1 addition & 1 deletion libc/src/stdio/baremetal/puts.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===-- Implementation of puts for baremetal-------------------------------===//
//===-- Implementation of puts for baremetal --------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
Expand Down
2 changes: 1 addition & 1 deletion libc/src/stdio/baremetal/remove.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===-- Linux implementation of remove ------------------------------------===//
//===-- Implementation of remove for baremetal ------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
Expand Down
16 changes: 3 additions & 13 deletions libc/src/stdio/baremetal/vprintf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,17 @@
//===----------------------------------------------------------------------===//

#include "src/stdio/vprintf.h"
#include "src/__support/OSUtil/io.h"

#include "src/__support/arg_list.h"
#include "src/__support/macros/config.h"
#include "src/stdio/printf_core/core_structs.h"
#include "src/stdio/baremetal/write_utils.h"
#include "src/stdio/printf_core/printf_main.h"
#include "src/stdio/printf_core/writer.h"

#include <stdarg.h>
#include <stddef.h>

namespace LIBC_NAMESPACE_DECL {

namespace {

LIBC_INLINE int stdout_write_hook(cpp::string_view new_str, void *) {
write_to_stdout(new_str);
return printf_core::WRITE_OK;
}

} // namespace

LLVM_LIBC_FUNCTION(int, vprintf,
(const char *__restrict format, va_list vlist)) {
internal::ArgList args(vlist); // This holder class allows for easier copying
Expand All @@ -37,7 +27,7 @@ LLVM_LIBC_FUNCTION(int, vprintf,
char buffer[BUFF_SIZE];

printf_core::WriteBuffer<printf_core::WriteMode::FLUSH_TO_STREAM> wb(
buffer, BUFF_SIZE, &stdout_write_hook, nullptr);
buffer, BUFF_SIZE, &write_utils::stdout_write_hook, nullptr);
printf_core::Writer<printf_core::WriteMode::FLUSH_TO_STREAM> writer(wb);

int retval = printf_core::printf_main(&writer, format, args);
Expand Down
46 changes: 46 additions & 0 deletions libc/src/stdio/baremetal/write_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//===-- Baremetal helper functions for writing to stdout/stderr -*- C++ -*-===//
//
// 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 "hdr/stdio_macros.h" // For stdout/err
#include "hdr/types/FILE.h"
#include "src/__support/CPP/string_view.h"
#include "src/__support/OSUtil/io.h"
#include "src/__support/macros/config.h"
#include "src/stdio/printf_core/core_structs.h" // For printf_core::WRITE_OK

namespace LIBC_NAMESPACE_DECL {
namespace write_utils {

LIBC_INLINE int stdout_write_hook(cpp::string_view new_str, void *) {
write_to_stdout(new_str);
return printf_core::WRITE_OK;
}

LIBC_INLINE int stderr_write_hook(cpp::string_view new_str, void *) {
write_to_stderr(new_str);
return printf_core::WRITE_OK;
}

LIBC_INLINE void write(::FILE *f, cpp::string_view new_str) {
if (f == stdout) {
write_to_stdout(new_str);
} else {
write_to_stderr(new_str);
}
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer explicitly checking if the stream is either stdout or stderr and if it's neither, returning an error.

}

using StreamWriter = int (*)(cpp::string_view, void *);
LIBC_INLINE StreamWriter get_write_hook(::FILE *f) {
if (f == stdout)
return &stdout_write_hook;

return &stderr_write_hook;
Copy link
Member

Choose a reason for hiding this comment

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

The same here, if the stream is neither stdout nor stderr, we shouldn't just implicitly fallback to stderr, we should return an error.

}
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer splitting these into a separate header so when we're compiling printf or scanf, we shouldn't be pulling in FILE*.


} // namespace write_utils
} // namespace LIBC_NAMESPACE_DECL
Loading