core.py revision e23fa984f5094b58c0b57260ade5a6728336d3ab
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", 23f532d448307215c9ddc1dbdea42afa74757c5e00Michael Gottesman "OpCode", 2461e22cd85cd4c84fff391da67018c92bf21a8e19Gregory Szorc "MemoryBuffer", 2537a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman "Module", 267dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman "Value", 277dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman "Function", 28e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman "BasicBlock", 296a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman "Context", 308184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman "PassRegistry" 315ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc] 325ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc 335ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorclib = get_library() 345ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc 35b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorcclass OpCode(object): 36b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc """Represents an individual OpCode enumeration.""" 37b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc 38b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc _value_map = {} 39b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc 40b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc def __init__(self, name, value): 41b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc self.name = name 42b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc self.value = value 43b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc 44b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc def __repr__(self): 45b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc return 'OpCode.%s' % self.name 46b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc 47b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc @staticmethod 48b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc def from_value(value): 49b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc """Obtain an OpCode instance from a numeric value.""" 50b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc result = OpCode._value_map.get(value, None) 51b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc 52b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc if result is None: 53b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc raise ValueError('Unknown OpCode: %d' % value) 54b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc 55b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc return result 56b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc 57b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc @staticmethod 58b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc def register(name, value): 59b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc """Registers a new OpCode enumeration. 60b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc 61b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc This is called by this module for each enumeration defined in 62b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc enumerations. You should not need to call this outside this module. 63b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc """ 64b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc if value in OpCode._value_map: 65b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc raise ValueError('OpCode value already registered: %d' % value) 66b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc 67b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc opcode = OpCode(name, value) 68b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc OpCode._value_map[value] = opcode 69b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc setattr(OpCode, name, opcode) 70b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc 7161e22cd85cd4c84fff391da67018c92bf21a8e19Gregory Szorcclass MemoryBuffer(LLVMObject): 725ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc """Represents an opaque memory buffer.""" 735ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc 745ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc def __init__(self, filename=None): 755ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc """Create a new memory buffer. 765ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc 775ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc Currently, we support creating from the contents of a file at the 785ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc specified filename. 795ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc """ 805ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc if filename is None: 815ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc raise Exception("filename argument must be defined") 825ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc 8361e22cd85cd4c84fff391da67018c92bf21a8e19Gregory Szorc memory = c_object_p() 845ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc out = c_char_p(None) 855ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc 865ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc result = lib.LLVMCreateMemoryBufferWithContentsOfFile(filename, 875ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc byref(memory), byref(out)) 885ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc 895ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc if result: 905ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc raise Exception("Could not create memory buffer: %s" % out.value) 915ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc 9261e22cd85cd4c84fff391da67018c92bf21a8e19Gregory Szorc LLVMObject.__init__(self, memory, disposer=lib.LLVMDisposeMemoryBuffer) 935ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc 94653212fdd1f5b5eea1c5b7d4d28b3f6c8fd05bbaMichael Gottesman def __len__(self): 95653212fdd1f5b5eea1c5b7d4d28b3f6c8fd05bbaMichael Gottesman return lib.LLVMGetBufferSize(self) 96653212fdd1f5b5eea1c5b7d4d28b3f6c8fd05bbaMichael Gottesman 977dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesmanclass Value(LLVMObject): 987dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 997dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman def __init__(self, value): 1007dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman LLVMObject.__init__(self, value) 1017dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 1027dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman @property 1037dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman def name(self): 1047dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman return lib.LLVMGetValueName(self) 1057dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 1067dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman def dump(self): 1077dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman lib.LLVMDumpValue(self) 1087dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 10937a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesmanclass Module(LLVMObject): 11037a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman """Represents the top-level structure of an llvm program in an opaque object.""" 11137a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 11237a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman def __init__(self, module, name=None, context=None): 11337a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman LLVMObject.__init__(self, module, disposer=lib.LLVMDisposeModule) 11437a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 11537a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman @classmethod 11637a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman def CreateWithName(cls, module_id): 11737a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman m = Module(lib.LLVMModuleCreateWithName(module_id)) 11837a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman c = Context.GetGlobalContext().take_ownership(m) 11937a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman return m 12037a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 12137a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman @property 122f495a2679248bca1a426052b3a297326bb46e9dbMichael Gottesman def datalayout(self): 12337a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman return lib.LLVMGetDataLayout(self) 12437a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 125f495a2679248bca1a426052b3a297326bb46e9dbMichael Gottesman @datalayout.setter 126f495a2679248bca1a426052b3a297326bb46e9dbMichael Gottesman def datalayout(self, new_data_layout): 12737a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman """new_data_layout is a string.""" 12837a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman lib.LLVMSetDataLayout(self, new_data_layout) 12937a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 13037a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman @property 13137a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman def target(self): 13237a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman return lib.LLVMGetTarget(self) 13337a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 13437a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman @target.setter 13537a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman def target(self, new_target): 13637a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman """new_target is a string.""" 13737a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman lib.LLVMSetTarget(self, new_target) 13837a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 13937a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman def dump(self): 14037a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman lib.LLVMDumpModule(self) 14137a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 1427dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman class __function_iterator(object): 1437dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman def __init__(self, module, reverse=False): 1447dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman self.module = module 1457dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman self.reverse = reverse 1467dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman if self.reverse: 1477dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman self.function = self.module.last 1487dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman else: 1497dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman self.function = self.module.first 1507dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 1517dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman def __iter__(self): 1527dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman return self 1537dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 1547dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman def next(self): 1557dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman if not isinstance(self.function, Function): 1567dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman raise StopIteration("") 1577dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman result = self.function 1587dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman if self.reverse: 1597dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman self.function = self.function.prev 1607dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman else: 1617dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman self.function = self.function.next 1627dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman return result 1637dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 1647dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman def __iter__(self): 1657dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman return Module.__function_iterator(self) 1667dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 1677dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman def __reversed__(self): 1687dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman return Module.__function_iterator(self, reverse=True) 1697dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 1707dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman @property 1717dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman def first(self): 1727dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman return Function(lib.LLVMGetFirstFunction(self)) 1737dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 1747dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman @property 1757dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman def last(self): 1767dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman return Function(lib.LLVMGetLastFunction(self)) 1777dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 17837a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman def print_module_to_file(self, filename): 17937a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman out = c_char_p(None) 1807400a858be88ed6fbffb46dcd48b6c9ad6c85a2bMichael Gottesman # Result is inverted so 0 means everything was ok. 1817400a858be88ed6fbffb46dcd48b6c9ad6c85a2bMichael Gottesman result = lib.LLVMPrintModuleToFile(self, filename, byref(out)) 1827400a858be88ed6fbffb46dcd48b6c9ad6c85a2bMichael Gottesman if result: 18337a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman raise RuntimeError("LLVM Error: %s" % out.value) 18437a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 1857dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesmanclass Function(Value): 1867dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 1877dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman def __init__(self, value): 1887dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman Value.__init__(self, value) 1897dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 1907dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman @property 1917dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman def next(self): 1927dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman f = lib.LLVMGetNextFunction(self) 1937dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman return f and Function(f) 1947dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 1957dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman @property 1967dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman def prev(self): 1977dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman f = lib.LLVMGetPreviousFunction(self) 1987dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman return f and Function(f) 1997dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 200e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman @property 201e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman def first(self): 202e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman b = lib.LLVMGetFirstBasicBlock(self) 203e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman return b and BasicBlock(b) 204e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 205e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman @property 206e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman def last(self): 207e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman b = lib.LLVMGetLastBasicBlock(self) 208e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman return b and BasicBlock(b) 209e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 210e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman class __bb_iterator(object): 211e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman def __init__(self, function, reverse=False): 212e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman self.function = function 213e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman self.reverse = reverse 214e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman if self.reverse: 215e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman self.bb = function.last 216e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman else: 217e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman self.bb = function.first 218e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 219e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman def __iter__(self): 220e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman return self 221e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 222e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman def next(self): 223e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman if not isinstance(self.bb, BasicBlock): 224e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman raise StopIteration("") 225e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman result = self.bb 226e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman if self.reverse: 227e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman self.bb = self.bb.prev 228e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman else: 229e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman self.bb = self.bb.next 230e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman return result 231e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 232e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman def __iter__(self): 233e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman return Function.__bb_iterator(self) 234e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 235e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman def __reversed__(self): 236e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman return Function.__bb_iterator(self, reverse=True) 237e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 238e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman def __len__(self): 239e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman return lib.LLVMCountBasicBlocks(self) 240e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 241e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesmanclass BasicBlock(LLVMObject): 242e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 243e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman def __init__(self, value): 244e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman LLVMObject.__init__(self, value) 245e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 246e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman @property 247e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman def next(self): 248e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman b = lib.LLVMGetNextBasicBlock(self) 249e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman return b and BasicBlock(b) 250e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 251e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman @property 252e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman def prev(self): 253e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman b = lib.LLVMGetPreviousBasicBlock(self) 254e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman return b and BasicBlock(b) 255e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 256e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman @property 257e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman def name(self): 258e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman return lib.LLVMGetValueName(Value(lib.LLVMBasicBlockAsValue(self))) 259e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 260e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman def dump(self): 261e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman lib.LLVMDumpValue(Value(lib.LLVMBasicBlockAsValue(self))) 262e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 2636a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesmanclass Context(LLVMObject): 2646a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman 2656a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman def __init__(self, context=None): 2666a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman if context is None: 2676a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman context = lib.LLVMContextCreate() 2686a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman LLVMObject.__init__(self, context, disposer=lib.LLVMContextDispose) 2696a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman else: 2706a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman LLVMObject.__init__(self, context) 2716a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman 2726a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman @classmethod 2736a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman def GetGlobalContext(cls): 2746a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman return Context(lib.LLVMGetGlobalContext()) 2756a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman 2768184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesmanclass PassRegistry(LLVMObject): 2778184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman """Represents an opaque pass registry object.""" 2788184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 2798184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman def __init__(self): 2808184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman LLVMObject.__init__(self, 2818184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman lib.LLVMGetGlobalPassRegistry()) 2828184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 2835ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorcdef register_library(library): 2848184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman # Initialization/Shutdown declarations. 2858184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeCore.argtypes = [PassRegistry] 2868184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeCore.restype = None 2878184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 2888184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeTransformUtils.argtypes = [PassRegistry] 2898184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeTransformUtils.restype = None 2908184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 2918184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeScalarOpts.argtypes = [PassRegistry] 2928184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeScalarOpts.restype = None 2938184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 2948184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeObjCARCOpts.argtypes = [PassRegistry] 2958184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeObjCARCOpts.restype = None 2968184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 2978184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeVectorization.argtypes = [PassRegistry] 2988184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeVectorization.restype = None 2998184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 3008184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeInstCombine.argtypes = [PassRegistry] 3018184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeInstCombine.restype = None 3028184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 3038184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeIPO.argtypes = [PassRegistry] 3048184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeIPO.restype = None 3058184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 3068184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeInstrumentation.argtypes = [PassRegistry] 3078184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeInstrumentation.restype = None 3088184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 3098184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeAnalysis.argtypes = [PassRegistry] 3108184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeAnalysis.restype = None 3118184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 3128184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeIPA.argtypes = [PassRegistry] 3138184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeIPA.restype = None 3148184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 3158184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeCodeGen.argtypes = [PassRegistry] 3168184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeCodeGen.restype = None 3178184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 3188184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeTarget.argtypes = [PassRegistry] 3198184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMInitializeTarget.restype = None 3208184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 3218184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMShutdown.argtypes = [] 3228184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMShutdown.restype = None 3238184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 3248184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman # Pass Registry declarations. 3258184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMGetGlobalPassRegistry.argtypes = [] 3268184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman library.LLVMGetGlobalPassRegistry.restype = c_object_p 3278184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 3286a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman # Context declarations. 3296a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman library.LLVMContextCreate.argtypes = [] 3306a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman library.LLVMContextCreate.restype = c_object_p 3316a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman 3326a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman library.LLVMContextDispose.argtypes = [Context] 3336a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman library.LLVMContextDispose.restype = None 3346a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman 3356a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman library.LLVMGetGlobalContext.argtypes = [] 3366a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman library.LLVMGetGlobalContext.restype = c_object_p 3376a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman 338653212fdd1f5b5eea1c5b7d4d28b3f6c8fd05bbaMichael Gottesman # Memory buffer declarations 3395ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc library.LLVMCreateMemoryBufferWithContentsOfFile.argtypes = [c_char_p, 34061e22cd85cd4c84fff391da67018c92bf21a8e19Gregory Szorc POINTER(c_object_p), POINTER(c_char_p)] 3415ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc library.LLVMCreateMemoryBufferWithContentsOfFile.restype = bool 3425ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc 343653212fdd1f5b5eea1c5b7d4d28b3f6c8fd05bbaMichael Gottesman library.LLVMGetBufferSize.argtypes = [MemoryBuffer] 344653212fdd1f5b5eea1c5b7d4d28b3f6c8fd05bbaMichael Gottesman 34561e22cd85cd4c84fff391da67018c92bf21a8e19Gregory Szorc library.LLVMDisposeMemoryBuffer.argtypes = [MemoryBuffer] 3465ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorc 34737a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman # Module declarations 34837a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMModuleCreateWithName.argtypes = [c_char_p] 34937a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMModuleCreateWithName.restype = c_object_p 35037a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 35137a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMDisposeModule.argtypes = [Module] 35237a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMDisposeModule.restype = None 35337a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 35437a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMGetDataLayout.argtypes = [Module] 35537a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMGetDataLayout.restype = c_char_p 35637a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 35737a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMSetDataLayout.argtypes = [Module, c_char_p] 35837a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMSetDataLayout.restype = None 35937a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 36037a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMGetTarget.argtypes = [Module] 36137a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMGetTarget.restype = c_char_p 36237a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 36337a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMSetTarget.argtypes = [Module, c_char_p] 36437a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMSetTarget.restype = None 36537a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 36637a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMDumpModule.argtypes = [Module] 36737a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMDumpModule.restype = None 36837a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 36937a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMPrintModuleToFile.argtypes = [Module, c_char_p, 37037a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman POINTER(c_char_p)] 37137a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman library.LLVMPrintModuleToFile.restype = bool 37237a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman 3737dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman library.LLVMGetFirstFunction.argtypes = [Module] 3747dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman library.LLVMGetFirstFunction.restype = c_object_p 3757dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 3767dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman library.LLVMGetLastFunction.argtypes = [Module] 3777dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman library.LLVMGetLastFunction.restype = c_object_p 3787dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 3797dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman library.LLVMGetNextFunction.argtypes = [Function] 3807dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman library.LLVMGetNextFunction.restype = c_object_p 3817dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 3827dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman library.LLVMGetPreviousFunction.argtypes = [Function] 3837dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman library.LLVMGetPreviousFunction.restype = c_object_p 3847dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 3857dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman # Value declarations. 3867dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman library.LLVMGetValueName.argtypes = [Value] 3877dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman library.LLVMGetValueName.restype = c_char_p 3887dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 3897dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman library.LLVMDumpValue.argtypes = [Value] 3907dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman library.LLVMDumpValue.restype = None 3917dfa4bc4716aaa34cc7c6226fd00675899263e9dMichael Gottesman 392e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman # Basic Block Declarations. 393e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman library.LLVMGetFirstBasicBlock.argtypes = [Function] 394e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman library.LLVMGetFirstBasicBlock.restype = c_object_p 395e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 396e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman library.LLVMGetLastBasicBlock.argtypes = [Function] 397e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman library.LLVMGetLastBasicBlock.restype = c_object_p 398e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 399e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman library.LLVMGetNextBasicBlock.argtypes = [BasicBlock] 400e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman library.LLVMGetNextBasicBlock.restype = c_object_p 401e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 402e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman library.LLVMGetPreviousBasicBlock.argtypes = [BasicBlock] 403e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman library.LLVMGetPreviousBasicBlock.restype = c_object_p 404e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 405e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman library.LLVMBasicBlockAsValue.argtypes = [BasicBlock] 406e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman library.LLVMBasicBlockAsValue.restype = c_object_p 407e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 408e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman library.LLVMCountBasicBlocks.argtypes = [Function] 409e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman library.LLVMCountBasicBlocks.restype = c_uint 410e23fa984f5094b58c0b57260ade5a6728336d3abMichael Gottesman 411b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorcdef register_enumerations(): 412b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc for name, value in enumerations.OpCodes: 413b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc OpCode.register(name, value) 414b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorc 4158184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesmandef initialize_llvm(): 4166a63cd12813fea01d711f098126d199c936c8f6bMichael Gottesman c = Context.GetGlobalContext() 4178184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman p = PassRegistry() 4188184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman lib.LLVMInitializeCore(p) 4198184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman lib.LLVMInitializeTransformUtils(p) 4208184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman lib.LLVMInitializeScalarOpts(p) 4218184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman lib.LLVMInitializeObjCARCOpts(p) 4228184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman lib.LLVMInitializeVectorization(p) 4238184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman lib.LLVMInitializeInstCombine(p) 4248184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman lib.LLVMInitializeIPO(p) 4258184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman lib.LLVMInitializeInstrumentation(p) 4268184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman lib.LLVMInitializeAnalysis(p) 4278184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman lib.LLVMInitializeIPA(p) 4288184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman lib.LLVMInitializeCodeGen(p) 4298184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman lib.LLVMInitializeTarget(p) 4308184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesman 4315ae04279e0ed16975e1eea012499e8d833aab3c5Gregory Szorcregister_library(lib) 432b7487d4edc34fa22b697d0874331c163ed2c6847Gregory Szorcregister_enumerations() 4338184ca673bbc23d55f17d903f98f5382f2b7da79Michael Gottesmaninitialize_llvm() 434