|
| 1 | +import CLLVM |
| 2 | + |
| 3 | +/// Modules represent the top-level structure in an LLVM program. An LLVM |
| 4 | +/// module is effectively a translation unit or a collection of |
| 5 | +/// translation units merged together. |
| 6 | +public final class Module: ModuleRef { |
| 7 | + private let llvm: LLVMModuleRef |
| 8 | + |
| 9 | + /// Retrieves the underlying LLVM value object. |
| 10 | + public var moduleRef: LLVMModuleRef { llvm } |
| 11 | + |
| 12 | + /// Init function by LLVM Value |
| 13 | + public init(llvm: LLVMModuleRef) { |
| 14 | + self.llvm = llvm |
| 15 | + } |
| 16 | + |
| 17 | + /// Create a new, empty module in the global context. |
| 18 | + /// |
| 19 | + /// This is equivalent to calling LLVMModuleCreateWithNameInContext with |
| 20 | + /// LLVMGetGlobalContext() as the context parameter. |
| 21 | + /// |
| 22 | + /// Every invocation should be paired with LLVMDisposeModule() or memory will be leaked. |
| 23 | + public init(name: String) { |
| 24 | + llvm = name.withCString { cString in |
| 25 | + LLVMModuleCreateWithName(cString) |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + /// Create a new, empty module in a specific context. |
| 30 | + /// |
| 31 | + /// Every invocation should be paired with LLVMDisposeModule() or memory will be leaked. |
| 32 | + public init(name: String, context: ContextRef) { |
| 33 | + llvm = name.withCString { cString in |
| 34 | + LLVMModuleCreateWithNameInContext(cString, context.contextRef) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + /// Return an exact copy of the specified module. |
| 39 | + public func clone_nodule() -> ModuleRef { |
| 40 | + let new_module = LLVMCloneModule(llvm)! |
| 41 | + return Self(llvm: new_module) |
| 42 | + } |
| 43 | + |
| 44 | + /// Obtain the identifier of a module. |
| 45 | + public var getLLVMModuleIdentifier: String { |
| 46 | + var length: UInt = 0 |
| 47 | + guard let cString = LLVMGetModuleIdentifier(llvm, &length) else { return "" } |
| 48 | + return String(cString: cString) |
| 49 | + } |
| 50 | + |
| 51 | + public func setLLVMModuleIdentifier(module: LLVMModuleRef, identifier: String) { |
| 52 | + identifier.withCString { cString in |
| 53 | + LLVMSetModuleIdentifier(module, cString, identifier.count) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public var getModuleIdentifier: String? { |
| 58 | + var length: UInt = 0 |
| 59 | + guard let cString = LLVMGetModuleIdentifier(llvm, &length) else { |
| 60 | + return nil |
| 61 | + } |
| 62 | + return String(cString: cString) |
| 63 | + } |
| 64 | + |
| 65 | + /// Set the identifier of a module to a string Ident with length Len. |
| 66 | + public func setSourceFileName(fileName: String) { |
| 67 | + fileName.withCString { cString in |
| 68 | + LLVMSetSourceFileName(llvm, cString, fileName.utf8.count) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /// Obtain the module's original source file name. |
| 73 | + public var getSourceFileName: String? { |
| 74 | + var length: size_t = 0 |
| 75 | + guard let cString = LLVMGetSourceFileName(llvm, &length) else { |
| 76 | + return nil |
| 77 | + } |
| 78 | + return String(cString: cString) |
| 79 | + } |
| 80 | + |
| 81 | + /// Set the data layout for a module. |
| 82 | + public func setDataLayout(module: LLVMModuleRef, dataLayout: String) { |
| 83 | + dataLayout.withCString { cString in |
| 84 | + LLVMSetDataLayout(module, cString) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /// Obtain the data layout for a module. |
| 89 | + public var getDataLayout: String? { |
| 90 | + guard let cString = LLVMGetDataLayoutStr(llvm) else { |
| 91 | + return nil |
| 92 | + } |
| 93 | + return String(cString: cString) |
| 94 | + } |
| 95 | + |
| 96 | + /// Set the target triple for a module. |
| 97 | + public func setTarget(triple: String) { |
| 98 | + triple.withCString { cString in |
| 99 | + LLVMSetTarget(llvm, cString) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /// Destroy a module instance. |
| 104 | + /// |
| 105 | + /// This must be called for every created module or memory will be leaked. |
| 106 | + deinit { |
| 107 | + LLVMDisposeModule(llvm) |
| 108 | + } |
| 109 | +} |
0 commit comments