b01fc4cc0a0008f492b35bd1e397f2a1862271b7
1 # python code-writer for OpenPOWER ISA pseudo-code parsing
7 from openpower
.decoder
.pseudo
.pagereader
import ISA
8 from openpower
.decoder
.power_pseudo
import convert_to_python
9 from openpower
.decoder
.orderedset
import OrderedSet
10 from openpower
.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 openpower.decoder.isa.caller import inject, instruction_info
23 from openpower.decoder.helpers import (
24 ne, eq, gt, ge, lt, le, ltu, gtu, length,
28 from openpower.decoder.selectable_int import SelectableInt
29 from openpower.decoder.selectable_int import selectconcat as concat
30 from openpower.decoder.orderedset import OrderedSet
31 from openpower.decoder.isafunctions.all import ISACallerFnHelper
36 from openpower.decoder.helpers import (
38 #FPADD32, FPSUB32, FPMUL32, FPDIV32,
39 FPADD64, FPSUB64, FPMUL64, FPDIV64,
46 iinfo_template
= """instruction_info(func=%s,
48 uninit_regs=%s, write_regs=%s,
49 special_regs=%s, op_fields=%s,
54 class PyISAWriter(ISA
):
57 self
.pages_written
= []
59 def write_pysource(self
, pagename
):
60 self
.pages_written
.append(pagename
)
61 instrs
= self
.page
[pagename
]
62 isadir
= get_isasrc_dir()
63 fname
= os
.path
.join(isadir
, "%s.py" % pagename
)
64 with
open(fname
, "w") as f
:
66 # write headers: FP gets extra imports
67 f
.write(header
) # write out header
68 if (pagename
.startswith("fp") or
69 pagename
.startswith("svfp")):
71 f
.write("class %s:\n" % pagename
)
73 # go through all instructions
76 print("page", pagename
, page
, fname
, d
.opcode
)
77 pcode
= '\n'.join(d
.pcode
) + '\n'
79 incl_carry
= pagename
== 'fixedshift'
80 pycode
, rused
= convert_to_python(pcode
, d
.form
, incl_carry
)
81 # create list of arguments to call
82 regs
= list(rused
['read_regs']) + list(rused
['uninit_regs'])
83 regs
+= list(rused
['special_regs'])
84 args
= ', '.join(create_args(regs
, 'self'))
85 # create list of arguments to return
86 retargs
= ', '.join(create_args(rused
['write_regs']))
87 # write out function. pre-pend "op_" because some instrs are
88 # also python keywords (cmp). also replace "." with "_"
89 op_fname
= "op_%s" % page
.replace(".", "_")
90 f
.write(" @inject()\n")
91 f
.write(" def %s(%s):\n" % (op_fname
, args
))
92 # blech! this works in combination with ISACaller
93 # @inject decorator, which works by injecting
94 # global variables into the function namespace.
95 for blech
in ['NIA', 'end_loop']:
96 if blech
in pycode
: # HACK - TODO fix
97 f
.write(" global %s\n" % blech
)
98 pycode
= pycode
.split("\n")
99 pycode
= '\n'.join(map(lambda x
: " %s" % x
, pycode
))
100 pycode
= pycode
.rstrip()
101 f
.write(pycode
+ '\n')
103 f
.write(" return (%s,)\n\n" % retargs
)
106 # accumulate the instruction info
107 ops
= repr(rused
['op_fields'])
108 iinfo
= iinfo_template
% (op_fname
, rused
['read_regs'],
109 rused
['uninit_regs'],
111 rused
['special_regs'],
113 iinf
+= " %s_instrs['%s'] = %s\n" % (pagename
, page
, iinfo
)
114 # write out initialisation of info, for ISACaller to use
115 f
.write(" %s_instrs = {}\n" % pagename
)
118 def patch_if_needed(self
, source
):
119 isadir
= get_isasrc_dir()
120 fname
= os
.path
.join(isadir
, "%s.py" % source
)
121 patchname
= os
.path
.join(isadir
, "%s.patch" % source
)
124 with
open(patchname
, 'r') as patch
:
125 newfname
= fname
+ '.orig'
126 shutil
.copyfile(fname
, newfname
)
127 subprocess
.check_call(['patch', fname
],
132 def write_isa_class(self
):
133 isadir
= get_isasrc_dir()
134 fname
= os
.path
.join(isadir
, "all.py")
136 with
open(fname
, "w") as f
:
137 f
.write('# auto-generated by pywriter.py: do not edit or commit\n')
138 f
.write('from openpower.decoder.isa.caller import ISACaller\n')
139 f
.write('from openpower.decoder.helpers import ISACallerHelper\n')
140 f
.write('from openpower.decoder.isafunctions.all '
141 'import ISACallerFnHelper\n')
142 for page
in self
.pages_written
:
143 f
.write('from openpower.decoder.isa.%s ' \
144 'import %s\n' % (page
, page
))
147 classes
= ', '.join(['ISACaller', 'ISACallerFnHelper',
148 'ISACallerHelper'] + self
.pages_written
)
149 f
.write('class ISA(%s):\n' % classes
)
150 f
.write(' def __init__(self, *args, **kwargs):\n')
151 f
.write(' super().__init__(*args, **kwargs)\n')
152 f
.write(' self.instrs = {\n')
153 for page
in self
.pages_written
:
154 f
.write(' **self.%s_instrs,\n' % page
)
160 write_isa_class
= True
161 if len(sys
.argv
) == 1: # quick way to do it
163 sources
= isa
.page
.keys()
165 sources
= sys
.argv
[1:]
166 if sources
[0] == "noall": # don't rewrite all.py
167 write_isa_class
= False
169 print ("sources", write_isa_class
, sources
)
170 for source
in sources
:
171 isa
.write_pysource(source
)
172 isa
.patch_if_needed(source
)
174 isa
.write_isa_class()
176 if __name__
== '__main__':