-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[libc] Modular printf option (float only) #147426
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
base: users/mysterymath/modular-printf/clang
Are you sure you want to change the base?
Changes from all commits
35025a0
634f442
b980f03
4f6d8ee
0d06e44
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//===-- Implementation of printf_modular-----------------------------------===// | ||
// | ||
// 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/printf.h" | ||
|
||
#include "src/__support/File/file.h" | ||
#include "src/__support/arg_list.h" | ||
#include "src/__support/macros/config.h" | ||
#include "src/stdio/printf_core/vfprintf_internal.h" | ||
|
||
#include "hdr/types/FILE.h" | ||
#include <stdarg.h> | ||
|
||
#ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE | ||
#define PRINTF_STDOUT LIBC_NAMESPACE::stdout | ||
#else // LIBC_COPT_STDIO_USE_SYSTEM_FILE | ||
#define PRINTF_STDOUT ::stdout | ||
#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
LLVM_LIBC_FUNCTION(int, __printf_modular, | ||
(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); | ||
int ret_val = printf_core::vfprintf_internal_modular( | ||
reinterpret_cast<::FILE *>(PRINTF_STDOUT), format, args); | ||
return ret_val; | ||
} | ||
|
||
} // namespace LIBC_NAMESPACE_DECL |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1122,6 +1122,18 @@ LIBC_INLINE int convert_float_dec_auto_typed(Writer<write_mode> *writer, | |
} | ||
} | ||
|
||
template <WriteMode write_mode> | ||
LIBC_PRINTF_MODULAR_DECL int | ||
convert_float_decimal(Writer<write_mode> *writer, const FormatSection &to_conv); | ||
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. It might be a separate PR, but can we change all the |
||
template <WriteMode write_mode> | ||
LIBC_PRINTF_MODULAR_DECL int | ||
convert_float_dec_exp(Writer<write_mode> *writer, const FormatSection &to_conv); | ||
template <WriteMode write_mode> | ||
LIBC_PRINTF_MODULAR_DECL int | ||
convert_float_dec_auto(Writer<write_mode> *writer, | ||
const FormatSection &to_conv); | ||
|
||
#ifdef LIBC_PRINTF_DEFINE_MODULAR | ||
// TODO: unify the float converters to remove the duplicated checks for inf/nan. | ||
|
||
template <WriteMode write_mode> | ||
|
@@ -1189,6 +1201,7 @@ LIBC_INLINE int convert_float_dec_auto(Writer<write_mode> *writer, | |
|
||
return convert_inf_nan(writer, to_conv); | ||
} | ||
#endif | ||
|
||
} // namespace printf_core | ||
} // namespace LIBC_NAMESPACE_DECL | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
/// | ||
/// \file | ||
/// This file instantiates the functionality needed for supporting floating | ||
/// point arguments in modular printf builds. Non-modular printf builds | ||
/// implicitly instantiate these functions. | ||
/// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifdef LIBC_COPT_PRINTF_MODULAR | ||
mysterymath marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#include "src/__support/arg_list.h" | ||
|
||
#define LIBC_PRINTF_DEFINE_MODULAR | ||
#include "src/stdio/printf_core/float_dec_converter.h" | ||
#include "src/stdio/printf_core/float_hex_converter.h" | ||
#include "src/stdio/printf_core/parser.h" | ||
Comment on lines
+19
to
+22
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. given this, do you need to modify |
||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
namespace printf_core { | ||
template class Parser<internal::ArgList>; | ||
template class Parser<internal::DummyArgList<false>>; | ||
template class Parser<internal::DummyArgList<true>>; | ||
template class Parser<internal::StructArgList<false>>; | ||
template class Parser<internal::StructArgList<true>>; | ||
|
||
#define INSTANTIATE_CONVERT_FN(NAME) \ | ||
template int NAME<WriteMode::FILL_BUFF_AND_DROP_OVERFLOW>( \ | ||
Writer<WriteMode::FILL_BUFF_AND_DROP_OVERFLOW> * writer, \ | ||
const FormatSection &to_conv); \ | ||
template int NAME<WriteMode::FLUSH_TO_STREAM>( \ | ||
Writer<WriteMode::FLUSH_TO_STREAM> * writer, \ | ||
const FormatSection &to_conv); \ | ||
template int NAME<WriteMode::RESIZE_AND_FILL_BUFF>( \ | ||
Writer<WriteMode::RESIZE_AND_FILL_BUFF> * writer, \ | ||
const FormatSection &to_conv); \ | ||
template int NAME<WriteMode::RUNTIME_DISPATCH>( \ | ||
Writer<WriteMode::RUNTIME_DISPATCH> * writer, \ | ||
const FormatSection &to_conv) | ||
|
||
INSTANTIATE_CONVERT_FN(convert_float_decimal); | ||
INSTANTIATE_CONVERT_FN(convert_float_dec_exp); | ||
INSTANTIATE_CONVERT_FN(convert_float_dec_auto); | ||
INSTANTIATE_CONVERT_FN(convert_float_hex_exp); | ||
|
||
} // namespace printf_core | ||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
// Bring this file into the link if __printf_float is referenced. | ||
extern "C" void __printf_float() {} | ||
mysterymath marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#endif |
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.
would it make sense to have
__printf_float
as a separate entrypoint so that it can be controlled per-platform with the same mechanism as other entrypoints?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.
If I'm understanding the idea correctly, this would allow platforms to control whether or not named aspects of the various format string function implementations are included in the libc build. This would only work with
LIBC_CONF_PRINTF_MODULAR
, and it would cause link failures unless programs linked against that libc could be proven not to use those aspects. (Or maybe if they could be proven to use those aspects; that seems more useful. The difference would be whether or not the plainprintf
definitions would refer to__printf_float
, for example.)Is this roughly what you were thinking? I'm open to this, but it seems to add complexity, and it stretches the notion of entrypoint a bit. Do we do anything like this elsewhere?