-
Notifications
You must be signed in to change notification settings - Fork 20
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
Add a concrete syntax for macaw #247
Draft
travitch
wants to merge
1
commit into
master
Choose a base branch
from
tr/macaw-syntax
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
-- | The atoms of the concrete syntax for macaw | ||
module Data.Macaw.Syntax.Atom ( | ||
Keyword(..) | ||
, keywords | ||
, AtomName(..) | ||
, Atom(..) | ||
) | ||
where | ||
|
||
import qualified Data.Map.Strict as Map | ||
import qualified Data.Text as T | ||
import Numeric.Natural ( Natural ) | ||
|
||
-- | Macaw syntax keywords | ||
-- | ||
-- These are the keywords for the *base* macaw IR (i.e., without | ||
-- architecture-specific extensions). The architecture-specific operations are | ||
-- parsed as 'AtomName's initially and resolved by architecture-specific parsers | ||
-- at the atom level. | ||
data Keyword = BVAdd | ||
| BVSub | ||
| BVMul | ||
| BVAdc | ||
| BVSbb | ||
| BVAnd | ||
| BVOr | ||
| BVXor | ||
| BVShl | ||
| BVShr | ||
| BVSar | ||
| PopCount | ||
| Bsf | ||
| Bsr | ||
| BVComplement | ||
| Mux | ||
| Lt | ||
| Le | ||
| Sle | ||
| Slt | ||
-- Syntax | ||
| Assign | ||
-- Statements | ||
| Comment | ||
| InstructionStart | ||
| WriteMemory | ||
| CondWriteMemory | ||
-- Expressions | ||
| ReadMemory | ||
-- Boolean operations | ||
| Eq_ | ||
| Not_ | ||
| And_ | ||
| Or_ | ||
| Xor_ | ||
-- Endianness | ||
| BigEndian | ||
| LittleEndian | ||
-- MemRepr | ||
| BVMemRepr | ||
-- Types | ||
| Bool_ | ||
| BV_ | ||
| Float_ | ||
| Tuple_ | ||
| Vec_ | ||
-- Values | ||
| True_ | ||
| False_ | ||
| BV | ||
| Undefined | ||
deriving (Eq, Ord, Show) | ||
|
||
-- | Uninterpreted atoms | ||
newtype AtomName = AtomText T.Text | ||
deriving (Eq, Ord, Show) | ||
|
||
data Atom = Keyword !Keyword -- ^ Keywords include all of the built-in expressions and operators | ||
| AtomName !AtomName -- ^ Non-keyword syntax atoms (to be interpreted at parse time) | ||
| Register !Natural -- ^ A numbered local register (e.g., @r12@) | ||
| Address !Natural -- ^ An arbitrary address rendered in hex ('ArchAddrWord' or 'SegoffAddr') | ||
| Integer_ !Integer -- ^ Literal integers | ||
| Natural_ !Natural -- ^ Literal naturals | ||
| String_ !T.Text -- ^ Literal strings | ||
deriving (Eq, Ord, Show) | ||
|
||
keywords :: Map.Map T.Text Keyword | ||
keywords = Map.fromList [ ("bv-add", BVAdd) | ||
, ("bv-sub", BVSub) | ||
, ("bv-mul", BVMul) | ||
, ("bv-adc", BVAdc) | ||
, ("bv-sbb", BVSbb) | ||
, ("bv-and", BVAnd) | ||
, ("bv-or", BVOr) | ||
, ("bv-xor", BVXor) | ||
, ("bv-shl", BVShl) | ||
, ("bv-shr", BVShr) | ||
, ("bv-sar", BVSar) | ||
, ("bv-complement", BVComplement) | ||
, ("popcount", PopCount) | ||
, ("bit-scan-forward", Bsf) | ||
, ("bit-scan-reverse", Bsr) | ||
, ("mux", Mux) | ||
, ("eq", Eq_) | ||
, ("<", Lt) | ||
, ("<=", Le) | ||
, ("<$", Slt) | ||
, ("<=$", Sle) | ||
, ("not", Not_) | ||
, ("and", And_) | ||
, ("or", Or_) | ||
, ("xor", Xor_) | ||
, ("Bool", Bool_) | ||
, ("BV", BV_) | ||
, ("Float", Float_) | ||
, ("Tuple", Tuple_) | ||
, ("Vec", Vec_) | ||
, ("true", True_) | ||
, ("false", False_) | ||
, ("bv", BV) | ||
, ("undefined", Undefined) | ||
, ("read-memory", ReadMemory) | ||
, (":=", Assign) | ||
, ("comment", Comment) | ||
, ("instruction-start", InstructionStart) | ||
, ("write-memory", WriteMemory) | ||
, ("cond-write-memory", CondWriteMemory) | ||
, ("big-endian", BigEndian) | ||
, ("little-endian", LittleEndian) | ||
, ("bv-mem", BVMemRepr) | ||
] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What exactly is the difference between a
Natural
and anInteger
? Is anInteger
just aNatural
with a ± sign? I ask since there are some operations that seem to require anInteger
rather than aNatural
argument, such asbv
, whose second argument must be anInteger
. Does this imply thatbv 8 +0
is legal butbv 8 0
is illegal?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't decided yet. There are a bunch of places where a negative number is not legal but where I don't want to have partial cases due to range checks on
Integer
. My current thought is that I would require a sign onInteger
(even for positive numbers), making all unsigned valuesNatural
.I think your observation about
bv
is good (and pointing out an error in the code): I would prefer that it be(bv 8 0)
(i.e., the actual value is also aNatural
).Note that this entire syntax is not really meant for human consumption at all. This will all be machine generated and machine parsed. It is only textual so that I don't go crazy debugging it, so I don't mind if it is ugly.