1pub mod instructions;
2pub mod program;
3
4use crate::{instructions::GcnInstruction, program::GcnProgram};
5use gaia_types::Result;
6
7pub struct GcnWriter {}
9
10impl GcnWriter {
11 pub fn new() -> Self {
12 Self {}
13 }
14
15 pub fn write(&self, program: &GcnProgram) -> Result<Vec<u8>> {
16 let mut code = String::new();
17 for kernel in &program.kernels {
18 code.push_str(&format!(".text\n.globl {}\n{}:\n", kernel.name, kernel.name));
19 for inst in &kernel.instructions {
20 code.push_str(" ");
21 code.push_str(&self.format_instruction(inst));
22 code.push('\n');
23 }
24 }
25
26 let mut binary = Vec::from("\x7fELF".as_bytes());
28 binary.extend_from_slice(code.as_bytes());
29 Ok(binary)
30 }
31
32 fn format_instruction(&self, inst: &GcnInstruction) -> String {
33 match inst {
34 GcnInstruction::VAddF32 { dst, src0, src1 } => format!("v_add_f32 {}, {}, {}", dst, src0, src1),
35 GcnInstruction::VMulF32 { dst, src0, src1 } => format!("v_mul_f32 {}, {}, {}", dst, src0, src1),
36 GcnInstruction::VDot2F32F16 { dst, src0, src1 } => format!("v_dot2_f32_f16 {}, {}, {}", dst, src0, src1),
37 GcnInstruction::GlobalLoadDword { dst, addr, offset } => {
38 format!("global_load_dword {}, {}, offset:{}", dst, addr, offset)
39 }
40 GcnInstruction::GlobalStoreDword { addr, src, offset } => {
41 format!("global_store_dword {}, {}, offset:{}", addr, src, offset)
42 }
43 GcnInstruction::SEndPgm => "s_endpgm".to_string(),
44 GcnInstruction::SNop(n) => format!("s_nop {}", n),
45 }
46 }
47}