1 # python code-writer for OpenPOWER ISA pseudo-code parsing
7 from soc
.decoder
.pseudo
.pagereader
import ISA
8 from soc
.decoder
.power_pseudo
import convert_to_python
9 from soc
.decoder
.orderedset
import OrderedSet
10 from soc
.decoder
.isa
.caller
import create_args
14 fdir
= os
.path
.abspath(os
.path
.dirname(__file__
))
15 fdir
= os
.path
.split(fdir
)[0]
16 return os
.path
.join(fdir
, "isa")
20 # auto-generated by pywriter.py, do not edit or commit
22 from soc.decoder.isa.caller import inject, instruction_info
23 from soc.decoder.helpers import (EXTS, EXTS64, EXTZ64, ROTL64, ROTL32, MASK,
24 ne, eq, gt, ge, lt, le, ltu, gtu, length,
25 trunc_divs, trunc_rems, MULS, DIVS, MODS)
26 from soc.decoder.selectable_int import SelectableInt
27 from soc.decoder.selectable_int import selectconcat as concat
28 from soc.decoder.orderedset import OrderedSet
34 iinfo_template
= """instruction_info(func=%s,
36 uninit_regs=%s, write_regs=%s,
37 special_regs=%s, op_fields=%s,
42 class PyISAWriter(ISA
):
45 self
.pages_written
= []
47 def write_pysource(self
, pagename
):
48 self
.pages_written
.append(pagename
)
49 instrs
= isa
.page
[pagename
]
50 isadir
= get_isasrc_dir()
51 fname
= os
.path
.join(isadir
, "%s.py" % pagename
)
52 with
open(fname
, "w") as f
:
54 f
.write(header
% pagename
) # write out header
55 # go through all instructions
58 print("page", pagename
, page
, fname
, d
.opcode
)
59 pcode
= '\n'.join(d
.pcode
) + '\n'
61 incl_carry
= pagename
== 'fixedshift'
62 pycode
, rused
= convert_to_python(pcode
, d
.form
, incl_carry
)
63 # create list of arguments to call
64 regs
= list(rused
['read_regs']) + list(rused
['uninit_regs'])
65 regs
+= list(rused
['special_regs'])
66 args
= ', '.join(create_args(regs
, 'self'))
67 # create list of arguments to return
68 retargs
= ', '.join(create_args(rused
['write_regs']))
69 # write out function. pre-pend "op_" because some instrs are
70 # also python keywords (cmp). also replace "." with "_"
71 op_fname
= "op_%s" % page
.replace(".", "_")
72 f
.write(" @inject()\n")
73 f
.write(" def %s(%s):\n" % (op_fname
, args
))
74 if 'NIA' in pycode
: # HACK - TODO fix
75 f
.write(" global NIA\n")
76 pycode
= pycode
.split("\n")
77 pycode
= '\n'.join(map(lambda x
: " %s" % x
, pycode
))
78 pycode
= pycode
.rstrip()
79 f
.write(pycode
+ '\n')
81 f
.write(" return (%s,)\n\n" % retargs
)
84 # accumulate the instruction info
85 ops
= repr(rused
['op_fields'])
86 iinfo
= iinfo_template
% (op_fname
, rused
['read_regs'],
89 rused
['special_regs'],
91 iinf
+= " %s_instrs['%s'] = %s\n" % (pagename
, page
, iinfo
)
92 # write out initialisation of info, for ISACaller to use
93 f
.write(" %s_instrs = {}\n" % pagename
)
96 def patch_if_needed(self
, source
):
97 isadir
= get_isasrc_dir()
98 fname
= os
.path
.join(isadir
, "%s.py" % source
)
99 patchname
= os
.path
.join(isadir
, "%s.patch" % source
)
102 with
open(patchname
, 'r') as patch
:
103 newfname
= fname
+ '.orig'
104 shutil
.copyfile(fname
, newfname
)
105 subprocess
.check_call(['patch', fname
],
110 def write_isa_class(self
):
111 isadir
= get_isasrc_dir()
112 fname
= os
.path
.join(isadir
, "all.py")
114 with
open(fname
, "w") as f
:
115 f
.write('# auto-generated by pywriter.py: do not edit or commit\n')
116 f
.write('from soc.decoder.isa.caller import ISACaller\n')
117 for page
in self
.pages_written
:
118 f
.write('from soc.decoder.isa.%s import %s\n' % (page
, page
))
121 classes
= ', '.join(['ISACaller'] + self
.pages_written
)
122 f
.write('class ISA(%s):\n' % classes
)
123 f
.write(' def __init__(self, *args, **kwargs):\n')
124 f
.write(' super().__init__(*args, **kwargs)\n')
125 f
.write(' self.instrs = {\n')
126 for page
in self
.pages_written
:
127 f
.write(' **self.%s_instrs,\n' % page
)
131 if __name__
== '__main__':
133 if len(sys
.argv
) == 1: # quick way to do it
135 sources
= isa
.page
.keys()
137 sources
= sys
.argv
[1:]
138 for source
in sources
:
139 isa
.write_pysource(source
)
140 isa
.patch_if_needed(source
)
141 isa
.write_isa_class()