|
| 1 | +mod module_builder; |
| 2 | +mod translator; |
| 3 | +mod types; |
| 4 | + |
| 5 | +#[cfg(test)] |
| 6 | +mod tests; |
| 7 | + |
| 8 | +use thiserror::Error; |
| 9 | + |
| 10 | +use crate::assembly::options::TextToBinaryOptions; |
| 11 | +use crate::diagnostic::DiagnosticMessage; |
| 12 | +use crate::target_env::TargetEnv; |
| 13 | +use crate::validation::span::SpanMap; |
| 14 | + |
| 15 | +pub use module_builder::ModuleBuilder; |
| 16 | +pub use translator::{assemble_instructions, AssemblyTranslator}; |
| 17 | +pub use types::{ |
| 18 | + ArrayTypeInfo, CompositeTypeInfo, MatrixTypeInfo, MemberLayout, StructTypeInfo, VectorTypeInfo, |
| 19 | +}; |
| 20 | + |
| 21 | +use translator::{ |
| 22 | + assemble_text_with_translator, assemble_text_with_translator_for_spans, |
| 23 | +}; |
| 24 | + |
| 25 | +/// Error emitted when the assembler produces diagnostics instead of a finished module. |
| 26 | +#[derive(Debug, Error)] |
| 27 | +#[error("assembly failed with diagnostics")] |
| 28 | +pub struct AssemblyError { |
| 29 | + diagnostics: Vec<DiagnosticMessage<'static>>, |
| 30 | +} |
| 31 | + |
| 32 | +impl AssemblyError { |
| 33 | + pub(crate) fn new(diagnostics: Vec<DiagnosticMessage<'static>>) -> Self { |
| 34 | + Self { diagnostics } |
| 35 | + } |
| 36 | + |
| 37 | + /// Borrows the underlying diagnostics describing the failure. |
| 38 | + pub fn diagnostics(&self) -> &[DiagnosticMessage<'static>] { |
| 39 | + &self.diagnostics |
| 40 | + } |
| 41 | + |
| 42 | + /// Consumes this error and returns the owned diagnostics. |
| 43 | + pub fn into_diagnostics(self) -> Vec<DiagnosticMessage<'static>> { |
| 44 | + self.diagnostics |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/// Result of assembling SPIR-V text with span tracking enabled. |
| 49 | +#[derive(Debug)] |
| 50 | +pub struct AssemblyWithSpans { |
| 51 | + /// The assembled SPIR-V binary words. |
| 52 | + pub words: Vec<u32>, |
| 53 | + /// Map from result IDs to their source locations. |
| 54 | + pub span_map: SpanMap, |
| 55 | +} |
| 56 | + |
| 57 | +/// Assembles a block of textual SPIR-V instructions separated by newlines into a binary module. |
| 58 | +/// Returns the assembled words on success along with any diagnostics emitted along the way. |
| 59 | +pub fn assemble_text(text: &str) -> Result<Vec<u32>, AssemblyError> { |
| 60 | + assemble_text_with_translator(text, AssemblyTranslator::new()) |
| 61 | +} |
| 62 | + |
| 63 | +/// Assembles SPIR-V text using the provided target environment to configure the module header. |
| 64 | +pub fn assemble_text_with_env(text: &str, env: TargetEnv) -> Result<Vec<u32>, AssemblyError> { |
| 65 | + assemble_text_with_options(text, env, TextToBinaryOptions::NONE) |
| 66 | +} |
| 67 | + |
| 68 | +/// Assembles SPIR-V text with the provided options and target environment. |
| 69 | +pub fn assemble_text_with_options( |
| 70 | + text: &str, |
| 71 | + env: TargetEnv, |
| 72 | + options: TextToBinaryOptions, |
| 73 | +) -> Result<Vec<u32>, AssemblyError> { |
| 74 | + assemble_text_with_translator( |
| 75 | + text, |
| 76 | + AssemblyTranslator::with_target_env_and_options(env, options), |
| 77 | + ) |
| 78 | +} |
| 79 | + |
| 80 | +/// Assembles SPIR-V text and tracks source locations for all result IDs. |
| 81 | +/// |
| 82 | +/// This is useful for validation error reporting, as the span map can be |
| 83 | +/// passed to the validator to provide precise source locations in errors. |
| 84 | +/// |
| 85 | +/// # Example |
| 86 | +/// |
| 87 | +/// ```ignore |
| 88 | +/// use spirv_tools_core::assembly::assemble_text_with_spans; |
| 89 | +/// |
| 90 | +/// let text = r#" |
| 91 | +/// OpCapability Shader |
| 92 | +/// OpMemoryModel Logical GLSL450 |
| 93 | +/// %void = OpTypeVoid |
| 94 | +/// "#; |
| 95 | +/// |
| 96 | +/// let result = assemble_text_with_spans(text)?; |
| 97 | +/// // result.span_map now contains the source location for %void |
| 98 | +/// ``` |
| 99 | +pub fn assemble_text_with_spans(text: &str) -> Result<AssemblyWithSpans, AssemblyError> { |
| 100 | + assemble_text_with_spans_and_env(text, TargetEnv::Universal1_6) |
| 101 | +} |
| 102 | + |
| 103 | +/// Assembles SPIR-V text with span tracking and a specific target environment. |
| 104 | +pub fn assemble_text_with_spans_and_env( |
| 105 | + text: &str, |
| 106 | + env: TargetEnv, |
| 107 | +) -> Result<AssemblyWithSpans, AssemblyError> { |
| 108 | + assemble_text_with_spans_full(text, env, TextToBinaryOptions::NONE) |
| 109 | +} |
| 110 | + |
| 111 | +/// Assembles SPIR-V text with span tracking, environment, and options. |
| 112 | +pub fn assemble_text_with_spans_full( |
| 113 | + text: &str, |
| 114 | + env: TargetEnv, |
| 115 | + options: TextToBinaryOptions, |
| 116 | +) -> Result<AssemblyWithSpans, AssemblyError> { |
| 117 | + let translator = AssemblyTranslator::with_full_options(env, options, true); |
| 118 | + assemble_text_with_translator_for_spans(text, translator) |
| 119 | +} |
0 commit comments