spirv_tools/assembler/
tool.rs1pub struct ToolAssembler {
2 target_env: crate::TargetEnv,
3}
4
5use super::Assembler;
6
7impl Assembler for ToolAssembler {
8 fn with_env(target_env: crate::TargetEnv) -> Self {
9 Self { target_env }
10 }
11
12 fn assemble(
13 &self,
14 text: &str,
15 options: super::AssemblerOptions,
16 ) -> Result<crate::binary::Binary, crate::error::Error> {
17 let mut cmd = std::process::Command::new("spirv-as");
18 cmd.arg("--target-env").arg(self.target_env.to_string());
19
20 if options.preserve_numeric_ids {
21 cmd.arg("--preserve-numeric-ids");
22 }
23
24 let cmd_output =
25 crate::cmd::exec(cmd, Some(text.as_bytes()), crate::cmd::Output::Retrieve)?;
26
27 crate::binary::Binary::try_from(cmd_output.binary)
28 }
29
30 fn disassemble(
31 &self,
32 binary: impl AsRef<[u32]>,
33 options: super::DisassembleOptions,
34 ) -> Result<Option<String>, crate::error::Error> {
35 let mut cmd = std::process::Command::new("spirv-dis");
36
37 if options.color {
38 cmd.arg("--color");
39 }
40
41 if !options.indent {
42 cmd.arg("--no-indent");
43 }
44
45 if options.show_byte_offset {
46 cmd.arg("--offsets");
47 }
48
49 if options.no_header {
50 cmd.arg("--no-header");
51 }
52
53 if !options.use_friendly_names {
54 cmd.arg("--raw-id");
55 }
56
57 if options.comment {
58 cmd.arg("--comment");
59 }
60
61 let bytes = crate::binary::from_binary(binary.as_ref());
62
63 let cmd_output = crate::cmd::exec(cmd, Some(bytes), crate::cmd::Output::Retrieve)?;
64
65 String::from_utf8(cmd_output.binary)
66 .map_err(|e| crate::error::Error {
67 inner: spirv_tools_sys::shared::SpirvResult::InvalidText,
68 diagnostic: Some(format!("spirv disassemble returned non-utf8 text: {e}").into()),
69 })
70 .map(|s| if s.is_empty() { None } else { Some(s) })
71 }
72}
73
74impl Default for ToolAssembler {
75 fn default() -> Self {
76 Self::with_env(crate::TargetEnv::default())
77 }
78}