summaryrefslogtreecommitdiffstats
path: root/src/intel/genxml/gen_bits_header.py
blob: 3b0f9ab2f3f72802d995d1c3a5060ca6ddb89de5 (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#encoding=utf-8
# Copyright © 2017 Intel Corporation

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from __future__ import (
    absolute_import, division, print_function, unicode_literals
)

import argparse
import os
import re
import xml.parsers.expat

from mako.template import Template

TEMPLATE = Template("""\
<%!
from operator import itemgetter
%>\
/*
 * Copyright © 2017 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

/* THIS FILE HAS BEEN GENERATED, DO NOT HAND EDIT.
 *
 * Sizes of bitfields in genxml instructions, structures, and registers.
 */

#ifndef ${guard}
#define ${guard}

#include <stdint.h>

#include "dev/gen_device_info.h"
#include "util/macros.h"

<%def name="emit_per_gen_prop_func(item, prop)">
%if item.has_prop(prop):
% for gen, value in sorted(item.iter_prop(prop), reverse=True):
#define ${gen.prefix(item.token_name)}_${prop}  ${value}
% endfor

static inline uint32_t ATTRIBUTE_PURE
${item.token_name}_${prop}(const struct gen_device_info *devinfo)
{
   switch (devinfo->gen) {
   case 12: return ${item.get_prop(prop, 12)};
   case 11: return ${item.get_prop(prop, 11)};
   case 10: return ${item.get_prop(prop, 10)};
   case 9: return ${item.get_prop(prop, 9)};
   case 8: return ${item.get_prop(prop, 8)};
   case 7:
      if (devinfo->is_haswell) {
         return ${item.get_prop(prop, 7.5)};
      } else {
         return ${item.get_prop(prop, 7)};
      }
   case 6: return ${item.get_prop(prop, 6)};
   case 5: return ${item.get_prop(prop, 5)};
   case 4:
      if (devinfo->is_g4x) {
         return ${item.get_prop(prop, 4.5)};
      } else {
         return ${item.get_prop(prop, 4)};
      }
   default:
      unreachable("Invalid hardware generation");
   }
}
%endif
</%def>

#ifdef __cplusplus
extern "C" {
#endif
% for _, container in sorted(containers.items(), key=itemgetter(0)):

/* ${container.name} */

${emit_per_gen_prop_func(container, 'length')}

% for _, field in sorted(container.fields.items(), key=itemgetter(0)):

/* ${container.name}::${field.name} */

${emit_per_gen_prop_func(field, 'bits')}

${emit_per_gen_prop_func(field, 'start')}

% endfor
% endfor

#ifdef __cplusplus
}
#endif

#endif /* ${guard} */""", output_encoding='utf-8')

alphanum_nono = re.compile(r'[ /\[\]()\-:.,=>#&*"+\\]+')
def to_alphanum(name):
    global alphanum_nono
    return alphanum_nono.sub('', name)

def safe_name(name):
    name = to_alphanum(name)
    if not name[0].isalpha():
        name = '_' + name
    return name

class Gen(object):

    def __init__(self, z):
        # Convert potential "major.minor" string
        self.tenx = int(float(z) * 10)

    def __lt__(self, other):
        return self.tenx < other.tenx

    def __hash__(self):
        return hash(self.tenx)

    def __eq__(self, other):
        return self.tenx == other.tenx

    def prefix(self, token):
        gen = self.tenx

        if gen % 10 == 0:
            gen //= 10

        if token[0] == '_':
            token = token[1:]

        return 'GEN{}_{}'.format(gen, token)

class Container(object):

    def __init__(self, name):
        self.name = name
        self.token_name = safe_name(name)
        self.length_by_gen = {}
        self.fields = {}

    def add_gen(self, gen, xml_attrs):
        assert isinstance(gen, Gen)
        if 'length' in xml_attrs:
            self.length_by_gen[gen] = xml_attrs['length']

    def get_field(self, field_name, create=False):
        key = to_alphanum(field_name)
        if key not in self.fields:
            if create:
                self.fields[key] = Field(self, field_name)
            else:
                return None
        return self.fields[key]

    def has_prop(self, prop):
        if prop == 'length':
            return bool(self.length_by_gen)
        else:
            raise ValueError('Invalid property: "{0}"'.format(prop))

    def iter_prop(self, prop):
        if prop == 'length':
            return self.length_by_gen.items()
        else:
            raise ValueError('Invalid property: "{0}"'.format(prop))

    def get_prop(self, prop, gen):
        if not isinstance(gen, Gen):
            gen = Gen(gen)

        if prop == 'length':
            return self.length_by_gen.get(gen, 0)
        else:
            raise ValueError('Invalid property: "{0}"'.format(prop))

class Field(object):

    def __init__(self, container, name):
        self.name = name
        self.token_name = safe_name('_'.join([container.name, self.name]))
        self.bits_by_gen = {}
        self.start_by_gen = {}

    def add_gen(self, gen, xml_attrs):
        assert isinstance(gen, Gen)
        start = int(xml_attrs['start'])
        end = int(xml_attrs['end'])
        self.start_by_gen[gen] = start
        self.bits_by_gen[gen] = 1 + end - start

    def has_prop(self, prop):
        return True

    def iter_prop(self, prop):
        if prop == 'bits':
            return self.bits_by_gen.items()
        elif prop == 'start':
            return self.start_by_gen.items()
        else:
            raise ValueError('Invalid property: "{0}"'.format(prop))

    def get_prop(self, prop, gen):
        if not isinstance(gen, Gen):
            gen = Gen(gen)

        if prop == 'bits':
            return self.bits_by_gen.get(gen, 0)
        elif prop == 'start':
            return self.start_by_gen.get(gen, 0)
        else:
            raise ValueError('Invalid property: "{0}"'.format(prop))

class XmlParser(object):

    def __init__(self, containers):
        self.parser = xml.parsers.expat.ParserCreate()
        self.parser.StartElementHandler = self.start_element
        self.parser.EndElementHandler = self.end_element

        self.gen = None
        self.containers = containers
        self.container = None

    def parse(self, filename):
        with open(filename, 'rb') as f:
            self.parser.ParseFile(f)

    def start_element(self, name, attrs):
        if name == 'genxml':
            self.gen = Gen(attrs['gen'])
        elif name in ('instruction', 'struct', 'register'):
            if name == 'instruction' and 'engine' in attrs:
                engines = set(attrs['engine'].split('|'))
                if not engines & self.engines:
                    return
            self.start_container(attrs)
        elif name == 'field':
            self.start_field(attrs)
        else:
            pass

    def end_element(self, name):
        if name == 'genxml':
            self.gen = None
        elif name in ('instruction', 'struct', 'register'):
            self.container = None
        else:
            pass

    def start_container(self, attrs):
        assert self.container is None
        name = attrs['name']
        if name not in self.containers:
            self.containers[name] = Container(name)
        self.container = self.containers[name]
        self.container.add_gen(self.gen, attrs)

    def start_field(self, attrs):
        if self.container is None:
            return

        field_name = attrs.get('name', None)
        if not field_name:
            return

        self.container.get_field(field_name, True).add_gen(self.gen, attrs)

def parse_args():
    p = argparse.ArgumentParser()
    p.add_argument('-o', '--output', type=str,
                   help="If OUTPUT is unset or '-', then it defaults to '/dev/stdout'")
    p.add_argument('--cpp-guard', type=str,
                   help='If unset, then CPP_GUARD is derived from OUTPUT.')
    p.add_argument('--engines', nargs='?', type=str, default='render',
                   help="Comma-separated list of engines whose instructions should be parsed (default: %(default)s)")
    p.add_argument('xml_sources', metavar='XML_SOURCE', nargs='+')

    pargs = p.parse_args()

    if pargs.output in (None, '-'):
        pargs.output = '/dev/stdout'

    if pargs.cpp_guard is None:
        pargs.cpp_guard = os.path.basename(pargs.output).upper().replace('.', '_')

    return pargs

def main():
    pargs = parse_args()

    engines = pargs.engines.split(',')
    valid_engines = [ 'render', 'blitter', 'video' ]
    if set(engines) - set(valid_engines):
        print("Invalid engine specified, valid engines are:\n")
        for e in valid_engines:
            print("\t%s" % e)
        sys.exit(1)

    # Maps name => Container
    containers = {}

    for source in pargs.xml_sources:
        p = XmlParser(containers)
        p.engines = set(engines)
        p.parse(source)

    with open(pargs.output, 'wb') as f:
        f.write(TEMPLATE.render(containers=containers, guard=pargs.cpp_guard))

if __name__ == '__main__':
    main()