-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
crucible-llvm-syntax: Add type alias parser extension
- Loading branch information
1 parent
cf09fff
commit b8db06d
Showing
7 changed files
with
164 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
crucible-llvm-syntax/src/Lang/Crucible/LLVM/Syntax/TypeAlias.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
|
||
module Lang.Crucible.LLVM.Syntax.TypeAlias | ||
( TypeAlias(..) | ||
, TypeLookup(..) | ||
, aarch32LinuxTypes | ||
, x86_64LinuxTypes | ||
, typeAliasParser | ||
, typeAliasParserHooks | ||
) where | ||
|
||
import Control.Applicative ( Alternative(empty) ) | ||
import qualified Data.Map as Map | ||
import qualified Data.Text as Text | ||
|
||
import qualified Data.Parameterized.NatRepr as PN | ||
import Data.Parameterized.Some ( Some(..) ) | ||
|
||
import qualified Lang.Crucible.LLVM.MemModel as LCLM | ||
import qualified Lang.Crucible.Syntax.Atoms as LCSA | ||
import qualified Lang.Crucible.Syntax.Concrete as LCSC | ||
import qualified Lang.Crucible.Syntax.ExprParse as LCSE | ||
import qualified Lang.Crucible.Types as LCT | ||
|
||
-- | Additional types beyond those built into crucible-llvm-syntax. | ||
data TypeAlias = Byte | Int | Long | PidT | Pointer | Short | SizeT | UidT | ||
deriving (Bounded, Enum, Eq, Show) | ||
|
||
-- | Lookup function from a 'TypeAlias' to the underlying crucible type it | ||
-- represents. | ||
newtype TypeLookup = TypeLookup (TypeAlias -> (Some LCT.TypeRepr)) | ||
|
||
-- | A lookup function from 'AFE.TypeAlias' to types with the appropriate width | ||
-- on Arm32 Linux. | ||
aarch32LinuxTypes :: TypeLookup | ||
aarch32LinuxTypes = | ||
TypeLookup $ | ||
\case | ||
Byte -> Some (LCT.BVRepr (PN.knownNat @8)) | ||
Int -> Some (LCT.BVRepr (PN.knownNat @32)) | ||
Long -> Some (LCT.BVRepr (PN.knownNat @32)) | ||
PidT -> Some (LCT.BVRepr (PN.knownNat @32)) | ||
Pointer -> Some (LCLM.LLVMPointerRepr (PN.knownNat @32)) | ||
Short -> Some (LCT.BVRepr (PN.knownNat @16)) | ||
SizeT -> Some (LCT.BVRepr (PN.knownNat @32)) | ||
UidT -> Some (LCT.BVRepr (PN.knownNat @32)) | ||
|
||
-- | A lookup function from 'TypeAlias' to types with the appropriate width on | ||
-- X86_64 Linux. | ||
x86_64LinuxTypes :: TypeLookup | ||
x86_64LinuxTypes = | ||
TypeLookup $ | ||
\case | ||
Byte -> Some (LCT.BVRepr (PN.knownNat @8)) | ||
Int -> Some (LCT.BVRepr (PN.knownNat @32)) | ||
Long -> Some (LCT.BVRepr (PN.knownNat @64)) | ||
PidT -> Some (LCT.BVRepr (PN.knownNat @32)) | ||
Pointer -> Some (LCLM.LLVMPointerRepr (PN.knownNat @64)) | ||
Short -> Some (LCT.BVRepr (PN.knownNat @16)) | ||
SizeT -> Some (LCT.BVRepr (PN.knownNat @64)) | ||
UidT -> Some (LCT.BVRepr (PN.knownNat @32)) | ||
|
||
-- | Parser for type extensions to Crucible syntax | ||
typeMapParser :: | ||
LCSE.MonadSyntax LCSA.Atomic m => | ||
-- | A mapping from type names to the crucible types they represent | ||
Map.Map LCSA.AtomName (Some LCT.TypeRepr) -> | ||
m (Some LCT.TypeRepr) | ||
typeMapParser types = do | ||
name <- LCSC.atomName | ||
case Map.lookup name types of | ||
Just someTypeRepr -> return someTypeRepr | ||
Nothing -> empty | ||
|
||
-- | Parser for type extensions to Crucible syntax | ||
typeAliasParser :: | ||
LCSE.MonadSyntax LCSA.Atomic m => | ||
TypeLookup -> | ||
m (Some LCT.TypeRepr) | ||
typeAliasParser (TypeLookup lookupFn) = | ||
typeMapParser $ | ||
Map.fromList | ||
[ (LCSA.AtomName (Text.pack (show t)), lookupFn t) | ||
| t <- [minBound..maxBound] | ||
] | ||
|
||
typeAliasParserHooks :: TypeLookup -> LCSC.ParserHooks ext | ||
typeAliasParserHooks lookupFn = | ||
LCSC.ParserHooks | ||
{ LCSC.extensionTypeParser = typeAliasParser lookupFn | ||
, LCSC.extensionParser = empty | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
(defun @test-type () Unit | ||
(start start: | ||
(let byte (the Byte (bv 8 0))) | ||
(let int (the Int (bv 32 0))) | ||
(let long (the Long (bv 64 0))) | ||
(let pid (the PidT (bv 32 0))) | ||
(let blk (the Nat 0)) | ||
(let off (bv 64 0)) | ||
(let p (ptr 64 blk off)) | ||
(let ptr (the Pointer p)) | ||
(let short (the Short (bv 16 0))) | ||
(let size (the SizeT (bv 64 0))) | ||
(return ()))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
(defun @test-type () Unit | ||
(start start: | ||
(let byte (the Byte (bv 8 0))) | ||
(let int (the Int (bv 32 0))) | ||
(let long (the Long (bv 64 0))) | ||
(let pid (the PidT (bv 32 0))) | ||
(let blk (the Nat 0)) | ||
(let off (bv 64 0)) | ||
(let p (ptr 64 blk off)) | ||
(let ptr (the Pointer p)) | ||
(let short (the Short (bv 16 0))) | ||
(let size (the SizeT (bv 64 0))) | ||
(return ()))) | ||
|
||
test-type | ||
%0 | ||
% 3:15 | ||
$0 = bVLit(8, BV 0) | ||
% 4:14 | ||
$1 = bVLit(32, BV 0) | ||
% 5:15 | ||
$2 = bVLit(64, BV 0) | ||
% 7:14 | ||
$3 = natLit(0) | ||
% 9:16 | ||
$4 = extensionApp(pointerExpr $3 $2) | ||
% 11:16 | ||
$5 = bVLit(16, BV 0) | ||
% 13:13 | ||
$6 = emptyApp() | ||
% 13:5 | ||
return $6 | ||
% no postdom |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters