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},
6};
7
8fn push_entries(tokens: &mut Vec<PtxToken>, entries: &[TargetString]) {
9    for (index, entry) in entries.iter().enumerate() {
10        if index > 0 {
11            tokens.push(PtxToken::Comma);
12        }
13        let name = match entry {
14            TargetString::Sm120a { .. } => "sm_120a",
15            TargetString::Sm120f { .. } => "sm_120f",
16            TargetString::Sm120 { .. } => "sm_120",
17            TargetString::Sm121a { .. } => "sm_121a",
18            TargetString::Sm121f { .. } => "sm_121f",
19            TargetString::Sm121 { .. } => "sm_121",
20            TargetString::Sm110a { .. } => "sm_110a",
21            TargetString::Sm110f { .. } => "sm_110f",
22            TargetString::Sm110 { .. } => "sm_110",
23            TargetString::Sm100a { .. } => "sm_100a",
24            TargetString::Sm100f { .. } => "sm_100f",
25            TargetString::Sm100 { .. } => "sm_100",
26            TargetString::Sm101a { .. } => "sm_101a",
27            TargetString::Sm101f { .. } => "sm_101f",
28            TargetString::Sm101 { .. } => "sm_101",
29            TargetString::Sm103a { .. } => "sm_103a",
30            TargetString::Sm103f { .. } => "sm_103f",
31            TargetString::Sm103 { .. } => "sm_103",
32            TargetString::Sm90a { .. } => "sm_90a",
33            TargetString::Sm90 { .. } => "sm_90",
34            TargetString::Sm80 { .. } => "sm_80",
35            TargetString::Sm86 { .. } => "sm_86",
36            TargetString::Sm87 { .. } => "sm_87",
37            TargetString::Sm88 { .. } => "sm_88",
38            TargetString::Sm89 { .. } => "sm_89",
39            TargetString::Sm70 { .. } => "sm_70",
40            TargetString::Sm72 { .. } => "sm_72",
41            TargetString::Sm75 { .. } => "sm_75",
42            TargetString::Sm60 { .. } => "sm_60",
43            TargetString::Sm61 { .. } => "sm_61",
44            TargetString::Sm62 { .. } => "sm_62",
45            TargetString::Sm50 { .. } => "sm_50",
46            TargetString::Sm52 { .. } => "sm_52",
47            TargetString::Sm53 { .. } => "sm_53",
48            TargetString::Sm30 { .. } => "sm_30",
49            TargetString::Sm32 { .. } => "sm_32",
50            TargetString::Sm35 { .. } => "sm_35",
51            TargetString::Sm37 { .. } => "sm_37",
52            TargetString::Sm20 { .. } => "sm_20",
53            TargetString::Sm10 { .. } => "sm_10",
54            TargetString::Sm11 { .. } => "sm_11",
55            TargetString::Sm12 { .. } => "sm_12",
56            TargetString::Sm13 { .. } => "sm_13",
57            TargetString::TexmodeUnified { .. } => "texmode_unified",
58            TargetString::TexmodeIndependent { .. } => "texmode_independent",
59            TargetString::Debug { .. } => "debug",
60            TargetString::MapF64ToF32 { .. } => "map_f64_to_f32",
61        };
62        push_identifier(tokens, name);
63    }
64}
65
66impl PtxUnparser for VersionDirective {
67    fn unparse_tokens(&self, tokens: &mut Vec<PtxToken>) {
68        push_directive(tokens, "version");
69        let value = format!("{}.{:}", self.major, self.minor);
70        tokens.push(PtxToken::Float(value));
71    }
72}
73
74impl PtxUnparser for TargetDirective {
75    fn unparse_tokens(&self, tokens: &mut Vec<PtxToken>) {
76        push_directive(tokens, "target");
77        push_entries(tokens, &self.entries);
78    }
79}
80
81impl PtxUnparser for AddressSizeDirective {
82    fn unparse_tokens(&self, tokens: &mut Vec<PtxToken>) {
83        push_directive(tokens, "address_size");
84        match &self.size {
85            AddressSize::Size32 { .. } => push_decimal(tokens, 32),
86            AddressSize::Size64 { .. } => push_decimal(tokens, 64),
87        }
88    }
89}
90
91impl PtxUnparser for ModuleInfoDirectiveKind {
92    fn unparse_tokens(&self, tokens: &mut Vec<PtxToken>) {
93        match self {
94            ModuleInfoDirectiveKind::Version { directive, .. } => directive.unparse_tokens(tokens),
95            ModuleInfoDirectiveKind::Target { directive, .. } => directive.unparse_tokens(tokens),
96            ModuleInfoDirectiveKind::AddressSize { directive, .. } => {
97                directive.unparse_tokens(tokens)
98            }
99        }
100    }
101}
102
103impl PtxUnparser for FileDirective {
104    fn unparse_tokens(&self, tokens: &mut Vec<PtxToken>) {
105        push_directive(tokens, "file");
106        push_decimal(tokens, self.index);
107        tokens.push(PtxToken::StringLiteral(self.path.clone()));
108        if let Some(timestamp) = self.timestamp {
109            tokens.push(PtxToken::Comma);
110            push_decimal(tokens, timestamp);
111            if let Some(size) = self.file_size {
112                tokens.push(PtxToken::Comma);
113                push_decimal(tokens, size);
114            }
115        }
116    }
117}
118
119impl PtxUnparser for ModuleDebugDirective {
120    fn unparse_tokens(&self, tokens: &mut Vec<PtxToken>) {
121        match self {
122            ModuleDebugDirective::File { directive, .. } => directive.unparse_tokens(tokens),
123            ModuleDebugDirective::Section { directive, .. } => directive.unparse_tokens(tokens),
124            ModuleDebugDirective::Dwarf { directive, .. } => directive.unparse_tokens(tokens),
125        }
126    }
127}
128
129impl PtxUnparser for ModuleDirective {
130    fn unparse_tokens(&self, tokens: &mut Vec<PtxToken>) {
131        match self {
132            ModuleDirective::ModuleVariable {
133                linkage, directive, ..
134            } => {
135                if let Some(link) = linkage {
136                    link.unparse_tokens(tokens);
137                }
138                directive.unparse_tokens(tokens)
139            }
140            ModuleDirective::EntryFunction {
141                linkage, directive, ..
142            } => {
143                if let Some(link) = linkage {
144                    link.unparse_tokens(tokens);
145                }
146                directive.unparse_tokens(tokens)
147            }
148            ModuleDirective::FuncFunction {
149                linkage, directive, ..
150            } => {
151                if let Some(link) = linkage {
152                    link.unparse_tokens(tokens);
153                }
154                directive.unparse_tokens(tokens)
155            }
156            ModuleDirective::AliasFunction { directive, .. } => directive.unparse_tokens(tokens),
157            ModuleDirective::ModuleInfo { directive, .. } => directive.unparse_tokens(tokens),
158            ModuleDirective::Debug { directive, .. } => directive.unparse_tokens(tokens),
159        }
160    }
161}
162
163impl PtxUnparser for Module {
164    fn unparse_tokens(&self, tokens: &mut Vec<PtxToken>) {
165        for directive in &self.directives {
166            directive.unparse_tokens(tokens);
167        }
168    }
169}