Skip to main content

ptx_parser/unparser/
module.rs

1use super::PtxUnparser;
2use crate::{
3    lexer::PtxToken,
4    r#type::module::*,
5    unparser::{push_decimal, push_directive, push_identifier, push_newline, push_space},
6};
7
8impl PtxUnparser for VersionDirective {
9    fn unparse_tokens(&self, tokens: &mut Vec<PtxToken>) {
10        self.unparse_tokens_mode(tokens, false);
11    }
12
13    fn unparse_tokens_mode(&self, tokens: &mut Vec<PtxToken>, spaced: bool) {
14        push_directive(tokens, "version");
15        push_space(tokens, spaced);
16        let value = format!("{}.{:}", self.major, self.minor);
17        tokens.push(PtxToken::Float(value));
18    }
19}
20
21impl PtxUnparser for TargetDirective {
22    fn unparse_tokens(&self, tokens: &mut Vec<PtxToken>) {
23        self.unparse_tokens_mode(tokens, false);
24    }
25
26    fn unparse_tokens_mode(&self, tokens: &mut Vec<PtxToken>, spaced: bool) {
27        push_directive(tokens, "target");
28        push_space(tokens, spaced);
29        for (index, entry) in self.entries.iter().enumerate() {
30            if index > 0 {
31                tokens.push(PtxToken::Comma);
32                push_space(tokens, spaced);
33            }
34            let name = match entry {
35                TargetString::Sm120a { .. } => "sm_120a",
36                TargetString::Sm120f { .. } => "sm_120f",
37                TargetString::Sm120 { .. } => "sm_120",
38                TargetString::Sm121a { .. } => "sm_121a",
39                TargetString::Sm121f { .. } => "sm_121f",
40                TargetString::Sm121 { .. } => "sm_121",
41                TargetString::Sm110a { .. } => "sm_110a",
42                TargetString::Sm110f { .. } => "sm_110f",
43                TargetString::Sm110 { .. } => "sm_110",
44                TargetString::Sm100a { .. } => "sm_100a",
45                TargetString::Sm100f { .. } => "sm_100f",
46                TargetString::Sm100 { .. } => "sm_100",
47                TargetString::Sm101a { .. } => "sm_101a",
48                TargetString::Sm101f { .. } => "sm_101f",
49                TargetString::Sm101 { .. } => "sm_101",
50                TargetString::Sm103a { .. } => "sm_103a",
51                TargetString::Sm103f { .. } => "sm_103f",
52                TargetString::Sm103 { .. } => "sm_103",
53                TargetString::Sm90a { .. } => "sm_90a",
54                TargetString::Sm90 { .. } => "sm_90",
55                TargetString::Sm80 { .. } => "sm_80",
56                TargetString::Sm86 { .. } => "sm_86",
57                TargetString::Sm87 { .. } => "sm_87",
58                TargetString::Sm88 { .. } => "sm_88",
59                TargetString::Sm89 { .. } => "sm_89",
60                TargetString::Sm70 { .. } => "sm_70",
61                TargetString::Sm72 { .. } => "sm_72",
62                TargetString::Sm75 { .. } => "sm_75",
63                TargetString::Sm60 { .. } => "sm_60",
64                TargetString::Sm61 { .. } => "sm_61",
65                TargetString::Sm62 { .. } => "sm_62",
66                TargetString::Sm50 { .. } => "sm_50",
67                TargetString::Sm52 { .. } => "sm_52",
68                TargetString::Sm53 { .. } => "sm_53",
69                TargetString::Sm30 { .. } => "sm_30",
70                TargetString::Sm32 { .. } => "sm_32",
71                TargetString::Sm35 { .. } => "sm_35",
72                TargetString::Sm37 { .. } => "sm_37",
73                TargetString::Sm20 { .. } => "sm_20",
74                TargetString::Sm10 { .. } => "sm_10",
75                TargetString::Sm11 { .. } => "sm_11",
76                TargetString::Sm12 { .. } => "sm_12",
77                TargetString::Sm13 { .. } => "sm_13",
78                TargetString::TexmodeUnified { .. } => "texmode_unified",
79                TargetString::TexmodeIndependent { .. } => "texmode_independent",
80                TargetString::Debug { .. } => "debug",
81                TargetString::MapF64ToF32 { .. } => "map_f64_to_f32",
82            };
83            push_identifier(tokens, name);
84        }
85    }
86}
87
88impl PtxUnparser for AddressSizeDirective {
89    fn unparse_tokens(&self, tokens: &mut Vec<PtxToken>) {
90        self.unparse_tokens_mode(tokens, false);
91    }
92
93    fn unparse_tokens_mode(&self, tokens: &mut Vec<PtxToken>, spaced: bool) {
94        push_directive(tokens, "address_size");
95        push_space(tokens, spaced);
96        match &self.size {
97            AddressSize::Size32 { .. } => push_decimal(tokens, 32),
98            AddressSize::Size64 { .. } => push_decimal(tokens, 64),
99        }
100    }
101}
102
103impl PtxUnparser for ModuleInfoDirectiveKind {
104    fn unparse_tokens(&self, tokens: &mut Vec<PtxToken>) {
105        self.unparse_tokens_mode(tokens, false);
106    }
107
108    fn unparse_tokens_mode(&self, tokens: &mut Vec<PtxToken>, spaced: bool) {
109        match self {
110            ModuleInfoDirectiveKind::Version { directive, .. } => {
111                directive.unparse_tokens_mode(tokens, spaced)
112            }
113            ModuleInfoDirectiveKind::Target { directive, .. } => {
114                directive.unparse_tokens_mode(tokens, spaced)
115            }
116            ModuleInfoDirectiveKind::AddressSize { directive, .. } => {
117                directive.unparse_tokens_mode(tokens, spaced)
118            }
119        }
120    }
121}
122
123impl PtxUnparser for FileDirective {
124    fn unparse_tokens(&self, tokens: &mut Vec<PtxToken>) {
125        self.unparse_tokens_mode(tokens, false);
126    }
127
128    fn unparse_tokens_mode(&self, tokens: &mut Vec<PtxToken>, spaced: bool) {
129        push_directive(tokens, "file");
130        push_space(tokens, spaced);
131        push_decimal(tokens, self.index);
132        push_space(tokens, spaced);
133        tokens.push(PtxToken::StringLiteral(self.path.clone()));
134        if let Some(timestamp) = self.timestamp {
135            tokens.push(PtxToken::Comma);
136            push_space(tokens, spaced);
137            push_decimal(tokens, timestamp);
138            if let Some(size) = self.file_size {
139                tokens.push(PtxToken::Comma);
140                push_space(tokens, spaced);
141                push_decimal(tokens, size);
142            }
143        }
144    }
145}
146
147impl PtxUnparser for ModuleDebugDirective {
148    fn unparse_tokens(&self, tokens: &mut Vec<PtxToken>) {
149        self.unparse_tokens_mode(tokens, false);
150    }
151
152    fn unparse_tokens_mode(&self, tokens: &mut Vec<PtxToken>, spaced: bool) {
153        match self {
154            ModuleDebugDirective::File { directive, .. } => {
155                directive.unparse_tokens_mode(tokens, spaced)
156            }
157            ModuleDebugDirective::Section { directive, .. } => {
158                directive.unparse_tokens_mode(tokens, spaced)
159            }
160            ModuleDebugDirective::Dwarf { directive, .. } => {
161                directive.unparse_tokens_mode(tokens, spaced)
162            }
163        }
164    }
165}
166
167impl PtxUnparser for ModuleDirective {
168    fn unparse_tokens(&self, tokens: &mut Vec<PtxToken>) {
169        self.unparse_tokens_mode(tokens, false);
170    }
171
172    fn unparse_tokens_mode(&self, tokens: &mut Vec<PtxToken>, spaced: bool) {
173        match self {
174            ModuleDirective::ModuleVariable {
175                linkage, directive, ..
176            } => {
177                if let Some(link) = linkage {
178                    link.unparse_tokens_mode(tokens, spaced);
179                    push_space(tokens, spaced);
180                }
181                directive.unparse_tokens_mode(tokens, spaced)
182            }
183            ModuleDirective::EntryFunction {
184                linkage, directive, ..
185            } => {
186                if let Some(link) = linkage {
187                    link.unparse_tokens_mode(tokens, spaced);
188                    push_space(tokens, spaced);
189                }
190                directive.unparse_tokens_mode(tokens, spaced)
191            }
192            ModuleDirective::FuncFunction {
193                linkage, directive, ..
194            } => {
195                if let Some(link) = linkage {
196                    link.unparse_tokens_mode(tokens, spaced);
197                    push_space(tokens, spaced);
198                }
199                directive.unparse_tokens_mode(tokens, spaced)
200            }
201            ModuleDirective::AliasFunction { directive, .. } => {
202                directive.unparse_tokens_mode(tokens, spaced)
203            }
204            ModuleDirective::ModuleInfo { directive, .. } => {
205                directive.unparse_tokens_mode(tokens, spaced)
206            }
207            ModuleDirective::Debug { directive, .. } => {
208                directive.unparse_tokens_mode(tokens, spaced)
209            }
210        }
211    }
212}
213
214impl PtxUnparser for Module {
215    fn unparse_tokens(&self, tokens: &mut Vec<PtxToken>) {
216        for directive in &self.directives {
217            directive.unparse_tokens(tokens);
218        }
219    }
220
221    fn unparse_tokens_mode(&self, tokens: &mut Vec<PtxToken>, spaced: bool) {
222        if !spaced {
223            return self.unparse_tokens(tokens);
224        }
225
226        for (idx, directive) in self.directives.iter().enumerate() {
227            if idx > 0 {
228                push_newline(tokens, spaced);
229            }
230            directive.unparse_tokens_mode(tokens, true);
231        }
232        push_newline(tokens, spaced);
233    }
234}