diff --git a/Sources/LLVM/BasicBlock.swift b/Sources/LLVM/BasicBlock.swift index 5ef28a2e..7af370d3 100644 --- a/Sources/LLVM/BasicBlock.swift +++ b/Sources/LLVM/BasicBlock.swift @@ -59,6 +59,12 @@ public struct BasicBlock: IRValue { return BasicBlock(llvm: blockRef) } + /// Returns the basic block before this basic block, if it exists. + public func previous() -> BasicBlock? { + guard let blockRef = LLVMGetPreviousBasicBlock(llvm) else { return nil } + return BasicBlock(llvm: blockRef) + } + /// Returns a sequence of the Instructions that make up this basic block. public var instructions: AnySequence { var current = firstInstruction diff --git a/Sources/LLVM/Instruction.swift b/Sources/LLVM/Instruction.swift index 4dbf10ad..28e8a085 100644 --- a/Sources/LLVM/Instruction.swift +++ b/Sources/LLVM/Instruction.swift @@ -33,6 +33,13 @@ public struct Instruction: IRValue { return Instruction(llvm: val) } + /// Retrieves the parent basic block that contains this instruction, if it + /// exists. + public var parentBlock: BasicBlock? { + guard let parent = LLVMGetInstructionParent(self.llvm) else { return nil } + return BasicBlock(llvm: parent) + } + /// Retrieves the first use of this instruction. public var firstUse: Use? { guard let use = LLVMGetFirstUse(llvm) else { return nil } diff --git a/Sources/LLVM/StructType.swift b/Sources/LLVM/StructType.swift index 39c5bdea..fd272f67 100644 --- a/Sources/LLVM/StructType.swift +++ b/Sources/LLVM/StructType.swift @@ -69,6 +69,27 @@ public struct StructType: IRType { } } + /// Retrieves the name associated with this structure type, or the empty + /// string if this type is unnamed. + public var name: String { + guard let sname = LLVMGetStructName(self.llvm) else { return "" } + return String(cString: sname) + } + + /// Retrieves the element types associated with this structure type. + public var elementTypes: [IRType] { + var params = [IRType]() + let count = Int(LLVMCountStructElementTypes(self.llvm)) + let paramsPtr = UnsafeMutablePointer.allocate(capacity: count) + defer { free(paramsPtr) } + LLVMGetStructElementTypes(self.llvm, paramsPtr) + for i in 0.. LLVMTypeRef { return llvm