ptx_parser/parser/instruction/
vop.rs

1//! Original PTX specification:
2//!
3//! // 32-bit scalar operation, with optional secondary operation
4//! vop.dtype.atype.btype{.sat}       d, a{.asel}, b{.bsel};
5//! vop.dtype.atype.btype{.sat}.op2   d, a{.asel}, b{.bsel}, c;
6//! // 32-bit scalar operation, with optional data merge
7//! vop.dtype.atype.btype{.sat}  d.dsel, a{.asel}, b{.bsel}, c;
8//! vop   = { vadd, vsub, vabsdiff, vmin, vmax };
9//! .dtype = .atype = .btype = { .u32, .s32 };
10//! .dsel  = .asel  = .bsel  = { .b0, .b1, .b2, .b3, .h0, .h1 };
11//! .op2   = { .add, .min, .max };
12
13#![allow(unused)]
14
15use crate::parser::{
16    PtxParseError, PtxParser, PtxTokenStream, Span,
17    util::{
18        between, comma_p, directive_p, exclamation_p, lbracket_p, lparen_p, map, minus_p, optional,
19        pipe_p, rbracket_p, rparen_p, semicolon_p, sep_by, string_p, try_map,
20    },
21};
22use crate::r#type::common::*;
23use crate::{alt, ok, seq_n};
24
25pub mod section_0 {
26    use super::*;
27    use crate::r#type::instruction::vop::section_0::*;
28
29    // ============================================================================
30    // Generated enum parsers
31    // ============================================================================
32
33    impl PtxParser for Asel {
34        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
35            alt!(
36                map(string_p(".b0"), |_, _span| Asel::B0),
37                map(string_p(".b1"), |_, _span| Asel::B1),
38                map(string_p(".b2"), |_, _span| Asel::B2),
39                map(string_p(".b3"), |_, _span| Asel::B3),
40                map(string_p(".h0"), |_, _span| Asel::H0),
41                map(string_p(".h1"), |_, _span| Asel::H1)
42            )
43        }
44    }
45
46    impl PtxParser for Atype {
47        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
48            alt!(
49                map(string_p(".u32"), |_, _span| Atype::U32),
50                map(string_p(".s32"), |_, _span| Atype::S32)
51            )
52        }
53    }
54
55    impl PtxParser for Bsel {
56        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
57            alt!(
58                map(string_p(".b0"), |_, _span| Bsel::B0),
59                map(string_p(".b1"), |_, _span| Bsel::B1),
60                map(string_p(".b2"), |_, _span| Bsel::B2),
61                map(string_p(".b3"), |_, _span| Bsel::B3),
62                map(string_p(".h0"), |_, _span| Bsel::H0),
63                map(string_p(".h1"), |_, _span| Bsel::H1)
64            )
65        }
66    }
67
68    impl PtxParser for Btype {
69        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
70            alt!(
71                map(string_p(".u32"), |_, _span| Btype::U32),
72                map(string_p(".s32"), |_, _span| Btype::S32)
73            )
74        }
75    }
76
77    impl PtxParser for Dsel {
78        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
79            alt!(
80                map(string_p(".b0"), |_, _span| Dsel::B0),
81                map(string_p(".b1"), |_, _span| Dsel::B1),
82                map(string_p(".b2"), |_, _span| Dsel::B2),
83                map(string_p(".b3"), |_, _span| Dsel::B3),
84                map(string_p(".h0"), |_, _span| Dsel::H0),
85                map(string_p(".h1"), |_, _span| Dsel::H1)
86            )
87        }
88    }
89
90    impl PtxParser for Dtype {
91        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
92            alt!(
93                map(string_p(".u32"), |_, _span| Dtype::U32),
94                map(string_p(".s32"), |_, _span| Dtype::S32)
95            )
96        }
97    }
98
99    impl PtxParser for Op2 {
100        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
101            alt!(
102                map(string_p(".add"), |_, _span| Op2::Add),
103                map(string_p(".min"), |_, _span| Op2::Min),
104                map(string_p(".max"), |_, _span| Op2::Max)
105            )
106        }
107    }
108
109    impl PtxParser for VaddDtypeAtypeBtypeSat {
110        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
111            try_map(
112                seq_n!(
113                    string_p("vadd"),
114                    Dtype::parse(),
115                    Atype::parse(),
116                    Btype::parse(),
117                    map(optional(string_p(".sat")), |value, _| value.is_some()),
118                    GeneralOperand::parse(),
119                    comma_p(),
120                    GeneralOperand::parse(),
121                    optional(Asel::parse()),
122                    comma_p(),
123                    GeneralOperand::parse(),
124                    optional(Bsel::parse()),
125                    semicolon_p()
126                ),
127                |(_, dtype, atype, btype, sat, d, _, a, asel, _, b, bsel, _), span| {
128                    ok!(VaddDtypeAtypeBtypeSat {
129                        dtype = dtype,
130                        atype = atype,
131                        btype = btype,
132                        sat = sat,
133                        d = d,
134                        a = a,
135                        asel = asel,
136                        b = b,
137                        bsel = bsel,
138
139                    })
140                },
141            )
142        }
143    }
144
145    impl PtxParser for VsubDtypeAtypeBtypeSat {
146        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
147            try_map(
148                seq_n!(
149                    string_p("vsub"),
150                    Dtype::parse(),
151                    Atype::parse(),
152                    Btype::parse(),
153                    map(optional(string_p(".sat")), |value, _| value.is_some()),
154                    GeneralOperand::parse(),
155                    comma_p(),
156                    GeneralOperand::parse(),
157                    optional(Asel::parse()),
158                    comma_p(),
159                    GeneralOperand::parse(),
160                    optional(Bsel::parse()),
161                    semicolon_p()
162                ),
163                |(_, dtype, atype, btype, sat, d, _, a, asel, _, b, bsel, _), span| {
164                    ok!(VsubDtypeAtypeBtypeSat {
165                        dtype = dtype,
166                        atype = atype,
167                        btype = btype,
168                        sat = sat,
169                        d = d,
170                        a = a,
171                        asel = asel,
172                        b = b,
173                        bsel = bsel,
174
175                    })
176                },
177            )
178        }
179    }
180
181    impl PtxParser for VabsdiffDtypeAtypeBtypeSat {
182        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
183            try_map(
184                seq_n!(
185                    string_p("vabsdiff"),
186                    Dtype::parse(),
187                    Atype::parse(),
188                    Btype::parse(),
189                    map(optional(string_p(".sat")), |value, _| value.is_some()),
190                    GeneralOperand::parse(),
191                    comma_p(),
192                    GeneralOperand::parse(),
193                    optional(Asel::parse()),
194                    comma_p(),
195                    GeneralOperand::parse(),
196                    optional(Bsel::parse()),
197                    semicolon_p()
198                ),
199                |(_, dtype, atype, btype, sat, d, _, a, asel, _, b, bsel, _), span| {
200                    ok!(VabsdiffDtypeAtypeBtypeSat {
201                        dtype = dtype,
202                        atype = atype,
203                        btype = btype,
204                        sat = sat,
205                        d = d,
206                        a = a,
207                        asel = asel,
208                        b = b,
209                        bsel = bsel,
210
211                    })
212                },
213            )
214        }
215    }
216
217    impl PtxParser for VminDtypeAtypeBtypeSat {
218        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
219            try_map(
220                seq_n!(
221                    string_p("vmin"),
222                    Dtype::parse(),
223                    Atype::parse(),
224                    Btype::parse(),
225                    map(optional(string_p(".sat")), |value, _| value.is_some()),
226                    GeneralOperand::parse(),
227                    comma_p(),
228                    GeneralOperand::parse(),
229                    optional(Asel::parse()),
230                    comma_p(),
231                    GeneralOperand::parse(),
232                    optional(Bsel::parse()),
233                    semicolon_p()
234                ),
235                |(_, dtype, atype, btype, sat, d, _, a, asel, _, b, bsel, _), span| {
236                    ok!(VminDtypeAtypeBtypeSat {
237                        dtype = dtype,
238                        atype = atype,
239                        btype = btype,
240                        sat = sat,
241                        d = d,
242                        a = a,
243                        asel = asel,
244                        b = b,
245                        bsel = bsel,
246
247                    })
248                },
249            )
250        }
251    }
252
253    impl PtxParser for VmaxDtypeAtypeBtypeSat {
254        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
255            try_map(
256                seq_n!(
257                    string_p("vmax"),
258                    Dtype::parse(),
259                    Atype::parse(),
260                    Btype::parse(),
261                    map(optional(string_p(".sat")), |value, _| value.is_some()),
262                    GeneralOperand::parse(),
263                    comma_p(),
264                    GeneralOperand::parse(),
265                    optional(Asel::parse()),
266                    comma_p(),
267                    GeneralOperand::parse(),
268                    optional(Bsel::parse()),
269                    semicolon_p()
270                ),
271                |(_, dtype, atype, btype, sat, d, _, a, asel, _, b, bsel, _), span| {
272                    ok!(VmaxDtypeAtypeBtypeSat {
273                        dtype = dtype,
274                        atype = atype,
275                        btype = btype,
276                        sat = sat,
277                        d = d,
278                        a = a,
279                        asel = asel,
280                        b = b,
281                        bsel = bsel,
282
283                    })
284                },
285            )
286        }
287    }
288
289    impl PtxParser for VaddDtypeAtypeBtypeSatOp2 {
290        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
291            try_map(
292                seq_n!(
293                    string_p("vadd"),
294                    Dtype::parse(),
295                    Atype::parse(),
296                    Btype::parse(),
297                    map(optional(string_p(".sat")), |value, _| value.is_some()),
298                    Op2::parse(),
299                    GeneralOperand::parse(),
300                    comma_p(),
301                    GeneralOperand::parse(),
302                    optional(Asel::parse()),
303                    comma_p(),
304                    GeneralOperand::parse(),
305                    optional(Bsel::parse()),
306                    comma_p(),
307                    GeneralOperand::parse(),
308                    semicolon_p()
309                ),
310                |(_, dtype, atype, btype, sat, op2, d, _, a, asel, _, b, bsel, _, c, _), span| {
311                    ok!(VaddDtypeAtypeBtypeSatOp2 {
312                        dtype = dtype,
313                        atype = atype,
314                        btype = btype,
315                        sat = sat,
316                        op2 = op2,
317                        d = d,
318                        a = a,
319                        asel = asel,
320                        b = b,
321                        bsel = bsel,
322                        c = c,
323
324                    })
325                },
326            )
327        }
328    }
329
330    impl PtxParser for VsubDtypeAtypeBtypeSatOp2 {
331        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
332            try_map(
333                seq_n!(
334                    string_p("vsub"),
335                    Dtype::parse(),
336                    Atype::parse(),
337                    Btype::parse(),
338                    map(optional(string_p(".sat")), |value, _| value.is_some()),
339                    Op2::parse(),
340                    GeneralOperand::parse(),
341                    comma_p(),
342                    GeneralOperand::parse(),
343                    optional(Asel::parse()),
344                    comma_p(),
345                    GeneralOperand::parse(),
346                    optional(Bsel::parse()),
347                    comma_p(),
348                    GeneralOperand::parse(),
349                    semicolon_p()
350                ),
351                |(_, dtype, atype, btype, sat, op2, d, _, a, asel, _, b, bsel, _, c, _), span| {
352                    ok!(VsubDtypeAtypeBtypeSatOp2 {
353                        dtype = dtype,
354                        atype = atype,
355                        btype = btype,
356                        sat = sat,
357                        op2 = op2,
358                        d = d,
359                        a = a,
360                        asel = asel,
361                        b = b,
362                        bsel = bsel,
363                        c = c,
364
365                    })
366                },
367            )
368        }
369    }
370
371    impl PtxParser for VabsdiffDtypeAtypeBtypeSatOp2 {
372        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
373            try_map(
374                seq_n!(
375                    string_p("vabsdiff"),
376                    Dtype::parse(),
377                    Atype::parse(),
378                    Btype::parse(),
379                    map(optional(string_p(".sat")), |value, _| value.is_some()),
380                    Op2::parse(),
381                    GeneralOperand::parse(),
382                    comma_p(),
383                    GeneralOperand::parse(),
384                    optional(Asel::parse()),
385                    comma_p(),
386                    GeneralOperand::parse(),
387                    optional(Bsel::parse()),
388                    comma_p(),
389                    GeneralOperand::parse(),
390                    semicolon_p()
391                ),
392                |(_, dtype, atype, btype, sat, op2, d, _, a, asel, _, b, bsel, _, c, _), span| {
393                    ok!(VabsdiffDtypeAtypeBtypeSatOp2 {
394                        dtype = dtype,
395                        atype = atype,
396                        btype = btype,
397                        sat = sat,
398                        op2 = op2,
399                        d = d,
400                        a = a,
401                        asel = asel,
402                        b = b,
403                        bsel = bsel,
404                        c = c,
405
406                    })
407                },
408            )
409        }
410    }
411
412    impl PtxParser for VminDtypeAtypeBtypeSatOp2 {
413        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
414            try_map(
415                seq_n!(
416                    string_p("vmin"),
417                    Dtype::parse(),
418                    Atype::parse(),
419                    Btype::parse(),
420                    map(optional(string_p(".sat")), |value, _| value.is_some()),
421                    Op2::parse(),
422                    GeneralOperand::parse(),
423                    comma_p(),
424                    GeneralOperand::parse(),
425                    optional(Asel::parse()),
426                    comma_p(),
427                    GeneralOperand::parse(),
428                    optional(Bsel::parse()),
429                    comma_p(),
430                    GeneralOperand::parse(),
431                    semicolon_p()
432                ),
433                |(_, dtype, atype, btype, sat, op2, d, _, a, asel, _, b, bsel, _, c, _), span| {
434                    ok!(VminDtypeAtypeBtypeSatOp2 {
435                        dtype = dtype,
436                        atype = atype,
437                        btype = btype,
438                        sat = sat,
439                        op2 = op2,
440                        d = d,
441                        a = a,
442                        asel = asel,
443                        b = b,
444                        bsel = bsel,
445                        c = c,
446
447                    })
448                },
449            )
450        }
451    }
452
453    impl PtxParser for VmaxDtypeAtypeBtypeSatOp2 {
454        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
455            try_map(
456                seq_n!(
457                    string_p("vmax"),
458                    Dtype::parse(),
459                    Atype::parse(),
460                    Btype::parse(),
461                    map(optional(string_p(".sat")), |value, _| value.is_some()),
462                    Op2::parse(),
463                    GeneralOperand::parse(),
464                    comma_p(),
465                    GeneralOperand::parse(),
466                    optional(Asel::parse()),
467                    comma_p(),
468                    GeneralOperand::parse(),
469                    optional(Bsel::parse()),
470                    comma_p(),
471                    GeneralOperand::parse(),
472                    semicolon_p()
473                ),
474                |(_, dtype, atype, btype, sat, op2, d, _, a, asel, _, b, bsel, _, c, _), span| {
475                    ok!(VmaxDtypeAtypeBtypeSatOp2 {
476                        dtype = dtype,
477                        atype = atype,
478                        btype = btype,
479                        sat = sat,
480                        op2 = op2,
481                        d = d,
482                        a = a,
483                        asel = asel,
484                        b = b,
485                        bsel = bsel,
486                        c = c,
487
488                    })
489                },
490            )
491        }
492    }
493
494    impl PtxParser for VaddDtypeAtypeBtypeSat1 {
495        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
496            try_map(
497                seq_n!(
498                    string_p("vadd"),
499                    Dtype::parse(),
500                    Atype::parse(),
501                    Btype::parse(),
502                    map(optional(string_p(".sat")), |value, _| value.is_some()),
503                    GeneralOperand::parse(),
504                    Dsel::parse(),
505                    comma_p(),
506                    GeneralOperand::parse(),
507                    optional(Asel::parse()),
508                    comma_p(),
509                    GeneralOperand::parse(),
510                    optional(Bsel::parse()),
511                    comma_p(),
512                    GeneralOperand::parse(),
513                    semicolon_p()
514                ),
515                |(_, dtype, atype, btype, sat, d, dsel, _, a, asel, _, b, bsel, _, c, _), span| {
516                    ok!(VaddDtypeAtypeBtypeSat1 {
517                        dtype = dtype,
518                        atype = atype,
519                        btype = btype,
520                        sat = sat,
521                        d = d,
522                        dsel = dsel,
523                        a = a,
524                        asel = asel,
525                        b = b,
526                        bsel = bsel,
527                        c = c,
528
529                    })
530                },
531            )
532        }
533    }
534
535    impl PtxParser for VsubDtypeAtypeBtypeSat1 {
536        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
537            try_map(
538                seq_n!(
539                    string_p("vsub"),
540                    Dtype::parse(),
541                    Atype::parse(),
542                    Btype::parse(),
543                    map(optional(string_p(".sat")), |value, _| value.is_some()),
544                    GeneralOperand::parse(),
545                    Dsel::parse(),
546                    comma_p(),
547                    GeneralOperand::parse(),
548                    optional(Asel::parse()),
549                    comma_p(),
550                    GeneralOperand::parse(),
551                    optional(Bsel::parse()),
552                    comma_p(),
553                    GeneralOperand::parse(),
554                    semicolon_p()
555                ),
556                |(_, dtype, atype, btype, sat, d, dsel, _, a, asel, _, b, bsel, _, c, _), span| {
557                    ok!(VsubDtypeAtypeBtypeSat1 {
558                        dtype = dtype,
559                        atype = atype,
560                        btype = btype,
561                        sat = sat,
562                        d = d,
563                        dsel = dsel,
564                        a = a,
565                        asel = asel,
566                        b = b,
567                        bsel = bsel,
568                        c = c,
569
570                    })
571                },
572            )
573        }
574    }
575
576    impl PtxParser for VabsdiffDtypeAtypeBtypeSat1 {
577        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
578            try_map(
579                seq_n!(
580                    string_p("vabsdiff"),
581                    Dtype::parse(),
582                    Atype::parse(),
583                    Btype::parse(),
584                    map(optional(string_p(".sat")), |value, _| value.is_some()),
585                    GeneralOperand::parse(),
586                    Dsel::parse(),
587                    comma_p(),
588                    GeneralOperand::parse(),
589                    optional(Asel::parse()),
590                    comma_p(),
591                    GeneralOperand::parse(),
592                    optional(Bsel::parse()),
593                    comma_p(),
594                    GeneralOperand::parse(),
595                    semicolon_p()
596                ),
597                |(_, dtype, atype, btype, sat, d, dsel, _, a, asel, _, b, bsel, _, c, _), span| {
598                    ok!(VabsdiffDtypeAtypeBtypeSat1 {
599                        dtype = dtype,
600                        atype = atype,
601                        btype = btype,
602                        sat = sat,
603                        d = d,
604                        dsel = dsel,
605                        a = a,
606                        asel = asel,
607                        b = b,
608                        bsel = bsel,
609                        c = c,
610
611                    })
612                },
613            )
614        }
615    }
616
617    impl PtxParser for VminDtypeAtypeBtypeSat1 {
618        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
619            try_map(
620                seq_n!(
621                    string_p("vmin"),
622                    Dtype::parse(),
623                    Atype::parse(),
624                    Btype::parse(),
625                    map(optional(string_p(".sat")), |value, _| value.is_some()),
626                    GeneralOperand::parse(),
627                    Dsel::parse(),
628                    comma_p(),
629                    GeneralOperand::parse(),
630                    optional(Asel::parse()),
631                    comma_p(),
632                    GeneralOperand::parse(),
633                    optional(Bsel::parse()),
634                    comma_p(),
635                    GeneralOperand::parse(),
636                    semicolon_p()
637                ),
638                |(_, dtype, atype, btype, sat, d, dsel, _, a, asel, _, b, bsel, _, c, _), span| {
639                    ok!(VminDtypeAtypeBtypeSat1 {
640                        dtype = dtype,
641                        atype = atype,
642                        btype = btype,
643                        sat = sat,
644                        d = d,
645                        dsel = dsel,
646                        a = a,
647                        asel = asel,
648                        b = b,
649                        bsel = bsel,
650                        c = c,
651
652                    })
653                },
654            )
655        }
656    }
657
658    impl PtxParser for VmaxDtypeAtypeBtypeSat1 {
659        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
660            try_map(
661                seq_n!(
662                    string_p("vmax"),
663                    Dtype::parse(),
664                    Atype::parse(),
665                    Btype::parse(),
666                    map(optional(string_p(".sat")), |value, _| value.is_some()),
667                    GeneralOperand::parse(),
668                    Dsel::parse(),
669                    comma_p(),
670                    GeneralOperand::parse(),
671                    optional(Asel::parse()),
672                    comma_p(),
673                    GeneralOperand::parse(),
674                    optional(Bsel::parse()),
675                    comma_p(),
676                    GeneralOperand::parse(),
677                    semicolon_p()
678                ),
679                |(_, dtype, atype, btype, sat, d, dsel, _, a, asel, _, b, bsel, _, c, _), span| {
680                    ok!(VmaxDtypeAtypeBtypeSat1 {
681                        dtype = dtype,
682                        atype = atype,
683                        btype = btype,
684                        sat = sat,
685                        d = d,
686                        dsel = dsel,
687                        a = a,
688                        asel = asel,
689                        b = b,
690                        bsel = bsel,
691                        c = c,
692
693                    })
694                },
695            )
696        }
697    }
698}