-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
saturn691
wants to merge
7
commits into
llvm:main
Choose a base branch
from
saturn691:add-more-stdio
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+250
−34
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
519f3e0
[libc] Add putc, fputc, and fprintf to stdio/baremetal
saturn691 b30faff
fixup! [libc] Add putc, fputc, and fprintf to stdio/baremetal
saturn691 20b1e65
fixup! fixup! [libc] Add putc, fputc, and fprintf to stdio/baremetal
saturn691 581740f
fixup! fixup! fixup! [libc] Add putc, fputc, and fprintf to stdio/bar…
saturn691 4474161
fixup! fixup! fixup! fixup! [libc] Add putc, fputc, and fprintf to st…
saturn691 94ea13f
fixup! fixup! fixup! fixup! fixup! [libc] Add putc, fputc, and fprint…
saturn691 9b4168f
fixup! fixup! fixup! fixup! fixup! fixup! [libc] Add putc, fputc, and…
saturn691 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
.baremetal_write_hooks | ||
.stdout | ||
.stderr | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,51 @@ | ||||||
//===-- Implementation of fprintf for baremetal -----------------*- C++ -*-===// | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
Suggested change
|
||||||
// | ||||||
// 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]; | ||||||
|
||||||
write_utils::StreamWriter write_hook = write_utils::get_write_hook(stream); | ||||||
if (write_hook == nullptr) { | ||||||
return 0; | ||||||
} | ||||||
|
||||||
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
.