Skip to content

Commit

Permalink
Cairo field size
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszcz committed Feb 22, 2024
1 parent 24172b1 commit 17ded18
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 27 deletions.
23 changes: 14 additions & 9 deletions app/CommonOptions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Data.List.NonEmpty qualified as NonEmpty
import Juvix.Compiler.Core.Data.TransformationId.Parser qualified as Core
import Juvix.Compiler.Reg.Data.TransformationId.Parser qualified as Reg
import Juvix.Compiler.Tree.Data.TransformationId.Parser qualified as Tree
import Juvix.Data.Field
import Juvix.Data.FileExt
import Juvix.Prelude
import Options.Applicative
Expand Down Expand Up @@ -111,17 +112,21 @@ naturalNumberOpt = eitherReader aux
aux :: String -> Either String Word
aux s = maybe (Left $ s <> " is not a nonnegative number") Right (readMaybe s :: Maybe Word)

numberInOpt :: [Natural] -> ReadM Natural
numberInOpt lst = eitherReader aux
fieldSizeOpt :: ReadM (Maybe Natural)
fieldSizeOpt = eitherReader aux
where
aux :: String -> Either String Natural
aux s =
either Left checkInList $
maybe (Left $ s <> " is not a nonnegative number") Right (readMaybe s :: Maybe Natural)
aux :: String -> Either String (Maybe Natural)
aux s = case s of
"cairo" -> Right $ Just cairoFieldSize
"small" -> Right $ Just defaultFieldSize
_ ->
mapRight Just $
either Left checkAllowed $
maybe (Left $ s <> " is not a valid field size") Right (readMaybe s :: Maybe Natural)

checkInList :: Natural -> Either String Natural
checkInList n
| n `elem` lst = Right n
checkAllowed :: Natural -> Either String Natural
checkAllowed n
| n `elem` allowedFieldSizes = Right n
| otherwise = Left $ Prelude.show n <> " is not a recognized field size"

extCompleter :: FileExt -> Completer
Expand Down
16 changes: 8 additions & 8 deletions app/GlobalOptions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ data GlobalOptions = GlobalOptions
_globalNoCoverage :: Bool,
_globalNoStdlib :: Bool,
_globalUnrollLimit :: Int,
_globalFieldSize :: Natural,
_globalFieldSize :: Maybe Natural,
_globalOffline :: Bool
}
deriving stock (Eq, Show)
Expand All @@ -48,7 +48,7 @@ instance CanonicalProjection GlobalOptions Core.CoreOptions where
Core.CoreOptions
{ Core._optCheckCoverage = not _globalNoCoverage,
Core._optUnrollLimit = _globalUnrollLimit,
Core._optFieldSize = _globalFieldSize,
Core._optFieldSize = fromMaybe defaultFieldSize _globalFieldSize,
Core._optOptimizationLevel = defaultOptimizationLevel,
Core._optInliningDepth = defaultInliningDepth
}
Expand All @@ -66,7 +66,7 @@ defaultGlobalOptions =
_globalNoCoverage = False,
_globalNoStdlib = False,
_globalUnrollLimit = defaultUnrollLimit,
_globalFieldSize = defaultFieldSize,
_globalFieldSize = Nothing,
_globalOffline = False
}

Expand Down Expand Up @@ -118,10 +118,10 @@ parseGlobalFlags = do
)
_globalFieldSize <-
option
(numberInOpt allowedFieldSizes)
fieldSizeOpt
( long "field-size"
<> value defaultFieldSize
<> help ("Field type size (default: " <> show defaultFieldSize <> ")")
<> value Nothing
<> help "Field type size [cairo,small,11]"
)
_globalUnrollLimit <-
option
Expand Down Expand Up @@ -174,7 +174,7 @@ entryPointFromGlobalOptions root mainFile opts = do
_entryPointGenericOptions = project opts,
_entryPointBuildDir = maybe (def ^. entryPointBuildDir) (CustomBuildDir . Abs) mabsBuildDir,
_entryPointOffline = opts ^. globalOffline,
_entryPointFieldSize = opts ^. globalFieldSize
_entryPointFieldSize = fromMaybe defaultFieldSize $ opts ^. globalFieldSize
}
where
optBuildDir :: Maybe (Prepath Dir)
Expand All @@ -197,7 +197,7 @@ entryPointFromGlobalOptionsNoFile root opts = do
_entryPointGenericOptions = project opts,
_entryPointBuildDir = maybe (def ^. entryPointBuildDir) (CustomBuildDir . Abs) mabsBuildDir,
_entryPointOffline = opts ^. globalOffline,
_entryPointFieldSize = opts ^. globalFieldSize
_entryPointFieldSize = fromMaybe defaultFieldSize $ opts ^. globalFieldSize
}
where
optBuildDir :: Maybe (Prepath Dir)
Expand Down
2 changes: 1 addition & 1 deletion src/Juvix/Compiler/Casm/Interpreter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ runCode (LabelInfo labelInfo) instrs0 = runST goCode
initialMemSize = 1024

fsize :: Natural
fsize = maximum allowedFieldSizes
fsize = cairoFieldSize

goCode :: ST s FField
goCode = do
Expand Down
2 changes: 1 addition & 1 deletion src/Juvix/Compiler/Core/Evaluator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ defaultEvalOptions =
{ _evalOptionsNormalize = False,
_evalOptionsNoFailure = False,
_evalOptionsSilent = False,
_evalOptionsFieldSize = maximum allowedFieldSizes
_evalOptionsFieldSize = defaultFieldSize
}

data EvalError = EvalError
Expand Down
1 change: 1 addition & 0 deletions src/Juvix/Compiler/Core/Options.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Juvix.Compiler.Core.Options where

import Juvix.Compiler.Pipeline.EntryPoint
import Juvix.Data.Field
import Juvix.Prelude

data CoreOptions = CoreOptions
Expand Down
2 changes: 1 addition & 1 deletion src/Juvix/Compiler/Core/Translation/FromSource.hs
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ exprConstString = P.try $ do
exprConstField :: ParsecS r Node
exprConstField = P.try $ do
(n, i) <- field
return $ mkConstant (Info.singleton (LocationInfo i)) (ConstField (fieldFromInteger (maximum allowedFieldSizes) n))
return $ mkConstant (Info.singleton (LocationInfo i)) (ConstField (fieldFromInteger defaultFieldSize n))

exprUniverse :: ParsecS r Type
exprUniverse = do
Expand Down
3 changes: 0 additions & 3 deletions src/Juvix/Compiler/Pipeline/EntryPoint.hs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,3 @@ defaultOptimizationLevel = 1

defaultInliningDepth :: Int
defaultInliningDepth = 3

defaultFieldSize :: Natural
defaultFieldSize = maximum allowedFieldSizes
2 changes: 1 addition & 1 deletion src/Juvix/Compiler/Tree/Data/InfoTable/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ emptyInfoTable =
_infoConstrs = mempty,
_infoInductives = mempty,
_infoMainFunction = Nothing,
_infoFieldSize = maximum allowedFieldSizes
_infoFieldSize = defaultFieldSize
}

lookupFunInfo :: InfoTable' a e -> Symbol -> FunctionInfo' a e
Expand Down
2 changes: 1 addition & 1 deletion src/Juvix/Compiler/Tree/Translation/FromSource/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ constant = fieldValue <|> integerValue <|> boolValue <|> stringValue <|> unitVal
fieldValue :: ParsecS r Constant
fieldValue = P.try $ do
(i, _) <- field
return $ ConstField (fieldFromInteger (maximum allowedFieldSizes) i)
return $ ConstField (fieldFromInteger defaultFieldSize i)

integerValue :: ParsecS r Constant
integerValue = do
Expand Down
8 changes: 7 additions & 1 deletion src/Juvix/Data/Field.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ newtype FField = FField

makeLenses ''FField

cairoFieldSize :: Natural
cairoFieldSize = 3618502788666131213697322783095070105623107215331596699973092056135872020481

defaultFieldSize :: Natural
defaultFieldSize = 2147483647

allowedFieldSizes :: [Natural]
allowedFieldSizes = [11, 2147483647]
allowedFieldSizes = [11, defaultFieldSize, cairoFieldSize]

instance Serialize FField where
put f = S.put (fieldSize f, fieldToInteger f)
Expand Down
2 changes: 1 addition & 1 deletion tests/Casm/positive/out/test014.out
Original file line number Diff line number Diff line change
@@ -1 +1 @@
214748354
2532951952066291849588125948166549073936175050732117689981164439295110414326

0 comments on commit 17ded18

Please sign in to comment.