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
6 from soc
.decoder
.orderedset
import OrderedSet
9 fdir
= os
.path
.abspath(os
.path
.dirname(__file__
))
10 fdir
= os
.path
.split(fdir
)[0]
11 return os
.path
.join(fdir
, "isa")
13 def create_args(reglist
, extra
=None):
20 return ', '.join(args
)
24 # auto-generated by pywriter.py, do not edit or commit
26 from soc.decoder.isa import ISACaller
27 from soc.decoder.helpers import (EXTS64, EXTZ64, ROTL64, ROTL32, MASK,)
28 from soc.decoder.selectable_int import SelectableInt
29 from soc.decoder.selectable_int import selectconcat as concat
30 from soc.decoder.orderedset import OrderedSet
36 class PyISAWriter(ISA
):
40 def write_pysource(self
, pagename
):
41 instrs
= isa
.page
[pagename
]
42 isadir
= get_isasrc_dir()
43 fname
= os
.path
.join(isadir
, "%s.py" % pagename
)
44 with
open(fname
, "w") as f
:
46 f
.write(header
% pagename
) # write out header
47 # go through all instructions
50 print (fname
, d
.opcode
)
51 pcode
= '\n'.join(d
.pcode
) + '\n'
53 pycode
, rused
= convert_to_python(pcode
)
54 # create list of arguments to call
55 regs
= list(rused
['read_regs']) + list(rused
['uninit_regs'])
56 args
= create_args(regs
, 'self')
57 # create list of arguments to return
58 retargs
= create_args(rused
['write_regs'])
59 # write out function. pre-pend "op_" because some instrs are
60 # also python keywords (cmp). also replace "." with "_"
61 op_fname
="op_%s" % page
.replace(".", "_")
62 f
.write(" def %s(%s):\n" % (op_fname
, args
))
63 pycode
= pycode
.split("\n")
64 pycode
= '\n'.join(map(lambda x
: " %s" % x
, pycode
))
65 pycode
= pycode
.rstrip()
66 f
.write(pycode
+ '\n')
68 f
.write(" return (%s,)\n\n" % retargs
)
71 # accumulate the instruction info
72 iinfo
= "(%s, %s,\n %s, %s)" % \
73 (op_fname
, rused
['read_regs'],
74 rused
['uninit_regs'], rused
['write_regs'])
75 iinf
+= " instrs['%s'] = %s\n" % (page
, iinfo
)
76 # write out initialisation of info, for ISACaller to use
77 f
.write(" instrs = {}\n")
80 if __name__
== '__main__':
82 isa
.write_pysource('fixedload')
84 isa
.write_pysource('comparefixed')
85 isa
.write_pysource('fixedarith')