llvm.org GIT mirror llvm / 0ee9a84
[python-bindings] Added OpCode like support for all enumerations with unittests. Also fixed some trailing whitespace issues. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@201929 91177308-0d34-0410-b5e6-96231b3b80d8 Michael Gottesman 6 years ago
2 changed file(s) with 141 addition(s) and 39 deletion(s). Raw diff Collapse all Expand all
1919
2020 __all__ = [
2121 "lib",
22 "Enums",
2223 "OpCode",
2324 "MemoryBuffer",
2425 "Module",
3132 ]
3233
3334 lib = get_library()
34
35 class OpCode(object):
36 """Represents an individual OpCode enumeration."""
37
38 _value_map = {}
35 Enums = []
36
37 class LLVMEnumeration(object):
38 """Represents an individual LLVM enumeration."""
3939
4040 def __init__(self, name, value):
4141 self.name = name
4242 self.value = value
4343
4444 def __repr__(self):
45 return 'OpCode.%s' % self.name
46
47 @staticmethod
48 def from_value(value):
49 """Obtain an OpCode instance from a numeric value."""
50 result = OpCode._value_map.get(value, None)
45 return '%s.%s' % (self.__class__.__name__,
46 self.name)
47
48 @classmethod
49 def from_value(cls, value):
50 """Obtain an enumeration instance from a numeric value."""
51 result = cls._value_map.get(value, None)
5152
5253 if result is None:
53 raise ValueError('Unknown OpCode: %d' % value)
54 raise ValueError('Unknown %s: %d' % (cls.__name__,
55 value))
5456
5557 return result
5658
57 @staticmethod
58 def register(name, value):
59 """Registers a new OpCode enumeration.
59 @classmethod
60 def register(cls, name, value):
61 """Registers a new enumeration.
6062
6163 This is called by this module for each enumeration defined in
6264 enumerations. You should not need to call this outside this module.
6365 """
64 if value in OpCode._value_map:
65 raise ValueError('OpCode value already registered: %d' % value)
66
67 opcode = OpCode(name, value)
68 OpCode._value_map[value] = opcode
69 setattr(OpCode, name, opcode)
66 if value in cls._value_map:
67 raise ValueError('%s value already registered: %d' % (cls.__name__,
68 value))
69 enum = cls(name, value)
70 cls._value_map[value] = enum
71 setattr(cls, name, enum)
72 #print cls, name, value
73
74 class Attribute(LLVMEnumeration):
75 """Represents an individual Attribute enumeration."""
76
77 _value_map = {}
78
79 def __init__(self, name, value):
80 super(Attribute, self).__init__(name, value)
81
82 class OpCode(LLVMEnumeration):
83 """Represents an individual OpCode enumeration."""
84
85 _value_map = {}
86
87 def __init__(self, name, value):
88 super(OpCode, self).__init__(name, value)
89
90 class TypeKind(LLVMEnumeration):
91 """Represents an individual TypeKind enumeration."""
92
93 _value_map = {}
94
95 def __init__(self, name, value):
96 super(TypeKind, self).__init__(name, value)
97
98 class Linkage(LLVMEnumeration):
99 """Represents an individual Linkage enumeration."""
100
101 _value_map = {}
102
103 def __init__(self, name, value):
104 super(Linkage, self).__init__(name, value)
105
106 class Visibility(LLVMEnumeration):
107 """Represents an individual visibility enumeration."""
108
109 _value_map = {}
110
111 def __init__(self, name, value):
112 super(Visibility, self).__init__(name, value)
113
114 class CallConv(LLVMEnumeration):
115 """Represents an individual calling convention enumeration."""
116
117 _value_map = {}
118
119 def __init__(self, name, value):
120 super(CallConv, self).__init__(name, value)
121
122 class IntPredicate(LLVMEnumeration):
123 """Represents an individual IntPredicate enumeration."""
124
125 _value_map = {}
126
127 def __init__(self, name, value):
128 super(IntPredicate, self).__init__(name, value)
129
130 class RealPredicate(LLVMEnumeration):
131 """Represents an individual RealPredicate enumeration."""
132
133 _value_map = {}
134
135 def __init__(self, name, value):
136 super(RealPredicate, self).__init__(name, value)
137
138 class LandingPadClauseTy(LLVMEnumeration):
139 """Represents an individual LandingPadClauseTy enumeration."""
140
141 _value_map = {}
142
143 def __init__(self, name, value):
144 super(LandingPadClauseTy, self).__init__(name, value)
70145
71146 class MemoryBuffer(LLVMObject):
72147 """Represents an opaque memory buffer."""
515590 library.LLVMGetInstructionOpcode.restype = c_uint
516591
517592 def register_enumerations():
518 for name, value in enumerations.OpCodes:
519 OpCode.register(name, value)
593 if Enums:
594 return None
595 enums = [
596 (Attribute, enumerations.Attributes),
597 (OpCode, enumerations.OpCodes),
598 (TypeKind, enumerations.TypeKinds),
599 (Linkage, enumerations.Linkages),
600 (Visibility, enumerations.Visibility),
601 (CallConv, enumerations.CallConv),
602 (IntPredicate, enumerations.IntPredicate),
603 (RealPredicate, enumerations.RealPredicate),
604 (LandingPadClauseTy, enumerations.LandingPadClauseTy),
605 ]
606 s = set([])
607 for enum_class, enum_spec in enums:
608 for name, value in enum_spec:
609 print name, value
610 enum_class.register(name, value)
611 return enums
520612
521613 def initialize_llvm():
522614 c = Context.GetGlobalContext()
535627 lib.LLVMInitializeTarget(p)
536628
537629 register_library(lib)
538 register_enumerations()
630 Enums = register_enumerations()
539631 initialize_llvm()
0 from .base import TestBase
1 from ..core import OpCode
21 from ..core import MemoryBuffer
32 from ..core import PassRegistry
43 from ..core import Context
54 from ..core import Module
5 from ..core import Enums
6 from ..core import OpCode
67 from ..bit_reader import parse_bitcode
78
89 class TestCore(TestBase):
9 def test_opcode(self):
10 self.assertTrue(hasattr(OpCode, 'Ret'))
11 self.assertTrue(isinstance(OpCode.Ret, OpCode))
12 self.assertEqual(OpCode.Ret.value, 1)
10 def test_enumerations(self):
11 for enum_cls, enum_spec in Enums:
12 for enum_name, enum_value in enum_spec:
13 # First make sure that enum_cls has the name of the enum as an
14 # attribute. People will access these values as
15 # EnumCls.EnumName.
16 self.assertTrue(hasattr(enum_cls, enum_name))
17 v_attr = getattr(enum_cls, enum_name)
18 self.assertTrue(isinstance(v_attr, enum_cls))
1319
14 op = OpCode.from_value(1)
15 self.assertTrue(isinstance(op, OpCode))
16 self.assertEqual(op, OpCode.Ret)
20 # Then make sure that the value returned for this attribute is
21 # correct in both ways.
22 self.assertEqual(v_attr.value, enum_value)
23
24 e = enum_cls.from_value(enum_value)
25 self.assertTrue(isinstance(e, enum_cls))
26 self.assertEqual(e, v_attr)
1727
1828 def test_memory_buffer_create_from_file(self):
1929 source = self.get_test_file()
6070 target = "thumbv7-apple-ios5.0.0"
6171 m.target = target
6272 m.print_module_to_file("test2.ll")
63
73
6474 def test_module_function_iteration(self):
6575 m = parse_bitcode(MemoryBuffer(filename=self.get_test_bc()))
6676 i = 0
8090 def test_function_basicblock_iteration(self):
8191 m = parse_bitcode(MemoryBuffer(filename=self.get_test_bc()))
8292 i = 0
83
93
8494 bb_list = ['b1', 'b2', 'end']
85
95
8696 f = m.first
8797 while f.name != "f6":
8898 f = f.next
89
99
90100 # Forward
91101 for bb in f:
92102 self.assertEqual(bb.name, bb_list[i])
93103 bb.dump()
94104 i += 1
95
105
96106 # Backwards
97107 for bb in reversed(f):
98108 i -= 1
102112 def test_basicblock_instruction_iteration(self):
103113 m = parse_bitcode(MemoryBuffer(filename=self.get_test_bc()))
104114 i = 0
105
115
106116 inst_list = [('arg1', OpCode.ExtractValue),
107117 ('arg2', OpCode.ExtractValue),
108118 ('', OpCode.Call),
109119 ('', OpCode.Ret)]
110
120
111121 bb = m.first.first
112122
113123 # Forward