spirv_tools/
assembler.rs

1#[cfg(feature = "use-compiled-tools")]
2pub mod compiled;
3
4#[cfg(feature = "use-installed-tools")]
5pub mod tool;
6
7#[derive(Copy, Clone, Default)]
8pub struct AssemblerOptions {
9    /// Numeric IDs in the binary will have the same values as in the source.
10    /// Non-numeric IDs are allocated by filling in the gaps, starting with 1
11    /// and going up.
12    pub preserve_numeric_ids: bool,
13}
14
15#[allow(clippy::from_over_into)]
16impl Into<u32> for AssemblerOptions {
17    fn into(self) -> u32 {
18        // This is weird, the "none" is 1, so I'm not sure if that means having
19        // it disables all other options or...?
20        let mut res = 0; //assembler::BinaryOptions::None as u32;
21
22        if self.preserve_numeric_ids {
23            res |= spirv_tools_sys::assembler::BinaryOptions::PreserveNumberIds as u32;
24        }
25
26        res
27    }
28}
29
30#[derive(Copy, Clone)]
31pub struct DisassembleOptions {
32    /// Print to stdout.
33    pub print: bool,
34    /// Add color codes to output
35    pub color: bool,
36    /// Indent assembly
37    pub indent: bool,
38    pub show_byte_offset: bool,
39    /// Do not output the module header as leading comments in the assembly.
40    pub no_header: bool,
41    /// Use friendly names where possible.  The heuristic may expand over
42    /// time, but will use common names for scalar types, and debug names from
43    /// OpName instructions.
44    pub use_friendly_names: bool,
45    /// Add some comments to the generated assembly
46    pub comment: bool,
47}
48
49impl Default for DisassembleOptions {
50    fn default() -> Self {
51        Self {
52            print: false,
53            color: false,
54            indent: true,
55            show_byte_offset: false,
56            no_header: false,
57            use_friendly_names: true,
58            comment: true,
59        }
60    }
61}
62
63#[allow(clippy::from_over_into)]
64impl Into<u32> for DisassembleOptions {
65    fn into(self) -> u32 {
66        let mut res = 0;
67
68        if self.print {
69            res |= spirv_tools_sys::assembler::DisassembleOptions::Print as u32;
70        }
71
72        if self.color {
73            res |= spirv_tools_sys::assembler::DisassembleOptions::Color as u32;
74        }
75
76        if self.indent {
77            res |= spirv_tools_sys::assembler::DisassembleOptions::Indent as u32;
78        }
79
80        if self.show_byte_offset {
81            res |= spirv_tools_sys::assembler::DisassembleOptions::ShowByteOffset as u32;
82        }
83
84        if self.no_header {
85            res |= spirv_tools_sys::assembler::DisassembleOptions::NoHeader as u32;
86        }
87
88        if self.use_friendly_names {
89            res |= spirv_tools_sys::assembler::DisassembleOptions::FriendlyNames as u32;
90        }
91
92        if self.comment {
93            res |= spirv_tools_sys::assembler::DisassembleOptions::Comment as u32;
94        }
95
96        res
97    }
98}
99
100pub trait Assembler: Default {
101    fn with_env(target_env: crate::TargetEnv) -> Self;
102    fn assemble(
103        &self,
104        text: &str,
105        options: AssemblerOptions,
106    ) -> Result<crate::binary::Binary, crate::error::Error>;
107    fn disassemble(
108        &self,
109        binary: impl AsRef<[u32]>,
110        options: DisassembleOptions,
111    ) -> Result<Option<String>, crate::error::Error>;
112}
113
114pub fn create(te: Option<crate::TargetEnv>) -> impl Assembler {
115    let target_env = te.unwrap_or_default();
116
117    #[cfg(feature = "use-compiled-tools")]
118    {
119        compiled::CompiledAssembler::with_env(target_env)
120    }
121
122    #[cfg(all(feature = "use-installed-tools", not(feature = "use-compiled-tools")))]
123    {
124        tool::ToolAssembler::with_env(target_env)
125    }
126}