Skip to content

Commit 169d1de

Browse files
committed
Added Constants
1 parent 21ad776 commit 169d1de

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

llvm-api/LLVM/Core/BasicBlock.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ public struct BasicBlock: BasicBlockRef {
9797
self.context = context
9898
}
9999

100+
/// Create a new basic block without inserting it into a function.
101+
public static func createBasicBlockInContext(context: Context, name: String) -> BasicBlockRef? {
102+
guard let blockRef = LLVMCreateBasicBlockInContext(context.contextRef, name) else { return nil }
103+
return BasicBlock(llvm: blockRef)
104+
}
105+
100106
/// Given that this block and a given block share a parent function, move this
101107
/// block before the given block in that function's basic block list.
102108
///
@@ -146,7 +152,7 @@ public struct BasicBlock: BasicBlockRef {
146152
/// Returns the first instruction in the basic block, if it exists.
147153
public static func getFirstInstruction(basicBlock: BasicBlockRef) -> ValueRef? {
148154
guard let val = LLVMGetFirstInstruction(basicBlock.basicBlockRef) else { return nil }
149-
return Values(llvm: val)
155+
return Value(llvm: val)
150156
}
151157

152158
/// Returns the first instruction in the basic block, if it exists.
@@ -157,7 +163,7 @@ public struct BasicBlock: BasicBlockRef {
157163
/// Returns the first instruction in the basic block, if it exists.
158164
public static func getLastInstruction(basicBlock: BasicBlockRef) -> ValueRef? {
159165
guard let val = LLVMGetLastInstruction(basicBlock.basicBlockRef) else { return nil }
160-
return Values(llvm: val)
166+
return Value(llvm: val)
161167
}
162168

163169
/// Returns the parent function of this basic block, if it exists.
@@ -249,7 +255,7 @@ public struct BasicBlock: BasicBlockRef {
249255
/// Convert a basic block instance to a value type.
250256
public static func basicBlockAsValue(basicBlockRef: BasicBlockRef) -> ValueRef? {
251257
guard let valueRef = LLVMBasicBlockAsValue(basicBlockRef.basicBlockRef) else { return nil }
252-
return Values(llvm: valueRef)
258+
return Value(llvm: valueRef)
253259
}
254260

255261
/// Determine whether an LLVMValueRef is itself a basic block.
@@ -347,14 +353,14 @@ public struct BasicBlock: BasicBlockRef {
347353
/// Returns the terminator instruction for current block. If a basic block is well formed or `nil` if it is not well formed.
348354
public var getBasicBlockTerminator: ValueRef? {
349355
guard let valueRef = LLVMGetBasicBlockTerminator(llvm) else { return nil }
350-
return Values(llvm: valueRef)
356+
return Value(llvm: valueRef)
351357
}
352358

353359
/// Returns the terminator instruction if a basic block is well formed or `nil` if it is not well formed.
354360
/// The returned LLVMValueRef corresponds to an llvm::Instruction.
355361
public static func getBasicBlockTerminator(basicBlockRef: BasicBlockRef) -> ValueRef? {
356362
guard let valueRef = LLVMGetBasicBlockTerminator(basicBlockRef.basicBlockRef) else { return nil }
357-
return Values(llvm: valueRef)
363+
return Value(llvm: valueRef)
358364
}
359365
}
360366

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import CLLVM
2+
3+
/// This section contains APIs for interacting with LLVMValueRef that correspond to llvm::Constant instances. More...
4+
public enum Constants {
5+
/// Obtain a constant value referring to the null instance of a type.
6+
public static func constNull(typeRef: TypeRef) -> Value? {
7+
guard let valueRef = LLVMConstNull(typeRef.typeRef) else { return nil }
8+
return Value(llvm: valueRef)
9+
}
10+
11+
/// Obtain a constant value referring to the instance of a type consisting of all ones.
12+
/// This is only valid for integer types.
13+
public static func constAllOnes(typeRef: TypeRef) -> Value? {
14+
guard let valueRef = LLVMConstAllOnes(typeRef.typeRef) else { return nil }
15+
return Value(llvm: valueRef)
16+
}
17+
18+
/// Obtain a constant value referring to an undefined value of a type.
19+
public static func getUndef(typeRef: TypeRef) -> Value? {
20+
guard let valueRef = LLVMGetUndef(typeRef.typeRef) else { return nil }
21+
return Value(llvm: valueRef)
22+
}
23+
24+
/// Obtain a constant value referring to a poison value of a type.
25+
public static func getPoison(typeRef: TypeRef) -> Value? {
26+
guard let valueRef = LLVMGetPoison(typeRef.typeRef) else { return nil }
27+
return Value(llvm: valueRef)
28+
}
29+
30+
// Determine whether a value instance is null.
31+
public static func constPointerNull(valueRef: ValueRef) -> Bool {
32+
LLVMIsNull(valueRef.valueRef) != 0
33+
}
34+
35+
/// Obtain a constant that is a constant pointer pointing to NULL for a specified type.
36+
public static func constPointerNull(typeRef: TypeRef) -> Value? {
37+
guard let valueRef = LLVMConstPointerNull(typeRef.typeRef) else { return nil }
38+
return Value(llvm: valueRef)
39+
}
40+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import CLLVM
22

3-
public struct Values: ValueRef {
3+
public struct Value: ValueRef {
44
let llvm: LLVMValueRef
55

66
/// Retrieves the underlying LLVM type object.

0 commit comments

Comments
 (0)