3 # NOTE that this program is python2 compatible, please do not stop it
4 # from working by adding syntax that prevents that.
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/
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
17 from os
.path
import dirname
, join
19 from collections
import OrderedDict
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
)
28 # Return an array of dictionaries from the CSV file name:
30 file_path
= find_wiki_file(name
)
31 with
open(file_path
, 'r') as csvfile
:
32 reader
= csv
.DictReader(csvfile
)
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
, 'w') as csvfile
:
39 writer
= csv
.DictWriter(csvfile
, fieldnames
)
41 writer
.writerows(items
)
43 # This will return True if all values are true.
44 # Not sure what this is about
46 #for v in row.values():
47 # if 'SPR' in v: # skip all SPRs
49 for v
in row
.values():
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
58 return field
.startswith('R') or field
.startswith('FR')
61 # These are the attributes of the instructions,
63 keycolumns
= ['unit', 'in1', 'in2', 'in3', 'out', 'CR in', 'CR out',
64 ] # don't think we need these: 'ldst len', 'rc', 'lk']
66 tablecols
= ['unit', 'in', 'outcnt', 'CR in', 'CR out', 'imm'
67 ] # don't think we need these: 'ldst len', 'rc', 'lk']
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']:
82 # If upd is 1 then increment the count of outputs
83 if 'outcnt' not in res
:
90 # CRs (Condition Register) (CR0 .. CR7)
91 if key
.startswith('CR'):
92 if row
[key
].startswith('NONE'):
96 if row
['comment'].startswith('cr'):
100 if row
[key
] == 'LDST': # we care about LDST units
104 # LDST len (LoadStore length)
105 if key
.startswith('ldst'):
106 if row
[key
].startswith('NONE'):
111 if key
in ['rc', 'lk']:
112 if row
[key
] == 'ONE':
114 elif row
[key
] == 'NONE':
121 # Convert the numerics 'in' & 'outcnt' to strings
122 res
['in'] = str(res
['in'])
123 res
['outcnt'] = str(res
['outcnt'])
127 if row
['in2'].startswith('CONST_'):
128 res
['imm'] = "1" # row['in2'].split("_")[1]
137 for k
, v
in d
.items():
138 res
.append("%s: %s" % (k
, v
))
142 return ' | '.join(d
) + " |"
146 if row
['unit'] != 'OTHER':
147 res
.append(row
['unit'])
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':
154 res
.append("CR=2R1W")
157 elif row
['CR in'] == '1':
159 elif row
['CR out'] == '1':
161 elif 'imm' in row
and row
['imm']:
170 dictkeys
= OrderedDict()
172 insns
= {} # dictionary of CSV row, by instruction
174 print ("# OpenPOWER ISA register 'profile's")
176 print ("this page is auto-generated, do not edit")
177 print ("created by http://libre-soc.org/openpower/sv_analysis.py")
180 # Expand that (all .csv files)
181 pth
= find_wiki_file("*.csv")
183 # Ignore those containing: valid test sprs
184 for fname
in glob(pth
):
193 csvname
= os
.path
.split(fname
)[1]
194 # csvname is something like: minor_59.csv, fname the whole path
200 insn_name
= row
['comment']
201 # skip instructions that are not suitable
202 if insn_name
in ['mcrxr', 'mcrxrx', 'darn']:
204 insns
[insn_name
] = row
# accumulate csv data by instruction
205 dkey
= create_key(row
)
206 key
= tuple(dkey
.values())
212 bykey
[key
].append((csvname
, row
['opcode'], insn_name
,
213 row
['form'].upper() + '-Form'))
215 # detect immediates, collate them (useful info)
216 if row
['in2'].startswith('CONST_'):
217 imm
= row
['in2'].split("_")[1]
218 if key
not in immediates
:
219 immediates
[key
] = set()
220 immediates
[key
].add(imm
)
222 primarykeys
= list(primarykeys
)
225 # mapping to old SVPrefix "Forms"
226 mapsto
= {'3R-1W-CRio': 'RM-1P-3S1D',
227 '2R-1W-CRio': 'RM-1P-2S1D',
228 '2R-1W-CRi': 'RM-1P-3S1D',
229 '2R-1W-CRo': 'RM-1P-2S1D',
231 '2R-1W': 'RM-1P-2S1D',
232 '1R-CRio': 'RM-2P-2S1D',
233 '2R-CRio': 'RM-1P-2S1D',
234 '2R-CRo': 'RM-1P-2S1D',
236 '1R-1W-CRio': 'RM-2P-1S1D',
237 '1R-1W-CRo': 'RM-2P-1S1D',
238 '1R-1W': 'RM-2P-1S1D',
239 '1R-1W-imm': 'RM-2P-1S1D',
240 '1R-CRo': 'RM-2P-1S1D',
243 '1W-CRi': 'RM-2P-1S1D',
244 'CRio': 'RM-2P-1S1D',
245 'CR=2R1W': 'RM-1P-2S1D',
249 'LDST-2R-imm': 'LDSTRM-2P-2S',
250 'LDST-2R-1W-imm': 'LDSTRM-2P-2S1D',
251 'LDST-2R-1W': 'LDSTRM-2P-2S1D',
252 'LDST-2R-2W': 'LDSTRM-2P-2S1D',
253 'LDST-1R-1W-imm': 'LDSTRM-2P-1S1D',
254 'LDST-1R-2W-imm': 'LDSTRM-2P-1S2D',
255 'LDST-3R': 'LDSTRM-2P-3S',
256 'LDST-3R-CRo': 'LDSTRM-2P-3S', # st*x
257 'LDST-3R-1W': 'LDSTRM-2P-2S1D', # st*x
259 print ("# map to old SV Prefix")
261 print ('[[!table data="""')
262 for key
in primarykeys
:
263 name
= keyname(dictkeys
[key
])
264 value
= mapsto
.get(name
, "-")
265 print (tformat([name
, value
+ " "]))
271 print ('[[!table data="""')
272 print (tformat(tablecols
) + " imms | name |")
274 # print out the keys and the table from which they're derived
275 for key
in primarykeys
:
276 name
= keyname(dictkeys
[key
])
277 row
= tformat(dictkeys
[key
].values())
278 imms
= list(immediates
.get(key
, ""))
280 row
+= " %s | " % ("/".join(imms
))
281 row
+= " %s |" % name
286 # print out, by remap name, all the instructions under that category
287 for key
in primarykeys
:
288 name
= keyname(dictkeys
[key
])
289 value
= mapsto
.get(name
, "-")
290 print ("## %s (%s)" % (name
, value
))
292 print ('[[!table data="""')
293 print (tformat(['CSV', 'opcode', 'asm', 'form']))
301 #for fname, csv in csvs.items():
304 #for insn, row in insns.items():
307 print ("# svp64 remaps")
308 svp64
= OrderedDict()
309 # create a CSV file, per category, with SV "augmentation" info
310 csvcols
= ['insn', 'Ptype', 'Etype', '0', '1', '2', '3']
311 for key
in primarykeys
:
312 # get the decoded key containing row-analysis, and name/value
315 value
= mapsto
.get(name
, "-")
316 if value
== 'non-SV':
319 # store csv entries by svp64 RM category
320 if value
not in svp64
:
323 # print out svp64 tables by category
324 print ("## %s (%s)" % (name
, value
))
326 print ('[[!table data="""')
327 print (tformat(csvcols
))
332 #for idx in range(len(row)):
333 # if row[idx] == 'NONE':
335 # get the instruction
337 insn
= insns
[insn_name
]
338 # start constructing svp64 CSV row
340 res
['insn'] = insn_name
341 res
['Ptype'] = value
.split('-')[1] # predication type (RM-xN-xxx)
342 # get whether R_xxx_EXTRAn fields are 2-bit or 3-bit
343 res
['Etype'] = 'EXTRA2'
344 # go through each register matching to Rxxxx_EXTRAx
345 for k
in ['0', '1', '2', '3']:
348 # temporary useful info
350 for k
in ['in1', 'in2', 'in3', 'out', 'CR in', 'CR out']:
351 if insn
[k
].startswith('CONST'):
356 if insn
[k
] == 'RA_OR_ZERO':
358 elif insn
[k
] != 'NONE':
364 # sigh now the fun begins. this isn't the sanest way to do it
365 # but the patterns are pretty regular.
366 if value
== 'LDSTRM-2P-1S1D':
367 res
['Etype'] = 'EXTRA3' # RM EXTRA3 type
368 res
['0'] = 'd:RT' # RT: Rdest_EXTRA3
369 res
['1'] = 's:RA' # RA: Rsrc1_EXTRA3
371 elif value
== 'LDSTRM-2P-1S2D':
372 res
['Etype'] = 'EXTRA2' # RM EXTRA2 type
373 res
['0'] = 'd:RT' # RT: Rdest1_EXTRA2
374 res
['1'] = 's:RA' # RA: Rsrc1_EXTRA2
375 res
['2'] = 'd:RA' # RA: Rdest2_EXTRA2
377 elif value
== 'LDSTRM-2P-2S':
378 res
['Etype'] = 'EXTRA3' # RM EXTRA2 type
379 res
['0'] = 'd:RS' # RT: Rdest1_EXTRA2
380 res
['1'] = 's:RA' # RA: Rsrc1_EXTRA2
382 elif value
== 'LDSTRM-2P-2S1D':
383 if 'st' in insn_name
and 'x' not in insn_name
: # stwu/stbu etc
384 res
['Etype'] = 'EXTRA2' # RM EXTRA2 type
385 res
['0'] = 'd:RS' # RS: Rdest1_EXTRA2
386 res
['1'] = 'd:RA' # RA: Rdest2_EXTRA2
387 res
['2'] = 's:RA' # RA: Rsrc1_EXTRA2
388 if 'st' in insn_name
and 'x' in insn_name
: # stwux
389 res
['Etype'] = 'EXTRA2' # RM EXTRA2 type
390 res
['0'] = 'd:RS' # RS: Rdest1_EXTRA2
391 res
['1'] = 'd:RA' # RA: Rdest2_EXTRA2, RA: Rsrc1_EXTRA2
392 res
['2'] = 's:RB' # RB: Rsrc2_EXTRA2
393 elif 'u' in insn_name
: # ldux etc.
394 res
['Etype'] = 'EXTRA2' # RM EXTRA2 type
395 res
['0'] = 'd:RT' # RT: Rdest1_EXTRA2
396 res
['1'] = 'd:RA' # RA: Rdest2_EXTRA2
397 res
['2'] = 's:RB' # RB: Rsrc1_EXTRA2
399 res
['Etype'] = 'EXTRA2' # RM EXTRA2 type
400 res
['0'] = 'd:RT' # RT: Rdest1_EXTRA2
401 res
['1'] = 's:RA' # RA: Rsrc1_EXTRA2
402 res
['2'] = 's:RB' # RB: Rsrc2_EXTRA2
404 elif value
== 'LDSTRM-2P-3S':
405 res
['Etype'] = 'EXTRA2' # RM EXTRA2 type
406 res
['0'] = 's:RS,d:CR0' # RS: Rsrc1_EXTRA2 CR0: dest
407 res
['1'] = 's:RA' # RA: Rsrc2_EXTRA2
408 res
['2'] = 's:RB' # RA: Rsrc3_EXTRA2
410 elif value
== 'RM-2P-1S1D':
411 res
['Etype'] = 'EXTRA3' # RM EXTRA3 type
412 if name
== 'CRio' and insn_name
== 'mcrf':
413 res
['0'] = 'd:BF' # BFA: Rdest1_EXTRA3
414 res
['1'] = 's:BFA' # BFA: Rsrc1_EXTRA3
415 elif 'mfcr' in insn_name
or 'mfocrf' in insn_name
:
416 res
['0'] = 'd:RT' # RT: Rdest1_EXTRA3
417 res
['1'] = 's:CR' # CR: Rsrc1_EXTRA3
418 elif insn_name
== 'setb':
419 res
['0'] = 'd:RT' # RT: Rdest1_EXTRA3
420 res
['1'] = 's:BFA' # BFA: Rsrc1_EXTRA3
421 elif insn_name
.startswith('cmp'): # cmpi
422 res
['0'] = 'd:BF' # BF: Rdest1_EXTRA3
423 res
['1'] = 's:RA' # RA: Rsrc1_EXTRA3
424 elif regs
== ['RA','','','RT','','']:
425 res
['0'] = 'd:RT' # RT: Rdest1_EXTRA3
426 res
['1'] = 's:RA' # RA: Rsrc1_EXTRA3
427 elif regs
== ['RA','','','RT','','CR0']:
428 res
['0'] = 'd:RT,d:CR0' # RT,CR0: Rdest1_EXTRA3
429 res
['1'] = 's:RA' # RA: Rsrc1_EXTRA3
430 elif (regs
== ['RS','','','RA','','CR0'] or
431 regs
== ['','','RS','RA','','CR0']):
432 res
['0'] = 'd:RA,d:CR0' # RA,CR0: Rdest1_EXTRA3
433 res
['1'] = 's:RS' # RS: Rsrc1_EXTRA3
434 elif regs
== ['RS','','','RA','','']:
435 res
['0'] = 'd:RA' # RA: Rdest1_EXTRA3
436 res
['1'] = 's:RS' # RS: Rsrc1_EXTRA3
440 elif value
== 'RM-1P-2S1D':
441 res
['Etype'] = 'EXTRA3' # RM EXTRA3 type
442 if insn_name
.startswith('cr'):
443 res
['0'] = 'd:BT' # BT: Rdest1_EXTRA3
444 res
['1'] = 's:BA' # BA: Rsrc1_EXTRA3
445 res
['2'] = 's:BB' # BB: Rsrc2_EXTRA3
446 elif name
== '2R-1W' or insn_name
== 'cmpb': # cmpb
447 if insn_name
in ['bpermd', 'cmpb']:
448 res
['0'] = 'd:RA' # RA: Rdest1_EXTRA3
449 res
['1'] = 's:RS' # RS: Rsrc1_EXTRA3
451 res
['0'] = 'd:RT' # RT: Rdest1_EXTRA3
452 res
['1'] = 's:RA' # RA: Rsrc1_EXTRA3
453 res
['2'] = 's:RB' # RB: Rsrc1_EXTRA3
454 elif insn_name
.startswith('cmp'): # cmp
455 res
['0'] = 'd:BF' # BF: Rdest1_EXTRA3
456 res
['1'] = 's:RA' # RA: Rsrc1_EXTRA3
457 res
['2'] = 's:RB' # RB: Rsrc1_EXTRA3
458 elif (regs
== ['','RB','RS','RA','','CR0'] or
459 regs
== ['RS','RB','','RA','','CR0']):
460 res
['0'] = 'd:RA,d:CR0' # RA,CR0: Rdest1_EXTRA3
461 res
['1'] = 's:RB' # RB: Rsrc1_EXTRA3
462 res
['2'] = 's:RS' # RS: Rsrc1_EXTRA3
463 elif regs
== ['RA','RB','','RT','','CR0']:
464 res
['0'] = 'd:RT,d:CR0' # RT,CR0: Rdest1_EXTRA3
465 res
['1'] = 's:RA' # RA: Rsrc1_EXTRA3
466 res
['2'] = 's:RB' # RB: Rsrc1_EXTRA3
467 elif regs
== ['RA','','RS','RA','','CR0']:
468 res
['0'] = 'd:RA,d:CR0' # RA,CR0: Rdest1_EXTRA3
469 res
['1'] = 's:RA' # RA: Rsrc1_EXTRA3
470 res
['2'] = 's:RS' # RS: Rsrc1_EXTRA3
474 elif value
== 'RM-2P-2S1D':
475 res
['Etype'] = 'EXTRA2' # RM EXTRA2 type
476 if insn_name
.startswith('mt'): # mtcrf
477 res
['0'] = 'd:CR' # CR: Rdest1_EXTRA2
478 res
['1'] = 's:RS' # RS: Rsrc1_EXTRA2
479 res
['2'] = 's:CR' # CR: Rsrc2_EXTRA2
483 elif value
== 'RM-1P-3S1D':
484 res
['Etype'] = 'EXTRA2' # RM EXTRA2 type
485 if insn_name
== 'isel':
486 res
['0'] = 'd:RT' # RT: Rdest1_EXTRA2
487 res
['1'] = 's:RA' # RA: Rsrc1_EXTRA2
488 res
['2'] = 's:RB' # RT: Rsrc2_EXTRA2
489 res
['3'] = 's:BC' # BC: Rsrc3_EXTRA2
491 res
['0'] = 'd:FRT,d:CR1' # FRT, CR1: Rdest1_EXTRA2
492 res
['1'] = 's:FRA' # FRA: Rsrc1_EXTRA2
493 res
['2'] = 's:FRB' # FRB: Rsrc2_EXTRA2
494 res
['3'] = 's:FRC' # FRC: Rsrc3_EXTRA2
498 print (tformat(res
.values()))
500 svp64
[value
].append(res
)
505 if __name__
== '__main__':