TPDE
Loading...
Searching...
No Matches
FunctionWriterA64.hpp
1// SPDX-FileCopyrightText: 2025 Contributors to TPDE <https://tpde.org>
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3#pragma once
4
5#include "tpde/FunctionWriter.hpp"
6
7namespace tpde::a64 {
8
9/// Helper class to write function text for AArch64.
10class FunctionWriterA64 : public FunctionWriter<FunctionWriterA64> {
11 friend class FunctionWriter<FunctionWriterA64>;
12
13 util::SmallVector<u32, 16> veneers;
14 u32 unresolved_cond_brs, unresolved_test_brs;
15
16 static const TargetCIEInfo CIEInfo;
17
18public:
19 FunctionWriterA64() noexcept : FunctionWriter(CIEInfo) {}
20
21 void begin_func(u32 align, u32 expected_size) noexcept {
22 veneers.clear();
23 // Must clear unresolved count here, begin_func will call more_space.
24 unresolved_cond_brs = unresolved_test_brs = 0;
25 FunctionWriter::begin_func(align, expected_size);
26 }
27
28 void more_space(u32 size) noexcept;
29
30 bool try_write_inst(u32 inst) noexcept {
31 if (inst == 0) {
32 return false;
33 }
34 write(inst);
35 return true;
36 }
37
38 void write_inst(u32 inst) noexcept {
39 assert(inst != 0);
40 write(inst);
41 }
42
43 void write_inst_unchecked(u32 inst) noexcept {
44 assert(inst != 0);
45 write_unchecked(inst);
46 }
47
48 void label_ref(Label label, u32 off, LabelFixupKind kind) noexcept {
49 FunctionWriter::label_ref(label, off, kind);
50 if (kind == LabelFixupKind::AARCH64_COND_BR) {
51 unresolved_cond_brs++;
52 } else if (kind == LabelFixupKind::AARCH64_TEST_BR) {
53 unresolved_test_brs++;
54 }
55 }
56
57 void eh_advance(u64 size) noexcept { eh_advance_raw(size / 4); }
58
59private:
60 void handle_fixups() noexcept;
61};
62
63} // namespace tpde::a64
void eh_advance_raw(u64 size_units) noexcept
Write CFA_advance_loc; size must be scaled by code alignment factor.
void label_ref(Label label, u32 off, LabelFixupKind kind) noexcept
Reference label at given offset inside the code section.