a8f9bdabfd4eadc075c297b08d3347374662f687
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
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
= """(%s, %s,
34 class PyISAWriter(ISA
):
37 self
.pages_written
= []
39 def write_pysource(self
, pagename
):
40 self
.pages_written
.append(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
, d
.form
)
54 # create list of arguments to call
55 regs
= list(rused
['read_regs']) + list(rused
['uninit_regs'])
56 args
= ', '.join(create_args(regs
, 'self'))
57 # create list of arguments to return
58 retargs
= ', '.join(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(" @inject()\n")
63 f
.write(" def %s(%s):\n" % (op_fname
, args
))
64 pycode
= pycode
.split("\n")
65 pycode
= '\n'.join(map(lambda x
: " %s" % x
, pycode
))
66 pycode
= pycode
.rstrip()
67 f
.write(pycode
+ '\n')
69 f
.write(" return (%s,)\n\n" % retargs
)
72 # accumulate the instruction info
73 ops
= repr(rused
['op_fields'])
74 iinfo
= iinfo_template
% (op_fname
, rused
['read_regs'],
75 rused
['uninit_regs'], rused
['write_regs'],
77 iinf
+= " %s_instrs['%s'] = %s\n" % (pagename
, page
, iinfo
)
78 # write out initialisation of info, for ISACaller to use
79 f
.write(" %s_instrs = {}\n" % pagename
)
82 def write_isa_class(self
):
83 isadir
= get_isasrc_dir()
84 fname
= os
.path
.join(isadir
, "all.py")
86 with
open(fname
, "w") as f
:
87 f
.write('from soc.decoder.isa.caller import ISACaller\n')
88 for page
in self
.pages_written
:
89 f
.write('from soc.decoder.isa.%s import %s\n' % (page
, page
))
92 classes
= ', '.join(['ISACaller'] + self
.pages_written
)
93 f
.write('class ISA(%s):\n' % classes
)
94 f
.write(' def __init__(self, dec, regs):\n')
95 f
.write(' super().__init__(dec, regs)\n')
96 f
.write(' self.instrs = {\n')
97 for page
in self
.pages_written
:
98 f
.write(' **self.%s_instrs,\n' % page
)
105 if __name__
== '__main__':
107 if len(sys
.argv
) == 1: # quick way to do it
109 sources
= isa
.page
.keys()
111 sources
= sys
.argv
[1:]
112 for source
in sources
:
113 isa
.write_pysource(source
)
114 isa
.write_isa_class()