llvm.org GIT mirror llvm / b7487d4
[llvm.py] Define enumerations from Core.h; add OpCode class git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@152483 91177308-0d34-0410-b5e6-96231b3b80d8 Gregory Szorc 7 years ago
3 changed file(s) with 265 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
1010 from .common import c_object_p
1111 from .common import get_library
1212
13 from . import enumerations
14
1315 from ctypes import POINTER
1416 from ctypes import byref
1517 from ctypes import c_char_p
2022 ]
2123
2224 lib = get_library()
25
26 class OpCode(object):
27 """Represents an individual OpCode enumeration."""
28
29 _value_map = {}
30
31 def __init__(self, name, value):
32 self.name = name
33 self.value = value
34
35 def __repr__(self):
36 return 'OpCode.%s' % self.name
37
38 @staticmethod
39 def from_value(value):
40 """Obtain an OpCode instance from a numeric value."""
41 result = OpCode._value_map.get(value, None)
42
43 if result is None:
44 raise ValueError('Unknown OpCode: %d' % value)
45
46 return result
47
48 @staticmethod
49 def register(name, value):
50 """Registers a new OpCode enumeration.
51
52 This is called by this module for each enumeration defined in
53 enumerations. You should not need to call this outside this module.
54 """
55 if value in OpCode._value_map:
56 raise ValueError('OpCode value already registered: %d' % value)
57
58 opcode = OpCode(name, value)
59 OpCode._value_map[value] = opcode
60 setattr(OpCode, name, opcode)
2361
2462 class MemoryBuffer(LLVMObject):
2563 """Represents an opaque memory buffer."""
5189
5290 library.LLVMDisposeMemoryBuffer.argtypes = [MemoryBuffer]
5391
92 def register_enumerations():
93 for name, value in enumerations.OpCodes:
94 OpCode.register(name, value)
95
5496 register_library(lib)
97 register_enumerations()
0 #===- enumerations.py - Python LLVM Enumerations -------------*- python -*--===#
1 #
2 # The LLVM Compiler Infrastructure
3 #
4 # This file is distributed under the University of Illinois Open Source
5 # License. See LICENSE.TXT for details.
6 #
7 #===------------------------------------------------------------------------===#
8
9 r"""
10 LLVM Enumerations
11 =================
12
13 This file defines enumerations from LLVM.
14
15 Each enumeration is exposed as a list of 2-tuples. These lists are consumed by
16 dedicated types elsewhere in the package. The enumerations are centrally
17 defined in this file so they are easier to locate and maintain.
18 """
19
20 __all__ = [
21 'Attributes',
22 'OpCodes',
23 'TypeKinds',
24 'Linkages',
25 'Visibility',
26 'CallConv',
27 'IntPredicate',
28 'RealPredicate',
29 'LandingPadClauseTy',
30 ]
31
32 Attributes = [
33 ('ZExt', 1 << 0),
34 ('MSExt', 1 << 1),
35 ('NoReturn', 1 << 2),
36 ('InReg', 1 << 3),
37 ('StructRet', 1 << 4),
38 ('NoUnwind', 1 << 5),
39 ('NoAlias', 1 << 6),
40 ('ByVal', 1 << 7),
41 ('Nest', 1 << 8),
42 ('ReadNone', 1 << 9),
43 ('ReadOnly', 1 << 10),
44 ('NoInline', 1 << 11),
45 ('AlwaysInline', 1 << 12),
46 ('OptimizeForSize', 1 << 13),
47 ('StackProtect', 1 << 14),
48 ('StackProtectReq', 1 << 15),
49 ('Alignment', 31 << 16),
50 ('NoCapture', 1 << 21),
51 ('NoRedZone', 1 << 22),
52 ('ImplicitFloat', 1 << 23),
53 ('Naked', 1 << 24),
54 ('InlineHint', 1 << 25),
55 ('StackAlignment', 7 << 26),
56 ('ReturnsTwice', 1 << 29),
57 ('UWTable', 1 << 30),
58 ('NonLazyBind', 1 << 31),
59 ]
60
61 OpCodes = [
62 ('Ret', 1),
63 ('Br', 2),
64 ('Switch', 3),
65 ('IndirectBr', 4),
66 ('Invoke', 5),
67 ('Unreachable', 7),
68 ('Add', 8),
69 ('FAdd', 9),
70 ('Sub', 10),
71 ('FSub', 11),
72 ('Mul', 12),
73 ('FMul', 13),
74 ('UDiv', 14),
75 ('SDiv', 15),
76 ('FDiv', 16),
77 ('URem', 17),
78 ('SRem', 18),
79 ('FRem', 19),
80 ('Shl', 20),
81 ('LShr', 21),
82 ('AShr', 22),
83 ('And', 23),
84 ('Or', 24),
85 ('Xor', 25),
86 ('Alloca', 26),
87 ('Load', 27),
88 ('Store', 28),
89 ('GetElementPtr', 29),
90 ('Trunc', 30),
91 ('ZExt', 31),
92 ('SExt', 32),
93 ('FPToUI', 33),
94 ('FPToSI', 34),
95 ('UIToFP', 35),
96 ('SIToFP', 36),
97 ('FPTrunc', 37),
98 ('FPExt', 38),
99 ('PtrToInt', 39),
100 ('IntToPtr', 40),
101 ('BitCast', 41),
102 ('ICmp', 42),
103 ('FCmpl', 43),
104 ('PHI', 44),
105 ('Call', 45),
106 ('Select', 46),
107 ('UserOp1', 47),
108 ('UserOp2', 48),
109 ('AArg', 49),
110 ('ExtractElement', 50),
111 ('InsertElement', 51),
112 ('ShuffleVector', 52),
113 ('ExtractValue', 53),
114 ('InsertValue', 54),
115 ('Fence', 55),
116 ('AtomicCmpXchg', 56),
117 ('AtomicRMW', 57),
118 ('Resume', 58),
119 ('LandingPad', 59),
120 ]
121
122 TypeKinds = [
123 ('Void', 0),
124 ('Half', 1),
125 ('Float', 2),
126 ('Double', 3),
127 ('X86_FP80', 4),
128 ('FP128', 5),
129 ('PPC_FP128', 6),
130 ('Label', 7),
131 ('Integer', 8),
132 ('Function', 9),
133 ('Struct', 10),
134 ('Array', 11),
135 ('Pointer', 12),
136 ('Vector', 13),
137 ('Metadata', 14),
138 ('X86_MMX', 15),
139 ]
140
141 Linkages = [
142 ('External', 0),
143 ('AvailableExternally', 1),
144 ('LinkOnceAny', 2),
145 ('LinkOnceODR', 3),
146 ('WeakAny', 4),
147 ('WeakODR', 5),
148 ('Appending', 6),
149 ('Internal', 7),
150 ('Private', 8),
151 ('DLLImport', 9),
152 ('DLLExport', 10),
153 ('ExternalWeak', 11),
154 ('Ghost', 12),
155 ('Common', 13),
156 ('LinkerPrivate', 14),
157 ('LinkerPrivateWeak', 15),
158 ('LinkerPrivateWeakDefAuto', 16),
159 ]
160
161 Visibility = [
162 ('Default', 0),
163 ('Hidden', 1),
164 ('Protected', 2),
165 ]
166
167 CallConv = [
168 ('CCall', 0),
169 ('FastCall', 8),
170 ('ColdCall', 9),
171 ('X86StdcallCall', 64),
172 ('X86FastcallCall', 65),
173 ]
174
175 IntPredicate = [
176 ('EQ', 32),
177 ('NE', 33),
178 ('UGT', 34),
179 ('UGE', 35),
180 ('ULT', 36),
181 ('ULE', 37),
182 ('SGT', 38),
183 ('SGE', 39),
184 ('SLT', 40),
185 ('SLE', 41),
186 ]
187
188 RealPredicate = [
189 ('PredicateFalse', 0),
190 ('OEQ', 1),
191 ('OGT', 2),
192 ('OGE', 3),
193 ('OLT', 4),
194 ('OLE', 5),
195 ('ONE', 6),
196 ('ORD', 7),
197 ('UNO', 8),
198 ('UEQ', 9),
199 ('UGT', 10),
200 ('UGE', 11),
201 ('ULT', 12),
202 ('ULE', 13),
203 ('UNE', 14),
204 ('PredicateTrue', 15),
205 ]
206
207 LandingPadClauseTy = [
208 ('Catch', 0),
209 ('Filter', 1),
210 ]
None from llvm.core import MemoryBuffer
1
2 from .base import TestBase
1 from ..core import OpCode
2 from ..core import MemoryBuffer
33
44 class TestCore(TestBase):
5 def test_opcode(self):
6 self.assertTrue(hasattr(OpCode, 'Ret'))
7 self.assertTrue(isinstance(OpCode.Ret, OpCode))
8 self.assertEqual(OpCode.Ret.value, 1)
9
10 op = OpCode.from_value(1)
11 self.assertTrue(isinstance(op, OpCode))
12 self.assertEqual(op, OpCode.Ret)
13
514 def test_memory_buffer_create_from_file(self):
615 source = self.get_test_binary()
716