1 # python code-writer for OpenPOWER ISA pseudo-code parsing
5 from soc
.decoder
.pseudo
.pagereader
import ISA
6 from soc
.decoder
.power_pseudo
import convert_to_python
7 from soc
.decoder
.orderedset
import OrderedSet
8 from soc
.decoder
.isa
.caller
import create_args
11 fdir
= os
.path
.abspath(os
.path
.dirname(__file__
))
12 fdir
= os
.path
.split(fdir
)[0]
13 return os
.path
.join(fdir
, "isa")
17 # auto-generated by pywriter.py, do not edit or commit
19 from soc.decoder.isa.caller import inject, instruction_info
20 from soc.decoder.helpers import (EXTS, EXTS64, EXTZ64, ROTL64, ROTL32, MASK,)
21 from soc.decoder.selectable_int import SelectableInt
22 from soc.decoder.selectable_int import selectconcat as concat
23 from soc.decoder.orderedset import OrderedSet
29 iinfo_template
= """instruction_info(func=%s,
31 uninit_regs=%s, write_regs=%s,
32 op_fields=%s, form='%s',
35 class PyISAWriter(ISA
):
38 self
.pages_written
= []
40 def write_pysource(self
, pagename
):
41 self
.pages_written
.append(pagename
)
42 instrs
= isa
.page
[pagename
]
43 isadir
= get_isasrc_dir()
44 fname
= os
.path
.join(isadir
, "%s.py" % pagename
)
45 with
open(fname
, "w") as f
:
47 f
.write(header
% pagename
) # write out header
48 # go through all instructions
51 print (fname
, d
.opcode
)
52 pcode
= '\n'.join(d
.pcode
) + '\n'
54 pycode
, rused
= convert_to_python(pcode
, d
.form
)
55 # create list of arguments to call
56 regs
= list(rused
['read_regs']) + list(rused
['uninit_regs'])
57 args
= ', '.join(create_args(regs
, 'self'))
58 # create list of arguments to return
59 retargs
= ', '.join(create_args(rused
['write_regs']))
60 # write out function. pre-pend "op_" because some instrs are
61 # also python keywords (cmp). also replace "." with "_"
62 op_fname
="op_%s" % page
.replace(".", "_")
63 f
.write(" @inject()\n")
64 f
.write(" def %s(%s):\n" % (op_fname
, args
))
65 pycode
= pycode
.split("\n")
66 pycode
= '\n'.join(map(lambda x
: " %s" % x
, pycode
))
67 pycode
= pycode
.rstrip()
68 f
.write(pycode
+ '\n')
70 f
.write(" return (%s,)\n\n" % retargs
)
73 # accumulate the instruction info
74 ops
= repr(rused
['op_fields'])
75 iinfo
= iinfo_template
% (op_fname
, rused
['read_regs'],
79 iinf
+= " %s_instrs['%s'] = %s\n" % (pagename
, page
, iinfo
)
80 # write out initialisation of info, for ISACaller to use
81 f
.write(" %s_instrs = {}\n" % pagename
)
84 def write_isa_class(self
):
85 isadir
= get_isasrc_dir()
86 fname
= os
.path
.join(isadir
, "all.py")
88 with
open(fname
, "w") as f
:
89 f
.write('from soc.decoder.isa.caller import ISACaller\n')
90 for page
in self
.pages_written
:
91 f
.write('from soc.decoder.isa.%s import %s\n' % (page
, page
))
94 classes
= ', '.join(['ISACaller'] + self
.pages_written
)
95 f
.write('class ISA(%s):\n' % classes
)
96 f
.write(' def __init__(self, dec, regs):\n')
97 f
.write(' super().__init__(dec, regs)\n')
98 f
.write(' self.instrs = {\n')
99 for page
in self
.pages_written
:
100 f
.write(' **self.%s_instrs,\n' % page
)
104 if __name__
== '__main__':
106 if len(sys
.argv
) == 1: # quick way to do it
108 sources
= isa
.page
.keys()
110 sources
= sys
.argv
[1:]
111 for source
in sources
:
112 isa
.write_pysource(source
)
113 isa
.write_isa_class()