Skip to content

Commit

Permalink
Also add atomic native function support in the frontend parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
lucteo committed Dec 3, 2024
1 parent 708cd66 commit b67d41f
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Sources/FrontEnd/BuiltinFunction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,31 @@ extension BuiltinFunction {
guard let t = builtinType(&tokens) else { return nil }
self = .init(name: .llvm(.zeroinitializer(t)))

case "atomic":
self.init(atomic: n)

Check warning on line 262 in Sources/FrontEnd/BuiltinFunction.swift

View check run for this annotation

Codecov / codecov/patch

Sources/FrontEnd/BuiltinFunction.swift#L262

Added line #L262 was not covered by tests

default:
return nil
}
}

/// Creates an atomic built-in function named `n` or returns `nil` if `n` isn't a valid atomic builtin name.
private init?(atomic n: String) {
guard let (fs, ts) = splitLastUnderscore(n) else { return nil }
guard let t = BuiltinType.init(ts) else { return nil }
switch fs {
case "atomic_store_relaxed":
self = .init(name: .llvm(.atomic_store_relaxed(t)))
case "atomic_store_release":
self = .init(name: .llvm(.atomic_store_release(t)))
case "atomic_store_seqcst":
self = .init(name: .llvm(.atomic_store_seqcst(t)))
case "atomic_load_relaxed":
self = .init(name: .llvm(.atomic_load_relaxed(t)))
case "atomic_load_acquire":
self = .init(name: .llvm(.atomic_load_acquire(t)))
case "atomic_load_seqcst":
self = .init(name: .llvm(.atomic_load_seqcst(t)))

Check warning on line 285 in Sources/FrontEnd/BuiltinFunction.swift

View check run for this annotation

Codecov / codecov/patch

Sources/FrontEnd/BuiltinFunction.swift#L270-L285

Added lines #L270 - L285 were not covered by tests
default:
return nil
}
Expand Down Expand Up @@ -335,6 +360,12 @@ private func take<T: RawRepresentable>(
}
}

/// Splits `s` into a pair `(prefix, suffix)` at the last underscore character, or returns `nil`.
private func splitLastUnderscore(_ s: String) -> (String, String)? {
guard let i = s.lastIndex(of: "_") else { return nil }
return (String(s[..<i]), String(s[s.index(after: i)...]))
}

Check warning on line 367 in Sources/FrontEnd/BuiltinFunction.swift

View check run for this annotation

Codecov / codecov/patch

Sources/FrontEnd/BuiltinFunction.swift#L364-L367

Added lines #L364 - L367 were not covered by tests

/// Returns a built-in type parsed from `stream`.
private func builtinType(_ stream: inout ArraySlice<Substring>) -> BuiltinType? {
stream.popFirst().flatMap(BuiltinType.init(_:))
Expand Down

0 comments on commit b67d41f

Please sign in to comment.