microfmt
Zero-allocation C++ formatting for constrained environments
Loading...
Searching...
No Matches
semihosting.hpp
Go to the documentation of this file.
1#pragma once
2#include "../microfmt.hpp"
3
4namespace microfmt {
5
13public:
14 constexpr semihosting_sink() noexcept = default;
15
21 static void write(void *, microfmt::string_view sv) noexcept {
22 if (sv.empty()) {
23 return;
24 }
25
26 for (char c : sv) {
27#if defined(__aarch64__)
28 register uint64_t reg_sysnum __asm__("x0") = 0x03; // SYS_WRITEC operation code
29 register const char *reg_ptr __asm__("x1") = &c; // Pointer to the character
30
31 __asm__ volatile("hlt #0xf000" : "+r"(reg_sysnum) : "r"(reg_ptr) : "memory", "cc");
32#elif defined(__arm__) || defined(__thumb__)
33 register uint32_t reg_sysnum __asm__("r0") = 0x03; // SYS_WRITEC operation code
34 register const char *reg_ptr __asm__("r1") = &c; // Pointer to the character
35
36 __asm__ volatile("bkpt #0xab" : "+r"(reg_sysnum) : "r"(reg_ptr) : "memory", "cc");
37#else
38#error "Unsupported architecture for semihosting_sink"
39#endif
40 }
41 }
42
47 [[nodiscard]] constexpr sink as_sink() const noexcept {
48 return sink{.ctx = nullptr, .write_fn = &semihosting_sink::write};
49 }
50};
51
52} // namespace microfmt
ARM Semihosting Sink for AArch64 freestanding environments.
Definition semihosting.hpp:12
constexpr sink as_sink() const noexcept
Adapts this semihosting sink into a type-erased microfmt::sink.
Definition semihosting.hpp:47
constexpr semihosting_sink() noexcept=default
#define MICROFMT_API_CLASS
Definition compat.hpp:181
Definition assert.hpp:19
reloco::sink sink
Type-erased, zero-allocation output sink.
Definition microfmt.hpp:46