[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
This commit is contained in:
Gregory Szorc
2012-03-10 05:50:56 +00:00
parent 61e22cd85c
commit b7487d4edc
3 changed files with 265 additions and 2 deletions

View File

@ -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()