Skip to main content

ptx_parser/type/
module.rs

1use super::common::*;
2use super::function::{DwarfDirective, SectionDirective};
3use super::variable::ModuleVariableDirective;
4use crate::Spanned;
5use crate::parser::Span;
6use crate::r#type::{AliasFunctionDirective, EntryFunctionDirective, FuncFunctionDirective};
7use serde::Serialize;
8
9/// A full PTX module containing directives and function definitions.
10#[derive(Debug, Clone, PartialEq, Spanned, Default, Serialize)]
11pub struct Module {
12    pub directives: Vec<ModuleDirective>,
13    pub span: Span,
14}
15
16/// Module-level directives recognised by the parser.
17#[derive(Debug, Clone, PartialEq, Spanned, Serialize)]
18pub enum ModuleDirective {
19    ModuleVariable {
20        linkage: Option<DataLinkage>,
21        directive: ModuleVariableDirective,
22        span: Span,
23    },
24    EntryFunction {
25        linkage: Option<CodeLinkage>,
26        directive: EntryFunctionDirective,
27        span: Span,
28    },
29    FuncFunction {
30        linkage: Option<CodeLinkage>,
31        directive: FuncFunctionDirective,
32        span: Span,
33    },
34    AliasFunction {
35        directive: AliasFunctionDirective,
36        span: Span,
37    },
38    ModuleInfo {
39        directive: ModuleInfoDirectiveKind,
40        span: Span,
41    },
42    Debug {
43        directive: ModuleDebugDirective,
44        span: Span,
45    },
46}
47
48/// Directives that apply to the PTX module as a whole.
49#[derive(Debug, Clone, PartialEq, Spanned, Serialize)]
50pub enum ModuleInfoDirectiveKind {
51    Version {
52        directive: VersionDirective,
53        span: Span,
54    },
55    Target {
56        directive: TargetDirective,
57        span: Span,
58    },
59    AddressSize {
60        directive: AddressSizeDirective,
61        span: Span,
62    },
63}
64
65/// Structured representation of the `.version` directive.
66///
67/// Syntax:
68/// .version  major.minor    // major, minor are integers
69///
70/// Example:
71/// .version 3.1
72/// .version 3.0
73/// .version 2.3
74#[derive(Debug, Clone, PartialEq, Spanned, Serialize)]
75pub struct VersionDirective {
76    pub major: u32,
77    pub minor: u32,
78    pub span: Span,
79}
80
81/// Structured representation of the `.target` directive.
82///
83/// Syntax:
84/// .target stringlist         // comma separated list of target specifiers
85/// string = { sm_120a, sm_120f, sm_120,          // sm_12x target architectures
86/// sm_121a, sm_121f, sm_121,          // sm_12x target architectures
87/// sm_110a, sm_110f, sm_110,          // sm_11x target architectures
88/// sm_100a, sm_100f, sm_100,          // sm_10x target architectures
89/// sm_101a, sm_101f, sm_101,          // sm_10x target architectures
90/// sm_103a, sm_103f, sm_103           // sm_10x target architectures
91/// sm_90a, sm_90,                     // sm_9x target architectures
92/// sm_80, sm_86, sm_87, sm_88, sm_89, // sm_8x target architectures
93/// sm_70, sm_72, sm_75,               // sm_7x target architectures
94/// sm_60, sm_61, sm_62,               // sm_6x target architectures
95/// sm_50, sm_52, sm_53,               // sm_5x target architectures
96/// sm_30, sm_32, sm_35, sm_37,        // sm_3x target architectures
97/// sm_20,                             // sm_2x target architectures
98/// sm_10, sm_11, sm_12, sm_13,        // sm_1x target architectures
99/// texmode_unified, texmode_independent,   // texturing mode
100/// debug,                                  // platform option
101/// map_f64_to_f32 };                       // platform option
102///
103/// Example:
104/// .target sm_10       // baseline target architecture
105/// .target sm_13       // supports double-precision
106/// .target sm_20, texmode_independent
107/// .target sm_90       // baseline target architecture
108/// .target sm_90a      // PTX using architecture-specific features
109/// .target sm_100f     // PTX using family-specific features
110#[derive(Debug, Clone, PartialEq, Spanned, Serialize)]
111pub struct TargetDirective {
112    pub entries: Vec<TargetString>,
113    pub span: Span,
114}
115
116/// Structured representation of the `.address_size` directive.
117///
118/// Syntax:
119/// .address_size  address-size
120/// address-size = { 32, 64 };
121///
122/// Example:
123/// .address_size 64
124#[derive(Debug, Clone, PartialEq, Spanned, Serialize)]
125pub struct AddressSizeDirective {
126    pub size: AddressSize,
127    pub span: Span,
128}
129
130/// Debugging directives defined by the PTX ISA.
131#[derive(Debug, Clone, PartialEq, Spanned, Serialize)]
132pub enum ModuleDebugDirective {
133    File {
134        directive: FileDirective,
135        span: Span,
136    },
137    Section {
138        directive: SectionDirective,
139        span: Span,
140    },
141    Dwarf {
142        directive: DwarfDirective,
143        span: Span,
144    },
145}
146
147/// Structured representation of the `.file` directive.
148///
149/// Syntax:
150/// .file file_index "filename" {, timestamp, file_size}
151///
152/// Example:
153/// .file 1 "example.cu"
154/// .file 2 "kernel.cu"
155/// .file 1 "kernel.cu", 1339013327, 64118
156#[derive(Debug, Clone, PartialEq, Spanned, Serialize)]
157pub struct FileDirective {
158    pub index: u32,
159    pub path: String,
160    pub timestamp: Option<u64>,
161    pub file_size: Option<u64>,
162    pub span: Span,
163}
164
165/// Target specifiers used in the `.target` directive.
166#[derive(Debug, Clone, PartialEq, Spanned, Serialize)]
167pub enum TargetString {
168    Sm120a { span: Span },
169    Sm120f { span: Span },
170    Sm120 { span: Span },
171    Sm121a { span: Span },
172    Sm121f { span: Span },
173    Sm121 { span: Span },
174    Sm110a { span: Span },
175    Sm110f { span: Span },
176    Sm110 { span: Span },
177    Sm100a { span: Span },
178    Sm100f { span: Span },
179    Sm100 { span: Span },
180    Sm101a { span: Span },
181    Sm101f { span: Span },
182    Sm101 { span: Span },
183    Sm103a { span: Span },
184    Sm103f { span: Span },
185    Sm103 { span: Span },
186    Sm90a { span: Span },
187    Sm90 { span: Span },
188    Sm80 { span: Span },
189    Sm86 { span: Span },
190    Sm87 { span: Span },
191    Sm88 { span: Span },
192    Sm89 { span: Span },
193    Sm70 { span: Span },
194    Sm72 { span: Span },
195    Sm75 { span: Span },
196    Sm60 { span: Span },
197    Sm61 { span: Span },
198    Sm62 { span: Span },
199    Sm50 { span: Span },
200    Sm52 { span: Span },
201    Sm53 { span: Span },
202    Sm30 { span: Span },
203    Sm32 { span: Span },
204    Sm35 { span: Span },
205    Sm37 { span: Span },
206    Sm20 { span: Span },
207    Sm10 { span: Span },
208    Sm11 { span: Span },
209    Sm12 { span: Span },
210    Sm13 { span: Span },
211    TexmodeUnified { span: Span },
212    TexmodeIndependent { span: Span },
213    Debug { span: Span },
214    MapF64ToF32 { span: Span },
215}
216
217/// Address size options for the `.address_size` directive.
218#[derive(Debug, Clone, PartialEq, Spanned, Serialize)]
219pub enum AddressSize {
220    Size32 { span: Span },
221    Size64 { span: Span },
222}