Skip to main content

gaia_assembler/backends/windows/
mod.rs

1pub mod emitter;
2pub mod x86;
3pub mod x86_64;
4
5use crate::{
6    backends::{Backend, GeneratedFiles},
7    config::GaiaConfig,
8    program::GaiaModule,
9};
10use gaia_types::{
11    helpers::{AbiCompatible, ApiCompatible, Architecture, ArtifactType, CompilationTarget},
12    Result,
13};
14
15/// Windows Backend implementation (Native PE)
16#[derive(Default)]
17pub struct WindowsBackend {}
18
19impl Backend for WindowsBackend {
20    fn name(&self) -> &'static str {
21        "Windows (Native x86_64)"
22    }
23
24    fn primary_target(&self) -> CompilationTarget {
25        #[cfg(feature = "x86_64-assembler")]
26        {
27            self.x86_64_backend().primary_target()
28        }
29        #[cfg(not(feature = "x86_64-assembler"))]
30        {
31            CompilationTarget { build: Architecture::X86_64, host: AbiCompatible::PE, target: ApiCompatible::MicrosoftVisualC }
32        }
33    }
34
35    fn artifact_type(&self) -> ArtifactType {
36        ArtifactType::Executable
37    }
38
39    fn match_score(&self, target: &CompilationTarget) -> f32 {
40        if target.host != AbiCompatible::PE {
41            return -100.0;
42        }
43
44        match target.build {
45            Architecture::X86_64 => {
46                #[cfg(feature = "x86_64-assembler")]
47                {
48                    self.x86_64_backend().match_score(target)
49                }
50                #[cfg(not(feature = "x86_64-assembler"))]
51                {
52                    0.0
53                }
54            }
55            Architecture::X86 => {
56                #[cfg(feature = "x86-assembler")]
57                {
58                    self.x86_backend().match_score(target)
59                }
60                #[cfg(not(feature = "x86-assembler"))]
61                {
62                    0.0
63                }
64            }
65            _ => 0.0,
66        }
67    }
68
69    fn generate(&self, program: &GaiaModule, config: &GaiaConfig) -> Result<GeneratedFiles> {
70        let target = &config.target;
71        match target.build {
72            Architecture::X86_64 => {
73                #[cfg(feature = "x86_64-assembler")]
74                {
75                    self.x86_64_backend().generate(program, config)
76                }
77                #[cfg(not(feature = "x86_64-assembler"))]
78                {
79                    Err(gaia_types::GaiaError::platform_unsupported("Windows x86_64", "x86_64-assembler feature not enabled"))
80                }
81            }
82            Architecture::X86 => {
83                #[cfg(feature = "x86-assembler")]
84                {
85                    self.x86_backend().generate(program, config)
86                }
87                #[cfg(not(feature = "x86-assembler"))]
88                {
89                    Err(gaia_types::GaiaError::platform_unsupported("Windows x86", "x86-assembler feature not enabled"))
90                }
91            }
92            _ => {
93                #[cfg(feature = "x86_64-assembler")]
94                {
95                    self.x86_64_backend().generate(program, config)
96                }
97                #[cfg(not(feature = "x86_64-assembler"))]
98                {
99                    Err(gaia_types::GaiaError::platform_unsupported("Windows", "x86_64-assembler feature not enabled"))
100                }
101            }
102        }
103    }
104}
105
106impl WindowsBackend {
107    #[cfg(feature = "x86_64-assembler")]
108    fn x86_64_backend(&self) -> x86_64::X64Backend {
109        x86_64::X64Backend::default()
110    }
111    #[cfg(feature = "x86-assembler")]
112    fn x86_backend(&self) -> x86::X86Backend {
113        x86::X86Backend::default()
114    }
115}