Skip to content
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

Feature/int widths #96

Draft
wants to merge 18 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions grin/src/AbstractInterpretation/CreatedBy/CodeGenBase.hs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ codeGenBlock_ = fmap snd . codeGenBlock
codeGenSimpleType :: SimpleType -> CG IR.Reg
codeGenSimpleType = \case
T_Unit -> newRegWithSimpleType (-1)
T_Int64 -> newRegWithSimpleType (-2)
T_Word64 -> newRegWithSimpleType (-3)
-- T_Int width -> newRegWithSimpleType (-2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@csabahruska do you have an existing plan how to implement this in these abstract interpretations?

Copy link
Author

@s5bug s5bug Apr 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering a bit while I was writing this why a mapping from types to registers wasn't handled by Data.Map or something similar. Maybe assigning a unique type to the next available register would work?

-- T_Word width -> newRegWithSimpleType (-3)
T_Float -> newRegWithSimpleType (-4)
T_Bool -> newRegWithSimpleType (-5)
T_String -> newRegWithSimpleType (-6)
Expand Down
4 changes: 2 additions & 2 deletions grin/src/AbstractInterpretation/HeapPointsTo/CodeGen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class (Typeable a, Enum a, Bounded a) => IntValue a where
codegenSimpleType :: SimpleType -> IR.SimpleType
codegenSimpleType = \case
T_Unit -> -1
T_Int64 -> -2
T_Word64 -> -3
-- T_Int w -> -2
-- T_Word w -> -3
T_Float -> -4
T_Bool -> -5
T_String -> -6
Expand Down
4 changes: 2 additions & 2 deletions grin/src/AbstractInterpretation/HeapPointsTo/CodeGenBase.hs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ codeGenBlock_ = fmap snd . codeGenBlock
codeGenSimpleType :: SimpleType -> CG IR.Reg
codeGenSimpleType = \case
T_Unit -> newRegWithSimpleType (-1)
T_Int64 -> newRegWithSimpleType (-2)
T_Word64 -> newRegWithSimpleType (-3)
-- T_Int width -> newRegWithSimpleType (-2)
-- T_Word width -> newRegWithSimpleType (-3)
T_Float -> newRegWithSimpleType (-4)
T_Bool -> newRegWithSimpleType (-5)
T_String -> newRegWithSimpleType (-6)
Expand Down
8 changes: 4 additions & 4 deletions grin/src/AbstractInterpretation/HeapPointsTo/Result.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import AbstractInterpretation.HeapPointsTo.CodeGenBase (HPTMapping)
type Loc = Int

data SimpleType
= T_Int64
| T_Word64
= T_Int Word32
| T_Word Word32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use Word32 as a parameter to define the concrete width of these types. Some ADT which is better aligned with the LLVM types would be better.

Copy link
Author

@s5bug s5bug Apr 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally used Word but llvm-hs-pure uses Word32; I got compile errors trying to use Word because of that. However, the maximum width in the LLVM spec is (2^24) - 1 bits.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah got it, if LLVM-hs uses in the API than no objections from my side anymore :)

| T_Float
| T_Bool
| T_Unit
Expand Down Expand Up @@ -99,8 +99,8 @@ concat <$> mapM makeLenses [''NodeSet, ''TypeSet, ''HPTResult]
toSimpleType :: IR.SimpleType -> SimpleType
toSimpleType = \case
-1 -> T_Unit
-2 -> T_Int64
-3 -> T_Word64
-- -2 -> T_Int w
-- -3 -> T_Word w
-4 -> T_Float
-5 -> T_Bool
-6 -> T_String
Expand Down
4 changes: 2 additions & 2 deletions grin/src/AbstractInterpretation/LiveVariable/CodeGenBase.hs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ codeGenBlock_ = fmap snd . codeGenBlock
codeGenSimpleType :: SimpleType -> CG IR.Reg
codeGenSimpleType = \case
T_Unit -> newRegWithSimpleType (-1)
T_Int64 -> newRegWithSimpleType (-2)
T_Word64 -> newRegWithSimpleType (-3)
-- T_Int w -> newRegWithSimpleType (-2)
-- T_Word w -> newRegWithSimpleType (-3)
T_Float -> newRegWithSimpleType (-4)
T_Bool -> newRegWithSimpleType (-5)
T_String -> newRegWithSimpleType (-6)
Expand Down
8 changes: 4 additions & 4 deletions grin/src/AbstractInterpretation/Model.hs
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,8 @@ instance ToTypeSet Val where typeSet = litOrConstTagNodeToTypeSet

typeOfLiteral :: Lit -> SimpleType
typeOfLiteral = \case
LInt64 _ -> T_Int64
LWord64 _ -> T_Word64
LInt w _ -> T_Int w
LWord w _ -> T_Word w
LFloat _ -> T_Float
LBool _ -> T_Bool

Expand Down Expand Up @@ -591,9 +591,9 @@ primitive name = case name of
"_prim_bool_ne" -> op [bool, bool] bool
_ -> Nothing
where
int = T_Int64
int = T_Int 64
bool = T_Bool
word = T_Word64
word = T_Word 64
unit = T_Unit
float = T_Float

Expand Down
10 changes: 8 additions & 2 deletions grin/src/Grin/ExtendedSyntax/Parse/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,16 @@ value = Lit <$> literal <|>
try (Unit <$ op "()") <|>
Undefined <$> parens (kw "#undefined" *> op "::" *> typeAnnot)

wordLiteral :: Parser Lit
wordLiteral = lexeme (LWord <$> (integer <* C.char 'u') <*> L.decimal)

intLiteral :: Parser Lit
intLiteral = lexeme (LInt <$> (signedInteger <* C.char 'i') <*> L.decimal)

literal :: Parser Lit
literal = (try $ LFloat . realToFrac <$> signedFloat) <|>
(try $ LWord64 . fromIntegral <$> lexeme (L.decimal <* C.char 'u')) <|>
(try $ LInt64 . fromIntegral <$> signedInteger) <|>
(try $ wordLiteral) <|>
(try $ intLiteral) <|>
(try $ LBool <$> (True <$ kw "#True" <|> False <$ kw "#False")) <|>
(try $ LString <$> lexeme (C.char '#' *> quotedString)) <|>
(try $ LChar <$> lexeme (C.string "#'" *> (escaped <|> anySingle) <* C.char '\''))
Expand Down
4 changes: 2 additions & 2 deletions grin/src/Grin/ExtendedSyntax/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ isExternalName es n = n `Prelude.elem` (eName <$> es)
-- QUESTION: Now #undefined can be pattern matched on.
-- Should the linter warn about this?
data Lit
= LInt64 Int64
| LWord64 Word64
= LInt Word32 Int
| LWord Word32 Word
| LFloat Float
| LBool Bool
| LString Text
Expand Down
10 changes: 8 additions & 2 deletions grin/src/Grin/Parse/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,16 @@ value = Unit <$ op "()" <|>
ValTag <$> tag <|>
simpleValue

wordLiteral :: Parser Lit
wordLiteral = lexeme (LWord <$> (integer <* C.char 'u') <*> L.decimal)

intLiteral :: Parser Lit
intLiteral = lexeme (LInt <$> (signedInteger <* C.char 'i') <*> L.decimal)

literal :: Parser Lit
literal = (try $ LFloat . realToFrac <$> signedFloat) <|>
(try $ LWord64 . fromIntegral <$> lexeme (L.decimal <* C.char 'u')) <|>
(try $ LInt64 . fromIntegral <$> signedInteger) <|>
(try $ wordLiteral) <|>
(try $ intLiteral) <|>
(try $ LBool <$> (True <$ kw "#True" <|> False <$ kw "#False")) <|>
(try $ LString <$> lexeme (C.char '#' *> quotedString)) <|>
(try $ LChar <$> lexeme (C.string "#'" *> (escaped <|> anySingle) <* C.char '\''))
Expand Down
6 changes: 3 additions & 3 deletions grin/src/Grin/Parse/Basic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ keywords = Set.fromList
] `Set.union` simpleTypes

simpleTypes = Set.fromList
[ "T_Int64", "T_Word64", "T_Float"
[ "T_Int", "T_Word", "T_Float"
, "T_Bool", "T_Unit"
, "T_Location", "T_Dead"
, "T_String", "T_Char"
Expand Down Expand Up @@ -140,8 +140,8 @@ tag = Tag C <$ char 'C' <*> var <|>

simpleType :: Parser SimpleType
simpleType =
T_Int64 <$ kw "T_Int64" <|>
T_Word64 <$ kw "T_Word64" <|>
T_Int <$ kw "T_Int" <*> integer <|>
T_Word <$ kw "T_Word" <*> integer <|>
T_Float <$ kw "T_Float" <|>
T_Bool <$ kw "T_Bool" <|>
T_Unit <$ kw "T_Unit" <|>
Expand Down
12 changes: 6 additions & 6 deletions grin/src/Grin/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ instance Pretty Val where

instance Pretty Lit where
pretty = \case
LInt64 a -> integer $ fromIntegral a
LWord64 a -> integer (fromIntegral a) <> text "u"
LFloat a -> float a
LBool a -> text "#" <> text (show a)
LString a -> text "#" <> text (show a)
LChar a -> text "#" <> text (show a)
LInt w a -> integer (fromIntegral a) <> text "i" <> integer (fromIntegral w)
LWord w a -> integer (fromIntegral a) <> text "u" <> integer (fromIntegral w)
LFloat a -> float a
LBool a -> text "#" <> text (show a)
LString a -> text "#" <> text (show a)
LChar a -> text "#" <> text (show a)

instance Pretty CPat where
pretty = \case
Expand Down
4 changes: 2 additions & 2 deletions grin/src/Grin/Research.hs
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ calcSimpleTypesAlg = \case

(_ CCTC.:< SReturnF v) ->
case v of
(Lit (LInt64 _)) -> pure $ IInfo T_Int64
(Lit (LWord64 _)) -> pure $ IInfo T_Word64
(Lit (LInt w _)) -> pure $ IInfo $ T_Int w
(Lit (LWord w _)) -> pure $ IInfo $ T_Word w
(Lit (LFloat _)) -> pure $ IInfo T_Float
(Lit (LBool _)) -> pure $ IInfo T_Bool
(Lit (LString _)) -> pure $ IInfo T_String
Expand Down
4 changes: 2 additions & 2 deletions grin/src/Grin/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ isExternalName es n = n `Prelude.elem` (eName <$> es)
-- QUESTION: Now #undefined can be pattern matched on.
-- Should the linter warn about this?
data Lit
= LInt64 Int64
| LWord64 Word64
= LInt Word32 Int
| LWord Word32 Word
| LFloat Float
| LBool Bool
| LString Text
Expand Down
5 changes: 3 additions & 2 deletions grin/src/Grin/SyntaxDefs.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module Grin.SyntaxDefs where

import Data.Text (Text, unpack)
import Data.Word
import Control.DeepSeq
import GHC.Generics (Generic)
import Data.Data
Expand Down Expand Up @@ -52,8 +53,8 @@ data Tag = Tag
type Loc = Int

data SimpleType
= T_Int64
| T_Word64
= T_Int {_width :: Word32}
| T_Word {_width :: Word32}
| T_Float
| T_Bool
| T_Unit
Expand Down
4 changes: 2 additions & 2 deletions grin/src/Grin/TypeCheck.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ typeEnvFromHPTResult :: HPTResult -> Either String TypeEnv.TypeEnv
typeEnvFromHPTResult hptResult = typeEnv where
convertSimpleType :: SimpleType -> Either String TypeEnv.SimpleType
convertSimpleType = \case
T_Int64 -> pure TypeEnv.T_Int64
T_Word64 -> pure TypeEnv.T_Word64
T_Int w -> pure $ TypeEnv.T_Int w
T_Word w -> pure $ TypeEnv.T_Word w
T_Float -> pure TypeEnv.T_Float
T_Bool -> pure TypeEnv.T_Bool
T_Unit -> pure TypeEnv.T_Unit
Expand Down
17 changes: 9 additions & 8 deletions grin/src/Grin/TypeEnv.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Data.Int
import Data.Map (Map)
import Data.Set (Set)
import Data.Vector (Vector)
import Data.Word
import qualified Data.Map as Map
import qualified Data.Set as Set
import qualified Data.Vector as Vector
Expand All @@ -34,8 +35,8 @@ dead_t = T_SimpleType T_Dead
unit_t :: Type
unit_t = T_SimpleType T_Unit

int64_t :: Type
int64_t = T_SimpleType T_Int64
int_t :: Word32 -> Type
int_t = T_SimpleType . T_Int

bool_t :: Type
bool_t = T_SimpleType T_Bool
Expand Down Expand Up @@ -113,12 +114,12 @@ typeOfLit = T_SimpleType . typeOfLitST

typeOfLitST :: Lit -> SimpleType
typeOfLitST lit = case lit of
LInt64{} -> T_Int64
LWord64{} -> T_Word64
LFloat{} -> T_Float
LBool{} -> T_Bool
LString{} -> T_String
LChar{} -> T_Char
LInt width _ -> T_Int width
LWord width _ -> T_Word width
LFloat{} -> T_Float
LBool{} -> T_Bool
LString{} -> T_String
LChar{} -> T_Char

-- Type of literal like values
typeOfVal :: Val -> Type
Expand Down
10 changes: 5 additions & 5 deletions grin/src/Reducer/LLVM/CodeGen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ toLLVM fname mod = withContext $ \ctx -> do

codeGenLit :: Lit -> CG C.Constant
codeGenLit = \case
LInt64 v -> pure $ Int {integerBits=64, integerValue=fromIntegral v}
LWord64 v -> pure $ Int {integerBits=64, integerValue=fromIntegral v}
LInt w v -> pure $ Int {integerBits=w, integerValue=fromIntegral v}
LWord w v -> pure $ Int {integerBits=w, integerValue=fromIntegral v}
LFloat v -> pure $ C.Float {floatValue=F.Single v}
LBool v -> pure $ Int {integerBits=1, integerValue=if v then 1 else 0}
LChar v -> pure $ Int {integerBits=8, integerValue=fromIntegral $ fromEnum v}
Expand Down Expand Up @@ -141,8 +141,8 @@ getCPatName :: CPat -> Grin.Name
getCPatName = \case
TagPat tag -> tagName tag
LitPat lit -> case lit of
LInt64 v -> "int_" <> showTS v
LWord64 v -> "word_" <> showTS v
LInt w v -> "int" <> showTS w <> "_" <> showTS v
LWord w v -> "word" <> showTS w <> "_" <> showTS v
LBool v -> "bool_" <> showTS v
LChar v -> "char_" <> showTS v
LString v -> error "pattern match on string is not supported"
Expand Down Expand Up @@ -587,7 +587,7 @@ runtimeErrorExternal =
external
(typeGenSimpleType T_Unit)
(mkName "__runtime_error")
[(typeGenSimpleType T_Int64, mkName "x0")]
[(typeGenSimpleType (T_Int 64), mkName "x0")]

errorBlock :: CG ()
errorBlock = do
Expand Down
33 changes: 17 additions & 16 deletions grin/src/Reducer/LLVM/PrimOps.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module Reducer.LLVM.PrimOps where

import Control.Monad (when)
import Data.Word
import LLVM.AST
import qualified LLVM.AST.IntegerPredicate as I
import qualified LLVM.AST.FloatingPointPredicate as F
Expand All @@ -17,13 +18,13 @@ import Reducer.LLVM.TypeGen
import Grin.PrimOpsPrelude


cgUnit = toCGType $ T_SimpleType T_Unit :: CGType
cgInt64 = toCGType $ T_SimpleType T_Int64 :: CGType
cgWord64 = toCGType $ T_SimpleType T_Word64 :: CGType
cgFloat = toCGType $ T_SimpleType T_Float :: CGType
cgBool = toCGType $ T_SimpleType T_Bool :: CGType
cgString = toCGType $ T_SimpleType T_String :: CGType
cgChar = toCGType $ T_SimpleType T_Char :: CGType
cgUnit = toCGType $ T_SimpleType T_Unit :: CGType
cgInt w = toCGType $ T_SimpleType $ T_Int w :: CGType
cgWord w = toCGType $ T_SimpleType $ T_Word w :: CGType
cgFloat = toCGType $ T_SimpleType T_Float :: CGType
cgBool = toCGType $ T_SimpleType T_Bool :: CGType
cgString = toCGType $ T_SimpleType T_String :: CGType
cgChar = toCGType $ T_SimpleType T_Char :: CGType

codeExternal :: Grin.External -> [Operand] -> CG Result
codeExternal e ops = case Grin.eKind e of
Expand All @@ -33,11 +34,11 @@ codeExternal e ops = case Grin.eKind e of
codeGenPrimOp :: Grin.Name -> [Operand] -> CG Result
codeGenPrimOp name [opA, opB] = pure $ case name of
-- Int
"_prim_int_add" -> I cgInt64 $ Add {nsw=False, nuw=False, operand0=opA, operand1=opB, metadata=[]}
"_prim_int_sub" -> I cgInt64 $ Sub {nsw=False, nuw=False, operand0=opA, operand1=opB, metadata=[]}
"_prim_int_mul" -> I cgInt64 $ Mul {nsw=False, nuw=False, operand0=opA, operand1=opB, metadata=[]}
"_prim_int_div" -> I cgInt64 $ SDiv {exact=False, operand0=opA, operand1=opB, metadata=[]}
"_prim_int_ashr" -> I cgInt64 $ AShr {exact=False, operand0=opA, operand1=opB, metadata=[]}
"_prim_int_add" -> I (cgInt 64) $ Add {nsw=False, nuw=False, operand0=opA, operand1=opB, metadata=[]}
"_prim_int_sub" -> I (cgInt 64) $ Sub {nsw=False, nuw=False, operand0=opA, operand1=opB, metadata=[]}
"_prim_int_mul" -> I (cgInt 64) $ Mul {nsw=False, nuw=False, operand0=opA, operand1=opB, metadata=[]}
"_prim_int_div" -> I (cgInt 64) $ SDiv {exact=False, operand0=opA, operand1=opB, metadata=[]}
"_prim_int_ashr" -> I (cgInt 64) $ AShr {exact=False, operand0=opA, operand1=opB, metadata=[]}
"_prim_int_eq" -> I cgBool $ ICmp {iPredicate=I.EQ, operand0=opA, operand1=opB, metadata=[]}
"_prim_int_ne" -> I cgBool $ ICmp {iPredicate=I.NE, operand0=opA, operand1=opB, metadata=[]}
"_prim_int_gt" -> I cgBool $ ICmp {iPredicate=I.SGT, operand0=opA, operand1=opB, metadata=[]}
Expand All @@ -46,10 +47,10 @@ codeGenPrimOp name [opA, opB] = pure $ case name of
"_prim_int_le" -> I cgBool $ ICmp {iPredicate=I.SLE, operand0=opA, operand1=opB, metadata=[]}

-- Word
"_prim_word_add" -> I cgWord64 $ Add {nsw=False, nuw=False, operand0=opA, operand1=opB, metadata=[]}
"_prim_word_sub" -> I cgWord64 $ Sub {nsw=False, nuw=False, operand0=opA, operand1=opB, metadata=[]}
"_prim_word_mul" -> I cgWord64 $ Mul {nsw=False, nuw=False, operand0=opA, operand1=opB, metadata=[]}
"_prim_word_div" -> I cgWord64 $ UDiv {exact=False, operand0=opA, operand1=opB, metadata=[]}
"_prim_word_add" -> I (cgWord 64) $ Add {nsw=False, nuw=False, operand0=opA, operand1=opB, metadata=[]}
"_prim_word_sub" -> I (cgWord 64) $ Sub {nsw=False, nuw=False, operand0=opA, operand1=opB, metadata=[]}
"_prim_word_mul" -> I (cgWord 64) $ Mul {nsw=False, nuw=False, operand0=opA, operand1=opB, metadata=[]}
"_prim_word_div" -> I (cgWord 64) $ UDiv {exact=False, operand0=opA, operand1=opB, metadata=[]}
"_prim_word_eq" -> I cgBool $ ICmp {iPredicate=I.EQ, operand0=opA, operand1=opB, metadata=[]}
"_prim_word_ne" -> I cgBool $ ICmp {iPredicate=I.NE, operand0=opA, operand1=opB, metadata=[]}
"_prim_word_gt" -> I cgBool $ ICmp {iPredicate=I.UGT, operand0=opA, operand1=opB, metadata=[]}
Expand Down
6 changes: 3 additions & 3 deletions grin/src/Reducer/LLVM/TypeGen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ stringType = ptr stringStructType

typeGenSimpleType :: SimpleType -> LLVM.Type
typeGenSimpleType = \case
T_Int64 -> i64
T_Word64 -> i64
T_Int w -> IntegerType w
T_Word w -> IntegerType w
T_Float -> float
T_Bool -> i1
T_String -> stringType
Expand All @@ -51,7 +51,7 @@ locationCGType :: CGType
locationCGType = toCGType $ T_SimpleType $ T_Location []

tagCGType :: CGType
tagCGType = toCGType $ T_SimpleType $ T_Int64
tagCGType = toCGType $ T_SimpleType $ T_Int 64

unitCGType :: CGType
unitCGType = toCGType $ T_SimpleType $ T_Unit
Expand Down
Loading