Skip to content

Commit 7fddf8e

Browse files
author
Brian Huffman
committed
Implement function llvm_sizeof : LLVMModule -> LLVMType -> Int.
Fixes #803.
1 parent bacae1a commit 7fddf8e

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/SAWScript/Crucible/LLVM/Builtins.hs

+17
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ module SAWScript.Crucible.LLVM.Builtins
5555
, crucible_symbolic_alloc
5656
, crucible_alloc_global
5757
, crucible_fresh_expanded_val
58+
, llvm_sizeof
5859

5960
--
6061
-- These function are common to LLVM & JVM implementation (not for external use)
@@ -1662,6 +1663,22 @@ memTypeForLLVMType loc _bic lty =
16621663
, err
16631664
]
16641665

1666+
llvm_sizeof ::
1667+
Some LLVMModule ->
1668+
L.Type ->
1669+
TopLevel Integer
1670+
llvm_sizeof (Some lm) lty =
1671+
do let mtrans = modTrans lm
1672+
let ?lc = mtrans ^. Crucible.transContext . Crucible.llvmTypeCtx
1673+
let dl = Crucible.llvmDataLayout ?lc
1674+
case Crucible.liftMemType lty of
1675+
Right mty -> pure (Crucible.bytesToInteger (Crucible.memTypeSize dl mty))
1676+
Left err -> fail $ unlines
1677+
[ "llvm_sizeof: Unsupported type: " ++ show (L.ppType lty)
1678+
, "Details:"
1679+
, err
1680+
]
1681+
16651682
llvmTypeAlias :: L.Type -> Maybe Crucible.Ident
16661683
llvmTypeAlias (L.Alias i) = Just i
16671684
llvmTypeAlias _ = Nothing

src/SAWScript/Interpreter.hs

+7
Original file line numberDiff line numberDiff line change
@@ -1634,6 +1634,13 @@ primitives = Map.fromList
16341634
, "scalar argument and return types are currently supported."
16351635
]
16361636

1637+
, prim "llvm_sizeof" "LLVMModule -> LLVMType -> Int"
1638+
(funVal2 llvm_sizeof)
1639+
Current
1640+
[ "In the context of the given LLVM module, compute the size of the"
1641+
, "given LLVM type in bytes. The module determines details of struct"
1642+
, "layout and the meaning of type aliases." ]
1643+
16371644
, prim "llvm_type" "String -> LLVMType"
16381645
(funVal1 llvm_type)
16391646
Current

0 commit comments

Comments
 (0)