diff options
author | Gregory Szorc <gregory.szorc@gmail.com> | 2012-03-10 05:50:56 +0000 |
---|---|---|
committer | Gregory Szorc <gregory.szorc@gmail.com> | 2012-03-10 05:50:56 +0000 |
commit | b7487d4edc34fa22b697d0874331c163ed2c6847 (patch) | |
tree | f3a31fa08bc4c31318197ccf7fef3d210c4adc36 /bindings/python/llvm/core.py | |
parent | 61e22cd85cd4c84fff391da67018c92bf21a8e19 (diff) | |
download | llvm-b7487d4edc34fa22b697d0874331c163ed2c6847.tar.gz llvm-b7487d4edc34fa22b697d0874331c163ed2c6847.tar.bz2 llvm-b7487d4edc34fa22b697d0874331c163ed2c6847.tar.xz |
[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
Diffstat (limited to 'bindings/python/llvm/core.py')
-rw-r--r-- | bindings/python/llvm/core.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/bindings/python/llvm/core.py b/bindings/python/llvm/core.py index e0f35203ba..6756637425 100644 --- a/bindings/python/llvm/core.py +++ b/bindings/python/llvm/core.py @@ -11,6 +11,8 @@ from .common import LLVMObject from .common import c_object_p from .common import get_library +from . import enumerations + from ctypes import POINTER from ctypes import byref from ctypes import c_char_p @@ -22,6 +24,42 @@ __all__ = [ lib = get_library() +class OpCode(object): + """Represents an individual OpCode enumeration.""" + + _value_map = {} + + def __init__(self, name, value): + self.name = name + self.value = value + + def __repr__(self): + return 'OpCode.%s' % self.name + + @staticmethod + def from_value(value): + """Obtain an OpCode instance from a numeric value.""" + result = OpCode._value_map.get(value, None) + + if result is None: + raise ValueError('Unknown OpCode: %d' % value) + + return result + + @staticmethod + def register(name, value): + """Registers a new OpCode enumeration. + + This is called by this module for each enumeration defined in + enumerations. You should not need to call this outside this module. + """ + if value in OpCode._value_map: + raise ValueError('OpCode value already registered: %d' % value) + + opcode = OpCode(name, value) + OpCode._value_map[value] = opcode + setattr(OpCode, name, opcode) + class MemoryBuffer(LLVMObject): """Represents an opaque memory buffer.""" @@ -52,4 +90,9 @@ def register_library(library): library.LLVMDisposeMemoryBuffer.argtypes = [MemoryBuffer] +def register_enumerations(): + for name, value in enumerations.OpCodes: + OpCode.register(name, value) + register_library(lib) +register_enumerations() |