1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use std::sync::OnceLock;

use regex::bytes::Regex;

#[expect(clippy::module_name_repetitions)]
pub fn const_marker_regex() -> &'static Regex {
    static CONST_MARKER_REGEX: OnceLock<Regex> = OnceLock::new();
    #[allow(clippy::unwrap_used)]
    CONST_MARKER_REGEX.get_or_init(|| {
        Regex::new(r"(?-u)// <rust-cuda-ptx-jit-const-load-(?P<tmpreg>%r\d+)-(?P<param>\d+)> //")
            .unwrap()
    })
}

#[expect(clippy::module_name_repetitions)]
pub fn const_base_register_regex() -> &'static Regex {
    static CONST_BASE_REGISTER_REGEX: OnceLock<Regex> = OnceLock::new();
    #[allow(clippy::unwrap_used)]
    CONST_BASE_REGISTER_REGEX.get_or_init(|| {
        Regex::new(r"(?-u)ld\.global\.u32\s*(?P<tmpreg>%r\d+)\s*,\s*\[(?P<basereg>%r[ds]?\d+)]\s*;")
            .unwrap()
    })
}

#[expect(clippy::module_name_repetitions)]
pub fn const_load_instruction_regex() -> &'static Regex {
    static CONST_LOAD_INSTRUCTION_REGEX: OnceLock<Regex> = OnceLock::new();
    #[allow(clippy::unwrap_used)]
    CONST_LOAD_INSTRUCTION_REGEX.get_or_init(|| {
        Regex::new(
            r"(?x-u)(?P<instruction>
                ld\.global
                (?:\.(?P<vector>v[24]))?
                \.
                (?P<loadtype>[suf])
                (?P<loadwidth>8|16|32|64)
                \s*
                (?P<constreg>
                    (?:%[rf][sd]?\d+) |
                    (?:\{(?:\s*%[rf][sd]?\d+,)*\s*%[rf][sd]?\d+\s*\})
                )
                ,\s*
                \[
                (?P<basereg>%r[ds]?\d+)
                (?:
                    \+
                    (?P<loadoffset>\d+)
                )?
                \]
                \s*;
            )",
        )
        .unwrap()
    })
}

#[expect(clippy::module_name_repetitions)]
pub fn register_regex() -> &'static Regex {
    static REGISTER_REGEX: OnceLock<Regex> = OnceLock::new();
    #[allow(clippy::unwrap_used)]
    REGISTER_REGEX.get_or_init(|| Regex::new(r"(?-u)(?P<register>%[rf][sd]?\d+)").unwrap())
}