1 # python code-writer for OpenPOWER ISA pseudo-code parsing
4 from soc
.decoder
.pseudo
.pagereader
import ISA
5 from soc
.decoder
.power_pseudo
import convert_to_python
8 fdir
= os
.path
.abspath(os
.path
.dirname(__file__
))
9 fdir
= os
.path
.split(fdir
)[0]
10 return os
.path
.join(fdir
, "isa")
12 def create_args(reglist
, extra
=None):
19 return ', '.join(args
)
22 # auto-generated by pywriter.py, do not edit or commit
24 from soc.decoder.isa import ISACaller
25 from soc.decoder.helpers import (EXTS64, EXTZ64, ROTL64, ROTL32, MASK,)
31 class PyISAWriter(ISA
):
35 def write_pysource(self
, pagename
):
36 instrs
= isa
.page
[pagename
]
37 isadir
= get_isasrc_dir()
38 fname
= os
.path
.join(isadir
, "%s.py" % pagename
)
39 with
open(fname
, "w") as f
:
41 f
.write(header
% pagename
) # write out header
42 # go through all instructions
45 print (fname
, d
.opcode
)
46 pcode
= '\n'.join(d
.pcode
) + '\n'
48 pycode
, rused
= convert_to_python(pcode
)
49 # create list of arguments to call
50 regs
= rused
['read_regs'] + rused
['uninit_regs']
51 args
= create_args(regs
, 'self')
52 # create list of arguments to return
53 retargs
= create_args(rused
['write_regs'])
54 # write out function. pre-pend "op_" because some instrs are
55 # also python keywords (cmp). also replace "." with "_"
56 op_fname
="op_%s" % page
.replace(".", "_")
57 f
.write(" def %s(%s):\n" % (op_fname
, args
))
58 pycode
= pycode
.split("\n")
59 pycode
= '\n'.join(map(lambda x
: " %s" % x
, pycode
))
60 pycode
= pycode
.rstrip()
61 f
.write(pycode
+ '\n')
63 f
.write(" return (%s,)\n\n" % retargs
)
66 # accumulate the instruction info
67 iinfo
= "(%s, %s, %s, %s)" % \
68 (op_fname
, rused
['read_regs'],
69 rused
['uninit_regs'], rused
['write_regs'])
70 iinf
+= " instrs['%s'] = %s\n" % (page
, iinfo
)
71 # write out initialisation of info, for ISACaller to use
72 f
.write(" instrs = {}\n")
75 if __name__
== '__main__':
77 isa
.write_pysource('comparefixed')
78 isa
.write_pysource('fixedarith')