(no commit message)
[libreriscv.git] / openpower / sv_analysis.py
1 #!/usr/bin/env python2
2 #
3 # NOTE that this program is python2 compatible, please do not stop it
4 # from working by adding syntax that prevents that.
5 #
6 # Initial version written by lkcl Oct 2020
7 # This program analyses the Power 9 op codes and looks at in/out register uses
8 # The results are displayed:
9 # https://libre-soc.org/openpower/opcode_regs_deduped/
10 #
11 # It finds .csv files in the directory isatables/
12 # then goes through the categories and creates svp64 CSV augmentation
13 # tables on a per-opcode basis
14
15 import csv
16 import os
17 from os.path import dirname, join
18 from glob import glob
19 from collections import OrderedDict
20
21 # Return absolute path (ie $PWD) + isatables + name
22 def find_wiki_file(name):
23 filedir = os.path.dirname(os.path.abspath(__file__))
24 tabledir = join(filedir, 'isatables')
25 file_path = join(tabledir, name)
26 return file_path
27
28 # Return an array of dictionaries from the CSV file name:
29 def get_csv(name):
30 file_path = find_wiki_file(name)
31 with open(file_path, 'r') as csvfile:
32 reader = csv.DictReader(csvfile)
33 return list(reader)
34
35 # Write an array of dictionaries to the CSV file name:
36 def write_csv(name, items, headers):
37 file_path = find_wiki_file(name)
38 with open(file_path, 'wb') as csvfile:
39 writer = csv.DictWriter(csvfile, headers, lineterminator="\n")
40 writer.writeheader()
41 writer.writerows(items)
42
43 # This will return True if all values are true.
44 # Not sure what this is about
45 def blank_key(row):
46 #for v in row.values():
47 # if 'SPR' in v: # skip all SPRs
48 # return True
49 for v in row.values():
50 if v:
51 return False
52 return True
53
54 # General purpose registers have names like: RA, RT, R1, ...
55 # Floating point registers names like: FRT, FRA, FR1, ..., FRTp, ...
56 # Return True if field is a register
57 def isreg(field):
58 return field.startswith('R') or field.startswith('FR')
59
60
61 # These are the attributes of the instructions,
62 # register names
63 keycolumns = ['unit', 'in1', 'in2', 'in3', 'out', 'CR in', 'CR out',
64 ] # don't think we need these: 'ldst len', 'rc', 'lk']
65
66 tablecols = ['unit', 'in', 'outcnt', 'CR in', 'CR out', 'imm'
67 ] # don't think we need these: 'ldst len', 'rc', 'lk']
68
69 def create_key(row):
70 res = OrderedDict()
71 #print ("row", row)
72 for key in keycolumns:
73 # registers IN - special-case: count number of regs RA/RB/RC/RS
74 if key in ['in1', 'in2', 'in3']:
75 if 'in' not in res:
76 res['in'] = 0
77 if isreg(row[key]):
78 res['in'] += 1
79
80 # registers OUT
81 if key == 'out':
82 # If upd is 1 then increment the count of outputs
83 if 'outcnt' not in res:
84 res['outcnt'] = 0
85 if isreg(row[key]):
86 res['outcnt'] += 1
87 if row['upd'] == '1':
88 res['outcnt'] += 1
89
90 # CRs (Condition Register) (CR0 .. CR7)
91 if key.startswith('CR'):
92 if row[key].startswith('NONE'):
93 res[key] = '0'
94 else:
95 res[key] = '1'
96 if row['comment'].startswith('cr'):
97 res['crop'] = '1'
98 # unit
99 if key == 'unit':
100 if row[key] == 'LDST': # we care about LDST units
101 res[key] = row[key]
102 else:
103 res[key] = 'OTHER'
104 # LDST len (LoadStore length)
105 if key.startswith('ldst'):
106 if row[key].startswith('NONE'):
107 res[key] = '0'
108 else:
109 res[key] = '1'
110 # rc, lk
111 if key in ['rc', 'lk']:
112 if row[key] == 'ONE':
113 res[key] = '1'
114 elif row[key] == 'NONE':
115 res[key] = '0'
116 else:
117 res[key] = 'R'
118 if key == 'lk':
119 res[key] = row[key]
120
121 # Convert the numerics 'in' & 'outcnt' to strings
122 res['in'] = str(res['in'])
123 res['outcnt'] = str(res['outcnt'])
124
125
126 # constants
127 if row['in2'].startswith('CONST_'):
128 res['imm'] = "1" # row['in2'].split("_")[1]
129 else:
130 res['imm'] = ''
131
132 return res
133
134 #
135 def dformat(d):
136 res = []
137 for k, v in d.items():
138 res.append("%s: %s" % (k, v))
139 return ' '.join(res)
140
141 def tformat(d):
142 return ' | '.join(d) + " |"
143
144 def keyname(row):
145 res = []
146 if row['unit'] != 'OTHER':
147 res.append(row['unit'])
148 if row['in'] != '0':
149 res.append('%sR' % row['in'])
150 if row['outcnt'] != '0':
151 res.append('%sW' % row['outcnt'])
152 if row['CR in'] == '1' and row['CR out'] == '1':
153 if 'crop' in row:
154 res.append("CR=2R1W")
155 else:
156 res.append("CRio")
157 elif row['CR in'] == '1':
158 res.append("CRi")
159 elif row['CR out'] == '1':
160 res.append("CRo")
161 elif 'imm' in row and row['imm']:
162 res.append("imm")
163 return '-'.join(res)
164
165
166 def process_csvs():
167 csvs = {}
168 bykey = {}
169 primarykeys = set()
170 dictkeys = OrderedDict()
171 immediates = {}
172 insns = {} # dictionary of CSV row, by instruction
173
174 print ("# OpenPOWER ISA register 'profile's")
175 print ('')
176 print ("this page is auto-generated, do not edit")
177 print ("created by http://libre-soc.org/openpower/sv_analysis.py")
178 print ('')
179
180 # Expand that (all .csv files)
181 pth = find_wiki_file("*.csv")
182
183 # Ignore those containing: valid test sprs
184 for fname in glob(pth):
185 if 'valid' in fname:
186 continue
187 if 'test' in fname:
188 continue
189 if 'sprs' in fname:
190 continue
191 if 'RM' in fname:
192 continue
193 #print (fname)
194 csvname = os.path.split(fname)[1]
195 # csvname is something like: minor_59.csv, fname the whole path
196 csv = get_csv(fname)
197 csvs[fname] = csv
198 for row in csv:
199 if blank_key(row):
200 continue
201 insn_name = row['comment']
202 # skip instructions that are not suitable
203 if insn_name in ['mcrxr', 'mcrxrx', 'darn']:
204 continue
205 insns[insn_name] = row # accumulate csv data by instruction
206 dkey = create_key(row)
207 key = tuple(dkey.values())
208 # print("key=", key)
209 dictkeys[key] = dkey
210 primarykeys.add(key)
211 if key not in bykey:
212 bykey[key] = []
213 bykey[key].append((csvname, row['opcode'], insn_name,
214 row['form'].upper() + '-Form'))
215
216 # detect immediates, collate them (useful info)
217 if row['in2'].startswith('CONST_'):
218 imm = row['in2'].split("_")[1]
219 if key not in immediates:
220 immediates[key] = set()
221 immediates[key].add(imm)
222
223 primarykeys = list(primarykeys)
224 primarykeys.sort()
225
226 # mapping to old SVPrefix "Forms"
227 mapsto = {'3R-1W-CRio': 'RM-1P-3S1D',
228 '2R-1W-CRio': 'RM-1P-2S1D',
229 '2R-1W-CRi': 'RM-1P-3S1D',
230 '2R-1W-CRo': 'RM-1P-2S1D',
231 '2R': 'non-SV',
232 '2R-1W': 'RM-1P-2S1D',
233 '1R-CRio': 'RM-2P-2S1D',
234 '2R-CRio': 'RM-1P-2S1D',
235 '2R-CRo': 'RM-1P-2S1D',
236 '1R': 'non-SV',
237 '1R-1W-CRio': 'RM-2P-1S1D',
238 '1R-1W-CRo': 'RM-2P-1S1D',
239 '1R-1W': 'RM-2P-1S1D',
240 '1R-1W-imm': 'RM-2P-1S1D',
241 '1R-CRo': 'RM-2P-1S1D',
242 '1R-imm': 'non-SV',
243 '1W': 'non-SV',
244 '1W-CRi': 'RM-2P-1S1D',
245 'CRio': 'RM-2P-1S1D',
246 'CR=2R1W': 'RM-1P-2S1D',
247 'CRi': 'non-SV',
248 'imm': 'non-SV',
249 '': 'non-SV',
250 'LDST-2R-imm': 'LDSTRM-2P-2S',
251 'LDST-2R-1W-imm': 'LDSTRM-2P-2S1D',
252 'LDST-2R-1W': 'LDSTRM-2P-2S1D',
253 'LDST-2R-2W': 'LDSTRM-2P-2S1D',
254 'LDST-1R-1W-imm': 'LDSTRM-2P-1S1D',
255 'LDST-1R-2W-imm': 'LDSTRM-2P-1S2D',
256 'LDST-3R': 'LDSTRM-2P-3S',
257 'LDST-3R-CRo': 'LDSTRM-2P-3S', # st*x
258 'LDST-3R-1W': 'LDSTRM-2P-2S1D', # st*x
259 }
260 print ("# map to old SV Prefix")
261 print ('')
262 print ('[[!table data="""')
263 for key in primarykeys:
264 name = keyname(dictkeys[key])
265 value = mapsto.get(name, "-")
266 print (tformat([name, value+ " "]))
267 print ('"""]]')
268 print ('')
269
270 print ("# keys")
271 print ('')
272 print ('[[!table data="""')
273 print (tformat(tablecols) + " imms | name |")
274
275 # print out the keys and the table from which they're derived
276 for key in primarykeys:
277 name = keyname(dictkeys[key])
278 row = tformat(dictkeys[key].values())
279 imms = list(immediates.get(key, ""))
280 imms.sort()
281 row += " %s | " % ("/".join(imms))
282 row += " %s |" % name
283 print (row)
284 print ('"""]]')
285 print ('')
286
287 # print out, by remap name, all the instructions under that category
288 for key in primarykeys:
289 name = keyname(dictkeys[key])
290 value = mapsto.get(name, "-")
291 print ("## %s (%s)" % (name, value))
292 print ('')
293 print ('[[!table data="""')
294 print (tformat(['CSV', 'opcode', 'asm', 'form']))
295 rows = bykey[key]
296 rows.sort()
297 for row in rows:
298 print (tformat(row))
299 print ('"""]]')
300 print ('')
301
302 #for fname, csv in csvs.items():
303 # print (fname)
304
305 #for insn, row in insns.items():
306 # print (insn, row)
307
308 print ("# svp64 remaps")
309 svp64 = OrderedDict()
310 # create a CSV file, per category, with SV "augmentation" info
311 csvcols = ['insn', 'Ptype', 'Etype', '0', '1', '2', '3']
312 csvcols += ['in1', 'in2', 'in3', 'out', 'CR in', 'CR out'] # temporary
313 for key in primarykeys:
314 # get the decoded key containing row-analysis, and name/value
315 dkey = dictkeys[key]
316 name = keyname(dkey)
317 value = mapsto.get(name, "-")
318 if value == 'non-SV':
319 continue
320
321 # store csv entries by svp64 RM category
322 if value not in svp64:
323 svp64[value] = []
324
325 # print out svp64 tables by category
326 print ("## %s (%s)" % (name, value))
327 print ('')
328 print ('[[!table format=csv file="openpower/isatables/%s.csv"]]' % \
329 value)
330 print ('')
331
332 rows = bykey[key]
333 rows.sort()
334
335 for row in rows:
336 #for idx in range(len(row)):
337 # if row[idx] == 'NONE':
338 # row[idx] = ''
339 # get the instruction
340 insn_name = row[2]
341 insn = insns[insn_name]
342 # start constructing svp64 CSV row
343 res = OrderedDict()
344 res['insn'] = insn_name
345 res['Ptype'] = value.split('-')[1] # predication type (RM-xN-xxx)
346 # get whether R_xxx_EXTRAn fields are 2-bit or 3-bit
347 res['Etype'] = 'EXTRA2'
348 # go through each register matching to Rxxxx_EXTRAx
349 for k in ['0', '1', '2', '3']:
350 res[k] = ''
351
352 # temporary useful info
353 regs = []
354 for k in ['in1', 'in2', 'in3', 'out', 'CR in', 'CR out']:
355 if insn[k].startswith('CONST'):
356 res[k] = ''
357 regs.append('')
358 else:
359 res[k] = insn[k]
360 if insn[k] == 'RA_OR_ZERO':
361 regs.append('RA')
362 elif insn[k] != 'NONE':
363 regs.append(insn[k])
364 else:
365 regs.append('')
366
367 # sigh now the fun begins. this isn't the sanest way to do it
368 # but the patterns are pretty regular.
369 if value == 'LDSTRM-2P-1S1D':
370 res['Etype'] = 'EXTRA3' # RM EXTRA3 type
371 res['0'] = 'd:RT' # RT: Rdest_EXTRA3
372 res['1'] = 's:RA' # RA: Rsrc1_EXTRA3
373
374 elif value == 'LDSTRM-2P-1S2D':
375 res['Etype'] = 'EXTRA2' # RM EXTRA2 type
376 res['0'] = 'd:RT' # RT: Rdest1_EXTRA2
377 res['1'] = 'd:RA' # RA: Rdest2_EXTRA2
378 res['2'] = 's:RA' # RA: Rsrc1_EXTRA2
379
380 elif value == 'LDSTRM-2P-2S':
381 res['Etype'] = 'EXTRA3' # RM EXTRA2 type
382 res['0'] = 'd:RS' # RT: Rdest1_EXTRA2
383 res['1'] = 's:RA' # RA: Rsrc1_EXTRA2
384
385 elif value == 'LDSTRM-2P-2S1D':
386 if 'st' in insn_name and 'x' not in insn_name: # stwu/stbu etc
387 res['Etype'] = 'EXTRA2' # RM EXTRA2 type
388 res['0'] = 'd:RA' # RA: Rdest1_EXTRA2
389 res['1'] = 's:RS' # RS: Rdsrc1_EXTRA2
390 res['2'] = 's:RA' # RA: Rsrc2_EXTRA2
391 elif 'st' in insn_name and 'x' in insn_name: # stwux
392 res['Etype'] = 'EXTRA2' # RM EXTRA2 type
393 res['0'] = 'd:RA' # RA: Rdest1_EXTRA2
394 res['1'] = 's:RS,s:RA' # RS: Rdest2_EXTRA2, RA: Rsrc1_EXTRA2
395 res['2'] = 's:RB' # RB: Rsrc2_EXTRA2
396 elif 'u' in insn_name: # ldux etc.
397 res['Etype'] = 'EXTRA2' # RM EXTRA2 type
398 res['0'] = 'd:RT' # RT: Rdest1_EXTRA2
399 res['1'] = 'd:RA' # RA: Rdest2_EXTRA2
400 res['2'] = 's:RB' # RB: Rsrc1_EXTRA2
401 else:
402 res['Etype'] = 'EXTRA2' # RM EXTRA2 type
403 res['0'] = 'd:RT' # RT: Rdest1_EXTRA2
404 res['1'] = 's:RA' # RA: Rsrc1_EXTRA2
405 res['2'] = 's:RB' # RB: Rsrc2_EXTRA2
406
407 elif value == 'LDSTRM-2P-3S':
408 res['Etype'] = 'EXTRA2' # RM EXTRA2 type
409 if 'cx' in insn_name:
410 res['0'] = 's:RS,d:CR0' # RS: Rsrc1_EXTRA2 CR0: dest
411 else:
412 res['0'] = 's:RS' # RS: Rsrc1_EXTRA2
413 res['1'] = 's:RA' # RA: Rsrc2_EXTRA2
414 res['2'] = 's:RB' # RA: Rsrc3_EXTRA2
415
416 elif value == 'RM-2P-1S1D':
417 res['Etype'] = 'EXTRA3' # RM EXTRA3 type
418 if name == 'CRio' and insn_name == 'mcrf':
419 res['0'] = 'd:BF' # BFA: Rdest1_EXTRA3
420 res['1'] = 's:BFA' # BFA: Rsrc1_EXTRA3
421 elif 'mfcr' in insn_name or 'mfocrf' in insn_name:
422 res['0'] = 'd:RT' # RT: Rdest1_EXTRA3
423 res['1'] = 's:CR' # CR: Rsrc1_EXTRA3
424 elif insn_name == 'setb':
425 res['0'] = 'd:RT' # RT: Rdest1_EXTRA3
426 res['1'] = 's:BFA' # BFA: Rsrc1_EXTRA3
427 elif insn_name.startswith('cmp'): # cmpi
428 res['0'] = 'd:BF' # BF: Rdest1_EXTRA3
429 res['1'] = 's:RA' # RA: Rsrc1_EXTRA3
430 elif regs == ['RA','','','RT','','']:
431 res['0'] = 'd:RT' # RT: Rdest1_EXTRA3
432 res['1'] = 's:RA' # RA: Rsrc1_EXTRA3
433 elif regs == ['RA','','','RT','','CR0']:
434 res['0'] = 'd:RT,d:CR0' # RT,CR0: Rdest1_EXTRA3
435 res['1'] = 's:RA' # RA: Rsrc1_EXTRA3
436 elif (regs == ['RS','','','RA','','CR0'] or
437 regs == ['','','RS','RA','','CR0']):
438 res['0'] = 'd:RA,d:CR0' # RA,CR0: Rdest1_EXTRA3
439 res['1'] = 's:RS' # RS: Rsrc1_EXTRA3
440 elif regs == ['RS','','','RA','','']:
441 res['0'] = 'd:RA' # RA: Rdest1_EXTRA3
442 res['1'] = 's:RS' # RS: Rsrc1_EXTRA3
443 else:
444 res['0'] = 'TODO'
445
446 elif value == 'RM-1P-2S1D':
447 res['Etype'] = 'EXTRA3' # RM EXTRA3 type
448 if insn_name.startswith('cr'):
449 res['0'] = 'd:BT' # BT: Rdest1_EXTRA3
450 res['1'] = 's:BA' # BA: Rsrc1_EXTRA3
451 res['2'] = 's:BB' # BB: Rsrc2_EXTRA3
452 elif name == '2R-1W' or insn_name == 'cmpb': # cmpb
453 if insn_name in ['bpermd', 'cmpb']:
454 res['0'] = 'd:RA' # RA: Rdest1_EXTRA3
455 res['1'] = 's:RS' # RS: Rsrc1_EXTRA3
456 else:
457 res['0'] = 'd:RT' # RT: Rdest1_EXTRA3
458 res['1'] = 's:RA' # RA: Rsrc1_EXTRA3
459 res['2'] = 's:RB' # RB: Rsrc1_EXTRA3
460 elif insn_name.startswith('cmp'): # cmp
461 res['0'] = 'd:BF' # BF: Rdest1_EXTRA3
462 res['1'] = 's:RA' # RA: Rsrc1_EXTRA3
463 res['2'] = 's:RB' # RB: Rsrc1_EXTRA3
464 elif (regs == ['','RB','RS','RA','','CR0'] or
465 regs == ['RS','RB','','RA','','CR0']):
466 res['0'] = 'd:RA,d:CR0' # RA,CR0: Rdest1_EXTRA3
467 res['1'] = 's:RB' # RB: Rsrc1_EXTRA3
468 res['2'] = 's:RS' # RS: Rsrc1_EXTRA3
469 elif regs == ['RA','RB','','RT','','CR0']:
470 res['0'] = 'd:RT,d:CR0' # RT,CR0: Rdest1_EXTRA3
471 res['1'] = 's:RA' # RA: Rsrc1_EXTRA3
472 res['2'] = 's:RB' # RB: Rsrc1_EXTRA3
473 elif regs == ['RA','','RS','RA','','CR0']:
474 res['0'] = 'd:RA,d:CR0' # RA,CR0: Rdest1_EXTRA3
475 res['1'] = 's:RA' # RA: Rsrc1_EXTRA3
476 res['2'] = 's:RS' # RS: Rsrc1_EXTRA3
477 else:
478 res['0'] = 'TODO'
479
480 elif value == 'RM-2P-2S1D':
481 res['Etype'] = 'EXTRA2' # RM EXTRA2 type
482 if insn_name.startswith('mt'): # mtcrf
483 res['0'] = 'd:CR' # CR: Rdest1_EXTRA2
484 res['1'] = 's:RS' # RS: Rsrc1_EXTRA2
485 res['2'] = 's:CR' # CR: Rsrc2_EXTRA2
486 else:
487 res['0'] = 'TODO'
488
489 elif value == 'RM-1P-3S1D':
490 res['Etype'] = 'EXTRA2' # RM EXTRA2 type
491 if insn_name == 'isel':
492 res['0'] = 'd:RT' # RT: Rdest1_EXTRA2
493 res['1'] = 's:RA' # RA: Rsrc1_EXTRA2
494 res['2'] = 's:RB' # RT: Rsrc2_EXTRA2
495 res['3'] = 's:BC' # BC: Rsrc3_EXTRA2
496 else:
497 res['0'] = 'd:FRT,d:CR1' # FRT, CR1: Rdest1_EXTRA2
498 res['1'] = 's:FRA' # FRA: Rsrc1_EXTRA2
499 res['2'] = 's:FRB' # FRB: Rsrc2_EXTRA2
500 res['3'] = 's:FRC' # FRC: Rsrc3_EXTRA2
501
502 # add to svp64 csvs
503 #for k in ['in1', 'in2', 'in3', 'out', 'CR in', 'CR out']:
504 # del res[k]
505 #if res['0'] != 'TODO':
506 svp64[value].append(res)
507
508 # now write out the csv files
509 for value, csv in svp64.items():
510 #csvcols = ['insn', 'Ptype', 'Etype', '0', '1', '2', '3']
511 write_csv("%s.csv" % value, csv, csvcols)
512
513 if __name__ == '__main__':
514 process_csvs()