ptx_parser/type/
function.rs

1use super::common::{CodeLinkage, Instruction};
2use super::variable::VariableDirective;
3
4/// All directives that describe kernel/function entities.
5#[derive(Debug, Clone, PartialEq)]
6pub enum FunctionKernelDirective {
7    Entry(EntryFunction),
8    Func(FuncFunction),
9    Alias(FunctionAlias),
10}
11
12/// Alias directive relating one function symbol to another.
13#[derive(Debug, Clone, PartialEq)]
14pub struct FunctionAlias {
15    pub alias: String,
16    pub target: String,
17    pub raw: String,
18}
19
20/// A PTX kernel declared with the `.entry` directive.
21#[derive(Debug, Clone, PartialEq)]
22pub struct EntryFunction {
23    pub name: String,
24    pub directives: Vec<FunctionHeaderDirective>,
25    pub params: Vec<VariableDirective>,
26    pub body: FunctionBody,
27}
28
29/// A PTX device function declared with the `.func` directive.
30#[derive(Debug, Clone, PartialEq)]
31pub struct FuncFunction {
32    pub name: String,
33    pub directives: Vec<FunctionHeaderDirective>,
34    pub return_param: Option<VariableDirective>,
35    pub params: Vec<VariableDirective>,
36    pub body: FunctionBody,
37}
38
39/// Statements contained within a PTX function body.
40#[derive(Debug, Clone, PartialEq, Default)]
41pub struct FunctionBody {
42    pub statements: Vec<FunctionStatement>,
43}
44
45/// Directive tokens that may decorate a PTX function header.
46#[derive(Debug, Clone, PartialEq)]
47pub enum FunctionHeaderDirective {
48    Linkage(CodeLinkage),
49    NoReturn,
50    AbiPreserve(u32),
51    AbiPreserveControl(u32),
52    MaxClusterRank(u32),
53    BlocksAreClusters,
54    ExplicitCluster(FunctionDim3),
55    ReqNctaPerCluster(FunctionDim3),
56    MaxNReg(u32),
57    MaxNTid(FunctionDim3),
58    MinNCtaPerSm(u32),
59    ReqNTid(FunctionDim3),
60    MaxNCtaPerSm(u32),
61    Pragma(Vec<String>),
62}
63
64/// Dimension triplet used by several function header directives.
65#[derive(Debug, Clone, PartialEq)]
66pub struct FunctionDim3 {
67    pub x: u32,
68    pub y: Option<u32>,
69    pub z: Option<u32>,
70}
71
72/// Nested statement block enclosed in braces.
73/// Executable items that appear within a function body.
74#[derive(Debug, Clone, PartialEq)]
75pub enum FunctionStatement {
76    Label(String),
77    Directive(StatementDirective),
78    Instruction(Instruction),
79    Block(Vec<FunctionStatement>),
80}
81
82/// .reg .ty name<range>
83#[derive(Debug, Clone, PartialEq)]
84pub struct RegisterDirective {
85    pub name: String,
86    pub ty: Option<String>,
87    pub range: Option<u32>,
88    pub comment: Option<String>,
89    pub raw: String,
90}
91
92/// Directive that applies to individual statements.
93#[derive(Debug, Clone, PartialEq)]
94pub enum StatementDirective {
95    Loc(LocationDirective),
96    Pragma(PragmaDirective),
97    Section(StatementSectionDirective),
98    Reg(RegisterDirective),
99    Local(VariableDirective),
100    Param(VariableDirective),
101    Shared(VariableDirective),
102    Dwarf(DwarfDirective),
103}
104
105/// Raw dwarf directive emitted by the compiler (e.g. @@dwarf).
106#[derive(Debug, Clone, PartialEq)]
107pub struct DwarfDirective {
108    pub keyword: String,
109    pub arguments: Vec<String>,
110    pub comment: Option<String>,
111    pub raw: String,
112}
113
114/// Structured representation of a `.loc` directive inside a PTX function.
115#[derive(Debug, Clone, PartialEq)]
116pub struct LocationDirective {
117    pub file_index: u32,
118    pub line: u32,
119    pub column: u32,
120    pub options: Vec<String>,
121    pub comment: Option<String>,
122    pub raw: String,
123}
124
125/// Structured representation of a `.pragma` directive.
126#[derive(Debug, Clone, PartialEq)]
127pub struct PragmaDirective {
128    pub arguments: Vec<String>,
129    pub comment: Option<String>,
130    pub raw: String,
131}
132
133/// Structured representation of a `.section` directive inside a function body.
134#[derive(Debug, Clone, PartialEq)]
135pub struct StatementSectionDirective {
136    pub name: String,
137    pub arguments: Vec<String>,
138    pub comment: Option<String>,
139    pub raw: String,
140}