ptx_parser/parser/instruction/
ex2.rs

1//! Original PTX specification:
2//!
3//! ex2.approx{.ftz}.f32  d, a;
4//!
5//! ex2.approx.atype     d, a;
6//! ex2.approx.ftz.btype d, a;
7//! .atype = { .f16,  .f16x2};
8//! .btype = { .bf16, .bf16x2};
9
10#![allow(unused)]
11
12use crate::lexer::PtxToken;
13use crate::parser::{PtxParseError, PtxParser, PtxTokenStream, Span};
14use crate::r#type::common::*;
15
16pub mod section_0 {
17    use super::*;
18    use crate::r#type::instruction::ex2::section_0::*;
19
20    // ============================================================================
21    // Generated enum parsers
22    // ============================================================================
23
24    impl PtxParser for Atype {
25        fn parse(stream: &mut PtxTokenStream) -> Result<Self, PtxParseError> {
26            // Try F16x2
27            {
28                let saved_pos = stream.position();
29                if stream.expect_string(".f16x2").is_ok() {
30                    return Ok(Atype::F16x2);
31                }
32                stream.set_position(saved_pos);
33            }
34            let saved_pos = stream.position();
35            // Try F16
36            {
37                let saved_pos = stream.position();
38                if stream.expect_string(".f16").is_ok() {
39                    return Ok(Atype::F16);
40                }
41                stream.set_position(saved_pos);
42            }
43            stream.set_position(saved_pos);
44            let span = stream
45                .peek()
46                .map(|(_, s)| s.clone())
47                .unwrap_or(Span { start: 0, end: 0 });
48            let expected = &[".f16x2", ".f16"];
49            let found = stream
50                .peek()
51                .map(|(t, _)| format!("{:?}", t))
52                .unwrap_or_else(|_| "<end of input>".to_string());
53            Err(crate::parser::unexpected_value(span, expected, found))
54        }
55    }
56
57    impl PtxParser for Btype {
58        fn parse(stream: &mut PtxTokenStream) -> Result<Self, PtxParseError> {
59            // Try Bf16x2
60            {
61                let saved_pos = stream.position();
62                if stream.expect_string(".bf16x2").is_ok() {
63                    return Ok(Btype::Bf16x2);
64                }
65                stream.set_position(saved_pos);
66            }
67            let saved_pos = stream.position();
68            // Try Bf16
69            {
70                let saved_pos = stream.position();
71                if stream.expect_string(".bf16").is_ok() {
72                    return Ok(Btype::Bf16);
73                }
74                stream.set_position(saved_pos);
75            }
76            stream.set_position(saved_pos);
77            let span = stream
78                .peek()
79                .map(|(_, s)| s.clone())
80                .unwrap_or(Span { start: 0, end: 0 });
81            let expected = &[".bf16x2", ".bf16"];
82            let found = stream
83                .peek()
84                .map(|(t, _)| format!("{:?}", t))
85                .unwrap_or_else(|_| "<end of input>".to_string());
86            Err(crate::parser::unexpected_value(span, expected, found))
87        }
88    }
89
90    impl PtxParser for Ex2ApproxFtzF32 {
91        fn parse(stream: &mut PtxTokenStream) -> Result<Self, PtxParseError> {
92            stream.expect_string("ex2")?;
93            stream.expect_string(".approx")?;
94            let approx = ();
95            stream.expect_complete()?;
96            let saved_pos = stream.position();
97            let ftz = stream.expect_string(".ftz").is_ok();
98            if !ftz {
99                stream.set_position(saved_pos);
100            }
101            stream.expect_complete()?;
102            stream.expect_string(".f32")?;
103            let f32 = ();
104            stream.expect_complete()?;
105            let d = GeneralOperand::parse(stream)?;
106            stream.expect_complete()?;
107            stream.expect(&PtxToken::Comma)?;
108            let a = GeneralOperand::parse(stream)?;
109            stream.expect_complete()?;
110            stream.expect_complete()?;
111            stream.expect(&PtxToken::Semicolon)?;
112            Ok(Ex2ApproxFtzF32 {
113                approx,
114                ftz,
115                f32,
116                d,
117                a,
118            })
119        }
120    }
121
122    impl PtxParser for Ex2ApproxAtype {
123        fn parse(stream: &mut PtxTokenStream) -> Result<Self, PtxParseError> {
124            stream.expect_string("ex2")?;
125            stream.expect_string(".approx")?;
126            let approx = ();
127            stream.expect_complete()?;
128            let atype = Atype::parse(stream)?;
129            stream.expect_complete()?;
130            let d = GeneralOperand::parse(stream)?;
131            stream.expect_complete()?;
132            stream.expect(&PtxToken::Comma)?;
133            let a = GeneralOperand::parse(stream)?;
134            stream.expect_complete()?;
135            stream.expect_complete()?;
136            stream.expect(&PtxToken::Semicolon)?;
137            Ok(Ex2ApproxAtype {
138                approx,
139                atype,
140                d,
141                a,
142            })
143        }
144    }
145
146    impl PtxParser for Ex2ApproxFtzBtype {
147        fn parse(stream: &mut PtxTokenStream) -> Result<Self, PtxParseError> {
148            stream.expect_string("ex2")?;
149            stream.expect_string(".approx")?;
150            let approx = ();
151            stream.expect_complete()?;
152            stream.expect_string(".ftz")?;
153            let ftz = ();
154            stream.expect_complete()?;
155            let btype = Btype::parse(stream)?;
156            stream.expect_complete()?;
157            let d = GeneralOperand::parse(stream)?;
158            stream.expect_complete()?;
159            stream.expect(&PtxToken::Comma)?;
160            let a = GeneralOperand::parse(stream)?;
161            stream.expect_complete()?;
162            stream.expect_complete()?;
163            stream.expect(&PtxToken::Semicolon)?;
164            Ok(Ex2ApproxFtzBtype {
165                approx,
166                ftz,
167                btype,
168                d,
169                a,
170            })
171        }
172    }
173}