From b67d41f9fa3d74c1df50f994df0ee23e6e617e95 Mon Sep 17 00:00:00 2001 From: Lucian Radu Teodorescu Date: Tue, 3 Dec 2024 19:46:58 +0200 Subject: [PATCH] Also add atomic native function support in the frontend parser. --- Sources/FrontEnd/BuiltinFunction.swift | 31 ++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/Sources/FrontEnd/BuiltinFunction.swift b/Sources/FrontEnd/BuiltinFunction.swift index 976d3e4f6..41635bf9f 100644 --- a/Sources/FrontEnd/BuiltinFunction.swift +++ b/Sources/FrontEnd/BuiltinFunction.swift @@ -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) + + 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))) default: return nil } @@ -335,6 +360,12 @@ private func take( } } +/// 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[..) -> BuiltinType? { stream.popFirst().flatMap(BuiltinType.init(_:))