Debian Codegraph @

src/nouveau/headers/struct_parser.py

raw
1 #! /usr/bin/env python3
2 #
3 # Copyright © 2024 Collabora Ltd. and Red Hat Inc.
4 # SPDX-License-Identifier: MIT
5
6 import argparse
7 import os.path
8 import re
9 import sys
10
11 from collections import namedtuple
12 from mako.template import Template
13
14 import util
15
16
17 TEMPLATE_RS = Template("""\
18 // Copyright © 2024 Collabora Ltd. and Red Hat Inc.
19 // SPDX-License-Identifier: MIT
20
21 // This file is generated by struct_parser.py. DO NOT EDIT!
22
23 #![allow(non_snake_case)]
24
25 use std::ops::Range;
26
27 % for s in structs:
28 pub const ${s.name}_MAX_BIT: usize = ${s.max_bit};
29 % for f in s.fields:
30 % if f.stride:
31 #[inline]
32 pub const fn ${s.name}_${f.name}(i: usize) -> Range<usize> {
33 % if f.stride == 1:
34 (i + ${f.lo})..(i + ${f.hi + 1})
35 % else:
36 (i * ${f.stride} + ${f.lo})..(i * ${f.stride} + ${f.hi + 1})
37 % endif
38 }
39 % else:
40 pub const ${s.name}_${f.name}: Range<usize> = ${f.lo}..${f.hi + 1};
41 % endif:
42 % for e in f.enums:
43 pub const ${s.name}_${f.name}_${e.name}: u32 = ${e.value};
44 % endfor
45 % endfor
46 % endfor
47 """)
48
49 STRUCTS = [
50 'SPHV3',
51 'SPHV4',
52 'TEXHEADV2',
53 'TEXHEADV3',
54 'TEXHEAD_BL',
55 'TEXHEAD_1D',
56 'TEXHEAD_PITCH',
57 # This one goes last because it's a substring of the others
58 'TEXHEAD',
59 'TEXSAMP',
60 'QMDV00_06',
61 'QMDV01_06',
62 'QMDV01_07',
63 'QMDV02_01',
64 'QMDV02_02',
65 'QMDV02_03',
66 'QMDV02_04',
67 'QMDV03_00',
68 'QMDV04_00',
69 'QMDV05_00',
70 ]
71
72 Enum = namedtuple('Enum', ['name', 'value'])
73
74 class Field(object):
75 def __init__(self, name, lo, hi, stride=0):
76 self.name = name
77 self.lo = lo
78 self.hi = hi
79 self.stride = stride
80 self.enums = []
81
82 def add_enum(self, name, value):
83 self.enums.append(Enum(name, value))
84
85 class Struct(object):
86 def __init__(self, name):
87 self.name = name
88 self.fields = []
89 self.max_bit = 0
90
91 def add_field(self, name, lo, hi, stride=0):
92 self.fields.append(Field(name, lo, hi, stride))
93 self.max_bit = max(self.max_bit, hi)
94
95 DRF_RE = re.compile(r'(?P<hi>[0-9]+):(?P<lo>[0-9]+)')
96 FIELD_NAME_RE = re.compile(r'_?(?P<dw>[0-9]+)?_?(?P<name>.*)')
97 MW_RE = re.compile(r'MW\((?P<hi>[0-9]+):(?P<lo>[0-9]+)\)')
98 MW_ARR_RE = re.compile(r'MW\(\((?P<hi>\d+)\+\(i\)\*(?P<stride>\d+)\):\((?P<lo>[0-9]+)\+\(i\)\*(?P=stride)\)\)')
99
100 def parse_header(nvcl, file):
101 structs = {}
102 for line in file:
103 line = line.strip().split()
104 if not line:
105 continue
106
107 if line[0] != '#define':
108 continue
109
110 if not line[1].startswith(nvcl):
111 continue
112
113 name = line[1][(len(nvcl)+1):]
114
115 struct = None
116 for s in STRUCTS:
117 if name.startswith(s):
118 if s not in structs:
119 structs[s] = Struct(s)
120 struct = structs[s]
121 name = name[len(s):]
122 break
123
124 if struct is None:
125 continue
126
127 name_m = FIELD_NAME_RE.match(name)
128 name = name_m.group('name')
129
130 drf = DRF_RE.match(line[2])
131 mw = MW_RE.match(line[2])
132 mw_arr = MW_ARR_RE.match(line[2])
133 if drf:
134 dw = int(name_m.group('dw'))
135 lo = int(drf.group('lo')) + dw * 32
136 hi = int(drf.group('hi')) + dw * 32
137 struct.add_field(name, lo, hi)
138 elif mw:
139 lo = int(mw.group('lo'))
140 hi = int(mw.group('hi'))
141 struct.add_field(name, lo, hi)
142 elif mw_arr:
143 lo = int(mw_arr.group('lo'))
144 hi = int(mw_arr.group('hi'))
145 stride = int(mw_arr.group('stride'))
146 assert name.endswith('(i)')
147 struct.add_field(name.removesuffix('(i)'), lo, hi, stride)
148 else:
149 for f in struct.fields:
150 if name.startswith(f.name + '_'):
151 name = name[(len(f.name)+1):]
152 f.add_enum(name, line[2])
153
154 return list(structs.values())
155
156 NVCL_RE = re.compile(r'cl(?P<clsver>[0-9a-f]{4}).*')
157
158 def main():
159 parser = argparse.ArgumentParser()
160 parser.add_argument('--out-rs', required=True, help='Output Rust file.')
161 parser.add_argument('--in-h',
162 help='Input class header file.',
163 required=True)
164 args = parser.parse_args()
165
166 clheader = os.path.basename(args.in_h)
167 nvcl = NVCL_RE.match(clheader).group('clsver')
168 nvcl = nvcl.upper()
169 nvcl = "NV" + nvcl
170
171 with open(args.in_h, 'r', encoding='utf-8') as f:
172 structs = parse_header(nvcl, f)
173
174 util.write_template_rs(args.out_rs, TEMPLATE_RS, dict(structs=structs))
175
176
177 if __name__ == '__main__':
178 main()