Skip to content

Commit ebdea52

Browse files
authored
[libc] Provide vprintf for baremetal (llvm#95363)
This is similar to baremetal printf that was implemented in llvm#94078.
1 parent 2e7b95e commit ebdea52

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

libc/src/stdio/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
2222
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
2323
endif()
2424

25-
if(NOT LIBC_TARGET_OS_IS_GPU)
25+
if(NOT LIBC_TARGET_OS_IS_BAREMETAL AND NOT LIBC_TARGET_OS_IS_GPU)
2626
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/generic)
2727
endif()
2828

libc/src/stdio/baremetal/CMakeLists.txt

+13
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,16 @@ add_entrypoint_object(
3131
libc.src.__support.OSUtil.osutil
3232
libc.src.__support.CPP.string_view
3333
)
34+
35+
add_entrypoint_object(
36+
vprintf
37+
SRCS
38+
vprintf.cpp
39+
HDRS
40+
../vprintf.h
41+
DEPENDS
42+
libc.src.stdio.printf_core.printf_main
43+
libc.src.stdio.printf_core.writer
44+
libc.src.__support.arg_list
45+
libc.src.__support.OSUtil.osutil
46+
)

libc/src/stdio/baremetal/vprintf.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//===-- Implementation of vprintf -------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/stdio/vprintf.h"
10+
#include "src/__support/OSUtil/io.h"
11+
#include "src/__support/arg_list.h"
12+
#include "src/stdio/printf_core/core_structs.h"
13+
#include "src/stdio/printf_core/printf_main.h"
14+
#include "src/stdio/printf_core/writer.h"
15+
16+
#include <stdarg.h>
17+
18+
namespace LIBC_NAMESPACE {
19+
20+
namespace {
21+
22+
LIBC_INLINE int raw_write_hook(cpp::string_view new_str, void *) {
23+
write_to_stderr(new_str);
24+
return printf_core::WRITE_OK;
25+
}
26+
27+
} // namespace
28+
29+
LLVM_LIBC_FUNCTION(int, vprintf,
30+
(const char *__restrict format, va_list vlist)) {
31+
internal::ArgList args(vlist); // This holder class allows for easier copying
32+
// and pointer semantics, as well as handling
33+
// destruction automatically.
34+
constexpr size_t BUFF_SIZE = 1024;
35+
char buffer[BUFF_SIZE];
36+
37+
printf_core::WriteBuffer wb(buffer, BUFF_SIZE, &raw_write_hook, nullptr);
38+
printf_core::Writer writer(&wb);
39+
40+
int retval = printf_core::printf_main(&writer, format, args);
41+
42+
int flushval = wb.overflow_write("");
43+
if (flushval != printf_core::WRITE_OK)
44+
retval = flushval;
45+
46+
return retval;
47+
}
48+
49+
} // namespace LIBC_NAMESPACE

0 commit comments

Comments
 (0)