1#!/usr/bin/env python3 2 3## 4## Copyright(c) 2019-2022 Qualcomm Innovation Center, Inc. All Rights Reserved. 5## 6## This program is free software; you can redistribute it and/or modify 7## it under the terms of the GNU General Public License as published by 8## the Free Software Foundation; either version 2 of the License, or 9## (at your option) any later version. 10## 11## This program is distributed in the hope that it will be useful, 12## but WITHOUT ANY WARRANTY; without even the implied warranty of 13## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14## GNU General Public License for more details. 15## 16## You should have received a copy of the GNU General Public License 17## along with this program; if not, see <http://www.gnu.org/licenses/>. 18## 19 20import sys 21import re 22import string 23 24behdict = {} # tag ->behavior 25semdict = {} # tag -> semantics 26attribdict = {} # tag -> attributes 27macros = {} # macro -> macro information... 28attribinfo = {} # Register information and misc 29tags = [] # list of all tags 30overrides = {} # tags with helper overrides 31 32# We should do this as a hash for performance, 33# but to keep order let's keep it as a list. 34def uniquify(seq): 35 seen = set() 36 seen_add = seen.add 37 return [x for x in seq if x not in seen and not seen_add(x)] 38 39regre = re.compile( 40 r"((?<!DUP)[MNORCPQXSGVZA])([stuvwxyzdefg]+)([.]?[LlHh]?)(\d+S?)") 41immre = re.compile(r"[#]([rRsSuUm])(\d+)(?:[:](\d+))?") 42reg_or_immre = \ 43 re.compile(r"(((?<!DUP)[MNRCOPQXSGVZA])([stuvwxyzdefg]+)" + \ 44 "([.]?[LlHh]?)(\d+S?))|([#]([rRsSuUm])(\d+)[:]?(\d+)?)") 45relimmre = re.compile(r"[#]([rR])(\d+)(?:[:](\d+))?") 46absimmre = re.compile(r"[#]([sSuUm])(\d+)(?:[:](\d+))?") 47 48finished_macros = set() 49 50def expand_macro_attribs(macro,allmac_re): 51 if macro.key not in finished_macros: 52 # Get a list of all things that might be macros 53 l = allmac_re.findall(macro.beh) 54 for submacro in l: 55 if not submacro: continue 56 if not macros[submacro]: 57 raise Exception("Couldn't find macro: <%s>" % l) 58 macro.attribs |= expand_macro_attribs( 59 macros[submacro], allmac_re) 60 finished_macros.add(macro.key) 61 return macro.attribs 62 63# When qemu needs an attribute that isn't in the imported files, 64# we'll add it here. 65def add_qemu_macro_attrib(name, attrib): 66 macros[name].attribs.add(attrib) 67 68immextre = re.compile(r'f(MUST_)?IMMEXT[(]([UuSsRr])') 69def calculate_attribs(): 70 add_qemu_macro_attrib('fREAD_PC', 'A_IMPLICIT_READS_PC') 71 add_qemu_macro_attrib('fTRAP', 'A_IMPLICIT_READS_PC') 72 add_qemu_macro_attrib('fWRITE_P0', 'A_WRITES_PRED_REG') 73 add_qemu_macro_attrib('fWRITE_P1', 'A_WRITES_PRED_REG') 74 add_qemu_macro_attrib('fWRITE_P2', 'A_WRITES_PRED_REG') 75 add_qemu_macro_attrib('fWRITE_P3', 'A_WRITES_PRED_REG') 76 add_qemu_macro_attrib('fSET_OVERFLOW', 'A_IMPLICIT_WRITES_USR') 77 add_qemu_macro_attrib('fSET_LPCFG', 'A_IMPLICIT_WRITES_USR') 78 add_qemu_macro_attrib('fSTORE', 'A_SCALAR_STORE') 79 80 # Recurse down macros, find attributes from sub-macros 81 macroValues = list(macros.values()) 82 allmacros_restr = "|".join(set([ m.re.pattern for m in macroValues ])) 83 allmacros_re = re.compile(allmacros_restr) 84 for macro in macroValues: 85 expand_macro_attribs(macro,allmacros_re) 86 # Append attributes to all instructions 87 for tag in tags: 88 for macname in allmacros_re.findall(semdict[tag]): 89 if not macname: continue 90 macro = macros[macname] 91 attribdict[tag] |= set(macro.attribs) 92 # Figure out which instructions write predicate registers 93 tagregs = get_tagregs() 94 for tag in tags: 95 regs = tagregs[tag] 96 for regtype, regid, toss, numregs in regs: 97 if regtype == "P" and is_written(regid): 98 attribdict[tag].add('A_WRITES_PRED_REG') 99 100def SEMANTICS(tag, beh, sem): 101 #print tag,beh,sem 102 behdict[tag] = beh 103 semdict[tag] = sem 104 attribdict[tag] = set() 105 tags.append(tag) # dicts have no order, this is for order 106 107def ATTRIBUTES(tag, attribstring): 108 attribstring = \ 109 attribstring.replace("ATTRIBS","").replace("(","").replace(")","") 110 if not attribstring: 111 return 112 attribs = attribstring.split(",") 113 for attrib in attribs: 114 attribdict[tag].add(attrib.strip()) 115 116class Macro(object): 117 __slots__ = ['key','name', 'beh', 'attribs', 're'] 118 def __init__(self, name, beh, attribs): 119 self.key = name 120 self.name = name 121 self.beh = beh 122 self.attribs = set(attribs) 123 self.re = re.compile("\\b" + name + "\\b") 124 125def MACROATTRIB(macname,beh,attribstring): 126 attribstring = attribstring.replace("(","").replace(")","") 127 if attribstring: 128 attribs = attribstring.split(",") 129 else: 130 attribs = [] 131 macros[macname] = Macro(macname,beh,attribs) 132 133def compute_tag_regs(tag): 134 return uniquify(regre.findall(behdict[tag])) 135 136def compute_tag_immediates(tag): 137 return uniquify(immre.findall(behdict[tag])) 138 139## 140## tagregs is the main data structure we'll use 141## tagregs[tag] will contain the registers used by an instruction 142## Within each entry, we'll use the regtype and regid fields 143## regtype can be one of the following 144## C control register 145## N new register value 146## P predicate register 147## R GPR register 148## M modifier register 149## Q HVX predicate vector 150## V HVX vector register 151## O HVX new vector register 152## regid can be one of the following 153## d, e destination register 154## dd destination register pair 155## s, t, u, v, w source register 156## ss, tt, uu, vv source register pair 157## x, y read-write register 158## xx, yy read-write register pair 159## 160def get_tagregs(): 161 return dict(zip(tags, list(map(compute_tag_regs, tags)))) 162 163def get_tagimms(): 164 return dict(zip(tags, list(map(compute_tag_immediates, tags)))) 165 166def is_pair(regid): 167 return len(regid) == 2 168 169def is_single(regid): 170 return len(regid) == 1 171 172def is_written(regid): 173 return regid[0] in "dexy" 174 175def is_writeonly(regid): 176 return regid[0] in "de" 177 178def is_read(regid): 179 return regid[0] in "stuvwxy" 180 181def is_readwrite(regid): 182 return regid[0] in "xy" 183 184def is_scalar_reg(regtype): 185 return regtype in "RPC" 186 187def is_hvx_reg(regtype): 188 return regtype in "VQ" 189 190def is_old_val(regtype, regid, tag): 191 return regtype+regid+'V' in semdict[tag] 192 193def is_new_val(regtype, regid, tag): 194 return regtype+regid+'N' in semdict[tag] 195 196def need_slot(tag): 197 if ('A_CONDEXEC' in attribdict[tag] or 198 'A_STORE' in attribdict[tag] or 199 'A_LOAD' in attribdict[tag]): 200 return 1 201 else: 202 return 0 203 204def need_part1(tag): 205 return re.compile(r"fPART1").search(semdict[tag]) 206 207def need_ea(tag): 208 return re.compile(r"\bEA\b").search(semdict[tag]) 209 210def skip_qemu_helper(tag): 211 return tag in overrides.keys() 212 213def is_tmp_result(tag): 214 return ('A_CVI_TMP' in attribdict[tag] or 215 'A_CVI_TMP_DST' in attribdict[tag]) 216 217def is_new_result(tag): 218 return ('A_CVI_NEW' in attribdict[tag]) 219 220def imm_name(immlett): 221 return "%siV" % immlett 222 223def read_semantics_file(name): 224 eval_line = "" 225 for line in open(name, 'rt').readlines(): 226 if not line.startswith("#"): 227 eval_line += line 228 if line.endswith("\\\n"): 229 eval_line.rstrip("\\\n") 230 else: 231 eval(eval_line.strip()) 232 eval_line = "" 233 234def read_attribs_file(name): 235 attribre = re.compile(r'DEF_ATTRIB\(([A-Za-z0-9_]+), ([^,]*), ' + 236 r'"([A-Za-z0-9_\.]*)", "([A-Za-z0-9_\.]*)"\)') 237 for line in open(name, 'rt').readlines(): 238 if not attribre.match(line): 239 continue 240 (attrib_base,descr,rreg,wreg) = attribre.findall(line)[0] 241 attrib_base = 'A_' + attrib_base 242 attribinfo[attrib_base] = {'rreg':rreg, 'wreg':wreg, 'descr':descr} 243 244def read_overrides_file(name): 245 overridere = re.compile("#define fGEN_TCG_([A-Za-z0-9_]+)\(.*") 246 for line in open(name, 'rt').readlines(): 247 if not overridere.match(line): 248 continue 249 tag = overridere.findall(line)[0] 250 overrides[tag] = True 251