[libc-commits] [libc] [libc] Add baremetal printf (PR #94078)

Petr Hosek via libc-commits libc-commits at lists.llvm.org
Fri May 31 18:24:15 PDT 2024


================
@@ -0,0 +1,51 @@
+//===-- Implementation of printf for baremetal ------------------*- C++ -*-===//
+//
+// 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/printf.h"
+#include "src/__support/arg_list.h"
+#include "src/stdio/printf_core/core_structs.h"
+#include "src/stdio/printf_core/printf_main.h"
+#include "src/stdio/printf_core/writer.h"
+
+#include <stdarg.h>
+
+// This is intended to be provided by the vendor. Name TBD
+extern "C" size_t __llvm_libc_raw_write(const char *s, size_t size);
----------------
petrhosek wrote:

`write_to_stderr` is a private C++ API, for vendor integration we need an internal C API. So far we only have `__llvm_libc_log_write` defined in baremetal `OSUtil`, and this change introduces `__llvm_libc_raw_write`. It's a question whether this API should be specific only to baremetal target or should be made more generic, but I'm fine starting with just baremetal and generalizing it as needed. It'd be also helpful to define all internal API in a single header that's extensively documented. There are also open questions around naming, versioning, prior art, etc. This is a broader topic that we should probably discuss on Discourse, but I'm fine landing this change in the meantime and refining the API later.

Regarding opaque `FILE`, for baremetal I think we'll need both. Most baremetal targets have some support for writing out characters, typically over serial. This would typically be used to implement `printf` (or similar API for logging). Many baremetal targets also support [semihosting](https://github.com/ARM-software/abi-aa/blob/5c0a393f50e083ccca9f32ca94995211141fe858/semihosting/semihosting.rst) which allows file system access and I'd like to implement `FILE` for baremetal through semihosting. We could also use semihosting to implement `printf` (using [`SYS_WRITE`](https://github.com/ARM-software/abi-aa/blob/5c0a393f50e083ccca9f32ca94995211141fe858/semihosting/semihosting.rst#sys_write-0x05) syscall) but that has a downside of requiring a debugger and so I don't think it should be the default implementation.

https://github.com/llvm/llvm-project/pull/94078


More information about the libc-commits mailing list