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# Parse instruction set XML into a normalized form for processing 24 25import xml.etree.ElementTree as ET 26import copy 27 28def parse_cond(cond, aliased = False): 29 if cond.tag == 'reserved': 30 return None 31 32 if cond.attrib.get('alias', False) and not aliased: 33 return ['alias', parse_cond(cond, True)] 34 35 if 'left' in cond.attrib: 36 return [cond.tag, cond.attrib['left'], cond.attrib['right']] 37 else: 38 return [cond.tag] + [parse_cond(x) for x in cond.findall('*')] 39 40def parse_exact(obj): 41 return [int(obj.attrib['mask'], 0), int(obj.attrib['exact'], 0)] 42 43def parse_derived(obj): 44 out = [] 45 46 for deriv in obj.findall('derived'): 47 loc = [int(deriv.attrib['start']), int(deriv.attrib['size'])] 48 count = 1 << loc[1] 49 50 opts = [parse_cond(d) for d in deriv.findall('*')] 51 default = [None] * count 52 opts_fit = (opts + default)[0:count] 53 54 out.append([loc, opts_fit]) 55 56 return out 57 58def parse_modifiers(obj): 59 out = [] 60 61 for mod in obj.findall('mod'): 62 name = mod.attrib['name'] 63 start = mod.attrib.get('start', None) 64 size = int(mod.attrib['size']) 65 66 if start is not None: 67 start = int(start) 68 69 opts = [x.text if x.tag == 'opt' else x.tag for x in mod.findall('*')] 70 71 if len(opts) == 0: 72 assert('opt' in mod.attrib) 73 opts = ['none', mod.attrib['opt']] 74 75 # Find suitable default 76 default = mod.attrib.get('default', 'none' if 'none' in opts else None) 77 78 # Pad out as reserved 79 count = (1 << size) 80 opts = (opts + (['reserved'] * count))[0:count] 81 out.append([[name, start, size], default, opts]) 82 83 return out 84 85def parse_copy(enc, existing): 86 for node in enc.findall('copy'): 87 name = node.get('name') 88 for ex in existing: 89 if ex[0][0] == name: 90 ex[0][1] = node.get('start') 91 92def parse_instruction(ins): 93 common = { 94 'srcs': [], 95 'modifiers': [], 96 'immediates': [], 97 'swaps': [], 98 'derived': [], 99 'staging': ins.attrib.get('staging', '') 100 } 101 102 if 'exact' in ins.attrib: 103 common['exact'] = parse_exact(ins) 104 105 for src in ins.findall('src'): 106 mask = int(src.attrib['mask'], 0) if ('mask' in src.attrib) else 0xFF 107 common['srcs'].append([int(src.attrib['start'], 0), mask]) 108 109 for imm in ins.findall('immediate'): 110 common['immediates'].append([imm.attrib['name'], int(imm.attrib['start']), int(imm.attrib['size'])]) 111 112 common['derived'] = parse_derived(ins) 113 common['modifiers'] = parse_modifiers(ins) 114 115 for swap in ins.findall('swap'): 116 lr = [int(swap.get('left')), int(swap.get('right'))] 117 cond = parse_cond(swap.findall('*')[0]) 118 rewrites = {} 119 120 for rw in swap.findall('rewrite'): 121 mp = {} 122 123 for m in rw.findall('map'): 124 mp[m.attrib['from']] = m.attrib['to'] 125 126 rewrites[rw.attrib['name']] = mp 127 128 common['swaps'].append([lr, cond, rewrites]) 129 130 encodings = ins.findall('encoding') 131 variants = [] 132 133 if len(encodings) == 0: 134 variants = [[None, common]] 135 else: 136 for enc in encodings: 137 variant = copy.deepcopy(common) 138 assert(len(variant['derived']) == 0) 139 140 variant['exact'] = parse_exact(enc) 141 variant['derived'] = parse_derived(enc) 142 parse_copy(enc, variant['modifiers']) 143 144 cond = parse_cond(enc.findall('*')[0]) 145 variants.append([cond, variant]) 146 147 return variants 148 149def parse_instructions(xml): 150 final = {} 151 instructions = ET.parse(xml).getroot().findall('ins') 152 153 for ins in instructions: 154 final[ins.attrib['name']] = parse_instruction(ins) 155 156 return final 157 158# Expand out an opcode name to something C-escaped 159 160def opname_to_c(name): 161 return name.lower().replace('*', 'fma_').replace('+', 'add_').replace('.', '_') 162 163# Expand out distinct states to distrinct instructions, with a placeholder 164# condition for instructions with a single state 165 166def expand_states(instructions): 167 out = {} 168 169 for ins in instructions: 170 c = instructions[ins] 171 172 for ((test, desc), i) in zip(c, range(len(c))): 173 # Construct a name for the state 174 name = ins + (('.' + str(i)) if len(c) > 1 else '') 175 176 out[name] = (ins, test if test is not None else [], desc) 177 178 return out 179