Skip to main content

ptx_parser/type/
variable.rs

1use crate::Spanned;
2use crate::parser::Span;
3use crate::r#type::common::{AttributeDirective, DataType};
4use crate::r#type::{FunctionSymbol, Immediate, VariableSymbol};
5use serde::Serialize;
6
7/// Module-level declarations that reserve storage in a specific address space.
8#[derive(Debug, Clone, PartialEq, Eq, Spanned, Serialize)]
9pub enum ModuleVariableDirective {
10    /// Deprecated `.tex` variable declaration.
11    ///
12    /// Example:
13    /// .tex .u32 tex_a; // is equivalent to .global .texref tex_a;
14    Tex {
15        directive: VariableDirective,
16        span: Span,
17    },
18    Shared {
19        directive: VariableDirective,
20        span: Span,
21    },
22    Global {
23        directive: VariableDirective,
24        span: Span,
25    },
26    Const {
27        directive: VariableDirective,
28        span: Span,
29    },
30}
31
32/// Module-scoped variable declaration shared by `.tex`, `.shared`, `.global`, and `.const`.
33#[derive(Debug, Clone, PartialEq, Eq, Spanned, Serialize)]
34pub struct VariableDirective {
35    /// Example:
36    /// .global .attribute(.managed) .s32 g;
37    /// .global .attribute(.managed) .u64 x;
38    /// .global .attribute(.unified(19,95)) .f32 f;
39    pub attributes: Vec<AttributeDirective>,
40    /// Modifiers applied to the variable (e.g., `.v4`, `.align 16`, `.ptr`).
41    pub modifiers: Vec<VariableModifier>,
42    /// The data type of the variable (e.g., `.s32`, `.f64`).
43    pub ty: DataType,
44    /// The variable name.
45    pub name: VariableSymbol,
46    /// The array dimensions, if any.
47    /// Example:
48    /// .global .s32 offset[][2] = { {-1, 0}, {0, -1}, {1, 0}, {0, 1} };
49    pub array_dims: Vec<Option<u64>>,
50    /// Optional global initializer for the variable.
51    pub initializer: Option<GlobalInitializer>,
52    pub span: Span,
53}
54
55/// Qualifiers left on module variable declarations (e.g. `.v4`).
56#[derive(Debug, Clone, PartialEq, Eq, Spanned, Serialize)]
57pub enum VariableModifier {
58    Vector { value: u32, span: Span },
59    Alignment { value: u32, span: Span },
60    Ptr { span: Span },
61}
62
63/// Parameters, used in function declarations and calls, e.g., `.param .b32 p`.
64#[derive(Debug, Clone, PartialEq, Eq, Spanned, Serialize)]
65pub enum ParameterDirective {
66    Register {
67        ty: DataType,
68        name: VariableSymbol,
69        span: Span,
70    },
71    Parameter {
72        align: Option<u32>,
73        ptr: bool,
74        space: Option<ParamStateSpace>,
75        ty: DataType,
76        name: VariableSymbol,
77        array: Vec<Option<u64>>,
78        span: Span,
79    },
80}
81
82/// Address space qualifiers for parameters.
83#[derive(Debug, Clone, PartialEq, Eq, Spanned, Serialize)]
84pub enum ParamStateSpace {
85    /// .const
86    Const { span: Span },
87    /// .global
88    Global { span: Span },
89    /// .local
90    Local { span: Span },
91    /// .shared
92    Shared { span: Span },
93}
94
95// Values that can appear in global initialiser lists.
96#[derive(Debug, Clone, PartialEq, Eq, Spanned, Serialize)]
97pub enum InitializerValue {
98    NumericLiteral { value: Immediate, span: Span },
99    FunctionSymbol { name: FunctionSymbol, span: Span },
100    StringLiteral { value: String, span: Span },
101}
102
103/// Structured representation of a global variable initialiser.
104#[derive(Debug, Clone, PartialEq, Eq, Spanned, Serialize)]
105pub enum GlobalInitializer {
106    Scalar {
107        value: InitializerValue,
108        span: Span,
109    },
110    Aggregate {
111        values: Vec<GlobalInitializer>,
112        span: Span,
113    },
114}