Skip to content

Commit 2d22993

Browse files
Improve alloc_aligned commands. (#648)
Add `crucible_alloc_readonly_aligned` command. Allow `crucible_alloc_aligned`/`crucible_alloc_readonly_aligned` to specify an alignment less than the default alignment of the type. Co-authored-by: brianhuffman <[email protected]>
1 parent bdb730d commit 2d22993

File tree

2 files changed

+51
-17
lines changed

2 files changed

+51
-17
lines changed

Diff for: src/SAWScript/Crucible/LLVM/Builtins.hs

+38-15
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ module SAWScript.Crucible.LLVM.Builtins
4646
, crucible_alloc
4747
, crucible_alloc_aligned
4848
, crucible_alloc_readonly
49+
, crucible_alloc_readonly_aligned
4950
, crucible_alloc_with_size
5051
, crucible_alloc_global
5152
, crucible_fresh_expanded_val
@@ -1485,10 +1486,10 @@ crucible_alloc_with_mutability_and_size mut sz alignment bic opts lty =
14851486
alignment' <-
14861487
case alignment of
14871488
Just a -> do
1488-
when (a < memTyAlign) $ fail $ unlines
1489-
[ "User error: manually-specified alignment was less than needed"
1489+
when (a < memTyAlign) $ liftIO $ printOutLn opts Info $ unlines
1490+
[ "Warning: manually-specified alignment was less than default for type"
14901491
, "Allocation type: " ++ show memTy
1491-
, "Minimum alignment for type: " ++ show (Crucible.fromAlignment memTyAlign) ++ "-byte"
1492+
, "Default alignment for type: " ++ show (Crucible.fromAlignment memTyAlign) ++ "-byte"
14921493
, "Specified alignment: " ++ show (Crucible.fromAlignment a) ++ "-byte"
14931494
]
14941495
pure a
@@ -1517,28 +1518,50 @@ crucible_alloc_aligned ::
15171518
Int ->
15181519
L.Type ->
15191520
LLVMCrucibleSetupM (AllLLVM SetupValue)
1520-
crucible_alloc_aligned bic opts n lty =
1521+
crucible_alloc_aligned =
1522+
crucible_alloc_aligned_with_mutability Crucible.Mutable
1523+
1524+
crucible_alloc_readonly ::
1525+
BuiltinContext ->
1526+
Options ->
1527+
L.Type ->
1528+
LLVMCrucibleSetupM (AllLLVM SetupValue)
1529+
crucible_alloc_readonly =
1530+
crucible_alloc_with_mutability_and_size Crucible.Immutable Nothing Nothing
1531+
1532+
crucible_alloc_readonly_aligned ::
1533+
BuiltinContext ->
1534+
Options ->
1535+
Int ->
1536+
L.Type ->
1537+
LLVMCrucibleSetupM (AllLLVM SetupValue)
1538+
crucible_alloc_readonly_aligned =
1539+
crucible_alloc_aligned_with_mutability Crucible.Immutable
1540+
1541+
crucible_alloc_aligned_with_mutability ::
1542+
Crucible.Mutability ->
1543+
BuiltinContext ->
1544+
Options ->
1545+
Int ->
1546+
L.Type ->
1547+
LLVMCrucibleSetupM (AllLLVM SetupValue)
1548+
crucible_alloc_aligned_with_mutability mut bic opts n lty =
15211549
case Crucible.toAlignment (Crucible.toBytes n) of
15221550
Nothing ->
1523-
LLVMCrucibleSetupM $ fail $
1524-
"crucible_alloc_aligned: invalid non-power-of-2 alignment: " ++ show n
1551+
LLVMCrucibleSetupM $ fail $ unwords
1552+
[ "crucible_alloc_aligned/crucible_alloc_readonly_aligned:"
1553+
, "invalid non-power-of-2 alignment:"
1554+
, show n
1555+
]
15251556
Just alignment ->
15261557
crucible_alloc_with_mutability_and_size
1527-
Crucible.Mutable
1558+
mut
15281559
Nothing
15291560
(Just alignment)
15301561
bic
15311562
opts
15321563
lty
15331564

1534-
crucible_alloc_readonly ::
1535-
BuiltinContext ->
1536-
Options ->
1537-
L.Type ->
1538-
LLVMCrucibleSetupM (AllLLVM SetupValue)
1539-
crucible_alloc_readonly =
1540-
crucible_alloc_with_mutability_and_size Crucible.Immutable Nothing Nothing
1541-
15421565
crucible_alloc_with_size ::
15431566
BuiltinContext ->
15441567
Options ->

Diff for: src/SAWScript/Interpreter.hs

+13-2
Original file line numberDiff line numberDiff line change
@@ -1816,8 +1816,7 @@ primitives = Map.fromList
18161816
[ "Declare that a memory region of the given type should be allocated in"
18171817
, "a Crucible specification, and also specify that the start of the region"
18181818
, "should be aligned to a multiple of the specified number of bytes (which"
1819-
, "must be a power of 2). The specified alignment must be no less than the"
1820-
, "minimum required by the LLVM type."
1819+
, "must be a power of 2)."
18211820
]
18221821

18231822
, prim "crucible_alloc_readonly" "LLVMType -> CrucibleSetup SetupValue"
@@ -1830,6 +1829,18 @@ primitives = Map.fromList
18301829
, "read-only regions."
18311830
]
18321831

1832+
, prim "crucible_alloc_readonly_aligned" "Int -> LLVMType -> CrucibleSetup SetupValue"
1833+
(bicVal crucible_alloc_readonly_aligned)
1834+
Current
1835+
[ "Declare that a read-only memory region of the given type should be"
1836+
, "a Crucible specification, and also specify that the start of the region"
1837+
, "should be aligned to a multiple of the specified number of bytes (which"
1838+
, "must be a power of 2). The function must not attempt to write to this"
1839+
, "memory region. Unlike `crucible_alloc`/`crucible_alloc_aligned`,"
1840+
, "regions allocated with `crucible_alloc_readonly_aligned` are allowed to"
1841+
, "alias other read-only regions."
1842+
]
1843+
18331844
, prim "crucible_alloc_with_size" "Int -> LLVMType -> CrucibleSetup SetupValue"
18341845
(bicVal crucible_alloc_with_size)
18351846
Experimental

0 commit comments

Comments
 (0)