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
20 from soc
.decoder
.power_svp64
import SVP64RM
23 # Return absolute path (ie $PWD) + isatables + name
24 def find_wiki_file(name
):
25 filedir
= os
.path
.dirname(os
.path
.abspath(__file__
))
26 tabledir
= join(filedir
, 'isatables')
27 file_path
= join(tabledir
, name
)
30 # Return an array of dictionaries from the CSV file name:
32 file_path
= find_wiki_file(name
)
33 with
open(file_path
, 'r') as csvfile
:
34 reader
= csv
.DictReader(csvfile
)
37 # Write an array of dictionaries to the CSV file name:
38 def write_csv(name
, items
, headers
):
39 file_path
= find_wiki_file(name
)
40 with
open(file_path
, 'w') as csvfile
:
41 writer
= csv
.DictWriter(csvfile
, headers
, lineterminator
="\n")
43 writer
.writerows(items
)
45 # This will return True if all values are true.
46 # Not sure what this is about
48 #for v in row.values():
49 # if 'SPR' in v: # skip all SPRs
51 for v
in row
.values():
56 # General purpose registers have names like: RA, RT, R1, ...
57 # Floating point registers names like: FRT, FRA, FR1, ..., FRTp, ...
58 # Return True if field is a register
60 return (field
.startswith('R') or field
.startswith('FR') or
64 # These are the attributes of the instructions,
66 keycolumns
= ['unit', 'in1', 'in2', 'in3', 'out', 'CR in', 'CR out',
67 ] # don't think we need these: 'ldst len', 'rc', 'lk']
69 tablecols
= ['unit', 'in', 'outcnt', 'CR in', 'CR out', 'imm'
70 ] # don't think we need these: 'ldst len', 'rc', 'lk']
75 for key
in keycolumns
:
76 # registers IN - special-case: count number of regs RA/RB/RC/RS
77 if key
in ['in1', 'in2', 'in3']:
85 # If upd is 1 then increment the count of outputs
86 if 'outcnt' not in res
:
93 # CRs (Condition Register) (CR0 .. CR7)
94 if key
.startswith('CR'):
95 if row
[key
].startswith('NONE'):
99 if row
['comment'].startswith('cr'):
103 if row
[key
] == 'LDST': # we care about LDST units
107 # LDST len (LoadStore length)
108 if key
.startswith('ldst'):
109 if row
[key
].startswith('NONE'):
114 if key
in ['rc', 'lk']:
115 if row
[key
] == 'ONE':
117 elif row
[key
] == 'NONE':
124 # Convert the numerics 'in' & 'outcnt' to strings
125 res
['in'] = str(res
['in'])
126 res
['outcnt'] = str(res
['outcnt'])
130 if row
['in2'].startswith('CONST_'):
131 res
['imm'] = "1" # row['in2'].split("_")[1]
140 for k
, v
in d
.items():
141 res
.append("%s: %s" % (k
, v
))
145 return ' | '.join(d
) + " |"
149 if row
['unit'] != 'OTHER':
150 res
.append(row
['unit'])
152 res
.append('%sR' % row
['in'])
153 if row
['outcnt'] != '0':
154 res
.append('%sW' % row
['outcnt'])
155 if row
['CR in'] == '1' and row
['CR out'] == '1':
157 res
.append("CR=2R1W")
160 elif row
['CR in'] == '1':
162 elif row
['CR out'] == '1':
164 elif 'imm' in row
and row
['imm']:
174 dictkeys
= OrderedDict()
176 insns
= {} # dictionary of CSV row, by instruction
179 print ("# OpenPOWER ISA register 'profile's")
181 print ("this page is auto-generated, do not edit")
182 print ("created by http://libre-soc.org/openpower/sv_analysis.py")
185 # Expand that (all .csv files)
186 pth
= find_wiki_file("*.csv")
188 # Ignore those containing: valid test sprs
189 for fname
in glob(pth
):
196 if fname
.endswith('sprs.csv'):
198 if fname
.endswith('minor_19_valid.csv'):
203 csvname
= os
.path
.split(fname
)[1]
204 csvname_
= csvname
.split(".")[0]
205 # csvname is something like: minor_59.csv, fname the whole path
208 csvs_svp64
[csvname_
] = []
212 insn_name
= row
['comment']
213 # skip instructions that are not suitable
214 if insn_name
in ['mcrxr', 'mcrxrx', 'darn']:
216 if insn_name
.startswith('bc') or 'rfid' in insn_name
:
218 insns
[insn_name
] = row
# accumulate csv data by instruction
219 insn_to_csv
[insn_name
] = csvname_
# CSV file name by instruction
220 dkey
= create_key(row
)
221 key
= tuple(dkey
.values())
227 bykey
[key
].append((csvname
, row
['opcode'], insn_name
,
228 row
['form'].upper() + '-Form'))
230 # detect immediates, collate them (useful info)
231 if row
['in2'].startswith('CONST_'):
232 imm
= row
['in2'].split("_")[1]
233 if key
not in immediates
:
234 immediates
[key
] = set()
235 immediates
[key
].add(imm
)
237 primarykeys
= list(primarykeys
)
240 # mapping to old SVPrefix "Forms"
241 mapsto
= {'3R-1W-CRio': 'RM-1P-3S1D',
242 '2R-1W-CRio': 'RM-1P-2S1D',
243 '2R-1W-CRi': 'RM-1P-3S1D',
244 '2R-1W-CRo': 'RM-1P-2S1D',
246 '2R-1W': 'RM-1P-2S1D',
247 '1R-CRio': 'RM-2P-2S1D',
248 '2R-CRio': 'RM-1P-2S1D',
249 '2R-CRo': 'RM-1P-2S1D',
251 '1R-1W-CRio': 'RM-2P-1S1D',
252 '1R-1W-CRo': 'RM-2P-1S1D',
253 '1R-1W': 'RM-2P-1S1D',
254 '1R-1W-imm': 'RM-2P-1S1D',
255 '1R-CRo': 'RM-2P-1S1D',
258 '1W-CRi': 'RM-2P-1S1D',
259 'CRio': 'RM-2P-1S1D',
260 'CR=2R1W': 'RM-1P-2S1D',
264 'LDST-2R-imm': 'LDSTRM-2P-2S',
265 'LDST-2R-1W-imm': 'LDSTRM-2P-2S1D',
266 'LDST-2R-1W': 'LDSTRM-2P-2S1D',
267 'LDST-2R-2W': 'LDSTRM-2P-2S1D',
268 'LDST-1R-1W-imm': 'LDSTRM-2P-1S1D',
269 'LDST-1R-2W-imm': 'LDSTRM-2P-1S2D',
270 'LDST-3R': 'LDSTRM-2P-3S',
271 'LDST-3R-CRo': 'LDSTRM-2P-3S', # st*x
272 'LDST-3R-1W': 'LDSTRM-2P-2S1D', # st*x
274 print ("# map to old SV Prefix")
276 print ('[[!table data="""')
277 for key
in primarykeys
:
278 name
= keyname(dictkeys
[key
])
279 value
= mapsto
.get(name
, "-")
280 print (tformat([name
, value
+ " "]))
286 print ('[[!table data="""')
287 print (tformat(tablecols
) + " imms | name |")
289 # print out the keys and the table from which they're derived
290 for key
in primarykeys
:
291 name
= keyname(dictkeys
[key
])
292 row
= tformat(dictkeys
[key
].values())
293 imms
= list(immediates
.get(key
, ""))
295 row
+= " %s | " % ("/".join(imms
))
296 row
+= " %s |" % name
301 # print out, by remap name, all the instructions under that category
302 for key
in primarykeys
:
303 name
= keyname(dictkeys
[key
])
304 value
= mapsto
.get(name
, "-")
305 print ("## %s (%s)" % (name
, value
))
307 print ('[[!table data="""')
308 print (tformat(['CSV', 'opcode', 'asm', 'form']))
316 #for fname, csv in csvs.items():
319 #for insn, row in insns.items():
322 print ("# svp64 remaps")
323 svp64
= OrderedDict()
324 # create a CSV file, per category, with SV "augmentation" info
325 csvcols
= ['insn', 'Ptype', 'Etype', '0', '1', '2', '3']
326 csvcols
+= ['in1', 'in2', 'in3', 'out', 'CR in', 'CR out'] # temporary
327 for key
in primarykeys
:
328 # get the decoded key containing row-analysis, and name/value
331 value
= mapsto
.get(name
, "-")
332 if value
== 'non-SV':
335 # print out svp64 tables by category
336 print ("* **%s**: %s" % (name
, value
))
338 # store csv entries by svp64 RM category
339 if value
not in svp64
:
346 #for idx in range(len(row)):
347 # if row[idx] == 'NONE':
349 # get the instruction
351 insn
= insns
[insn_name
]
352 # start constructing svp64 CSV row
354 res
['insn'] = insn_name
355 res
['Ptype'] = value
.split('-')[1] # predication type (RM-xN-xxx)
356 # get whether R_xxx_EXTRAn fields are 2-bit or 3-bit
357 res
['Etype'] = 'EXTRA2'
358 # go through each register matching to Rxxxx_EXTRAx
359 for k
in ['0', '1', '2', '3']:
362 # temporary useful info
364 for k
in ['in1', 'in2', 'in3', 'out', 'CR in', 'CR out']:
365 if insn
[k
].startswith('CONST'):
370 if insn
[k
] == 'RA_OR_ZERO':
372 elif insn
[k
] != 'NONE':
377 # sigh now the fun begins. this isn't the sanest way to do it
378 # but the patterns are pretty regular.
379 if value
== 'LDSTRM-2P-1S1D':
380 res
['Etype'] = 'EXTRA3' # RM EXTRA3 type
381 res
['0'] = 'd:RT' # RT: Rdest_EXTRA3
382 res
['1'] = 's:RA' # RA: Rsrc1_EXTRA3
384 elif value
== 'LDSTRM-2P-1S2D':
385 res
['Etype'] = 'EXTRA2' # RM EXTRA2 type
386 res
['0'] = 'd:RT' # RT: Rdest1_EXTRA2
387 res
['1'] = 'd:RA' # RA: Rdest2_EXTRA2
388 res
['2'] = 's:RA' # RA: Rsrc1_EXTRA2
390 elif value
== 'LDSTRM-2P-2S':
392 res
['Etype'] = 'EXTRA3' # RM EXTRA2 type
393 res
['0'] = 's:RS' # RT: Rdest1_EXTRA2
394 res
['1'] = 's:RA' # RA: Rsrc1_EXTRA2
396 elif value
== 'LDSTRM-2P-2S1D':
397 if 'st' in insn_name
and 'x' not in insn_name
: # stwu/stbu etc
398 res
['Etype'] = 'EXTRA2' # RM EXTRA2 type
399 res
['0'] = 'd:RA' # RA: Rdest1_EXTRA2
400 res
['1'] = 's:RS' # RS: Rdsrc1_EXTRA2
401 res
['2'] = 's:RA' # RA: Rsrc2_EXTRA2
402 elif 'st' in insn_name
and 'x' in insn_name
: # stwux
403 res
['Etype'] = 'EXTRA2' # RM EXTRA2 type
404 res
['0'] = 'd:RA' # RA: Rdest1_EXTRA2
405 res
['1'] = 's:RS;s:RA' # RS: Rdest2_EXTRA2, RA: Rsrc1_EXTRA2
406 res
['2'] = 's:RB' # RB: Rsrc2_EXTRA2
407 elif 'u' in insn_name
: # ldux etc.
408 res
['Etype'] = 'EXTRA2' # RM EXTRA2 type
409 res
['0'] = 'd:RT' # RT: Rdest1_EXTRA2
410 res
['1'] = 'd:RA' # RA: Rdest2_EXTRA2
411 res
['2'] = 's:RB' # RB: Rsrc1_EXTRA2
413 res
['Etype'] = 'EXTRA2' # RM EXTRA2 type
414 res
['0'] = 'd:RT' # RT: Rdest1_EXTRA2
415 res
['1'] = 's:RA' # RA: Rsrc1_EXTRA2
416 res
['2'] = 's:RB' # RB: Rsrc2_EXTRA2
418 elif value
== 'LDSTRM-2P-3S':
419 res
['Etype'] = 'EXTRA2' # RM EXTRA2 type
420 if 'cx' in insn_name
:
421 res
['0'] = 's:RS;d:CR0' # RS: Rsrc1_EXTRA2 CR0: dest
423 res
['0'] = 's:RS' # RS: Rsrc1_EXTRA2
424 res
['1'] = 's:RA' # RA: Rsrc2_EXTRA2
425 res
['2'] = 's:RB' # RA: Rsrc3_EXTRA2
427 elif value
== 'RM-2P-1S1D':
428 res
['Etype'] = 'EXTRA3' # RM EXTRA3 type
429 if insn_name
== 'mtspr':
430 res
['0'] = 'd:SPR' # SPR: Rdest1_EXTRA3
431 res
['1'] = 's:RS' # RS: Rsrc1_EXTRA3
432 elif insn_name
== 'mfspr':
433 res
['0'] = 'd:RS' # RS: Rdest1_EXTRA3
434 res
['1'] = 's:SPR' # SPR: Rsrc1_EXTRA3
435 elif name
== 'CRio' and insn_name
== 'mcrf':
436 res
['0'] = 'd:BF' # BFA: Rdest1_EXTRA3
437 res
['1'] = 's:BFA' # BFA: Rsrc1_EXTRA3
438 elif 'mfcr' in insn_name
or 'mfocrf' in insn_name
:
439 res
['0'] = 'd:RT' # RT: Rdest1_EXTRA3
440 res
['1'] = 's:CR' # CR: Rsrc1_EXTRA3
441 elif insn_name
== 'setb':
442 res
['0'] = 'd:RT' # RT: Rdest1_EXTRA3
443 res
['1'] = 's:BFA' # BFA: Rsrc1_EXTRA3
444 elif insn_name
.startswith('cmp'): # cmpi
445 res
['0'] = 'd:BF' # BF: Rdest1_EXTRA3
446 res
['1'] = 's:RA' # RA: Rsrc1_EXTRA3
447 elif regs
== ['RA','','','RT','','']:
448 res
['0'] = 'd:RT' # RT: Rdest1_EXTRA3
449 res
['1'] = 's:RA' # RA: Rsrc1_EXTRA3
450 elif regs
== ['RA','','','RT','','CR0']:
451 res
['0'] = 'd:RT;d:CR0' # RT,CR0: Rdest1_EXTRA3
452 res
['1'] = 's:RA' # RA: Rsrc1_EXTRA3
453 elif (regs
== ['RS','','','RA','','CR0'] or
454 regs
== ['','','RS','RA','','CR0']):
455 res
['0'] = 'd:RA;d:CR0' # RA,CR0: Rdest1_EXTRA3
456 res
['1'] = 's:RS' # RS: Rsrc1_EXTRA3
457 elif regs
== ['RS','','','RA','','']:
458 res
['0'] = 'd:RA' # RA: Rdest1_EXTRA3
459 res
['1'] = 's:RS' # RS: Rsrc1_EXTRA3
460 elif regs
== ['','FRB','','FRT','0','CR1']:
461 res
['0'] = 'd:FRT;d:CR1' # FRT,CR1: Rdest1_EXTRA3
462 res
['1'] = 's:FRA' # FRA: Rsrc1_EXTRA3
463 elif regs
== ['','FRB','','','','CR1']:
464 res
['0'] = 'd:CR1' # CR1: Rdest1_EXTRA3
465 res
['1'] = 's:FRB' # FRA: Rsrc1_EXTRA3
466 elif regs
== ['','FRB','','','','BF']:
467 res
['0'] = 'd:BF' # BF: Rdest1_EXTRA3
468 res
['1'] = 's:FRB' # FRA: Rsrc1_EXTRA3
469 elif regs
== ['','FRB','','FRT','','CR1']:
470 res
['0'] = 'd:FRT;d:CR1' # FRT,CR1: Rdest1_EXTRA3
471 res
['1'] = 's:FRB' # FRB: Rsrc1_EXTRA3
475 elif value
== 'RM-1P-2S1D':
476 res
['Etype'] = 'EXTRA3' # RM EXTRA3 type
477 if insn_name
.startswith('cr'):
478 res
['0'] = 'd:BT' # BT: Rdest1_EXTRA3
479 res
['1'] = 's:BA' # BA: Rsrc1_EXTRA3
480 res
['2'] = 's:BB' # BB: Rsrc2_EXTRA3
481 elif regs
== ['FRA','','FRC','FRT','','CR1']:
482 res
['0'] = 'd:FRT;d:CR1' # FRT,CR1: Rdest1_EXTRA3
483 res
['1'] = 's:FRA' # FRA: Rsrc1_EXTRA3
484 res
['2'] = 's:FRC' # FRC: Rsrc1_EXTRA3
486 elif regs
== ['FRA','FRB','','','','BF']:
487 res
['0'] = 'd:BF' # BF: Rdest1_EXTRA3
488 res
['1'] = 's:FRA' # FRA: Rsrc1_EXTRA3
489 res
['2'] = 's:FRB' # FRB: Rsrc1_EXTRA3
490 elif regs
== ['FRA','FRB','','FRT','','']:
491 res
['0'] = 'd:FRT' # FRT: Rdest1_EXTRA3
492 res
['1'] = 's:FRA' # FRA: Rsrc1_EXTRA3
493 res
['2'] = 's:FRB' # FRB: Rsrc1_EXTRA3
494 elif regs
== ['FRA','FRB','','FRT','','CR1']:
495 res
['0'] = 'd:FRT;d:CR1' # FRT,CR1: Rdest1_EXTRA3
496 res
['1'] = 's:FRA' # FRA: Rsrc1_EXTRA3
497 res
['2'] = 's:FRB' # FRB: Rsrc1_EXTRA3
498 elif name
== '2R-1W' or insn_name
== 'cmpb': # cmpb
499 if insn_name
in ['bpermd', 'cmpb']:
500 res
['0'] = 'd:RA' # RA: Rdest1_EXTRA3
501 res
['1'] = 's:RS' # RS: Rsrc1_EXTRA3
503 res
['0'] = 'd:RT' # RT: Rdest1_EXTRA3
504 res
['1'] = 's:RA' # RA: Rsrc1_EXTRA3
505 res
['2'] = 's:RB' # RB: Rsrc1_EXTRA3
506 elif insn_name
.startswith('cmp'): # cmp
507 res
['0'] = 'd:BF' # BF: Rdest1_EXTRA3
508 res
['1'] = 's:RA' # RA: Rsrc1_EXTRA3
509 res
['2'] = 's:RB' # RB: Rsrc1_EXTRA3
510 elif (regs
== ['','RB','RS','RA','','CR0'] or
511 regs
== ['RS','RB','','RA','','CR0']):
512 res
['0'] = 'd:RA;d:CR0' # RA,CR0: Rdest1_EXTRA3
513 res
['1'] = 's:RB' # RB: Rsrc1_EXTRA3
514 res
['2'] = 's:RS' # RS: Rsrc1_EXTRA3
515 elif regs
== ['RA','RB','','RT','','CR0']:
516 res
['0'] = 'd:RT;d:CR0' # RT,CR0: Rdest1_EXTRA3
517 res
['1'] = 's:RA' # RA: Rsrc1_EXTRA3
518 res
['2'] = 's:RB' # RB: Rsrc1_EXTRA3
519 elif regs
== ['RA','','RS','RA','','CR0']:
520 res
['0'] = 'd:RA;d:CR0' # RA,CR0: Rdest1_EXTRA3
521 res
['1'] = 's:RA' # RA: Rsrc1_EXTRA3
522 res
['2'] = 's:RS' # RS: Rsrc1_EXTRA3
526 elif value
== 'RM-2P-2S1D':
527 res
['Etype'] = 'EXTRA2' # RM EXTRA2 type
528 if insn_name
.startswith('mt'): # mtcrf
529 res
['0'] = 'd:CR' # CR: Rdest1_EXTRA2
530 res
['1'] = 's:RS' # RS: Rsrc1_EXTRA2
531 res
['2'] = 's:CR' # CR: Rsrc2_EXTRA2
535 elif value
== 'RM-1P-3S1D':
536 res
['Etype'] = 'EXTRA2' # RM EXTRA2 type
537 if insn_name
== 'isel':
538 res
['0'] = 'd:RT' # RT: Rdest1_EXTRA2
539 res
['1'] = 's:RA' # RA: Rsrc1_EXTRA2
540 res
['2'] = 's:RB' # RT: Rsrc2_EXTRA2
541 res
['3'] = 's:BC' # BC: Rsrc3_EXTRA2
543 res
['0'] = 'd:FRT;d:CR1' # FRT, CR1: Rdest1_EXTRA2
544 res
['1'] = 's:FRA' # FRA: Rsrc1_EXTRA2
545 res
['2'] = 's:FRB' # FRB: Rsrc2_EXTRA2
546 res
['3'] = 's:FRC' # FRC: Rsrc3_EXTRA2
549 #for k in ['in1', 'in2', 'in3', 'out', 'CR in', 'CR out']:
551 #if res['0'] != 'TODO':
553 if res
[k
] == 'NONE' or res
[k
] == '':
555 svp64
[value
].append(res
)
556 # also add to by-CSV version
557 csv_fname
= insn_to_csv
[insn_name
]
558 csvs_svp64
[csv_fname
].append(res
)
562 # now write out the csv files
563 for value
, csv
in svp64
.items():
564 # print out svp64 tables by category
565 print ("## %s" % value
)
567 print ('[[!table format=csv file="openpower/isatables/%s.csv"]]' % \
571 #csvcols = ['insn', 'Ptype', 'Etype', '0', '1', '2', '3']
572 write_csv("%s.csv" % value
, csv
, csvcols
)
574 # get SVP64 augmented CSV files
576 # Expand that (all .csv files)
577 pth
= find_wiki_file("*.csv")
579 # Ignore those containing: valid test sprs
580 for fname
in glob(pth
):
587 if fname
.endswith('sprs.csv'):
589 if fname
.endswith('minor_19_valid.csv'):
593 svp64_csv
= svt
.get_svp64_csv(fname
)
595 csvcols
= ['insn', 'Ptype', 'Etype']
596 csvcols
+= ['in1', 'in2', 'in3', 'out', 'CR in', 'CR out']
598 # and a nice microwatt VHDL file
599 file_path
= find_wiki_file("sv_decode.vhdl")
600 with
open(file_path
, 'w') as vhdl
:
601 # autogeneration warning
602 vhdl
.write("-- this file is auto-generated, do not edit\n")
603 vhdl
.write("-- http://libre-soc.org/openpower/sv_analysis.py")
604 vhdl
.write("-- part of Libre-SOC, sponsored by NLnet\n")
607 # first create array types
608 lens
= {'major' : 63,
619 for value
, csv
in csvs_svp64
.items():
621 value
= value
.lower()
622 value
= value
.replace("-", "_")
623 if value
not in lens
:
624 todo
= " -- TODO %s (or no SVP64 augmentation)\n"
625 vhdl
.write(todo
% value
)
628 typarray
= " type sv_%s_rom_array_t is " \
629 "array(0 to %d) of sv_decode_rom_t;\n"
630 vhdl
.write(typarray
% (value
, width
))
634 " constant sv_%s_decode_rom_array :\n" \
635 " sv_%s_rom_array_t := (\n"
636 ftr
= " others => sv_illegal_inst\n" \
638 for value
, csv
in csvs_svp64
.items():
640 value
= value
.lower()
641 value
= value
.replace("-", "_")
642 if value
not in lens
:
644 vhdl
.write(hdr
% (value
, value
))
646 insn
= str(entry
['insn'])
647 sventry
= svt
.svp64_instrs
.get(insn
, None)
648 op
= insns
[insn
]['opcode']
649 # binary-to-vhdl-binary
650 if op
.startswith("0b"):
651 op
= "2#%s#" % op
[2:]
653 for colname
in csvcols
[1:]:
655 # zero replace with NONE
659 re
= re
.replace("1P", "P1")
660 re
= re
.replace("2P", "P2")
663 for colname
in ['sv_in1', 'sv_in2', 'sv_in3', 'sv_out',
664 'sv_cr_in', 'sv_cr_out']:
668 re
= sventry
[colname
]
671 vhdl
.write(" %13s => (%s), -- %s\n" % (op
, row
, insn
))
674 if __name__
== '__main__':