Skip to content

Commit

Permalink
Cairo field compatibility (#2712)
Browse files Browse the repository at this point in the history
This PR implements changes to make the `eval` command and internal
development commands fully Cairo-compatible.
* Change the default field size to Cairo field size
* Change the printing of "negative" field elements to be compatible with
the Cairo VM
* Quote function names in the Reg to CASM translation
  • Loading branch information
lukaszcz authored Apr 9, 2024
1 parent 59f9b49 commit 279db70
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 16 deletions.
19 changes: 16 additions & 3 deletions src/Juvix/Compiler/Casm/Translation/FromReg.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Juvix.Compiler.Casm.Translation.FromReg where

import Data.HashMap.Strict qualified as HashMap
import Data.HashSet qualified as HashSet
import Data.Text qualified as Text
import Juvix.Compiler.Casm.Data.LabelInfoBuilder
import Juvix.Compiler.Casm.Data.Limits
import Juvix.Compiler.Casm.Data.Result
Expand Down Expand Up @@ -38,7 +39,7 @@ fromReg tab = uncurry Result $ run $ runLabelInfoBuilderWithNextId (Reg.getNextS

mkFunCall :: Symbol -> [Instruction]
mkFunCall sym =
[ mkCallRel $ Lab $ LabelRef sym (Just $ Reg.lookupFunInfo tab sym ^. Reg.functionName),
[ mkCallRel $ Lab $ LabelRef sym (Just $ quoteName $ Reg.lookupFunInfo tab sym ^. Reg.functionName),
Return,
Nop
]
Expand All @@ -50,10 +51,22 @@ fromReg tab = uncurry Result $ run $ runLabelInfoBuilderWithNextId (Reg.getNextS
unsupported :: Text -> a
unsupported what = error ("Cairo backend: unsupported: " <> what)

quoteName :: Text -> Text
quoteName txt =
foldr
(uncurry Text.replace)
txt
[ ("$", "__dollar__"),
(":", "__colon__"),
("@", "__at__"),
("ap", "__ap__"),
("fp", "__fp__")
]

goFun :: forall r. (Member LabelInfoBuilder r) => StdlibBuiltins -> LabelRef -> (Address, [[Instruction]]) -> Reg.FunctionInfo -> Sem r (Address, [[Instruction]])
goFun blts failLab (addr0, acc) funInfo = do
let sym = funInfo ^. Reg.functionSymbol
funName = funInfo ^. Reg.functionName
funName = quoteName (funInfo ^. Reg.functionName)
registerLabelName sym funName
registerLabelAddress sym addr0
let lab = Label $ LabelRef sym (Just funName)
Expand Down Expand Up @@ -400,7 +413,7 @@ fromReg tab = uncurry Result $ run $ runLabelInfoBuilderWithNextId (Reg.getNextS
mapM_ goAssignApValue (reverse args)
output'' $ mkCallRel $ Lab $ LabelRef sym (Just funName)
where
funName = Reg.lookupFunInfo tab sym ^. Reg.functionName
funName = quoteName (Reg.lookupFunInfo tab sym ^. Reg.functionName)
Reg.CallClosure cl -> do
mapM_ goAssignApValue (reverse args)
r <- mkMemRef cl
Expand Down
16 changes: 10 additions & 6 deletions src/Juvix/Data/Field.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ newtype FField = FField
makeLenses ''FField

defaultFieldSize :: Natural
defaultFieldSize = smallFieldSize
defaultFieldSize = cairoFieldSize

cairoFieldSize :: Natural
cairoFieldSize = 3618502788666131213697322783095070105623107215331596699973092056135872020481
Expand All @@ -35,10 +35,10 @@ instance Serialize FField where
return $ fieldFromInteger n f

instance Pretty FField where
pretty (FField (_ :&: f)) = pretty (toInteger f)
pretty f = pretty (fieldToInteger f)

instance Show FField where
show (FField (_ :&: f)) = show (toInteger f)
show f = show (fieldToInteger f)

fieldAdd :: FField -> FField -> FField
fieldAdd
Expand Down Expand Up @@ -87,11 +87,15 @@ fieldSize

fieldToInteger :: FField -> Integer
fieldToInteger
(FField ((_ :: Sing (p :: Natural)) :&: (f1 :: PrimeField p))) =
toInteger f1
(FField ((n1 :: Sing (p :: Natural)) :&: (f1 :: PrimeField p)))
| i < n `div` 2 = i
| otherwise = -(n - i)
where
i = toInteger f1
n = fromIntegral (fromSing n1)

fieldToNatural :: FField -> Natural
fieldToNatural f = (fromIntegral (fieldToInteger f) + s) `mod` s
fieldToNatural f = (fromIntegral ((fieldToInteger f) + fromIntegral s)) `mod` s
where
s = fieldSize f

Expand Down
3 changes: 1 addition & 2 deletions test/Casm/Compilation/Positive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,7 @@ tests =
$(mkRelFile "out/test073.out"),
posTest
"Test074: Fields"
-- different textual format for "negative" field elements
False
True
$(mkRelDir ".")
$(mkRelFile "test074.juvix")
$(mkRelFile "out/test074.out")
Expand Down
2 changes: 1 addition & 1 deletion test/Casm/Run/Positive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ tests =
$(mkRelFile "out/test013.out"),
PosTest
"Test014: Field arithmetic"
False -- different textual representation of "negative" field elements
True
$(mkRelDir ".")
$(mkRelFile "test014.casm")
$(mkRelFile "out/test014.out")
Expand Down
2 changes: 1 addition & 1 deletion tests/Casm/Compilation/positive/out/test074.out
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2532951952066291849588125948166549073936175050732117689981164439295110414326
-1085550836599839364109196834928521031686932164599479009991927616840761606155
2 changes: 1 addition & 1 deletion tests/Casm/positive/out/test014.out
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2532951952066291849588125948166549073936175050732117689981164439295110414326
-1085550836599839364109196834928521031686932164599479009991927616840761606155
2 changes: 1 addition & 1 deletion tests/Compilation/positive/out/test074.out
Original file line number Diff line number Diff line change
@@ -1 +1 @@
214748354
-1085550836599839364109196834928521031686932164599479009991927616840761606155
2 changes: 1 addition & 1 deletion tests/Core/positive/out/test061.out
Original file line number Diff line number Diff line change
@@ -1 +1 @@
214748354
-1085550836599839364109196834928521031686932164599479009991927616840761606155

0 comments on commit 279db70

Please sign in to comment.