Skip to content

Commit 40c75f7

Browse files
committed
Context API completed. Updated readme
1 parent 47fa270 commit 40c75f7

File tree

4 files changed

+79
-30
lines changed

4 files changed

+79
-30
lines changed

README.md

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,58 @@
1-
# Swift LLVM codegen
1+
# `llvm-api-swift`
22

3-
`SwiftLLVMCodegen` is a pure Swift interface to the [LLVM API](https://llvm.org/docs/) and its associated libraries. It provides
4-
native, easy-to-use components to create compilers codegen backend. It contains LLVM bindings,
5-
components and toolset for `Codegen` implementation on Swift.
3+
`llvm-api-swift` is a library representing Swift LLVM API, a pure Swift interface to the [LLVM API](https://llvm.org/docs/) and its associated libraries.
4+
It provides native, easy-to-use components to create compilers codegen backend. It contains LLVM bindings,
5+
components and toolset for efficiently use LLVM as compilers backend implementation on Swift.
66

7-
## Status
7+
### Compatibility with LLVM-C API
88

9-
In progress...
9+
When creating the library, we were guided by **full compatibility** with [LLVM-C API](https://llvm.org/doxygen/group__LLVMC.html).
10+
For this reason, structurally the library tries to inherit the `LLVM-C API` tree, thereby dividing the library structure into subdirectories.
11+
And filling the components also with the appropriate `LLVM-C API`.
12+
When implementing Swift types, we were guided by the approach of abstracting away from C types, completely transforming them into Swift types.
13+
At the same time adhering to the principles of a safety and reliability implementation - without explicit memory management, means of safe techniques, functions provided by Swift.
1014

11-
### Supported LLVM version
1215

13-
**v17.0**
16+
### Requirements
17+
- Supported OS: MacOS 12.0 or above
18+
- Swift lang: 5.9 or above
19+
20+
- Install LLVM:
21+
```
22+
brew install llvm
23+
```
24+
- Set Environment variables, that provided during `brew` llvm install
25+
- initialize `pkg-config`:
26+
```
27+
./utils/make-pkg-config.swift
28+
```
1429

15-
## Inspiration
30+
**NOTE:** Every time, when LLVM is updated or installed new version should be run `pkg-config` step.
1631

17-
Inspired by [SwiftLLVM](https://github.com/llvm-swift/LLVMSwift).
32+
### Supported LLVM version
1833

19-
## LICENS: [MIT](LICENSE)
34+
- [x] v17.0
35+
36+
37+
### Troubleshooting
38+
39+
If `LLVM-C` head files during compulation doesn't found, make sure that you:
40+
- installed LLVM
41+
```
42+
llv --version
43+
```
44+
- Set environment variables for your terminal (for example in `.zshrc`). To get instruction about variables run:
45+
```
46+
brew info llvm
47+
```
48+
- run pkg-config utility:
49+
```
50+
./utils/make-pkg-config.swift
51+
```
52+
- if utility doesn't have access right to run:
53+
```
54+
chmod +x ./utils/make-pkg-config.swift
55+
./utils/make-pkg-config.swift
56+
```
57+
58+
### LICENS: [MIT](LICENSE)

llvm-codegen/CLLVM/bridge.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include <llvm-c/BitReader.h>
1414
#include <llvm-c/BitWriter.h>
1515
#include <llvm-c/Comdat.h>
16-
#include <llvm-c/Core.h>
16+
#include <llvm-c/Core.h>
1717
#include <llvm-c/DataTypes.h>
1818
#include <llvm-c/DebugInfo.h>
1919
#include <llvm-c/Disassembler.h>

llvm-codegen/LLVM/Context.swift

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,17 @@ public class Context: ContextRef {
101101
/// Return a string representation of the DiagnosticInfo. Use
102102
/// LLVMDisposeMessage to free the string.
103103
public func getDiagInfoDescription(diagnosticInfo: DiagnosticInfoRef) -> String? {
104-
if let cString = LLVMGetDiagInfoDescription(diagnosticInfo.diagnosticInfoRef) {
105-
return String(cString: cString)
106-
} else {
107-
return nil
108-
}
104+
guard let cString = LLVMGetDiagInfoDescription(diagnosticInfo.diagnosticInfoRef) else { return nil }
105+
return String(cString: cString)
109106
}
110107

111108
/// Return an enum LLVMDiagnosticSeverity.
112109
public func getDiagInfoSeverity(diagnosticInfo: DiagnosticInfoRef) -> LLVMDiagnosticSeverity {
113110
LLVMGetDiagInfoSeverity(diagnosticInfo.diagnosticInfoRef)
114111
}
115112

113+
/// Get Metadata KindId by name in current Context.
114+
/// Useful for working with Metadata
116115
public func getMDKindIDInContext(name: String) -> UInt32 {
117116
name.withCString { cString in
118117
LLVMGetMDKindIDInContext(llvm, cString, UInt32(name.utf8.count))
@@ -121,7 +120,7 @@ public class Context: ContextRef {
121120

122121
public func getMDKindID(name: String) -> UInt32 {
123122
name.withCString { cString in
124-
LLVMGetMDKindIDInContext(llvm, cString, UInt32(name.utf8.count))
123+
LLVMGetMDKindID(cString, UInt32(name.utf8.count))
125124
}
126125
}
127126

@@ -192,33 +191,44 @@ public class Context: ContextRef {
192191
//===============================
193192

194193
/// Get the string attribute's kind.
195-
public func getStringAttributeKind() {
196-
// const char *LLVMGetStringAttributeKind(LLVMAttributeRef A, unsigned *Length);
194+
public func getStringAttributeKind(attributeRef: AttributeRef, length: UInt32) -> String? {
195+
var mutLength = length
196+
guard let cString = withUnsafeMutablePointer(to: &mutLength, { lengthPtr in
197+
LLVMGetStringAttributeKind(attributeRef.attributeRef, lengthPtr)
198+
}) else { return nil }
199+
return String(cString: cString)
197200
}
198201

199202
/// Get the string attribute's value.
200-
public func getStringAttributeValue() {
201-
// const char *LLVMGetStringAttributeValue(LLVMAttributeRef A, unsigned *Length);
203+
public func getStringAttributeValue(attributeRef: AttributeRef, length: UInt32) -> String? {
204+
var mutLength = length
205+
guard let cString = withUnsafeMutablePointer(to: &mutLength, { lengthPtr in
206+
LLVMGetStringAttributeValue(attributeRef.attributeRef, lengthPtr)
207+
}) else { return nil }
208+
return String(cString: cString)
202209
}
203210

204211
/// Check for the types of attributes.
205-
public func isEnumAttribute() {
206-
// LLVMBool LLVMIsEnumAttribute(LLVMAttributeRef A);
212+
public func isEnumAttribute(attributeRef: AttributeRef) -> Bool {
213+
return LLVMIsEnumAttribute(attributeRef.attributeRef) != 0
207214
}
208215

209216
/// Check for the types of attributes.
210-
public func isStringAttribute() {
211-
// LLVMBool LLVMIsStringAttribute(LLVMAttributeRef A);
217+
public func isStringAttribute(attributeRef: AttributeRef) -> Bool {
218+
return LLVMIsStringAttribute(attributeRef.attributeRef) != 0
212219
}
213220

214221
/// Check for the types of attributes.
215-
public func isTypeAttribute() {
216-
// LLVMBool LLVMIsTypeAttribute(LLVMAttributeRef A);
222+
public func isTypeAttribute(attributeRef: AttributeRef) -> Bool {
223+
return LLVMIsTypeAttribute(attributeRef.attributeRef) != 0
217224
}
218225

219226
/// Obtain a Type from a context by its registered name.
220-
public func getTypeByName2() {
221-
// LLVMTypeRef LLVMGetTypeByName2(LLVMContextRef C, const char *Name);
227+
public func getTypeByName2(name: String) -> TypeRef? {
228+
name.withCString { cString in
229+
guard let typeRef = LLVMGetTypeByName2(llvm, cString) else { return nil }
230+
return Types(llvm: typeRef)
231+
}
222232
}
223233

224234
/// Deinitialize this value and dispose of its resources.

utils/make-pkg-config.swift

100644100755
File mode changed.

0 commit comments

Comments
 (0)