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 unnamed_addr for functions #377

Open
wants to merge 2 commits into
base: llvm-9
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions llvm-hs-pure/src/LLVM/AST/Global.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ data Global
linkage :: L.Linkage,
visibility :: V.Visibility,
dllStorageClass :: Maybe DLL.StorageClass,
unnamedAddr :: Maybe UnnamedAddr,
callingConvention :: CC.CallingConvention,
returnAttributes :: [A.ParameterAttribute],
returnType :: Type,
Expand Down Expand Up @@ -124,6 +125,7 @@ functionDefaults =
linkage = L.External,
visibility = V.Default,
dllStorageClass = Nothing,
unnamedAddr = Nothing,
callingConvention = CC.C,
returnAttributes = [],
returnType = error "function return type not defined",
Expand Down
8 changes: 6 additions & 2 deletions llvm-hs/src/LLVM/Internal/Module.hs
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,15 @@ createModuleFromAST context@(Context c) (A.Module moduleId sourceFileName dataLa
setThreadLocalMode a' (A.G.threadLocalMode a)
(liftIO . FFI.setAliasee a') =<< encodeM (A.G.aliasee a)
return (FFI.upCast a')
(A.Function _ _ _ cc rAttrs resultType fName (args, isVarArgs) attrs _ _ _ gc prefix blocks personality metadata) -> do
fn@(A.Function _ _ _ _ cc rAttrs resultType fName (args, isVarArgs) attrs _ _ _ gc prefix blocks personality metadata) -> do
typ <- encodeM $ A.FunctionType resultType [t | A.Parameter t _ _ <- args] isVarArgs
f <- liftIO . withName fName $ \fName -> FFI.addFunction ffiMod fName typ
defineGlobal fName f
cc <- encodeM cc
liftIO $ FFI.setFunctionCallingConvention f cc
liftIO $ do
FFI.setFunctionCallingConvention f cc
hua <- encodeM (A.G.unnamedAddr fn)
FFI.setUnnamedAddr (FFI.upCast f) hua
setFunctionAttributes f (AttributeList attrs rAttrs [pa | A.Parameter _ _ pa <- args])
setPrefixData f prefix
setSection f (A.G.section g)
Expand Down Expand Up @@ -487,6 +490,7 @@ decodeFunctions mod = do
<$> getLinkage f
<*> getVisibility f
<*> getDLLStorageClass f
<*> (liftIO $ decodeM =<< FFI.getUnnamedAddr (FFI.upCast f))
<*> (liftIO $ decodeM =<< FFI.getFunctionCallingConvention f)
<*> return rAttrs
<*> return returnType
Expand Down
24 changes: 24 additions & 0 deletions llvm-hs/test/LLVM/Test/Global.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,29 @@ tests = testGroup "Global" [
gn (G.Function {}) = "function"
gn (G.GlobalVariable {}) = "variable"
name = gn g <> ", align " <> show a <> (maybe "" ((" section " <>) . ByteString.unpack . fromShort) s)
],
testGroup "UnnamedAddr" [
testCase name $ withContext $ \context -> do
let ast = Module "<string>" "<string>" Nothing Nothing [ GlobalDefinition g ]
ast' <- withModuleFromAST context ast moduleAST
ast' @?= ast
| a <- [Nothing, Just LocalAddr, Just GlobalAddr],
g <- [
globalVariableDefaults {
G.name = UnName 0,
G.type' = i32,
G.unnamedAddr = a
},
functionDefaults {
G.returnType = A.T.void,
G.name = UnName 0,
G.parameters = ([], False),
G.unnamedAddr = a
}
],
let
gn (G.Function {}) = "function"
gn (G.GlobalVariable {}) = "variable"
name = gn g <> ", unnamedAddr " <> show a
]
]
1 change: 1 addition & 0 deletions llvm-hs/test/LLVM/Test/Optimization.hs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ tests = testGroup "Optimization" [
G.name = Name "foo",
G.parameters = ([Parameter i32 (Name "x") []], False),
G.functionAttributes = [Left (A.GroupID 0)],
G.unnamedAddr = Just LocalAddr,
G.basicBlocks = [
BasicBlock (Name "here") [
] (
Expand Down