7c6e70ad01b60ce978ff9828a60c068fbc34790e
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
7 from soc
.decoder
.isa
.caller
import create_args
10 fdir
= os
.path
.abspath(os
.path
.dirname(__file__
))
11 fdir
= os
.path
.split(fdir
)[0]
12 return os
.path
.join(fdir
, "isa")
16 # auto-generated by pywriter.py, do not edit or commit
18 from soc.decoder.isa.caller import inject
19 from soc.decoder.helpers import (EXTS, EXTS64, EXTZ64, ROTL64, ROTL32, MASK,)
20 from soc.decoder.selectable_int import SelectableInt
21 from soc.decoder.selectable_int import selectconcat as concat
22 from soc.decoder.orderedset import OrderedSet
28 class PyISAWriter(ISA
):
32 def write_pysource(self
, pagename
):
33 instrs
= isa
.page
[pagename
]
34 isadir
= get_isasrc_dir()
35 fname
= os
.path
.join(isadir
, "%s.py" % pagename
)
36 with
open(fname
, "w") as f
:
38 f
.write(header
% pagename
) # write out header
39 # go through all instructions
42 print (fname
, d
.opcode
)
43 pcode
= '\n'.join(d
.pcode
) + '\n'
45 pycode
, rused
= convert_to_python(pcode
)
46 # create list of arguments to call
47 regs
= list(rused
['read_regs']) + list(rused
['uninit_regs'])
48 args
= ', '.join(create_args(regs
, 'self'))
49 # create list of arguments to return
50 retargs
= ', '.join(create_args(rused
['write_regs']))
51 # write out function. pre-pend "op_" because some instrs are
52 # also python keywords (cmp). also replace "." with "_"
53 op_fname
="op_%s" % page
.replace(".", "_")
54 f
.write(" @inject()\n")
55 f
.write(" def %s(%s):\n" % (op_fname
, args
))
56 pycode
= pycode
.split("\n")
57 pycode
= '\n'.join(map(lambda x
: " %s" % x
, pycode
))
58 pycode
= pycode
.rstrip()
59 f
.write(pycode
+ '\n')
61 f
.write(" return (%s,)\n\n" % retargs
)
64 # accumulate the instruction info
65 iinfo
= "(%s, %s,\n %s, %s, '%s')" % \
66 (op_fname
, rused
['read_regs'],
67 rused
['uninit_regs'], rused
['write_regs'],
69 iinf
+= " %s_instrs['%s'] = %s\n" % (pagename
, page
, iinfo
)
70 # write out initialisation of info, for ISACaller to use
71 f
.write(" %s_instrs = {}\n" % pagename
)
74 if __name__
== '__main__':
76 isa
.write_pysource('sprset')
77 #isa.write_pysource('system')
79 isa
.write_pysource('stringldst')
80 isa
.write_pysource('fixedshift')
81 isa
.write_pysource('condition')
82 isa
.write_pysource('fixedtrap')
83 isa
.write_pysource('branch')
84 isa
.write_pysource('fixedlogical')
85 isa
.write_pysource('fixedstore')
86 isa
.write_pysource('fixedload')
87 isa
.write_pysource('comparefixed')
88 isa
.write_pysource('fixedarith')