• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#
2# Copyright (C) 2020 Collabora, Ltd.
3#
4# Permission is hereby granted, free of charge, to any person obtaining a
5# copy of this software and associated documentation files (the "Software"),
6# to deal in the Software without restriction, including without limitation
7# the rights to use, copy, modify, merge, publish, distribute, sublicense,
8# and/or sell copies of the Software, and to permit persons to whom the
9# Software is furnished to do so, subject to the following conditions:
10#
11# The above copyright notice and this permission notice (including the next
12# paragraph) shall be included in all copies or substantial portions of the
13# Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21# IN THE SOFTWARE.
22
23# Useful for autogeneration
24COPYRIGHT = """/*
25 * Copyright (C) 2020 Collabora, Ltd.
26 *
27 * Permission is hereby granted, free of charge, to any person obtaining a
28 * copy of this software and associated documentation files (the "Software"),
29 * to deal in the Software without restriction, including without limitation
30 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
31 * and/or sell copies of the Software, and to permit persons to whom the
32 * Software is furnished to do so, subject to the following conditions:
33 *
34 * The above copyright notice and this permission notice (including the next
35 * paragraph) shall be included in all copies or substantial portions of the
36 * Software.
37 *
38 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
39 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
40 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
41 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
42 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
43 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
44 * SOFTWARE.
45 */
46
47/* Autogenerated file, do not edit */
48
49"""
50
51# Parse instruction set XML into a normalized form for processing
52
53import xml.etree.ElementTree as ET
54import copy
55import itertools
56from collections import OrderedDict
57
58def parse_cond(cond, aliased = False):
59    if cond.tag == 'reserved':
60        return None
61
62    if cond.attrib.get('alias', False) and not aliased:
63        return ['alias', parse_cond(cond, True)]
64
65    if 'left' in cond.attrib:
66        return [cond.tag, cond.attrib['left'], cond.attrib['right']]
67    else:
68        return [cond.tag] + [parse_cond(x) for x in cond.findall('*')]
69
70def parse_exact(obj):
71    return [int(obj.attrib['mask'], 0), int(obj.attrib['exact'], 0)]
72
73def parse_derived(obj):
74    out = []
75
76    for deriv in obj.findall('derived'):
77        loc = [int(deriv.attrib['start']), int(deriv.attrib['size'])]
78        count = 1 << loc[1]
79
80        opts = [parse_cond(d) for d in deriv.findall('*')]
81        default = [None] * count
82        opts_fit = (opts + default)[0:count]
83
84        out.append([loc, opts_fit])
85
86    return out
87
88def parse_modifiers(obj, include_pseudo):
89    out = []
90
91    for mod in obj.findall('mod'):
92        if mod.attrib.get('pseudo', False) and not include_pseudo:
93            continue
94
95        name = mod.attrib['name']
96        start = mod.attrib.get('start', None)
97        size = int(mod.attrib['size'])
98
99        if start is not None:
100            start = int(start)
101
102        opts = [x.text if x.tag == 'opt' else x.tag for x in mod.findall('*')]
103
104        if len(opts) == 0:
105            assert('opt' in mod.attrib)
106            opts = ['none', mod.attrib['opt']]
107
108        # Find suitable default
109        default = mod.attrib.get('default', 'none' if 'none' in opts else None)
110
111        # Pad out as reserved
112        count = (1 << size)
113        opts = (opts + (['reserved'] * count))[0:count]
114        out.append([[name, start, size], default, opts])
115
116    return out
117
118def parse_copy(enc, existing):
119    for node in enc.findall('copy'):
120        name = node.get('name')
121        for ex in existing:
122            if ex[0][0] == name:
123                ex[0][1] = node.get('start')
124
125def parse_instruction(ins, include_pseudo):
126    common = {
127            'srcs': [],
128            'modifiers': [],
129            'immediates': [],
130            'swaps': [],
131            'derived': [],
132            'staging': ins.attrib.get('staging', '').split('=')[0],
133            'staging_count': ins.attrib.get('staging', '=0').split('=')[1],
134            'dests': int(ins.attrib.get('dests', '1')),
135            'variable_dests': ins.attrib.get('variable_dests', False),
136            'variable_srcs': ins.attrib.get('variable_srcs', False),
137            'unused': ins.attrib.get('unused', False),
138            'pseudo': ins.attrib.get('pseudo', False),
139            'message': ins.attrib.get('message', 'none'),
140            'last': ins.attrib.get('last', False),
141            'table': ins.attrib.get('table', False),
142    }
143
144    if 'exact' in ins.attrib:
145        common['exact'] = parse_exact(ins)
146
147    for src in ins.findall('src'):
148        if src.attrib.get('pseudo', False) and not include_pseudo:
149            continue
150
151        mask = int(src.attrib['mask'], 0) if ('mask' in src.attrib) else 0xFF
152        common['srcs'].append([int(src.attrib['start'], 0), mask])
153
154    for imm in ins.findall('immediate'):
155        if imm.attrib.get('pseudo', False) and not include_pseudo:
156            continue
157
158        start = int(imm.attrib['start']) if 'start' in imm.attrib else None
159        common['immediates'].append([imm.attrib['name'], start, int(imm.attrib['size'])])
160
161    common['derived'] = parse_derived(ins)
162    common['modifiers'] = parse_modifiers(ins, include_pseudo)
163
164    for swap in ins.findall('swap'):
165        lr = [int(swap.get('left')), int(swap.get('right'))]
166        cond = parse_cond(swap.findall('*')[0])
167        rewrites = {}
168
169        for rw in swap.findall('rewrite'):
170            mp = {}
171
172            for m in rw.findall('map'):
173                mp[m.attrib['from']] = m.attrib['to']
174
175            rewrites[rw.attrib['name']] = mp
176
177        common['swaps'].append([lr, cond, rewrites])
178
179    encodings = ins.findall('encoding')
180    variants = []
181
182    if len(encodings) == 0:
183        variants = [[None, common]]
184    else:
185        for enc in encodings:
186            variant = copy.deepcopy(common)
187            assert(len(variant['derived']) == 0)
188
189            variant['exact'] = parse_exact(enc)
190            variant['derived'] = parse_derived(enc)
191            parse_copy(enc, variant['modifiers'])
192
193            cond = parse_cond(enc.findall('*')[0])
194            variants.append([cond, variant])
195
196    return variants
197
198def parse_instructions(xml, include_unused = False, include_pseudo = False):
199    final = {}
200    instructions = ET.parse(xml).getroot().findall('ins')
201
202    for ins in instructions:
203        parsed = parse_instruction(ins, include_pseudo)
204
205        # Some instructions are for useful disassembly only and can be stripped
206        # out of the compiler, particularly useful for release builds
207        if parsed[0][1]["unused"] and not include_unused:
208            continue
209
210        # On the other hand, some instructions are only for the IR, not disassembly
211        if parsed[0][1]["pseudo"] and not include_pseudo:
212            continue
213
214        final[ins.attrib['name']] = parsed
215
216    return final
217
218# Expand out an opcode name to something C-escaped
219
220def opname_to_c(name):
221    return name.lower().replace('*', 'fma_').replace('+', 'add_').replace('.', '_')
222
223# Expand out distinct states to distrinct instructions, with a placeholder
224# condition for instructions with a single state
225
226def expand_states(instructions):
227    out = {}
228
229    for ins in instructions:
230        c = instructions[ins]
231
232        for ((test, desc), i) in zip(c, range(len(c))):
233            # Construct a name for the state
234            name = ins + (('.' + str(i)) if len(c) > 1 else '')
235
236            out[name] = (ins, test if test is not None else [], desc)
237
238    return out
239
240# Drop keys used for packing to simplify IR representation, so we can check for
241# equivalence easier
242
243def simplify_to_ir(ins):
244    return {
245            'staging': ins['staging'],
246            'srcs': len(ins['srcs']),
247            'dests': ins['dests'],
248            'variable_dests': ins['variable_dests'],
249            'variable_srcs': ins['variable_srcs'],
250            'modifiers': [[m[0][0], m[2]] for m in ins['modifiers']],
251            'immediates': [m[0] for m in ins['immediates']]
252        }
253
254# Converstions to integers default to rounding-to-zero
255# All other opcodes default to rounding to nearest even
256def default_round_to_zero(name):
257    # 8-bit int to float is exact
258    subs = ['_TO_U', '_TO_S', '_TO_V2U', '_TO_V2S', '_TO_V4U', '_TO_V4S']
259    return any([x in name for x in subs])
260
261def combine_ir_variants(instructions, key):
262    seen = [op for op in instructions.keys() if op[1:] == key]
263    variant_objs = [[simplify_to_ir(Q[1]) for Q in instructions[x]] for x in seen]
264    variants = sum(variant_objs, [])
265
266    # Accumulate modifiers across variants
267    modifiers = {}
268
269    for s in variants[0:]:
270        # Check consistency
271        assert(s['srcs'] == variants[0]['srcs'])
272        assert(s['dests'] == variants[0]['dests'])
273        assert(s['immediates'] == variants[0]['immediates'])
274        assert(s['staging'] == variants[0]['staging'])
275
276        for name, opts in s['modifiers']:
277            if name not in modifiers:
278                modifiers[name] = copy.deepcopy(opts)
279            else:
280                modifiers[name] += opts
281
282    # Great, we've checked srcs/immediates are consistent and we've summed over
283    # modifiers
284    return {
285            'key': key,
286            'srcs': variants[0]['srcs'],
287            'dests': variants[0]['dests'],
288            'variable_dests': variants[0]['variable_dests'],
289            'variable_srcs': variants[0]['variable_srcs'],
290            'staging': variants[0]['staging'],
291            'immediates': sorted(variants[0]['immediates']),
292            'modifiers': modifiers,
293            'v': len(variants),
294            'ir': variants,
295            'rtz': default_round_to_zero(key)
296        }
297
298# Partition instructions to mnemonics, considering units and variants
299# equivalent.
300
301def partition_mnemonics(instructions):
302    key_func = lambda x: x[1:]
303    sorted_instrs = sorted(instructions.keys(), key = key_func)
304    partitions = itertools.groupby(sorted_instrs, key_func)
305    return { k: combine_ir_variants(instructions, k) for k, v in partitions }
306
307# Generate modifier lists, by accumulating all the possible modifiers, and
308# deduplicating thus assigning canonical enum values. We don't try _too_ hard
309# to be clever, but by preserving as much of the original orderings as
310# possible, later instruction encoding is simplified a bit.  Probably a micro
311# optimization but we have to pick _some_ ordering, might as well choose the
312# most convenient.
313#
314# THIS MUST BE DETERMINISTIC
315
316def order_modifiers(ir_instructions):
317    out = {}
318
319    # modifier name -> (list of option strings)
320    modifier_lists = {}
321
322    for ins in sorted(ir_instructions):
323        modifiers = ir_instructions[ins]["modifiers"]
324
325        for name in modifiers:
326            name_ = name[0:-1] if name[-1] in "0123" else name
327
328            if name_ not in modifier_lists:
329                modifier_lists[name_] = copy.deepcopy(modifiers[name])
330            else:
331                modifier_lists[name_] += modifiers[name]
332
333    for mod in modifier_lists:
334        lst = list(OrderedDict.fromkeys(modifier_lists[mod]))
335
336        # Ensure none is false for booleans so the builder makes sense
337        if len(lst) == 2 and lst[1] == "none":
338            lst.reverse()
339        elif mod == "table":
340            # We really need a zero sentinel to materialize DTSEL
341            assert(lst[2] == "none")
342            lst[2] = lst[0]
343            lst[0] = "none"
344
345        out[mod] = lst
346
347    return out
348
349# Count sources for a simplified (IR) instruction, including a source for a
350# staging register if necessary
351def src_count(op):
352    staging = 1 if (op["staging"] in ["r", "rw"]) else 0
353    return op["srcs"] + staging
354
355# Parses out the size part of an opocde name
356def typesize(opcode):
357    if opcode[-3:] == '128':
358        return 128
359    if opcode[-2:] == '48':
360        return 48
361    elif opcode[-1] == '8':
362        return 8
363    else:
364        try:
365            return int(opcode[-2:])
366        except:
367            return 32
368