111d643f0SMichael Jones //===-- Implementation of printf for baremetal ------------------*- C++ -*-===// 211d643f0SMichael Jones // 311d643f0SMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 411d643f0SMichael Jones // See https://llvm.org/LICENSE.txt for license information. 511d643f0SMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 611d643f0SMichael Jones // 711d643f0SMichael Jones //===----------------------------------------------------------------------===// 811d643f0SMichael Jones 911d643f0SMichael Jones #include "src/stdio/printf.h" 109a1611f9SMichael Jones #include "src/__support/OSUtil/io.h" 1111d643f0SMichael Jones #include "src/__support/arg_list.h" 12*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h" 1311d643f0SMichael Jones #include "src/stdio/printf_core/core_structs.h" 1411d643f0SMichael Jones #include "src/stdio/printf_core/printf_main.h" 1511d643f0SMichael Jones #include "src/stdio/printf_core/writer.h" 1611d643f0SMichael Jones 1711d643f0SMichael Jones #include <stdarg.h> 185aed6d67SMichael Jones #include <stddef.h> 1911d643f0SMichael Jones 20*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL { 2111d643f0SMichael Jones 2211d643f0SMichael Jones namespace { 2311d643f0SMichael Jones 2411d643f0SMichael Jones LIBC_INLINE int raw_write_hook(cpp::string_view new_str, void *) { 259a1611f9SMichael Jones write_to_stderr(new_str); 2611d643f0SMichael Jones return printf_core::WRITE_OK; 2711d643f0SMichael Jones } 2811d643f0SMichael Jones 2911d643f0SMichael Jones } // namespace 3011d643f0SMichael Jones 3111d643f0SMichael Jones LLVM_LIBC_FUNCTION(int, printf, (const char *__restrict format, ...)) { 3211d643f0SMichael Jones va_list vlist; 3311d643f0SMichael Jones va_start(vlist, format); 3411d643f0SMichael Jones internal::ArgList args(vlist); // This holder class allows for easier copying 3511d643f0SMichael Jones // and pointer semantics, as well as handling 3611d643f0SMichael Jones // destruction automatically. 3711d643f0SMichael Jones va_end(vlist); 3811d643f0SMichael Jones constexpr size_t BUFF_SIZE = 1024; 3911d643f0SMichael Jones char buffer[BUFF_SIZE]; 4011d643f0SMichael Jones 4111d643f0SMichael Jones printf_core::WriteBuffer wb(buffer, BUFF_SIZE, &raw_write_hook, nullptr); 4211d643f0SMichael Jones printf_core::Writer writer(&wb); 4311d643f0SMichael Jones 4411d643f0SMichael Jones int retval = printf_core::printf_main(&writer, format, args); 4511d643f0SMichael Jones 4611d643f0SMichael Jones int flushval = wb.overflow_write(""); 4711d643f0SMichael Jones if (flushval != printf_core::WRITE_OK) 4811d643f0SMichael Jones retval = flushval; 4911d643f0SMichael Jones 5011d643f0SMichael Jones return retval; 5111d643f0SMichael Jones } 5211d643f0SMichael Jones 53*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL 54