PyNerva 0.0.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of PyNerva might be problematic. Click here for more details.

Files changed (74) hide show
  1. nervapy/__init__.py +50 -0
  2. nervapy/abi.py +91 -0
  3. nervapy/arm/__init__.py +124 -0
  4. nervapy/arm/__main__.py +0 -0
  5. nervapy/arm/abi.py +138 -0
  6. nervapy/arm/formats.py +49 -0
  7. nervapy/arm/function.py +2405 -0
  8. nervapy/arm/generic.py +10797 -0
  9. nervapy/arm/instructions.py +519 -0
  10. nervapy/arm/isa.py +409 -0
  11. nervapy/arm/literal_pool.py +331 -0
  12. nervapy/arm/microarchitecture.py +211 -0
  13. nervapy/arm/pseudo.py +652 -0
  14. nervapy/arm/registers.py +1458 -0
  15. nervapy/arm/vfpneon.py +4092 -0
  16. nervapy/arm.py +13 -0
  17. nervapy/c/__init__.py +1 -0
  18. nervapy/c/types.py +436 -0
  19. nervapy/codegen.py +99 -0
  20. nervapy/common/__init__.py +4 -0
  21. nervapy/common/function.py +5 -0
  22. nervapy/common/regalloc.py +121 -0
  23. nervapy/constant_data.py +282 -0
  24. nervapy/encoder.py +246 -0
  25. nervapy/formats/__init__.py +2 -0
  26. nervapy/formats/elf/__init__.py +4 -0
  27. nervapy/formats/elf/file.py +178 -0
  28. nervapy/formats/elf/image.py +106 -0
  29. nervapy/formats/elf/section.py +422 -0
  30. nervapy/formats/elf/symbol.py +281 -0
  31. nervapy/formats/macho/__init__.py +2 -0
  32. nervapy/formats/macho/file.py +123 -0
  33. nervapy/formats/macho/image.py +143 -0
  34. nervapy/formats/macho/section.py +322 -0
  35. nervapy/formats/macho/symbol.py +158 -0
  36. nervapy/formats/mscoff/__init__.py +8 -0
  37. nervapy/formats/mscoff/image.py +132 -0
  38. nervapy/formats/mscoff/section.py +181 -0
  39. nervapy/formats/mscoff/symbol.py +148 -0
  40. nervapy/function.py +136 -0
  41. nervapy/literal.py +731 -0
  42. nervapy/loader.py +188 -0
  43. nervapy/name.py +159 -0
  44. nervapy/parse.py +52 -0
  45. nervapy/stream.py +58 -0
  46. nervapy/util.py +126 -0
  47. nervapy/writer.py +518 -0
  48. nervapy/x86_64/__init__.py +324 -0
  49. nervapy/x86_64/__main__.py +407 -0
  50. nervapy/x86_64/abi.py +517 -0
  51. nervapy/x86_64/amd.py +6464 -0
  52. nervapy/x86_64/avx.py +102029 -0
  53. nervapy/x86_64/crypto.py +1533 -0
  54. nervapy/x86_64/encoding.py +424 -0
  55. nervapy/x86_64/fma.py +19138 -0
  56. nervapy/x86_64/function.py +2707 -0
  57. nervapy/x86_64/generic.py +23384 -0
  58. nervapy/x86_64/instructions.py +500 -0
  59. nervapy/x86_64/isa.py +476 -0
  60. nervapy/x86_64/lower.py +126 -0
  61. nervapy/x86_64/mask.py +2593 -0
  62. nervapy/x86_64/meta.py +143 -0
  63. nervapy/x86_64/mmxsse.py +17265 -0
  64. nervapy/x86_64/nacl.py +327 -0
  65. nervapy/x86_64/operand.py +1204 -0
  66. nervapy/x86_64/options.py +21 -0
  67. nervapy/x86_64/pseudo.py +686 -0
  68. nervapy/x86_64/registers.py +1225 -0
  69. nervapy/x86_64/types.py +17 -0
  70. nervapy/x86_64/uarch.py +580 -0
  71. pynerva-0.0.5.dist-info/METADATA +310 -0
  72. pynerva-0.0.5.dist-info/RECORD +74 -0
  73. pynerva-0.0.5.dist-info/WHEEL +4 -0
  74. pynerva-0.0.5.dist-info/licenses/LICENSE.rst +15 -0
@@ -0,0 +1,407 @@
1
+ # This file is part of PeachPy package and is licensed under the Simplified BSD license.
2
+ # See license.rst for the full text of the license.
3
+
4
+ from __future__ import absolute_import
5
+
6
+ import argparse
7
+ import sys
8
+
9
+ import six
10
+
11
+ from nervapy import *
12
+ from nervapy.x86_64 import *
13
+
14
+ parser = argparse.ArgumentParser(
15
+ description="PeachPy: Portable Efficient Assembly Code-generation in High-level Python"
16
+ )
17
+ parser.add_argument(
18
+ "-g", dest="debug_level", type=int, default=0, help="Debug information level"
19
+ )
20
+ parser.add_argument(
21
+ "-S",
22
+ dest="generate_assembly",
23
+ action="store_true",
24
+ help="Generate assembly listing on output",
25
+ )
26
+ parser.add_argument(
27
+ "-MMD",
28
+ dest="generate_dependencies_makefile",
29
+ action="store_true",
30
+ help="Generate Makefile describing the dependencies",
31
+ )
32
+ parser.add_argument(
33
+ "-MF",
34
+ dest="dependencies_makefile_path",
35
+ help="Path to output Makefile with dependencies",
36
+ )
37
+ parser.add_argument(
38
+ "-I",
39
+ dest="include",
40
+ action="append",
41
+ default=list(),
42
+ help="Add directory to module search path",
43
+ )
44
+ parser.add_argument(
45
+ "-fdump-rtl", dest="rtl_dump", help="Path to output file for RTL dump"
46
+ )
47
+ parser.add_argument(
48
+ "-emit-json-metadata",
49
+ dest="json_metadata_file",
50
+ help="Path to output file for JSON metadata",
51
+ )
52
+ parser.add_argument(
53
+ "-emit-c-header", dest="c_header_file", help="Path to output file for C/C++ header"
54
+ )
55
+ parser.add_argument(
56
+ "-fname-mangling", dest="name_mangling", help="Mangling of function names"
57
+ )
58
+
59
+
60
+ abi_map = {
61
+ "ms": (nervapy.x86_64.abi.microsoft_x64_abi, ["masm", "nasm"], ["ms-coff"]),
62
+ "sysv": (
63
+ nervapy.x86_64.abi.system_v_x86_64_abi,
64
+ ["gas", "nasm"],
65
+ ["elf", "mach-o"],
66
+ ),
67
+ "x32": (nervapy.x86_64.abi.linux_x32_abi, ["gas"], ["elf"]),
68
+ "nacl": (nervapy.x86_64.abi.native_client_x86_64_abi, ["gas"], ["elf"]),
69
+ "gosyso": (
70
+ nervapy.x86_64.abi.gosyso_amd64_abi,
71
+ ["gas"],
72
+ ["elf", "mach-o", "ms-coff"],
73
+ ),
74
+ "goasm": (nervapy.x86_64.abi.goasm_amd64_abi, ["go"], []),
75
+ "gosyso-p32": (
76
+ nervapy.x86_64.abi.gosyso_amd64p32_abi,
77
+ ["gas"],
78
+ ["elf", "mach-o", "ms-coff"],
79
+ ),
80
+ "goasm-p32": (nervapy.x86_64.abi.goasm_amd64p32_abi, ["go"], []),
81
+ }
82
+ parser.add_argument(
83
+ "-mabi",
84
+ dest="abi",
85
+ default="native",
86
+ choices=(
87
+ "native",
88
+ "ms",
89
+ "sysv",
90
+ "x32",
91
+ "nacl",
92
+ "gosyso",
93
+ "gosyso-p32",
94
+ "goasm",
95
+ "goasm-p32",
96
+ ),
97
+ help="Generate code for specified ABI",
98
+ )
99
+
100
+ cpu_map = {
101
+ "default": nervapy.x86_64.uarch.default,
102
+ "prescott": nervapy.x86_64.uarch.prescott,
103
+ "conroe": nervapy.x86_64.uarch.conroe,
104
+ "penryn": nervapy.x86_64.uarch.penryn,
105
+ "nehalem": nervapy.x86_64.uarch.nehalem,
106
+ "sandybridge": nervapy.x86_64.uarch.sandy_bridge,
107
+ "ivybridge": nervapy.x86_64.uarch.ivy_bridge,
108
+ "haswell": nervapy.x86_64.uarch.haswell,
109
+ "broadwell": nervapy.x86_64.uarch.broadwell,
110
+ "skylake": nervapy.x86_64.uarch.skylake,
111
+ "skylake-xeon": nervapy.x86_64.uarch.skylake_xeon,
112
+ "cannonlake": nervapy.x86_64.uarch.cannonlake,
113
+ "k8": nervapy.x86_64.uarch.k8,
114
+ "k10": nervapy.x86_64.uarch.k10,
115
+ "bulldozer": nervapy.x86_64.uarch.bulldozer,
116
+ "piledriver": nervapy.x86_64.uarch.piledriver,
117
+ "steamroller": nervapy.x86_64.uarch.steamroller,
118
+ "excavator": nervapy.x86_64.uarch.excavator,
119
+ "zen": nervapy.x86_64.uarch.zen,
120
+ "bonnell": nervapy.x86_64.uarch.bonnell,
121
+ "saltwell": nervapy.x86_64.uarch.saltwell,
122
+ "silvermont": nervapy.x86_64.uarch.silvermont,
123
+ "airmont": nervapy.x86_64.uarch.airmont,
124
+ "goldmont": nervapy.x86_64.uarch.goldmont,
125
+ "bobcat": nervapy.x86_64.uarch.bobcat,
126
+ "jaguar": nervapy.x86_64.uarch.jaguar,
127
+ "knightslanding": nervapy.x86_64.uarch.knights_landing,
128
+ }
129
+ parser.add_argument(
130
+ "-mcpu",
131
+ dest="cpu",
132
+ default="default",
133
+ choices=(
134
+ "default",
135
+ "prescott",
136
+ "conroe",
137
+ "penryn",
138
+ "nehalem",
139
+ "sandybridge",
140
+ "ivybridge",
141
+ "haswell",
142
+ "broadwell",
143
+ "skylake",
144
+ "skylake-xeon",
145
+ "cannonlake",
146
+ "k8",
147
+ "k10",
148
+ "bulldozer",
149
+ "piledriver",
150
+ "steamroller",
151
+ "excavator",
152
+ "zen",
153
+ "bonnell",
154
+ "saltwell",
155
+ "silvermont",
156
+ "airmont",
157
+ "goldmont",
158
+ "bobcat",
159
+ "jaguar",
160
+ "knightslanding",
161
+ ),
162
+ help="Target specified microarchitecture",
163
+ )
164
+
165
+ parser.add_argument(
166
+ "-mimage-format",
167
+ dest="image_format",
168
+ default="native",
169
+ choices=("native", "elf", "mach-o", "ms-coff"),
170
+ help="Target binary image format",
171
+ )
172
+ parser.add_argument(
173
+ "-massembly-format",
174
+ dest="assembly_format",
175
+ choices=("golang", "nasm", "gas", "masm"),
176
+ help="Target assembly format",
177
+ )
178
+
179
+ parser.add_argument(
180
+ "-fpackage",
181
+ dest="package",
182
+ default="",
183
+ help="Use specified Go package name in generated Plan 9 assembly listings",
184
+ )
185
+ avx_group = parser.add_mutually_exclusive_group()
186
+ avx_group.add_argument(
187
+ "-mavx", dest="avx", action="store_true", help="Enable AVX extension"
188
+ )
189
+ avx_group.add_argument(
190
+ "-mno-avx", dest="avx", action="store_false", help="Disable AVX extension"
191
+ )
192
+ xop_group = parser.add_mutually_exclusive_group()
193
+ xop_group.add_argument(
194
+ "-mxop", dest="xop", action="store_true", help="Enable XOP extension"
195
+ )
196
+ xop_group.add_argument(
197
+ "-mno-xop", dest="xop", action="store_false", help="Disable XOP extension"
198
+ )
199
+ fma4_group = parser.add_mutually_exclusive_group()
200
+ fma4_group.add_argument(
201
+ "-mfma4", dest="fma4", action="store_true", help="Enable FMA4 extension"
202
+ )
203
+ fma4_group.add_argument(
204
+ "-mno-fma4", dest="fma4", action="store_false", help="Disable FMA4 extension"
205
+ )
206
+ fma3_group = parser.add_mutually_exclusive_group()
207
+ fma3_group.add_argument(
208
+ "-mfma3", dest="fma3", action="store_true", help="Enable FMA3 extension"
209
+ )
210
+ fma3_group.add_argument(
211
+ "-mno-fma3", dest="fma3", action="store_false", help="Disable FMA3 extension"
212
+ )
213
+ f16c_group = parser.add_mutually_exclusive_group()
214
+ f16c_group.add_argument(
215
+ "-mf16c", dest="f16c", action="store_true", help="Enable F16C extension"
216
+ )
217
+ f16c_group.add_argument(
218
+ "-mno-f16c", dest="f16c", action="store_false", help="Disable F16C extension"
219
+ )
220
+ avx2_group = parser.add_mutually_exclusive_group()
221
+ avx2_group.add_argument(
222
+ "-mavx2", dest="avx2", action="store_true", help="Enable AVX2 extension"
223
+ )
224
+ avx2_group.add_argument(
225
+ "-mno-avx2", dest="avx2", action="store_false", help="Disable AVX2 extension"
226
+ )
227
+ parser.add_argument(
228
+ "-o",
229
+ dest="output",
230
+ required=True,
231
+ help="Output file name (ELF/Mach-O/COFF image or Go assembly source)",
232
+ )
233
+ parser.add_argument(
234
+ "input", nargs=1, help="Input file name (must be a PeachPy Python script)"
235
+ )
236
+
237
+
238
+ def guess_assembly_format_from_abi(abi):
239
+ _, supported_assembly_formats, _ = abi_map[abi]
240
+ return supported_assembly_formats[0]
241
+
242
+
243
+ def check_abi_assembly_format_combination(abi, assembly_format):
244
+ _, supported_assembly_formats, _ = abi_map[abi]
245
+ if assembly_format not in supported_assembly_formats:
246
+ raise ValueError(
247
+ "Assembly format %s is not supported for %s" % (assembly_format, str(abi))
248
+ )
249
+
250
+
251
+ def check_abi_image_format_combination(image_format, abi):
252
+ _, _, supported_image_formats = abi_map[abi]
253
+ if image_format not in supported_image_formats:
254
+ raise ValueError(
255
+ "Image format %s is not supported for %s" % (image_format, str(abi))
256
+ )
257
+
258
+
259
+ def detect_native_image_format():
260
+ import platform
261
+
262
+ osname = platform.system()
263
+ if osname == "Darwin":
264
+ return "mach-o"
265
+ elif osname in ["Linux", "NaCl", "FreeBSD"]:
266
+ return "elf"
267
+ elif osname == "Windows":
268
+ return "ms-coff"
269
+
270
+
271
+ def add_module_files(module_files, module, roots):
272
+ """Recursively adds Python source files for module and its submodules inside the roots directories"""
273
+ if not hasattr(module, "__file__"):
274
+ return
275
+
276
+ module_file = module.__file__
277
+ if module_file is None:
278
+ return
279
+
280
+ import os
281
+
282
+ if not any(module_file.startswith(root + os.sep) for root in roots):
283
+ # The file is not inside any of the monitored roots
284
+ # This is typical for system modules
285
+ return
286
+
287
+ if module_file.endswith(".pyc") or module_file.endswith(".pyo"):
288
+ module_source_file = module_file[:-4] + ".py"
289
+ if os.path.isfile(module_source_file):
290
+ module_file = module_source_file
291
+
292
+ if module_file in module_files:
293
+ # This module was already added under a different name
294
+ return
295
+ module_files.add(module_file)
296
+
297
+ from types import ModuleType
298
+
299
+ for variable_name in dir(module):
300
+ if variable_name.startswith("__"):
301
+ continue
302
+
303
+ variable = getattr(module, variable_name)
304
+ if isinstance(variable, ModuleType):
305
+ add_module_files(module_files, variable, roots)
306
+
307
+
308
+ def execute_script(writers, source_filename):
309
+ if writers:
310
+ writer = writers.pop()
311
+ with writer:
312
+ execute_script(writers, source_filename)
313
+ else:
314
+ with open(source_filename) as input_file:
315
+ code = compile(input_file.read(), source_filename, "exec")
316
+ exec(code, globals())
317
+
318
+
319
+ def main():
320
+ options = parser.parse_args()
321
+ import nervapy.x86_64.options
322
+
323
+ nervapy.x86_64.options.debug_level = options.debug_level
324
+ if options.abi == "native":
325
+ abi = nervapy.x86_64.abi.detect(system_abi=True)
326
+ if abi is None:
327
+ raise ValueError("Could not auto-detect ABI: specify it with -mabi option")
328
+ # Set options.abi to the corresponding string value because it is used later on
329
+ options.abi = {abi: name for name, (abi, _, _) in six.iteritems(abi_map)}[abi]
330
+ else:
331
+ abi, _, _ = abi_map[options.abi]
332
+ nervapy.x86_64.options.abi = abi
333
+ nervapy.x86_64.options.target = cpu_map[options.cpu]
334
+ nervapy.x86_64.options.package = options.package
335
+ nervapy.x86_64.options.generate_assembly = options.generate_assembly
336
+ if options.name_mangling:
337
+ nervapy.x86_64.options.name_mangling = options.name_mangling
338
+
339
+ from nervapy.writer import (AssemblyWriter, CHeaderWriter, ELFWriter,
340
+ JSONMetadataWriter, MachOWriter, MSCOFFWriter)
341
+
342
+ writers = []
343
+ if nervapy.x86_64.options.generate_assembly:
344
+ assembly_format = options.assembly_format
345
+ if assembly_format is None:
346
+ assembly_format = guess_assembly_format_from_abi(options.abi)
347
+ else:
348
+ check_abi_assembly_format_combination(options.abi, assembly_format)
349
+ writers.append(
350
+ AssemblyWriter(options.output, assembly_format, options.input[0])
351
+ )
352
+ else:
353
+ image_format = options.image_format
354
+ if image_format == "native":
355
+ image_format = detect_native_image_format()
356
+ if image_format is None:
357
+ raise ValueError(
358
+ "Could not auto-detect image format: specify it with -mimage-format option"
359
+ )
360
+ check_abi_image_format_combination(image_format, options.abi)
361
+ if image_format == "elf":
362
+ writers.append(ELFWriter(options.output, abi, options.input[0]))
363
+ elif image_format == "mach-o":
364
+ writers.append(MachOWriter(options.output, abi))
365
+ elif image_format == "ms-coff":
366
+ writers.append(MSCOFFWriter(options.output, abi, options.input[0]))
367
+ else:
368
+ raise ValueError("Image format %s is not supported" % image_format)
369
+ dependencies_makefile_path = options.output + ".d"
370
+ if options.dependencies_makefile_path:
371
+ dependencies_makefile_path = options.dependencies_makefile_path
372
+ if options.rtl_dump:
373
+ nervapy.x86_64.options.rtl_dump_file = open(options.rtl_dump, "w")
374
+ if options.c_header_file:
375
+ writers.append(CHeaderWriter(options.c_header_file, options.input[0]))
376
+ if options.json_metadata_file:
377
+ writers.append(JSONMetadataWriter(options.json_metadata_file))
378
+
379
+ # PeachPy sources can import other modules or files from the same directory
380
+ import os
381
+
382
+ include_directories = [
383
+ os.path.abspath(include_dir) for include_dir in options.include
384
+ ]
385
+ include_directories.insert(0, os.path.abspath(os.path.dirname(options.input[0])))
386
+ sys.path.extend(include_directories)
387
+
388
+ # We would like to avoid situations where source file has changed, but Python uses its old precompiled version
389
+ sys.dont_write_bytecode = True
390
+
391
+ execute_script(writers, options.input[0])
392
+
393
+ if options.generate_dependencies_makefile:
394
+ module_files = set()
395
+ for module in sys.modules.values():
396
+ add_module_files(module_files, module, include_directories)
397
+
398
+ dependencies = list(sorted(module_files))
399
+ dependencies.insert(0, options.input[0])
400
+ with open(dependencies_makefile_path, "w") as dependencies_makefile:
401
+ dependencies_makefile.write(
402
+ options.output + ": \\\n " + " \\\n ".join(dependencies) + "\n"
403
+ )
404
+
405
+
406
+ if __name__ == "__main__":
407
+ main()