add extra (fake) out2 column to RM SVP64 CSV output
[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 from soc.decoder.power_svp64 import SVP64RM
21
22
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)
28 return file_path
29
30 # Return an array of dictionaries from the CSV file name:
31 def get_csv(name):
32 file_path = find_wiki_file(name)
33 with open(file_path, 'r') as csvfile:
34 reader = csv.DictReader(csvfile)
35 return list(reader)
36
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")
42 writer.writeheader()
43 writer.writerows(items)
44
45 # This will return True if all values are true.
46 # Not sure what this is about
47 def blank_key(row):
48 #for v in row.values():
49 # if 'SPR' in v: # skip all SPRs
50 # return True
51 for v in row.values():
52 if v:
53 return False
54 return True
55
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
59 def isreg(field):
60 return (field.startswith('R') or field.startswith('FR') or
61 field == 'SPR')
62
63
64 # These are the attributes of the instructions,
65 # register names
66 keycolumns = ['unit', 'in1', 'in2', 'in3', 'out', 'CR in', 'CR out',
67 ] # don't think we need these: 'ldst len', 'rc', 'lk']
68
69 tablecols = ['unit', 'in', 'outcnt', 'CR in', 'CR out', 'imm'
70 ] # don't think we need these: 'ldst len', 'rc', 'lk']
71
72 def create_key(row):
73 res = OrderedDict()
74 #print ("row", row)
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']:
78 if 'in' not in res:
79 res['in'] = 0
80 if isreg(row[key]):
81 res['in'] += 1
82
83 # registers OUT
84 if key == 'out':
85 # If upd is 1 then increment the count of outputs
86 if 'outcnt' not in res:
87 res['outcnt'] = 0
88 if isreg(row[key]):
89 res['outcnt'] += 1
90 if row['upd'] == '1':
91 res['outcnt'] += 1
92
93 # CRs (Condition Register) (CR0 .. CR7)
94 if key.startswith('CR'):
95 if row[key].startswith('NONE'):
96 res[key] = '0'
97 else:
98 res[key] = '1'
99 if row['comment'].startswith('cr'):
100 res['crop'] = '1'
101 # unit
102 if key == 'unit':
103 if row[key] == 'LDST': # we care about LDST units
104 res[key] = row[key]
105 else:
106 res[key] = 'OTHER'
107 # LDST len (LoadStore length)
108 if key.startswith('ldst'):
109 if row[key].startswith('NONE'):
110 res[key] = '0'
111 else:
112 res[key] = '1'
113 # rc, lk
114 if key in ['rc', 'lk']:
115 if row[key] == 'ONE':
116 res[key] = '1'
117 elif row[key] == 'NONE':
118 res[key] = '0'
119 else:
120 res[key] = 'R'
121 if key == 'lk':
122 res[key] = row[key]
123
124 # Convert the numerics 'in' & 'outcnt' to strings
125 res['in'] = str(res['in'])
126 res['outcnt'] = str(res['outcnt'])
127
128
129 # constants
130 if row['in2'].startswith('CONST_'):
131 res['imm'] = "1" # row['in2'].split("_")[1]
132 else:
133 res['imm'] = ''
134
135 return res
136
137 #
138 def dformat(d):
139 res = []
140 for k, v in d.items():
141 res.append("%s: %s" % (k, v))
142 return ' '.join(res)
143
144 def tformat(d):
145 return ' | '.join(d) + " |"
146
147 def keyname(row):
148 res = []
149 if row['unit'] != 'OTHER':
150 res.append(row['unit'])
151 if row['in'] != '0':
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':
156 if 'crop' in row:
157 res.append("CR=2R1W")
158 else:
159 res.append("CRio")
160 elif row['CR in'] == '1':
161 res.append("CRi")
162 elif row['CR out'] == '1':
163 res.append("CRo")
164 elif 'imm' in row and row['imm']:
165 res.append("imm")
166 return '-'.join(res)
167
168
169 def process_csvs():
170 csvs = {}
171 csvs_svp64 = {}
172 bykey = {}
173 primarykeys = set()
174 dictkeys = OrderedDict()
175 immediates = {}
176 insns = {} # dictionary of CSV row, by instruction
177 insn_to_csv = {}
178
179 print ("# OpenPOWER ISA register 'profile's")
180 print ('')
181 print ("this page is auto-generated, do not edit")
182 print ("created by http://libre-soc.org/openpower/sv_analysis.py")
183 print ('')
184
185 # Expand that (all .csv files)
186 pth = find_wiki_file("*.csv")
187
188 # Ignore those containing: valid test sprs
189 for fname in glob(pth):
190 if '-' in fname:
191 continue
192 if 'valid' in fname:
193 continue
194 if 'test' in fname:
195 continue
196 if fname.endswith('sprs.csv'):
197 continue
198 if fname.endswith('minor_19_valid.csv'):
199 continue
200 if 'RM' in fname:
201 continue
202 #print (fname)
203 csvname = os.path.split(fname)[1]
204 csvname_ = csvname.split(".")[0]
205 # csvname is something like: minor_59.csv, fname the whole path
206 csv = get_csv(fname)
207 csvs[fname] = csv
208 csvs_svp64[csvname_] = []
209 for row in csv:
210 if blank_key(row):
211 continue
212 insn_name = row['comment']
213 # skip instructions that are not suitable
214 if insn_name in ['mcrxr', 'mcrxrx', 'darn']:
215 continue
216 if insn_name.startswith('bc') or 'rfid' in insn_name:
217 continue
218 if insn_name in ['setvl',]: # SVP64 opcodes
219 continue
220
221 insns[insn_name] = row # accumulate csv data by instruction
222 insn_to_csv[insn_name] = csvname_ # CSV file name by instruction
223 dkey = create_key(row)
224 key = tuple(dkey.values())
225 # print("key=", key)
226 dictkeys[key] = dkey
227 primarykeys.add(key)
228 if key not in bykey:
229 bykey[key] = []
230 bykey[key].append((csvname, row['opcode'], insn_name,
231 row['form'].upper() + '-Form'))
232
233 # detect immediates, collate them (useful info)
234 if row['in2'].startswith('CONST_'):
235 imm = row['in2'].split("_")[1]
236 if key not in immediates:
237 immediates[key] = set()
238 immediates[key].add(imm)
239
240 primarykeys = list(primarykeys)
241 primarykeys.sort()
242
243 # mapping to old SVPrefix "Forms"
244 mapsto = {'3R-1W-CRio': 'RM-1P-3S1D',
245 '2R-1W-CRio': 'RM-1P-2S1D',
246 '2R-1W-CRi': 'RM-1P-3S1D',
247 '2R-1W-CRo': 'RM-1P-2S1D',
248 '2R': 'non-SV',
249 '2R-1W': 'RM-1P-2S1D',
250 '1R-CRio': 'RM-2P-2S1D',
251 '2R-CRio': 'RM-1P-2S1D',
252 '2R-CRo': 'RM-1P-2S1D',
253 '1R': 'non-SV',
254 '1R-1W-CRio': 'RM-2P-1S1D',
255 '1R-1W-CRo': 'RM-2P-1S1D',
256 '1R-1W': 'RM-2P-1S1D',
257 '1R-1W-imm': 'RM-2P-1S1D',
258 '1R-CRo': 'RM-2P-1S1D',
259 '1R-imm': 'non-SV',
260 '1W': 'non-SV',
261 '1W-CRi': 'RM-2P-1S1D',
262 'CRio': 'RM-2P-1S1D',
263 'CR=2R1W': 'RM-1P-2S1D',
264 'CRi': 'non-SV',
265 'imm': 'non-SV',
266 '': 'non-SV',
267 'LDST-2R-imm': 'LDSTRM-2P-2S',
268 'LDST-2R-1W-imm': 'LDSTRM-2P-2S1D',
269 'LDST-2R-1W': 'LDSTRM-2P-2S1D',
270 'LDST-2R-2W': 'LDSTRM-2P-2S1D',
271 'LDST-1R-1W-imm': 'LDSTRM-2P-1S1D',
272 'LDST-1R-2W-imm': 'LDSTRM-2P-1S2D',
273 'LDST-3R': 'LDSTRM-2P-3S',
274 'LDST-3R-CRo': 'LDSTRM-2P-3S', # st*x
275 'LDST-3R-1W': 'LDSTRM-2P-2S1D', # st*x
276 }
277 print ("# map to old SV Prefix")
278 print ('')
279 print ('[[!table data="""')
280 for key in primarykeys:
281 name = keyname(dictkeys[key])
282 value = mapsto.get(name, "-")
283 print (tformat([name, value+ " "]))
284 print ('"""]]')
285 print ('')
286
287 print ("# keys")
288 print ('')
289 print ('[[!table data="""')
290 print (tformat(tablecols) + " imms | name |")
291
292 # print out the keys and the table from which they're derived
293 for key in primarykeys:
294 name = keyname(dictkeys[key])
295 row = tformat(dictkeys[key].values())
296 imms = list(immediates.get(key, ""))
297 imms.sort()
298 row += " %s | " % ("/".join(imms))
299 row += " %s |" % name
300 print (row)
301 print ('"""]]')
302 print ('')
303
304 # print out, by remap name, all the instructions under that category
305 for key in primarykeys:
306 name = keyname(dictkeys[key])
307 value = mapsto.get(name, "-")
308 print ("## %s (%s)" % (name, value))
309 print ('')
310 print ('[[!table data="""')
311 print (tformat(['CSV', 'opcode', 'asm', 'form']))
312 rows = bykey[key]
313 rows.sort()
314 for row in rows:
315 print (tformat(row))
316 print ('"""]]')
317 print ('')
318
319 #for fname, csv in csvs.items():
320 # print (fname)
321
322 #for insn, row in insns.items():
323 # print (insn, row)
324
325 print ("# svp64 remaps")
326 svp64 = OrderedDict()
327 # create a CSV file, per category, with SV "augmentation" info
328 # XXX note: 'out2' not added here, needs to be added to CSV files
329 csvcols = ['insn', 'Ptype', 'Etype', '0', '1', '2', '3']
330 csvcols += ['in1', 'in2', 'in3', 'out', 'CR in', 'CR out'] # temporary
331 for key in primarykeys:
332 # get the decoded key containing row-analysis, and name/value
333 dkey = dictkeys[key]
334 name = keyname(dkey)
335 value = mapsto.get(name, "-")
336 if value == 'non-SV':
337 continue
338
339 # print out svp64 tables by category
340 print ("* **%s**: %s" % (name, value))
341
342 # store csv entries by svp64 RM category
343 if value not in svp64:
344 svp64[value] = []
345
346 rows = bykey[key]
347 rows.sort()
348
349 for row in rows:
350 #for idx in range(len(row)):
351 # if row[idx] == 'NONE':
352 # row[idx] = ''
353 # get the instruction
354 insn_name = row[2]
355 insn = insns[insn_name]
356 # start constructing svp64 CSV row
357 res = OrderedDict()
358 res['insn'] = insn_name
359 res['Ptype'] = value.split('-')[1] # predication type (RM-xN-xxx)
360 # get whether R_xxx_EXTRAn fields are 2-bit or 3-bit
361 res['Etype'] = 'EXTRA2'
362 # go through each register matching to Rxxxx_EXTRAx
363 for k in ['0', '1', '2', '3']:
364 res[k] = ''
365 # create "fake" out2 (TODO, needs to be added to CSV files)
366 res['out2'] = 'NONE'
367 if insn['upd'] == '1': # LD/ST with update has RA as out2
368 res['out2'] = 'RA'
369
370 # temporary useful info
371 regs = []
372 for k in ['in1', 'in2', 'in3', 'out', 'CR in', 'CR out']:
373 if insn[k].startswith('CONST'):
374 res[k] = ''
375 regs.append('')
376 else:
377 res[k] = insn[k]
378 if insn[k] == 'RA_OR_ZERO':
379 regs.append('RA')
380 elif insn[k] != 'NONE':
381 regs.append(insn[k])
382 else:
383 regs.append('')
384
385 # sigh now the fun begins. this isn't the sanest way to do it
386 # but the patterns are pretty regular.
387 if value == 'LDSTRM-2P-1S1D':
388 res['Etype'] = 'EXTRA3' # RM EXTRA3 type
389 res['0'] = 'd:RT' # RT: Rdest_EXTRA3
390 res['1'] = 's:RA' # RA: Rsrc1_EXTRA3
391
392 elif value == 'LDSTRM-2P-1S2D':
393 res['Etype'] = 'EXTRA2' # RM EXTRA2 type
394 res['0'] = 'd:RT' # RT: Rdest1_EXTRA2
395 res['1'] = 'd:RA' # RA: Rdest2_EXTRA2
396 res['2'] = 's:RA' # RA: Rsrc1_EXTRA2
397
398 elif value == 'LDSTRM-2P-2S':
399 # stw, std, sth, stb
400 res['Etype'] = 'EXTRA3' # RM EXTRA2 type
401 res['0'] = 's:RS' # RT: Rdest1_EXTRA2
402 res['1'] = 's:RA' # RA: Rsrc1_EXTRA2
403
404 elif value == 'LDSTRM-2P-2S1D':
405 if 'st' in insn_name and 'x' not in insn_name: # stwu/stbu etc
406 res['Etype'] = 'EXTRA2' # RM EXTRA2 type
407 res['0'] = 'd:RA' # RA: Rdest1_EXTRA2
408 res['1'] = 's:RS' # RS: Rdsrc1_EXTRA2
409 res['2'] = 's:RA' # RA: Rsrc2_EXTRA2
410 elif 'st' in insn_name and 'x' in insn_name: # stwux
411 res['Etype'] = 'EXTRA2' # RM EXTRA2 type
412 res['0'] = 'd:RA' # RA: Rdest1_EXTRA2
413 res['1'] = 's:RS;s:RA' # RS: Rdest2_EXTRA2, RA: Rsrc1_EXTRA2
414 res['2'] = 's:RB' # RB: Rsrc2_EXTRA2
415 elif 'u' in insn_name: # ldux etc.
416 res['Etype'] = 'EXTRA2' # RM EXTRA2 type
417 res['0'] = 'd:RT' # RT: Rdest1_EXTRA2
418 res['1'] = 'd:RA' # RA: Rdest2_EXTRA2
419 res['2'] = 's:RB' # RB: Rsrc1_EXTRA2
420 else:
421 res['Etype'] = 'EXTRA2' # RM EXTRA2 type
422 res['0'] = 'd:RT' # RT: Rdest1_EXTRA2
423 res['1'] = 's:RA' # RA: Rsrc1_EXTRA2
424 res['2'] = 's:RB' # RB: Rsrc2_EXTRA2
425
426 elif value == 'LDSTRM-2P-3S':
427 res['Etype'] = 'EXTRA2' # RM EXTRA2 type
428 if 'cx' in insn_name:
429 res['0'] = 's:RS;d:CR0' # RS: Rsrc1_EXTRA2 CR0: dest
430 else:
431 res['0'] = 's:RS' # RS: Rsrc1_EXTRA2
432 res['1'] = 's:RA' # RA: Rsrc2_EXTRA2
433 res['2'] = 's:RB' # RA: Rsrc3_EXTRA2
434
435 elif value == 'RM-2P-1S1D':
436 res['Etype'] = 'EXTRA3' # RM EXTRA3 type
437 if insn_name == 'mtspr':
438 res['0'] = 'd:SPR' # SPR: Rdest1_EXTRA3
439 res['1'] = 's:RS' # RS: Rsrc1_EXTRA3
440 elif insn_name == 'mfspr':
441 res['0'] = 'd:RS' # RS: Rdest1_EXTRA3
442 res['1'] = 's:SPR' # SPR: Rsrc1_EXTRA3
443 elif name == 'CRio' and insn_name == 'mcrf':
444 res['0'] = 'd:BF' # BFA: Rdest1_EXTRA3
445 res['1'] = 's:BFA' # BFA: Rsrc1_EXTRA3
446 elif 'mfcr' in insn_name or 'mfocrf' in insn_name:
447 res['0'] = 'd:RT' # RT: Rdest1_EXTRA3
448 res['1'] = 's:CR' # CR: Rsrc1_EXTRA3
449 elif insn_name == 'setb':
450 res['0'] = 'd:RT' # RT: Rdest1_EXTRA3
451 res['1'] = 's:BFA' # BFA: Rsrc1_EXTRA3
452 elif insn_name.startswith('cmp'): # cmpi
453 res['0'] = 'd:BF' # BF: Rdest1_EXTRA3
454 res['1'] = 's:RA' # RA: Rsrc1_EXTRA3
455 elif regs == ['RA','','','RT','','']:
456 res['0'] = 'd:RT' # RT: Rdest1_EXTRA3
457 res['1'] = 's:RA' # RA: Rsrc1_EXTRA3
458 elif regs == ['RA','','','RT','','CR0']:
459 res['0'] = 'd:RT;d:CR0' # RT,CR0: Rdest1_EXTRA3
460 res['1'] = 's:RA' # RA: Rsrc1_EXTRA3
461 elif (regs == ['RS','','','RA','','CR0'] or
462 regs == ['','','RS','RA','','CR0']):
463 res['0'] = 'd:RA;d:CR0' # RA,CR0: Rdest1_EXTRA3
464 res['1'] = 's:RS' # RS: Rsrc1_EXTRA3
465 elif regs == ['RS','','','RA','','']:
466 res['0'] = 'd:RA' # RA: Rdest1_EXTRA3
467 res['1'] = 's:RS' # RS: Rsrc1_EXTRA3
468 elif regs == ['','FRB','','FRT','0','CR1']:
469 res['0'] = 'd:FRT;d:CR1' # FRT,CR1: Rdest1_EXTRA3
470 res['1'] = 's:FRA' # FRA: Rsrc1_EXTRA3
471 elif regs == ['','FRB','','','','CR1']:
472 res['0'] = 'd:CR1' # CR1: Rdest1_EXTRA3
473 res['1'] = 's:FRB' # FRA: Rsrc1_EXTRA3
474 elif regs == ['','FRB','','','','BF']:
475 res['0'] = 'd:BF' # BF: Rdest1_EXTRA3
476 res['1'] = 's:FRB' # FRA: Rsrc1_EXTRA3
477 elif regs == ['','FRB','','FRT','','CR1']:
478 res['0'] = 'd:FRT;d:CR1' # FRT,CR1: Rdest1_EXTRA3
479 res['1'] = 's:FRB' # FRB: Rsrc1_EXTRA3
480 else:
481 res['0'] = 'TODO'
482
483 elif value == 'RM-1P-2S1D':
484 res['Etype'] = 'EXTRA3' # RM EXTRA3 type
485 if insn_name.startswith('cr'):
486 res['0'] = 'd:BT' # BT: Rdest1_EXTRA3
487 res['1'] = 's:BA' # BA: Rsrc1_EXTRA3
488 res['2'] = 's:BB' # BB: Rsrc2_EXTRA3
489 elif regs == ['FRA','','FRC','FRT','','CR1']:
490 res['0'] = 'd:FRT;d:CR1' # FRT,CR1: Rdest1_EXTRA3
491 res['1'] = 's:FRA' # FRA: Rsrc1_EXTRA3
492 res['2'] = 's:FRC' # FRC: Rsrc1_EXTRA3
493 # should be for fcmp
494 elif regs == ['FRA','FRB','','','','BF']:
495 res['0'] = 'd:BF' # BF: Rdest1_EXTRA3
496 res['1'] = 's:FRA' # FRA: Rsrc1_EXTRA3
497 res['2'] = 's:FRB' # FRB: Rsrc1_EXTRA3
498 elif regs == ['FRA','FRB','','FRT','','']:
499 res['0'] = 'd:FRT' # FRT: Rdest1_EXTRA3
500 res['1'] = 's:FRA' # FRA: Rsrc1_EXTRA3
501 res['2'] = 's:FRB' # FRB: Rsrc1_EXTRA3
502 elif regs == ['FRA','FRB','','FRT','','CR1']:
503 res['0'] = 'd:FRT;d:CR1' # FRT,CR1: Rdest1_EXTRA3
504 res['1'] = 's:FRA' # FRA: Rsrc1_EXTRA3
505 res['2'] = 's:FRB' # FRB: Rsrc1_EXTRA3
506 elif name == '2R-1W' or insn_name == 'cmpb': # cmpb
507 if insn_name in ['bpermd', 'cmpb']:
508 res['0'] = 'd:RA' # RA: Rdest1_EXTRA3
509 res['1'] = 's:RS' # RS: Rsrc1_EXTRA3
510 else:
511 res['0'] = 'd:RT' # RT: Rdest1_EXTRA3
512 res['1'] = 's:RA' # RA: Rsrc1_EXTRA3
513 res['2'] = 's:RB' # RB: Rsrc1_EXTRA3
514 elif insn_name.startswith('cmp'): # cmp
515 res['0'] = 'd:BF' # BF: Rdest1_EXTRA3
516 res['1'] = 's:RA' # RA: Rsrc1_EXTRA3
517 res['2'] = 's:RB' # RB: Rsrc1_EXTRA3
518 elif (regs == ['','RB','RS','RA','','CR0'] or
519 regs == ['RS','RB','','RA','','CR0']):
520 res['0'] = 'd:RA;d:CR0' # RA,CR0: Rdest1_EXTRA3
521 res['1'] = 's:RB' # RB: Rsrc1_EXTRA3
522 res['2'] = 's:RS' # RS: Rsrc1_EXTRA3
523 elif regs == ['RA','RB','','RT','','CR0']:
524 res['0'] = 'd:RT;d:CR0' # RT,CR0: Rdest1_EXTRA3
525 res['1'] = 's:RA' # RA: Rsrc1_EXTRA3
526 res['2'] = 's:RB' # RB: Rsrc1_EXTRA3
527 elif regs == ['RA','','RS','RA','','CR0']:
528 res['0'] = 'd:RA;d:CR0' # RA,CR0: Rdest1_EXTRA3
529 res['1'] = 's:RA' # RA: Rsrc1_EXTRA3
530 res['2'] = 's:RS' # RS: Rsrc1_EXTRA3
531 else:
532 res['0'] = 'TODO'
533
534 elif value == 'RM-2P-2S1D':
535 res['Etype'] = 'EXTRA2' # RM EXTRA2 type
536 if insn_name.startswith('mt'): # mtcrf
537 res['0'] = 'd:CR' # CR: Rdest1_EXTRA2
538 res['1'] = 's:RS' # RS: Rsrc1_EXTRA2
539 res['2'] = 's:CR' # CR: Rsrc2_EXTRA2
540 else:
541 res['0'] = 'TODO'
542
543 elif value == 'RM-1P-3S1D':
544 res['Etype'] = 'EXTRA2' # RM EXTRA2 type
545 if insn_name == 'isel':
546 res['0'] = 'd:RT' # RT: Rdest1_EXTRA2
547 res['1'] = 's:RA' # RA: Rsrc1_EXTRA2
548 res['2'] = 's:RB' # RT: Rsrc2_EXTRA2
549 res['3'] = 's:BC' # BC: Rsrc3_EXTRA2
550 else:
551 res['0'] = 'd:FRT;d:CR1' # FRT, CR1: Rdest1_EXTRA2
552 res['1'] = 's:FRA' # FRA: Rsrc1_EXTRA2
553 res['2'] = 's:FRB' # FRB: Rsrc2_EXTRA2
554 res['3'] = 's:FRC' # FRC: Rsrc3_EXTRA2
555
556 # add to svp64 csvs
557 #for k in ['in1', 'in2', 'in3', 'out', 'CR in', 'CR out']:
558 # del res[k]
559 #if res['0'] != 'TODO':
560 for k in res:
561 if res[k] == 'NONE' or res[k] == '':
562 res[k] = '0'
563 svp64[value].append(res)
564 # also add to by-CSV version
565 csv_fname = insn_to_csv[insn_name]
566 csvs_svp64[csv_fname].append(res)
567
568 print ('')
569
570 # now write out the csv files
571 for value, csv in svp64.items():
572 # print out svp64 tables by category
573 print ("## %s" % value)
574 print ('')
575 print ('[[!table format=csv file="openpower/isatables/%s.csv"]]' % \
576 value)
577 print ('')
578
579 #csvcols = ['insn', 'Ptype', 'Etype', '0', '1', '2', '3']
580 write_csv("%s.csv" % value, csv, csvcols + ['out2'])
581
582 # okaaay, now we re-read them back in for producing microwatt SV
583
584 # get SVP64 augmented CSV files
585 svt = SVP64RM(microwatt_format=True)
586 # Expand that (all .csv files)
587 pth = find_wiki_file("*.csv")
588
589 # Ignore those containing: valid test sprs
590 for fname in glob(pth):
591 if '-' in fname:
592 continue
593 if 'valid' in fname:
594 continue
595 if 'test' in fname:
596 continue
597 if fname.endswith('sprs.csv'):
598 continue
599 if fname.endswith('minor_19_valid.csv'):
600 continue
601 if 'RM' in fname:
602 continue
603 svp64_csv = svt.get_svp64_csv(fname)
604
605 csvcols = ['insn', 'Ptype', 'Etype']
606 csvcols += ['in1', 'in2', 'in3', 'out', 'CR in', 'CR out']
607
608 # and a nice microwatt VHDL file
609 file_path = find_wiki_file("sv_decode.vhdl")
610 with open(file_path, 'w') as vhdl:
611 # autogeneration warning
612 vhdl.write("-- this file is auto-generated, do not edit\n")
613 vhdl.write("-- http://libre-soc.org/openpower/sv_analysis.py")
614 vhdl.write("-- part of Libre-SOC, sponsored by NLnet\n")
615 vhdl.write("\n")
616
617 # first create array types
618 lens = {'major' : 63,
619 'minor_4': 63,
620 'minor_19': 7,
621 'minor_30': 15,
622 'minor_31': 1023,
623 'minor_58': 63,
624 'minor_59': 31,
625 'minor_62': 63,
626 'minor_63l': 511,
627 'minor_63h': 16,
628 }
629 for value, csv in csvs_svp64.items():
630 # munge name
631 value = value.lower()
632 value = value.replace("-", "_")
633 if value not in lens:
634 todo = " -- TODO %s (or no SVP64 augmentation)\n"
635 vhdl.write(todo % value)
636 continue
637 width = lens[value]
638 typarray = " type sv_%s_rom_array_t is " \
639 "array(0 to %d) of sv_decode_rom_t;\n"
640 vhdl.write(typarray % (value, width))
641
642 # now output structs
643 hdr = "\n" \
644 " constant sv_%s_decode_rom_array :\n" \
645 " sv_%s_rom_array_t := (\n"
646 ftr = " others => sv_illegal_inst\n" \
647 " );\n\n"
648 for value, csv in csvs_svp64.items():
649 # munge name
650 value = value.lower()
651 value = value.replace("-", "_")
652 if value not in lens:
653 continue
654 vhdl.write(hdr % (value, value))
655 for entry in csv:
656 insn = str(entry['insn'])
657 sventry = svt.svp64_instrs.get(insn, None)
658 op = insns[insn]['opcode']
659 # binary-to-vhdl-binary
660 if op.startswith("0b"):
661 op = "2#%s#" % op[2:]
662 row = []
663 for colname in csvcols[1:]:
664 re = entry[colname]
665 # zero replace with NONE
666 if re == '0':
667 re = 'NONE'
668 # 1/2 predication
669 re = re.replace("1P", "P1")
670 re = re.replace("2P", "P2")
671 row.append(re)
672 print (sventry)
673 for colname in ['sv_in1', 'sv_in2', 'sv_in3', 'sv_out',
674 'sv_cr_in', 'sv_cr_out']:
675 if sventry is None:
676 re = 'NONE'
677 else:
678 re = sventry[colname]
679 row.append(re)
680 row = ', '.join(row)
681 vhdl.write(" %13s => (%s), -- %s\n" % (op, row, insn))
682 vhdl.write(ftr)
683
684 if __name__ == '__main__':
685 process_csvs()