001c0f0a600dbb634eeee5799dd6962fe0ce1a5c
1 # python code-writer for OpenPOWER ISA pseudo-code parsing
7 from openpower
.decoder
.pseudo
.functionreader
import ISAFunctions
8 from openpower
.decoder
.power_pseudo
import convert_to_pure_python
11 def get_isafn_src_dir():
12 fdir
= os
.path
.abspath(os
.path
.dirname(__file__
))
13 fdir
= os
.path
.split(fdir
)[0]
14 return os
.path
.join(fdir
, "isafunctions")
18 # auto-generated by pyfnwriter.py, do not edit or commit
20 from openpower.decoder.helpers import (ISACallerHelper,
21 ne, eq, gt, ge, lt, le, ltu, gtu, length,
22 trunc_divs, trunc_rems,
24 from openpower.decoder.selectable_int import SelectableInt
25 from openpower.decoder.selectable_int import selectconcat as concat
31 class PyISAFnWriter(ISAFunctions
):
33 ISAFunctions
.__init
__(self
)
34 self
.pages_written
= []
36 def write_pysource(self
, pagename
):
37 self
.pages_written
.append(pagename
)
38 function
= self
.fns
[pagename
]
39 isadir
= get_isafn_src_dir()
40 os
.makedirs(isadir
, exist_ok
=True)
41 fname
= os
.path
.join(isadir
, "%s.py" % pagename
)
43 with
open(fname
, "w") as f
:
44 f
.write(header
% function
['desc']) # write out header
45 # go through all instructions
46 pcode
= function
['pcode']
48 # check if the code has already been compiled
50 if phash
in sourcecache
:
51 pycode
= sourcecache
[phash
]
53 pycode
= convert_to_pure_python(pcode
, True)
54 sourcecache
[phash
] = pycode
57 def write_isa_class(self
):
58 isadir
= get_isafn_src_dir()
59 fname
= os
.path
.join(isadir
, "all.py")
61 with
open(fname
, "w") as f
:
63 f
.write('# auto-generated by pyfnwriter.py: do not edit or commit\n')
64 f
.write('from openpower.decoder.helpers import ISACallerHelper\n')
65 for page
in self
.pages_written
:
66 module
= 'openpower.decoder.isafunctions.' + page
67 helper
= 'ISACallerFnHelper_' + page
68 helpers
.append(helper
)
69 f
.write('from %s import ISACallerFnHelper as %s\n' % (module
, helper
))
72 f
.write('class ISACallerFnHelper(%s):\n' % ', '.join(helpers
+ ['ISACallerHelper']))
78 write_isa_class
= True
79 if len(sys
.argv
) == 1: # quick way to do it
81 sources
= isa
.fns
.keys()
83 sources
= sys
.argv
[1:]
84 if sources
[0] == "noall": # don't rewrite all.py
85 write_isa_class
= False
87 print ("sources", write_isa_class
, sources
)
88 for source
in sources
:
89 isa
.write_pysource(source
)
93 if __name__
== '__main__':