PyNerva 0.0.7__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.
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 +2465 -0
  8. nervapy/arm/generic.py +10796 -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.7.dist-info/METADATA +310 -0
  72. pynerva-0.0.7.dist-info/RECORD +74 -0
  73. pynerva-0.0.7.dist-info/WHEEL +4 -0
  74. pynerva-0.0.7.dist-info/licenses/LICENSE.rst +15 -0
@@ -0,0 +1,181 @@
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
+
5
+ import six
6
+
7
+
8
+ class SectionFlags:
9
+ # Section contains executable code
10
+ code = 0x00000020
11
+ # Section contains initialized data
12
+ initialized_data = 0x00000040
13
+ # Section contains uninitialized data
14
+ uninitialized_data = 0x00000080
15
+ # Section contains extended relocations
16
+ extended_relocations = 0x01000000
17
+ # Section can be discarded as needed
18
+ discardable = 0x02000000
19
+ # Section can not be cached
20
+ uncached = 0x04000000
21
+ # Section can not be pageable
22
+ unpaged = 0x08000000
23
+ # Section data can be shared between process instances
24
+ shared = 0x10000000
25
+ # Section contains executable data during process execution
26
+ executable = 0x20000000
27
+ # Section contains readable data during process execution
28
+ readable = 0x40000000
29
+ # Section contains writable data during process execution
30
+ writable = 0x80000000
31
+
32
+
33
+ class Section(object):
34
+ header_size = 40
35
+
36
+ _alignment_flag_map = {
37
+ 1: 0x00100000,
38
+ 2: 0x00200000,
39
+ 4: 0x00300000,
40
+ 8: 0x00400000,
41
+ 16: 0x00500000,
42
+ 32: 0x00600000,
43
+ 64: 0x00700000,
44
+ 128: 0x00800000,
45
+ 256: 0x00900000,
46
+ 512: 0x00A00000,
47
+ 1024: 0x00B00000,
48
+ 2048: 0x00C00000,
49
+ 4096: 0x00D00000,
50
+ 8192: 0x00E00000,
51
+ }
52
+
53
+ _flag_alignment_map = {
54
+ flag: alignment for (alignment, flag) in six.iteritems(_alignment_flag_map)
55
+ }
56
+
57
+ _alignment_mask = 0x00F00000
58
+
59
+ def __init__(self, name, flags, alignment=None):
60
+ from nervapy.util import is_uint32
61
+
62
+ if not isinstance(name, str):
63
+ raise TypeError("Section name %s is not a string" % str(name))
64
+ if not is_uint32(flags):
65
+ raise TypeError(
66
+ "Flags %s are not representable as a 32-bit unsigned integer"
67
+ % str(flags)
68
+ )
69
+
70
+ super(Section, self).__init__()
71
+ # Section name
72
+ self.name = name
73
+ # Flags for the section
74
+ self.flags = (flags & ~Section._alignment_mask) | Section._alignment_flag_map[1]
75
+ if alignment is not None:
76
+ self.alignment = alignment
77
+
78
+ self.relocations = list()
79
+ self.content = bytearray()
80
+
81
+ @property
82
+ def content_size(self):
83
+ return len(self.content)
84
+
85
+ @property
86
+ def alignment(self):
87
+ return Section._flag_alignment_map.get(self.flags & Section._alignment_mask, 1)
88
+
89
+ @alignment.setter
90
+ def alignment(self, alignment):
91
+ from nervapy.util import is_int
92
+
93
+ if not is_int(alignment):
94
+ raise TypeError("Alignment %s is not an integer" % str(alignment))
95
+ if alignment < 0:
96
+ raise ValueError("Alignment %d is not a positive integer" % alignment)
97
+ if alignment & (alignment - 1) != 0:
98
+ raise ValueError("Alignment %d is not a power of 2" % alignment)
99
+ if alignment not in Section._alignment_flag_map:
100
+ raise ValueError(
101
+ "Alignment %d exceeds maximum alignment (8192)" % alignment
102
+ )
103
+ self.flags = (
104
+ self.flags & ~Section._alignment_mask
105
+ ) | Section._alignment_flag_map[alignment]
106
+
107
+ def encode_header(
108
+ self, encoder, name_index_map, offset, relocations_offset=None, address=None
109
+ ):
110
+ from nervapy.encoder import Encoder
111
+
112
+ assert isinstance(encoder, Encoder)
113
+ assert isinstance(name_index_map, dict)
114
+
115
+ if address is None:
116
+ address = 0
117
+ if relocations_offset is None:
118
+ relocations_offset = 0
119
+ line_numbers_offset = 0
120
+ line_numbers_count = 0
121
+ try:
122
+ name_8_bytes = encoder.fixed_string(self.name, 8)
123
+ except ValueError:
124
+ name_index = name_index_map[self.name]
125
+ name_8_bytes = encoder.fixed_string("/" + str(name_index), 8)
126
+ return (
127
+ name_8_bytes
128
+ + encoder.uint32(self.content_size)
129
+ + encoder.uint32(address)
130
+ + encoder.uint32(self.content_size)
131
+ + encoder.uint32(offset)
132
+ + encoder.uint32(relocations_offset)
133
+ + encoder.uint32(line_numbers_offset)
134
+ + encoder.uint16(len(self.relocations))
135
+ + encoder.uint16(line_numbers_count)
136
+ + encoder.uint32(self.flags)
137
+ )
138
+
139
+
140
+ class TextSection(Section):
141
+ def __init__(self, name=".text", alignment=None):
142
+ super(TextSection, self).__init__(
143
+ name,
144
+ SectionFlags.code | SectionFlags.readable | SectionFlags.executable,
145
+ alignment,
146
+ )
147
+
148
+
149
+ class ReadOnlyDataSection(Section):
150
+ def __init__(self, name=".rdata", alignment=None):
151
+ super(ReadOnlyDataSection, self).__init__(
152
+ name, SectionFlags.initialized_data | SectionFlags.readable, alignment
153
+ )
154
+
155
+
156
+ class StringTable:
157
+ def __init__(self):
158
+ self._strings = dict()
159
+ self.size = 4
160
+
161
+ def add(self, string):
162
+ import codecs
163
+
164
+ if string in self._strings:
165
+ return self._strings[string]
166
+ else:
167
+ string_index = self.size
168
+ self._strings[string] = string_index
169
+ bytestring = codecs.encode(string, "utf-8")
170
+ self.size += len(bytestring) + 1
171
+ return string_index
172
+
173
+ def encode(self):
174
+ import codecs
175
+
176
+ import nervapy.encoder
177
+
178
+ bytestring = nervapy.encoder.Encoder.uint32le(self.size)
179
+ for string in sorted(self._strings, key=self._strings.get):
180
+ bytestring += codecs.encode(string, "utf8") + b"\x00"
181
+ return bytestring
@@ -0,0 +1,148 @@
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
+
5
+ from enum import IntEnum
6
+
7
+
8
+ class StorageClass(IntEnum):
9
+ """
10
+ External symbol. If section number is undefined (0), value specifies symbol size. Otherwise value specifies offset
11
+ within section.
12
+ """
13
+
14
+ external = 2
15
+
16
+ """Static symbol. value specifies offset within section. value of 0 means that symbol represents section name."""
17
+ static = 3
18
+
19
+ """A function .bf (beginning of function), .ef (end of function) or .lf (lines in function record) """
20
+ function = 101
21
+ """Source-file symbol record. Followed by auxiliary records that name the file"""
22
+ file = 103
23
+
24
+
25
+ class SymbolType(IntEnum):
26
+ non_function = 0
27
+ function = 0x20
28
+
29
+
30
+ class Symbol:
31
+ entry_size = 18
32
+
33
+ def __init__(self):
34
+ # Name of the symbol
35
+ self.name = None
36
+ # Value of the symbol. Interpretation depends on section_index and storage_class
37
+ self.value = None
38
+ # Relevant section
39
+ self.section = None
40
+ # Symbol type. Microsoft tools use only 0x20 (function) or 0x0 (not a function)
41
+ self.symbol_type = None
42
+ # Storage class
43
+ self.storage_class = None
44
+
45
+ def encode_entry(self, encoder, name_index_map, section_index_map):
46
+ from nervapy.encoder import Encoder
47
+
48
+ assert isinstance(encoder, Encoder)
49
+
50
+ try:
51
+ name_8_bytes = encoder.fixed_string(self.name, 8)
52
+ except ValueError:
53
+ name_index = name_index_map[self.name]
54
+ name_8_bytes = encoder.uint32(0) + encoder.uint32(name_index)
55
+ section_index = section_index_map[self.section]
56
+ auxiliary_entries = 0
57
+ return (
58
+ name_8_bytes
59
+ + encoder.uint32(self.value)
60
+ + encoder.uint16(section_index)
61
+ + encoder.uint16(self.symbol_type)
62
+ + encoder.uint8(self.storage_class)
63
+ + encoder.uint8(auxiliary_entries)
64
+ )
65
+
66
+
67
+ class RelocationType(IntEnum):
68
+ # Relocation is ignored
69
+ absolute = 0
70
+
71
+ # 32-bit address
72
+ x86_address32 = 6
73
+ # 32-bit offset relative to image base
74
+ x86_imagebase_offset32 = 7
75
+ # 16-bit section index
76
+ x86_section_index = 10
77
+ # 32-bit offset relative to the section
78
+ x86_section_offset32 = 11
79
+ # CLR token
80
+ x86_clr_token = 12
81
+ # Unsigned 7-bit offset relative to the section
82
+ x86_section_offset7 = 13
83
+ # 32-bit offset relative to the end of relocation
84
+ x86_relocation_offset32 = 14
85
+
86
+ # 64-bit address
87
+ x86_64_address64 = 1
88
+ # 32-bit address
89
+ x86_64_address32 = 2
90
+ # 32-bit offset relative to image base
91
+ x86_64_imagebase_offset32 = 3
92
+ # 32-bit offset relative to the end of relocation
93
+ x86_64_relocation_offset32 = 4
94
+ # 32-bit offset relative to the end of relocation + 1 byte
95
+ x86_64_relocation_plus_1_offset32 = 5
96
+ # 32-bit offset relative to the end of relocation + 2 bytes
97
+ x86_64_relocation_plus_2_offset32 = 6
98
+ # 32-bit offset relative to the end of relocation + 3 bytes
99
+ x86_64_relocation_plus_3_offset32 = 7
100
+ # 32-bit offset relative to the end of relocation + 4 bytes
101
+ x86_64_relocation_plus_4_offset32 = 8
102
+ # 32-bit offset relative to the end of relocation + 5 bytes
103
+ x86_64_relocation_plus_5_offset32 = 9
104
+ # 16-bit section index
105
+ x86_64_section_index = 10
106
+ # 32-bit offset relative to the section
107
+ x86_64_section_offset32 = 11
108
+ # Unsigned 7-bit offset relative to the section
109
+ x86_64_section_offset7 = 12
110
+ # CLR token
111
+ x86_64_clr_token = 13
112
+
113
+
114
+ class Relocation:
115
+ entry_size = 10
116
+
117
+ def __init__(self, type, offset, symbol):
118
+ from nervapy.util import is_int, is_uint32
119
+
120
+ if not isinstance(type, RelocationType):
121
+ raise TypeError(
122
+ "Relocation type %s is not in RelocationType enumeration" % str(type)
123
+ )
124
+ if not is_int(offset):
125
+ raise TypeError("Offset %s is not an integer" % str(offset))
126
+ if not is_uint32(offset):
127
+ raise ValueError(
128
+ "Offset %d can not be represented as a 32-bit unsigned integer" % offset
129
+ )
130
+ if not isinstance(symbol, Symbol):
131
+ raise TypeError("Symbol %s is not an instance of Symbol type" % str(symbol))
132
+
133
+ self.type = type
134
+ self.offset = offset
135
+ self.symbol = symbol
136
+
137
+ def encode_entry(self, encoder, symbol_index_map, section_address=0):
138
+ import nervapy.encoder
139
+
140
+ assert isinstance(encoder, nervapy.encoder.Encoder)
141
+ assert self.symbol in symbol_index_map
142
+
143
+ symbol_index = symbol_index_map[self.symbol]
144
+ return (
145
+ encoder.uint32(section_address + self.offset)
146
+ + encoder.uint32(symbol_index)
147
+ + encoder.uint16(self.type)
148
+ )
nervapy/function.py ADDED
@@ -0,0 +1,136 @@
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
+
5
+ class Argument(object):
6
+ """
7
+ Function argument.
8
+
9
+ An argument must have a C type and a name.
10
+
11
+ :ivar c_type: the type of the argument in C
12
+ :type c_type: :class:`nervapy.c.types.Type`
13
+
14
+ :ivar name: the name of the argument
15
+ :type name: str
16
+ """
17
+
18
+ def __init__(self, c_type, name=None):
19
+ """
20
+ :param nervapy.c.types.Type c_type: the type of the argument in C.
21
+ When Go function is generated, the type is automatically converted to similar Go type.
22
+ Note that the ``short``, ``int``, ``long``, and ``long long`` types do not have an equivalents in Go.
23
+ In particular, C's ``int`` type is not an equivalent of Go's ``int`` type. To get Go's ``int`` and ``uint``
24
+ types use ``ptrdiff_t`` and ``size_t`` correspondingly.
25
+
26
+ :param str name: the name of the argument. If the name is not provided explicitly, PeachPy tries to parse it
27
+ from the caller code. The name must follow the C rules for identifiers:
28
+
29
+ - It can contain only Latin letters, digits, and underscore symbol
30
+ - It can not start with a digit
31
+ - It can not start with double underscore (these names are reserved for PeachPy)
32
+ - Name must be unique among the function arguments
33
+ """
34
+
35
+ from nervapy.c.types import Type
36
+
37
+ if not isinstance(c_type, Type):
38
+ raise TypeError("%s is not a C type" % str(c_type))
39
+ self.c_type = c_type
40
+
41
+ if name is None:
42
+ import inspect
43
+ import re
44
+
45
+ _, _, _, _, caller_lines, _ = inspect.stack()[1]
46
+ if caller_lines is None:
47
+ raise ValueError(
48
+ "Argument name is not specified and the caller context is not available"
49
+ )
50
+ source_line = caller_lines[0].strip()
51
+ match = re.match(
52
+ "(?:\\w+\\.)*(\\w+)\\s*=\\s*(?:\\w+\\.)*Argument\\(.+\\)", source_line
53
+ )
54
+ if match:
55
+ name = match.group(1)
56
+ while name.startswith("_"):
57
+ name = name[1:]
58
+ if name.endswith("argument") or name.endswith("Argument"):
59
+ name = name[: -len("argument")]
60
+ if name.endswith("arg") or name.endswith("Arg"):
61
+ name = name[: -len("arg")]
62
+ while name.endswith("_"):
63
+ name = name[:-1]
64
+ if not name:
65
+ raise ValueError(
66
+ "Argument name is not specified and can not be parsed from the code"
67
+ )
68
+
69
+ from nervapy.name import Name
70
+
71
+ Name.check_name(name)
72
+ self.name = name
73
+
74
+ def __str__(self):
75
+ return str(self.c_type) + " " + self.name
76
+
77
+ def __repr__(self):
78
+ return str(self)
79
+
80
+ def __eq__(self, other):
81
+ return (
82
+ isinstance(other, Argument)
83
+ and self.c_type == other.c_type
84
+ and self.name == other.name
85
+ )
86
+
87
+ def __ne__(self, other):
88
+ return (
89
+ not isinstance(other, Argument)
90
+ or self.c_type != other.c_type
91
+ or self.name != other.name
92
+ )
93
+
94
+ @property
95
+ def is_floating_point(self):
96
+ return self.c_type.is_floating_point
97
+
98
+ @property
99
+ def is_codeunit(self):
100
+ return self.c_type.is_codeunit
101
+
102
+ @property
103
+ def is_integer(self):
104
+ return self.c_type.is_integer
105
+
106
+ @property
107
+ def is_unsigned_integer(self):
108
+ return self.c_type.is_unsigned_integer
109
+
110
+ @property
111
+ def is_signed_integer(self):
112
+ return self.c_type.is_signed_integer
113
+
114
+ @property
115
+ def is_size_integer(self):
116
+ return self.c_type.is_size_integer
117
+
118
+ @property
119
+ def is_pointer_integer(self):
120
+ return self.c_type.is_pointer_integer
121
+
122
+ @property
123
+ def is_pointer(self):
124
+ return self.c_type.is_pointer
125
+
126
+ @property
127
+ def is_vector(self):
128
+ return self.c_type.is_vector
129
+
130
+ @property
131
+ def is_mask(self):
132
+ return self.c_type.is_mask
133
+
134
+ @property
135
+ def size(self):
136
+ return self.c_type.size