Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/usr/bin/env python
  2. CopyRight = '''
  3. /*
  4. * Copyright 2015 Advanced Micro Devices, Inc.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * on the rights to use, copy, modify, merge, publish, distribute, sub
  10. * license, and/or sell copies of the Software, and to permit persons to whom
  11. * the Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the next
  14. * paragraph) shall be included in all copies or substantial portions of the
  15. * Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
  21. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  23. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. */
  26. '''
  27. import sys
  28. import re
  29. class Field:
  30. def __init__(self, reg, s_name):
  31. self.s_name = s_name
  32. self.name = strip_prefix(s_name)
  33. self.values = []
  34. self.varname_values = '%s__%s__values' % (reg.r_name.lower(), self.name.lower())
  35. class Reg:
  36. def __init__(self, r_name):
  37. self.r_name = r_name
  38. self.name = strip_prefix(r_name)
  39. self.fields = []
  40. self.varname_fields = '%s__fields' % self.r_name.lower()
  41. self.own_fields = True
  42. def strip_prefix(s):
  43. '''Strip prefix in the form ._.*_, e.g. R_001234_'''
  44. return s[s[2:].find('_')+3:]
  45. def parse(filename):
  46. stream = open(filename)
  47. regs = []
  48. packets = []
  49. for line in stream:
  50. if not line.startswith('#define '):
  51. continue
  52. line = line[8:].strip()
  53. if line.startswith('R_'):
  54. reg = Reg(line.split()[0])
  55. regs.append(reg)
  56. elif line.startswith('S_'):
  57. field = Field(reg, line[:line.find('(')])
  58. reg.fields.append(field)
  59. elif line.startswith('V_'):
  60. field.values.append(line.split()[0])
  61. elif line.startswith('PKT3_') and line.find('0x') != -1 and line.find('(') == -1:
  62. packets.append(line.split()[0])
  63. # Copy fields to indexed registers which have their fields only defined
  64. # at register index 0.
  65. # For example, copy fields from CB_COLOR0_INFO to CB_COLORn_INFO, n > 0.
  66. match_number = re.compile('[0-9]+')
  67. reg_dict = dict()
  68. # Create a dict of registers with fields and '0' in their name
  69. for reg in regs:
  70. if len(reg.fields) and reg.name.find('0') != -1:
  71. reg_dict[reg.name] = reg
  72. # Assign fields
  73. for reg in regs:
  74. if not len(reg.fields):
  75. reg0 = reg_dict.get(match_number.sub('0', reg.name))
  76. if reg0 != None:
  77. reg.fields = reg0.fields
  78. reg.varname_fields = reg0.varname_fields
  79. reg.own_fields = False
  80. return (regs, packets)
  81. def write_tables(tables):
  82. regs = tables[0]
  83. packets = tables[1]
  84. print '/* This file is autogenerated by sid_tables.py from sid.h. Do not edit directly. */'
  85. print
  86. print CopyRight.strip()
  87. print '''
  88. #ifndef SID_TABLES_H
  89. #define SID_TABLES_H
  90. struct si_field {
  91. const char *name;
  92. unsigned mask;
  93. unsigned num_values;
  94. const char **values;
  95. };
  96. struct si_reg {
  97. const char *name;
  98. unsigned offset;
  99. unsigned num_fields;
  100. const struct si_field *fields;
  101. };
  102. struct si_packet3 {
  103. const char *name;
  104. unsigned op;
  105. };
  106. '''
  107. print 'static const struct si_packet3 packet3_table[] = {'
  108. for pkt in packets:
  109. print '\t{"%s", %s},' % (pkt[5:], pkt)
  110. print '};'
  111. print
  112. for reg in regs:
  113. if len(reg.fields) and reg.own_fields:
  114. for field in reg.fields:
  115. if len(field.values):
  116. print 'static const char *%s[] = {' % (field.varname_values)
  117. for value in field.values:
  118. print '\t[%s] = "%s",' % (value, strip_prefix(value))
  119. print '};'
  120. print
  121. print 'static const struct si_field %s[] = {' % (reg.varname_fields)
  122. for field in reg.fields:
  123. if len(field.values):
  124. print '\t{"%s", %s(~0u), ARRAY_SIZE(%s), %s},' % (field.name,
  125. field.s_name, field.varname_values, field.varname_values)
  126. else:
  127. print '\t{"%s", %s(~0u)},' % (field.name, field.s_name)
  128. print '};'
  129. print
  130. print 'static const struct si_reg reg_table[] = {'
  131. for reg in regs:
  132. if len(reg.fields):
  133. print '\t{"%s", %s, ARRAY_SIZE(%s), %s},' % (reg.name, reg.r_name,
  134. reg.varname_fields, reg.varname_fields)
  135. else:
  136. print '\t{"%s", %s},' % (reg.name, reg.r_name)
  137. print '};'
  138. print
  139. print '#endif'
  140. def main():
  141. tables = []
  142. for arg in sys.argv[1:]:
  143. tables.extend(parse(arg))
  144. write_tables(tables)
  145. if __name__ == '__main__':
  146. main()