ptx_parser/parser/instruction/
vsh.rs

1//! Original PTX specification:
2//!
3//! // 32-bit scalar operation, with optional secondary operation
4//! vop.dtype.atype.u32{.sat}.mode       d, a{.asel}, b{.bsel};
5//! vop.dtype.atype.u32{.sat}.mode.op2   d, a{.asel}, b{.bsel}, c;
6//! // 32-bit scalar operation, with optional data merge
7//! vop.dtype.atype.u32{.sat}.mode  d.dsel, a{.asel}, b{.bsel}, c;
8//! vop   = { vshl, vshr };
9//! .dtype = .atype = { .u32, .s32 };
10//! .mode  = { .clamp, .wrap };
11//! .dsel  = .asel  = .bsel  = { .b0, .b1, .b2, .b3, .h0, .h1 };
12//! .op2   = { .add, .min, .max };
13
14#![allow(unused)]
15
16use crate::parser::{
17    PtxParseError, PtxParser, PtxTokenStream, Span,
18    util::{
19        between, comma_p, directive_p, exclamation_p, lbracket_p, lparen_p, map, minus_p, optional,
20        pipe_p, rbracket_p, rparen_p, semicolon_p, sep_by, string_p, try_map,
21    },
22};
23use crate::r#type::common::*;
24use crate::{alt, ok, seq_n};
25
26pub mod section_0 {
27    use super::*;
28    use crate::r#type::instruction::vsh::section_0::*;
29
30    // ============================================================================
31    // Generated enum parsers
32    // ============================================================================
33
34    impl PtxParser for Asel {
35        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
36            alt!(
37                map(string_p(".b0"), |_, _span| Asel::B0),
38                map(string_p(".b1"), |_, _span| Asel::B1),
39                map(string_p(".b2"), |_, _span| Asel::B2),
40                map(string_p(".b3"), |_, _span| Asel::B3),
41                map(string_p(".h0"), |_, _span| Asel::H0),
42                map(string_p(".h1"), |_, _span| Asel::H1)
43            )
44        }
45    }
46
47    impl PtxParser for Atype {
48        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
49            alt!(
50                map(string_p(".u32"), |_, _span| Atype::U32),
51                map(string_p(".s32"), |_, _span| Atype::S32)
52            )
53        }
54    }
55
56    impl PtxParser for Bsel {
57        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
58            alt!(
59                map(string_p(".b0"), |_, _span| Bsel::B0),
60                map(string_p(".b1"), |_, _span| Bsel::B1),
61                map(string_p(".b2"), |_, _span| Bsel::B2),
62                map(string_p(".b3"), |_, _span| Bsel::B3),
63                map(string_p(".h0"), |_, _span| Bsel::H0),
64                map(string_p(".h1"), |_, _span| Bsel::H1)
65            )
66        }
67    }
68
69    impl PtxParser for Dsel {
70        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
71            alt!(
72                map(string_p(".b0"), |_, _span| Dsel::B0),
73                map(string_p(".b1"), |_, _span| Dsel::B1),
74                map(string_p(".b2"), |_, _span| Dsel::B2),
75                map(string_p(".b3"), |_, _span| Dsel::B3),
76                map(string_p(".h0"), |_, _span| Dsel::H0),
77                map(string_p(".h1"), |_, _span| Dsel::H1)
78            )
79        }
80    }
81
82    impl PtxParser for Dtype {
83        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
84            alt!(
85                map(string_p(".u32"), |_, _span| Dtype::U32),
86                map(string_p(".s32"), |_, _span| Dtype::S32)
87            )
88        }
89    }
90
91    impl PtxParser for Mode {
92        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
93            alt!(
94                map(string_p(".clamp"), |_, _span| Mode::Clamp),
95                map(string_p(".wrap"), |_, _span| Mode::Wrap)
96            )
97        }
98    }
99
100    impl PtxParser for Op2 {
101        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
102            alt!(
103                map(string_p(".add"), |_, _span| Op2::Add),
104                map(string_p(".min"), |_, _span| Op2::Min),
105                map(string_p(".max"), |_, _span| Op2::Max)
106            )
107        }
108    }
109
110    impl PtxParser for VshlDtypeAtypeU32SatMode {
111        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
112            try_map(
113                seq_n!(
114                    string_p("vshl"),
115                    Dtype::parse(),
116                    Atype::parse(),
117                    string_p(".u32"),
118                    map(optional(string_p(".sat")), |value, _| value.is_some()),
119                    Mode::parse(),
120                    GeneralOperand::parse(),
121                    comma_p(),
122                    GeneralOperand::parse(),
123                    optional(Asel::parse()),
124                    comma_p(),
125                    GeneralOperand::parse(),
126                    optional(Bsel::parse()),
127                    semicolon_p()
128                ),
129                |(_, dtype, atype, u32, sat, mode, d, _, a, asel, _, b, bsel, _), span| {
130                    ok!(VshlDtypeAtypeU32SatMode {
131                        dtype = dtype,
132                        atype = atype,
133                        u32 = u32,
134                        sat = sat,
135                        mode = mode,
136                        d = d,
137                        a = a,
138                        asel = asel,
139                        b = b,
140                        bsel = bsel,
141
142                    })
143                },
144            )
145        }
146    }
147
148    impl PtxParser for VshrDtypeAtypeU32SatMode {
149        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
150            try_map(
151                seq_n!(
152                    string_p("vshr"),
153                    Dtype::parse(),
154                    Atype::parse(),
155                    string_p(".u32"),
156                    map(optional(string_p(".sat")), |value, _| value.is_some()),
157                    Mode::parse(),
158                    GeneralOperand::parse(),
159                    comma_p(),
160                    GeneralOperand::parse(),
161                    optional(Asel::parse()),
162                    comma_p(),
163                    GeneralOperand::parse(),
164                    optional(Bsel::parse()),
165                    semicolon_p()
166                ),
167                |(_, dtype, atype, u32, sat, mode, d, _, a, asel, _, b, bsel, _), span| {
168                    ok!(VshrDtypeAtypeU32SatMode {
169                        dtype = dtype,
170                        atype = atype,
171                        u32 = u32,
172                        sat = sat,
173                        mode = mode,
174                        d = d,
175                        a = a,
176                        asel = asel,
177                        b = b,
178                        bsel = bsel,
179
180                    })
181                },
182            )
183        }
184    }
185
186    impl PtxParser for VshlDtypeAtypeU32SatModeOp2 {
187        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
188            try_map(
189                seq_n!(
190                    string_p("vshl"),
191                    Dtype::parse(),
192                    Atype::parse(),
193                    string_p(".u32"),
194                    map(optional(string_p(".sat")), |value, _| value.is_some()),
195                    Mode::parse(),
196                    Op2::parse(),
197                    GeneralOperand::parse(),
198                    comma_p(),
199                    GeneralOperand::parse(),
200                    optional(Asel::parse()),
201                    comma_p(),
202                    GeneralOperand::parse(),
203                    optional(Bsel::parse()),
204                    comma_p(),
205                    GeneralOperand::parse(),
206                    semicolon_p()
207                ),
208                |(_, dtype, atype, u32, sat, mode, op2, d, _, a, asel, _, b, bsel, _, c, _),
209                 span| {
210                    ok!(VshlDtypeAtypeU32SatModeOp2 {
211                        dtype = dtype,
212                        atype = atype,
213                        u32 = u32,
214                        sat = sat,
215                        mode = mode,
216                        op2 = op2,
217                        d = d,
218                        a = a,
219                        asel = asel,
220                        b = b,
221                        bsel = bsel,
222                        c = c,
223
224                    })
225                },
226            )
227        }
228    }
229
230    impl PtxParser for VshrDtypeAtypeU32SatModeOp2 {
231        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
232            try_map(
233                seq_n!(
234                    string_p("vshr"),
235                    Dtype::parse(),
236                    Atype::parse(),
237                    string_p(".u32"),
238                    map(optional(string_p(".sat")), |value, _| value.is_some()),
239                    Mode::parse(),
240                    Op2::parse(),
241                    GeneralOperand::parse(),
242                    comma_p(),
243                    GeneralOperand::parse(),
244                    optional(Asel::parse()),
245                    comma_p(),
246                    GeneralOperand::parse(),
247                    optional(Bsel::parse()),
248                    comma_p(),
249                    GeneralOperand::parse(),
250                    semicolon_p()
251                ),
252                |(_, dtype, atype, u32, sat, mode, op2, d, _, a, asel, _, b, bsel, _, c, _),
253                 span| {
254                    ok!(VshrDtypeAtypeU32SatModeOp2 {
255                        dtype = dtype,
256                        atype = atype,
257                        u32 = u32,
258                        sat = sat,
259                        mode = mode,
260                        op2 = op2,
261                        d = d,
262                        a = a,
263                        asel = asel,
264                        b = b,
265                        bsel = bsel,
266                        c = c,
267
268                    })
269                },
270            )
271        }
272    }
273
274    impl PtxParser for VshlDtypeAtypeU32SatMode1 {
275        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
276            try_map(
277                seq_n!(
278                    string_p("vshl"),
279                    Dtype::parse(),
280                    Atype::parse(),
281                    string_p(".u32"),
282                    map(optional(string_p(".sat")), |value, _| value.is_some()),
283                    Mode::parse(),
284                    GeneralOperand::parse(),
285                    Dsel::parse(),
286                    comma_p(),
287                    GeneralOperand::parse(),
288                    optional(Asel::parse()),
289                    comma_p(),
290                    GeneralOperand::parse(),
291                    optional(Bsel::parse()),
292                    comma_p(),
293                    GeneralOperand::parse(),
294                    semicolon_p()
295                ),
296                |(_, dtype, atype, u32, sat, mode, d, dsel, _, a, asel, _, b, bsel, _, c, _),
297                 span| {
298                    ok!(VshlDtypeAtypeU32SatMode1 {
299                        dtype = dtype,
300                        atype = atype,
301                        u32 = u32,
302                        sat = sat,
303                        mode = mode,
304                        d = d,
305                        dsel = dsel,
306                        a = a,
307                        asel = asel,
308                        b = b,
309                        bsel = bsel,
310                        c = c,
311
312                    })
313                },
314            )
315        }
316    }
317
318    impl PtxParser for VshrDtypeAtypeU32SatMode1 {
319        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
320            try_map(
321                seq_n!(
322                    string_p("vshr"),
323                    Dtype::parse(),
324                    Atype::parse(),
325                    string_p(".u32"),
326                    map(optional(string_p(".sat")), |value, _| value.is_some()),
327                    Mode::parse(),
328                    GeneralOperand::parse(),
329                    Dsel::parse(),
330                    comma_p(),
331                    GeneralOperand::parse(),
332                    optional(Asel::parse()),
333                    comma_p(),
334                    GeneralOperand::parse(),
335                    optional(Bsel::parse()),
336                    comma_p(),
337                    GeneralOperand::parse(),
338                    semicolon_p()
339                ),
340                |(_, dtype, atype, u32, sat, mode, d, dsel, _, a, asel, _, b, bsel, _, c, _),
341                 span| {
342                    ok!(VshrDtypeAtypeU32SatMode1 {
343                        dtype = dtype,
344                        atype = atype,
345                        u32 = u32,
346                        sat = sat,
347                        mode = mode,
348                        d = d,
349                        dsel = dsel,
350                        a = a,
351                        asel = asel,
352                        b = b,
353                        bsel = bsel,
354                        c = c,
355
356                    })
357                },
358            )
359        }
360    }
361}