diff options
author | Pavel I. Kryukov <kryukov@frtk.ru> | 2018-11-19 12:29:00 +0300 |
---|---|---|
committer | Andrew Waterman <aswaterman@gmail.com> | 2018-11-19 01:29:00 -0800 |
commit | 8a46cfda4607151689580eccccb2642264585443 (patch) | |
tree | 881efcc48b9894064e4abd7a20ee764b02c10067 /parse-opcodes | |
parent | 4743054914fa1a2f04208866d5ba22bbd8400f90 (diff) | |
download | riscv-opcodes-8a46cfda4607151689580eccccb2642264585443.zip riscv-opcodes-8a46cfda4607151689580eccccb2642264585443.tar.gz riscv-opcodes-8a46cfda4607151689580eccccb2642264585443.tar.bz2 |
Modernize to Python 3 (#22)
Diffstat (limited to 'parse-opcodes')
-rwxr-xr-x | parse-opcodes | 225 |
1 files changed, 114 insertions, 111 deletions
diff --git a/parse-opcodes b/parse-opcodes index 6ad06f1..510450b 100755 --- a/parse-opcodes +++ b/parse-opcodes @@ -1,5 +1,8 @@ #!/usr/bin/env python +from __future__ import print_function +from builtins import hex +from builtins import range import math import sys import tokenize @@ -289,34 +292,34 @@ def binary(n, digits=0): return rep if digits == 0 else ('0' * (digits - len(rep))) + rep def make_c(match,mask): - print '/* Automatically generated by parse-opcodes. */' - print '#ifndef RISCV_ENCODING_H' - print '#define RISCV_ENCODING_H' + print('/* Automatically generated by parse-opcodes. */') + print('#ifndef RISCV_ENCODING_H') + print('#define RISCV_ENCODING_H') for name in namelist: name2 = name.upper().replace('.','_') - print '#define MATCH_%s %s' % (name2, hex(match[name])) - print '#define MASK_%s %s' % (name2, hex(mask[name])) + print('#define MATCH_%s %s' % (name2, hex(match[name]))) + print('#define MASK_%s %s' % (name2, hex(mask[name]))) for num, name in csrs+csrs32: - print '#define CSR_%s %s' % (name.upper(), hex(num)) + print('#define CSR_%s %s' % (name.upper(), hex(num))) for num, name in causes: - print '#define CAUSE_%s %s' % (name.upper().replace(' ', '_'), hex(num)) - print '#endif' + print('#define CAUSE_%s %s' % (name.upper().replace(' ', '_'), hex(num))) + print('#endif') - print '#ifdef DECLARE_INSN' + print('#ifdef DECLARE_INSN') for name in namelist: name2 = name.replace('.','_') - print 'DECLARE_INSN(%s, MATCH_%s, MASK_%s)' % (name2, name2.upper(), name2.upper()) - print '#endif' + print('DECLARE_INSN(%s, MATCH_%s, MASK_%s)' % (name2, name2.upper(), name2.upper())) + print('#endif') - print '#ifdef DECLARE_CSR' + print('#ifdef DECLARE_CSR') for num, name in csrs+csrs32: - print 'DECLARE_CSR(%s, CSR_%s)' % (name, name.upper()) - print '#endif' + print('DECLARE_CSR(%s, CSR_%s)' % (name, name.upper())) + print('#endif') - print '#ifdef DECLARE_CAUSE' + print('#ifdef DECLARE_CAUSE') for num, name in causes: - print 'DECLARE_CAUSE("%s", CAUSE_%s)' % (name, name.upper().replace(' ', '_')) - print '#endif' + print('DECLARE_CAUSE("%s", CAUSE_%s)' % (name, name.upper().replace(' ', '_'))) + print('#endif') def yank(num,start,len): return (num >> start) & ((1 << len) - 1) @@ -333,7 +336,7 @@ def str_inst(name,arguments): return name.replace('.rv32','').upper() def print_unimp_type(name,match,arguments): - print """ + print(""" & \\multicolumn{10}{|c|}{%s} & %s \\\\ \\cline{2-11} @@ -341,10 +344,10 @@ def print_unimp_type(name,match,arguments): ( \ '0'*32, \ 'UNIMP' \ - ) + )) def print_u_type(name,match,arguments): - print """ + print(""" & \\multicolumn{8}{|c|}{%s} & \\multicolumn{1}{c|}{%s} & @@ -356,10 +359,10 @@ def print_u_type(name,match,arguments): str_arg('rd','',match,arguments), \ binary(yank(match,opcode_base,opcode_size),opcode_size), \ str_inst(name,arguments) \ - ) + )) def print_uj_type(name,match,arguments): - print """ + print(""" & \\multicolumn{8}{|c|}{%s} & \\multicolumn{1}{c|}{%s} & @@ -371,10 +374,10 @@ def print_uj_type(name,match,arguments): str_arg('rd','',match,arguments), \ binary(yank(match,opcode_base,opcode_size),opcode_size), \ str_inst(name,arguments) \ - ) + )) def print_s_type(name,match,arguments): - print """ + print(""" & \\multicolumn{4}{|c|}{%s} & \\multicolumn{2}{c|}{%s} & @@ -392,10 +395,10 @@ def print_s_type(name,match,arguments): str_arg('imm12lo','imm[4:0]',match,arguments), \ binary(yank(match,opcode_base,opcode_size),opcode_size), \ str_inst(name,arguments) \ - ) + )) def print_sb_type(name,match,arguments): - print """ + print(""" & \\multicolumn{4}{|c|}{%s} & \\multicolumn{2}{c|}{%s} & @@ -413,10 +416,10 @@ def print_sb_type(name,match,arguments): str_arg('bimm12lo','imm[4:1$\\vert$11]',match,arguments), \ binary(yank(match,opcode_base,opcode_size),opcode_size), \ str_inst(name,arguments) \ - ) + )) def print_i_type(name,match,arguments): - print """ + print(""" & \\multicolumn{6}{|c|}{%s} & \\multicolumn{1}{c|}{%s} & @@ -432,10 +435,10 @@ def print_i_type(name,match,arguments): str_arg('rd','',match,arguments), \ binary(yank(match,opcode_base,opcode_size),opcode_size), \ str_inst(name,arguments) \ - ) + )) def print_csr_type(name,match,arguments): - print """ + print(""" & \\multicolumn{6}{|c|}{%s} & \\multicolumn{1}{c|}{%s} & @@ -451,10 +454,10 @@ def print_csr_type(name,match,arguments): str_arg('rd','',match,arguments), \ binary(yank(match,opcode_base,opcode_size),opcode_size), \ str_inst(name,arguments) \ - ) + )) def print_ish_type(name,match,arguments): - print """ + print(""" & \\multicolumn{3}{|c|}{%s} & \\multicolumn{3}{c|}{%s} & @@ -472,10 +475,10 @@ def print_ish_type(name,match,arguments): str_arg('rd','',match,arguments), \ binary(yank(match,opcode_base,opcode_size),opcode_size), \ str_inst(name,arguments) \ - ) + )) def print_ishw_type(name,match,arguments): - print """ + print(""" & \\multicolumn{4}{|c|}{%s} & \\multicolumn{2}{c|}{%s} & @@ -493,10 +496,10 @@ def print_ishw_type(name,match,arguments): str_arg('rd','',match,arguments), \ binary(yank(match,opcode_base,opcode_size),opcode_size), \ str_inst(name,arguments) \ - ) + )) def print_r_type(name,match,arguments): - print """ + print(""" & \\multicolumn{4}{|c|}{%s} & \\multicolumn{2}{c|}{%s} & @@ -514,10 +517,10 @@ def print_r_type(name,match,arguments): str_arg('rd','',match,arguments), \ binary(yank(match,opcode_base,opcode_size),opcode_size), \ str_inst(name,arguments) \ - ) + )) def print_r4_type(name,match,arguments): - print """ + print(""" & \\multicolumn{2}{|c|}{%s} & \\multicolumn{2}{c|}{%s} & @@ -537,10 +540,10 @@ def print_r4_type(name,match,arguments): str_arg('rd','',match,arguments), \ binary(yank(match,opcode_base,opcode_size),opcode_size), \ str_inst(name,arguments) \ - ) + )) def print_amo_type(name,match,arguments): - print """ + print(""" & \\multicolumn{2}{|c|}{%s} & \\multicolumn{1}{c|}{aq} & @@ -560,10 +563,10 @@ def print_amo_type(name,match,arguments): str_arg('rd','',match,arguments), \ binary(yank(match,opcode_base,opcode_size),opcode_size), \ str_inst(name,arguments) \ - ) + )) def print_fence_type(name,match,arguments): - print """ + print(""" & \\multicolumn{2}{|c|}{%s} & \\multicolumn{3}{c|}{%s} & @@ -583,10 +586,10 @@ def print_fence_type(name,match,arguments): str_arg('rd','',match,arguments), \ binary(yank(match,opcode_base,opcode_size),opcode_size), \ str_inst(name,arguments) \ - ) + )) def print_header(*types): - print """ + print(""" \\newpage \\begin{table}[p] @@ -606,9 +609,9 @@ def print_header(*types): \\instbitrange{11}{7} & \\instbitrange{6}{0} \\\\ \\cline{2-11} -""" +""") if 'r' in types: - print """ + print(""" & \\multicolumn{4}{|c|}{funct7} & \\multicolumn{2}{c|}{rs2} & @@ -617,9 +620,9 @@ def print_header(*types): \\multicolumn{1}{c|}{rd} & \\multicolumn{1}{c|}{opcode} & R-type \\\\ \\cline{2-11} -""" +""") if 'r4' in types: - print """ + print(""" & \\multicolumn{2}{|c|}{rs3} & \\multicolumn{2}{c|}{funct2} & @@ -629,9 +632,9 @@ def print_header(*types): \\multicolumn{1}{c|}{rd} & \\multicolumn{1}{c|}{opcode} & R4-type \\\\ \\cline{2-11} - """ + """) if 'i' in types: - print """ + print(""" & \\multicolumn{6}{|c|}{imm[11:0]} & \\multicolumn{1}{c|}{rs1} & @@ -639,9 +642,9 @@ def print_header(*types): \\multicolumn{1}{c|}{rd} & \\multicolumn{1}{c|}{opcode} & I-type \\\\ \\cline{2-11} -""" +""") if 's' in types: - print """ + print(""" & \\multicolumn{4}{|c|}{imm[11:5]} & \\multicolumn{2}{c|}{rs2} & @@ -650,9 +653,9 @@ def print_header(*types): \\multicolumn{1}{c|}{imm[4:0]} & \\multicolumn{1}{c|}{opcode} & S-type \\\\ \\cline{2-11} -""" +""") if 'sb' in types: - print """ + print(""" & \\multicolumn{4}{|c|}{imm[12$\\vert$10:5]} & \\multicolumn{2}{c|}{rs2} & @@ -661,42 +664,42 @@ def print_header(*types): \\multicolumn{1}{c|}{imm[4:1$\\vert$11]} & \\multicolumn{1}{c|}{opcode} & B-type \\\\ \\cline{2-11} -""" +""") if 'u' in types: - print """ + print(""" & \\multicolumn{8}{|c|}{imm[31:12]} & \\multicolumn{1}{c|}{rd} & \\multicolumn{1}{c|}{opcode} & U-type \\\\ \\cline{2-11} -""" +""") if 'uj' in types: - print """ + print(""" & \\multicolumn{8}{|c|}{imm[20$\\vert$10:1$\\vert$11$\\vert$19:12]} & \\multicolumn{1}{c|}{rd} & \\multicolumn{1}{c|}{opcode} & J-type \\\\ \\cline{2-11} -""" +""") def print_subtitle(title): - print """ + print(""" & \\multicolumn{10}{c}{} & \\\\ & \\multicolumn{10}{c}{\\bf %s} & \\\\ \\cline{2-11} - """ % title + """ % title) def print_footer(caption=''): - print """ + print(""" \\end{tabular} \\end{center} \\end{small} %s \\label{instr-table} \\end{table} - """ % caption + """ % caption) def print_inst(n): if n == 'fence': @@ -819,40 +822,40 @@ def print_chisel_insn(name): s = '%s%d' % (s, yank(match[name], i, 1)) else: s = s + '?' - print s + "\")" + print(s + "\")") def make_chisel(): - print '/* Automatically generated by parse-opcodes */' - print 'object Instructions {' + print('/* Automatically generated by parse-opcodes */') + print('object Instructions {') for name in namelist: print_chisel_insn(name) - print '}' - print 'object Causes {' + print('}') + print('object Causes {') for num, name in causes: - print ' val %s = %s' % (name.lower().replace(' ', '_'), hex(num)) - print ' val all = {' - print ' val res = collection.mutable.ArrayBuffer[Int]()' + print(' val %s = %s' % (name.lower().replace(' ', '_'), hex(num))) + print(' val all = {') + print(' val res = collection.mutable.ArrayBuffer[Int]()') for num, name in causes: - print ' res += %s' % (name.lower().replace(' ', '_')) - print ' res.toArray' - print ' }' - print '}' - print 'object CSRs {' + print(' res += %s' % (name.lower().replace(' ', '_'))) + print(' res.toArray') + print(' }') + print('}') + print('object CSRs {') for num, name in csrs+csrs32: - print ' val %s = %s' % (name, hex(num)) - print ' val all = {' - print ' val res = collection.mutable.ArrayBuffer[Int]()' + print(' val %s = %s' % (name, hex(num))) + print(' val all = {') + print(' val res = collection.mutable.ArrayBuffer[Int]()') for num, name in csrs: - print ' res += %s' % (name) - print ' res.toArray' - print ' }' - print ' val all32 = {' - print ' val res = collection.mutable.ArrayBuffer(all:_*)' + print(' res += %s' % (name)) + print(' res.toArray') + print(' }') + print(' val all32 = {') + print(' val res = collection.mutable.ArrayBuffer(all:_*)') for num, name in csrs32: - print ' res += %s' % (name) - print ' res.toArray' - print ' }' - print '}' + print(' res += %s' % (name)) + print(' res.toArray') + print(' }') + print('}') def signed(value, width): if 0 <= value < (1<<(width-1)): @@ -861,37 +864,37 @@ def signed(value, width): return value - (1<<width) def print_go_insn(name): - print '\tcase A%s:' % name.upper().replace('.', '') + print('\tcase A%s:' % name.upper().replace('.', '')) m = match[name] opcode = yank(m, 0, 7) funct3 = yank(m, 12, 3) rs2 = yank(m, 20, 5) csr = yank(m, 20, 12) funct7 = yank(m, 25, 7) - print '\t\treturn &inst{0x%x, 0x%x, 0x%x, %d, 0x%x}, true' % (opcode, funct3, rs2, signed(csr, 12), funct7) + print('\t\treturn &inst{0x%x, 0x%x, 0x%x, %d, 0x%x}, true' % (opcode, funct3, rs2, signed(csr, 12), funct7)) def make_go(): - print '// Code generated by parse-opcodes; DO NOT EDIT.' - print - print 'package riscv' - print - print 'import "cmd/internal/obj"' - print - print 'type inst struct {' - print '\topcode uint32' - print '\tfunct3 uint32' - print '\trs2 uint32' - print '\tcsr int64' - print '\tfunct7 uint32' - print '}' - print - print 'func encode(a obj.As) (i *inst, ok bool) {' - print '\tswitch a {' + print('// Code generated by parse-opcodes; DO NOT EDIT.') + print() + print('package riscv') + print() + print('import "cmd/internal/obj"') + print() + print('type inst struct {') + print('\topcode uint32') + print('\tfunct3 uint32') + print('\trs2 uint32') + print('\tcsr int64') + print('\tfunct7 uint32') + print('}') + print() + print('func encode(a obj.As) (i *inst, ok bool) {') + print('\tswitch a {') for name in namelist: print_go_insn(name) - print '\t}' - print '\treturn nil, false' - print '}' + print('\t}') + print('\treturn nil, false') + print('}') for line in sys.stdin: line = line.partition('#') @@ -909,7 +912,7 @@ for line in sys.stdin: mymask = 0 cover = 0 - if not name in arguments.keys(): + if not name in list(arguments.keys()): arguments[name] = [] for token in tokens[1:]: @@ -950,7 +953,7 @@ for line in sys.stdin: if pseudo: pseudos[name] = 1 else: - for name2,match2 in match.iteritems(): + for name2,match2 in match.items(): if name2 not in pseudos and (match2 & mymask) == mymatch: sys.exit("%s and %s overlap" % (name,name2)) |