Skip to content

Commit

Permalink
crucible-llvm-syntax: Syntax for LLVM alloca statement
Browse files Browse the repository at this point in the history
  • Loading branch information
langston-barrett committed Oct 31, 2023
1 parent 7b4c01e commit cae6232
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions crucible-llvm-syntax/crucible-llvm-syntax.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ library
crucible-syntax,
mtl,
parameterized-utils >= 0.1.7,
prettyprinter,
text,
what4,

Expand Down
22 changes: 21 additions & 1 deletion crucible-llvm-syntax/src/Lang/Crucible/LLVM/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import Control.Monad.State.Strict (MonadState(..))
import Control.Monad.Writer.Strict (MonadWriter(..))
import Data.Functor ((<&>))

import Prettyprinter (pretty)

import Data.Parameterized.Context qualified as Ctx
import Data.Parameterized.Some (Some(..))

Expand All @@ -27,6 +29,8 @@ import Lang.Crucible.CFG.Reg qualified as Reg
import Lang.Crucible.CFG.Extension (IsSyntaxExtension)
import Lang.Crucible.Types (TypeRepr(..))

import Lang.Crucible.LLVM.DataLayout (Alignment)
import Lang.Crucible.LLVM.DataLayout qualified as DataLayout
import Lang.Crucible.LLVM.Extension (LLVM)
import Lang.Crucible.LLVM.Extension qualified as Ext
import Lang.Crucible.LLVM.MemModel (Mem, pattern LLVMPointerRepr)
Expand Down Expand Up @@ -76,7 +80,7 @@ llvmAtomParser ::
) =>
GlobalVar Mem ->
m (Some (Atom s))
llvmAtomParser _mvar =
llvmAtomParser mvar =
Parse.depCons Parse.atomName $
\case
Atom.AtomName "ptr" -> do
Expand Down Expand Up @@ -115,4 +119,20 @@ llvmAtomParser _mvar =
let expr = Ext.LLVM_PointerIte w b p1 p2
Some <$> Parse.freshAtom loc (Reg.EvalApp (Expr.ExtensionApp expr))

Atom.AtomName "alloca" -> do
loc <- Parse.position
(align, assign) <-
Parse.cons
parseAlign
(Parse.operands (Ctx.Empty Ctx.:> BVRepr ?ptrWidth))
let (Ctx.Empty, sz) = Ctx.decompose assign
let stmt = Ext.LLVM_Alloca ?ptrWidth mvar sz align (show (pretty loc))
Some <$> Parse.freshAtom loc (Reg.EvalExt stmt)

_ -> empty
where
parseAlign :: MonadSyntax Atomic m => m Alignment
parseAlign = do
s <- Parse.atomName
unless (s == Atom.AtomName "none") Parse.cut
pure DataLayout.noAlignment
4 changes: 4 additions & 0 deletions crucible-llvm-syntax/test-data/ptr.cbl
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@
(let off (ptr-offset 64 p))
(assert! (equal? blk0 blk) "block numbers equal")
(assert! (equal? off0 off) "offsets equal")

(let sz (bv 64 1))
(let a (alloca none sz))

(return p)))
8 changes: 7 additions & 1 deletion crucible-llvm-syntax/test-data/ptr.out.good
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
(let off (ptr-offset 64 p))
(assert! (equal? blk0 blk) "block numbers equal")
(assert! (equal? off0 off) "offsets equal")
(let sz (bv 64 1))
(let a (alloca none sz))
(return p)))

test-ptr
Expand Down Expand Up @@ -38,6 +40,10 @@ test-ptr
$10 = stringLit("offsets equal")
% 10:5
assert($9, $10)
% 11:5
% 12:13
$11 = bVLit(64, BV 1)
% 13:12
$12 = alloca crucible-llvm-syntax-test-memory $11 Alignment 0 test-data/ptr.cbl:13:12
% 15:5
return $4
% no postdom

0 comments on commit cae6232

Please sign in to comment.