Skip to content

Commit

Permalink
Extend Modules
Browse files Browse the repository at this point in the history
  • Loading branch information
mrLSD committed Jul 17, 2024
1 parent 28627f8 commit 7c12785
Show file tree
Hide file tree
Showing 5 changed files with 405 additions and 295 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/swift.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
swift --version
- name: Build
run: swift build -v
run: swift build -Xswiftc -DCLI_BUILD
# - name: Run tests
# run: swift test -v

15 changes: 10 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
import Foundation
import PackageDescription

// Get LLVM flags and version
let (cFlags, linkFlags, _version) = try! getLLVMConfig()
#if CLI_BUILD
let (cFlags, linkFlags, _version) = try! getLLVMConfig()
#else
let (cFlags, linkFlags, _version) = ([String](), [String](), [Int]())
#endif

let package = Package(
name: "llvm-api",
Expand All @@ -15,17 +19,18 @@ let package = Package(
targets: [
.systemLibrary(
name: "CLLVM",
path: "llvm-api/CLLVM"
path: "llvm-api/CLLVM",
pkgConfig: "cllvm"
),
.target(
name: "LLVM",
dependencies: ["CLLVM"],
path: "llvm-api/LLVM",
cSettings: [
.unsafeFlags(cFlags)
.unsafeFlags(cFlags),
],
linkerSettings: [
.unsafeFlags(linkFlags)
.unsafeFlags(linkFlags),
]
),
]
Expand Down
19 changes: 8 additions & 11 deletions llvm-api/LLVM/Core/Context.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import CLLVM
/// context, it is recommended that each thread of execution be assigned a unique
/// context. LLVM's core infrastructure and API provides no locking guarantees
/// and no atomicity guarantees.
public class Context: ContextRef {
public final class Context: ContextRef {
private let llvm: LLVMContextRef

/// Retrieves the underlying LLVM type object.
Expand Down Expand Up @@ -185,20 +185,17 @@ public class Context: ContextRef {
}

/// Get the string attribute's kind.
public static func getStringAttributeKind(attributeRef: AttributeRef, length: UInt32) -> String? {
var mutLength = length
guard let cString = withUnsafeMutablePointer(to: &mutLength, { lengthPtr in
LLVMGetStringAttributeKind(attributeRef.attributeRef, lengthPtr)
}) else { return nil }
public static func getStringAttributeKind(attributeRef: AttributeRef) -> String? {
var length: UInt32 = 0
guard let cString = LLVMGetStringAttributeKind(attributeRef.attributeRef, &length) else { return nil }
return String(cString: cString)
}

/// Get the string attribute's value.
public static func getStringAttributeValue(attributeRef: AttributeRef, length: UInt32) -> String? {
var mutLength = length
guard let cString = withUnsafeMutablePointer(to: &mutLength, { lengthPtr in
LLVMGetStringAttributeValue(attributeRef.attributeRef, lengthPtr)
}) else { return nil }
public static func getStringAttributeValue(attributeRef: AttributeRef) -> String? {
var length: UInt32 = 0
guard let cString =
LLVMGetStringAttributeValue(attributeRef.attributeRef, &length) else { return nil }
return String(cString: cString)
}

Expand Down
109 changes: 109 additions & 0 deletions llvm-api/LLVM/Core/Modules.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import CLLVM

/// Modules represent the top-level structure in an LLVM program. An LLVM
/// module is effectively a translation unit or a collection of
/// translation units merged together.
public final class Module: ModuleRef {
private let llvm: LLVMModuleRef

/// Retrieves the underlying LLVM value object.
public var moduleRef: LLVMModuleRef { llvm }

/// Init function by LLVM Value
public init(llvm: LLVMModuleRef) {
self.llvm = llvm
}

/// Create a new, empty module in the global context.
///
/// This is equivalent to calling LLVMModuleCreateWithNameInContext with
/// LLVMGetGlobalContext() as the context parameter.
///
/// Every invocation should be paired with LLVMDisposeModule() or memory will be leaked.
public init(name: String) {
llvm = name.withCString { cString in
LLVMModuleCreateWithName(cString)
}
}

/// Create a new, empty module in a specific context.
///
/// Every invocation should be paired with LLVMDisposeModule() or memory will be leaked.
public init(name: String, context: ContextRef) {
llvm = name.withCString { cString in
LLVMModuleCreateWithNameInContext(cString, context.contextRef)
}
}

/// Return an exact copy of the specified module.
public func clone_nodule() -> ModuleRef {
let new_module = LLVMCloneModule(llvm)!
return Self(llvm: new_module)
}

/// Obtain the identifier of a module.
public var getLLVMModuleIdentifier: String {
var length: UInt = 0
guard let cString = LLVMGetModuleIdentifier(llvm, &length) else { return "" }
return String(cString: cString)
}

public func setLLVMModuleIdentifier(module: LLVMModuleRef, identifier: String) {
identifier.withCString { cString in
LLVMSetModuleIdentifier(module, cString, identifier.count)
}
}

public var getModuleIdentifier: String? {
var length: UInt = 0
guard let cString = LLVMGetModuleIdentifier(llvm, &length) else {
return nil
}
return String(cString: cString)
}

/// Set the identifier of a module to a string Ident with length Len.
public func setSourceFileName(fileName: String) {
fileName.withCString { cString in
LLVMSetSourceFileName(llvm, cString, fileName.utf8.count)
}
}

/// Obtain the module's original source file name.
public var getSourceFileName: String? {
var length: size_t = 0
guard let cString = LLVMGetSourceFileName(llvm, &length) else {
return nil
}
return String(cString: cString)
}

/// Set the data layout for a module.
public func setDataLayout(module: LLVMModuleRef, dataLayout: String) {
dataLayout.withCString { cString in
LLVMSetDataLayout(module, cString)
}
}

/// Obtain the data layout for a module.
public var getDataLayout: String? {
guard let cString = LLVMGetDataLayoutStr(llvm) else {
return nil
}
return String(cString: cString)
}

/// Set the target triple for a module.
public func setTarget(triple: String) {
triple.withCString { cString in
LLVMSetTarget(llvm, cString)
}
}

/// Destroy a module instance.
///
/// This must be called for every created module or memory will be leaked.
deinit {
LLVMDisposeModule(llvm)
}
}
Loading

0 comments on commit 7c12785

Please sign in to comment.