Skip to content

Commit 938ff14

Browse files
committed
Implement inline assembly add for all architectures and bump swift-tools-version to 6.1
1 parent e243e2f commit 938ff14

5 files changed

Lines changed: 26 additions & 12 deletions

File tree

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version: 5.9
1+
// swift-tools-version: 6.1
22
import PackageDescription
33

44
let package = Package(

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ and add the `skipstone` plugin in order for Skip to generate gradle build for th
5757

5858

5959
```swift
60-
// swift-tools-version: 5.9
60+
// swift-tools-version: 6.1
6161
import PackageDescription
6262

6363
let package = Package(

Sources/LibCLibrary/src/demo.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,32 @@ short add_with_assembly(short a, short b) {
2020
short result;
2121

2222
#if __aarch64__ // ARM 64-bit
23-
asm volatile ("add %w[a], %w[a], %w[b]; mov %w[a], %w[result]"
23+
// add the two source W (32-bit) registers, writing the sum to the result register
24+
asm ("add %w[result], %w[a], %w[b]"
2425
: [result] "=r" (result)
25-
: [a] "r" (a), [b] "r" (b)
26-
: "cc");
26+
: [a] "r" (a), [b] "r" (b));
2727
#elif __i386__ // Intel 32-bit
28-
result = a + b;
28+
// tie `a` into the result register, then add `b` into it (AT&T order: addw src, dst)
29+
asm ("addw %[b], %[result]"
30+
: [result] "=r" (result)
31+
: [b] "r" (b), "0" (a)
32+
: "cc");
2933
#elif __x86_64__ // Intel 64-bit
30-
result = a + b;
34+
// tie `a` into the result register, then add `b` into it (AT&T order: addw src, dst)
35+
asm ("addw %[b], %[result]"
36+
: [result] "=r" (result)
37+
: [b] "r" (b), "0" (a)
38+
: "cc");
3139
#elif __arm__ // ARM 32-bit
32-
result = a + b;
33-
#elif __riscv_64 // RISC-V 64-bit (future)
34-
result = a + b;
40+
// add Rn (a) and Rm (b), writing the sum to Rd (result)
41+
asm ("add %[result], %[a], %[b]"
42+
: [result] "=r" (result)
43+
: [a] "r" (a), [b] "r" (b));
44+
#elif defined(__riscv) && (__riscv_xlen == 64) // RISC-V 64-bit (future)
45+
// add rs1 (a) and rs2 (b), writing the sum to rd (result)
46+
asm ("add %[result], %[a], %[b]"
47+
: [result] "=r" (result)
48+
: [a] "r" (a), [b] "r" (b));
3549
#else
3650
#error "Unsupported architecture"
3751
#endif

Sources/SkipCDemo/SkipCDemo.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import LibCLibrary
99
/// `DemoLibrary` is a Swift encapsulation of the embedded C library's functions and structures.
1010
internal final class DemoLibrary {
1111
/// The singleton library instance, registered using JNA to map the Kotlin functions to their native equivalents
12-
static let instance = registerNatives(DemoLibrary(), frameworkName: "SkipCDemo", libraryName: "clibrary")
12+
nonisolated(unsafe) static let instance = registerNatives(DemoLibrary(), frameworkName: "SkipCDemo", libraryName: "clibrary")
1313

1414
// Functions marked with "SKIP EXTERN" will eliminate the bodies and
1515
// mark the transpiled Kotlin functions as "extern",

Tests/SkipCDemoTests/SkipCDemoTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import XCTest
44
import Foundation
55
@testable import SkipCDemo
66

7-
fileprivate let lib: DemoLibrary = DemoLibrary.instance
7+
nonisolated(unsafe) fileprivate let lib: DemoLibrary = DemoLibrary.instance
88

99
final class SkipCDemoTests: XCTestCase {
1010
func testSkipCDemo() throws {

0 commit comments

Comments
 (0)