15ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc#===- core.py - Python LLVM Bindings -------------------------*- python -*--===# 25ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc# 35ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc# The LLVM Compiler Infrastructure 45ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc# 55ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc# This file is distributed under the University of Illinois Open Source 65ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc# License. See LICENSE.TXT for details. 75ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc# 85ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc#===------------------------------------------------------------------------===# 95ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc 1007c32218f448b7637d4acad8e87ce7cfaef0277eGregory Szorcfrom .common import LLVMObject 1161e22cd85cd4c84fff391da67018c92bf21a8e19Gregory Szorcfrom .common import c_object_p 125ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorcfrom .common import get_library 135ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc 14b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorcfrom . import enumerations 15b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc 165ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorcfrom ctypes import POINTER 175ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorcfrom ctypes import byref 185ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorcfrom ctypes import c_char_p 1937a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesmanfrom ctypes import c_uint 205ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc 215ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc__all__ = [ 225ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc "lib", 2336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines "Enums", 24f532d448307215c9ddc1dbdea42afa74757c5e00Michael Gottesman "OpCode", 2561e22cd85cd4c84fff391da67018c92bf21a8e19Gregory Szorc "MemoryBuffer", 2637a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman "Module", 277dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman "Value", 287dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman "Function", 29e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman "BasicBlock", 3073c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman "Instruction", 316a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman "Context", 328184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman "PassRegistry" 335ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc] 345ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc 355ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorclib = get_library() 3636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen HinesEnums = [] 375ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc 3836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesclass LLVMEnumeration(object): 3936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines """Represents an individual LLVM enumeration.""" 40b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc 41b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc def __init__(self, name, value): 42b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc self.name = name 43b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc self.value = value 44b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc 45b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc def __repr__(self): 4636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return '%s.%s' % (self.__class__.__name__, 4736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines self.name) 48b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc 4936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines @classmethod 5036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines def from_value(cls, value): 5136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines """Obtain an enumeration instance from a numeric value.""" 5236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines result = cls._value_map.get(value, None) 53b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc 54b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc if result is None: 5536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines raise ValueError('Unknown %s: %d' % (cls.__name__, 5636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines value)) 57b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc 58b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc return result 59b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc 6036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines @classmethod 6136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines def register(cls, name, value): 6236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines """Registers a new enumeration. 63b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc 64b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc This is called by this module for each enumeration defined in 65b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc enumerations. You should not need to call this outside this module. 66b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc """ 6736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if value in cls._value_map: 6836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines raise ValueError('%s value already registered: %d' % (cls.__name__, 6936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines value)) 7036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines enum = cls(name, value) 7136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines cls._value_map[value] = enum 7236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines setattr(cls, name, enum) 7336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 7436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesclass Attribute(LLVMEnumeration): 7536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines """Represents an individual Attribute enumeration.""" 7636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 7736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines _value_map = {} 7836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 7936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines def __init__(self, name, value): 8036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines super(Attribute, self).__init__(name, value) 8136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 8236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesclass OpCode(LLVMEnumeration): 8336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines """Represents an individual OpCode enumeration.""" 8436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 8536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines _value_map = {} 8636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 8736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines def __init__(self, name, value): 8836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines super(OpCode, self).__init__(name, value) 8936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 9036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesclass TypeKind(LLVMEnumeration): 9136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines """Represents an individual TypeKind enumeration.""" 9236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 9336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines _value_map = {} 9436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 9536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines def __init__(self, name, value): 9636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines super(TypeKind, self).__init__(name, value) 9736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 9836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesclass Linkage(LLVMEnumeration): 9936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines """Represents an individual Linkage enumeration.""" 10036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 10136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines _value_map = {} 10236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 10336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines def __init__(self, name, value): 10436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines super(Linkage, self).__init__(name, value) 10536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 10636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesclass Visibility(LLVMEnumeration): 10736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines """Represents an individual visibility enumeration.""" 10836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 10936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines _value_map = {} 11036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 11136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines def __init__(self, name, value): 11236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines super(Visibility, self).__init__(name, value) 11336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 11436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesclass CallConv(LLVMEnumeration): 11536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines """Represents an individual calling convention enumeration.""" 116b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc 11736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines _value_map = {} 11836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 11936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines def __init__(self, name, value): 12036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines super(CallConv, self).__init__(name, value) 12136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 12236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesclass IntPredicate(LLVMEnumeration): 12336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines """Represents an individual IntPredicate enumeration.""" 12436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 12536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines _value_map = {} 12636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 12736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines def __init__(self, name, value): 12836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines super(IntPredicate, self).__init__(name, value) 12936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 13036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesclass RealPredicate(LLVMEnumeration): 13136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines """Represents an individual RealPredicate enumeration.""" 13236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 13336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines _value_map = {} 13436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 13536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines def __init__(self, name, value): 13636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines super(RealPredicate, self).__init__(name, value) 13736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 13836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesclass LandingPadClauseTy(LLVMEnumeration): 13936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines """Represents an individual LandingPadClauseTy enumeration.""" 14036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 14136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines _value_map = {} 14236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 14336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines def __init__(self, name, value): 14436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines super(LandingPadClauseTy, self).__init__(name, value) 145b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc 14661e22cd85cd4c84fff391da67018c92bf21a8e19Gregory Szorcclass MemoryBuffer(LLVMObject): 1475ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc """Represents an opaque memory buffer.""" 1485ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc 1495ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc def __init__(self, filename=None): 1505ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc """Create a new memory buffer. 1515ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc 1525ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc Currently, we support creating from the contents of a file at the 1535ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc specified filename. 1545ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc """ 1555ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc if filename is None: 1565ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc raise Exception("filename argument must be defined") 1575ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc 15861e22cd85cd4c84fff391da67018c92bf21a8e19Gregory Szorc memory = c_object_p() 1595ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc out = c_char_p(None) 1605ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc 1615ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc result = lib.LLVMCreateMemoryBufferWithContentsOfFile(filename, 1625ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc byref(memory), byref(out)) 1635ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc 1645ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc if result: 1655ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc raise Exception("Could not create memory buffer: %s" % out.value) 1665ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc 16761e22cd85cd4c84fff391da67018c92bf21a8e19Gregory Szorc LLVMObject.__init__(self, memory, disposer=lib.LLVMDisposeMemoryBuffer) 1685ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc 169653212fdd1f5b5eea1c5b7d4d28b3f6c8fd05bbaMichael Gottesman def __len__(self): 170653212fdd1f5b5eea1c5b7d4d28b3f6c8fd05bbaMichael Gottesman return lib.LLVMGetBufferSize(self) 171653212fdd1f5b5eea1c5b7d4d28b3f6c8fd05bbaMichael Gottesman 1727dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesmanclass Value(LLVMObject): 1737dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 1747dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman def __init__(self, value): 1757dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman LLVMObject.__init__(self, value) 1767dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 1777dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman @property 1787dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman def name(self): 1797dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman return lib.LLVMGetValueName(self) 1807dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 1817dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman def dump(self): 1827dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman lib.LLVMDumpValue(self) 183abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman 184abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman def get_operand(self, i): 185abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman return Value(lib.LLVMGetOperand(self, i)) 186abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman 187abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman def set_operand(self, i, v): 188abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman return lib.LLVMSetOperand(self, i, v) 189abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman 190abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman def __len__(self): 191abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman return lib.LLVMGetNumOperands(self) 1927dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 19337a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesmanclass Module(LLVMObject): 19437a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman """Represents the top-level structure of an llvm program in an opaque object.""" 19537a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 19637a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman def __init__(self, module, name=None, context=None): 19737a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman LLVMObject.__init__(self, module, disposer=lib.LLVMDisposeModule) 19837a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 19937a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman @classmethod 20037a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman def CreateWithName(cls, module_id): 20137a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman m = Module(lib.LLVMModuleCreateWithName(module_id)) 20236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Context.GetGlobalContext().take_ownership(m) 20337a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman return m 20437a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 20537a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman @property 206f495a2679248bca1a426052b3a297326bb46e9dbMichael Gottesman def datalayout(self): 20737a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman return lib.LLVMGetDataLayout(self) 20837a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 209f495a2679248bca1a426052b3a297326bb46e9dbMichael Gottesman @datalayout.setter 210f495a2679248bca1a426052b3a297326bb46e9dbMichael Gottesman def datalayout(self, new_data_layout): 21137a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman """new_data_layout is a string.""" 21237a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman lib.LLVMSetDataLayout(self, new_data_layout) 21337a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 21437a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman @property 21537a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman def target(self): 21637a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman return lib.LLVMGetTarget(self) 21737a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 21837a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman @target.setter 21937a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman def target(self, new_target): 22037a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman """new_target is a string.""" 22137a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman lib.LLVMSetTarget(self, new_target) 22237a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 22337a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman def dump(self): 22437a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman lib.LLVMDumpModule(self) 22537a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 2267dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman class __function_iterator(object): 2277dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman def __init__(self, module, reverse=False): 2287dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman self.module = module 2297dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman self.reverse = reverse 2307dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman if self.reverse: 2317dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman self.function = self.module.last 2327dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman else: 2337dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman self.function = self.module.first 2347dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 2357dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman def __iter__(self): 2367dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman return self 2377dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 2387dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman def next(self): 2397dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman if not isinstance(self.function, Function): 2407dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman raise StopIteration("") 2417dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman result = self.function 2427dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman if self.reverse: 2437dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman self.function = self.function.prev 2447dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman else: 2457dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman self.function = self.function.next 2467dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman return result 2477dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 2487dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman def __iter__(self): 2497dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman return Module.__function_iterator(self) 2507dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 2517dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman def __reversed__(self): 2527dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman return Module.__function_iterator(self, reverse=True) 2537dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 2547dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman @property 2557dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman def first(self): 2567dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman return Function(lib.LLVMGetFirstFunction(self)) 2577dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 2587dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman @property 2597dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman def last(self): 2607dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman return Function(lib.LLVMGetLastFunction(self)) 2617dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 26237a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman def print_module_to_file(self, filename): 26337a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman out = c_char_p(None) 2647400a858be88ed6fbffb46dcd48b6c9ad6c85a2bMichael Gottesman # Result is inverted so 0 means everything was ok. 2657400a858be88ed6fbffb46dcd48b6c9ad6c85a2bMichael Gottesman result = lib.LLVMPrintModuleToFile(self, filename, byref(out)) 2667400a858be88ed6fbffb46dcd48b6c9ad6c85a2bMichael Gottesman if result: 26737a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman raise RuntimeError("LLVM Error: %s" % out.value) 26837a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 2697dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesmanclass Function(Value): 2707dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 2717dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman def __init__(self, value): 2727dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman Value.__init__(self, value) 2737dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 2747dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman @property 2757dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman def next(self): 2767dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman f = lib.LLVMGetNextFunction(self) 2777dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman return f and Function(f) 2787dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 2797dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman @property 2807dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman def prev(self): 2817dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman f = lib.LLVMGetPreviousFunction(self) 2827dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman return f and Function(f) 2837dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 284e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman @property 285e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman def first(self): 286e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman b = lib.LLVMGetFirstBasicBlock(self) 287e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman return b and BasicBlock(b) 288e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 289e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman @property 290e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman def last(self): 291e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman b = lib.LLVMGetLastBasicBlock(self) 292e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman return b and BasicBlock(b) 293e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 294e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman class __bb_iterator(object): 295e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman def __init__(self, function, reverse=False): 296e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman self.function = function 297e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman self.reverse = reverse 298e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman if self.reverse: 299e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman self.bb = function.last 300e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman else: 301e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman self.bb = function.first 302e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 303e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman def __iter__(self): 304e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman return self 305e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 306e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman def next(self): 307e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman if not isinstance(self.bb, BasicBlock): 308e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman raise StopIteration("") 309e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman result = self.bb 310e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman if self.reverse: 311e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman self.bb = self.bb.prev 312e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman else: 313e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman self.bb = self.bb.next 314e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman return result 315e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 316e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman def __iter__(self): 317e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman return Function.__bb_iterator(self) 318e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 319e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman def __reversed__(self): 320e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman return Function.__bb_iterator(self, reverse=True) 321e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 322e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman def __len__(self): 323e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman return lib.LLVMCountBasicBlocks(self) 324e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 325e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesmanclass BasicBlock(LLVMObject): 326e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 327e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman def __init__(self, value): 328e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman LLVMObject.__init__(self, value) 329e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 330e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman @property 331e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman def next(self): 332e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman b = lib.LLVMGetNextBasicBlock(self) 333e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman return b and BasicBlock(b) 334e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 335e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman @property 336e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman def prev(self): 337e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman b = lib.LLVMGetPreviousBasicBlock(self) 338e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman return b and BasicBlock(b) 339e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 340e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman @property 34173c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman def first(self): 34273c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman i = lib.LLVMGetFirstInstruction(self) 34373c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman return i and Instruction(i) 34473c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman 34573c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman @property 34673c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman def last(self): 34773c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman i = lib.LLVMGetLastInstruction(self) 34873c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman return i and Instruction(i) 34973c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman 350abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman def __as_value(self): 351abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman return Value(lib.LLVMBasicBlockAsValue(self)) 352abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman 35373c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman @property 354e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman def name(self): 355abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman return lib.LLVMGetValueName(self.__as_value()) 356e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 357e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman def dump(self): 358abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman lib.LLVMDumpValue(self.__as_value()) 359abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman 360abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman def get_operand(self, i): 361abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman return Value(lib.LLVMGetOperand(self.__as_value(), 362abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman i)) 363abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman 364abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman def set_operand(self, i, v): 365abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman return lib.LLVMSetOperand(self.__as_value(), 366abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman i, v) 367abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman 368abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman def __len__(self): 369abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman return lib.LLVMGetNumOperands(self.__as_value()) 370e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 37173c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman class __inst_iterator(object): 37273c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman def __init__(self, bb, reverse=False): 37373c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman self.bb = bb 37473c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman self.reverse = reverse 37573c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman if self.reverse: 37673c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman self.inst = self.bb.last 37773c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman else: 37873c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman self.inst = self.bb.first 37973c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman 38073c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman def __iter__(self): 38173c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman return self 38273c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman 38373c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman def next(self): 38473c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman if not isinstance(self.inst, Instruction): 38573c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman raise StopIteration("") 38673c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman result = self.inst 38773c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman if self.reverse: 38873c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman self.inst = self.inst.prev 38973c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman else: 39073c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman self.inst = self.inst.next 39173c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman return result 39273c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman 39373c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman def __iter__(self): 39473c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman return BasicBlock.__inst_iterator(self) 39573c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman 39673c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman def __reversed__(self): 39773c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman return BasicBlock.__inst_iterator(self, reverse=True) 39873c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman 39973c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman 40073c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesmanclass Instruction(Value): 40173c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman 40273c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman def __init__(self, value): 40373c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman Value.__init__(self, value) 40473c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman 40573c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman @property 40673c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman def next(self): 40773c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman i = lib.LLVMGetNextInstruction(self) 40873c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman return i and Instruction(i) 40973c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman 41073c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman @property 41173c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman def prev(self): 41273c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman i = lib.LLVMGetPreviousInstruction(self) 41373c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman return i and Instruction(i) 41473c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman 41573c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman @property 41673c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman def opcode(self): 41773c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman return OpCode.from_value(lib.LLVMGetInstructionOpcode(self)) 41873c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman 4196a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesmanclass Context(LLVMObject): 4206a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman 4216a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman def __init__(self, context=None): 4226a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman if context is None: 4236a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman context = lib.LLVMContextCreate() 4246a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman LLVMObject.__init__(self, context, disposer=lib.LLVMContextDispose) 4256a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman else: 4266a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman LLVMObject.__init__(self, context) 4276a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman 4286a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman @classmethod 4296a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman def GetGlobalContext(cls): 4306a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman return Context(lib.LLVMGetGlobalContext()) 4316a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman 4328184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesmanclass PassRegistry(LLVMObject): 4338184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman """Represents an opaque pass registry object.""" 4348184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 4358184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman def __init__(self): 4368184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman LLVMObject.__init__(self, 4378184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman lib.LLVMGetGlobalPassRegistry()) 4388184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 4395ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorcdef register_library(library): 4408184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman # Initialization/Shutdown declarations. 4418184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeCore.argtypes = [PassRegistry] 4428184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeCore.restype = None 4438184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 4448184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeTransformUtils.argtypes = [PassRegistry] 4458184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeTransformUtils.restype = None 4468184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 4478184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeScalarOpts.argtypes = [PassRegistry] 4488184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeScalarOpts.restype = None 4498184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 4508184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeObjCARCOpts.argtypes = [PassRegistry] 4518184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeObjCARCOpts.restype = None 4528184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 4538184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeVectorization.argtypes = [PassRegistry] 4548184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeVectorization.restype = None 4558184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 4568184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeInstCombine.argtypes = [PassRegistry] 4578184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeInstCombine.restype = None 4588184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 4598184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeIPO.argtypes = [PassRegistry] 4608184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeIPO.restype = None 4618184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 4628184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeInstrumentation.argtypes = [PassRegistry] 4638184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeInstrumentation.restype = None 4648184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 4658184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeAnalysis.argtypes = [PassRegistry] 4668184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeAnalysis.restype = None 4678184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 4688184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeIPA.argtypes = [PassRegistry] 4698184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeIPA.restype = None 4708184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 4718184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeCodeGen.argtypes = [PassRegistry] 4728184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeCodeGen.restype = None 4738184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 4748184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeTarget.argtypes = [PassRegistry] 4758184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeTarget.restype = None 4768184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 4778184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMShutdown.argtypes = [] 4788184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMShutdown.restype = None 4798184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 4808184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman # Pass Registry declarations. 4818184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMGetGlobalPassRegistry.argtypes = [] 4828184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMGetGlobalPassRegistry.restype = c_object_p 4838184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 4846a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman # Context declarations. 4856a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman library.LLVMContextCreate.argtypes = [] 4866a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman library.LLVMContextCreate.restype = c_object_p 4876a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman 4886a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman library.LLVMContextDispose.argtypes = [Context] 4896a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman library.LLVMContextDispose.restype = None 4906a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman 4916a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman library.LLVMGetGlobalContext.argtypes = [] 4926a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman library.LLVMGetGlobalContext.restype = c_object_p 4936a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman 494653212fdd1f5b5eea1c5b7d4d28b3f6c8fd05bbaMichael Gottesman # Memory buffer declarations 4955ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc library.LLVMCreateMemoryBufferWithContentsOfFile.argtypes = [c_char_p, 49661e22cd85cd4c84fff391da67018c92bf21a8e19Gregory Szorc POINTER(c_object_p), POINTER(c_char_p)] 4975ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc library.LLVMCreateMemoryBufferWithContentsOfFile.restype = bool 4985ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc 499653212fdd1f5b5eea1c5b7d4d28b3f6c8fd05bbaMichael Gottesman library.LLVMGetBufferSize.argtypes = [MemoryBuffer] 500653212fdd1f5b5eea1c5b7d4d28b3f6c8fd05bbaMichael Gottesman 50161e22cd85cd4c84fff391da67018c92bf21a8e19Gregory Szorc library.LLVMDisposeMemoryBuffer.argtypes = [MemoryBuffer] 5025ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc 50337a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman # Module declarations 50437a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMModuleCreateWithName.argtypes = [c_char_p] 50537a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMModuleCreateWithName.restype = c_object_p 50637a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 50737a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMDisposeModule.argtypes = [Module] 50837a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMDisposeModule.restype = None 50937a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 51037a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMGetDataLayout.argtypes = [Module] 51137a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMGetDataLayout.restype = c_char_p 51237a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 51337a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMSetDataLayout.argtypes = [Module, c_char_p] 51437a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMSetDataLayout.restype = None 51537a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 51637a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMGetTarget.argtypes = [Module] 51737a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMGetTarget.restype = c_char_p 51837a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 51937a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMSetTarget.argtypes = [Module, c_char_p] 52037a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMSetTarget.restype = None 52137a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 52237a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMDumpModule.argtypes = [Module] 52337a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMDumpModule.restype = None 52437a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 52537a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMPrintModuleToFile.argtypes = [Module, c_char_p, 52637a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman POINTER(c_char_p)] 52737a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMPrintModuleToFile.restype = bool 52837a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 5297dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman library.LLVMGetFirstFunction.argtypes = [Module] 5307dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman library.LLVMGetFirstFunction.restype = c_object_p 5317dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 5327dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman library.LLVMGetLastFunction.argtypes = [Module] 5337dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman library.LLVMGetLastFunction.restype = c_object_p 5347dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 5357dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman library.LLVMGetNextFunction.argtypes = [Function] 5367dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman library.LLVMGetNextFunction.restype = c_object_p 5377dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 5387dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman library.LLVMGetPreviousFunction.argtypes = [Function] 5397dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman library.LLVMGetPreviousFunction.restype = c_object_p 5407dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 5417dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman # Value declarations. 5427dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman library.LLVMGetValueName.argtypes = [Value] 5437dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman library.LLVMGetValueName.restype = c_char_p 5447dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 5457dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman library.LLVMDumpValue.argtypes = [Value] 5467dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman library.LLVMDumpValue.restype = None 5477dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 548abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman library.LLVMGetOperand.argtypes = [Value, c_uint] 549abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman library.LLVMGetOperand.restype = c_object_p 550abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman 551abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman library.LLVMSetOperand.argtypes = [Value, Value, c_uint] 552abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman library.LLVMSetOperand.restype = None 553abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman 554abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman library.LLVMGetNumOperands.argtypes = [Value] 555abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman library.LLVMGetNumOperands.restype = c_uint 556abaa85d88d13f6efa99c61d206155678ccd5f118Michael Gottesman 557e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman # Basic Block Declarations. 558e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman library.LLVMGetFirstBasicBlock.argtypes = [Function] 559e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman library.LLVMGetFirstBasicBlock.restype = c_object_p 560e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 561e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman library.LLVMGetLastBasicBlock.argtypes = [Function] 562e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman library.LLVMGetLastBasicBlock.restype = c_object_p 563e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 564e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman library.LLVMGetNextBasicBlock.argtypes = [BasicBlock] 565e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman library.LLVMGetNextBasicBlock.restype = c_object_p 566e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 567e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman library.LLVMGetPreviousBasicBlock.argtypes = [BasicBlock] 568e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman library.LLVMGetPreviousBasicBlock.restype = c_object_p 569e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 57073c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman library.LLVMGetFirstInstruction.argtypes = [BasicBlock] 57173c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman library.LLVMGetFirstInstruction.restype = c_object_p 57273c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman 57373c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman library.LLVMGetLastInstruction.argtypes = [BasicBlock] 57473c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman library.LLVMGetLastInstruction.restype = c_object_p 57573c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman 576e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman library.LLVMBasicBlockAsValue.argtypes = [BasicBlock] 577e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman library.LLVMBasicBlockAsValue.restype = c_object_p 578e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 579e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman library.LLVMCountBasicBlocks.argtypes = [Function] 580e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman library.LLVMCountBasicBlocks.restype = c_uint 581e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 58273c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman # Instruction Declarations. 58373c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman library.LLVMGetNextInstruction.argtypes = [Instruction] 58473c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman library.LLVMGetNextInstruction.restype = c_object_p 58573c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman 58673c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman library.LLVMGetPreviousInstruction.argtypes = [Instruction] 58773c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman library.LLVMGetPreviousInstruction.restype = c_object_p 58873c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman 58973c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman library.LLVMGetInstructionOpcode.argtypes = [Instruction] 59073c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman library.LLVMGetInstructionOpcode.restype = c_uint 59173c382f7fdbe034383600c1ddd385aea0cd27221Michael Gottesman 592b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorcdef register_enumerations(): 59336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if Enums: 59436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return None 59536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines enums = [ 59636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines (Attribute, enumerations.Attributes), 59736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines (OpCode, enumerations.OpCodes), 59836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines (TypeKind, enumerations.TypeKinds), 59936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines (Linkage, enumerations.Linkages), 60036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines (Visibility, enumerations.Visibility), 60136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines (CallConv, enumerations.CallConv), 60236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines (IntPredicate, enumerations.IntPredicate), 60336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines (RealPredicate, enumerations.RealPredicate), 60436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines (LandingPadClauseTy, enumerations.LandingPadClauseTy), 60536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines ] 60636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines for enum_class, enum_spec in enums: 60736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines for name, value in enum_spec: 60836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines print name, value 60936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines enum_class.register(name, value) 61036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return enums 611b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc 6128184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesmandef initialize_llvm(): 61336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Context.GetGlobalContext() 6148184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman p = PassRegistry() 6158184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman lib.LLVMInitializeCore(p) 6168184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman lib.LLVMInitializeTransformUtils(p) 6178184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman lib.LLVMInitializeScalarOpts(p) 6188184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman lib.LLVMInitializeObjCARCOpts(p) 6198184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman lib.LLVMInitializeVectorization(p) 6208184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman lib.LLVMInitializeInstCombine(p) 6218184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman lib.LLVMInitializeIPO(p) 6228184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman lib.LLVMInitializeInstrumentation(p) 6238184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman lib.LLVMInitializeAnalysis(p) 6248184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman lib.LLVMInitializeIPA(p) 6258184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman lib.LLVMInitializeCodeGen(p) 6268184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman lib.LLVMInitializeTarget(p) 6278184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 6285ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorcregister_library(lib) 62936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen HinesEnums = register_enumerations() 6308184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesmaninitialize_llvm() 631