scsi: move host_status handling into SCSI drivers
[qemu/ar7.git] / target / hexagon / hex_common.py
blobb3b534057d52851ead0c2ce3468df457348d0cc6
1 #!/usr/bin/env python3
3 ##
4 ## Copyright(c) 2019-2021 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.
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.
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/>.
20 import sys
21 import re
22 import string
24 behdict = {} # tag ->behavior
25 semdict = {} # tag -> semantics
26 attribdict = {} # tag -> attributes
27 macros = {} # macro -> macro information...
28 attribinfo = {} # Register information and misc
29 tags = [] # list of all tags
30 overrides = {} # tags with helper overrides
32 # We should do this as a hash for performance,
33 # but to keep order let's keep it as a list.
34 def 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)]
39 regre = re.compile(
40 r"((?<!DUP)[MNORCPQXSGVZA])([stuvwxyzdefg]+)([.]?[LlHh]?)(\d+S?)")
41 immre = re.compile(r"[#]([rRsSuUm])(\d+)(?:[:](\d+))?")
42 reg_or_immre = \
43 re.compile(r"(((?<!DUP)[MNRCOPQXSGVZA])([stuvwxyzdefg]+)" + \
44 "([.]?[LlHh]?)(\d+S?))|([#]([rRsSuUm])(\d+)[:]?(\d+)?)")
45 relimmre = re.compile(r"[#]([rR])(\d+)(?:[:](\d+))?")
46 absimmre = re.compile(r"[#]([sSuUm])(\d+)(?:[:](\d+))?")
48 finished_macros = set()
50 def 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
63 # When qemu needs an attribute that isn't in the imported files,
64 # we'll add it here.
65 def add_qemu_macro_attrib(name, attrib):
66 macros[name].attribs.add(attrib)
68 immextre = re.compile(r'f(MUST_)?IMMEXT[(]([UuSsRr])')
69 def 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')
77 # Recurse down macros, find attributes from sub-macros
78 macroValues = list(macros.values())
79 allmacros_restr = "|".join(set([ m.re.pattern for m in macroValues ]))
80 allmacros_re = re.compile(allmacros_restr)
81 for macro in macroValues:
82 expand_macro_attribs(macro,allmacros_re)
83 # Append attributes to all instructions
84 for tag in tags:
85 for macname in allmacros_re.findall(semdict[tag]):
86 if not macname: continue
87 macro = macros[macname]
88 attribdict[tag] |= set(macro.attribs)
89 # Figure out which instructions write predicate registers
90 tagregs = get_tagregs()
91 for tag in tags:
92 regs = tagregs[tag]
93 for regtype, regid, toss, numregs in regs:
94 if regtype == "P" and is_written(regid):
95 attribdict[tag].add('A_WRITES_PRED_REG')
97 def SEMANTICS(tag, beh, sem):
98 #print tag,beh,sem
99 behdict[tag] = beh
100 semdict[tag] = sem
101 attribdict[tag] = set()
102 tags.append(tag) # dicts have no order, this is for order
104 def ATTRIBUTES(tag, attribstring):
105 attribstring = \
106 attribstring.replace("ATTRIBS","").replace("(","").replace(")","")
107 if not attribstring:
108 return
109 attribs = attribstring.split(",")
110 for attrib in attribs:
111 attribdict[tag].add(attrib.strip())
113 class Macro(object):
114 __slots__ = ['key','name', 'beh', 'attribs', 're']
115 def __init__(self, name, beh, attribs):
116 self.key = name
117 self.name = name
118 self.beh = beh
119 self.attribs = set(attribs)
120 self.re = re.compile("\\b" + name + "\\b")
122 def MACROATTRIB(macname,beh,attribstring):
123 attribstring = attribstring.replace("(","").replace(")","")
124 if attribstring:
125 attribs = attribstring.split(",")
126 else:
127 attribs = []
128 macros[macname] = Macro(macname,beh,attribs)
130 def compute_tag_regs(tag):
131 return uniquify(regre.findall(behdict[tag]))
133 def compute_tag_immediates(tag):
134 return uniquify(immre.findall(behdict[tag]))
137 ## tagregs is the main data structure we'll use
138 ## tagregs[tag] will contain the registers used by an instruction
139 ## Within each entry, we'll use the regtype and regid fields
140 ## regtype can be one of the following
141 ## C control register
142 ## N new register value
143 ## P predicate register
144 ## R GPR register
145 ## M modifier register
146 ## regid can be one of the following
147 ## d, e destination register
148 ## dd destination register pair
149 ## s, t, u, v, w source register
150 ## ss, tt, uu, vv source register pair
151 ## x, y read-write register
152 ## xx, yy read-write register pair
154 def get_tagregs():
155 return dict(zip(tags, list(map(compute_tag_regs, tags))))
157 def get_tagimms():
158 return dict(zip(tags, list(map(compute_tag_immediates, tags))))
160 def is_pair(regid):
161 return len(regid) == 2
163 def is_single(regid):
164 return len(regid) == 1
166 def is_written(regid):
167 return regid[0] in "dexy"
169 def is_writeonly(regid):
170 return regid[0] in "de"
172 def is_read(regid):
173 return regid[0] in "stuvwxy"
175 def is_readwrite(regid):
176 return regid[0] in "xy"
178 def is_scalar_reg(regtype):
179 return regtype in "RPC"
181 def is_old_val(regtype, regid, tag):
182 return regtype+regid+'V' in semdict[tag]
184 def is_new_val(regtype, regid, tag):
185 return regtype+regid+'N' in semdict[tag]
187 def need_slot(tag):
188 if ('A_CONDEXEC' in attribdict[tag] or
189 'A_STORE' in attribdict[tag] or
190 'A_LOAD' in attribdict[tag]):
191 return 1
192 else:
193 return 0
195 def need_part1(tag):
196 return re.compile(r"fPART1").search(semdict[tag])
198 def need_ea(tag):
199 return re.compile(r"\bEA\b").search(semdict[tag])
201 def skip_qemu_helper(tag):
202 return tag in overrides.keys()
204 def imm_name(immlett):
205 return "%siV" % immlett
207 def read_semantics_file(name):
208 eval_line = ""
209 for line in open(name, 'rt').readlines():
210 if not line.startswith("#"):
211 eval_line += line
212 if line.endswith("\\\n"):
213 eval_line.rstrip("\\\n")
214 else:
215 eval(eval_line.strip())
216 eval_line = ""
218 def read_attribs_file(name):
219 attribre = re.compile(r'DEF_ATTRIB\(([A-Za-z0-9_]+), ([^,]*), ' +
220 r'"([A-Za-z0-9_\.]*)", "([A-Za-z0-9_\.]*)"\)')
221 for line in open(name, 'rt').readlines():
222 if not attribre.match(line):
223 continue
224 (attrib_base,descr,rreg,wreg) = attribre.findall(line)[0]
225 attrib_base = 'A_' + attrib_base
226 attribinfo[attrib_base] = {'rreg':rreg, 'wreg':wreg, 'descr':descr}
228 def read_overrides_file(name):
229 overridere = re.compile("#define fGEN_TCG_([A-Za-z0-9_]+)\(.*")
230 for line in open(name, 'rt').readlines():
231 if not overridere.match(line):
232 continue
233 tag = overridere.findall(line)[0]
234 overrides[tag] = True