This is an archive of the discontinued LLVM Phabricator instance.

[libc] Add implementation of getchar
ClosedPublic

Authored by xbjfk on Apr 10 2023, 1:35 AM.

Details

Summary

added getchar and getchar_unlocked which are just wrappers getc and getc_unlocked respectively.

Diff Detail

Event Timeline

xbjfk created this revision.Apr 10 2023, 1:35 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptApr 10 2023, 1:35 AM
xbjfk requested review of this revision.Apr 10 2023, 1:35 AM

In our codebase we try to avoid calling entrypoints from other entrypoints. In this case that means not calling getc from getchar, instead mostly copying the code. This may seem silly, but it allows users to include any entrypoint on its own without having to worry about dependencies.

Other than that, this patch looks good, I'll approve it once that change is made.

xbjfk updated this revision to Diff 512393.Apr 11 2023, 3:54 AM

Dont call getc/getc_unlocked from getchar/getchar_unlocked

xbjfk added a comment.Apr 11 2023, 3:58 AM

after this is reviewed, can someone please commit this? (I dont have commit access yet)

libc/spec/posix.td
1083

Is this the correct way to define a function that takes no arguments?

michaelrj accepted this revision.Apr 11 2023, 11:16 AM

LGTM with these changes. I can commit for you.

libc/spec/posix.td
1083

good catch, it should be ArgSpec<VoidType>

libc/spec/stdc.td
586

same here, it should be ArgSpec<VoidType>

This revision is now accepted and ready to land.Apr 11 2023, 11:16 AM
lntue added inline comments.Apr 11 2023, 3:36 PM
libc/config/linux/x86_64/entrypoints.txt
426

Can this also be enabled for ARM targets?

xbjfk updated this revision to Diff 512636.Apr 11 2023, 6:26 PM

Add to aarch64 entrypoints.txt, fix function spec
I didn't add to arm (32) entrypoints since the stdio.h isn't part of headers.txt yet

xbjfk marked an inline comment as done.Apr 11 2023, 6:26 PM
xbjfk marked an inline comment as done.
lntue accepted this revision.Apr 11 2023, 8:28 PM
sivachandra accepted this revision.Apr 11 2023, 10:18 PM
sivachandra added inline comments.
libc/src/stdio/getchar.cpp
25

Nit: This can be if (result.value != 1). Same in getchar_unlocked.

xbjfk updated this revision to Diff 513526.Apr 14 2023, 3:52 AM

Remove redundant temporary variable

xbjfk marked 2 inline comments as done.Apr 14 2023, 3:54 AM
michaelrj accepted this revision.Apr 14 2023, 3:13 PM

LGTM, I'm going to land it once I'm done with what I'm currently doing.

This revision was automatically updated to reflect the committed changes.