Skip to content

Commit

Permalink
Adde PointerType
Browse files Browse the repository at this point in the history
  • Loading branch information
mrLSD committed Dec 30, 2023
1 parent 7b79741 commit 6718c2d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
4 changes: 2 additions & 2 deletions llvm-codegen/LLVM/AddressSpace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ import CLLVM
/// any value. In practice, LLVM guarantees only 24 bits of precision, though higher address space
/// identifiers may succeed in being properly represented.
public struct AddressSpace: Equatable {
let rawValue: Int
let rawValue: UInt32

/// LLVM's default address space.
public static let zero = AddressSpace(0)

/// Creates and initializes an address space with the given identifier.
/// - Parameter identifier: The raw, integral address space identifier.
public init(_ identifier: Int) {
public init(_ identifier: UInt32) {
self.rawValue = identifier
}
}
73 changes: 73 additions & 0 deletions llvm-codegen/LLVM/Types/Pointer.swift
Original file line number Diff line number Diff line change
@@ -1 +1,74 @@
import CLLVM

/// `PointerType` is used to specify memory locations. Pointers are commonly
/// used to reference objects in memory.
///
/// `PointerType` may have an optional address space attribute defining the
/// numbered address space where the pointed-to object resides. The default
/// address space is number zero. The semantics of non-zero address spaces are
/// target-specific.
///
/// Note that LLVM does not permit pointers to void `(void*)` nor does it permit
/// pointers to labels `(label*)`. Use `ptr` instead.
public struct PointerType: TypeRef {
var llvm: LLVMTypeRef

/// Returns the context associated with this type.
public let context: Context?

/// Retrieves the underlying LLVM type object.
public var typeRef: LLVMTypeRef { llvm }

/// Retrieves the type of the value being pointed to.
public let pointee: TypeRef?

/// Retrieves the address space where the pointed-to object resides.
public let addressSpace: AddressSpace

/// Create a `PointerType` pointer type that points to a defined type and an optional address space.
/// The created type will exist in the context that its pointee type exists in .
///
/// - parameter pointee: The type of the pointed-to object.
/// - parameter addressSpace: The optional address space where the pointed-to object resides.
/// - note: The context of this type is taken from it's pointee
public init(pointee: TypeRef, addressSpace: AddressSpace = .zero) {
if pointee is VoidType || pointee is LabelType {
fatalError("Attempted to form pointer to unexpected Void or Label type - use pointer to IntType.int8 instead")
}
self.pointee = pointee
self.addressSpace = addressSpace
llvm = LLVMPointerType(pointee.typeRef, UInt32(addressSpace.rawValue))
context = nil
}

/// Create an opaque pointer type in a context.
public init(in context: Context, addressSpace: AddressSpace = .zero) {
pointee = nil
self.addressSpace = addressSpace
llvm = LLVMPointerTypeInContext(context.contextRef, UInt32(addressSpace.rawValue))
self.context = context
}

/// Determine whether a pointer is opaque.
/// True if this is an instance of an opaque PointerType.
public static func getPointerTypeIsOpaque(ty: TypeRef) -> Bool {
LLVMPointerTypeIsOpaque(ty.typeRef) != 0
}

/// Get the address space of a pointer type.
/// This only works on types that represent pointers.
public static func getPointerAddressSpace(ty: TypeRef) -> AddressSpace {
AddressSpace(LLVMGetPointerAddressSpace(ty.typeRef))
}

/// Get the element type of an Pointer type.
public static func getElementType(ty: TypeRef) -> TypeRef {
Types(typeRef: LLVMGetElementType(ty.typeRef)!)
}
}

extension PointerType: Equatable {
public static func == (lhs: PointerType, rhs: PointerType) -> Bool {
return lhs.typeRef == rhs.typeRef
}
}
5 changes: 4 additions & 1 deletion llvm-codegen/LLVM/Types/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ public extension TypeRef {
case .functionTypeKind: fatalError("unknown type kind \(ty.getTypeKind)")
case .structTypeKind: fatalError("unknown type kind \(ty.getTypeKind)")
case .arrayTypeKind: return ArrayType(typeRef: self)
case .pointerTypeKind: fatalError("unknown type kind \(ty.getTypeKind)")
case .pointerTypeKind:
let pointee = PointerType.getElementType(ty: self)
let addressSpace = PointerType.getPointerAddressSpace(ty: self)
return PointerType(pointee: pointee, addressSpace: addressSpace)
case .vectorTypeKind: return VectorType(typeRef: self)
case .metadataTypeKind: return MetadataType(typeRef: self, context: ty.getTypeContext)
case .x86_MMXTypeKind: return X86MMXType(typeRef: self, context: ty.getTypeContext)
Expand Down

0 comments on commit 6718c2d

Please sign in to comment.