Skip to main content

luaur_code_gen/methods/
assembly_builder_a_64_finalize.rs

1use crate::macros::codegen_assert::CODEGEN_ASSERT;
2use crate::records::assembly_builder_a_64::AssemblyBuilderA64;
3
4impl AssemblyBuilderA64 {
5    pub fn finalize(&mut self) -> bool {
6        let code_pos_ptr = self.code_pos as *const u32;
7        let code_data_ptr = self.code.as_ptr();
8        let code_len = unsafe { code_pos_ptr.offset_from(code_data_ptr) as usize };
9        self.code.resize(code_len, 0);
10
11        let pending_labels = core::mem::take(&mut self.pending_labels);
12        for fixup in pending_labels {
13            let label = fixup.label();
14            CODEGEN_ASSERT!(self.label_locations[label as usize - 1] != !0u32);
15            let value = (self.label_locations[label as usize - 1] as i32) - (fixup.location as i32);
16
17            self.patch_offset(fixup.location, value, fixup.kind());
18        }
19
20        let data_size = self.data.len() - self.data_pos;
21
22        if data_size > 0 {
23            self.data.copy_within(self.data_pos.., 0);
24        }
25
26        self.data.resize(data_size, 0);
27
28        self.finalized = true;
29
30        !self.overflowed
31    }
32}