120 Commits

Author SHA1 Message Date
Mikhail R. Gadelha
25a2aeb190 Revert "[libc] Add explicit constructor calls to fix compilation when using UInt<T>"
This reverts commit b663993067ffb5800632ad41ea7f2f92caab1093.

This caused a regression on aarch64:
https://lab.llvm.org/buildbot#builders/138/builds/43983
2023-05-16 13:06:37 -03:00
Mikhail R. Gadelha
b663993067 [libc] Add explicit constructor calls to fix compilation when using UInt<T>
This patch is similar to 86fe88c8d9 and adds several explicit
constructor calls (bool(...), uint64_t(...), uint8_t(...)) that are
needed when we use UInt<T> (in my case UInt<128> in riscv32).

This patch also adds two operators to UInt<T>:
* operator/= required by printf_core/float_hex_converter.h:148
* operator-- required by FPUtil/ManipulationFunctions.h:166

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D149594
2023-05-16 12:59:32 -03:00
Michael Jones
3befa46b91 [libc] Make printf decimal long doubles use hex
Decimal long doubles are not commonly used, and aren't currently
supported by the algorithm used for decimal float conversions. To avoid
giving incorrect answers, this patch adds a temporary exception to print
long doubles in hexadecimal even when decimal is requested.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D148179
2023-05-03 11:11:57 -07:00
Michael Jones
d94fe97280 [libc][bazel] add file printf targets and support
This patch adds targets for printf and fprintf to the bazel build.
Additionally, it adds support for the build system to specify where
files should be written for testing purposes. This was necessary to
enable the fprintf test under bazel.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D147008
2023-04-26 14:30:48 -07:00
Michael Jones
1c261e360f [libc] Add implementation of getchar
added getchar and getchar_unlocked which are just wrappers getc and getc_unlocked respectively.

Reviewed By: sivachandra, lntue, michaelrj

Differential Revision: https://reviews.llvm.org/D147919
2023-04-14 15:40:05 -07:00
Michael Jones
de939c6cd8 [libc] enable printf using system FILE
The printf and fprintf implementations use our internal implementation
to improve performance when it's available, but this patch enables using
the public FILE API for overlay mode.

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D146001
2023-03-23 09:56:45 -07:00
Michael Jones
99047c0501 [libc][bazel] add targets for sprintf
The bazel build is currently overlay mode only, so the FILE functions
are still out of reach for it, but sprintf only uses strings. This adds
targets for sprintf, snprintf, and all the interal printf pieces, as
well as tests.

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D146100
2023-03-20 15:09:20 -07:00
Michael Jones
04a9c62554 [libc] move stdlib and stdio to new errno pattern
Fixes https://github.com/llvm/llvm-project/issues/61071

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D145191
2023-03-03 11:15:36 -08:00
Michael Jones
b39f1e5c13 [libc] Fix printf %f rounding condition
When running the tbin2dec tests I found a rounding error in my code.
Upon inspection I realized it was due to a lack of parenthesis when
calculating the number of trailing digits. This patch fixes that mistake
and adds unit tests to catch regressions in future.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D145011
2023-03-01 10:57:26 -08:00
Michael Jones
cb3c41c285 [libc] Refactor string to float return values
The internal implementation of the string to float function previously
used pointer arguments for returning several values. Additionally it
set errno in several unexpected places. Now all of that goes through
return structs. For readability I also moved the function away from raw
pointer arithmetic towards proper indexing. I also added support for
rounding modes.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D144597
2023-02-28 10:50:43 -08:00
Michael Jones
8f0814f5dc [libc] Clarify printf percent conversion behavior.
Almost all printf conversions ending in '%' are undefined, but they're
traditionally treated as if the complete conversion specifier is "%%".
This patch modifies the parser to more closely match that behavior.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D144679
2023-02-24 13:29:30 -08:00
Michael Jones
bf279f903b [libc] Prevent printf index mode crashes
The posix standard defines an alternate mode for printf where the
conversions also have an index that describes which argument to select.
Due to how variadic arguments work in C, to reach the nth argument all
n-1 previous arguments must be read with their correct types. If the
format string does not specify the types for a continuous set of
arguments, then the arguments after the discontinuity cannot be safely
read. This patch causes all conversions requesting an argument that
comes after a gap be treated as raw (i.e. the conversion string is
printed literally).

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D143782
2023-02-22 16:27:26 -08:00
Siva Chandra Reddy
58a75c6a4b [libc] Introduce a libc internal assertion macro named LIBC_ASSERT.
Few existing call-sites have been modified to use LIBC_ASSERT.

Reviewed By: lntue, michaelrj

Differential Revision: https://reviews.llvm.org/D144161
2023-02-21 21:13:20 +00:00
Michael Jones
f6b724f1f9 [libc] Add basic fuzz target for the printf parser
The goal is to fuzz the entirety of printf, but the plan is to do it in
pieces for simplicity. This test fuzzes just the parser, while later
tests will fuzz the converters. This also adds a mock version of the
arg_list class.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D143784
2023-02-17 11:18:40 -08:00
Michael Jones
47fb6d1c65 [libc] add mock arg list
For testing purposes we need to be able to mock out the ArgList class.
This patch adds a mock version of that class as well as a flag to enable
it.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D144145
2023-02-17 11:18:38 -08:00
Guillaume Chatelet
c3228714cc [libc][NFC] Make tuning macros start with LIBC_COPT_
Rename preprocessor definitions that control tuning of llvm libc.

Differential Revision: https://reviews.llvm.org/D143913
2023-02-15 10:00:16 +00:00
Michael Jones
848c700b66 [libc][NFC] reorganize structs in printf
Previously the type description structs were defined in the parser. For
the fuzzing targets we'll need to use those, so I've moved them into
core_structs.h. Additionally I've renamed the function for determining
the TypeDesc from a given type. Previously it shared its name with
get_type_desc which is a related but separate function that is a part of
the parser.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D143595
2023-02-09 13:45:42 -08:00
Michael Jones
173d50235f [libc] add printf auto float conversion
This patch adds the final conversion to printf, %g. This is a floating
point conversion that selects automatically between the %e and %f
formats based on the precision requested and resulting exponent.
Additionally it trims trailing zeroes. With this done all that's left
for finishing printf is adding long double support to the decimal float
conversions.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D143006
2023-02-03 15:17:37 -08:00
Michael Jones
edf964e0dd [libc] add scanf pointer conversion
This patch adds the last conversion for scanf, %p. It is set up to match
the %p implementation in our printf.

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D142510
2023-01-25 15:29:25 -08:00
Michael Jones
afa764c9a6 [libc] add scanf current position conversion
To add the current position (%n) conversion, some reorganization needed
to be done. The "write a number to this pointer using the length
modifier" utilities and a couple other shared parsing functions have
been moved into converter_utils.h. This made implementing
current_pos_converter very simple.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D142495
2023-01-25 15:29:22 -08:00
Siva Chandra Reddy
494734b06d [libc][NFC] Another round of replacement of "inline" with "LIBC_INLINE".
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D142509
2023-01-25 07:13:24 +00:00
Siva Chandra Reddy
05d9cc4799 [libc][NFC] Replace "inline" keyword with "LIBC_INLINE".
This is the first of patches doing similar cleanup. A section in the
code style doc has been added explaining where and how LIBC_INLINE is to
be used.

Reviewed By: jeffbailey, lntue

Differential Revision: https://reviews.llvm.org/D142434
2023-01-24 19:35:34 +00:00
Michael Jones
9a32e53941 [libc] add scanf float converter
This patch adds the %f/F/e/E/g/G/a/A conversions for scanf, as well as
accompanying tests. This implementation matches the definition set forth
in the standard, which may conflict with some other implementations.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D141091
2023-01-19 09:26:25 -08:00
Siva Chandra Reddy
a439c4afdc [libc][Obvious] Address few GCC warnings. 2023-01-09 20:54:13 +00:00
Guillaume Chatelet
c25af9bb1e [libc][NFC] Use a function instead of templated static member functions for TYPE_DESC
I'm surprised that clang accepts the current code.
It seems odd to me to specialize templated static member variables.

GCC rejects them: https://godbolt.org/z/3ecE9Ps7T

This patch is in the context of https://github.com/llvm/llvm-project/issues/59368

Differential Revision: https://reviews.llvm.org/D140981
2023-01-05 10:02:57 +00:00
Guillaume Chatelet
feb0778862 [libc][NFC] Remove unused variable 2023-01-04 15:27:04 +00:00
Siva Chandra Reddy
f9868aa729 [libc][NFC] Use operator delete to cleanup a File object.
The File API has been refactored to allow cleanup using operator delete.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D140574
2022-12-22 22:31:06 +00:00
Siva Chandra Reddy
d06308df8b [libc] Handle allocation failures gracefully in FILE related API.
Few uses of free have not yet been replaced by the custom operator
delete yet. They will be done in a follow up patch.

Reviewed By: lntue, michaelrj

Differential Revision: https://reviews.llvm.org/D140526
2022-12-22 19:30:16 +00:00
Michael Jones
5112651826 [libc] add exponent format to printf
Add support for the %e/E conversion in printf, as well as unit tests. It
does not yet support long doubles.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D140042
2022-12-22 10:20:59 -08:00
Michael Jones
65f4cc6310 [libc] add scanf int conversions
This patch adds the integer conversions %d/i/u/o/x/X to scanf as well as
unit tests to check their correctness.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D138880
2022-12-13 12:51:29 -08:00
Michael Jones
ddae13c303 [libc] fix scanf error handling
Scanf is supposed to return EOF when it fails to make any conversions
and there is an input failure. Previously it would return EOF on a
matching failure, which may be an input failure but can also be a
parsing error.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D139891
2022-12-13 12:51:26 -08:00
Michael Jones
9beb8d1109 [libc] move errno out of file internals
Now errno is only set by the terminal entrypoints, and not the internal
implementations. This patch is part of the larger effort to not set
errno in libc internal code: https://github.com/llvm/llvm-project/issues/59278

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D139576
2022-12-12 13:11:45 -08:00
Michael Jones
74da5e6c08 [libc] add result class to strtointeger
This is a class intended to improve errno handling for internal
functions by allowing functions to return their result and error status
instead of setting errno. This specific class will be used for
strtointeger and (in a followup patch) strtofloat.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D133163
2022-12-09 14:35:47 -08:00
Michael Jones
36991d8342 [libc] add scanf entrypoints
This patch adds scanf, sscanf, and fscanf entrypoints. It also adds unit
tests for sscanf and a basic test to fscanf. The scanf function is
basically impossible to test in an automated fashion due to it recieving
user input.

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D138076
2022-11-17 15:37:52 -08:00
Michael Jones
1c40d5ec7d [libc] add scanf string converters
This patch adds the basic conversion facilities to scanf as well as unit
tests for them. It also adds scanf_main which will be used for the
eventual scanf entrypoints.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D137376
2022-11-07 13:49:01 -08:00
Michael Jones
0c8db885f6 [libc] add scanf reader
This is the interface that will be used to read from a file or string in
scanf. This patch also adds the string and file implementations of the
reader, although the file reader is not yet complete since ungetc has
not yet been implemented.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D137117
2022-11-07 13:48:58 -08:00
Siva Chandra Reddy
6a6101958a [libc] Add implementation of getc, getc_unlocked and fgetc_unlocked.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D137507
2022-11-07 21:47:29 +00:00
Siva Chandra Reddy
43e52ad553 [libc] Add implementations of ftell.
Reviewed By: michaelrj, lntue

Differential Revision: https://reviews.llvm.org/D137395
2022-11-07 21:38:53 +00:00
Michael Jones
28e312cbf0 [libc][obvious] fix printf failing to stop on %\0
Previously, the printf parser would treat "%\0" as a conversion with the
name "\0", and advance past the null byte causing a buffer overflow.
This patch corrects that in both printf and scanf.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D137367
2022-11-07 10:44:30 -08:00
Siva Chandra Reddy
4eea884959 [libc] Add implementation of setbuf and setvbuf.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D137356
2022-11-04 07:15:13 +00:00
Siva Chandra Reddy
1ceafe5e0f [libc] Add implementation of ungetc.
A bug in the file read logic has also been fixed along the way. Parts
of the ungetc tests will fail without that bug fixed.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D137286
2022-11-02 22:45:57 +00:00
Guillaume Chatelet
67437dd014 [reland][libc] Switch to new implementation of mem* functions
The new framework makes it explicit which processor feature is being
used and allows for easier per platform customization:
 - ARM cpu now uses trivial implementations to reduce code size.
 - Memcmp, Bcmp and Memmove have been optimized for x86
 - Bcmp has been optimized for aarch64.

This is a reland of https://reviews.llvm.org/D135134 (b3f1d58, 028414881381)

Reviewed By: courbet

Differential Revision: https://reviews.llvm.org/D136595
2022-11-02 09:09:46 +00:00
Siva Chandra Reddy
53c251bd05 [libc] Fix the return value of fread and fwrite.
They were previously returning the number of bytes read. They should
instead be returning the number of objects read.
2022-10-29 06:10:35 +00:00
Michael Jones
7a129f0756 [libc] add scanf parser and core utilities
This is the first piece of scanf. It's very similar in design to printf,
and so much of the code is copied from that. There were potential issues
with conflicting macros so I've also renamed the "ASSERT_FORMAT_EQ"
macro for printf to "ASSERT_PFORMAT_EQ".

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D136288
2022-10-28 10:52:51 -07:00
Michael Jones
2d52283391 [libc] add fgets
This adds the fgets function and its unit tests.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D136785
2022-10-27 10:27:24 -07:00
Guillaume Chatelet
b6d3ae3d3d Revert D136595 "[libc] Switch to new implementation of mem* functions"
This patch seems to introduce bugs on aarch64.
Reverting while we investigate the root cause.

This reverts commit 02841488138160f9064f334a833d4bf3e80385c6.
2022-10-27 08:38:46 +00:00
Guillaume Chatelet
0284148813 [libc] Switch to new implementation of mem* functions
The new framework makes it explicit which processor feature is being
used and allows for easier per platform customization:
 - ARM cpu now uses trivial implementations to reduce code size.
 - Memcmp, Bcmp and Memmove have been optimized for x86
 - Bcmp has been optimized for aarch64.

This is a reland of https://reviews.llvm.org/D135134 (b3f1d58)

Differential Revision: https://reviews.llvm.org/D136595
2022-10-25 10:49:34 +00:00
Siva Chandra Reddy
0480b45e9e [libc] Add implementation of fgetc.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D136421
2022-10-22 03:34:44 +00:00
Siva Chandra Reddy
2a038f9138 [libc] Add stdin definition.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D136398
2022-10-22 03:27:31 +00:00
Siva Chandra Reddy
52d707d9fd [libc][Obvious] Add few missing license headers. 2022-10-21 00:01:36 +00:00