luaur_code_gen/functions/
get_assembly_from_ir_impl.rs1use alloc::string::String;
3use alloc::vec::Vec;
4
5use crate::enums::code_gen_compilation_result::CodeGenCompilationResult;
6use crate::functions::assemble_helpers_code_gen_a_64::assemble_helpers as assemble_helpers_a_64;
7use crate::functions::assemble_helpers_code_gen_x_64::assemble_helpers as assemble_helpers_x_64;
8use crate::functions::lower_function::{lower_function_a_64, lower_function_x_64};
9use crate::records::assembly_builder_a_64::AssemblyBuilderA64;
10use crate::records::assembly_builder_x_64::AssemblyBuilderX64;
11use crate::records::assembly_options::AssemblyOptions;
12use crate::records::ir_builder::IrBuilder;
13use crate::records::lowering_stats::LoweringStats;
14use crate::records::module_helpers::ModuleHelpers;
15
16pub unsafe fn get_assembly_from_ir_impl_x_64(
17 build: &mut AssemblyBuilderX64,
18 ir: &mut IrBuilder,
19 options: AssemblyOptions,
20 stats: *mut LoweringStats,
21) -> String {
22 let mut helpers = ModuleHelpers::default();
23 assemble_helpers_x_64(build, &mut helpers);
24
25 if !options.include_outlined_code && options.include_assembly {
26 build.text.clear();
27 build.log_append(format_args!(
28 "; skipping {} bytes of outlined helpers\n",
29 build
30 .get_code_size()
31 .wrapping_mul(core::mem::size_of::<u8>() as u32)
32 ));
33 }
34
35 let mut result = CodeGenCompilationResult::Success;
36
37 if !lower_function_x_64(
38 ir,
39 build,
40 &mut helpers,
41 core::ptr::null_mut(),
42 options.clone(),
43 stats,
44 &mut result,
45 ) {
46 if build.log_text {
47 build.log_append(format_args!("; skipping (can't lower)\n"));
48 }
49 }
50
51 if build.log_text {
52 build.log_append(format_args!("\n"));
53 }
54
55 if !build.finalize() {
56 return String::new();
57 }
58
59 if options.output_binary {
60 let mut bytes = Vec::with_capacity(build.code.len() + build.data.len());
61 bytes.extend_from_slice(&build.code);
62 bytes.extend_from_slice(&build.data);
63 String::from_utf8_unchecked(bytes)
64 } else {
65 build.text.clone()
66 }
67}
68
69pub unsafe fn get_assembly_from_ir_impl_a_64(
70 build: &mut AssemblyBuilderA64,
71 ir: &mut IrBuilder,
72 options: AssemblyOptions,
73 stats: *mut LoweringStats,
74) -> String {
75 let mut helpers = ModuleHelpers::default();
76 assemble_helpers_a_64(build, &mut helpers);
77
78 if !options.include_outlined_code && options.include_assembly {
79 build.text.clear();
80 build.log_append(format_args!(
81 "; skipping {} bytes of outlined helpers\n",
82 build
83 .get_code_size()
84 .wrapping_mul(core::mem::size_of::<u32>() as u32)
85 ));
86 }
87
88 let mut result = CodeGenCompilationResult::Success;
89
90 if !lower_function_a_64(
91 ir,
92 build,
93 &mut helpers,
94 core::ptr::null_mut(),
95 options.clone(),
96 stats,
97 &mut result,
98 ) {
99 if build.log_text {
100 build.log_append(format_args!("; skipping (can't lower)\n"));
101 }
102 }
103
104 if build.log_text {
105 build.log_append(format_args!("\n"));
106 }
107
108 if !build.finalize() {
109 return String::new();
110 }
111
112 if options.output_binary {
113 let code = core::slice::from_raw_parts(
114 build.code.as_ptr().cast::<u8>(),
115 build.code.len() * core::mem::size_of::<u32>(),
116 );
117 let mut bytes = Vec::with_capacity(code.len() + build.data.len());
118 bytes.extend_from_slice(code);
119 bytes.extend_from_slice(&build.data);
120 String::from_utf8_unchecked(bytes)
121 } else {
122 build.text.clone()
123 }
124}