diff --git a/libc/config/gpu/entrypoints.txt b/libc/config/gpu/entrypoints.txt --- a/libc/config/gpu/entrypoints.txt +++ b/libc/config/gpu/entrypoints.txt @@ -82,6 +82,9 @@ # stdio.h entrypoints libc.src.stdio.puts + libc.src.stdio.fopen + libc.src.stdio.fclose + libc.src.stdio.fread libc.src.stdio.fputs libc.src.stdio.stdin libc.src.stdio.stdout diff --git a/libc/docs/gpu/support.rst b/libc/docs/gpu/support.rst --- a/libc/docs/gpu/support.rst +++ b/libc/docs/gpu/support.rst @@ -124,6 +124,7 @@ ============= ========= ============ puts |check| |check| fputs |check| |check| -fclose |check| -fopen |check| +fclose |check| |check| +fopen |check| |check| +fread |check| |check| ============= ========= ============ diff --git a/libc/src/stdio/CMakeLists.txt b/libc/src/stdio/CMakeLists.txt --- a/libc/src/stdio/CMakeLists.txt +++ b/libc/src/stdio/CMakeLists.txt @@ -18,39 +18,16 @@ endif() endfunction(add_stdio_entrypoint_object) -add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/generic) if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS}) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS}) +elseif(NOT LIBC_TARGET_ARCHITECTURE_IS_GPU) + add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/generic) endif() + add_subdirectory(printf_core) add_subdirectory(scanf_core) -add_entrypoint_object( - fopen - SRCS - fopen.cpp - HDRS - fopen.h - DEPENDS - libc.include.stdio - libc.src.__support.File.file - libc.src.__support.File.platform_file -) - -add_entrypoint_object( - fclose - SRCS - fclose.cpp - HDRS - fclose.h - DEPENDS - libc.include.stdio - libc.src.errno.errno - libc.src.__support.File.file - libc.src.__support.File.platform_file -) - add_entrypoint_object( clearerr SRCS @@ -251,32 +228,6 @@ libc.src.__support.File.platform_file ) -add_entrypoint_object( - fread_unlocked - SRCS - fread_unlocked.cpp - HDRS - fread_unlocked.h - DEPENDS - libc.src.errno.errno - libc.include.stdio - libc.src.__support.File.file - libc.src.__support.File.platform_file -) - -add_entrypoint_object( - fread - SRCS - fread.cpp - HDRS - fread.h - DEPENDS - libc.src.errno.errno - libc.include.stdio - libc.src.__support.File.file - libc.src.__support.File.platform_file -) - add_entrypoint_object( fwrite_unlocked SRCS @@ -568,6 +519,10 @@ ) # These entrypoints have multiple potential implementations. +add_stdio_entrypoint_object(fopen) +add_stdio_entrypoint_object(fclose) +add_stdio_entrypoint_object(fread_unlocked) +add_stdio_entrypoint_object(fread) add_stdio_entrypoint_object(puts) add_stdio_entrypoint_object(fputs) add_stdio_entrypoint_object(stdin) diff --git a/libc/src/stdio/generic/CMakeLists.txt b/libc/src/stdio/generic/CMakeLists.txt --- a/libc/src/stdio/generic/CMakeLists.txt +++ b/libc/src/stdio/generic/CMakeLists.txt @@ -1,7 +1,53 @@ -if(LIBC_TARGET_ARCHITECTURE_IS_GPU) - # The GPU build cannot use any generic implementations. - return() -endif() +add_entrypoint_object( + fopen + SRCS + fopen.cpp + HDRS + ../fopen.h + DEPENDS + libc.include.stdio + libc.src.__support.File.file + libc.src.__support.File.platform_file +) + +add_entrypoint_object( + fclose + SRCS + fclose.cpp + HDRS + ../fclose.h + DEPENDS + libc.include.stdio + libc.src.errno.errno + libc.src.__support.File.file + libc.src.__support.File.platform_file +) + +add_entrypoint_object( + fread_unlocked + SRCS + fread_unlocked.cpp + HDRS + ../fread_unlocked.h + DEPENDS + libc.src.errno.errno + libc.include.stdio + libc.src.__support.File.file + libc.src.__support.File.platform_file +) + +add_entrypoint_object( + fread + SRCS + fread.cpp + HDRS + ../fread.h + DEPENDS + libc.src.errno.errno + libc.include.stdio + libc.src.__support.File.file + libc.src.__support.File.platform_file +) add_entrypoint_object( fputs diff --git a/libc/src/stdio/fclose.cpp b/libc/src/stdio/generic/fclose.cpp rename from libc/src/stdio/fclose.cpp rename to libc/src/stdio/generic/fclose.cpp diff --git a/libc/src/stdio/fopen.cpp b/libc/src/stdio/generic/fopen.cpp rename from libc/src/stdio/fopen.cpp rename to libc/src/stdio/generic/fopen.cpp diff --git a/libc/src/stdio/fread.cpp b/libc/src/stdio/generic/fread.cpp rename from libc/src/stdio/fread.cpp rename to libc/src/stdio/generic/fread.cpp diff --git a/libc/src/stdio/fread_unlocked.cpp b/libc/src/stdio/generic/fread_unlocked.cpp rename from libc/src/stdio/fread_unlocked.cpp rename to libc/src/stdio/generic/fread_unlocked.cpp diff --git a/libc/src/stdio/gpu/CMakeLists.txt b/libc/src/stdio/gpu/CMakeLists.txt --- a/libc/src/stdio/gpu/CMakeLists.txt +++ b/libc/src/stdio/gpu/CMakeLists.txt @@ -4,8 +4,39 @@ file.h DEPENDS libc.src.__support.common - libc.src.__support.CPP.string_view - libc.src.__support.RPC.rpc_client + .stdin + .stdout + .stderr +) + +add_entrypoint_object( + fopen + SRCS + fopen.cpp + HDRS + ../fopen.h + DEPENDS + libc.include.stdio +) + +add_entrypoint_object( + fclose + SRCS + fclose.cpp + HDRS + ../fclose.h + DEPENDS + libc.include.stdio +) + +add_entrypoint_object( + fread + SRCS + fread.cpp + HDRS + ../fread.h + DEPENDS + libc.include.stdio ) add_entrypoint_object( diff --git a/libc/src/stdio/fclose.cpp b/libc/src/stdio/gpu/fclose.cpp rename from libc/src/stdio/fclose.cpp rename to libc/src/stdio/gpu/fclose.cpp --- a/libc/src/stdio/fclose.cpp +++ b/libc/src/stdio/gpu/fclose.cpp @@ -1,4 +1,4 @@ -//===-- Implementation of fclose ------------------------------------------===// +//===-- GPU Implementation of fclose --------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -7,20 +7,23 @@ //===----------------------------------------------------------------------===// #include "src/stdio/fclose.h" -#include "src/__support/File/file.h" +#include "src/stdio/gpu/file.h" -#include "src/errno/libc_errno.h" #include namespace __llvm_libc { LLVM_LIBC_FUNCTION(int, fclose, (::FILE * stream)) { - int result = reinterpret_cast<__llvm_libc::File *>(stream)->close(); - if (result != 0) { - libc_errno = result; + uint64_t ret = 0; + uintptr_t file = reinterpret_cast(stream); + rpc::Client::Port port = rpc::client.open(); + port.send_and_recv([=](rpc::Buffer *buffer) { buffer->data[0] = file; }, + [&](rpc::Buffer *buffer) { ret = buffer->data[0]; }); + port.close(); + + if (ret != 0) return EOF; - } - return 0; + return static_cast(ret); } } // namespace __llvm_libc diff --git a/libc/src/stdio/gpu/file.h b/libc/src/stdio/gpu/file.h --- a/libc/src/stdio/gpu/file.h +++ b/libc/src/stdio/gpu/file.h @@ -85,5 +85,12 @@ return ret; } +LIBC_INLINE uint64_t read(FILE *f, void *data, size_t size) { + if (f == stdin) + return read_from_stdin(data, size); + else + return read_from_stream(reinterpret_cast(f), data, size); +} + } // namespace file } // namespace __llvm_libc diff --git a/libc/src/stdio/gpu/fopen.cpp b/libc/src/stdio/gpu/fopen.cpp new file mode 100644 --- /dev/null +++ b/libc/src/stdio/gpu/fopen.cpp @@ -0,0 +1,32 @@ +//===-- GPU Implementation of fopen ---------------------------------------===// +// +// 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/fopen.h" +#include "src/__support/CPP/string_view.h" +#include "src/stdio/gpu/file.h" + +#include + +namespace __llvm_libc { + +LLVM_LIBC_FUNCTION(::FILE *, fopen, + (const char *__restrict path, const char *__restrict mode)) { + uintptr_t file; + rpc::Client::Port port = rpc::client.open(); + port.send_n(path, internal::string_length(path) + 1); + port.send_and_recv( + [=](rpc::Buffer *buffer) { + inline_memcpy(buffer->data, mode, internal::string_length(mode) + 1); + }, + [&](rpc::Buffer *buffer) { file = buffer->data[0]; }); + port.close(); + + return reinterpret_cast(file); +} + +} // namespace __llvm_libc diff --git a/libc/src/stdio/fread.cpp b/libc/src/stdio/gpu/fread.cpp rename from libc/src/stdio/fread.cpp rename to libc/src/stdio/gpu/fread.cpp --- a/libc/src/stdio/fread.cpp +++ b/libc/src/stdio/gpu/fread.cpp @@ -1,4 +1,4 @@ -//===-- Implementation of fread -------------------------------------------===// +//===-- GPU Implementation of fread ---------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -7,9 +7,8 @@ //===----------------------------------------------------------------------===// #include "src/stdio/fread.h" -#include "src/__support/File/file.h" +#include "src/stdio/gpu/file.h" -#include "src/errno/libc_errno.h" #include namespace __llvm_libc { @@ -19,11 +18,8 @@ ::FILE *stream)) { if (size == 0 || nmemb == 0) return 0; - auto result = - reinterpret_cast<__llvm_libc::File *>(stream)->read(buffer, size * nmemb); - if (result.has_error()) - libc_errno = result.error; - return result.value / size; + auto result = file::read(stream, buffer, size * nmemb); + return result / size; } } // namespace __llvm_libc