Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support more AtomicRWOps #138

Merged
merged 4 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Text/LLVM/AST.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
{- |
Because this library supports many LLVM versions, it is possible to construct
an AST with the types in this module that only some LLVM versions will accept.
These cases are usually documented in the haddocks for the relevant data types.
tomsmeding marked this conversation as resolved.
Show resolved Hide resolved
When trying to pretty-print constructions that are unsupported by the current
LLVM version, pretty-printing may 'error'.

At the same time, while the AST coverage is fairly extensive, it is also
incomplete: there are some values that new LLVM versions would accept but are
not yet represented here.
-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE DeriveDataTypeable, DeriveFunctor, DeriveGeneric #-}
{-# LANGUAGE PatternGuards #-}
Expand Down Expand Up @@ -994,6 +1005,12 @@ data AtomicRWOp
| AtomicMin
| AtomicUMax
| AtomicUMin
| AtomicFAdd -- ^ Introduced in LLVM 9
| AtomicFSub -- ^ Introduced in LLVM 9
| AtomicFMax -- ^ Introduced in LLVM 15
| AtomicFMin -- ^ Introduced in LLVM 15
| AtomicUIncWrap -- ^ Introduced in LLVM 16
| AtomicUDecWrap -- ^ Introduced in LLVM 16
deriving (Data, Eq, Enum, Generic, Ord, Show, Typeable)

data AtomicOrdering
Expand Down
23 changes: 23 additions & 0 deletions src/Text/LLVM/PP.hs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ ppLLVM38 = withConfig Config { cfgVer = llvmV3_8 }
llvmVer :: (?config :: Config) => LLVMVer
llvmVer = cfgVer ?config

llvmVerToString :: LLVMVer -> String
llvmVerToString 0 = "3.5"
llvmVerToString 1 = "3.6"
llvmVerToString 2 = "3.7"
llvmVerToString 3 = "3.8"
llvmVerToString n
| n >= 4 = show n
| otherwise = error $ "Invalid LLVMVer: " ++ show n

-- | This is a helper function for when a list of parameters is gated by a
-- condition (usually the llvmVer value).
when' :: Monoid a => Bool -> a -> a
Expand Down Expand Up @@ -547,6 +556,12 @@ ppAtomicOp AtomicMax = "max"
ppAtomicOp AtomicMin = "min"
ppAtomicOp AtomicUMax = "umax"
ppAtomicOp AtomicUMin = "umin"
ppAtomicOp AtomicFAdd = onlyOnLLVM 9 "AtomicFAdd" "fadd"
ppAtomicOp AtomicFSub = onlyOnLLVM 9 "AtomicFSub" "fsub"
ppAtomicOp AtomicFMax = onlyOnLLVM 15 "AtomicFMax" "fmax"
ppAtomicOp AtomicFMin = onlyOnLLVM 15 "AtomicFMin" "fmin"
ppAtomicOp AtomicUIncWrap = onlyOnLLVM 16 "AtomicUIncWrap" "uincwrap"
ppAtomicOp AtomicUDecWrap = onlyOnLLVM 16 "AtomicUDecWrap" "udecwrap"

ppScope :: Fmt (Maybe String)
ppScope Nothing = empty
Expand Down Expand Up @@ -1353,3 +1368,11 @@ structBraces body = char '{' <+> body <+> char '}'

ppMaybe :: Fmt a -> Fmt (Maybe a)
ppMaybe = maybe empty

-- | Throw an error if the ?config version is older than the given version. The
tomsmeding marked this conversation as resolved.
Show resolved Hide resolved
-- String indicates the constructor that is unavailable.
tomsmeding marked this conversation as resolved.
Show resolved Hide resolved
onlyOnLLVM :: (?config :: Config) => LLVMVer -> String -> a -> a
onlyOnLLVM fromVer name
| llvmVer >= fromVer = id
| otherwise = error $ name ++ " is supported only on LLVM >= "
++ llvmVerToString fromVer
Loading