1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#[cfg(feature = "use-compiled-tools")]
pub mod compiled;

#[cfg(feature = "use-installed-tools")]
pub mod tool;

#[derive(Copy, Clone, Default)]
pub struct AssemblerOptions {
    /// Numeric IDs in the binary will have the same values as in the source.
    /// Non-numeric IDs are allocated by filling in the gaps, starting with 1
    /// and going up.
    pub preserve_numeric_ids: bool,
}

#[allow(clippy::from_over_into)]
impl Into<u32> for AssemblerOptions {
    fn into(self) -> u32 {
        // This is weird, the "none" is 1, so I'm not sure if that means having
        // it disables all other options or...?
        let mut res = 0; //assembler::BinaryOptions::None as u32;

        if self.preserve_numeric_ids {
            res |= spirv_tools_sys::assembler::BinaryOptions::PreserveNumberIds as u32;
        }

        res
    }
}

#[derive(Copy, Clone)]
pub struct DisassembleOptions {
    /// Print to stdout.
    pub print: bool,
    /// Add color codes to output
    pub color: bool,
    /// Indent assembly
    pub indent: bool,
    pub show_byte_offset: bool,
    /// Do not output the module header as leading comments in the assembly.
    pub no_header: bool,
    /// Use friendly names where possible.  The heuristic may expand over
    /// time, but will use common names for scalar types, and debug names from
    /// OpName instructions.
    pub use_friendly_names: bool,
    /// Add some comments to the generated assembly
    pub comment: bool,
}

impl Default for DisassembleOptions {
    fn default() -> Self {
        Self {
            print: false,
            color: false,
            indent: true,
            show_byte_offset: false,
            no_header: false,
            use_friendly_names: true,
            comment: true,
        }
    }
}

#[allow(clippy::from_over_into)]
impl Into<u32> for DisassembleOptions {
    fn into(self) -> u32 {
        let mut res = 0;

        if self.print {
            res |= spirv_tools_sys::assembler::DisassembleOptions::Print as u32;
        }

        if self.color {
            res |= spirv_tools_sys::assembler::DisassembleOptions::Color as u32;
        }

        if self.indent {
            res |= spirv_tools_sys::assembler::DisassembleOptions::Indent as u32;
        }

        if self.show_byte_offset {
            res |= spirv_tools_sys::assembler::DisassembleOptions::ShowByteOffset as u32;
        }

        if self.no_header {
            res |= spirv_tools_sys::assembler::DisassembleOptions::NoHeader as u32;
        }

        if self.use_friendly_names {
            res |= spirv_tools_sys::assembler::DisassembleOptions::FriendlyNames as u32;
        }

        if self.comment {
            res |= spirv_tools_sys::assembler::DisassembleOptions::Comment as u32;
        }

        res
    }
}

pub trait Assembler: Default {
    fn with_env(target_env: crate::TargetEnv) -> Self;
    fn assemble(
        &self,
        text: &str,
        options: AssemblerOptions,
    ) -> Result<crate::binary::Binary, crate::error::Error>;
    fn disassemble(
        &self,
        binary: impl AsRef<[u32]>,
        options: DisassembleOptions,
    ) -> Result<Option<String>, crate::error::Error>;
}

pub fn create(te: Option<crate::TargetEnv>) -> impl Assembler {
    let target_env = te.unwrap_or_default();

    #[cfg(feature = "use-compiled-tools")]
    {
        compiled::CompiledAssembler::with_env(target_env)
    }

    #[cfg(all(feature = "use-installed-tools", not(feature = "use-compiled-tools")))]
    {
        tool::ToolAssembler::with_env(target_env)
    }
}