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 special_regs=%s, op_fields=%s,
36 class PyISAWriter(ISA
):
39 self
.pages_written
= []
41 def write_pysource(self
, pagename
):
42 self
.pages_written
.append(pagename
)
43 instrs
= isa
.page
[pagename
]
44 isadir
= get_isasrc_dir()
45 fname
= os
.path
.join(isadir
, "%s.py" % pagename
)
46 with
open(fname
, "w") as f
:
48 f
.write(header
% pagename
) # write out header
49 # go through all instructions
52 print (fname
, d
.opcode
)
53 pcode
= '\n'.join(d
.pcode
) + '\n'
55 pycode
, rused
= convert_to_python(pcode
, d
.form
)
56 # create list of arguments to call
57 regs
= list(rused
['read_regs']) + list(rused
['uninit_regs'])
58 regs
+= list(rused
['special_regs'])
59 args
= ', '.join(create_args(regs
, 'self'))
60 # create list of arguments to return
61 retargs
= ', '.join(create_args(rused
['write_regs']))
62 # write out function. pre-pend "op_" because some instrs are
63 # also python keywords (cmp). also replace "." with "_"
64 op_fname
="op_%s" % page
.replace(".", "_")
65 f
.write(" @inject()\n")
66 f
.write(" def %s(%s):\n" % (op_fname
, args
))
67 pycode
= pycode
.split("\n")
68 pycode
= '\n'.join(map(lambda x
: " %s" % x
, pycode
))
69 pycode
= pycode
.rstrip()
70 f
.write(pycode
+ '\n')
72 f
.write(" return (%s,)\n\n" % retargs
)
75 # accumulate the instruction info
76 ops
= repr(rused
['op_fields'])
77 iinfo
= iinfo_template
% (op_fname
, rused
['read_regs'],
80 rused
['special_regs'],
82 iinf
+= " %s_instrs['%s'] = %s\n" % (pagename
, page
, iinfo
)
83 # write out initialisation of info, for ISACaller to use
84 f
.write(" %s_instrs = {}\n" % pagename
)
87 def write_isa_class(self
):
88 isadir
= get_isasrc_dir()
89 fname
= os
.path
.join(isadir
, "all.py")
91 with
open(fname
, "w") as f
:
92 f
.write('from soc.decoder.isa.caller import ISACaller\n')
93 for page
in self
.pages_written
:
94 f
.write('from soc.decoder.isa.%s import %s\n' % (page
, page
))
97 classes
= ', '.join(['ISACaller'] + self
.pages_written
)
98 f
.write('class ISA(%s):\n' % classes
)
99 f
.write(' def __init__(self, dec, regs):\n')
100 f
.write(' super().__init__(dec, regs)\n')
101 f
.write(' self.instrs = {\n')
102 for page
in self
.pages_written
:
103 f
.write(' **self.%s_instrs,\n' % page
)
107 if __name__
== '__main__':
109 if len(sys
.argv
) == 1: # quick way to do it
111 sources
= isa
.page
.keys()
113 sources
= sys
.argv
[1:]
114 for source
in sources
:
115 isa
.write_pysource(source
)
116 #isa.write_isa_class()