Debian Codegraph @

src/nouveau/headers/class_parser.py

raw
1 #! /usr/bin/env python3
2
3 # script to parse nvidia CL headers and generate inlines to be used in pushbuffer encoding.
4 # probably needs python3.9
5
6 import argparse
7 import os.path
8 import sys
9 import re
10 import subprocess
11
12 from mako.template import Template
13
14 import util
15
16
17 METHOD_ARRAY_SIZES = {
18 'BIND_GROUP_CONSTANT_BUFFER' : 16,
19 'CALL_MME_DATA' : 256,
20 'CALL_MME_MACRO' : 256,
21 'LOAD_CONSTANT_BUFFER' : 16,
22 'LOAD_INLINE_QMD_DATA' : 64,
23 'SET_ANTI_ALIAS_SAMPLE_POSITIONS' : 4,
24 'SET_BLEND' : 8,
25 'SET_BLEND_PER_TARGET_*' : 8,
26 'SET_COLOR_TARGET_*' : 8,
27 'SET_COLOR_COMPRESSION' : 8,
28 'SET_COLOR_CLEAR_VALUE' : 4,
29 'SET_CT_WRITE' : 8,
30 # For compute, this is only 8:
31 'SET_MME_SHADOW_SCRATCH' : 256,
32 'SET_MULTI_VIEW_RENDER_TARGET_ARRAY_INDEX_OFFSET' : 4,
33 'SET_PIPELINE_*' : 6,
34 'SET_ROOT_TABLE_VISIBILITY' : 8,
35 'SET_SCG_COMPUTE_SCHEDULING_PARAMETERS' : 16,
36 'SET_SCG_GRAPHICS_SCHEDULING_PARAMETERS' : 16,
37 'SET_SCISSOR_*' : 16,
38 'SET_SHADER_PERFORMANCE_SNAPSHOT_COUNTER_VALUE*' : 8,
39 'SET_SHADER_PERFORMANCE_COUNTER_VALUE*' : 8,
40 'SET_SHADER_PERFORMANCE_COUNTER_EVENT' : 8,
41 'SET_SHADER_PERFORMANCE_COUNTER_CONTROL_A' : 8,
42 'SET_SHADER_PERFORMANCE_COUNTER_CONTROL_B' : 8,
43 'SET_SHADING_RATE_INDEX_SURFACE_*' : 1,
44 'SET_SPARE_MULTI_VIEW_RENDER_TARGET_ARRAY_INDEX_OFFSET' : 4,
45 'SET_STREAM_OUT_BUFFER_*' : 4,
46 'SET_STREAM_OUT_CONTROL_*' : 4,
47 'SET_VARIABLE_PIXEL_RATE_SAMPLE_ORDER' : 13,
48 'SET_VARIABLE_PIXEL_RATE_SHADING_CONTROL*' : 16,
49 'SET_VARIABLE_PIXEL_RATE_SHADING_INDEX_TO_RATE*' : 16,
50 'SET_VIEWPORT_*' : 16,
51 'SET_VERTEX_ATTRIBUTE_*' : 16,
52 'SET_VERTEX_STREAM_*' : 16,
53 'SET_WINDOW_CLIP_*' : 8,
54 'SET_CLIP_ID_EXTENT_*' : 4,
55 }
56
57 METHOD_IS_FLOAT = [
58 'SET_BLEND_CONST_*',
59 'SET_DEPTH_BIAS',
60 'SET_SLOPE_SCALE_DEPTH_BIAS',
61 'SET_DEPTH_BIAS_CLAMP',
62 'SET_DEPTH_BOUNDS_M*',
63 'SET_LINE_WIDTH_FLOAT',
64 'SET_ALIASED_LINE_WIDTH_FLOAT',
65 'SET_VIEWPORT_SCALE_*',
66 'SET_VIEWPORT_OFFSET_*',
67 'SET_VIEWPORT_CLIP_MIN_Z',
68 'SET_VIEWPORT_CLIP_MAX_Z',
69 'SET_Z_CLEAR_VALUE',
70 'SET_POINT_SIZE',
71 ]
72
73 # generator chokes on these in later files
74 SKIP_FIELD = [
75 'CURRENT_SCG_TYPE'
76 ]
77
78 TEMPLATE_H = Template("""\
79 /* parsed class ${nvcl} */
80
81 #include "nvtypes.h"
82 #include "${clheader}"
83
84 #include <assert.h>
85 #include <stdio.h>
86 #include "util/macros.h"
87
88 %for mthd in methods:
89 struct nv_${nvcl.lower()}_${mthd.name} {
90 %for field in mthd.fields:
91 uint32_t ${field.name.lower()};
92 %endfor
93 };
94
95 static inline void
96 __${nvcl}_${mthd.name}(uint32_t *val_out, struct nv_${nvcl.lower()}_${mthd.name} st)
97 {
98 uint32_t val = 0;
99 %for field in mthd.fields:
100 <% field_width = field.end - field.start + 1 %>
101 %if field_width == 32:
102 val |= st.${field.name.lower()};
103 %else:
104 assert(st.${field.name.lower()} < (UINT64_C(1) << ${field_width}));
105 val |= st.${field.name.lower()} << ${field.start};
106 %endif
107 %endfor
108 *val_out = val;
109 }
110
111 #define V_${nvcl}_${mthd.name}(val, args...) { ${bs}
112 %for field in mthd.fields:
113 %for d in field.defs:
114 UNUSED uint32_t ${field.name}_${d} = ${nvcl}_${mthd.name}_${field.name}_${d}; ${bs}
115 %endfor
116 %endfor
117 %if len(mthd.fields) > 1:
118 struct nv_${nvcl.lower()}_${mthd.name} __data = args; ${bs}
119 %else:
120 <% field_name = mthd.fields[0].name.lower() %>\
121 struct nv_${nvcl.lower()}_${mthd.name} __data = { .${field_name} = (args) }; ${bs}
122 %endif
123 __${nvcl}_${mthd.name}(&val, __data); ${bs}
124 }
125
126 %if mthd.is_array:
127 #define VA_${nvcl}_${mthd.name}(i) V_${nvcl}_${mthd.name}
128 %else:
129 #define VA_${nvcl}_${mthd.name} V_${nvcl}_${mthd.name}
130 %endif
131
132 %if mthd.is_array:
133 #define P_${nvcl}_${mthd.name}(push, idx, args...) do { ${bs}
134 %else:
135 #define P_${nvcl}_${mthd.name}(push, args...) do { ${bs}
136 %endif
137 %for field in mthd.fields:
138 %for d in field.defs:
139 UNUSED uint32_t ${field.name}_${d} = ${nvcl}_${mthd.name}_${field.name}_${d}; ${bs}
140 %endfor
141 %endfor
142 uint32_t nvk_p_ret; ${bs}
143 V_${nvcl}_${mthd.name}(nvk_p_ret, args); ${bs}
144 %if mthd.is_array:
145 nv_push_val(push, ${nvcl}_${mthd.name}(idx), nvk_p_ret); ${bs}
146 %else:
147 nv_push_val(push, ${nvcl}_${mthd.name}, nvk_p_ret); ${bs}
148 %endif
149 } while(0)
150
151 %endfor
152
153 const char *P_PARSE_${nvcl}_MTHD(uint16_t idx) ATTRIBUTE_COLD;
154 void P_DUMP_${nvcl}_MTHD_DATA(FILE *fp, uint16_t idx, uint32_t data,
155 const char *prefix) ATTRIBUTE_COLD;
156 """)
157
158 TEMPLATE_C = Template("""\
159 #include "${header}"
160
161 #include <stdio.h>
162
163 #include "util/u_math.h"
164
165 <%def name="cases(mthd)">
166 %if mthd.is_array:
167 %for i in range(mthd.array_size):
168 case ${nvcl}_${mthd.name}(${i}):
169 %endfor
170 % else:
171 case ${nvcl}_${mthd.name}:
172 %endif
173 </%def>
174
175 <%def name="prev_cases()">
176 %if any_method_removed:
177 %for mthd in methods:
178 %if prev_methods.get(mthd.name, None) == mthd:
179 ${cases(mthd)}
180 %endif
181 %endfor
182 %else:
183 default:
184 %endif
185 </%def>
186
187 %if prev_nvcl:
188 const char *P_PARSE_${prev_nvcl}_MTHD(uint16_t idx) ATTRIBUTE_COLD;
189 void P_DUMP_${prev_nvcl}_MTHD_DATA(FILE *fp, uint16_t idx, uint32_t data,
190 const char *prefix) ATTRIBUTE_COLD;
191 %endif
192
193 const char*
194 P_PARSE_${nvcl}_MTHD(uint16_t idx)
195 {
196 switch (idx) {
197
198 %if prev_nvcl:
199 ${prev_cases()}
200 return P_PARSE_${prev_nvcl}_MTHD(idx);
201 %endif
202
203 %for mthd in methods:
204 %if mthd.is_array and mthd.array_size == 0:
205 <% continue %>
206 %endif
207 %if prev_methods.get(mthd.name, None) == mthd:
208 <% continue %>
209 %endif
210 %if mthd.is_array:
211 %for i in range(mthd.array_size):
212 case ${nvcl}_${mthd.name}(${i}):
213 return "${nvcl}_${mthd.name}(${i})";
214 %endfor
215 % else:
216 case ${nvcl}_${mthd.name}:
217 return "${nvcl}_${mthd.name}";
218 %endif
219 %endfor
220
221 %if not prev_nvcl or any_method_removed:
222 default:
223 return "unknown method";
224 %endif
225 }
226 }
227
228 void
229 P_DUMP_${nvcl}_MTHD_DATA(FILE *fp, uint16_t idx, uint32_t data,
230 const char *prefix)
231 {
232 UNUSED uint32_t parsed;
233 switch (idx) {
234
235 %if prev_nvcl:
236 ${prev_cases()}
237 P_DUMP_${prev_nvcl}_MTHD_DATA(fp, idx, data, prefix);
238 break;
239 %endif
240
241 %for mthd in methods:
242 %if mthd.is_array and mthd.array_size == 0:
243 <% continue %>
244 %endif
245 %if prev_methods.get(mthd.name, None) == mthd:
246 <% continue %>
247 %endif
248 ${cases(mthd)}
249 %for field in mthd.fields:
250 <% field_width = field.end - field.start + 1 %>
251 %if field_width == 32:
252 parsed = data;
253 %else:
254 parsed = (data >> ${field.start}) & ((1u << ${field_width}) - 1);
255 %endif
256 fprintf(fp, "%s.${field.name} = ", prefix);
257 %if len(field.defs):
258 switch (parsed) {
259 %for d in field.unique_defs:
260 case ${nvcl}_${mthd.name}_${field.name}_${d}:
261 fprintf(fp, "${d}${bs}n");
262 break;
263 %endfor
264 default:
265 fprintf(fp, "0x%x${bs}n", parsed);
266 break;
267 }
268 %else:
269 %if mthd.is_float:
270 fprintf(fp, "%ff (0x%x)${bs}n", uif(parsed), parsed);
271 %else:
272 fprintf(fp, "(0x%x)${bs}n", parsed);
273 %endif
274 %endif
275 %endfor
276 break;
277 %endfor
278 %if not prev_nvcl or any_method_removed:
279 default:
280 fprintf(fp, "%s.VALUE = 0x%x${bs}n", prefix, data);
281 break;
282 %endif
283 }
284 }
285 """)
286
287 TEMPLATE_RS = Template("""\
288 // parsed class ${nvcl}
289
290 % if version is not None:
291 pub const ${version[0]}: u16 = ${version[1]};
292 % endif
293 """)
294
295 TEMPLATE_RS_MTHD = Template("""\
296 use crate::Mthd;
297 use crate::ArrayMthd;
298
299 %if prev_mod is not None:
300 use crate::classes::${prev_mod}::mthd as ${prev_mod};
301 %endif
302
303 // parsed class ${nvcl}
304
305 ## Write out the methods in Rust
306 %for mthd in methods:
307
308 ## If this method is the same as the one in the previous class header, just
309 ## pub use everything instead of re-generating it. This significantly
310 ## reduces the number of unique Rust types and trait implementations.
311 %if prev_methods.get(mthd.name, None) == mthd:
312 %for field in mthd.fields:
313 %if field.is_rs_enum:
314 pub use ${prev_mod}::${field.rs_type(mthd)};
315 %endif
316 %endfor
317 pub use ${prev_mod}::${to_camel(mthd.name)};
318 <% continue %>
319 %endif
320
321 ## If there are a range of values for a field, we define an enum.
322 %for field in mthd.fields:
323 %if field.is_rs_enum:
324 #[repr(u16)]
325 #[derive(Copy, Clone, Debug, PartialEq)]
326 pub enum ${field.rs_type(mthd)} {
327 %for def_name, def_value in field.unique_defs.items():
328 ${to_camel(def_name)} = ${def_value.lower()},
329 %endfor
330 }
331 %endif
332 %endfor
333
334 ## We also define a struct with the fields for the mthd.
335 #[derive(Copy, Clone, Debug, PartialEq)]
336 pub struct ${to_camel(mthd.name)} {
337 %for field in mthd.fields:
338 pub ${field.rs_name}: ${field.rs_type(mthd)},
339 %endfor
340 }
341
342 ## Notice that the "to_bits" implementation is identical, so the first brace is
343 ## not closed.
344 % if not mthd.is_array:
345 ## This trait lays out how the conversion to u32 happens
346 impl Mthd for ${to_camel(mthd.name)} {
347 const ADDR: u16 = ${mthd.addr_expr(None)};
348 const CLASS: u16 = ${version[1].lower() if version is not None else nvcl.lower().replace("nv", "0x")};
349
350 %else:
351 impl ArrayMthd for ${to_camel(mthd.name)} {
352 const CLASS: u16 = ${version[1].lower() if version is not None else nvcl.lower().replace("nv", "0x")};
353
354 fn addr(i: usize) -> u16 {
355 (${mthd.addr_expr('i')}).try_into().unwrap()
356 }
357 %endif
358
359 #[inline]
360 fn to_bits(self) -> u32 {
361 let mut val = 0;
362 %for field in mthd.fields:
363 <% field_width = field.end - field.start + 1 %>
364 %if field.rs_type(mthd) == "u32":
365 let field_u32 = self.${field.rs_name};
366 %else:
367 let field_u32 = self.${field.rs_name} as u32;
368 %endif
369 %if field_width == 32:
370 <% assert field_width == 32 %>
371 %else:
372 assert!(field_u32 < (1 << ${field_width}));
373 %endif
374 %if field.start == 0:
375 val |= field_u32;
376 %else:
377 val |= field_u32 << ${field.start};
378 %endif
379 %endfor
380
381 val
382 }
383 ## Close the first brace.
384 }
385 %endfor
386 """)
387
388 ## A mere convenience to convert snake_case to CamelCase. Numbers are prefixed
389 ## with "_".
390 def to_camel(snake_str):
391 words = snake_str.split("_")
392 words = [w for w in words if w != ""]
393 assert len(words) > 0
394
395 def to_camel_word(prev_word, word):
396 if word[0].isdigit():
397 if prev_word is None or prev_word[-1].isdigit():
398 return "_" + word.lower()
399 else:
400 return word.lower()
401 else:
402 return word.title()
403
404 return "".join(
405 to_camel_word(prev_word, word)
406 for prev_word, word in zip([None] + words, words)
407 )
408
409 def strip_parens(s):
410 s = s.strip()
411 while s.startswith('(') and s.endswith(')'):
412 s = s[1:-1].strip()
413 return s
414
415 def glob_match(glob, name):
416 if glob.endswith('*'):
417 return name.startswith(glob[:-1])
418 else:
419 assert '*' not in glob
420 return name == glob
421
422 class Field(object):
423 def __init__(self, name, start, end):
424 self.name = name
425 self.start = int(start)
426 self.end = int(end)
427 self.defs = {}
428 self._values = set()
429 self.unique_defs = {}
430
431 def __eq__(self, other):
432 if not isinstance(other, Field):
433 return False
434
435 return self.name == other.name and \
436 self.start == other.start and \
437 self.end == other.end and \
438 self.defs == other.defs
439
440 def add_def(self, name, value):
441 assert name not in self.defs
442 self.defs[name] = value
443
444 if value not in self._values:
445 self._values.add(value)
446 self.unique_defs[name] = value
447
448 @property
449 def is_bool(self):
450 if len(self.defs) != 2:
451 return False
452
453 for d in self.defs:
454 if not d.lower() in ['true', 'false']:
455 return False
456
457 return True
458
459 @property
460 def is_rs_enum(self):
461 return not self.is_bool and len(self.defs) > 0
462
463 def rs_type(self, mthd):
464 if self.is_bool:
465 return "bool"
466 elif self.name == 'V' and len(self.defs) > 0:
467 return to_camel(mthd.name) + 'V'
468 elif len(self.defs) > 0:
469 assert(self.name != "")
470 return to_camel(mthd.name) + to_camel(self.name)
471 elif mthd.is_float:
472 return "f32"
473 else:
474 return "u32"
475
476 @property
477 def rs_name(self):
478 name = self.name.lower()
479
480 # Fix up some Rust keywords
481 if name == 'type':
482 return 'type_'
483 elif name == 'override':
484 return 'override_'
485 elif name[0].isdigit():
486 return '_' + name
487 else:
488 return re.sub(r'_+', '_', name)
489
490 class Method(object):
491 def __init__(self, name, addr, is_array=False):
492 self.name = name
493 self.addr = addr
494 self.is_array = is_array
495 self.fields = []
496
497 def __eq__(self, other):
498 if not isinstance(other, Method):
499 return False
500
501 return self.name == other.name and \
502 self.addr == other.addr and \
503 self.is_array == other.is_array and \
504 self.fields == other.fields
505
506 def addr_expr(self, idx_var):
507 expr = self.addr
508 if self.is_array:
509 # We don't support multiple-indexing but the headers aren't
510 # consistent in choosing i vs. j.
511 assert idx_var is not None
512 assert not ('i' in self.addr and 'j' in self.addr)
513 expr = re.sub(r'[ij]', idx_var, expr)
514
515 # Rust doesn't like extra parens
516 expr = expr.replace('(' + idx_var + ')', idx_var)
517 else:
518 assert not 'i' in self.addr and not 'j' in self.addr
519
520 # Rust also doesn't like extra parens
521 expr = strip_parens(expr)
522
523 return expr
524
525 @property
526 def array_size(self):
527 for (glob, value) in METHOD_ARRAY_SIZES.items():
528 if glob_match(glob, self.name):
529 return value
530 return 0
531
532 @property
533 def is_float(self):
534 for glob in METHOD_IS_FLOAT:
535 if glob_match(glob, self.name):
536 assert len(self.fields) == 1
537 return True
538 return False
539
540 def parse_header(nvcl, f):
541 # Simple state machine
542 # state 0 looking for a new method define
543 # state 1 looking for new fields in a method
544 # state 2 looking for enums for a fields in a method
545 # blank lines reset the state machine to 0
546
547 version = None
548 state = 0
549 methods = {}
550 curmthd = None
551
552 for line in f:
553 if line.strip() == "":
554 state = 0
555 if curmthd is not None and len(curmthd.fields) == 0:
556 del methods[curmthd.name]
557 curmthd = None
558 continue
559
560 if line.startswith("#define"):
561 list = [strip_parens(e) for e in line.split()]
562 if "_cl_" in list[1]:
563 continue
564
565 if (not list[1].startswith(nvcl)) or list[1].endswith('VIDEO_DECODER'):
566 if len(list) > 2 and list[2].startswith("0x"):
567 assert version is None
568 version = (list[1], list[2])
569 continue
570
571 if list[1].endswith("TYPEDEF"):
572 continue
573
574 if state == 2:
575 teststr = nvcl + "_" + curmthd.name + "_" + curfield.name + "_"
576 if ":" in list[2]:
577 state = 1
578 elif teststr in list[1]:
579 if not SKIP_FIELD[0] in list[1]:
580 curfield.add_def(list[1].removeprefix(teststr), list[2])
581 else:
582 state = 1
583
584 if state == 1:
585 teststr = nvcl + "_" + curmthd.name + "_"
586 if teststr in list[1]:
587 if ("0x" in list[2]):
588 state = 1
589 else:
590 field = list[1].removeprefix(teststr)
591 bitfield = list[2].split(":")
592 f = Field(field, bitfield[1], bitfield[0])
593 curmthd.fields.append(f)
594 curfield = f
595 state = 2
596 else:
597 if len(curmthd.fields) == 0:
598 del methods[curmthd.name]
599 curmthd = None
600 state = 0
601
602 if state == 0:
603 if curmthd is not None and len(curmthd.fields) == 0:
604 del methods[curmthd.name]
605
606 teststr = nvcl + "_"
607 is_array = 0
608 if (':' in list[2]):
609 continue
610 name = list[1].removeprefix(teststr)
611 if name.endswith("(i)"):
612 is_array = 1
613 name = name.removesuffix("(i)")
614 if name.endswith("(j)"):
615 is_array = 1
616 name = name.removesuffix("(j)")
617
618 curmthd = Method(name, list[2], is_array)
619 methods[name] = curmthd
620 state = 1
621
622 return (version, methods)
623
624 def nvcl_for_filename(name):
625 name = name.removeprefix("cl")
626 name = name.removesuffix(".h")
627 return "NV" + name.upper()
628
629 def main():
630 parser = argparse.ArgumentParser()
631 parser.add_argument('--out-h', required=False, help='Output C header.')
632 parser.add_argument('--out-c', required=False, help='Output C file.')
633 parser.add_argument('--out-rs', required=False, help='Output Rust file.')
634 parser.add_argument('--out-rs-mthd', required=False,
635 help='Output Rust file for methods.')
636 parser.add_argument('--in-h',
637 help='Input class header file.',
638 required=True)
639 parser.add_argument('--prev-in-h',
640 help='Previous input class header file.',
641 required=False)
642 args = parser.parse_args()
643
644 clheader = os.path.basename(args.in_h)
645 nvcl = nvcl_for_filename(clheader)
646
647 with open(args.in_h, 'r', encoding='utf-8') as f:
648 (version, methods) = parse_header(nvcl, f)
649
650 prev_mod = None
651 prev_methods = {}
652 prev_nvcl = None
653 if args.prev_in_h is not None:
654 prev_clheader = os.path.basename(args.prev_in_h)
655 prev_nvcl = nvcl_for_filename(prev_clheader)
656 prev_mod = prev_clheader.removesuffix(".h")
657 with open(args.prev_in_h, 'r', encoding='utf-8') as f:
658 (prev_version, prev_methods) = parse_header(prev_nvcl, f)
659
660 any_method_removed = any(x not in methods for x in prev_methods)
661
662 environment = {
663 'clheader': clheader,
664 'nvcl': nvcl,
665 'version': version,
666 'methods': list(methods.values()),
667 'prev_mod': prev_mod,
668 'prev_methods': prev_methods,
669 'prev_nvcl': prev_nvcl,
670 'any_method_removed': any_method_removed,
671 'to_camel': to_camel,
672 'bs': '\\'
673 }
674
675 if args.out_h is not None:
676 environment['header'] = os.path.basename(args.out_h)
677 util.write_template(args.out_h, TEMPLATE_H, environment)
678 if args.out_c is not None:
679 util.write_template(args.out_c, TEMPLATE_C, environment)
680 if args.out_rs is not None:
681 util.write_template_rs(args.out_rs, TEMPLATE_RS, environment)
682 if args.out_rs_mthd is not None:
683 util.write_template_rs(args.out_rs_mthd, TEMPLATE_RS_MTHD, environment)
684
685
686 if __name__ == '__main__':
687 main()