diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt index a8e653fdd5159..08b143d045463 100644 --- a/libc/config/baremetal/aarch64/entrypoints.txt +++ b/libc/config/baremetal/aarch64/entrypoints.txt @@ -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 diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt index acafef17fa5d1..276f00a04b16a 100644 --- a/libc/config/baremetal/arm/entrypoints.txt +++ b/libc/config/baremetal/arm/entrypoints.txt @@ -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 diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt index 023826f12d723..dc173f69ce554 100644 --- a/libc/config/baremetal/riscv/entrypoints.txt +++ b/libc/config/baremetal/riscv/entrypoints.txt @@ -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 diff --git a/libc/src/stdio/baremetal/CMakeLists.txt b/libc/src/stdio/baremetal/CMakeLists.txt index e879230a9d02c..b720b99869bd7 100644 --- a/libc/src/stdio/baremetal/CMakeLists.txt +++ b/libc/src/stdio/baremetal/CMakeLists.txt @@ -1,3 +1,26 @@ +add_entrypoint_object( + fprintf + SRCS + fprintf.cpp + HDRS + ../fprintf.h + DEPENDS + libc.src.__support.CPP.string_view + libc.src.stdio.printf_core.printf_main + .baremetal_write_utils +) + +add_entrypoint_object( + fputc + SRCS + fputc.cpp + HDRS + ../fputc.h + DEPENDS + libc.src.__support.CPP.string_view + .baremetal_write_utils +) + add_entrypoint_object( getchar SRCS @@ -20,6 +43,17 @@ add_entrypoint_object( libc.include.stdio ) +add_entrypoint_object( + putc + SRCS + putc.cpp + HDRS + ../putc.h + DEPENDS + libc.src.__support.CPP.string_view + .baremetal_write_utils +) + add_entrypoint_object( printf SRCS @@ -28,9 +62,8 @@ add_entrypoint_object( ../printf.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 + .baremetal_write_hooks ) add_entrypoint_object( @@ -85,9 +118,8 @@ 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 + .baremetal_write_hooks ) add_entrypoint_object( @@ -102,3 +134,27 @@ add_entrypoint_object( libc.src.__support.arg_list libc.src.__support.OSUtil.osutil ) + +add_header_library( + baremetal_write_hooks + HDRS + write_hooks.h + DEPENDS + libc.src.__support.OSUtil.osutil + libc.src.__support.CPP.string_view + libc.src.stdio.printf_core.core_structs +) + +add_header_library( + baremetal_write_utils + HDRS + write_utils.h + DEPENDS + libc.hdr.types.FILE + libc.hdr.stdio_macros + libc.src.__support.OSUtil.osutil + libc.src.__support.CPP.string_view + .baremetal_write_hooks + .stdout + .stderr +) diff --git a/libc/src/stdio/baremetal/fprintf.cpp b/libc/src/stdio/baremetal/fprintf.cpp new file mode 100644 index 0000000000000..4081bf166bb16 --- /dev/null +++ b/libc/src/stdio/baremetal/fprintf.cpp @@ -0,0 +1,51 @@ +//===-- Implementation of fprintf 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/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 + +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]; + + write_utils::StreamWriter write_hook = write_utils::get_write_hook(stream); + if (write_hook == nullptr) { + return 0; + } + + printf_core::WriteBuffer wb( + buffer, BUFF_SIZE, write_utils::get_write_hook(stream), nullptr); + printf_core::Writer 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 diff --git a/libc/src/stdio/baremetal/fputc.cpp b/libc/src/stdio/baremetal/fputc.cpp new file mode 100644 index 0000000000000..7d49df1254307 --- /dev/null +++ b/libc/src/stdio/baremetal/fputc.cpp @@ -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(c); + + write_utils::write(stream, cpp::string_view(&uc, 1)); + + return 0; +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/stdio/baremetal/getchar.cpp b/libc/src/stdio/baremetal/getchar.cpp index 8fb7bc32537e7..0c718fab86ffa 100644 --- a/libc/src/stdio/baremetal/getchar.cpp +++ b/libc/src/stdio/baremetal/getchar.cpp @@ -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. diff --git a/libc/src/stdio/baremetal/printf.cpp b/libc/src/stdio/baremetal/printf.cpp index 7253c6549a4e4..96f288fde14fa 100644 --- a/libc/src/stdio/baremetal/printf.cpp +++ b/libc/src/stdio/baremetal/printf.cpp @@ -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_hooks.h" #include "src/stdio/printf_core/printf_main.h" -#include "src/stdio/printf_core/writer.h" #include #include 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); @@ -39,7 +29,7 @@ LLVM_LIBC_FUNCTION(int, printf, (const char *__restrict format, ...)) { char buffer[BUFF_SIZE]; printf_core::WriteBuffer wb( - buffer, BUFF_SIZE, &stdout_write_hook, nullptr); + buffer, BUFF_SIZE, &write_utils::stdout_write_hook, nullptr); printf_core::Writer writer(wb); int retval = printf_core::printf_main(&writer, format, args); diff --git a/libc/src/stdio/baremetal/putc.cpp b/libc/src/stdio/baremetal/putc.cpp new file mode 100644 index 0000000000000..6482956830614 --- /dev/null +++ b/libc/src/stdio/baremetal/putc.cpp @@ -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(c); + + write_utils::write(stream, cpp::string_view(&uc, 1)); + + return 0; +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/stdio/baremetal/putchar.cpp b/libc/src/stdio/baremetal/putchar.cpp index ac21e6e783b01..f0175964eb125 100644 --- a/libc/src/stdio/baremetal/putchar.cpp +++ b/libc/src/stdio/baremetal/putchar.cpp @@ -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. @@ -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" diff --git a/libc/src/stdio/baremetal/puts.cpp b/libc/src/stdio/baremetal/puts.cpp index fcd3aa086b2bf..72619cb548900 100644 --- a/libc/src/stdio/baremetal/puts.cpp +++ b/libc/src/stdio/baremetal/puts.cpp @@ -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. diff --git a/libc/src/stdio/baremetal/remove.cpp b/libc/src/stdio/baremetal/remove.cpp index d383d0cf78887..0301c0db58fa0 100644 --- a/libc/src/stdio/baremetal/remove.cpp +++ b/libc/src/stdio/baremetal/remove.cpp @@ -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. diff --git a/libc/src/stdio/baremetal/vprintf.cpp b/libc/src/stdio/baremetal/vprintf.cpp index ab02533f14911..2114a79f57299 100644 --- a/libc/src/stdio/baremetal/vprintf.cpp +++ b/libc/src/stdio/baremetal/vprintf.cpp @@ -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_hooks.h" #include "src/stdio/printf_core/printf_main.h" -#include "src/stdio/printf_core/writer.h" #include #include 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 @@ -37,7 +27,7 @@ LLVM_LIBC_FUNCTION(int, vprintf, char buffer[BUFF_SIZE]; printf_core::WriteBuffer wb( - buffer, BUFF_SIZE, &stdout_write_hook, nullptr); + buffer, BUFF_SIZE, &write_utils::stdout_write_hook, nullptr); printf_core::Writer writer(wb); int retval = printf_core::printf_main(&writer, format, args); diff --git a/libc/src/stdio/baremetal/write_hooks.h b/libc/src/stdio/baremetal/write_hooks.h new file mode 100644 index 0000000000000..d03fe0e98f605 --- /dev/null +++ b/libc/src/stdio/baremetal/write_hooks.h @@ -0,0 +1,28 @@ +//===-- 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 "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; +} + +} // namespace write_utils +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/stdio/baremetal/write_utils.h b/libc/src/stdio/baremetal/write_utils.h new file mode 100644 index 0000000000000..a59542b0bd9cc --- /dev/null +++ b/libc/src/stdio/baremetal/write_utils.h @@ -0,0 +1,41 @@ +//===-- 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/baremetal/write_hooks.h" + +namespace LIBC_NAMESPACE_DECL { +namespace write_utils { + +LIBC_INLINE void write(::FILE *f, cpp::string_view new_str) { + if (f == stdout) { + write_to_stdout(new_str); + } else if (f == stderr) { + write_to_stderr(new_str); + } else { + // Do nothing + } +} + +using StreamWriter = int (*)(cpp::string_view, void *); +LIBC_INLINE StreamWriter get_write_hook(::FILE *f) { + if (f == stdout) { + return &stdout_write_hook; + } else if (f == stderr) { + return &stderr_write_hook; + } + + return nullptr; +} + +} // namespace write_utils +} // namespace LIBC_NAMESPACE_DECL